You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

170 lines
5.6 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2012 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. #include "api/audio_codecs/audio_decoder.h"
  11. #include <assert.h>
  12. #include <memory>
  13. #include <utility>
  14. #include "api/array_view.h"
  15. #include "rtc_base/checks.h"
  16. #include "rtc_base/sanitizer.h"
  17. #include "rtc_base/trace_event.h"
  18. namespace webrtc {
  19. namespace {
  20. class OldStyleEncodedFrame final : public AudioDecoder::EncodedAudioFrame {
  21. public:
  22. OldStyleEncodedFrame(AudioDecoder* decoder, rtc::Buffer&& payload)
  23. : decoder_(decoder), payload_(std::move(payload)) {}
  24. size_t Duration() const override {
  25. const int ret = decoder_->PacketDuration(payload_.data(), payload_.size());
  26. return ret < 0 ? 0 : static_cast<size_t>(ret);
  27. }
  28. absl::optional<DecodeResult> Decode(
  29. rtc::ArrayView<int16_t> decoded) const override {
  30. auto speech_type = AudioDecoder::kSpeech;
  31. const int ret = decoder_->Decode(
  32. payload_.data(), payload_.size(), decoder_->SampleRateHz(),
  33. decoded.size() * sizeof(int16_t), decoded.data(), &speech_type);
  34. return ret < 0 ? absl::nullopt
  35. : absl::optional<DecodeResult>(
  36. {static_cast<size_t>(ret), speech_type});
  37. }
  38. private:
  39. AudioDecoder* const decoder_;
  40. const rtc::Buffer payload_;
  41. };
  42. } // namespace
  43. bool AudioDecoder::EncodedAudioFrame::IsDtxPacket() const {
  44. return false;
  45. }
  46. AudioDecoder::ParseResult::ParseResult() = default;
  47. AudioDecoder::ParseResult::ParseResult(ParseResult&& b) = default;
  48. AudioDecoder::ParseResult::ParseResult(uint32_t timestamp,
  49. int priority,
  50. std::unique_ptr<EncodedAudioFrame> frame)
  51. : timestamp(timestamp), priority(priority), frame(std::move(frame)) {
  52. RTC_DCHECK_GE(priority, 0);
  53. }
  54. AudioDecoder::ParseResult::~ParseResult() = default;
  55. AudioDecoder::ParseResult& AudioDecoder::ParseResult::operator=(
  56. ParseResult&& b) = default;
  57. std::vector<AudioDecoder::ParseResult> AudioDecoder::ParsePayload(
  58. rtc::Buffer&& payload,
  59. uint32_t timestamp) {
  60. std::vector<ParseResult> results;
  61. std::unique_ptr<EncodedAudioFrame> frame(
  62. new OldStyleEncodedFrame(this, std::move(payload)));
  63. results.emplace_back(timestamp, 0, std::move(frame));
  64. return results;
  65. }
  66. int AudioDecoder::Decode(const uint8_t* encoded,
  67. size_t encoded_len,
  68. int sample_rate_hz,
  69. size_t max_decoded_bytes,
  70. int16_t* decoded,
  71. SpeechType* speech_type) {
  72. TRACE_EVENT0("webrtc", "AudioDecoder::Decode");
  73. rtc::MsanCheckInitialized(rtc::MakeArrayView(encoded, encoded_len));
  74. int duration = PacketDuration(encoded, encoded_len);
  75. if (duration >= 0 &&
  76. duration * Channels() * sizeof(int16_t) > max_decoded_bytes) {
  77. return -1;
  78. }
  79. return DecodeInternal(encoded, encoded_len, sample_rate_hz, decoded,
  80. speech_type);
  81. }
  82. int AudioDecoder::DecodeRedundant(const uint8_t* encoded,
  83. size_t encoded_len,
  84. int sample_rate_hz,
  85. size_t max_decoded_bytes,
  86. int16_t* decoded,
  87. SpeechType* speech_type) {
  88. TRACE_EVENT0("webrtc", "AudioDecoder::DecodeRedundant");
  89. rtc::MsanCheckInitialized(rtc::MakeArrayView(encoded, encoded_len));
  90. int duration = PacketDurationRedundant(encoded, encoded_len);
  91. if (duration >= 0 &&
  92. duration * Channels() * sizeof(int16_t) > max_decoded_bytes) {
  93. return -1;
  94. }
  95. return DecodeRedundantInternal(encoded, encoded_len, sample_rate_hz, decoded,
  96. speech_type);
  97. }
  98. int AudioDecoder::DecodeRedundantInternal(const uint8_t* encoded,
  99. size_t encoded_len,
  100. int sample_rate_hz,
  101. int16_t* decoded,
  102. SpeechType* speech_type) {
  103. return DecodeInternal(encoded, encoded_len, sample_rate_hz, decoded,
  104. speech_type);
  105. }
  106. bool AudioDecoder::HasDecodePlc() const {
  107. return false;
  108. }
  109. size_t AudioDecoder::DecodePlc(size_t num_frames, int16_t* decoded) {
  110. return 0;
  111. }
  112. // TODO(bugs.webrtc.org/9676): Remove default implementation.
  113. void AudioDecoder::GeneratePlc(size_t /*requested_samples_per_channel*/,
  114. rtc::BufferT<int16_t>* /*concealment_audio*/) {}
  115. int AudioDecoder::ErrorCode() {
  116. return 0;
  117. }
  118. int AudioDecoder::PacketDuration(const uint8_t* encoded,
  119. size_t encoded_len) const {
  120. return kNotImplemented;
  121. }
  122. int AudioDecoder::PacketDurationRedundant(const uint8_t* encoded,
  123. size_t encoded_len) const {
  124. return kNotImplemented;
  125. }
  126. bool AudioDecoder::PacketHasFec(const uint8_t* encoded,
  127. size_t encoded_len) const {
  128. return false;
  129. }
  130. AudioDecoder::SpeechType AudioDecoder::ConvertSpeechType(int16_t type) {
  131. switch (type) {
  132. case 0: // TODO(hlundin): Both iSAC and Opus return 0 for speech.
  133. case 1:
  134. return kSpeech;
  135. case 2:
  136. return kComfortNoise;
  137. default:
  138. assert(false);
  139. return kSpeech;
  140. }
  141. }
  142. } // namespace webrtc