[farsight2/master] Make load_codecs_cache return a GError

Olivier Crête olivier.crete at collabora.co.uk
Tue Dec 23 15:19:43 PST 2008


---
 gst-libs/gst/farsight/fs-conference-iface.h  |    4 ++-
 gst/fsrtpconference/fs-rtp-codec-cache.c     |   49 +++++++++++++++-----------
 gst/fsrtpconference/fs-rtp-codec-cache.h     |    2 +-
 gst/fsrtpconference/fs-rtp-discover-codecs.c |    4 +-
 4 files changed, 34 insertions(+), 25 deletions(-)

diff --git a/gst-libs/gst/farsight/fs-conference-iface.h b/gst-libs/gst/farsight/fs-conference-iface.h
index 17c1f72..4c8b2f3 100644
--- a/gst-libs/gst/farsight/fs-conference-iface.h
+++ b/gst-libs/gst/farsight/fs-conference-iface.h
@@ -85,6 +85,7 @@ GType fs_conference_get_type (void);
  * @FS_ERROR_NETWORK: A network related error
  * @FS_ERROR_NOT_IMPLEMENTED: This functionality is not implemented
  * by this plugins
+ * @FS_ERROR_INTERNAL: An internal error happened in Farsight
  *
  * This is the enum of error numbers that will come either on the "error"
  * signal or from the Gst Bus.
@@ -94,7 +95,8 @@ typedef enum {
   FS_ERROR_CONSTRUCTION,
   FS_ERROR_INVALID_ARGUMENTS,
   FS_ERROR_NETWORK,
-  FS_ERROR_NOT_IMPLEMENTED
+  FS_ERROR_NOT_IMPLEMENTED,
+  FS_ERROR_INTERNAL
 } FsError;
 
 /**
diff --git a/gst/fsrtpconference/fs-rtp-codec-cache.c b/gst/fsrtpconference/fs-rtp-codec-cache.c
index abf53b5..ec7cfb7 100644
--- a/gst/fsrtpconference/fs-rtp-codec-cache.c
+++ b/gst/fsrtpconference/fs-rtp-codec-cache.c
@@ -30,6 +30,8 @@
 
 #include "fs-rtp-codec-cache.h"
 
+#include <gst/farsight/fs-conference-iface.h>
+
 #include <string.h>
 #include <unistd.h>
 
@@ -80,7 +82,7 @@ static gboolean codecs_cache_valid(gchar *cache_path) {
 }
 
 static gchar *
-get_codecs_cache_path(FsMediaType media_type) {
+get_codecs_cache_path (FsMediaType media_type, GError **error) {
   gchar *cache_path;
 
   if (media_type == FS_MEDIA_TYPE_AUDIO) {
@@ -96,7 +98,8 @@ get_codecs_cache_path(FsMediaType media_type) {
           "codecs.video." HOST_CPU ".cache", NULL);
     }
   } else {
-    g_warning ("Unknown media type %d for cache loading", media_type);
+    g_set_error (error, FS_ERROR, FS_ERROR_INVALID_ARGUMENTS,
+      "Unknown media type %d for cache loading", media_type);
     return NULL;
   }
 
@@ -237,7 +240,7 @@ load_codec_blueprint (FsMediaType media_type, gchar **in, gsize *size) {
  *
  */
 GList *
-load_codecs_cache (FsMediaType media_type)
+load_codecs_cache (FsMediaType media_type, GError **error)
 {
   GMappedFile *mapped = NULL;
   gchar *contents = NULL;
@@ -258,11 +261,15 @@ load_codecs_cache (FsMediaType media_type)
   } else if(media_type == FS_MEDIA_TYPE_VIDEO) {
     magic_media = 'V';
   } else {
-    g_warning ("Invalid media type %d", media_type);
+    g_set_error (error, FS_ERROR, FS_ERROR_INVALID_ARGUMENTS,
+      "Invalid media type %d", media_type);
     return FALSE;
   }
 
-  cache_path = get_codecs_cache_path(media_type);
+  cache_path = get_codecs_cache_path(media_type, error);
+
+  if (!cache_path)
+    return FALSE;
 
   if (!codecs_cache_valid(cache_path)) {
     g_debug ("Codecs cache %s is outdated or does not exist", cache_path);
@@ -273,20 +280,17 @@ load_codecs_cache (FsMediaType media_type)
   g_debug ("Loading codecs cache %s", cache_path);
 
   mapped = g_mapped_file_new (cache_path, FALSE, &err);
-  if (err != NULL) {
-    g_debug ("Unable to mmap file %s : %s", cache_path, err->message);
-    g_error_free (err);
-    err = NULL;
-
-    g_file_get_contents (cache_path, &contents, &size, &err);
-    if (err != NULL) {
-      g_warning ("Unable to read file %s : %s", cache_path, err->message);
-      g_error_free (err);
+  if (mapped == NULL) {
+    g_debug ("Unable to mmap file %s : %s", cache_path,
+      err ? err->message: "unknown error");
+    g_clear_error (&err);
+
+    if(!g_file_get_contents (cache_path, &contents, &size, error))
       goto error;
-    }
   } else {
     if ((contents = g_mapped_file_get_contents (mapped)) == NULL) {
-      g_warning ("Can't load file %s : %s", cache_path, g_strerror (errno));
+      g_set_error (error, FS_ERROR, FS_ERROR_INTERNAL,
+        "Can't load file %s : %s", cache_path, g_strerror (errno));
       goto error;
     }
     /* check length for header */
@@ -298,7 +302,7 @@ load_codecs_cache (FsMediaType media_type)
   in = contents;
 
   if (size < sizeof (magic)) {
-    g_warning ("Cache file corrupt");
+    g_set_error (error, FS_ERROR, FS_ERROR_INTERNAL, "Cache file corrupt");
     goto error;
   }
 
@@ -312,12 +316,14 @@ load_codecs_cache (FsMediaType media_type)
       magic[3] != 'C' ||
       magic[4] != '1' ||
       magic[5] != '0') {
-    g_warning ("Cache file has incorrect magic header. File corrupted");
+    g_set_error (error, FS_ERROR, FS_ERROR_INTERNAL,
+      "Cache file has incorrect magic header. File corrupted");
     goto error;
   }
 
   if (size < sizeof(gint)) {
-    g_warning ("Cache file corrupt");
+    g_set_error (error, FS_ERROR, FS_ERROR_INTERNAL,
+      "Cache file corrupt (size: %d < sizeof(int))", size);
     goto error;
   }
 
@@ -328,7 +334,8 @@ load_codecs_cache (FsMediaType media_type)
   for (i = 0; i < num_blueprints; i++) {
     CodecBlueprint *blueprint = load_codec_blueprint (media_type, &in, &size);
     if (!blueprint) {
-      g_warning ("Problem reading codecs cache. File corrupt");
+      g_set_error (error, FS_ERROR, FS_ERROR_INTERNAL,
+        "Can not load all of the blueprints, cache corrupted");
 
       if (blueprints) {
         g_list_foreach (blueprints, (GFunc) codec_blueprint_destroy, NULL);
@@ -446,7 +453,7 @@ save_codecs_cache(FsMediaType media_type, GList *blueprints)
   int size;
   gchar magic[8] = {0};
 
-  cache_path = get_codecs_cache_path(media_type);
+  cache_path = get_codecs_cache_path(media_type, NULL);
   if (!cache_path)
     return FALSE;
 
diff --git a/gst/fsrtpconference/fs-rtp-codec-cache.h b/gst/fsrtpconference/fs-rtp-codec-cache.h
index 0aa582e..6d09b45 100644
--- a/gst/fsrtpconference/fs-rtp-codec-cache.h
+++ b/gst/fsrtpconference/fs-rtp-codec-cache.h
@@ -31,7 +31,7 @@
 
 G_BEGIN_DECLS
 
-GList *load_codecs_cache(FsMediaType media_type);
+GList *load_codecs_cache(FsMediaType media_type, GError **error);
 gboolean save_codecs_cache(FsMediaType media_type, GList *codec_blueprints);
 
 
diff --git a/gst/fsrtpconference/fs-rtp-discover-codecs.c b/gst/fsrtpconference/fs-rtp-discover-codecs.c
index 9f13680..03951ba 100644
--- a/gst/fsrtpconference/fs-rtp-discover-codecs.c
+++ b/gst/fsrtpconference/fs-rtp-discover-codecs.c
@@ -222,8 +222,8 @@ load_codecs (FsMediaType media_type)
     elem_config = load_config_file();
   }
 #endif
-
-  if (load_codecs_cache(media_type)) {
+  list_codec_blueprints[media_type] = load_codecs_cache(media_type, NULL);
+  if (list_codec_blueprints[media_type]) {
     g_debug("Loaded codec blueprints from cache file");
     return TRUE;
   }
-- 
1.5.6.5




More information about the farsight-commits mailing list