9e9ff2fb3a0621b1faf0b264d8da7e95a61fae57
[profile/ivi/rygel.git] / src / plugins / media-export / rygel-media-export-item.vala
1 /*
2  * Copyright (C) 2008 Zeeshan Ali <zeenix@gmail.com>.
3  * Copyright (C) 2008 Nokia Corporation.
4  *
5  * Author: Zeeshan Ali <zeenix@gmail.com>
6  *
7  * This file is part of Rygel.
8  *
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.
13  *
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.
18  *
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.
22  */
23
24 using GUPnP;
25 using Gst;
26
27 /**
28  * Represents MediaExport item.
29  */
30 namespace Rygel.MediaExport.ItemFactory {
31     public static MediaItem create_simple (MediaContainer parent,
32                                            File           file,
33                                            string         mime,
34                                            uint64         size,
35                                            uint64         mtime) {
36         var title = file.get_basename ();
37         MediaItem item;
38
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);
43         } else {
44             item = new MusicItem (MediaCache.get_id (file), parent, title);
45         }
46
47         item.mime_type = mime;
48         item.size = (int64) size;
49         item.modified = mtime;
50         item.add_uri (file.get_uri ());
51
52         return item;
53     }
54
55     public static MediaItem? create_from_info (MediaContainer        parent,
56                                                File                  file,
57                                                GUPnP.DLNAInformation dlna_info,
58                                                string                mime,
59                                                uint64                size,
60                                                uint64                mtime) {
61         MediaItem item;
62         string id = MediaCache.get_id (file);
63         GLib.List<DiscovererAudioInfo> audio_streams;
64         GLib.List<DiscovererVideoInfo> video_streams;
65
66         audio_streams = dlna_info.info.get_audio_streams ();
67         video_streams = dlna_info.info.get_video_streams ();
68
69         if (audio_streams == null && video_streams == null) {
70             debug ("%s had neither audio nor video/picture " +
71                    "streams. Ignoring.",
72                    file.get_uri ());
73
74             return null;
75         }
76
77         if (audio_streams == null && video_streams.data.is_image()) {
78             item = new PhotoItem (id, parent, "");
79             return fill_photo_item (item as PhotoItem,
80                                     file,
81                                     dlna_info,
82                                     video_streams.data,
83                                     mime,
84                                     size,
85                                     mtime);
86         } else if (audio_streams != null && video_streams != null) {
87             item = new VideoItem (id, parent, "");
88             return fill_video_item (item as VideoItem,
89                                     file,
90                                     dlna_info,
91                                     video_streams.data,
92                                     audio_streams.data,
93                                     mime,
94                                     size,
95                                     mtime);
96         } else if (audio_streams != null) {
97             item = new MusicItem (id, parent, "");
98             return fill_music_item (item as MusicItem,
99                                     file,
100                                     dlna_info,
101                                     audio_streams.data,
102                                     mime,
103                                     size,
104                                     mtime);
105         } else {
106             return null;
107         }
108     }
109
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);
115         } else {
116             item.duration = -1;
117         }
118
119         if (audio_info != null) {
120             if (audio_info.get_tags () != null) {
121                 uint tmp;
122                 audio_info.get_tags ().get_uint (TAG_BITRATE, out tmp);
123                 item.bitrate = (int) tmp / 8;
124             }
125             item.channels = (int) audio_info.get_channels ();
126             item.sample_freq = (int) audio_info.get_sample_rate ();
127         }
128     }
129
130
131     private static MediaItem fill_video_item (VideoItem            item,
132                                                 File                 file,
133                                               DLNAInformation      dlna_info,
134                                               DiscovererVideoInfo  video_info,
135                                               DiscovererAudioInfo? audio_info,
136                                               string               mime,
137                                               uint64               size,
138                                               uint64               mtime) {
139         fill_audio_item (item as AudioItem, dlna_info, audio_info);
140         fill_media_item (item, file, dlna_info, mime, size, mtime);
141
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 ();
145
146         return item;
147     }
148
149     private static MediaItem fill_photo_item (PhotoItem           item,
150                                               File                file,
151                                               DLNAInformation     dlna_info,
152                                               DiscovererVideoInfo video_info,
153                                               string              mime,
154                                               uint64              size,
155                                               uint64              mtime) {
156         fill_media_item (item, file, dlna_info, mime, size, mtime);
157
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 ();
161
162         return item;
163     }
164
165     private static MediaItem fill_music_item (MusicItem            item,
166                                               File                 file,
167                                               DLNAInformation      dlna_info,
168                                               DiscovererAudioInfo? audio_info,
169                                               string               mime,
170                                               uint64               size,
171                                               uint64               mtime) {
172         fill_audio_item (item as AudioItem, dlna_info, audio_info);
173         fill_media_item (item, file, dlna_info, mime, size, mtime);
174
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);
181                     int image_type;
182                     structure.get_enum ("image-type",
183                             typeof (Gst.TagImageType),
184                             out image_type);
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",
190                                     item,
191                                     true);
192                             try {
193                                 var writer = new JPEGWriter ();
194                                 writer.write (buffer, thumb);
195                             } catch (Error error) {}
196                             break;
197                         default:
198                             break;
199                     }
200                 }
201             }
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);
205
206             uint tmp;
207             dlna_info.info.get_tags() .get_uint (TAG_TRACK_NUMBER, out tmp);
208             item.track_number = (int) tmp;
209         }
210
211         return item;
212     }
213
214     private static void fill_media_item (MediaItem       item,
215                                   File                   file,
216                                   DLNAInformation dlna_info,
217                                   string           mime,
218                                   uint64           size,
219                                   uint64           mtime) {
220         string title = null;
221
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 ();
225         }
226
227         item.title = title;
228
229         if (dlna_info.info.get_tags () != null) {
230             GLib.Date? date;
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;
235             }
236         }
237
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 ();
242         }
243
244         item.size = (int64) size;
245         item.modified = (int64) mtime;
246
247         if (dlna_info.name != null) {
248             item.dlna_profile = dlna_info.name;
249             item.mime_type = dlna_info.mime;
250         } else {
251             item.mime_type = mime;
252         }
253
254         item.add_uri (file.get_uri ());
255     }
256 }
257