Test whether video is transcoded to all resolutions
[mediagoblin.git] / mediagoblin / tests / media_tools.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2013 MediaGoblin contributors. See AUTHORS.
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Affero General Public License for more details.
13 #
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17 from contextlib import contextmanager
18 import tempfile
19
20 import gi
21 gi.require_version('Gst', '1.0')
22 from gi.repository import Gst
23 Gst.init(None)
24
25 @contextmanager
26 def create_av(make_video=False, make_audio=False):
27 'creates audio/video in `path`, throws AssertionError on any error'
28 media = tempfile.NamedTemporaryFile(suffix='.ogg')
29 pipeline = Gst.Pipeline()
30 mux = Gst.ElementFactory.make('oggmux', 'mux')
31 pipeline.add(mux)
32 if make_video:
33 video_src = Gst.ElementFactory.make('videotestsrc', 'video_src')
34 video_src.set_property('num-buffers', 20)
35 video_enc = Gst.ElementFactory.make('theoraenc', 'video_enc')
36 pipeline.add(video_src)
37 pipeline.add(video_enc)
38 assert video_src.link(video_enc)
39 assert video_enc.link(mux)
40 if make_audio:
41 audio_src = Gst.ElementFactory.make('audiotestsrc', 'audio_src')
42 audio_src.set_property('num-buffers', 20)
43 audio_enc = Gst.ElementFactory.make('vorbisenc', 'audio_enc')
44 pipeline.add(audio_src)
45 pipeline.add(audio_enc)
46 assert audio_src.link(audio_enc)
47 assert audio_enc.link(mux)
48 sink = Gst.ElementFactory.make('filesink', 'sink')
49 sink.set_property('location', media.name)
50 pipeline.add(sink)
51 mux.link(sink)
52 pipeline.set_state(Gst.State.PLAYING)
53 state = pipeline.get_state(Gst.SECOND)
54 assert state[0] == Gst.StateChangeReturn.SUCCESS
55 bus = pipeline.get_bus()
56 message = bus.timed_pop_filtered(
57 Gst.SECOND, # one second should be more than enough for 50-buf vid
58 Gst.MessageType.ERROR | Gst.MessageType.EOS)
59 assert message.type == Gst.MessageType.EOS
60 pipeline.set_state(Gst.State.NULL)
61 yield media.name