1 /*
2 * Copyright (c) 2019 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 #ifndef MODULES_AUDIO_CODING_CODECS_OPUS_AUDIO_CODER_OPUS_COMMON_H_
12 #define MODULES_AUDIO_CODING_CODECS_OPUS_AUDIO_CODER_OPUS_COMMON_H_
13
14 #include <string>
15 #include <utility>
16 #include <vector>
17
18 #include "absl/types/optional.h"
19 #include "api/audio_codecs/audio_decoder.h"
20 #include "api/audio_codecs/audio_format.h"
21 #include "rtc_base/string_to_number.h"
22
23 namespace webrtc {
24
25 absl::optional<std::string> GetFormatParameter(const SdpAudioFormat& format,
26 const std::string& param);
27
28 template <typename T>
GetFormatParameter(const SdpAudioFormat & format,const std::string & param)29 absl::optional<T> GetFormatParameter(const SdpAudioFormat& format,
30 const std::string& param) {
31 return rtc::StringToNumber<T>(GetFormatParameter(format, param).value_or(""));
32 }
33
34 template <>
35 absl::optional<std::vector<unsigned char>> GetFormatParameter(
36 const SdpAudioFormat& format,
37 const std::string& param);
38
39 class OpusFrame : public AudioDecoder::EncodedAudioFrame {
40 public:
OpusFrame(AudioDecoder * decoder,rtc::Buffer && payload,bool is_primary_payload)41 OpusFrame(AudioDecoder* decoder,
42 rtc::Buffer&& payload,
43 bool is_primary_payload)
44 : decoder_(decoder),
45 payload_(std::move(payload)),
46 is_primary_payload_(is_primary_payload) {}
47
Duration()48 size_t Duration() const override {
49 int ret;
50 if (is_primary_payload_) {
51 ret = decoder_->PacketDuration(payload_.data(), payload_.size());
52 } else {
53 ret = decoder_->PacketDurationRedundant(payload_.data(), payload_.size());
54 }
55 return (ret < 0) ? 0 : static_cast<size_t>(ret);
56 }
57
IsDtxPacket()58 bool IsDtxPacket() const override { return payload_.size() <= 2; }
59
Decode(rtc::ArrayView<int16_t> decoded)60 absl::optional<DecodeResult> Decode(
61 rtc::ArrayView<int16_t> decoded) const override {
62 AudioDecoder::SpeechType speech_type = AudioDecoder::kSpeech;
63 int ret;
64 if (is_primary_payload_) {
65 ret = decoder_->Decode(
66 payload_.data(), payload_.size(), decoder_->SampleRateHz(),
67 decoded.size() * sizeof(int16_t), decoded.data(), &speech_type);
68 } else {
69 ret = decoder_->DecodeRedundant(
70 payload_.data(), payload_.size(), decoder_->SampleRateHz(),
71 decoded.size() * sizeof(int16_t), decoded.data(), &speech_type);
72 }
73
74 if (ret < 0)
75 return absl::nullopt;
76
77 return DecodeResult{static_cast<size_t>(ret), speech_type};
78 }
79
80 private:
81 AudioDecoder* const decoder_;
82 const rtc::Buffer payload_;
83 const bool is_primary_payload_;
84 };
85
86 } // namespace webrtc
87
88 #endif // MODULES_AUDIO_CODING_CODECS_OPUS_AUDIO_CODER_OPUS_COMMON_H_
89