Skip to content

Commit

Permalink
Add duration to opus encoder output
Browse files Browse the repository at this point in the history
This CL adds duration metadata to encoded chunks output by opus encoder.

Bug: 1235153
Change-Id: I0c92fa809c9cf4dd1d609b0ac44dcfd7486a6fb2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3084018
Commit-Queue: Thomas Guilbert <tguilbert@chromium.org>
Reviewed-by: Eugene Zemtsov <eugene@chromium.org>
Cr-Commit-Position: refs/heads/master@{#910429}
  • Loading branch information
tguilbert-google authored and Chromium LUCI CQ committed Aug 10, 2021
1 parent 485eacc commit 49cf621
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 6 deletions.
10 changes: 8 additions & 2 deletions media/audio/audio_opus_encoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,15 @@ void AudioOpusEncoder::OnFifoOutput(const AudioBus& output_bus,
desc = PrepareExtraData();
need_to_emit_extra_data_ = false;
}

auto ts = base::TimeTicks() + timestamp_tracker_->GetTimestamp();
EncodedAudioBuffer encoded_buffer(
converted_params_, std::move(encoded_data), encoded_data_size, ts);

auto duration = timestamp_tracker_->GetFrameDuration(
converted_params_.frames_per_buffer());

EncodedAudioBuffer encoded_buffer(converted_params_,
std::move(encoded_data),
encoded_data_size, ts, duration);
output_cb_.Run(std::move(encoded_buffer), desc);
}
timestamp_tracker_->AddFrames(converted_params_.frames_per_buffer());
Expand Down
6 changes: 4 additions & 2 deletions media/base/audio_encoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ AudioEncoder::Options::~Options() = default;
EncodedAudioBuffer::EncodedAudioBuffer(const AudioParameters& params,
std::unique_ptr<uint8_t[]> data,
size_t size,
base::TimeTicks timestamp)
base::TimeTicks timestamp,
base::TimeDelta duration)
: params(params),
encoded_data(std::move(data)),
encoded_data_size(size),
timestamp(timestamp) {}
timestamp(timestamp),
duration(duration) {}

EncodedAudioBuffer::EncodedAudioBuffer(EncodedAudioBuffer&&) = default;

Expand Down
9 changes: 8 additions & 1 deletion media/base/audio_encoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "media/base/audio_parameters.h"
#include "media/base/media_export.h"
#include "media/base/status.h"
#include "media/base/timestamp_constants.h"
#include "third_party/abseil-cpp/absl/types/optional.h"

namespace media {
Expand All @@ -24,7 +25,8 @@ struct MEDIA_EXPORT EncodedAudioBuffer {
EncodedAudioBuffer(const AudioParameters& params,
std::unique_ptr<uint8_t[]> data,
size_t size,
base::TimeTicks timestamp);
base::TimeTicks timestamp,
base::TimeDelta duration = media::kNoTimestamp);
EncodedAudioBuffer(EncodedAudioBuffer&&);
~EncodedAudioBuffer();

Expand All @@ -46,6 +48,11 @@ struct MEDIA_EXPORT EncodedAudioBuffer {
// The capture time of the first sample of the current AudioBus, or a previous
// AudioBus If this output was generated because of a call to Flush().
const base::TimeTicks timestamp;

// The duration of the encoded samples, if they were decoded and played out.
// A duration of media::kNoTimestamp means we don't know the duration or don't
// care about it.
const base::TimeDelta duration;
};

// Defines an interface for audio encoders.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ void AudioEncoder::CallOutputCallback(
std::move(encoded_buffer.encoded_data), encoded_buffer.encoded_data_size);
buffer->set_timestamp(encoded_buffer.timestamp - base::TimeTicks());
buffer->set_is_key_frame(true);
buffer->set_duration(encoded_buffer.duration);
auto* chunk = MakeGarbageCollected<EncodedAudioChunk>(std::move(buffer));

auto* metadata = MakeGarbageCollected<EncodedAudioChunkMetadata>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,17 @@ promise_test(async t => {
encoder.close();
assert_greater_than_equal(outputs.length, data_count);
assert_equals(outputs[0].timestamp, 0, "first chunk timestamp");
let total_encoded_duration = 0
for (chunk of outputs) {
assert_greater_than(chunk.byteLength, 0);
assert_greater_than_equal(timestamp_us, chunk.timestamp);
assert_greater_than_equal(chunk.duration, 0);
assert_greater_than(chunk.duration, 0);
total_encoded_duration += chunk.duration;
}

// The total duration might be padded with silence.
assert_greater_than_equal(
total_encoded_duration, total_duration_s * 1_000_000);
}, 'Simple audio encoding');

promise_test(async t => {
Expand Down

0 comments on commit 49cf621

Please sign in to comment.