| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# |
| 3 |
# Copyright (c) 2016 Alexandru Băluț <[email protected]> |
| 4 |
# Copyright (c) 2016, Thibault Saunier |
| 5 |
# |
| 6 |
# This program is free software; you can redistribute it and/or |
| 7 |
# modify it under the terms of the GNU Lesser General Public |
| 8 |
# License as published by the Free Software Foundation; either |
| 9 |
# version 2.1 of the License, or (at your option) any later version. |
| 10 |
# |
| 11 |
# This program is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
# Lesser General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU Lesser General Public |
| 17 |
# License along with this program; if not, write to the |
| 18 |
# Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, |
| 19 |
# Boston, MA 02110-1301, USA. |
| 20 |
|
| 21 |
from urllib.parse import urlparse |
| 22 |
import gi |
| 23 |
|
| 24 |
gi.require_version("Gst", "1.0") |
| 25 |
gi.require_version("GES", "1.0") |
| 26 |
|
| 27 |
from gi.repository import Gst # noqa |
| 28 |
from gi.repository import GES # noqa |
| 29 |
from gi.repository import GLib # noqa |
| 30 |
from gi.repository import GObject # noqa |
| 31 |
import contextlib # noqa |
| 32 |
import os #noqa |
| 33 |
import unittest # noqa |
| 34 |
import tempfile # noqa |
| 35 |
|
| 36 |
try: |
| 37 |
gi.require_version("GstTranscoder", "1.0") |
| 38 |
from gi.repository import GstTranscoder |
| 39 |
except ValueError: |
| 40 |
GstTranscoder = None |
| 41 |
|
| 42 |
Gst.init(None) |
| 43 |
GES.init() |
| 44 |
|
| 45 |
|
| 46 |
def create_main_loop(): |
| 47 |
"""Creates a MainLoop with a timeout.""" |
| 48 |
mainloop = GLib.MainLoop() |
| 49 |
timed_out = False |
| 50 |
|
| 51 |
def timeout_cb(unused): |
| 52 |
nonlocal timed_out |
| 53 |
timed_out = True |
| 54 |
mainloop.quit() |
| 55 |
|
| 56 |
def run(timeout_seconds=5, until_empty=False): |
| 57 |
source = GLib.timeout_source_new_seconds(timeout_seconds) |
| 58 |
source.set_callback(timeout_cb) |
| 59 |
source.attach() |
| 60 |
if until_empty: |
| 61 |
GLib.idle_add(mainloop.quit) |
| 62 |
GLib.MainLoop.run(mainloop) |
| 63 |
source.destroy() |
| 64 |
if timed_out: |
| 65 |
raise Exception("Timed out after %s seconds" % timeout_seconds) |
| 66 |
|
| 67 |
mainloop.run = run |
| 68 |
return mainloop |
| 69 |
|
| 70 |
|
| 71 |
def create_project(with_group=False, saved=False): |
| 72 |
"""Creates a project with two clips in a group.""" |
| 73 |
timeline = GES.Timeline.new_audio_video() |
| 74 |
layer = timeline.append_layer() |
| 75 |
|
| 76 |
if with_group: |
| 77 |
clip1 = GES.TitleClip() |
| 78 |
clip1.set_start(0) |
| 79 |
clip1.set_duration(10*Gst.SECOND) |
| 80 |
layer.add_clip(clip1) |
| 81 |
clip2 = GES.TitleClip() |
| 82 |
clip2.set_start(100 * Gst.SECOND) |
| 83 |
clip2.set_duration(10*Gst.SECOND) |
| 84 |
layer.add_clip(clip2) |
| 85 |
group = GES.Container.group([clip1, clip2]) |
| 86 |
|
| 87 |
if saved: |
| 88 |
if isinstance(saved, str): |
| 89 |
suffix = "-%s.xges" % saved |
| 90 |
else: |
| 91 |
suffix = ".xges" |
| 92 |
uri = "file://%s" % tempfile.NamedTemporaryFile(suffix=suffix).name |
| 93 |
timeline.get_asset().save(timeline, uri, None, overwrite=True) |
| 94 |
|
| 95 |
return timeline |
| 96 |
|
| 97 |
|
| 98 |
@contextlib.contextmanager |
| 99 |
def created_project_file(xges): |
| 100 |
_, xges_path = tempfile.mkstemp(suffix=".xges") |
| 101 |
with open(xges_path, "w") as f: |
| 102 |
f.write(xges) |
| 103 |
|
| 104 |
yield Gst.filename_to_uri(os.path.abspath(xges_path)) |
| 105 |
|
| 106 |
os.remove(xges_path) |
| 107 |
|
| 108 |
|
| 109 |
def can_generate_assets(): |
| 110 |
if GstTranscoder is None: |
| 111 |
return False, "GstTranscoder is not available" |
| 112 |
|
| 113 |
if not Gst.ElementFactory.make("testsrcbin"): |
| 114 |
return False, "testbinsrc is not available" |
| 115 |
|
| 116 |
return True, None |
| 117 |
|
| 118 |
|
| 119 |
@contextlib.contextmanager |
| 120 |
def created_video_asset(uri=None, num_bufs=30, framerate="30/1"): |
| 121 |
with tempfile.NamedTemporaryFile(suffix=".ogg") as f: |
| 122 |
if not uri: |
| 123 |
uri = Gst.filename_to_uri(f.name) |
| 124 |
name = f.name |
| 125 |
else: |
| 126 |
name = urlparse(uri).path |
| 127 |
pipe = Gst.parse_launch(f"videotestsrc num-buffers={num_bufs} ! video/x-raw,framerate={framerate} ! theoraenc ! oggmux ! filesink location={name}") |
| 128 |
pipe.set_state(Gst.State.PLAYING) |
| 129 |
assert pipe.get_bus().timed_pop_filtered(Gst.CLOCK_TIME_NONE, Gst.MessageType.EOS) |
| 130 |
pipe.set_state(Gst.State.NULL) |
| 131 |
|
| 132 |
yield uri |
| 133 |
|
| 134 |
|
| 135 |
def get_asset_uri(name): |
| 136 |
python_tests_dir = os.path.dirname(os.path.abspath(__file__)) |
| 137 |
assets_dir = os.path.join(python_tests_dir, "..", "assets") |
| 138 |
return Gst.filename_to_uri(os.path.join(assets_dir, name)) |
| 139 |
|
| 140 |
|
| 141 |
class GESTest(unittest.TestCase): |
| 142 |
|
| 143 |
def _log(self, func, format, *args): |
| 144 |
string = format |
| 145 |
if args: |
| 146 |
string = string % args[0] |
| 147 |
func(string) |
| 148 |
|
| 149 |
def log(self, format, *args): |
| 150 |
self._log(Gst.log, format, *args) |
| 151 |
|
| 152 |
def debug(self, format, *args): |
| 153 |
self._log(Gst.debug, format, *args) |
| 154 |
|
| 155 |
def info(self, format, *args): |
| 156 |
self._log(Gst.info, format, *args) |
| 157 |
|
| 158 |
def fixme(self, format, *args): |
| 159 |
self._log(Gst.fixme, format, *args) |
| 160 |
|
| 161 |
def warning(self, format, *args): |
| 162 |
self._log(Gst.warning, format, *args) |
| 163 |
|
| 164 |
def error(self, format, *args): |
| 165 |
self._log(Gst.error, format, *args) |
| 166 |
|
| 167 |
def check_clip_values(self, clip, start, in_point, duration): |
| 168 |
for elem in [clip] + clip.get_children(False): |
| 169 |
self.check_element_values(elem, start, in_point, duration) |
| 170 |
|
| 171 |
def check_element_values(self, element, start, in_point, duration): |
| 172 |
self.assertEqual(element.props.start, start, element) |
| 173 |
self.assertEqual(element.props.in_point, in_point, element) |
| 174 |
self.assertEqual(element.props.duration, duration, element) |
| 175 |
|
| 176 |
def assert_effects(self, clip, *effects): |
| 177 |
# Make sure there are no other effects. |
| 178 |
self.assertEqual(set(clip.get_top_effects()), set(effects)) |
| 179 |
|
| 180 |
# Make sure their order is correct. |
| 181 |
indexes = [clip.get_top_effect_index(effect) |
| 182 |
for effect in effects] |
| 183 |
self.assertEqual(indexes, list(range(len(effects)))) |
| 184 |
|
| 185 |
def assertGESError(self, error, code, message=""): |
| 186 |
if error is None: |
| 187 |
raise AssertionError( |
| 188 |
"{}{}Received no error".format(message, message and ": ")) |
| 189 |
if error.domain != "GES_ERROR": |
| 190 |
raise AssertionError( |
| 191 |
"{}{}Received error ({}) in domain {} rather than " |
| 192 |
"GES_ERROR".format( |
| 193 |
message, message and ": ", error.message, error.domain)) |
| 194 |
err_code = GES.Error(error.code) |
| 195 |
if err_code != code: |
| 196 |
raise AssertionError( |
| 197 |
"{}{}Received {} error ({}) rather than {}".format( |
| 198 |
message, message and ": ", err_code.value_name, |
| 199 |
error.message, code.value_name)) |
| 200 |
|
| 201 |
class GESSimpleTimelineTest(GESTest): |
| 202 |
|
| 203 |
def __init__(self, *args): |
| 204 |
self.track_types = [GES.TrackType.AUDIO, GES.TrackType.VIDEO] |
| 205 |
super(GESSimpleTimelineTest, self).__init__(*args) |
| 206 |
|
| 207 |
def timeline_as_str(self): |
| 208 |
res = "====== %s =======\n" % self.timeline |
| 209 |
for layer in self.timeline.get_layers(): |
| 210 |
res += "Layer %04d: " % layer.get_priority() |
| 211 |
for clip in layer.get_clips(): |
| 212 |
res += "{ %s }" % clip |
| 213 |
res += '\n------------------------\n' |
| 214 |
|
| 215 |
for group in self.timeline.get_groups(): |
| 216 |
res += "GROUP %s :" % group |
| 217 |
for clip in group.get_children(False): |
| 218 |
res += " { %s }" % clip.props.name |
| 219 |
res += '\n' |
| 220 |
res += "================================\n" |
| 221 |
return res |
| 222 |
|
| 223 |
def print_timeline(self): |
| 224 |
print(self.timeline_as_str()) |
| 225 |
|
| 226 |
def setUp(self): |
| 227 |
self.timeline = GES.Timeline.new() |
| 228 |
for track_type in self.track_types: |
| 229 |
self.assertIn( |
| 230 |
track_type, [GES.TrackType.AUDIO, GES.TrackType.VIDEO]) |
| 231 |
if track_type == GES.TrackType.AUDIO: |
| 232 |
self.assertTrue(self.timeline.add_track(GES.AudioTrack.new())) |
| 233 |
else: |
| 234 |
self.assertTrue(self.timeline.add_track(GES.VideoTrack.new())) |
| 235 |
|
| 236 |
self.assertEqual(len(self.timeline.get_tracks()), |
| 237 |
len(self.track_types)) |
| 238 |
self.layer = self.timeline.append_layer() |
| 239 |
|
| 240 |
def add_clip(self, start, in_point, duration, asset_type=GES.TestClip): |
| 241 |
clip = GES.Asset.request(asset_type, None).extract() |
| 242 |
clip.props.start = start |
| 243 |
clip.props.in_point = in_point |
| 244 |
clip.props.duration = duration |
| 245 |
self.assertTrue(self.layer.add_clip(clip)) |
| 246 |
|
| 247 |
return clip |
| 248 |
|
| 249 |
def append_clip(self, layer=0, asset_type=GES.TestClip, asset_id=None): |
| 250 |
while len(self.timeline.get_layers()) < layer + 1: |
| 251 |
self.timeline.append_layer() |
| 252 |
layer = self.timeline.get_layers()[layer] |
| 253 |
if asset_type == GES.UriClip: |
| 254 |
asset = GES.UriClipAsset.request_sync(asset_id) |
| 255 |
else: |
| 256 |
asset = GES.Asset.request(asset_type, asset_id) |
| 257 |
clip = asset.extract() |
| 258 |
clip.props.start = layer.get_duration() |
| 259 |
clip.props.duration = 10 |
| 260 |
self.assertTrue(layer.add_clip(clip)) |
| 261 |
|
| 262 |
return clip |
| 263 |
|
| 264 |
def assertElementAreEqual(self, ref, element): |
| 265 |
self.assertTrue(isinstance(element, type(ref)), "%s and %s do not have the same type!" % (ref, element)) |
| 266 |
|
| 267 |
props = [p for p in ref.list_properties() if p.name not in ['name'] |
| 268 |
and not GObject.type_is_a(p.value_type, GObject.Object)] |
| 269 |
for p in props: |
| 270 |
pname = p.name |
| 271 |
refval = GObject.Value() |
| 272 |
refval.init(p.value_type) |
| 273 |
refval.set_value(ref.get_property(pname)) |
| 274 |
|
| 275 |
value = GObject.Value() |
| 276 |
value.init(p.value_type) |
| 277 |
value.set_value(element.get_property(pname)) |
| 278 |
|
| 279 |
self.assertTrue(Gst.value_compare(refval, value) == Gst.VALUE_EQUAL, |
| 280 |
"%s are not equal: %s != %s\n %s != %s" % (pname, value, refval, element, ref)) |
| 281 |
|
| 282 |
if isinstance(ref, GES.TrackElement): |
| 283 |
self.assertElementAreEqual(ref.get_nleobject(), element.get_nleobject()) |
| 284 |
return |
| 285 |
|
| 286 |
if not isinstance(ref, GES.Clip): |
| 287 |
return |
| 288 |
|
| 289 |
ttypes = [track.type for track in self.timeline.get_tracks()] |
| 290 |
for ttype in ttypes: |
| 291 |
if ttypes.count(ttype) > 1: |
| 292 |
self.warning("Can't deeply check %s and %s " |
| 293 |
"(only one track per type supported %s %s found)" % (ref, |
| 294 |
element, ttypes.count(ttype), ttype)) |
| 295 |
return |
| 296 |
|
| 297 |
children = element.get_children(False) |
| 298 |
for ref_child in ref.get_children(False): |
| 299 |
ref_track = ref_child.get_track() |
| 300 |
if not ref_track: |
| 301 |
self.warning("Can't check %s as not in a track" % (ref_child)) |
| 302 |
continue |
| 303 |
|
| 304 |
child = None |
| 305 |
for tmpchild in children: |
| 306 |
if not isinstance(tmpchild, type(ref_child)): |
| 307 |
continue |
| 308 |
|
| 309 |
if ref_track.type != tmpchild.get_track().type: |
| 310 |
continue |
| 311 |
|
| 312 |
if not isinstance(ref_child, GES.Effect): |
| 313 |
child = tmpchild |
| 314 |
break |
| 315 |
elif ref_child.props.bin_description == tmpchild.props.bin_description: |
| 316 |
child = tmpchild |
| 317 |
break |
| 318 |
|
| 319 |
self.assertIsNotNone(child, "Could not find equivalent child %s in %s(%s)" % (ref_child, |
| 320 |
element, children)) |
| 321 |
|
| 322 |
self.assertElementAreEqual(ref_child, child) |
| 323 |
|
| 324 |
def check_reload_timeline(self): |
| 325 |
tmpf = tempfile.NamedTemporaryFile(suffix='.xges') |
| 326 |
uri = Gst.filename_to_uri(tmpf.name) |
| 327 |
self.assertTrue(self.timeline.save_to_uri(uri, None, True)) |
| 328 |
project = GES.Project.new(uri) |
| 329 |
mainloop = create_main_loop() |
| 330 |
def loaded_cb(unused_project, unused_timeline): |
| 331 |
mainloop.quit() |
| 332 |
|
| 333 |
project.connect("loaded", loaded_cb) |
| 334 |
reloaded_timeline = project.extract() |
| 335 |
|
| 336 |
mainloop.run() |
| 337 |
self.assertIsNotNone(reloaded_timeline) |
| 338 |
|
| 339 |
layers = self.timeline.get_layers() |
| 340 |
reloaded_layers = reloaded_timeline.get_layers() |
| 341 |
self.assertEqual(len(layers), len(reloaded_layers)) |
| 342 |
for layer, reloaded_layer in zip(layers, reloaded_layers): |
| 343 |
clips = layer.get_clips() |
| 344 |
reloaded_clips = reloaded_layer.get_clips() |
| 345 |
self.assertEqual(len(clips), len(reloaded_clips)) |
| 346 |
for clip, reloaded_clip in zip(clips, reloaded_clips): |
| 347 |
self.assertElementAreEqual(clip, reloaded_clip) |
| 348 |
|
| 349 |
return reloaded_timeline |
| 350 |
|
| 351 |
def assertTimelineTopology(self, topology, groups=[]): |
| 352 |
res = [] |
| 353 |
for layer in self.timeline.get_layers(): |
| 354 |
layer_timings = [] |
| 355 |
for clip in layer.get_clips(): |
| 356 |
layer_timings.append( |
| 357 |
(type(clip), clip.props.start, clip.props.duration)) |
| 358 |
for child in clip.get_children(True): |
| 359 |
self.assertEqual(child.props.start, clip.props.start) |
| 360 |
self.assertEqual(child.props.duration, clip.props.duration) |
| 361 |
|
| 362 |
res.append(layer_timings) |
| 363 |
if topology != res: |
| 364 |
Gst.error(self.timeline_as_str()) |
| 365 |
self.assertEqual(topology, res) |
| 366 |
|
| 367 |
timeline_groups = self.timeline.get_groups() |
| 368 |
if groups and timeline_groups: |
| 369 |
for i, group in enumerate(groups): |
| 370 |
self.assertEqual(set(group), set(timeline_groups[i].get_children(False))) |
| 371 |
self.assertEqual(len(timeline_groups), i + 1) |
| 372 |
|
| 373 |
return res |
| 374 |
|
| 375 |
|
| 376 |
class GESTimelineConfigTest(GESTest): |
| 377 |
""" |
| 378 |
Tests where all the configuration changes, snapping positions and |
| 379 |
auto-transitions are accounted for. |
| 380 |
""" |
| 381 |
|
| 382 |
def setUp(self): |
| 383 |
timeline = GES.Timeline.new() |
| 384 |
self.timeline = timeline |
| 385 |
timeline.set_auto_transition(True) |
| 386 |
|
| 387 |
self.snap_occured = False |
| 388 |
self.snap = None |
| 389 |
|
| 390 |
def snap_started(tl, el1, el2, pos): |
| 391 |
if self.snap_occured: |
| 392 |
raise AssertionError( |
| 393 |
"Previous snap {} not accounted for".format(self.snap)) |
| 394 |
self.snap_occured = True |
| 395 |
if self.snap is not None: |
| 396 |
raise AssertionError( |
| 397 |
"Previous snap {} not ended".format(self.snap)) |
| 398 |
self.snap = (el1.get_parent(), el2.get_parent(), pos) |
| 399 |
|
| 400 |
def snap_ended(tl, el1, el2, pos): |
| 401 |
self.assertEqual( |
| 402 |
self.snap, (el1.get_parent(), el2.get_parent(), pos)) |
| 403 |
self.snap = None |
| 404 |
|
| 405 |
timeline.connect("snapping-started", snap_started) |
| 406 |
timeline.connect("snapping-ended", snap_ended) |
| 407 |
|
| 408 |
self.lost_clips = [] |
| 409 |
|
| 410 |
def unrecord_lost_clip(layer, clip): |
| 411 |
if clip in self.lost_clips: |
| 412 |
self.lost_clips.remove(clip) |
| 413 |
|
| 414 |
def record_lost_clip(layer, clip): |
| 415 |
self.lost_clips.append(clip) |
| 416 |
|
| 417 |
def layer_added(tl, layer): |
| 418 |
layer.connect("clip-added", unrecord_lost_clip) |
| 419 |
layer.connect("clip-removed", record_lost_clip) |
| 420 |
|
| 421 |
timeline.connect("layer-added", layer_added) |
| 422 |
|
| 423 |
self.clips = [] |
| 424 |
self.auto_transitions = {} |
| 425 |
self.config = {} |
| 426 |
|
| 427 |
@staticmethod |
| 428 |
def new_config(start, duration, inpoint, maxduration, layer): |
| 429 |
return {"start": start, "duration": duration, "in-point": inpoint, |
| 430 |
"max-duration": maxduration, "layer": layer} |
| 431 |
|
| 432 |
def add_clip(self, name, layer, tracks, start, duration, inpoint=0, |
| 433 |
maxduration=Gst.CLOCK_TIME_NONE, clip_type=GES.TestClip, |
| 434 |
asset_id=None, effects=None): |
| 435 |
""" |
| 436 |
Create a clip with the given @name and properties and add it to the |
| 437 |
layer of priority @layer to the tracks in @tracks. Also registers |
| 438 |
its expected configuration. |
| 439 |
""" |
| 440 |
if effects is None: |
| 441 |
effects = [] |
| 442 |
|
| 443 |
lay = self.timeline.get_layer(layer) |
| 444 |
while lay is None: |
| 445 |
self.timeline.append_layer() |
| 446 |
lay = self.timeline.get_layer(layer) |
| 447 |
|
| 448 |
asset = GES.Asset.request(clip_type, asset_id) |
| 449 |
clip = asset.extract() |
| 450 |
self.assertTrue(clip.set_name(name)) |
| 451 |
# FIXME: would be better to use select-tracks-for-object |
| 452 |
# hack around the fact that we cannot use select-tracks-for-object |
| 453 |
# in python by setting start to large number to ensure no conflict |
| 454 |
# when adding a clip |
| 455 |
self.assertTrue(clip.set_start(10000)) |
| 456 |
self.assertTrue(clip.set_duration(duration)) |
| 457 |
self.assertTrue(clip.set_inpoint(inpoint)) |
| 458 |
|
| 459 |
for effect in effects: |
| 460 |
self.assertTrue(clip.add(effect)) |
| 461 |
|
| 462 |
if lay.add_clip(clip) != True: |
| 463 |
raise AssertionError( |
| 464 |
"Failed to add clip {} to layer {}".format(name, layer)) |
| 465 |
|
| 466 |
# then remove the children not in the selected tracks, which may |
| 467 |
# now allow some clips to fully/triple overlap because they do |
| 468 |
# not share a track |
| 469 |
for child in clip.get_children(False): |
| 470 |
if child.get_track() not in tracks: |
| 471 |
clip.remove(child) |
| 472 |
|
| 473 |
# then move to the desired start |
| 474 |
prev_snap = self.timeline.get_snapping_distance() |
| 475 |
self.timeline.set_snapping_distance(0) |
| 476 |
self.assertTrue(clip.set_start(start)) |
| 477 |
self.timeline.set_snapping_distance(prev_snap) |
| 478 |
|
| 479 |
self.assertTrue(clip.set_max_duration(maxduration)) |
| 480 |
|
| 481 |
self.config[clip] = self.new_config( |
| 482 |
start, duration, inpoint, maxduration, layer) |
| 483 |
self.clips.append(clip) |
| 484 |
|
| 485 |
return clip |
| 486 |
|
| 487 |
def add_group(self, name, to_group): |
| 488 |
""" |
| 489 |
Create a group with the given @name and the elements in @to_group. |
| 490 |
Also registers its expected configuration. |
| 491 |
""" |
| 492 |
group = GES.Group.new() |
| 493 |
self.assertTrue(group.set_name(name)) |
| 494 |
start = None |
| 495 |
end = None |
| 496 |
layer = None |
| 497 |
for element in to_group: |
| 498 |
if start is None: |
| 499 |
start = element.start |
| 500 |
end = element.start + element.duration |
| 501 |
layer = element.get_layer_priority() |
| 502 |
else: |
| 503 |
start = min(start, element.start) |
| 504 |
end = max(end, element.start + element.duration) |
| 505 |
layer = min(layer, element.get_layer_priority()) |
| 506 |
self.assertTrue(group.add(element)) |
| 507 |
|
| 508 |
self.config[group] = self.new_config( |
| 509 |
start, end - start, 0, Gst.CLOCK_TIME_NONE, layer) |
| 510 |
return group |
| 511 |
|
| 512 |
def register_auto_transition(self, clip1, clip2, track): |
| 513 |
""" |
| 514 |
Register that we expect an auto-transition to exist between |
| 515 |
@clip1 and @clip2 in @track. |
| 516 |
""" |
| 517 |
transition = self._find_transition(clip1, clip2, track) |
| 518 |
if transition is None: |
| 519 |
raise AssertionError( |
| 520 |
"{} and {} have no auto-transition in track {}".format( |
| 521 |
clip1, clip2, track)) |
| 522 |
if transition in self.auto_transitions.values(): |
| 523 |
raise AssertionError( |
| 524 |
"Auto-transition between {} and {} in track {} already " |
| 525 |
"registered".format(clip1, clip2, track)) |
| 526 |
key = (clip1, clip2, track) |
| 527 |
if key in self.auto_transitions: |
| 528 |
raise AssertionError( |
| 529 |
"Auto-transition already registered for {}".format(key)) |
| 530 |
|
| 531 |
self.auto_transitions[key] = transition |
| 532 |
|
| 533 |
def add_video_track(self): |
| 534 |
track = GES.VideoTrack.new() |
| 535 |
self.assertTrue(self.timeline.add_track(track)) |
| 536 |
return track |
| 537 |
|
| 538 |
def add_audio_track(self): |
| 539 |
track = GES.AudioTrack.new() |
| 540 |
self.assertTrue(self.timeline.add_track(track)) |
| 541 |
return track |
| 542 |
|
| 543 |
def assertElementConfig(self, element, config): |
| 544 |
for prop in config: |
| 545 |
if prop == "layer": |
| 546 |
val = element.get_layer_priority() |
| 547 |
else: |
| 548 |
val = element.get_property(prop) |
| 549 |
|
| 550 |
if val != config[prop]: |
| 551 |
raise AssertionError("{} property {}: {} != {}".format( |
| 552 |
element, prop, val, config[prop])) |
| 553 |
|
| 554 |
@staticmethod |
| 555 |
def _source_in_track(clip, track): |
| 556 |
if clip.find_track_element(track, GES.Source): |
| 557 |
return True |
| 558 |
return False |
| 559 |
|
| 560 |
def _find_transition(self, clip1, clip2, track): |
| 561 |
"""find transition from earlier clip1 to later clip2""" |
| 562 |
if not self._source_in_track(clip1, track) or \ |
| 563 |
not self._source_in_track(clip2, track): |
| 564 |
return None |
| 565 |
|
| 566 |
layer_prio = clip1.get_layer_priority() |
| 567 |
if layer_prio != clip2.get_layer_priority(): |
| 568 |
return None |
| 569 |
|
| 570 |
if clip1.start >= clip2.start: |
| 571 |
return None |
| 572 |
|
| 573 |
start = clip2.start |
| 574 |
end = clip1.start + clip1.duration |
| 575 |
if start >= end: |
| 576 |
return None |
| 577 |
duration = end - start |
| 578 |
|
| 579 |
layer = self.timeline.get_layer(layer_prio) |
| 580 |
self.assertIsNotNone(layer) |
| 581 |
|
| 582 |
for clip in layer.get_clips(): |
| 583 |
children = clip.get_children(False) |
| 584 |
if len(children) == 1: |
| 585 |
child = children[0] |
| 586 |
else: |
| 587 |
continue |
| 588 |
if isinstance(clip, GES.TransitionClip) and clip.start == start \ |
| 589 |
and clip.duration == duration and child.get_track() == track: |
| 590 |
return clip |
| 591 |
|
| 592 |
raise AssertionError( |
| 593 |
"No auto-transition between {} and {} in track {}".format( |
| 594 |
clip1, clip2, track)) |
| 595 |
|
| 596 |
def _transition_between(self, new, existing, clip1, clip2, track): |
| 597 |
if clip1.start < clip2.start: |
| 598 |
entry = (clip1, clip2, track) |
| 599 |
else: |
| 600 |
entry = (clip2, clip1, track) |
| 601 |
trans = self._find_transition(*entry) |
| 602 |
|
| 603 |
if trans is None: |
| 604 |
return |
| 605 |
|
| 606 |
if entry in new: |
| 607 |
new.remove(entry) |
| 608 |
self.auto_transitions[entry] = trans |
| 609 |
elif entry in existing: |
| 610 |
existing.remove(entry) |
| 611 |
expect = self.auto_transitions[entry] |
| 612 |
if trans != expect: |
| 613 |
raise AssertionError( |
| 614 |
"Auto-transition between {} and {} in track {} changed " |
| 615 |
"from {} to {}".format( |
| 616 |
clip1, clip2, track, expect, trans)) |
| 617 |
else: |
| 618 |
raise AssertionError( |
| 619 |
"Unexpected transition found between {} and {} in track {}" |
| 620 |
"".format(clip1, clip2, track)) |
| 621 |
|
| 622 |
def assertTimelineConfig( |
| 623 |
self, new_props=None, snap_position=None, snap_froms=None, |
| 624 |
snap_tos=None, new_transitions=None, lost_transitions=None): |
| 625 |
""" |
| 626 |
Check that the timeline configuration has only changed by the |
| 627 |
differences present in @new_props. |
| 628 |
Check that a snap occurred at @snap_position between one of the |
| 629 |
clips in @snap_froms and one of the clips in @snap_tos. |
| 630 |
Check that all new transitions in the timeline are present in |
| 631 |
@new_transitions. |
| 632 |
Checl that all the transitions that were lost are in |
| 633 |
@lost_transitions. |
| 634 |
""" |
| 635 |
if new_props is None: |
| 636 |
new_props = {} |
| 637 |
if snap_froms is None: |
| 638 |
snap_froms = [] |
| 639 |
if snap_tos is None: |
| 640 |
snap_tos = [] |
| 641 |
if new_transitions is None: |
| 642 |
new_transitions = [] |
| 643 |
if lost_transitions is None: |
| 644 |
lost_transitions = [] |
| 645 |
|
| 646 |
for element, config in new_props.items(): |
| 647 |
if element not in self.config: |
| 648 |
self.config[element] = {} |
| 649 |
|
| 650 |
for prop in config: |
| 651 |
self.config[element][prop] = new_props[element][prop] |
| 652 |
|
| 653 |
for element, config in self.config.items(): |
| 654 |
self.assertElementConfig(element, config) |
| 655 |
|
| 656 |
# check that snapping occurred |
| 657 |
snaps = [] |
| 658 |
for snap_from in snap_froms: |
| 659 |
for snap_to in snap_tos: |
| 660 |
snaps.append((snap_from, snap_to, snap_position)) |
| 661 |
|
| 662 |
if self.snap is None: |
| 663 |
if snaps: |
| 664 |
raise AssertionError( |
| 665 |
"No snap occurred, but expected a snap in {}".format(snaps)) |
| 666 |
elif not snaps: |
| 667 |
if self.snap_occured: |
| 668 |
raise AssertionError( |
| 669 |
"Snap {} occurred, but expected no snap".format(self.snap)) |
| 670 |
elif self.snap not in snaps: |
| 671 |
raise AssertionError( |
| 672 |
"Snap {} occurred, but expected a snap in {}".format( |
| 673 |
self.snap, snaps)) |
| 674 |
self.snap_occured = False |
| 675 |
|
| 676 |
# check that lost transitions are not part of the layer |
| 677 |
for clip1, clip2, track in lost_transitions: |
| 678 |
key = (clip1, clip2, track) |
| 679 |
if key not in self.auto_transitions: |
| 680 |
raise AssertionError( |
| 681 |
"No such auto-transition between {} and {} in track {} " |
| 682 |
"is registered".format(clip1, clip2, track)) |
| 683 |
# make sure original transition was removed from the layer |
| 684 |
trans = self.auto_transitions[key] |
| 685 |
if trans not in self.lost_clips: |
| 686 |
raise AssertionError( |
| 687 |
"The auto-transition {} between {} and {} track {} was " |
| 688 |
"not removed from the layers, but expect it to be lost" |
| 689 |
"".format(trans, clip1, clip2, track)) |
| 690 |
self.lost_clips.remove(trans) |
| 691 |
# make sure a new one wasn't created |
| 692 |
trans = self._find_transition(clip1, clip2, track) |
| 693 |
if trans is not None: |
| 694 |
raise AssertionError( |
| 695 |
"Found auto-transition between {} and {} in track {} " |
| 696 |
"is present, but expected it to be lost".format( |
| 697 |
clip1, clip2, track)) |
| 698 |
# since it was lost, remove it |
| 699 |
del self.auto_transitions[key] |
| 700 |
|
| 701 |
# check that all lost clips are accounted for |
| 702 |
if self.lost_clips: |
| 703 |
raise AssertionError( |
| 704 |
"Clips were lost that are not accounted for: {}".format( |
| 705 |
self.lost_clips)) |
| 706 |
|
| 707 |
# check that all other transitions are either existing ones or |
| 708 |
# new ones |
| 709 |
new = set(new_transitions) |
| 710 |
existing = set(self.auto_transitions.keys()) |
| 711 |
for i, clip1 in enumerate(self.clips): |
| 712 |
for clip2 in self.clips[i+1:]: |
| 713 |
for track in self.timeline.get_tracks(): |
| 714 |
self._transition_between( |
| 715 |
new, existing, clip1, clip2, track) |
| 716 |
|
| 717 |
# make sure we are not missing any expected transitions |
| 718 |
if new: |
| 719 |
raise AssertionError( |
| 720 |
"Did not find new transitions for {}".format(new)) |
| 721 |
if existing: |
| 722 |
raise AssertionError( |
| 723 |
"Did not find existing transitions for {}".format(existing)) |
| 724 |
|
| 725 |
# make sure there aren't any clips we are unaware of |
| 726 |
transitions = self.auto_transitions.values() |
| 727 |
for layer in self.timeline.get_layers(): |
| 728 |
for clip in layer.get_clips(): |
| 729 |
if clip not in self.clips and clip not in transitions: |
| 730 |
raise AssertionError("Unknown clip {}".format(clip)) |
| 731 |
|
| 732 |
def assertEdit(self, element, layer, mode, edge, position, snap, |
| 733 |
snap_froms, snap_tos, new_props, new_transitions, |
| 734 |
lost_transitions): |
| 735 |
if not element.edit_full(layer, mode, edge, position): |
| 736 |
raise AssertionError( |
| 737 |
"Edit of {} to layer {}, mode {}, edge {}, at position {} " |
| 738 |
"failed when a success was expected".format( |
| 739 |
element, layer, mode, edge, position)) |
| 740 |
self.assertTimelineConfig( |
| 741 |
new_props=new_props, snap_position=snap, snap_froms=snap_froms, |
| 742 |
snap_tos=snap_tos, new_transitions=new_transitions, |
| 743 |
lost_transitions=lost_transitions) |
| 744 |
|
| 745 |
def assertFailEdit(self, element, layer, mode, edge, position, err_code): |
| 746 |
res = None |
| 747 |
error = None |
| 748 |
try: |
| 749 |
res = element.edit_full(layer, mode, edge, position) |
| 750 |
except GLib.Error as exception: |
| 751 |
error = exception |
| 752 |
|
| 753 |
if err_code is None: |
| 754 |
if res is not False: |
| 755 |
raise AssertionError( |
| 756 |
"Edit of {} to layer {}, mode {}, edge {}, at " |
| 757 |
"position {} succeeded when a failure was expected" |
| 758 |
"".format( |
| 759 |
element, layer, mode, edge, position)) |
| 760 |
if error is not None: |
| 761 |
raise AssertionError( |
| 762 |
"Edit of {} to layer {}, mode {}, edge {}, at " |
| 763 |
"position {} did produced an error when none was " |
| 764 |
"expected".format( |
| 765 |
element, layer, mode, edge, position)) |
| 766 |
else: |
| 767 |
self.assertGESError( |
| 768 |
error, err_code, |
| 769 |
"Edit of {} to layer {}, mode {}, edge {}, at " |
| 770 |
"position {}".format(element, layer, mode, edge, position)) |
| 771 |
# should be no change or snapping if edit fails |
| 772 |
self.assertTimelineConfig() |