Skip to content

Commit

Permalink
Fix short videos with length below 1 second
Browse files Browse the repository at this point in the history
  • Loading branch information
Rados13 committed May 15, 2024
1 parent 79081d5 commit e606a7f
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 19 deletions.
5 changes: 5 additions & 0 deletions lib/compositor_utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ defmodule RecordingConverter.Compositor do

@avatar_threshold_ns 1_000_000_000

@spec avatar_threshold_ns() :: non_neg_integer()
def avatar_threshold_ns() do
@avatar_threshold_ns
end

@spec server_setup(binary) :: :start_locally | {:start_locally, String.t()}
def server_setup(compositor_path) do
compositor_path = compositor_path
Expand Down
42 changes: 24 additions & 18 deletions lib/report_parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ defmodule RecordingConverter.ReportParser do
bucket_name
|> get_report(report_path)
|> Map.fetch!("tracks")
|> Enum.reject(fn {_key, track} ->
calculate_duration_in_ns(track) < Compositor.avatar_threshold_ns()
end)
|> Enum.map(fn {key, value} -> Map.put(value, :id, key) end)
end

Expand All @@ -31,24 +34,7 @@ defmodule RecordingConverter.ReportParser do

@spec calculate_track_end(map(), non_neg_integer()) :: non_neg_integer()
def calculate_track_end(track, offset) do
clock_rate_ms = div(track["clock_rate"], 1_000)

end_timestamp = track["end_timestamp"]
start_timestamp = track["start_timestamp"]

timestamp_difference =
if end_timestamp < start_timestamp do
end_timestamp + @max_timestamp_value - start_timestamp
else
end_timestamp - start_timestamp
end

difference_in_milliseconds = div(timestamp_difference, clock_rate_ms)

duration =
(difference_in_milliseconds - @delta_timestamp_milliseconds)
|> Membrane.Time.milliseconds()
|> Membrane.Time.as_nanoseconds(:round)
duration = calculate_duration_in_ns(track)

offset + duration
end
Expand Down Expand Up @@ -139,6 +125,26 @@ defmodule RecordingConverter.ReportParser do
{audio_end_timestamp, video_end_timestamp}
end

defp calculate_duration_in_ns(track) do
clock_rate_ms = div(track["clock_rate"], 1_000)

end_timestamp = track["end_timestamp"]
start_timestamp = track["start_timestamp"]

timestamp_difference =
if end_timestamp < start_timestamp do
end_timestamp + @max_timestamp_value - start_timestamp
else
end_timestamp - start_timestamp
end

difference_in_milliseconds = div(timestamp_difference, clock_rate_ms)

(difference_in_milliseconds - @delta_timestamp_milliseconds)
|> Membrane.Time.milliseconds()
|> Membrane.Time.as_nanoseconds(:round)
end

defp calculate_end_timestamp(tracks) do
if Enum.count(tracks) > 0 do
{_atom, _video_track, timestamp} = Enum.at(tracks, -1)
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
45 changes: 45 additions & 0 deletions test/fixtures/short_videos/report.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"tracks": {
"853ec649-88a9-4629-991d-f8e2b17f93f7.msr": {
"offset": 0,
"type": "video",
"encoding": "H264",
"metadata": null,
"clock_rate": 90000,
"start_timestamp": 2620484184,
"end_timestamp": 2620691744,
"origin": 1
},
"87b2a39e-b62a-48b4-bc37-135da9c4d0d1.msr": {
"offset": 10000000000,
"type": "video",
"encoding": "H264",
"metadata": null,
"clock_rate": 90000,
"start_timestamp": 493739605,
"end_timestamp": 493951365,
"origin": 1
},
"8d49170b-d6d3-497d-ba5c-ba2837247b3c.msr": {
"offset": 100000000,
"type": "video",
"encoding": "H264",
"metadata": null,
"clock_rate": 90000,
"start_timestamp": 2356366771,
"end_timestamp": 2356455401,
"origin": 1
},
"9cfd9266-2256-406b-b9a1-03383243d1d8.msr": {
"offset": 1000000,
"type": "video",
"encoding": "H264",
"metadata": null,
"clock_rate": 90000,
"start_timestamp": 3020099711,
"end_timestamp": 3020120681,
"origin": 1
}
},
"recording_id": "74"
}
3 changes: 2 additions & 1 deletion test/pipeline_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ defmodule RecordingConverter.PipelineTest do
requests: 16,
factor: 1
},
%{type: "peers-before-recording-started", requests: 15, factor: 1}
%{type: "peers-before-recording-started", requests: 15, factor: 1},
%{type: "short_videos", requests: 6, factor: 1}
]

for test <- tests do
Expand Down

0 comments on commit e606a7f

Please sign in to comment.