2016-09-06 12:27:38 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# Copyright (c) 2016 Alexandru Băluț <alexandru.balut@gmail.com>
|
2016-08-14 01:09:53 +00:00
|
|
|
# Copyright (c) 2016, Thibault Saunier
|
2016-09-06 12:27:38 +00:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU Lesser General Public
|
|
|
|
# License as published by the Free Software Foundation; either
|
|
|
|
# version 2.1 of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
# Lesser General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser General Public
|
|
|
|
# License along with this program; if not, write to the
|
|
|
|
# Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
# Boston, MA 02110-1301, USA.
|
|
|
|
|
2016-08-14 01:09:53 +00:00
|
|
|
import gi
|
|
|
|
|
|
|
|
gi.require_version("Gst", "1.0")
|
|
|
|
gi.require_version("GES", "1.0")
|
|
|
|
|
|
|
|
from gi.repository import Gst # noqa
|
|
|
|
from gi.repository import GES # noqa
|
|
|
|
from gi.repository import GLib # noqa
|
2018-11-27 03:55:17 +00:00
|
|
|
import contextlib # noqa
|
|
|
|
import os #noqa
|
2016-08-14 01:09:53 +00:00
|
|
|
import unittest # noqa
|
|
|
|
import tempfile # noqa
|
|
|
|
|
|
|
|
Gst.init(None)
|
|
|
|
GES.init()
|
2016-09-06 12:27:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
def create_main_loop():
|
|
|
|
"""Creates a MainLoop with a timeout."""
|
|
|
|
mainloop = GLib.MainLoop()
|
|
|
|
timed_out = False
|
|
|
|
|
2018-10-08 22:45:29 +00:00
|
|
|
def timeout_cb(unused):
|
2016-09-06 12:27:38 +00:00
|
|
|
nonlocal timed_out
|
|
|
|
timed_out = True
|
|
|
|
mainloop.quit()
|
|
|
|
|
2018-10-08 22:45:29 +00:00
|
|
|
def run(timeout_seconds=5, until_empty=False):
|
2016-09-06 12:27:38 +00:00
|
|
|
source = GLib.timeout_source_new_seconds(timeout_seconds)
|
2018-10-08 22:45:29 +00:00
|
|
|
source.set_callback(timeout_cb)
|
2016-09-06 12:27:38 +00:00
|
|
|
source.attach()
|
2018-10-08 22:45:29 +00:00
|
|
|
if until_empty:
|
|
|
|
GLib.idle_add(mainloop.quit)
|
2016-09-06 12:27:38 +00:00
|
|
|
GLib.MainLoop.run(mainloop)
|
|
|
|
source.destroy()
|
|
|
|
if timed_out:
|
|
|
|
raise Exception("Timed out after %s seconds" % timeout_seconds)
|
|
|
|
|
|
|
|
mainloop.run = run
|
|
|
|
return mainloop
|
|
|
|
|
2018-03-18 14:03:00 +00:00
|
|
|
|
2016-09-06 12:27:38 +00:00
|
|
|
def create_project(with_group=False, saved=False):
|
|
|
|
"""Creates a project with two clips in a group."""
|
2019-02-06 22:49:14 +00:00
|
|
|
project = GES.Project.new(None)
|
2016-09-06 12:27:38 +00:00
|
|
|
timeline = project.extract()
|
|
|
|
layer = timeline.append_layer()
|
|
|
|
|
|
|
|
if with_group:
|
|
|
|
clip1 = GES.TitleClip()
|
|
|
|
clip1.set_start(0)
|
|
|
|
clip1.set_duration(10)
|
|
|
|
layer.add_clip(clip1)
|
|
|
|
clip2 = GES.TitleClip()
|
|
|
|
clip2.set_start(100)
|
|
|
|
clip2.set_duration(10)
|
|
|
|
layer.add_clip(clip2)
|
|
|
|
group = GES.Container.group([clip1, clip2])
|
|
|
|
|
|
|
|
if saved:
|
|
|
|
uri = "file://%s" % tempfile.NamedTemporaryFile(suffix=".xges").name
|
|
|
|
project.save(timeline, uri, None, overwrite=True)
|
|
|
|
|
|
|
|
return timeline
|
|
|
|
|
2018-03-18 14:03:00 +00:00
|
|
|
|
2018-11-27 03:55:17 +00:00
|
|
|
@contextlib.contextmanager
|
|
|
|
def created_project_file(xges):
|
|
|
|
_, xges_path = tempfile.mkstemp(suffix=".xges")
|
|
|
|
with open(xges_path, "w") as f:
|
|
|
|
f.write(xges)
|
|
|
|
|
|
|
|
yield Gst.filename_to_uri(os.path.abspath(xges_path))
|
|
|
|
|
|
|
|
os.remove(xges_path)
|
|
|
|
|
|
|
|
|
|
|
|
def get_asset_uri(name):
|
|
|
|
python_tests_dir = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
assets_dir = os.path.join(python_tests_dir, "..", "assets")
|
|
|
|
return Gst.filename_to_uri(os.path.join(assets_dir, name))
|
|
|
|
|
|
|
|
|
2016-08-14 01:09:53 +00:00
|
|
|
class GESTest(unittest.TestCase):
|
2018-10-08 22:45:29 +00:00
|
|
|
|
2016-08-14 01:09:53 +00:00
|
|
|
def _log(self, func, format, *args):
|
|
|
|
string = format
|
|
|
|
if args:
|
|
|
|
string = string % args[0]
|
|
|
|
func(string)
|
|
|
|
|
|
|
|
def log(self, format, *args):
|
|
|
|
self._log(Gst.log, format, *args)
|
|
|
|
|
|
|
|
def debug(self, format, *args):
|
|
|
|
self._log(Gst.debug, format, *args)
|
|
|
|
|
|
|
|
def info(self, format, *args):
|
|
|
|
self._log(Gst.info, format, *args)
|
|
|
|
|
|
|
|
def fixme(self, format, *args):
|
|
|
|
self._log(Gst.fixme, format, *args)
|
|
|
|
|
|
|
|
def warning(self, format, *args):
|
|
|
|
self._log(Gst.warning, format, *args)
|
|
|
|
|
|
|
|
def error(self, format, *args):
|
|
|
|
self._log(Gst.error, format, *args)
|
|
|
|
|
2018-10-08 22:45:29 +00:00
|
|
|
def check_clip_values(self, clip, start, in_point, duration):
|
|
|
|
for elem in [clip] + clip.get_children(False):
|
|
|
|
self.check_element_values(elem, start, in_point, duration)
|
|
|
|
|
|
|
|
def check_element_values(self, element, start, in_point, duration):
|
|
|
|
self.assertEqual(element.props.start, start, element)
|
|
|
|
self.assertEqual(element.props.in_point, in_point, element)
|
|
|
|
self.assertEqual(element.props.duration, duration, element)
|
|
|
|
|
|
|
|
def assert_effects(self, clip, *effects):
|
|
|
|
# Make sure there are no other effects.
|
|
|
|
self.assertEqual(set(clip.get_top_effects()), set(effects))
|
|
|
|
|
|
|
|
# Make sure their order is correct.
|
|
|
|
indexes = [clip.get_top_effect_index(effect)
|
|
|
|
for effect in effects]
|
|
|
|
self.assertEqual(indexes, list(range(len(effects))))
|
|
|
|
|
2016-08-14 01:09:53 +00:00
|
|
|
|
|
|
|
class GESSimpleTimelineTest(GESTest):
|
2018-10-08 22:45:29 +00:00
|
|
|
|
2018-03-18 14:03:00 +00:00
|
|
|
def __init__(self, *args):
|
|
|
|
self.track_types = [GES.TrackType.AUDIO, GES.TrackType.VIDEO]
|
|
|
|
super(GESSimpleTimelineTest, self).__init__(*args)
|
|
|
|
|
2016-08-14 01:09:53 +00:00
|
|
|
def setUp(self):
|
2018-03-18 14:03:00 +00:00
|
|
|
self.timeline = GES.Timeline.new()
|
|
|
|
for track_type in self.track_types:
|
|
|
|
self.assertIn(
|
|
|
|
track_type, [GES.TrackType.AUDIO, GES.TrackType.VIDEO])
|
|
|
|
if track_type == GES.TrackType.AUDIO:
|
2018-10-08 22:45:29 +00:00
|
|
|
self.assertTrue(self.timeline.add_track(GES.AudioTrack.new()))
|
2018-03-18 14:03:00 +00:00
|
|
|
else:
|
2018-10-08 22:45:29 +00:00
|
|
|
self.assertTrue(self.timeline.add_track(GES.VideoTrack.new()))
|
2018-03-18 14:03:00 +00:00
|
|
|
|
|
|
|
self.assertEqual(len(self.timeline.get_tracks()),
|
|
|
|
len(self.track_types))
|
2016-08-14 01:09:53 +00:00
|
|
|
self.layer = self.timeline.append_layer()
|
|
|
|
|
|
|
|
def add_clip(self, start, in_point, duration):
|
|
|
|
clip = GES.TestClip()
|
|
|
|
clip.props.start = start
|
|
|
|
clip.props.in_point = in_point
|
|
|
|
clip.props.duration = duration
|
2018-10-08 22:45:29 +00:00
|
|
|
self.assertTrue(self.layer.add_clip(clip))
|
2016-08-14 01:09:53 +00:00
|
|
|
|
|
|
|
return clip
|
|
|
|
|
2019-02-08 20:47:48 +00:00
|
|
|
def assertTimelineTopology(self, topology):
|
|
|
|
res = []
|
|
|
|
for layer in self.timeline.get_layers():
|
|
|
|
layer_timings = []
|
|
|
|
for clip in layer.get_clips():
|
|
|
|
layer_timings.append(
|
|
|
|
(type(clip), clip.props.start, clip.props.duration))
|
|
|
|
|
|
|
|
res.append(layer_timings)
|
|
|
|
|
|
|
|
self.assertEqual(topology, res)
|
|
|
|
return res
|