utils/dvb: write channels into the file
authorMauro Carvalho Chehab <mchehab@redhat.com>
Tue, 3 Jan 2012 19:39:06 +0000 (17:39 -0200)
committerMauro Carvalho Chehab <mchehab@redhat.com>
Sat, 7 Jan 2012 13:12:15 +0000 (11:12 -0200)
Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>
utils/dvb/dvb-file.c
utils/dvb/dvb-file.h
utils/dvb/dvbv5-scan.c
utils/dvb/libscan.c

index 85804d67334bdb145d8b956a627863f50cddf847..fc4edf868222887f4cfb5cdd12cd4ca70f5a9fe1 100644 (file)
@@ -22,6 +22,7 @@
 #include <string.h>
 
 #include "dvb-file.h"
+#include "libscan.h"
 
 static const char *parm_name(const struct parse_table *table)
 {
@@ -223,12 +224,12 @@ int write_dvb_file(const char *fname, struct dvb_file *dvb_file)
 
                        fprintf(fp, "\tVIDEO_PID =");
                        for (i = 0; i < entry->video_pid_len; i++)
-                               fprintf(fp, " %d\n", entry->video_pid[i]);
+                               fprintf(fp, " %d", entry->video_pid[i]);
                        fprintf(fp, "\n");
 
                        fprintf(fp, "\tAUDIO_PID =");
                        for (i = 0; i < entry->audio_pid_len; i++)
-                               fprintf(fp, " %d\n", entry->audio_pid[i]);
+                               fprintf(fp, " %d", entry->audio_pid[i]);
                        fprintf(fp, "\n");
 
                        if (entry->pol != POLARIZATION_OFF) {
@@ -264,3 +265,80 @@ int write_dvb_file(const char *fname, struct dvb_file *dvb_file)
        fclose(fp);
        return 0;
 };
+
+int store_dvb_channel(struct dvb_file **dvb_file,
+                     struct dvb_v5_fe_parms *parms,
+                     struct dvb_descriptors *dvb_desc,
+                     int get_detected)
+{
+       struct dvb_entry *entry;
+       int i, j;
+
+       if (!*dvb_file) {
+               *dvb_file = calloc(sizeof(*dvb_file), 1);
+               if (!*dvb_file) {
+                       perror("Allocating memory for dvb_file");
+                       return -1;
+               }
+       }
+
+       /* Go to the last entry */
+       entry = (*dvb_file)->first_entry;
+       while (entry && entry->next)
+               entry = entry->next;
+
+       for (i = 0; i < dvb_desc->sdt_table.service_table_len; i++) {
+               struct service_table *service_table = &dvb_desc->sdt_table.service_table[i];
+               struct pat_table *pat_table = &dvb_desc->pat_table;
+               struct pid_table *pid_table = NULL;
+
+               if (!entry) {
+                       (*dvb_file)->first_entry = calloc(sizeof(*entry), 1);
+                       entry = (*dvb_file)->first_entry;
+               } else {
+                       entry->next = calloc(sizeof(*entry), 1);
+                       entry = entry->next;
+               }
+               if (!entry) {
+                       fprintf(stderr, "Not enough memory\n");
+                       return -1;
+               }
+
+               entry->channel = service_table->service_name;
+               entry->service_id = service_table->service_id;
+
+               for (j = 0; j < pat_table->pid_table_len; j++) {
+                       pid_table = &pat_table->pid_table[j];
+                       if (service_table->service_id == pid_table->program_number)
+                               break;
+               }
+               if (j == pat_table->pid_table_len) {
+                       fprintf(stderr, "Service ID 0x%04x not found!\n",
+                             service_table->service_id);
+                       return -1;
+               }
+               entry->video_pid = calloc(sizeof(*entry[i].video_pid),
+                                           pid_table->video_pid_len);
+               for (j = 0; j < pid_table->video_pid_len; j++)
+                       entry->video_pid[j] = pid_table->video_pid[j];
+               entry->video_pid_len = pid_table->video_pid_len;
+
+               entry->audio_pid = calloc(sizeof(*entry[i].audio_pid),
+                                           pid_table->audio_pid_len);
+               for (j = 0; j < pid_table->audio_pid_len; j++)
+                       entry->audio_pid[j] = pid_table->audio_pid[j];
+               entry->audio_pid_len = pid_table->audio_pid_len;
+
+               /* Copy data from parms */
+               if (get_detected)
+                       dvb_fe_get_parms(parms);
+
+               for (j = 0; j < parms->n_props; j++) {
+                       entry->props[j].cmd = parms->dvb_prop[j].cmd;
+                       entry->props[j].u.data = parms->dvb_prop[j].u.data;
+               }
+               entry->n_props = parms->n_props;
+       }
+
+       return 0;
+}
index 438aba328ba695c423918754967e36f92772a49a..c8df79495a86ea3cd6195a5703883ad3987b7270 100644 (file)
@@ -67,6 +67,8 @@ struct parse_struct {
 #define DTV_SERVICE_ID          (DTV_MAX_COMMAND + 203)
 #define DTV_CH_NAME             (DTV_MAX_COMMAND + 204)
 
+struct dvb_descriptors;
+
 static inline void dvb_file_free(struct dvb_file *dvb_file)
 {
        struct dvb_entry *entry = dvb_file->first_entry, *next;
@@ -74,7 +76,10 @@ static inline void dvb_file_free(struct dvb_file *dvb_file)
                next = entry->next;
                if (entry->channel)
                        free (entry->channel);
-               free (entry);
+               if (entry->video_pid)
+                       free (entry->video_pid);
+               if (entry->audio_pid)
+                       free (entry->audio_pid);
                entry = next;
        }
        free (dvb_file);
@@ -90,4 +95,11 @@ extern const const struct parse_struct zap_formats[];
 struct dvb_file *parse_format_oneline(const char *fname, const char *delimiter,
                                      uint32_t delsys,
                                      const struct parse_struct *formats);
+
 int write_dvb_file(const char *fname, struct dvb_file *dvb_file);
+
+int store_dvb_channel(struct dvb_file **dvb_file,
+                     struct dvb_v5_fe_parms *parms,
+                     struct dvb_descriptors *dvb_desc,
+                     int get_detected);
+
index 2429373f9014047a9bc113861b46c5c717119aac..a9fa51f243de7e8af09fd1b086fc0292f0166aa4 100644 (file)
@@ -207,6 +207,7 @@ int main(int argc, char **argv)
        int opt;
        struct dvb_v5_fe_parms *parms;
        struct dvb_descriptors *dvb_desc;
+       struct dvb_file *dvb_file = NULL;
 
        while ((opt = getopt(argc, argv, "H?hrpxRsFSn:a:f:d:c:t:o:")) != -1) {
                switch (opt) {
@@ -276,7 +277,8 @@ int main(int argc, char **argv)
 
        dvb_desc = get_dvb_ts_tables(DEMUX_DEV);
 
-//     write_dvb_file("dvb_channels.conf", dvb_file);
+       store_dvb_channel(&dvb_file, parms, dvb_desc, 0);
+       write_dvb_file("dvb_channels.conf", dvb_file);
 
        dvb_fe_close(parms);
        return 0;
index 714362aa2bf1dadbb26ae04c431fa304f8573a6b..3128fe45f0c78a55dfb6af73c2c1b333d77862f6 100644 (file)
@@ -119,17 +119,18 @@ static void parse_pmt(struct dvb_descriptors *dvb_desc,
                        printf("other pid (type 0x%02x) 0x%04x\n", buf[0], pid);
                };
 
+#if 0 /* FIXME */
                while (len > 0) {
-                       int dlen = ((int)buf[6]) + 2;
+                       int dlen = ((int)buf[5]) + 2;
 
-                       parse_pmt_descriptor(dvb_desc, &buf[5], len, NULL);
+                       parse_pmt_descriptor(dvb_desc, &buf[6], len, NULL);
                        buf += dlen;
                        section_length   -= dlen;
                        len -= dlen;
                }
-
-               buf += 5;
-               *section_length -= 5;
+#endif
+               buf += 5 + len;
+               *section_length -= 5 + len;
        };
 }
 
@@ -169,8 +170,8 @@ static void parse_nit(struct dvb_descriptors *dvb_desc,
                               id, nit_table->tr_table[n].tr_id);
                        continue;
                } else {
-                       printf("Transport stream ID 0x%04x, len %d\n",
-                               nit_table->tr_table[n].tr_id, len);
+                       printf("Transport stream #%d ID 0x%04x, len %d\n",
+                               n, nit_table->tr_table[n].tr_id, len);
 
                        parse_nit_descriptor(dvb_desc, &buf[6], len,
                                        &nit_table->tr_table[n]);
@@ -211,7 +212,8 @@ static void parse_sdt(struct dvb_descriptors *dvb_desc,
                        sdt_table->service_table[n].running = (buf[3] >> 5) & 0x7;
                        sdt_table->service_table[n].scrambled = (buf[3] >> 4) & 1;
 
-                       printf("Service ID 0x%04x, running %d, scrambled %d\n",
+                       printf("Service #%d ID 0x%04x, running %d, scrambled %d\n",
+                              n,
                               sdt_table->service_table[n].service_id,
                               sdt_table->service_table[n].running,
                               sdt_table->service_table[n].scrambled);