server,engine: Return null DataSource on error
[profile/ivi/rygel.git] / src / media-engines / gstreamer / rygel-gst-media-engine.vala
1 /*
2  * Copyright (C) 2012 Intel Corporation.
3  *
4  * Author: Jens Georg <jensg@openismus.com>
5  *
6  * This file is part of Rygel.
7  *
8  * Rygel is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * Rygel is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  */
22
23 using Gst;
24 using Gee;
25
26 // Remove for GStreamer 1.0
27 [CCode (cname = "PRESET_DIR")]
28 internal extern static const string PRESET_DIR;
29
30 [CCode (cname="gst_preset_set_app_dir")]
31 extern bool gst_preset_set_app_dir (string app_dir);
32
33 public class Rygel.GstMediaEngine : Rygel.MediaEngine {
34     private GLib.List<DLNAProfile> dlna_profiles = null;
35     private GLib.List<Transcoder> transcoders = null;
36
37     public GstMediaEngine () {
38         // Work-around vapi bug, fixed for GStreamer 1.0
39         unowned string[] args = null;
40
41         Gst.init (ref args);
42         gst_preset_set_app_dir (PRESET_DIR);
43
44         var discoverer = new GUPnP.DLNADiscoverer ((ClockTime) SECOND,
45                                                    true,
46                                                    false);
47         foreach (var profile in discoverer.list_profiles ()) {
48             var p = new DLNAProfile (profile.mime, profile.name);
49             this.dlna_profiles.prepend (p);
50         }
51
52         this.dlna_profiles.reverse ();
53
54         var transcoding = true;
55         var transcoder_list = new ArrayList<string> ();
56
57         var config = MetaConfig.get_default ();
58         try {
59             transcoding = config.get_transcoding ();
60             transcoder_list = config.get_string_list ("MediaEngine",
61                                                       "transcoders");
62         } catch (Error err) {}
63
64         if (transcoding) {
65             foreach (var transcoder in transcoder_list) {
66                 switch (transcoder) {
67                     case "lpcm":
68                         this.transcoders.prepend (new L16Transcoder ());
69                         break;
70                     case "mp3":
71                         this.transcoders.prepend (new MP3Transcoder ());
72                         break;
73                     case "mp2ts":
74                         this.transcoders.prepend (new MP2TSTranscoder(MP2TSProfile.SD));
75                         this.transcoders.prepend (new MP2TSTranscoder(MP2TSProfile.HD));
76                         break;
77                     case "wmv":
78                         this.transcoders.prepend (new WMVTranscoder ());
79                         break;
80                     case "aac":
81                         this.transcoders.prepend (new AACTranscoder ());
82                         break;
83                     case "avc":
84                         this.transcoders.prepend (new AVCTranscoder ());
85                         break;
86                     default:
87                         debug ("Unsupported transcoder \"%s\"", transcoder);
88                         break;
89                 }
90             }
91
92             this.transcoders.reverse ();
93         }
94     }
95
96     public override unowned GLib.List<DLNAProfile> get_dlna_profiles () {
97         return this.dlna_profiles;
98     }
99
100     public override unowned GLib.List<Transcoder>? get_transcoders () {
101         return this.transcoders;
102     }
103
104     public override DataSource? create_data_source (string uri) {
105         try {
106             return new GstDataSource (uri);
107         } catch (Error error) {
108             return null;
109         }
110     }
111
112     public DataSource create_data_source_from_element (Element element) {
113         return new GstDataSource.from_element (element);
114     }
115 }
116
117 public static Rygel.MediaEngine module_get_instance () {
118     return new Rygel.GstMediaEngine ();
119 }