comparison recpt1/recpt1.c @ 4:43d177fa65c9

fixed indentation
author Yoshiki Yazawa <yaz@honeyplanet.jp>
date Tue, 17 Feb 2009 01:46:54 +0900
parents 6801fe7e04ff
children 97fd2315114e
comparison
equal deleted inserted replaced
3:6801fe7e04ff 4:43d177fa65c9
13 13
14 #include "recpt1.h" 14 #include "recpt1.h"
15 #include "decoder.h" 15 #include "decoder.h"
16 16
17 /* globals */ 17 /* globals */
18 int wfd; /* for output file */ 18 int wfd; /* for output file */
19 int f_exit = FALSE ; 19 int f_exit = FALSE ;
20 20
21 typedef struct thread_data { 21 typedef struct thread_data {
22 QUEUE_T *queue; 22 QUEUE_T *queue;
23 decoder *decoder; 23 decoder *decoder;
24 } thread_data; 24 } thread_data;
25 25
26 // 周波数テーブル変換 26 // 周波数テーブル変換
27 ISDB_T_FREQ_CONV_TABLE * 27 ISDB_T_FREQ_CONV_TABLE *
28 searchrecoff(char *channel) 28 searchrecoff(char *channel)
29 { 29 {
30 int lp ; 30 int lp ;
31 31
32 for(lp = 0 ; lp < 113 ; lp++){ 32 for(lp = 0 ; lp < 113 ; lp++){
33 // 文字列&長さ一致したら周波数テーブル番号を返却する 33 // 文字列&長さ一致したら周波数テーブル番号を返却する
34 if((memcmp(isdb_t_conv_table[lp].parm_freq, channel, 34 if((memcmp(isdb_t_conv_table[lp].parm_freq, channel,
35 strlen(channel)) == 0) && 35 strlen(channel)) == 0) &&
36 (strlen(channel) == strlen(isdb_t_conv_table[lp].parm_freq))){ 36 (strlen(channel) == strlen(isdb_t_conv_table[lp].parm_freq))){
37 return &isdb_t_conv_table[lp] ; 37 return &isdb_t_conv_table[lp] ;
38 } 38 }
39 } 39 }
40 return NULL ; 40 return NULL ;
41 } 41 }
42 42
43 QUEUE_T * 43 QUEUE_T *
44 create_queue(size_t size) 44 create_queue(size_t size)
45 { 45 {
46 QUEUE_T* p_queue; 46 QUEUE_T *p_queue;
47 int memsize = sizeof(QUEUE_T) + size * sizeof(BUFSZ); 47 int memsize = sizeof(QUEUE_T) + size * sizeof(BUFSZ);
48 48
49 p_queue = (QUEUE_T*)calloc(memsize, sizeof(char)); 49 p_queue = (QUEUE_T*)calloc(memsize, sizeof(char));
50 50
51 if(p_queue != NULL){ 51 if(p_queue != NULL){
52 p_queue->size = size; 52 p_queue->size = size;
53 p_queue->no_full = size; 53 p_queue->no_full = size;
54 p_queue->no_empty = 0; 54 p_queue->no_empty = 0;
55 pthread_mutex_init(&p_queue->mutex, NULL); 55 pthread_mutex_init(&p_queue->mutex, NULL);
56 pthread_cond_init(&p_queue->cond_full, NULL); 56 pthread_cond_init(&p_queue->cond_full, NULL);
57 pthread_cond_init(&p_queue->cond_empty, NULL); 57 pthread_cond_init(&p_queue->cond_empty, NULL);
58 } 58 }
59 59
60 return p_queue; 60 return p_queue;
61 } 61 }
62 62
63 void 63 void
64 destroy_queue(QUEUE_T *p_queue) 64 destroy_queue(QUEUE_T *p_queue)
65 { 65 {
66 if(p_queue != NULL){ 66 if(p_queue != NULL) {
67 pthread_mutex_destroy(&p_queue->mutex); 67 pthread_mutex_destroy(&p_queue->mutex);
68 pthread_cond_destroy(&p_queue->cond_full); 68 pthread_cond_destroy(&p_queue->cond_full);
69 pthread_cond_destroy(&p_queue->cond_empty); 69 pthread_cond_destroy(&p_queue->cond_empty);
70 free(p_queue); 70 free(p_queue);
71 } 71 }
72 } 72 }
73 73
74 /* enqueue data. this function will block if queue is full. */ 74 /* enqueue data. this function will block if queue is full. */
75 void 75 void
76 enqueue(QUEUE_T *p_queue, BUFSZ *data) 76 enqueue(QUEUE_T *p_queue, BUFSZ *data)
77 { 77 {
78 pthread_mutex_lock(&p_queue->mutex); 78 pthread_mutex_lock(&p_queue->mutex);
79 /* entered critical section */ 79 /* entered critical section */
80 80
81 /* wait until queue is not full */ 81 /* wait until queue is not full */
82 while(!p_queue->no_full) { 82 while(!p_queue->no_full) {
83 pthread_cond_wait(&p_queue->cond_full, &p_queue->mutex); 83 pthread_cond_wait(&p_queue->cond_full, &p_queue->mutex);
84 printf("Full\n"); 84 printf("Full\n");
85 } 85 }
86 86
87 p_queue->buffer[p_queue->in] = data; 87 p_queue->buffer[p_queue->in] = data;
88 88
89 p_queue->in++; 89 p_queue->in++;
90 p_queue->in %= p_queue->size; 90 p_queue->in %= p_queue->size;
91 91
92 p_queue->no_full--; 92 p_queue->no_full--;
93 p_queue->no_empty++; 93 p_queue->no_empty++;
94 94
95 /* leaving critical section */ 95 /* leaving critical section */
96 pthread_mutex_unlock(&p_queue->mutex); 96 pthread_mutex_unlock(&p_queue->mutex);
97 pthread_cond_signal(&p_queue->cond_empty); 97 pthread_cond_signal(&p_queue->cond_empty);
98 } 98 }
99 99
100 /* dequeue data. this function will block if queue is empty. */ 100 /* dequeue data. this function will block if queue is empty. */
101 BUFSZ * 101 BUFSZ *
102 dequeue(QUEUE_T *p_queue) 102 dequeue(QUEUE_T *p_queue)
103 { 103 {
104 BUFSZ *buffer; 104 BUFSZ *buffer;
105 105
106 pthread_mutex_lock(&p_queue->mutex); 106 pthread_mutex_lock(&p_queue->mutex);
107 /* entered the critical section*/ 107 /* entered the critical section*/
108 108
109 /* wait until queue is filled */ 109 /* wait until queue is filled */
110 while (!p_queue->no_empty) { 110 while (!p_queue->no_empty) {
111 pthread_cond_wait(&p_queue->cond_empty, &p_queue->mutex); 111 pthread_cond_wait(&p_queue->cond_empty, &p_queue->mutex);
112 } 112 }
113 113
114 /* take buffer address */ 114 /* take buffer address */
115 buffer = p_queue->buffer[p_queue->out]; 115 buffer = p_queue->buffer[p_queue->out];
116 116
117 // 次にデータを取り出す場所をインクリメント 117 // 次にデータを取り出す場所をインクリメント
118 p_queue->out++; 118 p_queue->out++;
119 p_queue->out %= p_queue->size; 119 p_queue->out %= p_queue->size;
120 120
121 /* update flags */ 121 /* update flags */
122 p_queue->no_full++; 122 p_queue->no_full++;
123 p_queue->no_empty--; 123 p_queue->no_empty--;
124 124
125 /* leaving the critical section */ 125 /* leaving the critical section */
126 pthread_mutex_unlock(&p_queue->mutex); 126 pthread_mutex_unlock(&p_queue->mutex);
127 pthread_cond_signal(&p_queue->cond_full); 127 pthread_cond_signal(&p_queue->cond_full);
128 128
129 return buffer; 129 return buffer;
130 } 130 }
131 131
132 /* this function will be a writing thread */ 132 /* this function will be a writing thread */
133 void * 133 void *
134 write_func(void *p) 134 write_func(void *p)
135 { 135 {
136 thread_data *data = (thread_data *)p; 136 thread_data *data = (thread_data *)p;
137 QUEUE_T *p_queue = data->queue; 137 QUEUE_T *p_queue = data->queue;
138 decoder *dec = data->decoder; 138 decoder *dec = data->decoder;
139 BUFSZ *buf ; 139 BUFSZ *buf ;
140 ARIB_STD_B25_BUFFER sbuf, dbuf; 140 ARIB_STD_B25_BUFFER sbuf, dbuf;
141 141
142 while(1){ 142 while(1) {
143 buf = dequeue(p_queue); 143 buf = dequeue(p_queue);
144 /* no entry in the queue */ 144 /* no entry in the queue */
145 if(buf == NULL){ 145 if(buf == NULL){
146 close(wfd); 146 close(wfd);
147 break ; 147 break ;
148 } 148 }
149 149
150 sbuf.data = buf->buffer; 150 sbuf.data = buf->buffer;
151 sbuf.size = buf->size; 151 sbuf.size = buf->size;
152 152
153 /* write data to output file*/ 153 /* write data to output file*/
154 b25_decode(dec, &sbuf, &dbuf); 154 b25_decode(dec, &sbuf, &dbuf);
155 write(wfd, dbuf.data, dbuf.size); 155 write(wfd, dbuf.data, dbuf.size);
156 free(buf); 156 free(buf);
157 157
158 /* normal exit */ 158 /* normal exit */
159 if((f_exit) && (!p_queue->no_empty)){ 159 if((f_exit) && (!p_queue->no_empty)){
160 b25_finish(dec, &sbuf, &dbuf); 160 b25_finish(dec, &sbuf, &dbuf);
161 write(wfd, dbuf.data, dbuf.size); 161 write(wfd, dbuf.data, dbuf.size);
162 close(wfd); 162 close(wfd);
163 break ; 163 break ;
164 } 164 }
165 } 165 }
166 166
167 return NULL; 167 return NULL;
168 } 168 }
169 169
170 int 170 int
171 main(int argc, char **argv) 171 main(int argc, char **argv)
172 { 172 {
173 int fd ; 173 int fd ;
174 int lp ; 174 int lp ;
175 int recsec ; 175 int recsec ;
176 time_t start_time ; 176 time_t start_time ;
177 time_t cur_time ; 177 time_t cur_time ;
178 FREQUENCY freq; 178 FREQUENCY freq;
179 ISDB_T_FREQ_CONV_TABLE *ptr ; 179 ISDB_T_FREQ_CONV_TABLE *ptr ;
180 pthread_t dequeue_threads; 180 pthread_t dequeue_threads;
181 QUEUE_T *p_queue = create_queue(MAX_QUEUE); 181 QUEUE_T *p_queue = create_queue(MAX_QUEUE);
182 BUFSZ *bufptr ; 182 BUFSZ *bufptr ;
183 decoder *dec; 183 decoder *dec;
184 thread_data tdata; 184 thread_data tdata;
185 185
186 if(argc < 4) { 186 if(argc < 4) {
187 printf("Usage %s: channel recsec destfile\n", argv[0]); 187 printf("Usage %s: channel recsec destfile\n", argv[0]);
188 printf("channel =\n"); 188 printf("channel =\n");
189 printf("151ch:BS朝日\n"); 189 printf("151ch:BS朝日\n");
190 printf("161ch:BS-i\n"); 190 printf("161ch:BS-i\n");
191 printf("171ch:BSジャパン\n"); 191 printf("171ch:BSジャパン\n");
192 printf("211ch:BS11デジタル\n"); 192 printf("211ch:BS11デジタル\n");
193 printf("222ch:TwellV\n"); 193 printf("222ch:TwellV\n");
194 printf("141ch:BS日テレ\n"); 194 printf("141ch:BS日テレ\n");
195 printf("181ch:BSフジ\n"); 195 printf("181ch:BSフジ\n");
196 printf("101ch:NHK衛星第1放送(BS1)\n"); 196 printf("101ch:NHK衛星第1放送(BS1)\n");
197 printf("102ch:NHK衛星第2放送(BS2)\n"); 197 printf("102ch:NHK衛星第2放送(BS2)\n");
198 printf("103ch:NHKハイビジョン(BShi)\n"); 198 printf("103ch:NHKハイビジョン(BShi)\n");
199 return 1; 199 return 1;
200 } 200 }
201 ptr = searchrecoff(argv[1]); 201 ptr = searchrecoff(argv[1]);
202 if(ptr == NULL) { 202 if(ptr == NULL) {
203 printf("Channel Select Error(%s)\n", argv[1]); 203 printf("Channel Select Error(%s)\n", argv[1]);
204 return 1 ; 204 return 1 ;
205 } 205 }
206 206
207 freq.frequencyno = ptr->set_freq ; 207 freq.frequencyno = ptr->set_freq ;
208 freq.slot = ptr->add_freq ; 208 freq.slot = ptr->add_freq ;
209 209
210 if(ptr->type == CHTYPE_SATELLITE) { 210 if(ptr->type == CHTYPE_SATELLITE) {
211 for(lp = 0 ; lp < 2 ; lp++) { 211 for(lp = 0 ; lp < 2 ; lp++) {
212 fd = open(bsdev[lp], O_RDONLY); 212 fd = open(bsdev[lp], O_RDONLY);
213 if(fd >= 0) { 213 if(fd >= 0) {
214 break ; 214 break ;
215 } 215 }
216 } 216 }
217 if(fd < 0) { 217 if(fd < 0) {
218 printf("Device Open Error\n"); 218 printf("Device Open Error\n");
219 return 1; 219 return 1;
220 } 220 }
221 } else { 221 } else {
222 for(lp = 0 ; lp < 2 ; lp++) { 222 for(lp = 0 ; lp < 2 ; lp++) {
223 fd = open(isdb_t_dev[lp], O_RDONLY); 223 fd = open(isdb_t_dev[lp], O_RDONLY);
224 if(fd >= 0) { 224 if(fd >= 0) {
225 break ; 225 break ;
226 } 226 }
227 } 227 }
228 if(fd < 0) { 228 if(fd < 0) {
229 printf("Device Open Error\n"); 229 printf("Device Open Error\n");
230 return 1; 230 return 1;
231 } 231 }
232 } 232 }
233 recsec = atoi(argv[2]); 233 recsec = atoi(argv[2]);
234 234
235 /* initialize decoder */ 235 /* initialize decoder */
236 dec = b25_startup(); 236 dec = b25_startup();
237 237
238 /* open output file */ 238 /* open output file */
239 wfd = open(argv[3], (O_RDWR | O_CREAT | O_TRUNC), 0666); 239 wfd = open(argv[3], (O_RDWR | O_CREAT | O_TRUNC), 0666);
240 if(wfd < 0) { 240 if(wfd < 0) {
241 printf("Output File Open Error(%s)\n", argv[3]); 241 printf("Output File Open Error(%s)\n", argv[3]);
242 return 0; 242 return 0;
243 } 243 }
244 244
245 if(ioctl(fd, SET_CHANNEL, &freq) < 0) { 245 if(ioctl(fd, SET_CHANNEL, &freq) < 0) {
246 printf("Tuner Select Error\n"); 246 printf("Tuner Select Error\n");
247 return 0 ;
248 }
249
250 /* make reading thread */
251 tdata.queue = p_queue;
252 tdata.decoder = dec;
253 pthread_create(&dequeue_threads, NULL, write_func, &tdata);
254
255 /* start recording*/
256 if(ioctl(fd, START_REC, 0) < 0) {
257 printf("Tuner Start Error\n");
258 return 0 ;
259 }
260
261 time(&start_time);
262
263 /* read from tuner */
264 while(1) {
265 time(&cur_time);
266 bufptr = malloc(sizeof(BUFSZ));
267 bufptr->size = read(fd, bufptr->buffer, MAX_READ_SIZE);
268 if(bufptr->size <= 0) {
269 if((cur_time - start_time) >= recsec) {
270 f_exit = TRUE ;
271 enqueue(p_queue, NULL);
272 break ;
273 } else {
274 continue ;
275 }
276 }
277 enqueue(p_queue, bufptr);
278
279 /* stop recording */
280 if((cur_time - start_time) >= recsec) {
281 ioctl(fd, STOP_REC, 0);
282 /* read remaining data */
283 while(1) {
284 bufptr = malloc(sizeof(BUFSZ));
285 bufptr->size = read(fd, bufptr->buffer, MAX_READ_SIZE);
286 if(bufptr->size <= 0) {
287 f_exit = TRUE ;
288 enqueue(p_queue, NULL);
289 break ;
290 }
291 enqueue(p_queue, bufptr);
292 }
293 break ;
294 }
295 }
296 /* close tuner */
297 close(fd);
298
299 /* wait reading thread */
300 pthread_join(dequeue_threads, NULL);
301 destroy_queue(p_queue);
302
303 /* release decoder */
304 b25_shutdown(dec);
305
247 return 0 ; 306 return 0 ;
248 } 307 }
249
250 /* make reading thread */
251 tdata.queue = p_queue;
252 tdata.decoder = dec;
253 pthread_create(&dequeue_threads, NULL, write_func, &tdata);
254
255 /* start recording*/
256 if(ioctl(fd, START_REC, 0) < 0) {
257 printf("Tuner Start Error\n");
258 return 0 ;
259 }
260
261 time(&start_time);
262
263 /* read from tuner */
264 while(1) {
265 time(&cur_time);
266 bufptr = malloc(sizeof(BUFSZ));
267 bufptr->size = read(fd, bufptr->buffer, MAX_READ_SIZE);
268 if(bufptr->size <= 0) {
269 if((cur_time - start_time) >= recsec) {
270 f_exit = TRUE ;
271 enqueue(p_queue, NULL);
272 break ;
273 } else {
274 continue ;
275 }
276 }
277 enqueue(p_queue, bufptr);
278
279 /* stop recording */
280 if((cur_time - start_time) >= recsec) {
281 ioctl(fd, STOP_REC, 0);
282 /* read remaining data */
283 while(1) {
284 bufptr = malloc(sizeof(BUFSZ));
285 bufptr->size = read(fd, bufptr->buffer, MAX_READ_SIZE);
286 if(bufptr->size <= 0) {
287 f_exit = TRUE ;
288 enqueue(p_queue, NULL);
289 break ;
290 }
291 enqueue(p_queue, bufptr);
292 }
293 break ;
294 }
295 }
296 /* close tuner */
297 close(fd);
298
299 /* wait reading thread */
300 pthread_join(dequeue_threads, NULL);
301 destroy_queue(p_queue);
302
303 /* release decoder */
304 b25_shutdown(dec);
305
306 return 0 ;
307 }