Chromium Code Reviews| Index: webrtc/modules/audio_coding/codecs/legacy_encoded_audio_frame.cc |
| diff --git a/webrtc/modules/audio_coding/codecs/legacy_encoded_audio_frame.cc b/webrtc/modules/audio_coding/codecs/legacy_encoded_audio_frame.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..fd79dc9dd0eb6aa40846020c34d0ecd86564fdff |
| --- /dev/null |
| +++ b/webrtc/modules/audio_coding/codecs/legacy_encoded_audio_frame.cc |
| @@ -0,0 +1,102 @@ |
| +/* |
| + * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
| + * |
| + * Use of this source code is governed by a BSD-style license |
| + * that can be found in the LICENSE file in the root of the source |
| + * tree. An additional intellectual property rights grant can be found |
| + * in the file PATENTS. All contributing project authors may |
| + * be found in the AUTHORS file in the root of the source tree. |
| + */ |
| + |
| +#include "webrtc/modules/audio_coding/codecs/legacy_encoded_audio_frame.h" |
| + |
| +#include <algorithm> |
| +#include <memory> |
| +#include <utility> |
| + |
| +namespace webrtc { |
| + |
| +LegacyEncodedAudioFrame::LegacyEncodedAudioFrame(AudioDecoder* decoder, |
| + rtc::Buffer* payload, |
|
hlundin-webrtc
2016/09/15 08:49:14
As discussed offline, and simply for the record, I
ossu
2016/09/15 08:58:11
I'd like that too. It's almost (but not completely
kwiberg-webrtc
2016/09/15 09:03:43
Pass it by value, then.
For cheap-to-move move-on
ossu
2016/09/15 09:15:13
Of course I don't _prefer_ the style guide over ea
kwiberg-webrtc
2016/09/15 09:29:53
Sorry if I sounded a bit aggressive. But it is the
hlundin-webrtc
2016/09/15 09:43:38
I tricked you into discussing the merits of the st
ossu
2016/09/15 11:56:21
So are we saying rtc::Buffer&& in LegacyEncodedAud
kwiberg-webrtc
2016/09/15 12:06:43
Yes, I'd say taking an rtc::Buffer&& argument is b
ossu
2016/09/15 14:41:05
FOR GLORY!
|
| + bool is_primary_payload) |
| + : decoder_(decoder), |
| + payload_(std::move(*payload)), |
| + is_primary_payload_(is_primary_payload) {} |
| + |
| +LegacyEncodedAudioFrame::~LegacyEncodedAudioFrame() = default; |
| + |
| +size_t LegacyEncodedAudioFrame::Duration() const { |
| + int ret; |
| + if (is_primary_payload_) { |
| + ret = decoder_->PacketDuration(payload_.data(), payload_.size()); |
| + } else { |
| + ret = decoder_->PacketDurationRedundant(payload_.data(), payload_.size()); |
| + } |
| + return (ret < 0) ? 0 : static_cast<size_t>(ret); |
| +} |
| + |
| +rtc::Optional<AudioDecoder::EncodedAudioFrame::DecodeResult> |
| +LegacyEncodedAudioFrame::Decode(rtc::ArrayView<int16_t> decoded) const { |
| + AudioDecoder::SpeechType speech_type = AudioDecoder::kSpeech; |
| + int ret; |
| + if (is_primary_payload_) { |
| + ret = decoder_->Decode( |
| + payload_.data(), payload_.size(), decoder_->SampleRateHz(), |
| + decoded.size() * sizeof(int16_t), decoded.data(), &speech_type); |
| + } else { |
| + ret = decoder_->DecodeRedundant( |
| + payload_.data(), payload_.size(), decoder_->SampleRateHz(), |
| + decoded.size() * sizeof(int16_t), decoded.data(), &speech_type); |
| + } |
| + |
| + if (ret < 0) |
| + return rtc::Optional<DecodeResult>(); |
| + |
| + return rtc::Optional<DecodeResult>({static_cast<size_t>(ret), speech_type}); |
| +} |
| + |
| +std::vector<AudioDecoder::ParseResult> LegacyEncodedAudioFrame::SplitBySamples( |
| + AudioDecoder* decoder, |
| + rtc::Buffer* payload, |
| + uint32_t timestamp, |
| + bool is_primary, |
| + size_t bytes_per_ms, |
| + uint32_t timestamps_per_ms) { |
| + RTC_DCHECK(payload->data()); |
| + std::vector<AudioDecoder::ParseResult> results; |
| + size_t split_size_bytes = payload->size(); |
| + |
| + // Find a "chunk size" >= 20 ms and < 40 ms. |
| + const size_t min_chunk_size = bytes_per_ms * 20; |
| + if (min_chunk_size >= payload->size()) { |
| + std::unique_ptr<LegacyEncodedAudioFrame> frame( |
| + new LegacyEncodedAudioFrame(decoder, payload, is_primary)); |
| + results.emplace_back(timestamp, is_primary, std::move(frame)); |
| + } else { |
| + // Reduce the split size by half as long as |split_size_bytes| is at least |
| + // twice the minimum chunk size (so that the resulting size is at least as |
| + // large as the minimum chunk size). |
| + while (split_size_bytes >= 2 * min_chunk_size) { |
| + split_size_bytes >>= 1; |
|
hlundin-webrtc
2016/09/15 08:49:14
We could get rid of this very questionable "optimi
ossu
2016/09/15 08:58:11
Acknowledged.
kwiberg-webrtc
2016/09/15 13:01:25
+1. split_size_bytes is unsigned, so the compiler
|
| + } |
| + |
| + const uint32_t timestamps_per_chunk = static_cast<uint32_t>( |
| + split_size_bytes * timestamps_per_ms / bytes_per_ms); |
| + for (size_t byte_offset = 0, timestamp_offset = 0; |
| + byte_offset < payload->size(); |
| + byte_offset += split_size_bytes, |
| + timestamp_offset += timestamps_per_chunk) { |
| + split_size_bytes = |
| + std::min(split_size_bytes, payload->size() - byte_offset); |
| + rtc::Buffer new_payload(payload->data() + byte_offset, split_size_bytes); |
| + std::unique_ptr<LegacyEncodedAudioFrame> frame( |
| + new LegacyEncodedAudioFrame(decoder, &new_payload, is_primary)); |
| + results.emplace_back(timestamp + timestamp_offset, is_primary, |
| + std::move(frame)); |
| + } |
| + } |
| + |
| + return results; |
| +} |
| + |
| +} // namespace webrtc |