dvb/parsechannels: implement channels.conf format autodetection
authorReynaldo H. Verdejo Pinochet <reynaldo@osg.samsung.com>
Wed, 22 Jun 2016 23:14:33 +0000 (16:14 -0700)
committerReynaldo H. Verdejo Pinochet <reynaldo@osg.samsung.com>
Sat, 25 Jun 2016 01:23:17 +0000 (18:23 -0700)
Simple autodetection avoids having to call the parsers
one after the other.

sys/dvb/parsechannels.c

index 0cb8ad9..11de6ed 100644 (file)
 
 #include <linux/dvb/frontend.h>
 
+typedef enum
+{
+  CHANNEL_CONF_FORMAT_NONE,
+  CHANNEL_CONF_FORMAT_DVBV5,
+  CHANNEL_CONF_FORMAT_ZAP
+} GstDvbChannelConfFormat;
+
 static gboolean parse_and_configure_from_v5_conf_file (GstElement * dvbbasebin,
     const gchar * filename, const gchar * channel_name, GError ** error);
 static gboolean parse_and_configure_from_zap_conf_file (GstElement * dvbbasebin,
     const gchar * filename, const gchar * channel_name, GError ** error);
+static GstDvbChannelConfFormat detect_file_format (const gchar * filename);
 
 
 GST_DEBUG_CATEGORY_EXTERN (dvb_base_bin_debug);
@@ -776,6 +784,36 @@ unknown_channel:
   }
 }
 
+static GstDvbChannelConfFormat
+detect_file_format (const gchar * filename)
+{
+  gchar *contents;
+  gchar **lines;
+  gchar **line;
+  GstDvbChannelConfFormat ret = CHANNEL_CONF_FORMAT_NONE;
+
+  if (!g_file_get_contents (filename, &contents, NULL, NULL))
+    return ret;
+
+  lines = g_strsplit (contents, "\n", 0);
+  line = lines;
+
+  while (*line) {
+    if (g_str_has_prefix (*line, "[") && g_str_has_suffix (*line, "]")) {
+      ret = CHANNEL_CONF_FORMAT_DVBV5;
+      break;
+    } else if (g_strrstr (*line, ":")) {
+      ret = CHANNEL_CONF_FORMAT_ZAP;
+      break;
+    }
+    line++;
+  }
+
+  g_strfreev (lines);
+  g_free (contents);
+  return ret;
+}
+
 gboolean
 set_properties_for_channel (GstElement * dvbbasebin,
     const gchar * channel_name, GError ** error)
@@ -790,23 +828,37 @@ set_properties_for_channel (GstElement * dvbbasebin,
         "gstreamer-" GST_API_VERSION, "dvb-channels.conf", NULL);
   }
 
-  /* TODO detect channels.conf file format */
-
   adapter = g_getenv ("GST_DVB_ADAPTER");
   if (adapter)
     g_object_set (dvbbasebin, "adapter", atoi (adapter), NULL);
 
-  if (!(ret = parse_and_configure_from_v5_conf_file (dvbbasebin, filename,
-              channel_name, error))) {
-    /* TODO only fallback if it was a parsing error */
-
-    g_clear_error (error);
-    GST_DEBUG_OBJECT (dvbbasebin, "Resorting to old ZAP format file parsing");
-
-    /* fallback to old format */
-    ret = parse_and_configure_from_zap_conf_file (dvbbasebin, filename,
-        channel_name, error);
+  switch (detect_file_format (filename)) {
+    case CHANNEL_CONF_FORMAT_DVBV5:
+      if (!parse_and_configure_from_v5_conf_file (dvbbasebin, filename,
+              channel_name, error)) {
+        GST_WARNING_OBJECT (dvbbasebin, "Could not parse libdvbv5 channel "
+            "configuration file '%s'", filename);
+      } else {
+        GST_INFO_OBJECT (dvbbasebin, "Parsed libdvbv5 channel configuration "
+            "file");
+        ret = TRUE;
+      }
+      break;
+    case CHANNEL_CONF_FORMAT_ZAP:
+      if (!parse_and_configure_from_zap_conf_file (dvbbasebin, filename,
+              channel_name, error)) {
+        GST_WARNING_OBJECT (dvbbasebin, "Could not parse ZAP channel "
+            "configuration file '%s'", filename);
+      } else {
+        GST_INFO_OBJECT (dvbbasebin, "Parsed ZAP channel configuration file");
+        ret = TRUE;
+      }
+      break;
+    default:
+      GST_WARNING_OBJECT (dvbbasebin, "Unknown configuration file format. "
+          "Can not get parameters for channel");
   }
+
   g_free (filename);
   return ret;
 }