OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 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/audio_decoder.h" | 11 #include "webrtc/modules/audio_coding/codecs/audio_decoder.h" |
12 | 12 |
13 #include <assert.h> | 13 #include <assert.h> |
14 | 14 |
| 15 #include <utility> |
| 16 |
15 #include "webrtc/base/array_view.h" | 17 #include "webrtc/base/array_view.h" |
16 #include "webrtc/base/checks.h" | 18 #include "webrtc/base/checks.h" |
17 #include "webrtc/base/sanitizer.h" | 19 #include "webrtc/base/sanitizer.h" |
18 #include "webrtc/base/trace_event.h" | 20 #include "webrtc/base/trace_event.h" |
19 | 21 |
20 namespace webrtc { | 22 namespace webrtc { |
21 | 23 |
| 24 namespace { |
| 25 class LegacyFrame final : public AudioDecoder::EncodedAudioFrame { |
| 26 public: |
| 27 LegacyFrame(AudioDecoder* decoder, |
| 28 rtc::Buffer&& payload, |
| 29 bool is_primary_payload) |
| 30 : decoder_(decoder), |
| 31 payload_(std::move(payload)), |
| 32 is_primary_payload_(is_primary_payload) {} |
| 33 |
| 34 size_t Duration() const override { |
| 35 int ret; |
| 36 if (is_primary_payload_) { |
| 37 ret = decoder_->PacketDuration(payload_.data(), payload_.size()); |
| 38 } else { |
| 39 ret = decoder_->PacketDurationRedundant(payload_.data(), payload_.size()); |
| 40 } |
| 41 return (ret < 0) ? 0 : static_cast<size_t>(ret); |
| 42 } |
| 43 |
| 44 rtc::Optional<DecodeResult> Decode( |
| 45 rtc::ArrayView<int16_t> decoded) const override { |
| 46 AudioDecoder::SpeechType speech_type = AudioDecoder::kSpeech; |
| 47 int ret; |
| 48 if (is_primary_payload_) { |
| 49 ret = decoder_->Decode( |
| 50 payload_.data(), payload_.size(), decoder_->SampleRateHz(), |
| 51 decoded.size() * sizeof(int16_t), decoded.data(), &speech_type); |
| 52 } else { |
| 53 ret = decoder_->DecodeRedundant( |
| 54 payload_.data(), payload_.size(), decoder_->SampleRateHz(), |
| 55 decoded.size() * sizeof(int16_t), decoded.data(), &speech_type); |
| 56 } |
| 57 |
| 58 if (ret < 0) |
| 59 return rtc::Optional<DecodeResult>(); |
| 60 |
| 61 return rtc::Optional<DecodeResult>({static_cast<size_t>(ret), speech_type}); |
| 62 } |
| 63 |
| 64 private: |
| 65 AudioDecoder* const decoder_; |
| 66 const rtc::Buffer payload_; |
| 67 const bool is_primary_payload_; |
| 68 }; |
| 69 } // namespace |
| 70 |
| 71 AudioDecoder::ParseResult::ParseResult() = default; |
| 72 AudioDecoder::ParseResult::ParseResult(ParseResult&& b) = default; |
| 73 AudioDecoder::ParseResult::ParseResult(uint32_t timestamp, |
| 74 bool primary, |
| 75 std::unique_ptr<EncodedAudioFrame> frame) |
| 76 : timestamp(timestamp), primary(primary), frame(std::move(frame)) {} |
| 77 |
| 78 AudioDecoder::ParseResult::~ParseResult() = default; |
| 79 |
| 80 AudioDecoder::ParseResult& AudioDecoder::ParseResult::operator=( |
| 81 ParseResult&& b) = default; |
| 82 |
| 83 std::vector<AudioDecoder::ParseResult> AudioDecoder::ParsePayload( |
| 84 rtc::Buffer&& payload, |
| 85 uint32_t timestamp, |
| 86 bool is_primary) { |
| 87 std::vector<ParseResult> results; |
| 88 std::unique_ptr<EncodedAudioFrame> frame( |
| 89 new LegacyFrame(this, std::move(payload), is_primary)); |
| 90 results.emplace_back(timestamp, is_primary, std::move(frame)); |
| 91 return results; |
| 92 } |
| 93 |
22 int AudioDecoder::Decode(const uint8_t* encoded, size_t encoded_len, | 94 int AudioDecoder::Decode(const uint8_t* encoded, size_t encoded_len, |
23 int sample_rate_hz, size_t max_decoded_bytes, | 95 int sample_rate_hz, size_t max_decoded_bytes, |
24 int16_t* decoded, SpeechType* speech_type) { | 96 int16_t* decoded, SpeechType* speech_type) { |
25 TRACE_EVENT0("webrtc", "AudioDecoder::Decode"); | 97 TRACE_EVENT0("webrtc", "AudioDecoder::Decode"); |
26 rtc::MsanCheckInitialized(rtc::MakeArrayView(encoded, encoded_len)); | 98 rtc::MsanCheckInitialized(rtc::MakeArrayView(encoded, encoded_len)); |
27 int duration = PacketDuration(encoded, encoded_len); | 99 int duration = PacketDuration(encoded, encoded_len); |
28 if (duration >= 0 && | 100 if (duration >= 0 && |
29 duration * Channels() * sizeof(int16_t) > max_decoded_bytes) { | 101 duration * Channels() * sizeof(int16_t) > max_decoded_bytes) { |
30 return -1; | 102 return -1; |
31 } | 103 } |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 return kSpeech; | 165 return kSpeech; |
94 case 2: | 166 case 2: |
95 return kComfortNoise; | 167 return kComfortNoise; |
96 default: | 168 default: |
97 assert(false); | 169 assert(false); |
98 return kSpeech; | 170 return kSpeech; |
99 } | 171 } |
100 } | 172 } |
101 | 173 |
102 } // namespace webrtc | 174 } // namespace webrtc |
OLD | NEW |