2 * Copyright (C) 2008 Zeeshan Ali <zeenix@gmail.com>.
3 * Copyright (C) 2008 Nokia Corporation.
5 * Author: Zeeshan Ali <zeenix@gmail.com>
7 * This file is part of Rygel.
9 * Rygel is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * Rygel is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 * Represents MediaExport item.
30 namespace Rygel.MediaExport.ItemFactory {
31 public static MediaItem create_simple (MediaContainer parent,
36 var title = file.get_basename ();
39 if (mime.has_prefix ("video/")) {
40 item = new VideoItem (MediaCache.get_id (file), parent, title);
41 } else if (mime.has_prefix ("image/")) {
42 item = new PhotoItem (MediaCache.get_id (file), parent, title);
44 item = new MusicItem (MediaCache.get_id (file), parent, title);
47 item.mime_type = mime;
48 item.size = (int64) size;
49 item.modified = mtime;
50 item.add_uri (file.get_uri ());
55 public static MediaItem? create_from_info (MediaContainer parent,
57 GUPnP.DLNAInformation dlna_info,
62 string id = MediaCache.get_id (file);
63 GLib.List<DiscovererAudioInfo> audio_streams;
64 GLib.List<DiscovererVideoInfo> video_streams;
66 audio_streams = dlna_info.info.get_audio_streams ();
67 video_streams = dlna_info.info.get_video_streams ();
69 if (audio_streams == null && video_streams == null) {
70 debug ("%s had neither audio nor video/picture " +
77 if (audio_streams == null && video_streams.data.is_image()) {
78 item = new PhotoItem (id, parent, "");
79 return fill_photo_item (item as PhotoItem,
86 } else if (audio_streams != null && video_streams != null) {
87 item = new VideoItem (id, parent, "");
88 return fill_video_item (item as VideoItem,
96 } else if (audio_streams != null) {
97 item = new MusicItem (id, parent, "");
98 return fill_music_item (item as MusicItem,
110 private static void fill_audio_item (AudioItem item,
111 DLNAInformation dlna_info,
112 DiscovererAudioInfo? audio_info) {
113 if (dlna_info.info.get_duration () > 0) {
114 item.duration = (long) (dlna_info.info.get_duration () / Gst.SECOND);
119 if (audio_info != null) {
120 if (audio_info.get_tags () != null) {
122 audio_info.get_tags ().get_uint (TAG_BITRATE, out tmp);
123 item.bitrate = (int) tmp / 8;
125 item.channels = (int) audio_info.get_channels ();
126 item.sample_freq = (int) audio_info.get_sample_rate ();
131 private static MediaItem fill_video_item (VideoItem item,
133 DLNAInformation dlna_info,
134 DiscovererVideoInfo video_info,
135 DiscovererAudioInfo? audio_info,
139 fill_audio_item (item as AudioItem, dlna_info, audio_info);
140 fill_media_item (item, file, dlna_info, mime, size, mtime);
142 item.width = (int) video_info.get_width ();
143 item.height = (int) video_info.get_height ();
144 item.color_depth = (int) video_info.get_depth ();
149 private static MediaItem fill_photo_item (PhotoItem item,
151 DLNAInformation dlna_info,
152 DiscovererVideoInfo video_info,
156 fill_media_item (item, file, dlna_info, mime, size, mtime);
158 item.width = (int) video_info.get_width ();
159 item.height = (int) video_info.get_height ();
160 item.color_depth = (int) video_info.get_depth ();
165 private static MediaItem fill_music_item (MusicItem item,
167 DLNAInformation dlna_info,
168 DiscovererAudioInfo? audio_info,
172 fill_audio_item (item as AudioItem, dlna_info, audio_info);
173 fill_media_item (item, file, dlna_info, mime, size, mtime);
175 if (audio_info != null) {
176 if (audio_info.get_tags () != null) {
177 unowned Gst.Buffer buffer;
178 audio_info.get_tags ().get_buffer (TAG_IMAGE, out buffer);
179 if (buffer != null) {
180 var structure = buffer.caps.get_structure (0);
182 structure.get_enum ("image-type",
183 typeof (Gst.TagImageType),
185 switch (image_type) {
186 case TagImageType.UNDEFINED:
187 case TagImageType.FRONT_COVER:
188 var store = MediaArtStore.get_default ();
189 var thumb = store.get_media_art_file ("album",
193 var writer = new JPEGWriter ();
194 writer.write (buffer, thumb);
195 } catch (Error error) {}
202 dlna_info.info.get_tags ().get_string (TAG_ARTIST, out item.artist);
203 dlna_info.info.get_tags ().get_string (TAG_ALBUM, out item.album);
204 dlna_info.info.get_tags ().get_string (TAG_GENRE, out item.genre);
207 dlna_info.info.get_tags() .get_uint (TAG_TRACK_NUMBER, out tmp);
208 item.track_number = (int) tmp;
214 private static void fill_media_item (MediaItem item,
216 DLNAInformation dlna_info,
222 if (dlna_info.info.get_tags () == null ||
223 !dlna_info.info.get_tags ().get_string (TAG_TITLE, out title)) {
224 title = file.get_basename ();
229 if (dlna_info.info.get_tags () != null) {
231 if (dlna_info.info.get_tags ().get_date (TAG_DATE, out date)) {
232 char[] datestr = new char[30];
233 date.strftime (datestr, "%F");
234 item.date = (string) datestr;
238 // use mtime if no time tag was available
239 if (item.date == null) {
240 TimeVal tv = { (long) mtime, 0 };
241 item.date = tv.to_iso8601 ();
244 item.size = (int64) size;
245 item.modified = (int64) mtime;
247 if (dlna_info.name != null) {
248 item.dlna_profile = dlna_info.name;
249 item.mime_type = dlna_info.mime;
251 item.mime_type = mime;
254 item.add_uri (file.get_uri ());