diff options
author | Jens Georg <mail@jensge.org> | 2016-05-15 22:07:31 +0200 |
---|---|---|
committer | Jens Georg <mail@jensge.org> | 2016-06-26 23:09:46 +0200 |
commit | 15bdb67cd357e018caa7555065c80fe7be1b828a (patch) | |
tree | e5e898f57b872caa1667bd8efd454ad6fd72896f | |
parent | 07ffa9196955c72ea19aa4219c0d6335fe065823 (diff) | |
download | rygel-15bdb67cd357e018caa7555065c80fe7be1b828a.tar.gz |
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>
7 files changed, 138 insertions, 319 deletions
diff --git a/src/plugins/media-export/Makefile.am b/src/plugins/media-export/Makefile.am index cee3351f..df59e886 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 783c3bf6..1c4678d3 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 9e950aa8..4308307f 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 261a7c12..b0ed62c8 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-info-serializer.vala b/src/plugins/media-export/rygel-media-export-info-serializer.vala deleted file mode 100644 index 203747ef..00000000 --- a/src/plugins/media-export/rygel-media-export-info-serializer.vala +++ /dev/null @@ -1,150 +0,0 @@ -/* - * Copyright (C) 2015 Jens Georg <mail@jensge.org>. - * - * Author: Jens Georg <mail@jensge.org> - * - * This file is part of Rygel. - * - * Rygel is free software; you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * Rygel is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ - -using Gst; -using Gst.PbUtils; -using Rygel.MediaExport; - -internal errordomain InfoSerializerError { - INVALID_STREAM, - BAD_MIME -} - -internal class Rygel.InfoSerializer : GLib.Object { - private VariantType? t = null; - - public Variant serialize (VariantDict in_v) throws Error { - return new Variant ("(smvmvmvmvmvmv)", - in_v.lookup_value - (Serializer.UPNP_CLASS, this.t).get_string (), - this.serialize_file_info (in_v), - this.serialize_dlna_profile (in_v), - this.serialize_info (in_v), - this.serialize_audio_info (in_v), - this.serialize_video_info (in_v), - this.serialize_meta_data (in_v)); - } - - private Variant serialize_file_info (VariantDict in_v) { - return new Variant ("(stst)", - in_v.lookup_value (Serializer.TITLE, - this.t).get_string (), - in_v.lookup_value (Serializer.MODIFIED, - this.t).get_uint64 (), - in_v.lookup_value (Serializer.MIME_TYPE, - this.t).get_string (), - in_v.lookup_value (Serializer.SIZE, - this.t).get_uint64 ()); - } - - private Variant? serialize_dlna_profile (VariantDict in_v) { - var val = in_v.lookup_value (Serializer.DLNA_PROFILE, this.t); - if (val == null) { - return null; - } - - return new Variant ("(ss)", - val.get_string (), - in_v.lookup_value - (Serializer.MIME_TYPE, this.t).get_string ()); - } - - private Variant? serialize_info (VariantDict in_v) { - long duration = -1; - in_v.lookup (Serializer.DURATION, "i", out duration); - - string? title = null; - in_v.lookup (Serializer.TITLE, "s", out title); - - string? date = null; - in_v.lookup (Serializer.DATE, "s", out date); - - return new Variant ("(msmsi)", - title, - date, - duration); - } - - private Variant? serialize_video_info (VariantDict in_v) { - int width = -1; - int height = -1; - int depth = -1; - - in_v.lookup (Serializer.VIDEO_WIDTH, "i", out width); - in_v.lookup (Serializer.VIDEO_HEIGHT, "i", out height); - in_v.lookup (Serializer.VIDEO_DEPTH, "i", out depth); - - - if (width == -1 && height == -1) { - return null; - } - - return new Variant ("(iii)", width, height, depth); - } - - private Variant? serialize_audio_info (VariantDict in_v) { - int channels = -1; - int rate = -1; - - in_v.lookup (Serializer.AUDIO_CHANNELS, "i", out channels); - in_v.lookup (Serializer.AUDIO_RATE, "i", out rate); - - if (channels == -1 && rate == -1) { - return null; - } - - return new Variant ("(ii)", channels, rate); - } - - private Variant? serialize_meta_data (VariantDict in_v) { - string artist = null; - in_v.lookup (Serializer.ARTIST, "s", out artist); - - string album = null; - in_v.lookup (Serializer.ALBUM, "s", out album); - - string genre = null; - in_v.lookup (Serializer.GENRE, "s", out genre); - - int volume = -1; - in_v.lookup (Serializer.VOLUME_NUMBER, "i", out volume); - - int track = -1; - in_v.lookup (Serializer.TRACK_NUMBER, "i", out track); - - int bitrate = -1; - in_v.lookup (Serializer.AUDIO_BITRATE, "i", out bitrate); - - if (artist == null && album == null && genre == null && - volume == -1 && track == -1 && bitrate == -1) { - return null; - } - - return new Variant ("(msmsmsiii)", - artist, - album, - genre, - volume, - track, - bitrate); - } -} 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 c872810d..f8109be3 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 20e2e2c0..647a215e 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 () { |