diff recpt1/recpt1.c @ 3:6801fe7e04ff

updated to ariv25v023
author Yoshiki Yazawa <yaz@honeyplanet.jp>
date Tue, 17 Feb 2009 01:40:56 +0900
parents 8ac7c59fefc9
children 43d177fa65c9
line wrap: on
line diff
--- a/recpt1/recpt1.c	Mon Feb 16 21:40:16 2009 +0900
+++ b/recpt1/recpt1.c	Tue Feb 17 01:40:56 2009 +0900
@@ -1,24 +1,27 @@
-#include	<stdio.h>
-#include	<sys/types.h>
-#include	<sys/stat.h>
-#include	<fcntl.h>
-#include	<time.h>
-#include	<stdlib.h>
-#include	<string.h>
-#include	<pthread.h>
-#include        <unistd.h>
+#include <fcntl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <time.h>
+#include <stdlib.h>
+#include <string.h>
+#include <pthread.h>
+#include <unistd.h>
+#include <stdio.h>
 
-#include	<sys/ioctl.h>
-#include	"pt1_ioctl.h"
+#include <sys/ioctl.h>
+#include "pt1_ioctl.h"
 
 #include "recpt1.h"
 #include "decoder.h"
 
 /* globals */
-int	wfd;		// ファイル書き込み用
+int	wfd;		/* for output file */
 int	f_exit = FALSE ;
-decoder *dec;
 
+typedef struct thread_data {
+  QUEUE_T *queue;
+  decoder *decoder;
+} thread_data;
 
 // 周波数テーブル変換
 ISDB_T_FREQ_CONV_TABLE *
@@ -41,7 +44,7 @@
 create_queue(size_t size)
 {
   QUEUE_T*	p_queue;
-  int		memsize = sizeof(QUEUE_T) + (size * sizeof(BUFSZ));
+  int		memsize = sizeof(QUEUE_T) + size * sizeof(BUFSZ);
 
   p_queue = (QUEUE_T*)calloc(memsize, sizeof(char));
 
@@ -68,16 +71,14 @@
   }
 }
 
-// 関数名: enqueue
-//  キューにデータを入れる
-//  データが満タンな場合は、ブロックします
+/* enqueue data. this function will block if queue is full. */
 void
 enqueue(QUEUE_T *p_queue, BUFSZ *data)
 {
   pthread_mutex_lock(&p_queue->mutex);
-  // -- ここから、クリティカルセクション --
+  /* entered critical section */
 
-  // 満タンじゃなくなるまで待つ
+  /* wait until queue is not full */
   while(!p_queue->no_full) {
     pthread_cond_wait(&p_queue->cond_full, &p_queue->mutex);
     printf("Full\n");
@@ -91,67 +92,68 @@
   p_queue->no_full--;
   p_queue->no_empty++;
 
+  /* leaving critical section */
   pthread_mutex_unlock(&p_queue->mutex);
   pthread_cond_signal(&p_queue->cond_empty);
 }
 
-// 関数名: dequeue
-//  キューにデータを入れる
-//  データが満タンな場合は、ブロックします
+/* dequeue data. this function will block if queue is empty. */
 BUFSZ *
 dequeue(QUEUE_T *p_queue)
 {
-  void	*result;
+  BUFSZ	*buffer;
 
   pthread_mutex_lock(&p_queue->mutex);
-  // -- ここから、クリティカルセクション --
+  /* entered the critical section*/
 
-  // 空っぽじゃなくなるまで待つ
+  /* wait until queue is filled */
   while (!p_queue->no_empty) {
     pthread_cond_wait(&p_queue->cond_empty, &p_queue->mutex);
   }
 
-  // データを取り出す
-  result = p_queue->buffer[p_queue->out];
+  /* take buffer address */
+  buffer = p_queue->buffer[p_queue->out];
 
   // 次にデータを取り出す場所をインクリメント
   p_queue->out++;
   p_queue->out %= p_queue->size;
 
-  // フラグの更新
+  /* update flags */
   p_queue->no_full++;
   p_queue->no_empty--;
 
-  // -- ここまで、クリティカルセクション --
+  /* leaving the critical section */
   pthread_mutex_unlock(&p_queue->mutex);
   pthread_cond_signal(&p_queue->cond_full);
 
-  return result;
+  return buffer;
 }
 
 /* this function will be a writing thread */
 void *
 write_func(void *p)
 {
-  QUEUE_T *p_queue = (QUEUE_T*)p;
-  BUFSZ	*ptr ;
+  thread_data *data = (thread_data *)p;
+  QUEUE_T *p_queue = data->queue;
+  decoder *dec = data->decoder;
+  BUFSZ	*buf ;
   ARIB_STD_B25_BUFFER sbuf, dbuf;
 
   while(1){
-    ptr = dequeue(p_queue);
+    buf = dequeue(p_queue);
     /* no entry in the queue */
-    if(ptr == NULL){
+    if(buf == NULL){
       close(wfd);
       break ;
     }
 
-    sbuf.data = ptr->buffer;
-    sbuf.size = ptr->size;
+    sbuf.data = buf->buffer;
+    sbuf.size = buf->size;
 
     /* write data to output file*/
     b25_decode(dec, &sbuf, &dbuf);
     write(wfd, dbuf.data, dbuf.size);
-    free(ptr);
+    free(buf);
 
     /* normal exit */
     if((f_exit) && (!p_queue->no_empty)){
@@ -178,8 +180,10 @@
   pthread_t dequeue_threads;
   QUEUE_T *p_queue = create_queue(MAX_QUEUE);
   BUFSZ	*bufptr ;
+  decoder *dec;
+  thread_data tdata;
 
-  if(argc < 4){
+  if(argc < 4) {
     printf("Usage %s: channel recsec destfile\n", argv[0]);
     printf("channel =\n");
     printf("151ch:BS朝日\n");
@@ -195,7 +199,7 @@
     return 1;
   }
   ptr = searchrecoff(argv[1]);
-  if(ptr == NULL){
+  if(ptr == NULL) {
     printf("Channel Select Error(%s)\n", argv[1]);
     return 1 ;
   }
@@ -203,25 +207,25 @@
   freq.frequencyno = ptr->set_freq ;
   freq.slot = ptr->add_freq ;
 
-  if(ptr->type == CHTYPE_SATELLITE){
-    for(lp = 0 ; lp < 2 ; lp++){
+  if(ptr->type == CHTYPE_SATELLITE) {
+    for(lp = 0 ; lp < 2 ; lp++) {
       fd = open(bsdev[lp], O_RDONLY);
-      if(fd >= 0){
+      if(fd >= 0) {
 	break ;
       }
     }
-    if(fd < 0){
+    if(fd < 0) {
       printf("Device Open Error\n");
       return 1;
     }
-  }else{
-    for(lp = 0 ; lp < 2 ; lp++){
+  } else {
+    for(lp = 0 ; lp < 2 ; lp++) {
       fd = open(isdb_t_dev[lp], O_RDONLY);
-      if(fd >= 0){
+      if(fd >= 0) {
 	break ;
       }
     }
-    if(fd < 0){
+    if(fd < 0) {
       printf("Device Open Error\n");
       return 1;
     }
@@ -232,49 +236,54 @@
   dec = b25_startup();
 
   /* open output file */
-  wfd = open64(argv[3], (O_RDWR | O_CREAT | O_TRUNC), 0666);
-  if(wfd < 0){
+  wfd = open(argv[3], (O_RDWR | O_CREAT | O_TRUNC), 0666);
+  if(wfd < 0) {
     printf("Output File Open Error(%s)\n", argv[3]);
     return 0;
   }
 
-  if(ioctl(fd, SET_CHANNEL, &freq) < 0){
+  if(ioctl(fd, SET_CHANNEL, &freq) < 0) {
     printf("Tuner Select Error\n");
     return 0 ;
   }
 
   /* make reading thread */
-  pthread_create(&dequeue_threads, NULL, write_func, p_queue);
-  if(ioctl(fd, START_REC, 0) < 0){
+  tdata.queue = p_queue;
+  tdata.decoder = dec;
+  pthread_create(&dequeue_threads, NULL, write_func, &tdata);
+
+  /* start recording*/
+  if(ioctl(fd, START_REC, 0) < 0) {
     printf("Tuner Start Error\n");
     return 0 ;
   }
 
   time(&start_time);
 
-  /* read-write loop */
-  while(1){
+  /* read from tuner */
+  while(1) {
     time(&cur_time);
-    bufptr = calloc(1, sizeof(BUFSZ));
+    bufptr = malloc(sizeof(BUFSZ));
     bufptr->size = read(fd, bufptr->buffer, MAX_READ_SIZE);
-    if(bufptr->size <= 0){
-      if((cur_time - start_time) >= recsec){
+    if(bufptr->size <= 0) {
+      if((cur_time - start_time) >= recsec) {
 	f_exit = TRUE ;
 	enqueue(p_queue, NULL);
 	break ;
-      }else{
+      } else {
 	continue ;
       }
     }
     enqueue(p_queue, bufptr);
 
-    if((cur_time - start_time) >= recsec){
+    /* stop recording */
+    if((cur_time - start_time) >= recsec) {
       ioctl(fd, STOP_REC, 0);
-      //なくなるまでデータを読み出す
-      while(1){
-	bufptr = calloc(1, sizeof(BUFSZ));
+      /* read remaining data */
+      while(1) {
+	bufptr = malloc(sizeof(BUFSZ));
 	bufptr->size = read(fd, bufptr->buffer, MAX_READ_SIZE);
-	if(bufptr->size <= 0){
+	if(bufptr->size <= 0) {
 	  f_exit = TRUE ;
 	  enqueue(p_queue, NULL);
 	  break ;
@@ -284,7 +293,8 @@
       break ;
     }
   }
-  close(fd); /* close tuner */
+  /* close tuner */
+  close(fd);
 
   /* wait reading thread */
   pthread_join(dequeue_threads, NULL);