Mercurial > pt1.oyama
comparison src/recpt1.c @ 124:9c7bc6c0327e
Add DLNA server function test. (from uShare project)
author | naoyan@johnstown.minaminoshima.org |
---|---|
date | Wed, 29 Sep 2010 23:18:55 +0900 |
parents | recpt1/recpt1.c@4009737ea899 |
children | e413158cae13 |
comparison
equal
deleted
inserted
replaced
123:215a51fa3df3 | 124:9c7bc6c0327e |
---|---|
1 /* -*- tab-width: 4; indent-tabs-mode: nil -*- */ | |
2 /* vim: set ts=4 sts=4 sw=4 expandtab number : */ | |
3 #include <stdio.h> | |
4 #include <fcntl.h> | |
5 #include <sys/types.h> | |
6 #include <sys/stat.h> | |
7 #include <time.h> | |
8 #include <stdlib.h> | |
9 #include <string.h> | |
10 #include <pthread.h> | |
11 #include <math.h> | |
12 #include <unistd.h> | |
13 #include <getopt.h> | |
14 #include <signal.h> | |
15 #include <errno.h> | |
16 #include <sys/time.h> | |
17 #include <ctype.h> | |
18 #include <libgen.h> | |
19 | |
20 #include <netdb.h> | |
21 #include <arpa/inet.h> | |
22 #include <netinet/in.h> | |
23 | |
24 #include <sys/ioctl.h> | |
25 #include <pt1_ioctl.h> | |
26 | |
27 #include "config.h" | |
28 #include "decoder.h" | |
29 #include "recpt1.h" | |
30 #include "version.h" | |
31 #include "mkpath.h" | |
32 | |
33 #include <sys/ipc.h> | |
34 #include <sys/msg.h> | |
35 #include "pt1_dev.h" | |
36 #include "tssplitter_lite.h" | |
37 #include "ushare.h" | |
38 #include "trace.h" | |
39 | |
40 /* maximum write length at once */ | |
41 #define SIZE_CHANK 1316 | |
42 | |
43 /* ipc message size */ | |
44 #define MSGSZ 255 | |
45 | |
46 typedef struct pt1_msgbuf { | |
47 long mtype; | |
48 char mtext[MSGSZ]; | |
49 } pt1_message_buf; | |
50 | |
51 /* globals */ | |
52 boolean f_exit = FALSE; | |
53 | |
54 /* prototypes */ | |
55 int tune(char *channel, thread_data *tdata, char *device); | |
56 int close_tuner(thread_data *tdata); | |
57 | |
58 | |
59 /* ipc message receive */ | |
60 void * | |
61 mq_recv(void *t) | |
62 { | |
63 thread_data *tdata = (thread_data *)t; | |
64 pt1_message_buf rbuf; | |
65 char channel[16]; | |
66 int ch = 0, recsec = 0, time_to_add = 0; | |
67 | |
68 while(1) { | |
69 if(msgrcv(tdata->msqid, &rbuf, MSGSZ, 1, 0) < 0) { | |
70 return NULL; | |
71 } | |
72 | |
73 sscanf(rbuf.mtext, "ch=%s t=%d e=%d", channel, &recsec, &time_to_add); | |
74 ch = atoi(channel); | |
75 // fprintf(stderr, "ch=%d time=%d extend=%d\n", ch, recsec, time_to_add); | |
76 | |
77 if(ch && tdata->ch != ch) { | |
78 /* stop stream */ | |
79 ioctl(tdata->tfd, STOP_REC, 0); | |
80 #if 0 | |
81 /* re-initialize decoder */ | |
82 if(tdata->decoder) { | |
83 // b25_finish(tdata->decoder); | |
84 b25_shutdown(tdata->decoder); | |
85 tdata->decoder = b25_startup(tdata->dopt); | |
86 if(!tdata->decoder) { | |
87 fprintf(stderr, "Cannot start b25 decoder\n"); | |
88 fprintf(stderr, "Fall back to encrypted recording\n"); | |
89 } | |
90 } | |
91 #endif | |
92 /* tune to new channel */ | |
93 if(close_tuner(tdata) != 0) | |
94 return NULL; | |
95 | |
96 /* wait for remainder */ | |
97 while(tdata->queue->num_used > 0) { | |
98 usleep(10000); | |
99 } | |
100 | |
101 tune(channel, tdata, NULL); | |
102 | |
103 /* restart recording */ | |
104 if(ioctl(tdata->tfd, START_REC, 0) < 0) { | |
105 fprintf(stderr, "Tuner cannot start recording\n"); | |
106 return NULL; | |
107 } | |
108 } | |
109 | |
110 if(time_to_add) { | |
111 tdata->recsec += time_to_add; | |
112 fprintf(stderr, "Extended %d sec\n", time_to_add); | |
113 } | |
114 | |
115 if(recsec) { | |
116 time_t cur_time; | |
117 time(&cur_time); | |
118 if(cur_time - tdata->start_time > recsec) { | |
119 f_exit = TRUE; | |
120 } | |
121 else { | |
122 tdata->recsec = recsec; | |
123 fprintf(stderr, "Total recording time = %d sec\n", recsec); | |
124 } | |
125 } | |
126 | |
127 if(f_exit) | |
128 return NULL; | |
129 } | |
130 } | |
131 | |
132 | |
133 /* lookup frequency conversion table*/ | |
134 ISDB_T_FREQ_CONV_TABLE * | |
135 searchrecoff(char *channel) | |
136 { | |
137 int lp; | |
138 | |
139 for(lp = 0; isdb_t_conv_table[lp].parm_freq != NULL; lp++) { | |
140 /* return entry number in the table when strings match and | |
141 * lengths are same. */ | |
142 if((memcmp(isdb_t_conv_table[lp].parm_freq, channel, | |
143 strlen(channel)) == 0) && | |
144 (strlen(channel) == strlen(isdb_t_conv_table[lp].parm_freq))) { | |
145 return &isdb_t_conv_table[lp]; | |
146 } | |
147 } | |
148 return NULL; | |
149 } | |
150 | |
151 QUEUE_T * | |
152 create_queue(size_t size) | |
153 { | |
154 QUEUE_T *p_queue; | |
155 int memsize = sizeof(QUEUE_T) + size * sizeof(BUFSZ); | |
156 | |
157 p_queue = (QUEUE_T*)calloc(memsize, sizeof(char)); | |
158 | |
159 if(p_queue != NULL) { | |
160 p_queue->size = size; | |
161 p_queue->num_avail = size; | |
162 p_queue->num_used = 0; | |
163 p_queue->in = 0; | |
164 p_queue->out = 0; | |
165 pthread_mutex_init(&p_queue->mutex, NULL); | |
166 pthread_cond_init(&p_queue->cond_avail, NULL); | |
167 pthread_cond_init(&p_queue->cond_used, NULL); | |
168 } | |
169 | |
170 return p_queue; | |
171 } | |
172 | |
173 STREAM_QUEUE_T * | |
174 create_stream_queue(size_t size) | |
175 { | |
176 STREAM_QUEUE_T *p_queue; | |
177 int memsize = sizeof(STREAM_QUEUE_T) + size * sizeof(ARIB_STD_B25_BUFFER); | |
178 | |
179 p_queue = (STREAM_QUEUE_T*)calloc(memsize, sizeof(char)); | |
180 | |
181 if(p_queue != NULL) { | |
182 p_queue->size = size; | |
183 p_queue->num_avail = size; | |
184 p_queue->num_used = 0; | |
185 p_queue->in = 0; | |
186 p_queue->out = 0; | |
187 pthread_mutex_init(&p_queue->mutex, NULL); | |
188 pthread_cond_init(&p_queue->cond_avail, NULL); | |
189 pthread_cond_init(&p_queue->cond_used, NULL); | |
190 } | |
191 | |
192 return p_queue; | |
193 } | |
194 | |
195 void | |
196 destroy_queue(QUEUE_T *p_queue) | |
197 { | |
198 if(!p_queue) | |
199 return; | |
200 | |
201 pthread_mutex_destroy(&p_queue->mutex); | |
202 pthread_cond_destroy(&p_queue->cond_avail); | |
203 pthread_cond_destroy(&p_queue->cond_used); | |
204 free(p_queue); | |
205 } | |
206 | |
207 /* enqueue data. this function will block if queue is full. */ | |
208 void | |
209 enqueue(QUEUE_T *p_queue, BUFSZ *data) | |
210 { | |
211 struct timeval now; | |
212 struct timespec spec; | |
213 //fprintf (stderr, "enqueue() start.\n"); | |
214 | |
215 gettimeofday(&now, NULL); | |
216 spec.tv_sec = now.tv_sec + 1; | |
217 spec.tv_nsec = now.tv_usec * 1000; | |
218 | |
219 //fprintf (stderr, "enqueue() mutex lock try. num_used[%d] num_avail[%d]\n", p_queue->num_used, p_queue->num_avail); | |
220 pthread_mutex_lock(&p_queue->mutex); | |
221 //fprintf (stderr, "enqueue() mutex lock success. num_used[%d] num_avail[%d]\n", p_queue->num_used, p_queue->num_avail); | |
222 /* entered critical section */ | |
223 | |
224 /* wait while queue is full */ | |
225 while(p_queue->num_avail == 0) { | |
226 pthread_cond_timedwait(&p_queue->cond_avail, | |
227 &p_queue->mutex, &spec); | |
228 if(f_exit) { | |
229 pthread_mutex_unlock(&p_queue->mutex); | |
230 return; | |
231 } | |
232 } | |
233 | |
234 p_queue->buffer[p_queue->in] = data; | |
235 | |
236 /* move position marker for input to next position */ | |
237 p_queue->in++; | |
238 p_queue->in %= p_queue->size; | |
239 | |
240 /* update counters */ | |
241 p_queue->num_avail--; | |
242 p_queue->num_used++; | |
243 | |
244 /* leaving critical section */ | |
245 //fprintf (stderr, "enqueue() mutex unlock. num_used[%d]\n", p_queue->num_used); | |
246 pthread_mutex_unlock(&p_queue->mutex); | |
247 pthread_cond_signal(&p_queue->cond_used); | |
248 } | |
249 | |
250 /* | |
251 * stream_func()$B$N;HMQ$9$k(B enqueue() $B$O6u$-$,$J$$>l9g$K$O!"L58BBT$A$O$7$F$O$J$i$J$$!#(B | |
252 * $B6u$-$,$J$$>l9g$K$O!"(Bqueue$B$r=i4|2=$7$F$7$^$$!"C<Kv$X$NAw?.%G!<%?$r;E@Z$jD>$7$7$F$7$^$&J}$,%^%7!#(B | |
253 * $B$3$l$K$h$j!"C<Kv$X$NAw?.%G!<%?$O?tICJ,%9%-%C%W$9$k;v$K$J$k$,!"(B | |
254 * $B%P%C%U%!$OM-8B$G$"$j!"%9%H%j!<%`$G$"$k$N$G:F@8$5$lB3$1$k$3$H$rM%@h$9$k!#(B | |
255 */ | |
256 void | |
257 stream_enqueue(STREAM_QUEUE_T *p_queue, ARIB_STD_B25_BUFFER *data) | |
258 { | |
259 struct timeval now; | |
260 struct timespec spec; | |
261 int i; | |
262 //fprintf (stderr, "stream_enqueue() start.\n"); | |
263 | |
264 gettimeofday(&now, NULL); | |
265 spec.tv_sec = now.tv_sec + 1; | |
266 spec.tv_nsec = now.tv_usec * 1000; | |
267 | |
268 //fprintf (stderr, "stream_enqueue() mutex lock try. num_used[%d]\n", p_queue->num_used); | |
269 pthread_mutex_lock(&p_queue->mutex); | |
270 //fprintf (stderr, "stream_enqueue() mutex lock success. num_used[%d]\n", p_queue->num_used); | |
271 /* entered critical section */ | |
272 | |
273 if (p_queue->num_avail == 0) { | |
274 //fprintf (stderr, "stream_enqueue() num_used reach max[%d].\n", p_queue->num_used); | |
275 /* stream queue $B$O0lGU$K$J$C$?$i>C5n$7$F$7$^$&(B */ | |
276 for ( i=0; i < p_queue->size; i++ ) { | |
277 if ( p_queue->buffer[i] != NULL ) { | |
278 free(p_queue->buffer[i]->data); | |
279 p_queue->buffer[i]->data = NULL; | |
280 free(p_queue->buffer[i]); | |
281 p_queue->buffer[i] = NULL; | |
282 } | |
283 } | |
284 p_queue->in = 0; | |
285 p_queue->out = 0; | |
286 p_queue->num_used = 0; | |
287 p_queue->num_avail = p_queue->size; | |
288 } | |
289 | |
290 p_queue->buffer[p_queue->in] = data; | |
291 | |
292 /* move position marker for input to next position */ | |
293 p_queue->in++; | |
294 p_queue->in %= p_queue->size; | |
295 | |
296 /* update counters */ | |
297 p_queue->num_avail--; | |
298 p_queue->num_used++; | |
299 | |
300 /* leaving critical section */ | |
301 //fprintf (stderr, "stream_enqueue() mutex unlock.\n"); | |
302 pthread_mutex_unlock(&p_queue->mutex); | |
303 pthread_cond_signal(&p_queue->cond_used); | |
304 } | |
305 | |
306 /* dequeue data. this function will block if queue is empty. */ | |
307 BUFSZ * | |
308 dequeue(QUEUE_T *p_queue) | |
309 { | |
310 struct timeval now; | |
311 struct timespec spec; | |
312 BUFSZ *buffer; | |
313 | |
314 //fprintf (stderr, "dequeue() start.\n"); | |
315 gettimeofday(&now, NULL); | |
316 spec.tv_sec = now.tv_sec + 1; | |
317 spec.tv_nsec = now.tv_usec * 1000; | |
318 | |
319 //fprintf (stderr, "dequeue() mutex lock try. num_used[%d]\n", p_queue->num_used); | |
320 pthread_mutex_lock(&p_queue->mutex); | |
321 //fprintf (stderr, "dequeue() mutex lock success. num_used[%d]\n", p_queue->num_used); | |
322 /* entered the critical section*/ | |
323 | |
324 /* wait while queue is empty */ | |
325 while(p_queue->num_used == 0) { | |
326 pthread_cond_timedwait(&p_queue->cond_used, | |
327 &p_queue->mutex, &spec); | |
328 if(f_exit) { | |
329 pthread_mutex_unlock(&p_queue->mutex); | |
330 return NULL; | |
331 } | |
332 } | |
333 | |
334 /* take buffer address */ | |
335 buffer = p_queue->buffer[p_queue->out]; | |
336 | |
337 /* move position marker for output to next position */ | |
338 p_queue->out++; | |
339 p_queue->out %= p_queue->size; | |
340 | |
341 /* update counters */ | |
342 p_queue->num_avail++; | |
343 p_queue->num_used--; | |
344 | |
345 /* leaving the critical section */ | |
346 //fprintf (stderr, "dequeue() mutex unlock.\n"); | |
347 pthread_mutex_unlock(&p_queue->mutex); | |
348 pthread_cond_signal(&p_queue->cond_avail); | |
349 | |
350 return buffer; | |
351 } | |
352 | |
353 ARIB_STD_B25_BUFFER * | |
354 stream_dequeue(STREAM_QUEUE_T *p_queue) | |
355 { | |
356 struct timeval now; | |
357 struct timespec spec; | |
358 ARIB_STD_B25_BUFFER *buffer; | |
359 | |
360 //fprintf (stderr, "stream_dequeue() start.\n"); | |
361 gettimeofday(&now, NULL); | |
362 spec.tv_sec = now.tv_sec + 1; | |
363 spec.tv_nsec = now.tv_usec * 1000; | |
364 | |
365 //fprintf (stderr, "stream_dequeue() mutex lock try. num_used[%d]\n", p_queue->num_used); | |
366 pthread_mutex_lock(&p_queue->mutex); | |
367 //fprintf (stderr, "stream_dequeue() mutex lock success. num_used[%d]\n", p_queue->num_used); | |
368 /* entered the critical section*/ | |
369 | |
370 /* wait while queue is empty */ | |
371 while(p_queue->num_used == 0) { | |
372 pthread_cond_timedwait(&p_queue->cond_used, | |
373 &p_queue->mutex, &spec); | |
374 if(f_exit) { | |
375 pthread_mutex_unlock(&p_queue->mutex); | |
376 return NULL; | |
377 } | |
378 } | |
379 | |
380 /* take buffer address */ | |
381 buffer = p_queue->buffer[p_queue->out]; | |
382 | |
383 /* move position marker for output to next position */ | |
384 p_queue->out++; | |
385 p_queue->out %= p_queue->size; | |
386 | |
387 /* update counters */ | |
388 p_queue->num_avail++; | |
389 p_queue->num_used--; | |
390 | |
391 /* leaving the critical section */ | |
392 pthread_mutex_unlock(&p_queue->mutex); | |
393 //fprintf (stderr, "stream_dequeue() mutex unlock.\n"); | |
394 pthread_cond_signal(&p_queue->cond_avail); | |
395 //fprintf (stderr, "dequeue() finish.\n"); | |
396 | |
397 return buffer; | |
398 } | |
399 | |
400 /* this function will be reader thread */ | |
401 void * | |
402 reader_func(void *p) | |
403 { | |
404 thread_data *data = (thread_data *)p; | |
405 QUEUE_T *p_queue = data->queue; | |
406 decoder *dec = data->decoder; | |
407 splitter *splitter = data->splitter; | |
408 int wfd = data->wfd; | |
409 boolean use_b25 = dec ? TRUE : FALSE; | |
410 boolean use_udp = data->sock_data ? TRUE : FALSE; | |
411 boolean fileless = FALSE; | |
412 boolean use_splitter = splitter ? TRUE : FALSE; | |
413 boolean use_streaming = TRUE; // $BK\Ev$O0z?t$K$9$k$3$H(B | |
414 int sfd = -1; | |
415 pthread_t signal_thread = data->signal_thread; | |
416 struct sockaddr_in *addr = NULL; | |
417 BUFSZ *qbuf; | |
418 ARIB_STD_B25_BUFFER *eqbuf; | |
419 splitbuf_t splitbuf; | |
420 ARIB_STD_B25_BUFFER sbuf, dbuf, buf; | |
421 int code; | |
422 int split_select_finish = TSS_ERROR; | |
423 | |
424 buf.size = 0; | |
425 buf.data = NULL; | |
426 splitbuf.size = 0; | |
427 | |
428 if(wfd == -1) | |
429 fileless = TRUE; | |
430 | |
431 if(use_udp) { | |
432 sfd = data->sock_data->sfd; | |
433 addr = &data->sock_data->addr; | |
434 } | |
435 | |
436 while(1) { | |
437 // fprintf (stderr, "reader_func() while loop\n"); | |
438 ssize_t wc = 0; | |
439 int file_err = 0; | |
440 //fprintf (stderr, "reader_func() dequeue() start.\n"); | |
441 qbuf = dequeue(p_queue); | |
442 //fprintf (stderr, "reader_func() dequeue() finish.\n"); | |
443 /* no entry in the queue */ | |
444 if(qbuf == NULL) { | |
445 break; | |
446 } | |
447 | |
448 sbuf.data = qbuf->buffer; | |
449 sbuf.size = qbuf->size; | |
450 | |
451 buf = sbuf; /* default */ | |
452 | |
453 if(use_b25) { | |
454 code = b25_decode(dec, &sbuf, &dbuf); | |
455 if(code < 0) { | |
456 fprintf(stderr, "b25_decode failed (code=%d). fall back to encrypted recording.\n", code); | |
457 use_b25 = FALSE; | |
458 } | |
459 else | |
460 buf = dbuf; | |
461 } | |
462 | |
463 | |
464 if(use_splitter) { | |
465 splitbuf.size = 0; | |
466 | |
467 while(buf.size) { | |
468 /* $BJ,N%BP>](BPID$B$NCj=P(B */ | |
469 if(split_select_finish != TSS_SUCCESS) { | |
470 split_select_finish = split_select(splitter, &buf); | |
471 if(split_select_finish == TSS_NULL) { | |
472 /* malloc$B%(%i!<H/@8(B */ | |
473 fprintf(stderr, "split_select malloc failed\n"); | |
474 use_splitter = FALSE; | |
475 goto fin; | |
476 } | |
477 else if(split_select_finish != TSS_SUCCESS) { | |
478 /* $BJ,N%BP>](BPID$B$,40A4$KCj=P$G$-$k$^$G=PNO$7$J$$(B | |
479 * 1$BICDxEYM>M5$r8+$k$H$$$$$+$b(B | |
480 */ | |
481 time_t cur_time; | |
482 time(&cur_time); | |
483 if(cur_time - data->start_time > 4) { | |
484 use_splitter = FALSE; | |
485 goto fin; | |
486 } | |
487 break; | |
488 } | |
489 } | |
490 /* $BJ,N%BP>]0J30$r$U$k$$Mn$H$9(B */ | |
491 code = split_ts(splitter, &buf, &splitbuf); | |
492 if(code != TSS_SUCCESS) { | |
493 fprintf(stderr, "split_ts failed\n"); | |
494 break; | |
495 } | |
496 | |
497 break; | |
498 } /* while */ | |
499 | |
500 buf.size = splitbuf.size; | |
501 buf.data = splitbuf.buffer; | |
502 fin: | |
503 ; | |
504 } /* if */ | |
505 | |
506 /* | |
507 * 2. reader_func$B2~B$E@(B | |
508 * 2.1 tdata->p_queue $B$+$i(B dequeue() $B$7$F%9%H%j!<%`$J%G!<%?$r<hF@$9$k(B | |
509 * 2.1.1 dequeue()$B$O(B tdata->p_queue->mutex $B$r(B lock/unlock $B$7$FFI$_9~$_;~$NF1;~99?7$rKI;_$7$F$$$k(B | |
510 * 2.2 tdata->stream_queue $B$K(B enqueue() $B$r<B9T$7$F(B http_stream $B$NBg85$H$9$k%G!<%?%P%C%U%!$N%3%T!<$r<B;\(B | |
511 * 2.2.1 http_stream$B$N%3%T!<85$H$9$k$?$a$N%P%C%U%!$r(Balloc$B$9$k(B | |
512 * 2.2.2 2.2.1$B$G(Balloc$B$7$?NN0h$K(B rader_func $B$,(B dequeue $B$7$?%9%H%j!<%`$J%G!<%?$r(Bmemcpy()$B$9$k(B | |
513 * 2.2.3 tdata->stream_queue $B$K(B 2.2.1 $B$N%]%$%s%?$r(B enqueue() $B$9$k(B | |
514 * 2.2.3.1 enqueue() $B$O(B tdata->stream_queue->mutex $B$r(B lock/unlock $B$7$F=q$-9~$_;~$NF1;~99?7$rKI;_$7$F$$$k(B | |
515 */ | |
516 //fprintf (stderr, "reader_func() buf.size[%d]\n", buf.size); | |
517 if ( use_streaming && buf.size > 0 ) { | |
518 do { | |
519 eqbuf = malloc(sizeof(ARIB_STD_B25_BUFFER)); | |
520 if ( eqbuf == NULL ) { | |
521 fprintf (stderr, "Cannot malloc eqbuf memory. streaming abort.\n"); | |
522 use_streaming = FALSE; | |
523 break; | |
524 } | |
525 eqbuf->data = malloc(buf.size); | |
526 if ( eqbuf->data == NULL ) { | |
527 fprintf (stderr, "Cannot malloc eqbuf memory. streaming abort.\n"); | |
528 use_streaming = FALSE; | |
529 break; | |
530 } | |
531 eqbuf->size = buf.size; | |
532 memcpy(eqbuf->data, buf.data, buf.size); | |
533 // $B$3$A$i$b0n$l$?$i>C5n$7$F$7$^$&(B stream_enqueue() $B$r;HMQ(B | |
534 stream_enqueue(data->stream_queue, eqbuf); | |
535 } while(0); | |
536 } | |
537 | |
538 if(!fileless) { | |
539 /* write data to output file */ | |
540 int size_remain = buf.size; | |
541 int offset = 0; | |
542 | |
543 while(size_remain > 0) { | |
544 int ws = size_remain < SIZE_CHANK ? size_remain : SIZE_CHANK; | |
545 | |
546 wc = write(wfd, buf.data + offset, ws); | |
547 if(wc < 0) { | |
548 perror("write"); | |
549 file_err = 1; | |
550 pthread_kill(signal_thread, | |
551 errno == EPIPE ? SIGPIPE : SIGUSR2); | |
552 break; | |
553 } | |
554 size_remain -= wc; | |
555 offset += wc; | |
556 } | |
557 } | |
558 | |
559 if(use_udp && sfd != -1) { | |
560 /* write data to socket */ | |
561 int size_remain = buf.size; | |
562 int offset = 0; | |
563 while(size_remain > 0) { | |
564 int ws = size_remain < SIZE_CHANK ? size_remain : SIZE_CHANK; | |
565 wc = write(sfd, buf.data + offset, ws); | |
566 if(wc < 0) { | |
567 if(errno == EPIPE) | |
568 pthread_kill(signal_thread, SIGPIPE); | |
569 break; | |
570 } | |
571 size_remain -= wc; | |
572 offset += wc; | |
573 } | |
574 } | |
575 | |
576 free(qbuf); | |
577 qbuf = NULL; | |
578 | |
579 /* normal exit */ | |
580 if((f_exit && !p_queue->num_used) || file_err) { | |
581 | |
582 buf = sbuf; /* default */ | |
583 | |
584 if(use_b25) { | |
585 code = b25_finish(dec, &sbuf, &dbuf); | |
586 if(code < 0) | |
587 fprintf(stderr, "b25_finish failed\n"); | |
588 else | |
589 buf = dbuf; | |
590 } | |
591 | |
592 if(use_splitter) { | |
593 /* $BJ,N%BP>]0J30$r$U$k$$Mn$H$9(B */ | |
594 code = split_ts(splitter, &buf, &splitbuf); | |
595 if(code != TSS_SUCCESS) { | |
596 break; | |
597 } | |
598 | |
599 buf.data = splitbuf.buffer; | |
600 buf.size = splitbuf.size; | |
601 } | |
602 | |
603 if(!fileless && !file_err) { | |
604 wc = write(wfd, buf.data, buf.size); | |
605 if(wc < 0) { | |
606 perror("write"); | |
607 file_err = 1; | |
608 pthread_kill(signal_thread, | |
609 errno == EPIPE ? SIGPIPE : SIGUSR2); | |
610 } | |
611 } | |
612 | |
613 if(use_udp && sfd != -1) { | |
614 wc = write(sfd, buf.data, buf.size); | |
615 if(wc < 0) { | |
616 if(errno == EPIPE) | |
617 pthread_kill(signal_thread, SIGPIPE); | |
618 } | |
619 } | |
620 | |
621 break; | |
622 } | |
623 } | |
624 | |
625 time_t cur_time; | |
626 time(&cur_time); | |
627 fprintf(stderr, "Recorded %dsec\n", | |
628 (int)(cur_time - data->start_time)); | |
629 | |
630 return NULL; | |
631 } | |
632 | |
633 /* | |
634 * 3. stream_func() $B$O(B reader_func() $B$+$i%9%H%j!<%`8~$1$K%3%T!<$5$l$?%G!<%?$r!"%9%H%j!<%`%;%C%7%g%sKh$N(Bqueue$B$K%3%T!<$9$k(B | |
635 * 3.1 tdata->stream_queue $B$+$i(B dequeue $B$9$k(B | |
636 * 3.1.1 tdata->stream_queue->mutex $B$NFI$_9~$_;~$N(B lock/unlokc $B$,H/@8$9$k(B | |
637 * 3.2 tdata->streamer->mutex $B$r(B lock | |
638 * 3.3 $B0J2<$r(B tdata->streamer->stream_nr $B$N?t$@$1%k!<%W(B | |
639 * 3.3.1 tdata->streamer->stream_session[N]->is_valid $B$,M-8z$+3NG'(B | |
640 * 3.3.2 tdata->streamer->stream_session[N]->p_queue $B$X$N%3%T!<MQ%P%C%U%!$N(Balloc | |
641 * 3.3.3 3.1$B$G(B dequeue $B$7$?%P%C%U%!$r(B3.3.2$B$G(B alloc $B$7$?%P%C%U%!$X(B memcpy() | |
642 * 3.3.4 tdata->streamer->stream_session[N]->p_queue $B$X(B enqueue() | |
643 * 3.3.4.1 tdata->streamer->stream_session[N]->p_queue->mutex $B$N(B lock/unlock $B$,H/@8(B | |
644 * 3.4 tdata->streamer->mutex $B$r(B unlock | |
645 * stream_func()$B$N(B lock $B$9$k$b$N$H=g=x(B | |
646 * #1. tdata->stream_queue->mutex $B$N(Block/unlock | |
647 * #2. tdata->streamer->mutex $B$r(B lock | |
648 * #2.1 tdata->streamer->stream_session[N]->p_queue->mutex $B$N(B lock/unlock | |
649 * #3. tdata->streamer->mutex $B$N(B unlock | |
650 * $B>e5-$K4X$7$F!"(Block/unlock$B$,I,MW$JItJ,$H!"NN0h3NJ]$H%3%T!<$NCY$a$N=hM}$H$G(B | |
651 * $B@Z$jJ,$1$i$l$kItJ,$K$D$$$F$O@Z$jJ,$1$F$7$^$C$?$[$&$,$$$$$+$b!#(B | |
652 * $B%/%j%F%#%+%k%;%/%7%g%s$O!"%]%$%s%?A`:n$@$1$H$9$k$Y$-!#(B | |
653 */ | |
654 void * | |
655 stream_func(void *p) | |
656 { | |
657 thread_data *data = (thread_data *)p; | |
658 STREAM_QUEUE_T *p_queue = data->stream_queue; | |
659 ARIB_STD_B25_BUFFER *qbuf = NULL; | |
660 ARIB_STD_B25_BUFFER *buf; | |
661 int i; | |
662 //fprintf (stderr, "stream_func(): start.\n"); | |
663 | |
664 while(1) { | |
665 // 3.1 tdata->stream_queue $B$+$i(B dequeue $B$9$k(B | |
666 // dequeue $B$7$?%G!<%?$O(B ARIB_STD_B25_BUFFER | |
667 qbuf = stream_dequeue(p_queue); | |
668 /* no entry in the queue */ | |
669 if(qbuf == NULL) { | |
670 //fprintf (stderr, "stream_func(): dequeue() return NULL pointer. streaming abort.\n"); | |
671 continue; | |
672 } | |
673 // $B%/%j%F%#%+%k%;%/%7%g%sD9$$$N$J$s$H$+$7$?$$$J$!!D(B | |
674 // ToDo: memcpy $B$H$+%/%j%F%#%+%k%;%/%7%g%s$N30$K=P$9(B | |
675 // 3.2 tdata->streamer->mutex $B$r(B lock | |
676 //fprintf (stderr, "stream_func(): mutex lock try.\n"); | |
677 pthread_mutex_lock(&data->streamer->mutex); | |
678 //fprintf (stderr, "stream_func(): mutex lock success.\n"); | |
679 // 3.3 $B0J2<$r(B tdata->streamer->stream_nr $B$N?t$@$1%k!<%W(B | |
680 for ( i=0; i < data->streamer->stream_nr; i++ ) { | |
681 // 3.3.1 tdata->streamer->stream_session[N]->is_valid $B$,M-8z$+3NG'(B | |
682 if ( data->streamer->stream_session[i] != NULL ) { | |
683 if ( data->streamer->stream_session[i]->is_valid ) { | |
684 // 3.3.2 tdata->streamer->stream_session[N]->p_queue $B$X$N%3%T!<MQ%P%C%U%!$N(Balloc | |
685 buf = malloc(sizeof(ARIB_STD_B25_BUFFER)); | |
686 if ( buf == NULL ) { | |
687 pthread_mutex_unlock(&data->streamer->mutex); | |
688 log_error ("stream_func(): alloc NULL pointer. streaming abort.\n"); | |
689 return NULL; | |
690 } | |
691 buf->data = NULL; | |
692 buf->data = malloc(qbuf->size); | |
693 if ( buf->data == NULL ) { | |
694 log_error ("Cannot malloc buf memory. streaming session_id[%d] abort.\n", i); | |
695 pthread_mutex_unlock(&data->streamer->mutex); | |
696 return NULL; | |
697 } | |
698 // 3.3.3 3.1$B$G(B dequeue $B$7$?%P%C%U%!$r(B3.3.2$B$G(B alloc $B$7$?%P%C%U%!$X(B memcpy() | |
699 memcpy(buf->data, qbuf->data, qbuf->size); | |
700 buf->size = qbuf->size; | |
701 // 3.3.4 tdata->streamer->stream_session[N]->p_queue $B$X(B enqueue() | |
702 stream_enqueue(data->streamer->stream_session[i]->p_queue, buf); | |
703 } | |
704 } | |
705 } | |
706 // 3.4 tdata->streamer->mutex $B$r(B unlock | |
707 pthread_mutex_unlock(&data->streamer->mutex); | |
708 free(qbuf->data); | |
709 free(qbuf); | |
710 //fprintf (stderr, "stream_func(): mutex unlock.\n"); | |
711 } | |
712 return NULL; | |
713 } | |
714 | |
715 void | |
716 show_usage(char *cmd) | |
717 { | |
718 #ifdef HAVE_LIBARIB25 | |
719 fprintf(stderr, "Usage: \n%s [--b25 [--round N] [--strip] [--EMM]] [--udp [--addr hostname --port portnumber]] [--device devicefile] [--lnb voltage] [--sid SID1,SID2] [--es filename_suffix] [--start_time YYYYMMDDHHMISS] channel rectime destfile\n", cmd); | |
720 #else | |
721 fprintf(stderr, "Usage: \n%s [--strip] [--EMM]] [--udp [--addr hostname --port portnumber]] [--device devicefile] [--lnb voltage] [--sid SID1,SID2] [--es filename_suffix] [--start_time YYYYMMDDHHMISS] channel rectime destfile\n", cmd); | |
722 #endif | |
723 fprintf(stderr, "\n"); | |
724 fprintf(stderr, "Remarks:\n"); | |
725 fprintf(stderr, "if rectime is '-', records indefinitely.\n"); | |
726 fprintf(stderr, "if destfile is '-', stdout is used for output.\n"); | |
727 } | |
728 | |
729 void | |
730 show_options(void) | |
731 { | |
732 fprintf(stderr, "Options:\n"); | |
733 #ifdef HAVE_LIBARIB25 | |
734 fprintf(stderr, "--b25: Decrypt using BCAS card\n"); | |
735 fprintf(stderr, " --round N: Specify round number\n"); | |
736 fprintf(stderr, " --strip: Strip null stream\n"); | |
737 fprintf(stderr, " --EMM: Instruct EMM operation\n"); | |
738 #endif | |
739 fprintf(stderr, "--udp: Turn on udp broadcasting\n"); | |
740 fprintf(stderr, " --addr hostname: Hostname or address to connect\n"); | |
741 fprintf(stderr, " --port portnumber: Port number to connect\n"); | |
742 fprintf(stderr, "--device devicefile: Specify devicefile to use\n"); | |
743 fprintf(stderr, "--lnb voltage: Specify LNB voltage (0, 11, 15)\n"); | |
744 fprintf(stderr, "--sid SID1,SID2,...: Specify SID number in CSV format (101,102,...)\n"); | |
745 fprintf(stderr, " --es filename: Specify ES out filename prefix\n"); | |
746 fprintf(stderr, " --start_time YYYYMMDDHHMISS: Specify record start datetime\n"); | |
747 fprintf(stderr, "--help: Show this help\n"); | |
748 fprintf(stderr, "--version: Show version\n"); | |
749 fprintf(stderr, "--list: Show channel list\n"); | |
750 } | |
751 | |
752 void | |
753 show_channels(void) | |
754 { | |
755 FILE *f; | |
756 char *home; | |
757 char buf[255], filename[255]; | |
758 | |
759 fprintf(stderr, "Available Channels:\n"); | |
760 | |
761 home = getenv("HOME"); | |
762 sprintf(filename, "%s/.recpt1-channels", home); | |
763 f = fopen(filename, "r"); | |
764 if(f) { | |
765 while(fgets(buf, 255, f)) | |
766 fprintf(stderr, "%s", buf); | |
767 fclose(f); | |
768 } | |
769 else | |
770 fprintf(stderr, "13-62: Terrestrial Channels\n"); | |
771 | |
772 fprintf(stderr, "101ch: NHK BS1\n"); | |
773 fprintf(stderr, "102ch: NHK BS2\n"); | |
774 fprintf(stderr, "103ch: NHK BShi\n"); | |
775 fprintf(stderr, "141ch: BS Nittele\n"); | |
776 fprintf(stderr, "151ch: BS Asahi\n"); | |
777 fprintf(stderr, "161ch: BS-TBS\n"); | |
778 fprintf(stderr, "171ch: BS Japan\n"); | |
779 fprintf(stderr, "181ch: BS Fuji\n"); | |
780 fprintf(stderr, "191ch: WOWOW\n"); | |
781 fprintf(stderr, "192ch: WOWOW2\n"); | |
782 fprintf(stderr, "193ch: WOWOW3\n"); | |
783 fprintf(stderr, "200ch: Star Channel\n"); | |
784 fprintf(stderr, "211ch: BS11 Digital\n"); | |
785 fprintf(stderr, "222ch: TwellV\n"); | |
786 fprintf(stderr, "C13-C63: CATV Channels\n"); | |
787 fprintf(stderr, "CS2-CS24: CS Channels\n"); | |
788 } | |
789 | |
790 float | |
791 getsignal_isdb_s(int signal) | |
792 { | |
793 /* apply linear interpolation */ | |
794 static const float afLevelTable[] = { | |
795 24.07f, // 00 00 0 24.07dB | |
796 24.07f, // 10 00 4096 24.07dB | |
797 18.61f, // 20 00 8192 18.61dB | |
798 15.21f, // 30 00 12288 15.21dB | |
799 12.50f, // 40 00 16384 12.50dB | |
800 10.19f, // 50 00 20480 10.19dB | |
801 8.140f, // 60 00 24576 8.140dB | |
802 6.270f, // 70 00 28672 6.270dB | |
803 4.550f, // 80 00 32768 4.550dB | |
804 3.730f, // 88 00 34816 3.730dB | |
805 3.630f, // 88 FF 35071 3.630dB | |
806 2.940f, // 90 00 36864 2.940dB | |
807 1.420f, // A0 00 40960 1.420dB | |
808 0.000f // B0 00 45056 -0.01dB | |
809 }; | |
810 | |
811 unsigned char sigbuf[4]; | |
812 memset(sigbuf, '\0', sizeof(sigbuf)); | |
813 sigbuf[0] = (((signal & 0xFF00) >> 8) & 0XFF); | |
814 sigbuf[1] = (signal & 0xFF); | |
815 | |
816 /* calculate signal level */ | |
817 if(sigbuf[0] <= 0x10U) { | |
818 /* clipped maximum */ | |
819 return 24.07f; | |
820 } | |
821 else if (sigbuf[0] >= 0xB0U) { | |
822 /* clipped minimum */ | |
823 return 0.0f; | |
824 } | |
825 else { | |
826 /* linear interpolation */ | |
827 const float fMixRate = | |
828 (float)(((unsigned short)(sigbuf[0] & 0x0FU) << 8) | | |
829 (unsigned short)sigbuf[0]) / 4096.0f; | |
830 return afLevelTable[sigbuf[0] >> 4] * (1.0f - fMixRate) + | |
831 afLevelTable[(sigbuf[0] >> 4) + 0x01U] * fMixRate; | |
832 } | |
833 } | |
834 | |
835 void | |
836 calc_cn(int fd, int type) | |
837 { | |
838 int rc ; | |
839 double P ; | |
840 double CNR; | |
841 | |
842 if(ioctl(fd, GET_SIGNAL_STRENGTH, &rc) < 0) { | |
843 fprintf(stderr, "Tuner Select Error\n"); | |
844 return ; | |
845 } | |
846 | |
847 if(type == CHTYPE_GROUND) { | |
848 P = log10(5505024/(double)rc) * 10; | |
849 CNR = (0.000024 * P * P * P * P) - (0.0016 * P * P * P) + | |
850 (0.0398 * P * P) + (0.5491 * P)+3.0965; | |
851 fprintf(stderr, "C/N = %fdB\n", CNR); | |
852 } | |
853 else { | |
854 CNR = getsignal_isdb_s(rc); | |
855 fprintf(stderr, "C/N = %fdB\n", CNR); | |
856 } | |
857 } | |
858 | |
859 void | |
860 cleanup(thread_data *tdata) | |
861 { | |
862 /* stop recording */ | |
863 ioctl(tdata->tfd, STOP_REC, 0); | |
864 | |
865 /* xxx need mutex? */ | |
866 f_exit = TRUE; | |
867 | |
868 pthread_cond_signal(&tdata->queue->cond_avail); | |
869 pthread_cond_signal(&tdata->queue->cond_used); | |
870 } | |
871 | |
872 /* will be signal handler thread */ | |
873 void * | |
874 process_signals(void *data) | |
875 { | |
876 sigset_t waitset; | |
877 int sig; | |
878 thread_data *tdata = (thread_data *)data; | |
879 | |
880 sigemptyset(&waitset); | |
881 sigaddset(&waitset, SIGPIPE); | |
882 sigaddset(&waitset, SIGINT); | |
883 sigaddset(&waitset, SIGTERM); | |
884 sigaddset(&waitset, SIGUSR1); | |
885 sigaddset(&waitset, SIGUSR2); | |
886 | |
887 sigwait(&waitset, &sig); | |
888 | |
889 switch(sig) { | |
890 case SIGPIPE: | |
891 fprintf(stderr, "\nSIGPIPE received. cleaning up...\n"); | |
892 cleanup(tdata); | |
893 break; | |
894 case SIGINT: | |
895 fprintf(stderr, "\nSIGINT received. cleaning up...\n"); | |
896 cleanup(tdata); | |
897 break; | |
898 case SIGTERM: | |
899 fprintf(stderr, "\nSIGTERM received. cleaning up...\n"); | |
900 cleanup(tdata); | |
901 break; | |
902 case SIGUSR1: /* normal exit*/ | |
903 cleanup(tdata); | |
904 break; | |
905 case SIGUSR2: /* error */ | |
906 fprintf(stderr, "Detected an error. cleaning up...\n"); | |
907 cleanup(tdata); | |
908 break; | |
909 } | |
910 | |
911 return NULL; /* dummy */ | |
912 } | |
913 | |
914 void | |
915 init_signal_handlers(pthread_t *signal_thread, thread_data *tdata) | |
916 { | |
917 sigset_t blockset; | |
918 | |
919 sigemptyset(&blockset); | |
920 sigaddset(&blockset, SIGPIPE); | |
921 sigaddset(&blockset, SIGINT); | |
922 sigaddset(&blockset, SIGTERM); | |
923 sigaddset(&blockset, SIGUSR1); | |
924 sigaddset(&blockset, SIGUSR2); | |
925 | |
926 if(pthread_sigmask(SIG_BLOCK, &blockset, NULL)) | |
927 fprintf(stderr, "pthread_sigmask() failed.\n"); | |
928 | |
929 pthread_create(signal_thread, NULL, process_signals, tdata); | |
930 } | |
931 | |
932 int | |
933 tune(char *channel, thread_data *tdata, char *device) | |
934 { | |
935 char **tuner; | |
936 int num_devs; | |
937 int lp; | |
938 FREQUENCY freq; | |
939 | |
940 /* get channel */ | |
941 tdata->table = searchrecoff(channel); | |
942 if(tdata->table == NULL) { | |
943 fprintf(stderr, "Invalid Channel: %s\n", channel); | |
944 return 1; | |
945 } | |
946 | |
947 freq.frequencyno = tdata->table->set_freq; | |
948 freq.slot = tdata->table->add_freq; | |
949 | |
950 /* open tuner */ | |
951 /* case 1: specified tuner device */ | |
952 if(device) { | |
953 tdata->tfd = open(device, O_RDONLY); | |
954 if(tdata->tfd < 0) { | |
955 fprintf(stderr, "Cannot open tuner device: %s\n", device); | |
956 return 1; | |
957 } | |
958 | |
959 /* power on LNB */ | |
960 if(tdata->table->type == CHTYPE_SATELLITE) { | |
961 if(ioctl(tdata->tfd, LNB_ENABLE, tdata->lnb) < 0) { | |
962 fprintf(stderr, "Power on LNB failed: %s\n", device); | |
963 } | |
964 } | |
965 | |
966 /* tune to specified channel */ | |
967 if(ioctl(tdata->tfd, SET_CHANNEL, &freq) < 0) { | |
968 close(tdata->tfd); | |
969 fprintf(stderr, "Cannot tune to the specified channel: %s\n", device); | |
970 return 1; | |
971 } | |
972 else { | |
973 tdata->ch = atoi(channel); | |
974 } | |
975 } | |
976 else { | |
977 /* case 2: loop around available devices */ | |
978 if(tdata->table->type == CHTYPE_SATELLITE) { | |
979 tuner = bsdev; | |
980 num_devs = NUM_BSDEV; | |
981 } | |
982 else { | |
983 tuner = isdb_t_dev; | |
984 num_devs = NUM_ISDB_T_DEV; | |
985 } | |
986 | |
987 for(lp = 0; lp < num_devs; lp++) { | |
988 tdata->tfd = open(tuner[lp], O_RDONLY); | |
989 if(tdata->tfd >= 0) { | |
990 /* power on LNB */ | |
991 if(tdata->table->type == CHTYPE_SATELLITE) { | |
992 if(ioctl(tdata->tfd, LNB_ENABLE, tdata->lnb) < 0) { | |
993 fprintf(stderr, "Warning: Power on LNB failed: %s\n", tuner[lp]); | |
994 } | |
995 } | |
996 | |
997 /* tune to specified channel */ | |
998 if(ioctl(tdata->tfd, SET_CHANNEL, &freq) < 0) { | |
999 close(tdata->tfd); | |
1000 tdata->tfd = -1; | |
1001 continue; | |
1002 } | |
1003 | |
1004 break; /* found suitable tuner */ | |
1005 } | |
1006 } | |
1007 | |
1008 /* all tuners cannot be used */ | |
1009 if(tdata->tfd < 0) { | |
1010 fprintf(stderr, "Cannot tune to the specified channel\n"); | |
1011 return 1; | |
1012 } | |
1013 else { | |
1014 tdata->ch = atoi(channel); | |
1015 } | |
1016 } | |
1017 | |
1018 /* show signal strength */ | |
1019 calc_cn(tdata->tfd, tdata->table->type); | |
1020 | |
1021 return 0; /* success */ | |
1022 } | |
1023 | |
1024 int | |
1025 parse_time(char *rectimestr, thread_data *tdata) | |
1026 { | |
1027 /* indefinite */ | |
1028 if(!strcmp("-", rectimestr)) { | |
1029 tdata->indefinite = TRUE; | |
1030 tdata->recsec = -1; | |
1031 } | |
1032 /* colon */ | |
1033 else if(strchr(rectimestr, ':')) { | |
1034 int n1, n2, n3; | |
1035 if(sscanf(rectimestr, "%d:%d:%d", &n1, &n2, &n3) == 3) | |
1036 tdata->recsec = n1 * 3600 + n2 * 60 + n3; | |
1037 else if(sscanf(rectimestr, "%d:%d", &n1, &n2) == 2) | |
1038 tdata->recsec = n1 * 3600 + n2 * 60; | |
1039 } | |
1040 /* HMS */ | |
1041 else { | |
1042 char *tmpstr; | |
1043 char *p1, *p2; | |
1044 | |
1045 tmpstr = strdup(rectimestr); | |
1046 p1 = tmpstr; | |
1047 while(*p1 && !isdigit(*p1)) | |
1048 p1++; | |
1049 | |
1050 /* hour */ | |
1051 if((p2 = strchr(p1, 'H')) || (p2 = strchr(p1, 'h'))) { | |
1052 *p2 = '\0'; | |
1053 tdata->recsec += atoi(p1) * 3600; | |
1054 p1 = p2 + 1; | |
1055 while(*p1 && !isdigit(*p1)) | |
1056 p1++; | |
1057 } | |
1058 | |
1059 /* minute */ | |
1060 if((p2 = strchr(p1, 'M')) || (p2 = strchr(p1, 'm'))) { | |
1061 *p2 = '\0'; | |
1062 tdata->recsec += atoi(p1) * 60; | |
1063 p1 = p2 + 1; | |
1064 while(*p1 && !isdigit(*p1)) | |
1065 p1++; | |
1066 } | |
1067 | |
1068 /* second */ | |
1069 tdata->recsec += atoi(p1); | |
1070 | |
1071 free(tmpstr); | |
1072 } | |
1073 | |
1074 return 0; /* success */ | |
1075 } | |
1076 | |
1077 int | |
1078 close_tuner(thread_data *tdata) | |
1079 { | |
1080 int rv = 0; | |
1081 | |
1082 if(tdata->table->type == CHTYPE_SATELLITE) { | |
1083 if(ioctl(tdata->tfd, LNB_DISABLE, 0) < 0) { | |
1084 rv = 1; | |
1085 } | |
1086 } | |
1087 close(tdata->tfd); | |
1088 | |
1089 return rv; | |
1090 } | |
1091 | |
1092 thread_data *gp_tdata; | |
1093 | |
1094 int | |
1095 main(int argc, char **argv) | |
1096 { | |
1097 time_t cur_time; | |
1098 pthread_t signal_thread; | |
1099 pthread_t reader_thread; | |
1100 pthread_t stream_thread; | |
1101 pthread_t ipc_thread; | |
1102 pthread_t dlna_thread; | |
1103 QUEUE_T *p_queue = create_queue(MAX_QUEUE); | |
1104 STREAM_QUEUE_T *stream_queue = create_stream_queue(MAX_QUEUE); | |
1105 BUFSZ *bufptr; | |
1106 decoder *dec = NULL; | |
1107 splitter *splitter = NULL; | |
1108 static thread_data tdata; | |
1109 gp_tdata = &tdata; | |
1110 decoder_options dopt = { | |
1111 4, /* round */ | |
1112 0, /* strip */ | |
1113 0 /* emm */ | |
1114 }; | |
1115 tdata.dopt = &dopt; | |
1116 tdata.lnb = 0; | |
1117 | |
1118 int result; | |
1119 int option_index; | |
1120 struct option long_options[] = { | |
1121 #ifdef HAVE_LIBARIB25 | |
1122 { "b25", 0, NULL, 'b'}, | |
1123 { "B25", 0, NULL, 'b'}, | |
1124 { "round", 1, NULL, 'r'}, | |
1125 { "strip", 0, NULL, 's'}, | |
1126 { "emm", 0, NULL, 'm'}, | |
1127 { "EMM", 0, NULL, 'm'}, | |
1128 #endif | |
1129 { "LNB", 1, NULL, 'n'}, | |
1130 { "lnb", 1, NULL, 'n'}, | |
1131 { "udp", 0, NULL, 'u'}, | |
1132 { "addr", 1, NULL, 'a'}, | |
1133 { "port", 1, NULL, 'p'}, | |
1134 { "device", 1, NULL, 'd'}, | |
1135 { "help", 0, NULL, 'h'}, | |
1136 { "version", 0, NULL, 'v'}, | |
1137 { "list", 0, NULL, 'l'}, | |
1138 { "sid", 1, NULL, 'i'}, | |
1139 { "SID", 1, NULL, 'i'}, | |
1140 { "es", 1, NULL, 'e'}, | |
1141 { "ES", 1, NULL, 'e'}, | |
1142 { "start_time", 1, NULL, 'y'}, | |
1143 {0, 0, NULL, 0} /* terminate */ | |
1144 }; | |
1145 | |
1146 boolean use_b25 = FALSE; | |
1147 boolean use_udp = FALSE; | |
1148 boolean fileless = FALSE; | |
1149 boolean use_stdout = FALSE; | |
1150 boolean use_splitter = FALSE; | |
1151 char *host_to = NULL; | |
1152 int port_to = 1234; | |
1153 sock_data *sockdata = NULL; | |
1154 char *device = NULL; | |
1155 int val; | |
1156 char *voltage[] = {"0V", "11V", "15V"}; | |
1157 char *sid_list = NULL; | |
1158 char *es_name_prefix = NULL; | |
1159 char *start_time = NULL; | |
1160 | |
1161 while((result = getopt_long(argc, argv, "br:smn:ua:p:d:hvli:", | |
1162 long_options, &option_index)) != -1) { | |
1163 switch(result) { | |
1164 case 'b': | |
1165 use_b25 = TRUE; | |
1166 fprintf(stderr, "using B25...\n"); | |
1167 break; | |
1168 case 's': | |
1169 dopt.strip = TRUE; | |
1170 fprintf(stderr, "enable B25 strip\n"); | |
1171 break; | |
1172 case 'm': | |
1173 dopt.emm = TRUE; | |
1174 fprintf(stderr, "enable B25 emm processing\n"); | |
1175 break; | |
1176 case 'u': | |
1177 use_udp = TRUE; | |
1178 host_to = "localhost"; | |
1179 fprintf(stderr, "enable UDP broadcasting\n"); | |
1180 break; | |
1181 case 'h': | |
1182 fprintf(stderr, "\n"); | |
1183 show_usage(argv[0]); | |
1184 fprintf(stderr, "\n"); | |
1185 show_options(); | |
1186 fprintf(stderr, "\n"); | |
1187 show_channels(); | |
1188 fprintf(stderr, "\n"); | |
1189 exit(0); | |
1190 break; | |
1191 case 'v': | |
1192 fprintf(stderr, "%s %s\n", argv[0], version); | |
1193 fprintf(stderr, "recorder command for PT1/2 digital tuner.\n"); | |
1194 exit(0); | |
1195 break; | |
1196 case 'l': | |
1197 show_channels(); | |
1198 exit(0); | |
1199 break; | |
1200 /* following options require argument */ | |
1201 case 'n': | |
1202 val = atoi(optarg); | |
1203 switch(val) { | |
1204 case 11: | |
1205 tdata.lnb = 1; | |
1206 break; | |
1207 case 15: | |
1208 tdata.lnb = 2; | |
1209 break; | |
1210 default: | |
1211 tdata.lnb = 0; | |
1212 break; | |
1213 } | |
1214 fprintf(stderr, "LNB = %s\n", voltage[tdata.lnb]); | |
1215 break; | |
1216 case 'r': | |
1217 dopt.round = atoi(optarg); | |
1218 fprintf(stderr, "set round %d\n", dopt.round); | |
1219 break; | |
1220 case 'a': | |
1221 use_udp = TRUE; | |
1222 host_to = optarg; | |
1223 fprintf(stderr, "UDP destination address: %s\n", host_to); | |
1224 break; | |
1225 case 'p': | |
1226 port_to = atoi(optarg); | |
1227 fprintf(stderr, "UDP port: %d\n", port_to); | |
1228 break; | |
1229 case 'd': | |
1230 device = optarg; | |
1231 fprintf(stderr, "using device: %s\n", device); | |
1232 break; | |
1233 case 'i': | |
1234 use_splitter = TRUE; | |
1235 sid_list = optarg; | |
1236 break; | |
1237 case 'e': | |
1238 es_name_prefix = optarg; | |
1239 break; | |
1240 case 'y': | |
1241 start_time = optarg; | |
1242 break; | |
1243 } | |
1244 } | |
1245 | |
1246 if(argc - optind < 3) { | |
1247 if(argc - optind == 2 && use_udp) { | |
1248 fprintf(stderr, "Fileless UDP broadcasting\n"); | |
1249 fileless = TRUE; | |
1250 tdata.wfd = -1; | |
1251 } | |
1252 else { | |
1253 fprintf(stderr, "Arguments are necessary!\n"); | |
1254 fprintf(stderr, "Try '%s --help' for more information.\n", argv[0]); | |
1255 return 1; | |
1256 } | |
1257 } | |
1258 | |
1259 fprintf(stderr, "pid = %d\n", getpid()); | |
1260 | |
1261 /* tune */ | |
1262 if(tune(argv[optind], &tdata, device) != 0) | |
1263 return 1; | |
1264 | |
1265 /* set recsec */ | |
1266 if(parse_time(argv[optind + 1], &tdata) != 0) | |
1267 return 1; | |
1268 | |
1269 /* open output file */ | |
1270 char *destfile = argv[optind + 2]; | |
1271 if(destfile && !strcmp("-", destfile)) { | |
1272 use_stdout = TRUE; | |
1273 tdata.wfd = 1; /* stdout */ | |
1274 } | |
1275 else { | |
1276 if(!fileless) { | |
1277 int status; | |
1278 char *path = strdup(argv[optind + 2]); | |
1279 char *dir = dirname(path); | |
1280 status = mkpath(dir, 0777); | |
1281 if(status == -1) | |
1282 perror("mkpath"); | |
1283 free(path); | |
1284 | |
1285 tdata.wfd = open(argv[optind + 2], (O_RDWR | O_CREAT | O_TRUNC), 0666); | |
1286 if(tdata.wfd < 0) { | |
1287 fprintf(stderr, "Cannot open output file: %s\n", | |
1288 argv[optind + 2]); | |
1289 return 1; | |
1290 } | |
1291 } | |
1292 } | |
1293 | |
1294 /* initialize decoder */ | |
1295 if(use_b25) { | |
1296 dec = b25_startup(&dopt); | |
1297 if(!dec) { | |
1298 fprintf(stderr, "Cannot start b25 decoder\n"); | |
1299 fprintf(stderr, "Fall back to encrypted recording\n"); | |
1300 use_b25 = FALSE; | |
1301 } | |
1302 } | |
1303 /* initialize splitter */ | |
1304 if(use_splitter) { | |
1305 splitter = split_startup(sid_list, es_name_prefix, start_time); | |
1306 if(splitter->sid_list == NULL) { | |
1307 fprintf(stderr, "Cannot start TS splitter\n"); | |
1308 return 1; | |
1309 } | |
1310 } | |
1311 | |
1312 boolean use_dlna = TRUE; | |
1313 /* initialize DLNA */ | |
1314 if(use_dlna) { | |
1315 do { | |
1316 tdata.stream_queue = stream_queue; | |
1317 tdata.streamer = malloc(sizeof(streamer)); | |
1318 if ( tdata.streamer == NULL ) { | |
1319 use_dlna = FALSE; | |
1320 break; | |
1321 } | |
1322 tdata.streamer->stream_nr = 0; | |
1323 tdata.streamer->stream_session[0] = NULL; | |
1324 pthread_mutex_init(&tdata.streamer->mutex, NULL); | |
1325 } while(0); | |
1326 } | |
1327 | |
1328 /* initialize udp connection */ | |
1329 if(use_udp) { | |
1330 sockdata = calloc(1, sizeof(sock_data)); | |
1331 struct in_addr ia; | |
1332 ia.s_addr = inet_addr(host_to); | |
1333 if(ia.s_addr == INADDR_NONE) { | |
1334 struct hostent *hoste = gethostbyname(host_to); | |
1335 if(!hoste) { | |
1336 perror("gethostbyname"); | |
1337 return 1; | |
1338 } | |
1339 ia.s_addr = *(in_addr_t*) (hoste->h_addr_list[0]); | |
1340 } | |
1341 if((sockdata->sfd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) { | |
1342 perror("socket"); | |
1343 return 1; | |
1344 } | |
1345 | |
1346 sockdata->addr.sin_family = AF_INET; | |
1347 sockdata->addr.sin_port = htons (port_to); | |
1348 sockdata->addr.sin_addr.s_addr = ia.s_addr; | |
1349 | |
1350 if(connect(sockdata->sfd, (struct sockaddr *)&sockdata->addr, | |
1351 sizeof(sockdata->addr)) < 0) { | |
1352 perror("connect"); | |
1353 return 1; | |
1354 } | |
1355 } | |
1356 | |
1357 /* prepare thread data */ | |
1358 tdata.queue = p_queue; | |
1359 tdata.decoder = dec; | |
1360 tdata.splitter = splitter; | |
1361 tdata.sock_data = sockdata; | |
1362 | |
1363 /* spawn signal handler thread */ | |
1364 init_signal_handlers(&signal_thread, &tdata); | |
1365 | |
1366 /* spawn reader thread */ | |
1367 tdata.signal_thread = signal_thread; | |
1368 pthread_create(&reader_thread, NULL, reader_func, &tdata); | |
1369 pthread_create(&stream_thread, NULL, stream_func, &tdata); | |
1370 pthread_create(&dlna_thread, NULL, dlna_startup, NULL); | |
1371 | |
1372 /* spawn ipc thread */ | |
1373 key_t key; | |
1374 key = (key_t)getpid(); | |
1375 | |
1376 if ((tdata.msqid = msgget(key, IPC_CREAT | 0666)) < 0) { | |
1377 perror("msgget"); | |
1378 } | |
1379 pthread_create(&ipc_thread, NULL, mq_recv, &tdata); | |
1380 | |
1381 /* start recording */ | |
1382 if(ioctl(tdata.tfd, START_REC, 0) < 0) { | |
1383 fprintf(stderr, "Tuner cannot start recording\n"); | |
1384 return 1; | |
1385 } | |
1386 | |
1387 fprintf(stderr, "Recording...\n"); | |
1388 | |
1389 time(&tdata.start_time); | |
1390 | |
1391 /* read from tuner */ | |
1392 while(1) { | |
1393 if(f_exit) | |
1394 break; | |
1395 //fprintf (stderr, "main() while loop start.\n"); | |
1396 | |
1397 time(&cur_time); | |
1398 bufptr = malloc(sizeof(BUFSZ)); | |
1399 if(!bufptr) { | |
1400 f_exit = TRUE; | |
1401 break; | |
1402 } | |
1403 //fprintf (stderr, "main() loop#1.\n"); | |
1404 bufptr->size = read(tdata.tfd, bufptr->buffer, MAX_READ_SIZE); | |
1405 //fprintf (stderr, "main() loop#2.\n"); | |
1406 if(bufptr->size <= 0) { | |
1407 if((cur_time - tdata.start_time) >= tdata.recsec && !tdata.indefinite) { | |
1408 //fprintf (stderr, "main() loop#3.\n"); | |
1409 f_exit = TRUE; | |
1410 enqueue(p_queue, NULL); | |
1411 break; | |
1412 } | |
1413 else { | |
1414 //fprintf (stderr, "main() loop#4.\n"); | |
1415 continue; | |
1416 } | |
1417 } | |
1418 //fprintf (stderr, "main() loop#5.\n"); | |
1419 //fprintf (stderr, "PT1 enqueue start.\n"); | |
1420 enqueue(p_queue, bufptr); | |
1421 //fprintf (stderr, "PT1 enqueue finish.\n"); | |
1422 | |
1423 /* stop recording */ | |
1424 time(&cur_time); | |
1425 if((cur_time - tdata.start_time) >= tdata.recsec && !tdata.indefinite) { | |
1426 ioctl(tdata.tfd, STOP_REC, 0); | |
1427 /* read remaining data */ | |
1428 while(1) { | |
1429 bufptr = malloc(sizeof(BUFSZ)); | |
1430 if(!bufptr) { | |
1431 f_exit = TRUE; | |
1432 break; | |
1433 } | |
1434 bufptr->size = read(tdata.tfd, bufptr->buffer, MAX_READ_SIZE); | |
1435 if(bufptr->size <= 0) { | |
1436 f_exit = TRUE; | |
1437 enqueue(p_queue, NULL); | |
1438 break; | |
1439 } | |
1440 enqueue(p_queue, bufptr); | |
1441 } | |
1442 break; | |
1443 } | |
1444 } | |
1445 //fprintf (stderr, "main() break?.\n"); | |
1446 | |
1447 /* delete message queue*/ | |
1448 msgctl(tdata.msqid, IPC_RMID, NULL); | |
1449 | |
1450 pthread_kill(signal_thread, SIGUSR1); | |
1451 | |
1452 /* wait for threads */ | |
1453 pthread_join(reader_thread, NULL); | |
1454 pthread_join(stream_thread, NULL); | |
1455 pthread_join(signal_thread, NULL); | |
1456 pthread_join(ipc_thread, NULL); | |
1457 // pthread_join(dlna_startup, NULL); | |
1458 pthread_join(dlna_thread, NULL); | |
1459 | |
1460 /* close tuner */ | |
1461 if(close_tuner(&tdata) != 0) | |
1462 return 1; | |
1463 | |
1464 /* release queue */ | |
1465 destroy_queue(p_queue); | |
1466 | |
1467 /* close output file */ | |
1468 if(!use_stdout) | |
1469 close(tdata.wfd); | |
1470 | |
1471 /* free socket data */ | |
1472 if(use_udp) { | |
1473 close(sockdata->sfd); | |
1474 free(sockdata); | |
1475 } | |
1476 | |
1477 /* release decoder */ | |
1478 if(use_b25) { | |
1479 b25_shutdown(dec); | |
1480 } | |
1481 if(use_splitter) { | |
1482 split_shutdown(splitter); | |
1483 } | |
1484 | |
1485 return 0; | |
1486 } | |
1487 |