diff src/recpt1.c @ 152:30e91361506a

EXPERIMENTAL: Enable change phisical channel by DLNA.(ISDB-T only)
author Naoya OYAMA <naoya.oyama@gmail.com>
date Sun, 02 Sep 2012 01:59:27 +0900
parents f7f594bf4e98
children 578ebda88997
line wrap: on
line diff
--- a/src/recpt1.c	Fri Aug 31 23:39:46 2012 +0900
+++ b/src/recpt1.c	Sun Sep 02 01:59:27 2012 +0900
@@ -50,6 +50,7 @@
 
 /* globals */
 boolean f_exit = FALSE;
+struct channel_info_list *channel_list = NULL;
 extern struct ushare_t *ut;
 
 /* prototypes */
@@ -57,6 +58,96 @@
 int close_tuner(thread_data *tdata);
 static int get_device_id_by_name ( const char *name );
 
+static struct channel_info_list *open_list_file(char *type)
+{
+    char   *buf                                 = NULL;
+    char   filename[PATH_MAX];
+    char   *home                                = NULL;
+    char   *p                                   = NULL;
+    struct channel_info      *channel_info      = NULL;
+    struct channel_info_list *channel_info_list = NULL;
+    FILE   *f                                   = NULL;
+
+    home = getenv("HOME");
+    snprintf(filename, PATH_MAX, "%s/.recpt1_%s", home, type);
+    f = fopen(filename, "r");
+    if(!f) {
+        return channel_info_list;
+   	}
+    channel_info_list = malloc(sizeof(*channel_info_list));
+    if(!channel_info_list)
+        return NULL;
+    channel_info_list->nr_channel = 0;
+    while(1) {
+        buf = NULL;
+        buf = malloc(256);
+        if(!buf)
+            break;
+        if(!fgets(buf, 255, f)) {
+            free(buf);
+            buf = NULL;
+            break;
+        }
+        channel_info = NULL;
+        channel_info = malloc(sizeof(*channel_info));
+        if(!channel_info)
+            break;
+
+        channel_info->sid  = NULL;
+        channel_info->tp   = NULL;
+        channel_info->name = NULL;
+
+        channel_info->sid = buf;
+
+        p = strchr(buf, C_CHAR_COMMA);
+        if (p == NULL) {
+            /* FILE ERROR */
+            free(channel_info);
+			channel_info = NULL;
+           	continue;
+        }
+        *p = '\0';
+        channel_info->tp = ++p;
+
+        p = strchr(p, C_CHAR_COMMA);
+        if (p == NULL) {
+            /* FILE ERROR */
+            free(channel_info);
+            channel_info = NULL;
+            continue;
+        }
+        *p = '\0';
+        channel_info->name = ++p;
+        p = strchr(p, '\n');
+        if (p)
+            *p = '\0';
+
+        if(channel_info_list->nr_channel < CHANNEL_MAX) {
+            channel_info_list->channel_info[channel_info_list->nr_channel] = channel_info;
+            channel_info_list->nr_channel += 1;
+        }
+    }
+    fclose(f);
+    return channel_info_list;
+}
+
+static void close_list_file(struct channel_info_list *channel_info_list)
+{
+    int i;
+    if (!channel_info_list)
+        return;
+    for (i=0; i < channel_info_list->nr_channel; i++) {
+        free(channel_info_list->channel_info[i]->sid);
+        channel_info_list->channel_info[i]->sid = NULL;
+        free(channel_info_list->channel_info[i]);
+        channel_info_list->channel_info[i] = NULL;
+    }
+    channel_info_list->nr_channel = 0;
+    free(channel_info_list);
+    channel_info_list = NULL;
+    return;
+}
+
 /* ipc message receive */
 void *
 mq_recv(void *t)
@@ -416,7 +507,9 @@
     int code;
     int split_select_finish = TSS_ERROR;
     int old_sid = 0, new_sid = 0;
+    int old_tp = 0, new_tp = 0;
     time_t split_start_time;
+    struct channel_info *channel_info = NULL;
 
     time(&split_start_time);
 
@@ -521,16 +614,50 @@
          *        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
          */
         /*
-         * DLNA $B$G$N(BSID$BJQ99$N<BAu<B83(B
+         * DLNA $B$G$NJ*M}%A%c%s%M%k!&(BSID$BJQ99$N<BAu<B83(B
          */
         if ( use_dlna && buf.size > 0 &&
              strcmp("all", ut->request_channel)) {
             if ( use_splitter ) {
-                new_sid = atoi(ut->request_channel);
+                if (ut->tp)
+                    new_tp  = atoi(ut->tp);
+                if (ut->sid)
+                    new_sid = atoi(ut->sid);
+                if (old_tp != new_tp) {
+                    old_tp = new_tp;
+                    // msgsnd() $B$r<+J,<+?H$KAw$l$k$N$J$i$=$NJ}$,NI$5$=$&$J5$$,$9$k(B
+                    ioctl(data->tfd, STOP_REC, 0);
+                    if(close_tuner(data) != 0)
+                        break;
+                    pthread_mutex_lock(&data->queue->mutex);
+                    while(data->queue->num_used > 0) {
+                        while(1) {
+                             QUEUE_T *p_queue = data->queue;
+                             if (p_queue->num_used == 0)
+                                 break;
+                             free(p_queue->buffer[p_queue->out]);
+                             p_queue->buffer[p_queue->out] = NULL;
+                             p_queue->out++;
+                             p_queue->out %= p_queue->size;
+
+                             /* update counters */
+                             p_queue->num_avail++;
+                             p_queue->num_used--;
+                        }
+                    }
+                    pthread_mutex_unlock(&data->queue->mutex);
+                    tune(ut->tp, data, NULL);
+                    if(ioctl(data->tfd, START_REC, 0) < 0) {
+                        // $B$3$3$N07$$$I$3$m$,LB$&$H$3$m!#(Bexit $B$7$F$b$$$$5$$,$9$k(B
+                        fprintf(stderr, "Tuner cannot start recording\n");
+                        break;
+                    }
+                    time(&split_start_time);
+                }
                 if ( old_sid != new_sid ) {
                     old_sid = new_sid;
                     split_shutdown(splitter);
-                    splitter = split_startup(ut->request_channel, NULL, NULL);
+                    splitter = split_startup(ut->sid, NULL, NULL);
                     if ( splitter == NULL ) {
                         fprintf (stderr, "reader_func() splitter RESTART FAILED.\n", old_sid, new_sid);
                         use_splitter = FALSE;
@@ -556,7 +683,6 @@
                          p_queue->num_used--;
                     }
                     pthread_mutex_unlock(&data->stream_queue->mutex);
-                    fprintf (stderr, "reader_func() free stream queue finish.\n");
                     time(&split_start_time);
                     continue;
                 }
@@ -1392,6 +1518,8 @@
     /* initialize DLNA */
     if(use_dlna) {
         do {
+            if(use_splitter)
+                channel_list = open_list_file("ISDB");
             tdata.stream_queue = stream_queue;
             tdata.streamer = malloc(sizeof(streamer));
             if ( tdata.streamer == NULL ) {
@@ -1531,6 +1659,8 @@
     pthread_join(ipc_thread, NULL);
     if ( use_dlna ) {
         pthread_join(dlna_thread, NULL);
+        if ( channel_list )
+            close_list_file(channel_list);
     }
 
     /* close tuner */