Skip to content

Commit 1547fb1

Browse files
committed
New unit tests around GetMaxFrame / GetMaxTime. Improvemetns to apply_json_to_timeline() to not clear all cache when setting the timeline's "duration" - an optimization for performance
1 parent f50e18e commit 1547fb1

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

src/Timeline.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1526,6 +1526,7 @@ void Timeline::apply_json_to_effects(Json::Value change, EffectBase* existing_ef
15261526

15271527
// Apply JSON diff to timeline properties
15281528
void Timeline::apply_json_to_timeline(Json::Value change) {
1529+
bool cache_dirty = true;
15291530

15301531
// Get key and type of change
15311532
std::string change_type = change["type"].asString();
@@ -1534,9 +1535,6 @@ void Timeline::apply_json_to_timeline(Json::Value change) {
15341535
if (change["key"].size() >= 2)
15351536
sub_key = change["key"][(uint)1].asString();
15361537

1537-
// Clear entire cache
1538-
ClearAllCache();
1539-
15401538
// Determine type of change operation
15411539
if (change_type == "insert" || change_type == "update") {
15421540

@@ -1558,6 +1556,9 @@ void Timeline::apply_json_to_timeline(Json::Value change) {
15581556
// Update duration of timeline
15591557
info.duration = change["value"].asDouble();
15601558
info.video_length = info.fps.ToFloat() * info.duration;
1559+
1560+
// We don't want to clear cache for duration adjustments
1561+
cache_dirty = false;
15611562
}
15621563
else if (root_key == "width") {
15631564
// Set width
@@ -1645,6 +1646,10 @@ void Timeline::apply_json_to_timeline(Json::Value change) {
16451646

16461647
}
16471648

1649+
if (cache_dirty) {
1650+
// Clear entire cache
1651+
ClearAllCache();
1652+
}
16481653
}
16491654

16501655
// Clear all caches

tests/Timeline.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,7 @@ TEST_CASE( "GetMaxFrame and GetMaxTime", "[libopenshot][timeline]" )
607607
std::stringstream path1;
608608
path1 << TEST_MEDIA_PATH << "interlaced.png";
609609
Clip clip1(path1.str());
610+
clip1.Id("C1");
610611
clip1.Layer(1);
611612
clip1.Position(50);
612613
clip1.End(45);
@@ -616,6 +617,7 @@ TEST_CASE( "GetMaxFrame and GetMaxTime", "[libopenshot][timeline]" )
616617
CHECK(t.GetMaxFrame() == 95 * 30 + 1);
617618

618619
Clip clip2(path1.str());
620+
clip2.Id("C2");
619621
clip2.Layer(2);
620622
clip2.Position(0);
621623
clip2.End(55);
@@ -640,6 +642,22 @@ TEST_CASE( "GetMaxFrame and GetMaxTime", "[libopenshot][timeline]" )
640642
t.RemoveClip(&clip1);
641643
CHECK(t.GetMaxFrame() == 115 * 30 + 1);
642644
CHECK(t.GetMaxTime() == Approx(115.0).margin(0.001));
645+
646+
// Update Clip's basic properties with JSON Diff
647+
std::stringstream json_change1;
648+
json_change1 << "[{\"type\":\"update\",\"key\":[\"clips\",{\"id\":\"C2\"}],\"value\":{\"id\":\"C2\",\"layer\":4000000,\"position\":0.0,\"start\":0,\"end\":10},\"partial\":false}]";
649+
t.ApplyJsonDiff(json_change1.str());
650+
651+
CHECK(t.GetMaxFrame() == 10 * 30 + 1);
652+
CHECK(t.GetMaxTime() == Approx(10.0).margin(0.001));
653+
654+
// Insert NEW Clip with JSON Diff
655+
std::stringstream json_change2;
656+
json_change2 << "[{\"type\":\"insert\",\"key\":[\"clips\"],\"value\":{\"id\":\"C3\",\"layer\":4000000,\"position\":10.0,\"start\":0,\"end\":10,\"reader\":{\"acodec\":\"\",\"audio_bit_rate\":0,\"audio_stream_index\":-1,\"audio_timebase\":{\"den\":1,\"num\":1},\"channel_layout\":4,\"channels\":0,\"display_ratio\":{\"den\":1,\"num\":1},\"duration\":3600.0,\"file_size\":\"160000\",\"fps\":{\"den\":1,\"num\":30},\"has_audio\":false,\"has_single_image\":true,\"has_video\":true,\"height\":200,\"interlaced_frame\":false,\"metadata\":{},\"path\":\"" << path1.str() << "\",\"pixel_format\":-1,\"pixel_ratio\":{\"den\":1,\"num\":1},\"sample_rate\":0,\"top_field_first\":true,\"type\":\"QtImageReader\",\"vcodec\":\"\",\"video_bit_rate\":0,\"video_length\":\"108000\",\"video_stream_index\":-1,\"video_timebase\":{\"den\":30,\"num\":1},\"width\":200}},\"partial\":false}]";
657+
t.ApplyJsonDiff(json_change2.str());
658+
659+
CHECK(t.GetMaxFrame() == 20 * 30 + 1);
660+
CHECK(t.GetMaxTime() == Approx(20.0).margin(0.001));
643661
}
644662

645663
TEST_CASE( "Multi-threaded Timeline GetFrame", "[libopenshot][timeline]" )

0 commit comments

Comments
 (0)