changeset 161:ade99239f234

Enable change phisical channel by DLNA.
author Naoya OYAMA <naoya.oyama@gmail.com>
date Wed, 12 Sep 2012 21:36:39 +0900
parents ee69ddb11f93
children 185ff17f89f7
files src/recpt1.c
diffstat 1 files changed, 63 insertions(+), 35 deletions(-) [+]
line wrap: on
line diff
--- a/src/recpt1.c	Mon Sep 10 20:56:14 2012 +0900
+++ b/src/recpt1.c	Wed Sep 12 21:36:39 2012 +0900
@@ -62,6 +62,7 @@
 int tune(char *channel, thread_data *tdata, char *device);
 int close_tuner(thread_data *tdata);
 static int get_device_id_by_name ( const char *name );
+void calc_cn(int fd, int type);
 
 static struct channel_info_list *open_list_file(
        char *type,
@@ -543,9 +544,8 @@
     int code;
     int split_select_finish = TSS_ERROR;
     int old_sid = 0, new_sid = 0;
-    int old_tp = 0, new_tp = 0;
+    char *old_tp = NULL, *new_tp = NULL;
     time_t split_start_time;
-    struct channel_info *channel_info = NULL;
 
     time(&split_start_time);
 
@@ -652,52 +652,80 @@
         /*
          * 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_dlna && buf.size > 0 ) {
             if ( use_splitter ) {
-                if (ut->tp)
-                    new_tp  = atoi(ut->tp);
+                if (ut->tp) {
+                    if (!old_tp)
+                        old_tp = "0";
+                    new_tp = 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;
+                if ((new_tp) && (old_tp))
+                    if (strcmp(old_tp, new_tp)) {
+                        int current_type = data->table->type;
+                        ISDB_T_FREQ_CONV_TABLE *table = searchrecoff(new_tp);
+                        if (table == NULL) {
+                            fprintf(stderr, "Invalid Channel: %s\n", new_tp);
+                            break;
+                        }
+                        data->table = table;
+                        old_tp = new_tp;
+    
+                        /* stop stream */
+                        ioctl(data->tfd, STOP_REC, 0);
+                        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--;
+                                 /* update counters */
+                                 p_queue->num_avail++;
+                                 p_queue->num_used--;
+                            }
                         }
+                        pthread_mutex_unlock(&data->queue->mutex);
+                        if (data->table->type != current_type) {
+                            /* re-open device */
+                            if(close_tuner(data) != 0)
+                                break;
+                            tune(new_tp, data, NULL);
+                        } else {
+                            /* SET_CHANNEL only */
+                            const FREQUENCY freq = {
+                               .frequencyno = data->table->set_freq,
+                               .slot = data->table->add_freq,
+                            };
+                            if(ioctl(data->tfd, SET_CHANNEL, &freq) < 0) {
+                                fprintf(stderr, "Cannot tune to the specified channel\n");
+                                data->ch = 0;
+                                break;
+                            }
+                            data->ch = atoi(new_tp);
+                            calc_cn(data->tfd, data->table->type);
+                        }
+                        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);
                     }
-                    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->sid, NULL, NULL);
                     if ( splitter == NULL ) {
-                        fprintf (stderr, "reader_func() splitter RESTART FAILED.\n", old_sid, new_sid);
+                        fprintf (stderr, "reader_func() splitter RESTART FAILED.\n");
                         use_splitter = FALSE;
                     }
+                    fprintf (stderr, "reader_func() splitter plit_select_finish set ERROR.\n");
                     split_select_finish = TSS_ERROR;
                     free(qbuf);
                     qbuf = NULL;