[rygel] media-export: Remove Variant conversion



commit 15bdb67cd357e018caa7555065c80fe7be1b828a
Author: Jens Georg <mail jensge org>
Date:   Sun May 15 22:07:31 2016 +0200

    media-export: Remove Variant conversion
    
    Use the a{sv} Variant as transport between extracting process and Rygel and
    directly translate it into MediaItem in harvesting task.
    
    Signed-off-by: Jens Georg <mail jensge org>

 src/plugins/media-export/Makefile.am               |    6 +-
 src/plugins/media-export/constants.vala            |   27 ++-
 .../rygel-media-export-dvd-parser.vala             |   19 ++-
 .../media-export/rygel-media-export-extractor.vala |   14 +-
 .../rygel-media-export-info-serializer.vala        |  150 -------------
 .../rygel-media-export-item-factory.vala           |  235 ++++++++------------
 .../rygel-media-export-metadata-extractor.vala     |    2 +-
 7 files changed, 136 insertions(+), 317 deletions(-)
---
diff --git a/src/plugins/media-export/Makefile.am b/src/plugins/media-export/Makefile.am
index cee3351..df59e88 100644
--- a/src/plugins/media-export/Makefile.am
+++ b/src/plugins/media-export/Makefile.am
@@ -15,8 +15,7 @@ mx_extract_SOURCES = \
        rygel-media-export-playlist-extractor.vala \
        rygel-media-export-image-extractor.vala \
        rygel-media-export-extractor.vala \
-       rygel-media-export-generic-extractor.vala \
-       rygel-media-export-info-serializer.vala
+       rygel-media-export-generic-extractor.vala
 
 mx_extract_VALAFLAGS = \
        --enable-experimental \
@@ -67,7 +66,8 @@ librygel_media_export_la_SOURCES = \
        rygel-media-export-trackable-db-container.vala \
        rygel-media-export-updatable-object.vala \
        rygel-media-export-dvd-container.vala \
-       rygel-media-export-dvd-track.vala
+       rygel-media-export-dvd-track.vala \
+       constants.vala
 
 librygel_media_export_la_VALAFLAGS = \
        --enable-experimental \
diff --git a/src/plugins/media-export/constants.vala b/src/plugins/media-export/constants.vala
index 783c3bf..1c4678d 100644
--- a/src/plugins/media-export/constants.vala
+++ b/src/plugins/media-export/constants.vala
@@ -21,23 +21,36 @@
  */
 
 namespace Rygel.MediaExport.Serializer {
+    // Generic things - always set
+    public const string UPNP_CLASS = "UPnPClass";
+    public const string ID = "Id";
+    public const string URI = "Uri";
     public const string TITLE = "Title";
+    public const string DATE = "Date";
+
+    // Item things - always set
     public const string MODIFIED = "MTime";
     public const string MIME_TYPE = "MimeType";
     public const string SIZE = "Size";
-    public const string UPNP_CLASS = "UPnPClass";
+
+    // Item things
+    public const string DLNA_PROFILE = "DLNAProfile";
+
+    // AudioItem
     public const string DURATION = "Duration";
+    public const string AUDIO_CHANNELS = "AudioChannels";
+    public const string AUDIO_RATE = "AudioRate";
+    public const string AUDIO_BITRATE = "AudioBitrate";
+
+    // VisualItem
     public const string VIDEO_WIDTH = "VideoWidth";
     public const string VIDEO_HEIGHT = "VideoHeight";
-    public const string DLNA_PROFILE = "DLNAProfile";
-    public const string ARTIST = "Artist";
-    public const string DATE = "Date";
     public const string VIDEO_DEPTH = "VideoDepth";
-    public const string AUDIO_CHANNELS = "AudioChannels";
-    public const string AUDIO_RATE = "AudioRate";
+
+    // MusicItem
+    public const string ARTIST = "Artist";
     public const string ALBUM = "Album";
     public const string GENRE = "Genre";
     public const string VOLUME_NUMBER = "VolumeNumber";
     public const string TRACK_NUMBER = "TrackNumber";
-    public const string AUDIO_BITRATE = "AudioBitrate";
 }
diff --git a/src/plugins/media-export/rygel-media-export-dvd-parser.vala 
b/src/plugins/media-export/rygel-media-export-dvd-parser.vala
index 9e950aa..4308307 100644
--- a/src/plugins/media-export/rygel-media-export-dvd-parser.vala
+++ b/src/plugins/media-export/rygel-media-export-dvd-parser.vala
@@ -73,15 +73,30 @@ internal class Rygel.MediaExport.DVDParser : Extractor {
             throw new DVDParserError.GENERAL ("Failed to read cache file");
         }
 
+        var id = this.serialized_info.lookup_value (Serializer.ID,
+                                                    VariantType.STRING);
+        var uri = this.serialized_info.lookup_value (Serializer.URI,
+                                                     VariantType.STRING);
+
+        // Unset size
+        this.serialized_info.insert (Serializer.SIZE, "i", -1);
+
         var context = new Xml.XPath.Context (doc);
         var xpo = context.eval ("/lsdvd/track");
         if ((xpo != null) &&
             (xpo->type == Xml.XPath.ObjectType.NODESET) &&
             (xpo->nodesetval->length () == 1)) {
+            var new_uri = new Soup.URI (uri.get_string ());
+            new_uri.set_scheme ("dvd");
+            new_uri.set_query ("title=1");
             this.serialized_info.insert (Serializer.UPNP_CLASS,
                                          "s",
-                                         UPNP_CLASS_DVD_TRACK);
+                                         UPNP_CLASS_VIDEO);
+            this.serialized_info.insert (Serializer.ID, "s",
+                                         "dvd-track:" + id.get_string () + ":0");
             this.serialized_info.insert (Serializer.MIME_TYPE, "s", "video/mpeg");
+            this.serialized_info.insert (Serializer.URI, "s",
+                                         new_uri.to_string (false));
 
             var node = xpo->nodesetval->item (0);
 
@@ -114,6 +129,8 @@ internal class Rygel.MediaExport.DVDParser : Extractor {
                 it = it->next;
             }
         } else {
+            this.serialized_info.insert (Serializer.ID, "s",
+                                         "dvd:" + id.get_string ());
             this.serialized_info.insert (Serializer.UPNP_CLASS,
                                          "s",
                                          UPNP_CLASS_PLAYLIST_CONTAINER_DVD);
diff --git a/src/plugins/media-export/rygel-media-export-extractor.vala 
b/src/plugins/media-export/rygel-media-export-extractor.vala
index 261a7c1..b0ed62c 100644
--- a/src/plugins/media-export/rygel-media-export-extractor.vala
+++ b/src/plugins/media-export/rygel-media-export-extractor.vala
@@ -97,8 +97,6 @@ public class Rygel.MediaExport.Extractor : Object {
                                                  FileAttribute.STANDARD_DISPLAY_NAME,
                                                  FileQueryInfoFlags.NONE);
         var display_name = file_info.get_display_name ();
-        this.serialized_info.insert ("DisplayName", "s", display_name);
-
         var title = this.strip_invalid_entities (display_name);
         this.serialized_info.insert (Serializer.TITLE, "s", title);
 
@@ -109,15 +107,15 @@ public class Rygel.MediaExport.Extractor : Object {
                                         (file_info.get_content_type ());
         this.serialized_info.insert (Serializer.MIME_TYPE, "s", content_type);
         this.serialized_info.insert (Serializer.SIZE, "t", file_info.get_size ());
+        var id = Checksum.compute_for_string (ChecksumType.MD5,
+                                              file.get_uri ());
+        this.serialized_info.insert (Serializer.ID, "s", id);
+        this.serialized_info.insert (Serializer.URI, "s",
+                                     file.get_uri ());
      }
 
     public new Variant? @get () {
-        var s = new Rygel.InfoSerializer ();
-        try {
-            return s.serialize (this.serialized_info);
-        } catch (Error error) {
-            return null;
-        }
+        return this.serialized_info.end ();
     }
 
     private string strip_invalid_entities (string original) {
diff --git a/src/plugins/media-export/rygel-media-export-item-factory.vala 
b/src/plugins/media-export/rygel-media-export-item-factory.vala
index c872810..f8109be 100644
--- a/src/plugins/media-export/rygel-media-export-item-factory.vala
+++ b/src/plugins/media-export/rygel-media-export-item-factory.vala
@@ -33,6 +33,7 @@ using Gst.PbUtils;
  * Represents MediaExport item.
  */
 namespace Rygel.MediaExport.ItemFactory {
+
     internal errordomain ItemFactoryError {
         DESERIALIZATION,
         MISMATCH
@@ -50,193 +51,133 @@ namespace Rygel.MediaExport.ItemFactory {
         return true;
     }
 
-    private static MediaFileItem? create_playlist_item (File file,
-                                                        MediaContainer parent,
-                                                        string fallback_title) {
-        try {
-            uint8[] contents;
-
-            if (!file.load_contents (null, out contents, null)) {
-                return null;
-            }
-
-            /* Do some heuristic check if this file looks like XML */
-            var i = 0;
-            while (((char) contents[i]).isspace () && i < contents.length) {
-                i++;
-            }
-
-            if (contents[i] != '<') {
-                return null;
-            }
-
-            var didl_s = new MediaCollection.from_string ((string) contents);
-            var author = didl_s.author;
-            var title = didl_s.title;
+    static MediaObject? create (MediaContainer parent, VariantDict vd) {
+        VariantType? expected = null;
+        MediaObject? object = null;
+
+        var upnp_class = vd.lookup_value (Serializer.UPNP_CLASS, expected);
+        var id = vd.lookup_value (Serializer.ID, expected);
+        var title = vd.lookup_value (Serializer.TITLE, expected);
+        var uri = vd.lookup_value (Serializer.URI, expected);
+
+        var factory = new ObjectFactory ();
+        if (upnp_class.get_string ().has_prefix (MediaContainer.UPNP_CLASS)) {
+            object = factory.get_container (id.get_string (),
+                                            title.get_string (),
+                                            0,
+                                            uri.get_string ());
+        } else {
+            object = factory.get_item (parent,
+                                       id.get_string (),
+                                       title.get_string (),
+                                       upnp_class.get_string ());
+        }
 
-            if (author == null &&
-                title == null &&
-                didl_s.get_items () == null) {
-                return null;
-            }
+        object.add_uri (uri.get_string ());
 
-            if (title == null) {
-                title = fallback_title;
-            }
-
-            var item = new PlaylistItem (MediaCache.get_id (file),
-                                         parent,
-                                         title);
+        return object;
+    }
 
-            if (author != null) {
-                item.creator = author;
-            }
+    static int64 get_int64 (VariantDict vd, string key) {
+        var val = vd.lookup_value (key, VariantType.UINT64);
+        if (val != null) {
+            return (int64) val.get_uint64 ();
+        }
 
-            item.dlna_profile = "DIDL_S";
+        return -1;
+    }
 
-            return item;
-        } catch (Error e) {
-            return null;
+    static int32 get_int32 (VariantDict vd, string key) {
+        var val = vd.lookup_value (key, VariantType.INT32);
+        if (val != null) {
+            return val.get_int32 ();
         }
+
+        return -1;
     }
 
+
     static MediaObject? create_from_variant (MediaContainer parent,
                                              File           file,
                                              Variant?       v)
                                              throws Error {
-
+        VariantType? expected = null;
         if (v == null) {
             return null;
         }
 
-        ItemFactory.check_variant_type (v,"(smvmvmvmvmvmv)");
-
-        Variant? upnp_class,
-                 file_info,
-                 dlna_profile,
-                 info,
-                 video_info,
-                 audio_info,
-                 meta_data;
-
-        var it = v.iterator ();
+        ItemFactory.check_variant_type (v,"a{sv}");
 
-        var id = MediaCache.get_id (file);
+        VariantDict vd = new VariantDict (v);
 
-        upnp_class = it.next_value ();
+        var upnp_class = vd.lookup_value (Serializer.UPNP_CLASS, expected);
+        if (upnp_class == null) {
+            debug ("Did not find a UPnP class for item, ignoring");
 
-        file_info = it.next_value ().get_maybe ();
-        if (file_info != null) {
-            file_info = file_info.get_variant ();
+            return null;
         }
 
-        dlna_profile = it.next_value ().get_maybe ();
-        if (dlna_profile != null) {
-            dlna_profile = dlna_profile.get_variant ();
+        var object = create (parent, vd);
+        if (object is MediaContainer) {
+            return object;
         }
 
-        info = it.next_value ().get_maybe ();
-        if (info != null) {
-            info = info.get_variant ();
-        }
+        var item = object as MediaFileItem;
 
-        audio_info = it.next_value ().get_maybe ();
-        if (audio_info != null) {
-            audio_info = audio_info.get_variant ();
-        }
+        // Fill general things
+        var val = vd.lookup_value (Serializer.MIME_TYPE, expected);
+        item.mime_type = val.get_string ();
+
+        item.size = get_int64 (vd, Serializer.SIZE);
 
-        video_info = it.next_value ().get_maybe ();
-        if (video_info != null) {
-            video_info = video_info.get_variant ();
+        val = vd.lookup_value (Serializer.MODIFIED, expected);
+        item.modified = val.get_uint64 ();
+
+        val = vd.lookup_value (Serializer.DLNA_PROFILE, expected);
+        if (val != null) {
+            item.dlna_profile = val.get_string ();
         }
 
-        meta_data = it.next_value ().get_maybe ();
-        if (meta_data != null) {
-            meta_data = meta_data.get_variant ();
+        val = vd.lookup_value (Serializer.DATE, expected);
+        if (val != null) {
+            item.date = val.get_string ();
         }
 
-        MediaFileItem item = null;
-        MediaObject object = null;
-        switch (upnp_class.get_string ()) {
-            case Rygel.PhotoItem.UPNP_CLASS:
-                item = new PhotoItem (id, parent, "");
-                break;
-            case Rygel.VideoItem.UPNP_CLASS:
-                item = new VideoItem (id, parent, "");
-                break;
-            case Rygel.MusicItem.UPNP_CLASS:
-                item = new MusicItem (id, parent, "");
-                break;
-            case Rygel.PlaylistItem.UPNP_CLASS:
-                item = ItemFactory.create_playlist_item (file, parent, "");
-                if (item == null) {
-                    return null;
-                }
-                break;
-            case DVDContainer.UPNP_CLASS:
-                var container = new DVDContainer ("dvd:" + id, parent, "", file.get_path ());
-                object = container;
+        if (item is AudioItem) {
+            var audio_item = item as AudioItem;
+            audio_item.duration = get_int32 (vd, Serializer.DURATION);
+            audio_item.bitrate = get_int32 (vd, Serializer.AUDIO_BITRATE);
+            audio_item.channels = get_int32 (vd, Serializer.AUDIO_CHANNELS);
+            audio_item.sample_freq = get_int32 (vd, Serializer.AUDIO_RATE);
 
-                if (file_info != null) {
-                    apply_file_info (object, file_info);
+            if (item is MusicItem) {
+                var music_item = item as MusicItem;
+                val = vd.lookup_value (Serializer.ARTIST, expected);
+                if (val != null) {
+                    music_item.artist = val.get_string ();
                 }
 
-                // If the DVD has a single track, just export that as a plain
-                // video. Copy over all important information such as
-                // modified, the uri and the title.
-                if (container.child_count == 1) {
-                    object = container.children.get (0);
-                    object.title = container.title;
-                    object.modified = container.modified;
+                val = vd.lookup_value (Serializer.ALBUM, expected);
+                if (val != null) {
+                    music_item.album = val.get_string ();
                 }
 
-                return object;
-            default:
-                return null;
-        }
-
-        item.add_uri (file.get_uri ());
-
-        if (dlna_profile != null) {
-            apply_dlna_profile (item, dlna_profile);
-        }
-
-        if (info != null) {
-            apply_info (item, info);
-        }
-
-        var strip_title = item.title == null || item.title == "";
-
-        if (file_info != null) {
-            apply_file_info (item, file_info);
+                val = vd.lookup_value (Serializer.GENRE, expected);
+                if (val != null) {
+                    music_item.genre = val.get_string ();
+                }
 
-            // If AVI, don't trust the meta-data as it is not easily
-            // modifiable
-            if (item.mime_type == "video/x-msvideo") {
-                item.title = "";
-                strip_title = true;
-                apply_file_info (item, file_info);
+                music_item.track_number = get_int32 (vd,
+                                                     Serializer.TRACK_NUMBER);
+                music_item.disc = get_int32 (vd, Serializer.VOLUME_NUMBER);
             }
-
-        }
-
-        if (audio_info != null) {
-            apply_audio_info (item, audio_info);
-        }
-
-        if (video_info != null) {
-            apply_video_info (item, video_info);
-        }
-
-        if (meta_data != null) {
-            apply_meta_data (item, meta_data);
         }
 
-        // If the date has a timezone offset, make sure it contains a
-        // colon bgo#702231, DLNA 7.3.21.1
-        if ("T" in item.date) {
-            var date = new Soup.Date.from_string (item.date);
-            item.date = date.to_string (Soup.DateFormat.ISO8601_FULL);
+        if (item is VisualItem) {
+            var visual_item = item as VisualItem;
+            visual_item.width = get_int32 (vd, Serializer.VIDEO_WIDTH);
+            visual_item.height = get_int32 (vd, Serializer.VIDEO_HEIGHT);
+            visual_item.color_depth = get_int32 (vd, Serializer.VIDEO_DEPTH);
         }
 
         return item as MediaFileItem;
diff --git a/src/plugins/media-export/rygel-media-export-metadata-extractor.vala 
b/src/plugins/media-export/rygel-media-export-metadata-extractor.vala
index 20e2e2c..647a215 100644
--- a/src/plugins/media-export/rygel-media-export-metadata-extractor.vala
+++ b/src/plugins/media-export/rygel-media-export-metadata-extractor.vala
@@ -72,7 +72,7 @@ public class Rygel.MediaExport.MetadataExtractor: GLib.Object {
     extern static bool open_pipe ([CCode (array_length = false)]int[] fds, int flags) throws GLib.Error;
 
     static construct {
-        SERIALIZED_DATA_TYPE = new VariantType ("(smvmvmvmvmvmv)");
+        SERIALIZED_DATA_TYPE = new VariantType ("a{sv}");
     }
 
     public MetadataExtractor () {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]