OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2015 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/opus/audio_decoder_opus.h" | 11 #include "webrtc/modules/audio_coding/codecs/opus/audio_decoder_opus.h" |
12 | 12 |
| 13 #include <utility> |
| 14 |
13 #include "webrtc/base/checks.h" | 15 #include "webrtc/base/checks.h" |
14 | 16 |
15 namespace webrtc { | 17 namespace webrtc { |
16 | 18 |
| 19 namespace { |
| 20 class OpusFrame : public AudioDecoder::EncodedAudioFrame { |
| 21 public: |
| 22 OpusFrame(AudioDecoderOpus* decoder, |
| 23 rtc::Buffer&& payload, |
| 24 bool is_primary_payload) |
| 25 : decoder_(decoder), |
| 26 payload_(std::move(payload)), |
| 27 is_primary_payload_(is_primary_payload) {} |
| 28 |
| 29 size_t Duration() const override { |
| 30 int ret; |
| 31 if (is_primary_payload_) { |
| 32 ret = decoder_->PacketDuration(payload_.data(), payload_.size()); |
| 33 } else { |
| 34 ret = decoder_->PacketDurationRedundant(payload_.data(), payload_.size()); |
| 35 } |
| 36 return (ret < 0) ? 0 : static_cast<size_t>(ret); |
| 37 } |
| 38 |
| 39 rtc::Optional<DecodeResult> Decode( |
| 40 rtc::ArrayView<int16_t> decoded) const override { |
| 41 AudioDecoder::SpeechType speech_type = AudioDecoder::kSpeech; |
| 42 int ret; |
| 43 if (is_primary_payload_) { |
| 44 ret = decoder_->Decode( |
| 45 payload_.data(), payload_.size(), decoder_->SampleRateHz(), |
| 46 decoded.size() * sizeof(int16_t), decoded.data(), &speech_type); |
| 47 } else { |
| 48 ret = decoder_->DecodeRedundant( |
| 49 payload_.data(), payload_.size(), decoder_->SampleRateHz(), |
| 50 decoded.size() * sizeof(int16_t), decoded.data(), &speech_type); |
| 51 } |
| 52 |
| 53 if (ret < 0) |
| 54 return rtc::Optional<DecodeResult>(); |
| 55 |
| 56 return rtc::Optional<DecodeResult>({static_cast<size_t>(ret), speech_type}); |
| 57 } |
| 58 |
| 59 private: |
| 60 AudioDecoderOpus* const decoder_; |
| 61 const rtc::Buffer payload_; |
| 62 const bool is_primary_payload_; |
| 63 }; |
| 64 |
| 65 } // namespace |
| 66 |
17 AudioDecoderOpus::AudioDecoderOpus(size_t num_channels) | 67 AudioDecoderOpus::AudioDecoderOpus(size_t num_channels) |
18 : channels_(num_channels) { | 68 : channels_(num_channels) { |
19 RTC_DCHECK(num_channels == 1 || num_channels == 2); | 69 RTC_DCHECK(num_channels == 1 || num_channels == 2); |
20 WebRtcOpus_DecoderCreate(&dec_state_, channels_); | 70 WebRtcOpus_DecoderCreate(&dec_state_, channels_); |
21 WebRtcOpus_DecoderInit(dec_state_); | 71 WebRtcOpus_DecoderInit(dec_state_); |
22 } | 72 } |
23 | 73 |
24 AudioDecoderOpus::~AudioDecoderOpus() { | 74 AudioDecoderOpus::~AudioDecoderOpus() { |
25 WebRtcOpus_DecoderFree(dec_state_); | 75 WebRtcOpus_DecoderFree(dec_state_); |
26 } | 76 } |
27 | 77 |
| 78 std::vector<AudioDecoder::ParseResult> AudioDecoderOpus::ParsePayload( |
| 79 rtc::Buffer&& payload, |
| 80 uint32_t timestamp) { |
| 81 std::vector<ParseResult> results; |
| 82 |
| 83 if (PacketHasFec(payload.data(), payload.size())) { |
| 84 const int duration = |
| 85 PacketDurationRedundant(payload.data(), payload.size()); |
| 86 RTC_DCHECK_GE(duration, 0); |
| 87 rtc::Buffer payload_copy(payload.data(), payload.size()); |
| 88 std::unique_ptr<EncodedAudioFrame> fec_frame( |
| 89 new OpusFrame(this, std::move(payload_copy), false)); |
| 90 results.emplace_back(timestamp - duration, 1, std::move(fec_frame)); |
| 91 } |
| 92 std::unique_ptr<EncodedAudioFrame> frame( |
| 93 new OpusFrame(this, std::move(payload), true)); |
| 94 results.emplace_back(timestamp, 0, std::move(frame)); |
| 95 return results; |
| 96 } |
| 97 |
28 int AudioDecoderOpus::DecodeInternal(const uint8_t* encoded, | 98 int AudioDecoderOpus::DecodeInternal(const uint8_t* encoded, |
29 size_t encoded_len, | 99 size_t encoded_len, |
30 int sample_rate_hz, | 100 int sample_rate_hz, |
31 int16_t* decoded, | 101 int16_t* decoded, |
32 SpeechType* speech_type) { | 102 SpeechType* speech_type) { |
33 RTC_DCHECK_EQ(sample_rate_hz, 48000); | 103 RTC_DCHECK_EQ(sample_rate_hz, 48000); |
34 int16_t temp_type = 1; // Default is speech. | 104 int16_t temp_type = 1; // Default is speech. |
35 int ret = | 105 int ret = |
36 WebRtcOpus_Decode(dec_state_, encoded, encoded_len, decoded, &temp_type); | 106 WebRtcOpus_Decode(dec_state_, encoded, encoded_len, decoded, &temp_type); |
37 if (ret > 0) | 107 if (ret > 0) |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 | 159 |
90 int AudioDecoderOpus::SampleRateHz() const { | 160 int AudioDecoderOpus::SampleRateHz() const { |
91 return 48000; | 161 return 48000; |
92 } | 162 } |
93 | 163 |
94 size_t AudioDecoderOpus::Channels() const { | 164 size_t AudioDecoderOpus::Channels() const { |
95 return channels_; | 165 return channels_; |
96 } | 166 } |
97 | 167 |
98 } // namespace webrtc | 168 } // namespace webrtc |
OLD | NEW |