1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 import pygst
19 pygst.require('0.10')
20 import gst
21 from threading import Event
22
23 __maintainer__ = "Benjamin Kampmann <benjamin@fluendo.com>"
24
25 from elisa.core.utils.deferred_action import DeferredActionsManager
26
28 """
29 Class for getting the type and mime info of a file.
30 """
31
37
39 self._count = 0
40 self._src = gst.element_factory_make(source)
41 self._src.set_property("blocksize", 550000)
42
43 self._dbin = gst.element_factory_make("decodebin")
44
45 self._type = self._dbin.get_by_name("typefind")
46 self._fakesink = []
47
48
49 self._pipeline = gst.Pipeline('Typefinder')
50 self._pipeline.add(self._src)
51 self._pipeline.add(self._dbin)
52
53 self._src.link(self._dbin)
54
55
56 self._pipeline.get_bus().connect("message", self._event_cb)
57 self._pipeline.get_bus().add_signal_watch()
58
59
60 self._type.connect("have-type", self._got_type)
61 self._dbin.connect("new-decoded-pad", self._new_decoded_pad_cb)
62 self._dbin.connect("unknown-type", self._unknown_type_cb)
63
64
66 """
67 Return a defer which will look up the type of a file. This defer might
68 needs a lot of time, because we are currently only doing one lookup at
69 a time and the other ones are put into a queue.
70
71 The callback-result of the L{Deferred} then is a dictionary containing
72 this:
73
74 - file_type: mapped to a string, which is one of 'video', 'audio' or
75 'image' or '' (if none of these three)
76 - mime_type: mapped to a string, containing the mime_type
77
78 @param source: the name of the gstreamer-source element to use
79 for e.g. 'filesrc' or 'gnomevfssrc'
80 @type source: String
81
82 @param location: to which location should the source be set to?
83 @type location: String
84
85 @rtype: L{Deferred}
86 """
87
88
89
90
91 return self._actions_manager.enqueue_action(self._set_pipeline,
92 source, location)
93
95
96 self._pipeline.set_state(gst.STATE_NULL)
97 self._pipeline.get_state()
98 for sink in self._fakesink:
99 self._dbin.unlink(sink)
100 self._pipeline.remove(sink)
101 sink.set_state(gst.STATE_NULL)
102
103 if self._count >= 500:
104 self._src.unlink(self._dbin)
105 self._pipeline.remove(self._src, self._dbin)
106 self._set_up_pipeline(source)
107
108 self._count += 1
109 self._fakesink = []
110 self.is_video = False
111 self.is_audio = False
112 self._mime = None
113
114
115 if self._src.get_factory().get_name() != source:
116
117
118
119
120
121 self._src.unlink(self._dbin)
122 self._pipeline.remove(self._src)
123 self._src = gst.element_factory_make(source)
124 self._pipeline.add(self._src)
125 self._src.link(self._dbin)
126
127
128
129 self._src.set_property("location", location)
130
131
132
133
134 self._pipeline.set_state(gst.STATE_PLAYING)
135 self._pipeline.get_state()
136 self._lock.wait()
137
138 if self.is_video:
139 if self._mime.find('image') == -1:
140 return {'file_type' : 'video', 'mime_type' : self._mime}
141
142 return {'file_type' : 'image', 'mime_type' : self._mime}
143
144 elif self.is_audio:
145 return {'file_type' : 'audio', 'mime_type' : self._mime}
146
147 return {'file_type' : '', 'mime_type' : self._mime}
148
149
152
155
157
158 caps = pad.get_caps()
159
160 if "audio" in caps.to_string():
161 self.is_audio = True
162 elif "video" in caps.to_string():
163 self.is_video = True
164 else:
165 return
166
167
168 fakesink = gst.element_factory_make("fakesink")
169
170 self._fakesink.append(fakesink)
171
172 self._pipeline.add(fakesink)
173 self._dbin.link(fakesink)
174 sinkpad = fakesink.get_pad("sink")
175
176
177 sinkpad.connect("notify::caps", self._notify_caps_cb)
178 fakesink.set_state(gst.STATE_PLAYING)
179
180
181 - def _got_type(self, typefind, arg1, mime_type):
182 self._mime = str(mime_type)
183
185
186 t = msg.type
187 if t == gst.MESSAGE_EOS:
188
189 pass
190 elif t == gst.MESSAGE_ERROR:
191 e, d = msg.parse_error()
192
193 self._got_type(None, None, "")
194 return True
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247