| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# |
| 3 |
# Copyright (c) 2019 Thibault Saunier <[email protected]> |
| 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 gi |
| 24 |
import tempfile |
| 25 |
|
| 26 |
gi.require_version("Gst", "1.0") |
| 27 |
gi.require_version("GES", "1.0") |
| 28 |
gi.require_version("GstPbutils", "1.0") |
| 29 |
|
| 30 |
from gi.repository import Gst # noqa |
| 31 |
from gi.repository import GstPbutils # noqa |
| 32 |
from gi.repository import GLib # noqa |
| 33 |
from gi.repository import GES # noqa |
| 34 |
import unittest # noqa |
| 35 |
from unittest import mock |
| 36 |
|
| 37 |
from . import common |
| 38 |
from .common import GESSimpleTimelineTest # noqa |
| 39 |
|
| 40 |
Gst.init(None) |
| 41 |
GES.init() |
| 42 |
|
| 43 |
|
| 44 |
class TestTimeline(GESSimpleTimelineTest): |
| 45 |
|
| 46 |
def test_request_relocated_assets_sync(self): |
| 47 |
path = os.path.join(__file__, "../../../", "png.png") |
| 48 |
with self.assertRaises(GLib.Error): |
| 49 |
GES.UriClipAsset.request_sync(Gst.filename_to_uri(path)) |
| 50 |
|
| 51 |
GES.add_missing_uri_relocation_uri(Gst.filename_to_uri(os.path.join(__file__, "../../assets")), False) |
| 52 |
path = os.path.join(__file__, "../../", "png.png") |
| 53 |
self.assertEqual(GES.UriClipAsset.request_sync(Gst.filename_to_uri(path)).props.id, |
| 54 |
Gst.filename_to_uri(os.path.join(__file__, "../../assets/png.png"))) |
| 55 |
|
| 56 |
def test_request_relocated_twice(self): |
| 57 |
GES.add_missing_uri_relocation_uri(Gst.filename_to_uri(os.path.join(__file__, "../../")), True) |
| 58 |
proj = GES.Project.new() |
| 59 |
|
| 60 |
asset = proj.create_asset_sync("file:///png.png", GES.UriClip) |
| 61 |
self.assertIsNotNone(asset) |
| 62 |
asset = proj.create_asset_sync("file:///png.png", GES.UriClip) |
| 63 |
self.assertIsNotNone(asset) |
| 64 |
|
| 65 |
@unittest.skipUnless(*common.can_generate_assets()) |
| 66 |
def test_reload_asset(self): |
| 67 |
with common.created_video_asset(num_bufs=2, framerate="2/1") as uri: |
| 68 |
asset0 = GES.UriClipAsset.request_sync(uri) |
| 69 |
self.assertEqual(asset0.props.duration, Gst.SECOND) |
| 70 |
|
| 71 |
with common.created_video_asset(uri, 4, framerate="2/1") as uri: |
| 72 |
GES.Asset.needs_reload(GES.UriClip, uri) |
| 73 |
asset1 = GES.UriClipAsset.request_sync(uri) |
| 74 |
self.assertEqual(asset1.props.duration, 2 * Gst.SECOND) |
| 75 |
self.assertEqual(asset1, asset0) |
| 76 |
|
| 77 |
with common.created_video_asset(uri, 6, framerate="2/1") as uri: |
| 78 |
mainloop = common.create_main_loop() |
| 79 |
|
| 80 |
def asset_loaded_cb(_, res, mainloop): |
| 81 |
asset2 = GES.Asset.request_finish(res) |
| 82 |
self.assertEqual(asset2.props.duration, 3 * Gst.SECOND) |
| 83 |
self.assertEqual(asset2, asset0) |
| 84 |
mainloop.quit() |
| 85 |
|
| 86 |
GES.Asset.needs_reload(GES.UriClip, uri) |
| 87 |
GES.Asset.request_async(GES.UriClip, uri, None, asset_loaded_cb, mainloop) |
| 88 |
mainloop.run() |
| 89 |
|
| 90 |
def test_asset_metadata_on_reload(self): |
| 91 |
mainloop = GLib.MainLoop() |
| 92 |
|
| 93 |
unused, xges_path = tempfile.mkstemp(suffix=".xges") |
| 94 |
project_uri = Gst.filename_to_uri(os.path.abspath(xges_path)) |
| 95 |
|
| 96 |
asset_uri = Gst.filename_to_uri(os.path.join(__file__, "../../assets/audio_video.ogg")) |
| 97 |
xges = """<ges version='0.3'> |
| 98 |
<project properties='properties;' metadatas='metadatas;'> |
| 99 |
<ressources> |
| 100 |
<asset id='%(uri)s' extractable-type-name='GESUriClip' properties='properties, supported-formats=(int)6, duration=(guint64)2003000000;' metadatas='metadatas, container-format=(string)Matroska, language-code=(string)und, application-name=(string)Lavc56.60.100, encoder-version=(uint)0, audio-codec=(string)Vorbis, nominal-bitrate=(uint)80000, bitrate=(uint)80000, video-codec=(string)"On2 VP8", file-size=(guint64)223340, foo=(string)bar;' > |
| 101 |
</asset> |
| 102 |
</ressources> |
| 103 |
</project> |
| 104 |
</ges>""" % {"uri": asset_uri} |
| 105 |
with open(xges_path, "w") as xges_file: |
| 106 |
xges_file.write(xges) |
| 107 |
|
| 108 |
def loaded_cb(project, timeline): |
| 109 |
asset = project.list_assets(GES.Extractable)[0] |
| 110 |
self.assertEqual(asset.get_meta("foo"), "bar") |
| 111 |
mainloop.quit() |
| 112 |
|
| 113 |
loaded_project = GES.Project(uri=project_uri, extractable_type=GES.Timeline) |
| 114 |
loaded_project.connect("loaded", loaded_cb) |
| 115 |
timeline = loaded_project.extract() |
| 116 |
mainloop.run() |
| 117 |
|
| 118 |
def test_asset_load_serialized_info(self): |
| 119 |
mainloop = GLib.MainLoop() |
| 120 |
|
| 121 |
serialized_infos = {} |
| 122 |
n_calls = 0 |
| 123 |
n_cache_hits = 0 |
| 124 |
|
| 125 |
def load_serialized_info_cb(_manager, uri): |
| 126 |
nonlocal n_calls, n_cache_hits, serialized_infos |
| 127 |
|
| 128 |
n_calls += 1 |
| 129 |
res = serialized_infos.get(uri) |
| 130 |
if res: |
| 131 |
n_cache_hits += 1 |
| 132 |
return res |
| 133 |
|
| 134 |
GES.DiscovererManager.get_default().connect("load-serialized-info", |
| 135 |
load_serialized_info_cb) |
| 136 |
|
| 137 |
self.assertEqual(n_calls, 0) |
| 138 |
asset = GES.UriClipAsset.request_sync(Gst.filename_to_uri(os.path.join(__file__, "../../assets/audio_video.ogg"))) |
| 139 |
self.assertEqual(n_calls, 1) |
| 140 |
self.assertEqual(n_cache_hits, 0) |
| 141 |
|
| 142 |
serialized_infos[asset.get_id()] = asset.get_info() |
| 143 |
|
| 144 |
# Clear the GES internal asset cache |
| 145 |
GES.deinit() |
| 146 |
GES.init() |
| 147 |
|
| 148 |
# Connect to the new manager, previous one was destroyed on deinit |
| 149 |
GES.DiscovererManager.get_default().connect("load-serialized-info", |
| 150 |
load_serialized_info_cb) |
| 151 |
asset = GES.UriClipAsset.request_sync(Gst.filename_to_uri(os.path.join(__file__, "../../assets/audio_video.ogg"))) |
| 152 |
self.assertEqual(n_calls, 2) |
| 153 |
self.assertEqual(n_cache_hits, 1) |