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