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