ede7b5306cac8e7bc572cdde69a249f1af6023b5
[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 public class Rygel.MediaExport.Item : Rygel.MediaItem {
31     public Item.simple (MediaContainer parent,
32                         File           file,
33                         string         mime,
34                         uint64         size,
35                         uint64         mtime) {
36         string id = Checksum.compute_for_string (ChecksumType.MD5,
37                                                  file.get_uri ());
38         var title = file.get_basename ();
39         string upnp_class;
40
41         if (mime.has_prefix ("video/")) {
42             upnp_class = MediaItem.VIDEO_CLASS;
43         } else if (mime.has_prefix ("image/")) {
44             upnp_class = MediaItem.PHOTO_CLASS;
45         } else {
46             upnp_class = MediaItem.AUDIO_CLASS;
47         }
48
49         base (id, parent, title, upnp_class);
50         this.mime_type = mime;
51         this.add_uri (file.get_uri (), null);
52     }
53
54     public static Item? create_from_info (MediaContainer        parent,
55                                           File                  file,
56                                           GUPnP.DLNAInformation dlna_info,
57                                           string                mime,
58                                           uint64                size,
59                                           uint64                mtime) {
60         string id = Checksum.compute_for_string (ChecksumType.MD5,
61                                                  file.get_uri ());
62         unowned StreamAudioInformation audio_info = null;
63         unowned StreamVideoInformation video_info = null;
64
65         foreach (unowned StreamInformation stream_info in
66                  dlna_info.info.stream_list) {
67             if (audio_info == null &&
68                 stream_info.streamtype == Gst.StreamType.AUDIO) {
69                 audio_info = (StreamAudioInformation) stream_info;
70             } else if (video_info == null &&
71                        (stream_info.streamtype == Gst.StreamType.VIDEO ||
72                         stream_info.streamtype == Gst.StreamType.IMAGE)) {
73                 video_info = (StreamVideoInformation) stream_info;
74             }
75         }
76
77         if (video_info != null) {
78             if (audio_info == null &&
79                 video_info.streamtype == Gst.StreamType.IMAGE) {
80                 return new Item.photo (parent,
81                                        id,
82                                        file,
83                                        dlna_info,
84                                        video_info,
85                                        mime,
86                                        size,
87                                        mtime);
88             } else {
89                 return new Item.video (parent,
90                                        id,
91                                        file,
92                                        dlna_info,
93                                        video_info,
94                                        audio_info,
95                                        mime,
96                                        size,
97                                        mtime);
98             }
99         } else if (audio_info != null) {
100             return new Item.audio (parent,
101                                    id,
102                                    file,
103                                    dlna_info,
104                                    audio_info,
105                                    mime,
106                                    size,
107                                    mtime);
108         } else {
109             return null;
110         }
111     }
112
113     private Item.video (MediaContainer              parent,
114                         string                      id,
115                         File                        file,
116                         GUPnP.DLNAInformation       dlna_info,
117                         Gst.StreamVideoInformation  video_info,
118                         Gst.StreamAudioInformation? audio_info,
119                         string                      mime,
120                         uint64                      size,
121                         uint64                      mtime) {
122         this (parent,
123               id,
124               file,
125               dlna_info,
126               mime,
127               size,
128               mtime,
129               MediaItem.VIDEO_CLASS);
130
131         this.width = (int) video_info.width;
132         this.height = (int) video_info.height;
133         this.color_depth = (int) video_info.depth;
134
135         if (video_info.tags != null) {
136             uint tmp;
137
138             video_info.tags.get_uint (TAG_BITRATE, out tmp);
139             this.bitrate = (int) tmp / 8;
140         }
141
142         if (audio_info != null) {
143             this.n_audio_channels = (int) audio_info.channels;
144             this.sample_freq = (int) audio_info.sample_rate;
145         }
146     }
147
148     private Item.photo (MediaContainer             parent,
149                         string                     id,
150                         File                       file,
151                         GUPnP.DLNAInformation      dlna_info,
152                         Gst.StreamVideoInformation video_info,
153                         string                     mime,
154                         uint64                     size,
155                         uint64                     mtime) {
156         this (parent,
157               id,
158               file,
159               dlna_info,
160               mime,
161               size,
162               mtime,
163               MediaItem.PHOTO_CLASS);
164
165         this.width = (int) video_info.width;
166         this.height = (int) video_info.height;
167         this.color_depth = (int) video_info.depth;
168     }
169
170     private Item.audio (MediaContainer             parent,
171                         string                     id,
172                         File                       file,
173                         GUPnP.DLNAInformation      dlna_info,
174                         Gst.StreamAudioInformation audio_info,
175                         string                     mime,
176                         uint64                     size,
177                         uint64                     mtime) {
178         this (parent,
179               id,
180               file,
181               dlna_info,
182               mime,
183               size,
184               mtime,
185               MediaItem.MUSIC_CLASS);
186
187         this.n_audio_channels = (int) audio_info.channels;
188         this.sample_freq = (int) audio_info.sample_rate;
189     }
190
191     private Item (MediaContainer        parent,
192                   string                id,
193                   File                  file,
194                   GUPnP.DLNAInformation dlna_info,
195                   string                mime,
196                   uint64                size,
197                   uint64                mtime,
198                   string                upnp_class) {
199         string title = null;
200
201         if (dlna_info.info.tags == null ||
202             !dlna_info.info.tags.get_string (TAG_TITLE, out title)) {
203             title = file.get_basename ();
204         }
205
206         base (id, parent, title, upnp_class);
207
208         if (dlna_info.info.duration > -1) {
209             this.duration = -1;
210         } else {
211             this.duration = dlna_info.info.duration / Gst.SECOND;
212         }
213
214         if (dlna_info.info.tags != null) {
215             dlna_info.info.tags.get_string (TAG_ARTIST, out this.author);
216             dlna_info.info.tags.get_string (TAG_ALBUM, out this.album);
217
218             uint tmp;
219             dlna_info.info.tags.get_uint (TAG_TRACK_NUMBER, out tmp);
220             this.track_number = (int) tmp;
221
222             GLib.Date? date;
223             if (dlna_info.info.tags.get_date (TAG_DATE, out date)) {
224                 char[] datestr = new char[30];
225                 date.strftime (datestr, "%F");
226                 this.date = (string) datestr;
227             } else {
228                 TimeVal tv = { (long) mtime, 0 };
229                 this.date = tv.to_iso8601 ();
230             }
231         }
232
233         this.size = (long) size;
234         this.modified = (int64) mtime;
235
236         if (dlna_info.name != null) {
237             this.dlna_profile = dlna_info.name;
238             this.mime_type = dlna_info.mime;
239         } else {
240             this.mime_type = mime;
241         }
242
243         this.add_uri (file.get_uri (), null);
244     }
245 }
246