OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
| 3 * |
| 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ |
| 10 |
| 11 #include "webrtc/modules/audio_coding/codecs/legacy_encoded_audio_frame.h" |
| 12 |
| 13 #include <algorithm> |
| 14 #include <memory> |
| 15 #include <utility> |
| 16 |
| 17 namespace webrtc { |
| 18 |
| 19 LegacyEncodedAudioFrame::LegacyEncodedAudioFrame(AudioDecoder* decoder, |
| 20 rtc::Buffer&& payload, |
| 21 bool is_primary_payload) |
| 22 : decoder_(decoder), |
| 23 payload_(std::move(payload)), |
| 24 is_primary_payload_(is_primary_payload) {} |
| 25 |
| 26 LegacyEncodedAudioFrame::~LegacyEncodedAudioFrame() = default; |
| 27 |
| 28 size_t LegacyEncodedAudioFrame::Duration() const { |
| 29 int ret; |
| 30 if (is_primary_payload_) { |
| 31 ret = decoder_->PacketDuration(payload_.data(), payload_.size()); |
| 32 } else { |
| 33 ret = decoder_->PacketDurationRedundant(payload_.data(), payload_.size()); |
| 34 } |
| 35 return (ret < 0) ? 0 : static_cast<size_t>(ret); |
| 36 } |
| 37 |
| 38 rtc::Optional<AudioDecoder::EncodedAudioFrame::DecodeResult> |
| 39 LegacyEncodedAudioFrame::Decode(rtc::ArrayView<int16_t> decoded) const { |
| 40 AudioDecoder::SpeechType speech_type = AudioDecoder::kSpeech; |
| 41 int ret; |
| 42 if (is_primary_payload_) { |
| 43 ret = decoder_->Decode( |
| 44 payload_.data(), payload_.size(), decoder_->SampleRateHz(), |
| 45 decoded.size() * sizeof(int16_t), decoded.data(), &speech_type); |
| 46 } else { |
| 47 ret = decoder_->DecodeRedundant( |
| 48 payload_.data(), payload_.size(), decoder_->SampleRateHz(), |
| 49 decoded.size() * sizeof(int16_t), decoded.data(), &speech_type); |
| 50 } |
| 51 |
| 52 if (ret < 0) |
| 53 return rtc::Optional<DecodeResult>(); |
| 54 |
| 55 return rtc::Optional<DecodeResult>({static_cast<size_t>(ret), speech_type}); |
| 56 } |
| 57 |
| 58 std::vector<AudioDecoder::ParseResult> LegacyEncodedAudioFrame::SplitBySamples( |
| 59 AudioDecoder* decoder, |
| 60 rtc::Buffer&& payload, |
| 61 uint32_t timestamp, |
| 62 bool is_primary, |
| 63 size_t bytes_per_ms, |
| 64 uint32_t timestamps_per_ms) { |
| 65 RTC_DCHECK(payload.data()); |
| 66 std::vector<AudioDecoder::ParseResult> results; |
| 67 size_t split_size_bytes = payload.size(); |
| 68 |
| 69 // Find a "chunk size" >= 20 ms and < 40 ms. |
| 70 const size_t min_chunk_size = bytes_per_ms * 20; |
| 71 if (min_chunk_size >= payload.size()) { |
| 72 std::unique_ptr<LegacyEncodedAudioFrame> frame( |
| 73 new LegacyEncodedAudioFrame(decoder, std::move(payload), is_primary)); |
| 74 results.emplace_back(timestamp, is_primary, std::move(frame)); |
| 75 } else { |
| 76 // Reduce the split size by half as long as |split_size_bytes| is at least |
| 77 // twice the minimum chunk size (so that the resulting size is at least as |
| 78 // large as the minimum chunk size). |
| 79 while (split_size_bytes >= 2 * min_chunk_size) { |
| 80 split_size_bytes /= 2; |
| 81 } |
| 82 |
| 83 const uint32_t timestamps_per_chunk = static_cast<uint32_t>( |
| 84 split_size_bytes * timestamps_per_ms / bytes_per_ms); |
| 85 size_t byte_offset; |
| 86 uint32_t timestamp_offset; |
| 87 for (byte_offset = 0, timestamp_offset = 0; |
| 88 byte_offset < payload.size(); |
| 89 byte_offset += split_size_bytes, |
| 90 timestamp_offset += timestamps_per_chunk) { |
| 91 split_size_bytes = |
| 92 std::min(split_size_bytes, payload.size() - byte_offset); |
| 93 rtc::Buffer new_payload(payload.data() + byte_offset, split_size_bytes); |
| 94 std::unique_ptr<LegacyEncodedAudioFrame> frame( |
| 95 new LegacyEncodedAudioFrame(decoder, std::move(new_payload), |
| 96 is_primary)); |
| 97 results.emplace_back(timestamp + timestamp_offset, is_primary, |
| 98 std::move(frame)); |
| 99 } |
| 100 } |
| 101 |
| 102 return results; |
| 103 } |
| 104 |
| 105 } // namespace webrtc |
OLD | NEW |