1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
|
/*
This file defines the "plugins".
There is no need to provide both recorder and a player. Read-only or
write-only plugins are fine.
To create one, implement:
+ player:
void play_xxx(FILE *f,
void (*synch_init_wait)(const struct timeval *ts, void *arg),
void (*synch_wait)(const struct timeval *tv, void *arg),
void (*synch_print)(const char *buf, int len, void *arg),
void *arg, const struct timeval *cont);
You are guaranteed to be running in a thread-equivalent on your own.
*f
is the file you're reading from, don't expect it to be seekable.
void synch_init_wait(const struct timeval *ts, void *arg);
you may call this to mark the starting time of the stream. It's purely
optional, and its only use is to mention the date of the recording, if
known.
void synch_wait(const struct timeval *tv, void *arg);
call this to convey timing information. The value given is the _delay_
since the last call, not the absolute time.
void synch_print(const char *buf, int len, void *arg);
call this to write the actual output.
void *arg
please pass it to each of the functions you call.
+ recorder:
void* record_xxx_init(FILE *f, const struct timeval *tm);
Will be called exactly once per stream.
*f
is the file you're supposed to write the stream to. Don't expect it to
be seekable.
*tm
is the date of the recording, if known. tm will be a null pointer if
this information is not available -- in this case, all timing data will
be relative to 0:0.
You may allocate some memory for keeping track of the state. In this case,
return the pointer to that memory, it will be passed in every subsequent
call. It may be arbitrary data, and is never used outside your code.
void record_xxx(FILE *f, void* state, const struct timeval *tm, const char *buf, int len);
Will be called every time a new chunk of input comes.
*f
is the file we're recording to.
*state
is the pointer returned by record_xxx_init().
*tm
is the "current" time of the chunk, as an absolute value (_not_ the delay
since the last call). It usually will be measured as the real time since
the Epoch, but in some cases, it may start at 0:0.
*buf
is the chunk to be written. It won't be terminated with a \0.
len
is the length of the chunk.
void record_xxx_finish(FILE *f, void* state);
Will be called when the recording ends.
*f
is the file we're recording to.
*state
is the pointer returned by record_xxx_init().
At this point, you need to free any memory you've allocated.
*/
#include "config.h"
#ifdef HAVE_SYS_TIME_H
# include <sys/time.h>
#endif
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "export.h"
#include "formats.h"
#include "compat.h"
// ttyrec checks the byte sex on runtime, during _every_ swap. Uh oh.
#ifdef WORDS_BIGENDIAN
# define little_endian(x) ((uint32_t) ( \
(((uint32_t) (x) & (uint32_t) 0x000000ffU) << 24) | \
(((uint32_t) (x) & (uint32_t) 0x0000ff00U) << 8) | \
(((uint32_t) (x) & (uint32_t) 0x00ff0000U) >> 8) | \
(((uint32_t) (x) & (uint32_t) 0xff000000U) >> 24)))
#else
# define little_endian(x) ((uint32_t)(x))
#endif
#define tadd(t, d) {if (((t).tv_usec+=(d).tv_usec)>1000000) \
(t).tv_usec-=1000000, (t).tv_sec++; \
(t).tv_sec+=(d).tv_sec; \
}
#define tsub(t, d) {if ((signed)((t).tv_usec-=(d).tv_usec)<0) \
(t).tv_usec+=1000000, (t).tv_sec--; \
(t).tv_sec-=(d).tv_sec; \
}
#define BUFFER_SIZE 32768
/********************/
/* format: baudrate */
/********************/
static void play_baudrate(FILE *f,
void (*synch_init_wait)(const struct timeval *ts, void *arg),
void (*synch_wait)(const struct timeval *tv, void *arg),
void (*synch_print)(const char *buf, int len, void *arg),
void *arg, const struct timeval *cont)
{
char buf[BUFFER_SIZE];
size_t b;
struct timeval tv;
tv.tv_sec=0;
tv.tv_usec=200000;
while ((b=fread(buf, 1, 60, f))>0) // 2400 baud
{
synch_print(buf, b, arg);
synch_wait(&tv, arg);
}
}
static void* record_baudrate_init(FILE *f, const struct timeval *tm)
{
return 0;
}
static void record_baudrate(FILE *f, void* state, const struct timeval *tm, const char *buf, int len)
{
fwrite(buf, 1, len, f);
}
static void record_baudrate_finish(FILE *f, void* state)
{
}
/***********************/
/* pseudo-format: live */
/***********************/
static void play_live(FILE *f,
void (*synch_init_wait)(const struct timeval *ts, void *arg),
void (*synch_wait)(const struct timeval *tv, void *arg),
void (*synch_print)(const char *buf, int len, void *arg),
void *arg, const struct timeval *cont)
{
struct timeval tv, tp, tm;
char buf[BUFFER_SIZE];
int len;
if (cont)
tp=*cont;
else
{
gettimeofday(&tp, 0);
synch_init_wait(&tp, arg);
}
// using read() not fread(), we need unbuffered IO
while ((len=read(fileno(f), buf, BUFFER_SIZE))>0)
{
gettimeofday(&tv, 0);
tm=tv;
tsub(tm, tp);
synch_wait(&tm, arg);
tp=tv;
synch_print(buf, len, arg);
}
}
static void* record_live_init(FILE *f, const struct timeval *tm)
{
struct timeval *tv;
tv=malloc(sizeof(struct timeval));
gettimeofday(tv, 0);
if (tm)
tsub(*tv, *tm);
return tv;
}
static void record_live(FILE *f, void* state, const struct timeval *tm, const char *buf, int len)
{
struct timeval tv, wall;
gettimeofday(&wall, 0);
tv=*tm;
tadd(tv, *((struct timeval*)state));
tsub(tv, wall);
if (tv.tv_sec>=0 && (tv.tv_sec || tv.tv_usec)) // can't go back in time
select(0, 0, 0, 0, &tv);
else
tsub(*(struct timeval*)state, tv); // move the origin by the (negative) time skipped
fwrite(buf, 1, len, f);
fflush(f);
}
static void record_live_finish(FILE *f, void* state)
{
free(state);
}
/******************/
/* format: ttyrec */
/******************/
struct ttyrec_header
{
uint32_t sec;
uint32_t usec;
uint32_t len;
};
static void play_ttyrec(FILE *f,
void (*synch_init_wait)(const struct timeval *ts, void *arg),
void (*synch_wait)(const struct timeval *tv, void *arg),
void (*synch_print)(const char *buf, int len, void *arg),
void *arg, const struct timeval *cont)
{
char buf[BUFFER_SIZE];
size_t b,w;
struct timeval tv,tl;
int first=1;
struct ttyrec_header head;
if (cont)
{
tv=*cont;
first=0;
}
while (fread(&head, 1, sizeof(struct ttyrec_header), f)
==sizeof(struct ttyrec_header))
{
b=little_endian(head.len);
/*
printf("b=%u, time=%i.%09i\n", b, little_endian(head.sec),
little_endian(head.usec));
*/
// pre-read the first block to make the timing more accurate
if (b>BUFFER_SIZE)
w=BUFFER_SIZE;
else
w=b;
if (fread(buf, 1, w, f)<w)
goto the_end;
b-=w;
if (first)
{
tv.tv_sec=little_endian(head.sec);
tv.tv_usec=little_endian(head.usec);
synch_init_wait(&tv, arg);
first=0;
}
else
{
tl=tv;
tv.tv_sec=little_endian(head.sec);
tv.tv_usec=little_endian(head.usec);
tl.tv_sec=tv.tv_sec-tl.tv_sec;
if ((tl.tv_usec=tv.tv_usec-tl.tv_usec)<0)
{
tl.tv_usec+=1000000;
tl.tv_sec--;
}
synch_wait(&tl, arg);
}
while (w>0)
{
synch_print(buf, w, arg);
if (!b)
break;
if (b>BUFFER_SIZE)
w=BUFFER_SIZE;
else
w=b;
if (fread(buf, 1, w, f)<w)
goto the_end;
b-=w;
}
}
the_end:;
}
static void* record_ttyrec_init(FILE *f, const struct timeval *tm)
{
return 0;
}
static void record_ttyrec(FILE *f, void* state, const struct timeval *tm, const char *buf, int len)
{
struct ttyrec_header h;
h.sec=little_endian(tm->tv_sec);
h.usec=little_endian(tm->tv_usec);
h.len=little_endian(len);
fwrite(&h, 1, sizeof(h), f);
fwrite(buf, 1, len, f);
}
static void record_ttyrec_finish(FILE *f, void* state)
{
}
/***********************/
/* format: nh_recorder */
/***********************/
static uint32_t get_u32(const void *mem)
{
typedef struct __attribute__((__packed__)) { uint32_t v; } u32_unal;
uint32_t bad_endian = ((u32_unal*)mem)->v;
return little_endian(bad_endian);
}
static void play_nh_recorder(FILE *f,
void (*synch_init_wait)(const struct timeval *ts, void *arg),
void (*synch_wait)(const struct timeval *tv, void *arg),
void (*synch_print)(const char *buf, int len, void *arg),
void *arg, const struct timeval *cont)
{
char buf[BUFFER_SIZE];
int b,i,i0;
struct timeval tv;
uint32_t t,tp;
t=0;
i=b=0;
while ((b=fread(buf+b-i, 1, BUFFER_SIZE-(b-i), f))>0)
{
i0=0;
for (i=0;i<b;i++)
if (!buf[i])
{
if (i0<i)
synch_print(buf+i0, i-i0, arg);
if (i+4>b) // timestamp happened on a block boundary
{
if (b<5) // incomplete last timestamp
return;
memmove(buf+i, buf, b-i);
goto block_end;
}
else
{
tp=t;
t=get_u32(buf+i+1);
i0=i+5;
i+=4;
tv.tv_sec=(t-tp)/100;
tv.tv_usec=(t-tp)%100*10000;
synch_wait(&tv, arg);
}
}
if (i0<i)
synch_print(buf+i0, i-i0, arg);
block_end:;
// FIXME: frames shouldn't get split just because not being aligned
// to our read block
}
}
static void* record_nh_recorder_init(FILE *f, const struct timeval *tm)
{
struct timeval *tv;
tv=malloc(sizeof(struct timeval));
if (tm)
*tv=*tm;
else
tv->tv_sec=tv->tv_usec=0;
fwrite("\0\0\0\0\0", 1, 5, f);
return tv;
}
static void record_nh_recorder(FILE *f, void* state, const struct timeval *tm, const char *buf, int len)
{
int32_t i;
i=(tm->tv_sec-((struct timeval*)state)->tv_sec)*100+
(tm->tv_usec-((struct timeval*)state)->tv_usec)/10000;
fwrite(buf, 1, len, f);
fwrite("\0", 1, 1, f);
fwrite(&i, 1, 4, f);
}
static void record_nh_recorder_finish(FILE *f, void* state)
{
free(state);
}
/***********************/
/* pseudo-format: auto */
/***********************/
#define WANT(x) if (!len--) return 1; if (*buf++!=(x)) return 0;
#define SPACE while (1) { if (!len--) return 1; \
if (*buf!=' '&&*buf!='\t'&&*buf!='\n'&&*buf!='\r') break; \
buf++; }
static int is_asciicast(const char *buf)
{
int len=12;
WANT('{')
SPACE
WANT('"')
WANT('v')
WANT('e')
WANT('r')
WANT('s')
WANT('i')
WANT('o')
WANT('n')
WANT('"')
SPACE
WANT(':')
SPACE
return (!len || *buf=='1' || *buf=='2');
}
#undef WANT
#undef SPACE
static void play_auto(FILE *f,
void (*synch_init_wait)(const struct timeval *ts, void *arg),
void (*synch_wait)(const struct timeval *tv, void *arg),
void (*synch_print)(const char *buf, int len, void *arg),
void *arg, const struct timeval *cont)
{
struct ttyrec_header tth;
int len, got;
char buf[BUFFER_SIZE];
struct timeval tv;
// first, grab 12 bytes and see if it looks like a ttyrec header
got=0;
do
{
// must use unbuffered I/O here, to not spoil it for play_live()
if ((len=read(fileno(f), ((char*)&tth)+got, 12-got))<=0)
return;
got+=len;
} while (got<12);
if (little_endian(tth.usec)<1000000 &&
little_endian(tth.len)>0 && little_endian(tth.len)<65536)
{
tv.tv_sec=little_endian(tth.sec);
tv.tv_usec=little_endian(tth.usec);
synch_init_wait(&tv, arg);
got=little_endian(tth.len);
while (got>0)
{
if ((len=fread(buf, 1, (got>BUFFER_SIZE)?BUFFER_SIZE:got, f))<=0)
return;
synch_print(buf, len, arg);
got-=len;
}
play_ttyrec(f, 0, synch_wait, synch_print, arg, &tv);
return;
}
if (is_asciicast((const char*)&tth))
{
do_play_asciicast(fileno(f), (const char*)&tth, 12,
synch_init_wait, synch_wait, synch_print, arg, &tv);
return;
}
// fall back to "live"
gettimeofday(&tv, 0);
synch_init_wait(&tv, arg);
synch_print((char*)&tth, 12, arg);
play_live(f, 0, synch_wait, synch_print, arg, &tv);
}
/****************/
/* format: null */
/****************/
static void* record_null_init(FILE *f, const struct timeval *tm)
{
return 0;
}
static void record_null(FILE *f, void* state, const struct timeval *tm, const char *buf, int len)
{
}
static void record_null_finish(FILE *f, void* state)
{
}
recorder_info rec[]={
{"ansi",".txt",record_baudrate_init,record_baudrate,record_baudrate_finish},
{"ttyrec",".ttyrec",record_ttyrec_init,record_ttyrec,record_ttyrec_finish},
{"nh_recorder",".nh",record_nh_recorder_init,record_nh_recorder,record_nh_recorder_finish},
{"asciicast",".cast",record_asciicast_init,record_asciicast,record_asciicast_finish},
{"asciicast-v1",".cast-v1",record_asciicast_v1_init,record_asciicast,record_asciicast_finish},
{"live", 0, record_live_init, record_live, record_live_finish},
{"null",0,record_null_init,record_null,record_null_finish},
{0, 0, 0, 0, 0},
};
player_info play[]={
{"baudrate",".txt",play_baudrate},
{"ttyrec",".ttyrec",play_ttyrec},
{"nh_recorder",".nh",play_nh_recorder},
#if (defined HAVE_LIBZ) || (SHIPPED_LIBZ)
{"dosrecorder",".dm2",play_dosrecorder},
#endif
{"asciicast",".cast",play_asciicast},
{"live",0,play_live},
{"auto",0,play_auto},
{0, 0, 0},
};
#ifndef ARRAYSIZE
# define ARRAYSIZE(a) (sizeof(a)/sizeof(a[0]))
#endif
int rec_n=ARRAYSIZE(rec)-1;
int play_n=ARRAYSIZE(play)-1;
|