| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# |
| 3 |
# Copyright (c) 2015, Thibault Saunier |
| 4 |
# |
| 5 |
# This program is free software; you can redistribute it and/or |
| 6 |
# modify it under the terms of the GNU Lesser General Public |
| 7 |
# License as published by the Free Software Foundation; either |
| 8 |
# version 2.1 of the License, or (at your option) any later version. |
| 9 |
# |
| 10 |
# This program is distributed in the hope that it will be useful, |
| 11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
# Lesser General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU Lesser General Public |
| 16 |
# License along with this program; if not, write to the |
| 17 |
# Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, |
| 18 |
# Boston, MA 02110-1301, USA. |
| 19 |
|
| 20 |
from . import overrides_hack |
| 21 |
|
| 22 |
import os |
| 23 |
import tempfile |
| 24 |
|
| 25 |
import gi |
| 26 |
gi.require_version("Gst", "1.0") |
| 27 |
gi.require_version("GES", "1.0") |
| 28 |
|
| 29 |
from gi.repository import Gst # noqa |
| 30 |
Gst.init(None) # noqa |
| 31 |
from gi.repository import GES # noqa |
| 32 |
GES.init() |
| 33 |
|
| 34 |
from . import common # noqa |
| 35 |
|
| 36 |
import unittest # noqa |
| 37 |
|
| 38 |
|
| 39 |
class TestCopyPaste(unittest.TestCase): |
| 40 |
|
| 41 |
def setUp(self): |
| 42 |
self.timeline = GES.Timeline.new_audio_video() |
| 43 |
self.assertEqual(len(self.timeline.get_tracks()), 2) |
| 44 |
self.layer = self.timeline.append_layer() |
| 45 |
|
| 46 |
def testCopyClipRemoveAndPaste(self): |
| 47 |
clip1 = GES.TestClip.new() |
| 48 |
clip1.props.duration = 10 |
| 49 |
|
| 50 |
self.layer.add_clip(clip1) |
| 51 |
|
| 52 |
self.assertEqual(len(clip1.get_children(False)), 2) |
| 53 |
|
| 54 |
copy = clip1.copy(True) |
| 55 |
self.assertEqual(len(self.layer.get_clips()), 1) |
| 56 |
|
| 57 |
self.layer.remove_clip(clip1) |
| 58 |
|
| 59 |
copy.paste(10) |
| 60 |
self.assertEqual(len(self.layer.get_clips()), 1) |
| 61 |
|
| 62 |
def testCopyPasteTitleClip(self): |
| 63 |
clip1 = GES.TitleClip.new() |
| 64 |
clip1.props.duration = 10 |
| 65 |
|
| 66 |
self.layer.add_clip(clip1) |
| 67 |
self.assertEqual(len(clip1.get_children(False)), 1) |
| 68 |
|
| 69 |
copy = clip1.copy(True) |
| 70 |
self.assertEqual(len(self.layer.get_clips()), 1) |
| 71 |
|
| 72 |
copy.paste(10) |
| 73 |
self.assertEqual(len(self.layer.get_clips()), 2) |
| 74 |
|
| 75 |
|
| 76 |
class TestTransitionClip(unittest.TestCase): |
| 77 |
|
| 78 |
def test_serialize_invert(self): |
| 79 |
timeline = GES.Timeline.new() |
| 80 |
timeline.add_track(GES.VideoTrack.new()) |
| 81 |
layer = timeline.append_layer() |
| 82 |
|
| 83 |
clip1 = GES.TransitionClip.new_for_nick("crossfade") |
| 84 |
clip1.props.duration = Gst.SECOND |
| 85 |
self.assertTrue(layer.add_clip(clip1)) |
| 86 |
|
| 87 |
vtransition, = clip1.children |
| 88 |
vtransition.set_inverted(True) |
| 89 |
self.assertEqual(vtransition.props.invert, True) |
| 90 |
|
| 91 |
with tempfile.NamedTemporaryFile() as tmpxges: |
| 92 |
uri = Gst.filename_to_uri(tmpxges.name) |
| 93 |
timeline.save_to_uri(uri, None, True) |
| 94 |
|
| 95 |
timeline = GES.Timeline.new_from_uri(uri) |
| 96 |
project = timeline.get_asset() |
| 97 |
mainloop = common.create_main_loop() |
| 98 |
project.connect("loaded", lambda _, __: mainloop.quit()) |
| 99 |
mainloop.run() |
| 100 |
self.assertIsNotNone(timeline) |
| 101 |
layer, = timeline.get_layers() |
| 102 |
clip, = layer.get_clips() |
| 103 |
vtransition, = clip.children |
| 104 |
self.assertEqual(vtransition.props.invert, True) |
| 105 |
|
| 106 |
class TestTitleClip(unittest.TestCase): |
| 107 |
|
| 108 |
def testSetColor(self): |
| 109 |
timeline = GES.Timeline.new_audio_video() |
| 110 |
clip = GES.TitleClip.new() |
| 111 |
timeline.append_layer().add_clip(clip ) |
| 112 |
self.assertTrue(clip.set_child_property('color', 1)) |
| 113 |
self.assertTrue(clip.set_child_property('color', 4294967295)) |
| 114 |
|
| 115 |
def testGetPropertyNotInTrack(self): |
| 116 |
title_clip = GES.TitleClip.new() |
| 117 |
self.assertEqual(title_clip.props.text, "") |
| 118 |
self.assertEqual(title_clip.props.font_desc, "Serif 36") |
| 119 |
|
| 120 |
def test_split_effect(self): |
| 121 |
timeline = GES.Timeline.new() |
| 122 |
timeline.add_track(GES.VideoTrack.new()) |
| 123 |
layer = timeline.append_layer() |
| 124 |
|
| 125 |
clip1 = GES.TitleClip.new() |
| 126 |
clip1.props.duration = Gst.SECOND |
| 127 |
self.assertTrue(layer.add_clip(clip1)) |
| 128 |
|
| 129 |
effect = GES.Effect.new("agingtv") |
| 130 |
self.assertTrue(clip1.add(effect)) |
| 131 |
|
| 132 |
children1 = clip1.get_children(True) |
| 133 |
self.assertNotEqual(children1[0].props.priority, |
| 134 |
children1[1].props.priority) |
| 135 |
|
| 136 |
clip2 = clip1.split(Gst.SECOND / 2) |
| 137 |
|
| 138 |
children1 = clip1.get_children(True) |
| 139 |
self.assertNotEqual(children1[0].props.priority, |
| 140 |
children1[1].props.priority) |
| 141 |
|
| 142 |
children2 = clip2.get_children(True) |
| 143 |
self.assertNotEqual(children2[0].props.priority, |
| 144 |
children2[1].props.priority) |
| 145 |
|
| 146 |
|
| 147 |
class TestUriClip(common.GESSimpleTimelineTest): |
| 148 |
def test_max_duration_on_extract(self): |
| 149 |
asset = GES.UriClipAsset.request_sync(common.get_asset_uri("audio_video.ogg")) |
| 150 |
clip = asset.extract() |
| 151 |
|
| 152 |
self.assertEqual(clip.props.max_duration, Gst.SECOND) |
| 153 |
|
| 154 |
|
| 155 |
class TestTrackElements(common.GESSimpleTimelineTest): |
| 156 |
|
| 157 |
def test_add_to_layer_with_effect_remove_add(self): |
| 158 |
timeline = GES.Timeline.new_audio_video() |
| 159 |
video_track, audio_track = timeline.get_tracks() |
| 160 |
layer = timeline.append_layer() |
| 161 |
|
| 162 |
test_clip = GES.TestClip() |
| 163 |
self.assertEqual(test_clip.get_children(True), []) |
| 164 |
self.assertTrue(layer.add_clip(test_clip)) |
| 165 |
audio_source = test_clip.find_track_element(None, GES.AudioSource) |
| 166 |
video_source = test_clip.find_track_element(None, GES.VideoSource) |
| 167 |
|
| 168 |
self.assertTrue(test_clip.set_child_property("volume", 0.0)) |
| 169 |
self.assertEqual(audio_source.get_child_property("volume")[1], 0.0) |
| 170 |
|
| 171 |
effect = GES.Effect.new("agingtv") |
| 172 |
test_clip.add(effect) |
| 173 |
self.assertEqual(audio_source.props.track, audio_track) |
| 174 |
self.assertEqual(video_source.props.track, video_track) |
| 175 |
self.assertEqual(effect.props.track, video_track) |
| 176 |
|
| 177 |
children = test_clip.get_children(True) |
| 178 |
layer.remove_clip(test_clip) |
| 179 |
self.assertEqual(test_clip.get_children(True), children) |
| 180 |
self.assertEqual(audio_source.props.track, None) |
| 181 |
self.assertEqual(video_source.props.track, None) |
| 182 |
self.assertEqual(effect.props.track, None) |
| 183 |
|
| 184 |
self.assertTrue(layer.add_clip(test_clip)) |
| 185 |
self.assertEqual(test_clip.get_children(True), children) |
| 186 |
self.assertEqual(audio_source.props.track, audio_track) |
| 187 |
self.assertEqual(video_source.props.track, video_track) |
| 188 |
self.assertEqual(effect.props.track, video_track) |
| 189 |
|
| 190 |
audio_source = test_clip.find_track_element(None, GES.AudioSource) |
| 191 |
self.assertFalse(audio_source is None) |
| 192 |
self.assertEqual(audio_source.get_child_property("volume")[1], 0.0) |
| 193 |
self.assertEqual(audio_source.props.track, audio_track) |
| 194 |
self.assertEqual(video_source.props.track, video_track) |
| 195 |
self.assertEqual(effect.props.track, video_track) |
| 196 |
|
| 197 |
def test_effects_priority(self): |
| 198 |
timeline = GES.Timeline.new_audio_video() |
| 199 |
layer = timeline.append_layer() |
| 200 |
|
| 201 |
test_clip = GES.TestClip.new() |
| 202 |
layer.add_clip(test_clip) |
| 203 |
self.assert_effects(test_clip) |
| 204 |
|
| 205 |
effect1 = GES.Effect.new("agingtv") |
| 206 |
test_clip.add(effect1) |
| 207 |
self.assert_effects(test_clip, effect1) |
| 208 |
|
| 209 |
test_clip.set_top_effect_index(effect1, 1) |
| 210 |
self.assert_effects(test_clip, effect1) |
| 211 |
test_clip.set_top_effect_index(effect1, 10) |
| 212 |
self.assert_effects(test_clip, effect1) |
| 213 |
|
| 214 |
effect2 = GES.Effect.new("dicetv") |
| 215 |
test_clip.add(effect2) |
| 216 |
self.assert_effects(test_clip, effect1, effect2) |
| 217 |
|
| 218 |
test_clip.remove(effect1) |
| 219 |
self.assert_effects(test_clip, effect2) |
| 220 |
|
| 221 |
def test_effects_index(self): |
| 222 |
timeline = GES.Timeline.new_audio_video() |
| 223 |
layer = timeline.append_layer() |
| 224 |
|
| 225 |
test_clip = GES.TestClip.new() |
| 226 |
layer.add_clip(test_clip) |
| 227 |
self.assert_effects(test_clip) |
| 228 |
|
| 229 |
ref_effects_list = [] |
| 230 |
|
| 231 |
def add_effect(effect): |
| 232 |
test_clip.add(effect) |
| 233 |
ref_effects_list.append(effect) |
| 234 |
|
| 235 |
self.assert_effects(test_clip, *ref_effects_list) |
| 236 |
|
| 237 |
def move_effect(old_index, new_index): |
| 238 |
effect = ref_effects_list[old_index] |
| 239 |
self.assertTrue(test_clip.set_top_effect_index(effect, new_index)) |
| 240 |
|
| 241 |
ref_effects_list.insert(new_index, ref_effects_list.pop(old_index)) |
| 242 |
|
| 243 |
self.assert_effects(test_clip, *ref_effects_list) |
| 244 |
|
| 245 |
effects = ["agingtv", "dicetv", "burn", "gamma", "edgetv", "alpha", |
| 246 |
"exclusion", "chromahold", "coloreffects", "videobalance"] |
| 247 |
|
| 248 |
for effect in effects: |
| 249 |
add_effect(GES.Effect.new(effect)) |
| 250 |
|
| 251 |
move_effect(3, 8) |
| 252 |
move_effect(5, 6) |
| 253 |
move_effect(0, 9) |
| 254 |
|
| 255 |
self.assert_effects(test_clip, *ref_effects_list) |
| 256 |
|
| 257 |
def test_signal_order_when_removing_effect(self): |
| 258 |
timeline = GES.Timeline.new_audio_video() |
| 259 |
layer = timeline.append_layer() |
| 260 |
|
| 261 |
test_clip = GES.TestClip.new() |
| 262 |
layer.add_clip(test_clip) |
| 263 |
self.assert_effects(test_clip) |
| 264 |
|
| 265 |
effect1 = GES.Effect.new("agingtv") |
| 266 |
test_clip.add(effect1) |
| 267 |
effect2 = GES.Effect.new("dicetv") |
| 268 |
test_clip.add(effect2) |
| 269 |
self.assert_effects(test_clip, effect1, effect2) |
| 270 |
|
| 271 |
mainloop = common.create_main_loop() |
| 272 |
|
| 273 |
signals = [] |
| 274 |
|
| 275 |
def handler_cb(*args): |
| 276 |
signals.append(args[-1]) |
| 277 |
|
| 278 |
test_clip.connect("child-removed", handler_cb, "child-removed") |
| 279 |
effect2.connect("notify::priority", handler_cb, "notify::priority") |
| 280 |
test_clip.remove(effect1) |
| 281 |
test_clip.disconnect_by_func(handler_cb) |
| 282 |
effect2.disconnect_by_func(handler_cb) |
| 283 |
self.assert_effects(test_clip, effect2) |
| 284 |
|
| 285 |
mainloop.run(until_empty=True) |
| 286 |
|
| 287 |
self.assertEqual(signals, ["child-removed", "notify::priority"]) |
| 288 |
|
| 289 |
def test_moving_core_track_elements(self): |
| 290 |
clip = self.append_clip() |
| 291 |
clip1 = self.append_clip() |
| 292 |
title_clip = self.append_clip(asset_type=GES.TitleClip) |
| 293 |
|
| 294 |
track_element = clip.find_track_element(None, GES.VideoSource) |
| 295 |
self.assertTrue(clip.remove(track_element)) |
| 296 |
|
| 297 |
track_element1 = clip1.find_track_element(None, GES.VideoSource) |
| 298 |
self.assertTrue(clip1.remove(track_element1)) |
| 299 |
|
| 300 |
self.assertTrue(clip1.add(track_element)) |
| 301 |
self.assertIsNotNone(track_element.get_track()) |
| 302 |
# We can add another TestSource to the clip as it has the same parent |
| 303 |
# asset |
| 304 |
self.assertTrue(clip1.add(track_element1)) |
| 305 |
# We already have a core TrackElement for the video track, not adding |
| 306 |
# a second one. |
| 307 |
self.assertIsNone(track_element1.get_track()) |
| 308 |
|
| 309 |
clip1.remove(track_element) |
| 310 |
clip1.remove(track_element1) |
| 311 |
title = title_clip.find_track_element(None, GES.VideoSource) |
| 312 |
self.assertTrue(title_clip.remove(title)) |
| 313 |
# But we can't add an element that has been created by a TitleClip |
| 314 |
self.assertFalse(clip.add(title)) |
| 315 |
self.assertFalse(title_clip.add(track_element)) |
| 316 |
self.assertTrue(clip.add(track_element)) |
| 317 |
self.assertTrue(clip1.add(track_element1)) |
| 318 |
|
| 319 |
def test_ungroup_regroup(self): |
| 320 |
clip = self.append_clip() |
| 321 |
children = clip.get_children(True) |
| 322 |
|
| 323 |
clip1, clip2 = GES.Container.ungroup(clip, True) |
| 324 |
|
| 325 |
self.assertEqual(clip, clip1) |
| 326 |
clip1_child, = clip1.get_children(True) |
| 327 |
clip2_child, = clip2.get_children(True) |
| 328 |
self.assertCountEqual (children, [clip1_child, clip2_child]) |
| 329 |
|
| 330 |
# can freely move children between the ungrouped clips |
| 331 |
self.assertTrue(clip1.remove(clip1_child)) |
| 332 |
self.assertTrue(clip2.add(clip1_child)) |
| 333 |
|
| 334 |
self.assertTrue(clip2.remove(clip2_child)) |
| 335 |
self.assertTrue(clip1.add(clip2_child)) |
| 336 |
|
| 337 |
grouped = GES.Container.group([clip1, clip2]) |
| 338 |
self.assertEqual(grouped, clip1) |
| 339 |
|
| 340 |
self.assertCountEqual(clip1.get_children(True), |
| 341 |
[clip1_child, clip2_child]) |
| 342 |
self.assertEqual(clip2.get_children(True), []) |
| 343 |
|
| 344 |
# can freely move children between the grouped clips |
| 345 |
self.assertTrue(clip1.remove(clip2_child)) |
| 346 |
self.assertTrue(clip2.add(clip2_child)) |
| 347 |
|
| 348 |
self.assertTrue(clip1.remove(clip1_child)) |
| 349 |
self.assertTrue(clip2.add(clip1_child)) |
| 350 |
|
| 351 |
self.assertTrue(clip2.remove(clip1_child)) |
| 352 |
self.assertTrue(clip1.add(clip1_child)) |
| 353 |
|
| 354 |
self.assertTrue(clip2.remove(clip2_child)) |
| 355 |
self.assertTrue(clip1.add(clip2_child)) |
| 356 |
|
| 357 |
# clip2 no longer part of the timeline |
| 358 |
self.assertIsNone(clip2.props.layer) |
| 359 |
self.assertEqual(clip1.props.layer, self.layer) |
| 360 |
self.assertIsNone(clip2.props.timeline) |
| 361 |
self.assertEqual(clip1.props.timeline, self.timeline) |
| 362 |
|
| 363 |
def test_image_source_asset(self): |
| 364 |
asset = GES.UriClipAsset.request_sync(common.get_asset_uri("png.png")) |
| 365 |
clip = self.layer.add_asset(asset, 0, 0, Gst.SECOND, GES.TrackType.UNKNOWN) |
| 366 |
|
| 367 |
image_src, = clip.get_children(True) |
| 368 |
|
| 369 |
self.assertTrue(image_src.get_asset().is_image()) |
| 370 |
self.assertTrue(isinstance(image_src, GES.VideoUriSource)) |
| 371 |
imagefreeze, = [e for e in image_src.get_nleobject().iterate_recurse() |
| 372 |
if e.get_factory().get_name() == "imagefreeze"] |
| 373 |
|
| 374 |
asset = GES.UriClipAsset.request_sync(common.get_asset_uri("audio_video.ogg")) |
| 375 |
clip = self.layer.add_asset(asset, Gst.SECOND, 0, Gst.SECOND, GES.TrackType.VIDEO) |
| 376 |
video_src, = clip.get_children(True) |
| 377 |
self.assertEqual([e for e in video_src.get_nleobject().iterate_recurse() |
| 378 |
if e.get_factory().get_name() == "imagefreeze"], []) |