• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2016 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 #include "modules/audio_coding/codecs/legacy_encoded_audio_frame.h"
12 
13 #include <algorithm>
14 #include <memory>
15 #include <utility>
16 
17 #include "rtc_base/checks.h"
18 
19 namespace webrtc {
20 
LegacyEncodedAudioFrame(AudioDecoder * decoder,rtc::Buffer && payload)21 LegacyEncodedAudioFrame::LegacyEncodedAudioFrame(AudioDecoder* decoder,
22                                                  rtc::Buffer&& payload)
23     : decoder_(decoder), payload_(std::move(payload)) {}
24 
25 LegacyEncodedAudioFrame::~LegacyEncodedAudioFrame() = default;
26 
Duration() const27 size_t LegacyEncodedAudioFrame::Duration() const {
28   const int ret = decoder_->PacketDuration(payload_.data(), payload_.size());
29   return (ret < 0) ? 0 : static_cast<size_t>(ret);
30 }
31 
32 absl::optional<AudioDecoder::EncodedAudioFrame::DecodeResult>
Decode(rtc::ArrayView<int16_t> decoded) const33 LegacyEncodedAudioFrame::Decode(rtc::ArrayView<int16_t> decoded) const {
34   AudioDecoder::SpeechType speech_type = AudioDecoder::kSpeech;
35   const int ret = decoder_->Decode(
36       payload_.data(), payload_.size(), decoder_->SampleRateHz(),
37       decoded.size() * sizeof(int16_t), decoded.data(), &speech_type);
38 
39   if (ret < 0)
40     return absl::nullopt;
41 
42   return DecodeResult{static_cast<size_t>(ret), speech_type};
43 }
44 
SplitBySamples(AudioDecoder * decoder,rtc::Buffer && payload,uint32_t timestamp,size_t bytes_per_ms,uint32_t timestamps_per_ms)45 std::vector<AudioDecoder::ParseResult> LegacyEncodedAudioFrame::SplitBySamples(
46     AudioDecoder* decoder,
47     rtc::Buffer&& payload,
48     uint32_t timestamp,
49     size_t bytes_per_ms,
50     uint32_t timestamps_per_ms) {
51   RTC_DCHECK(payload.data());
52   std::vector<AudioDecoder::ParseResult> results;
53   size_t split_size_bytes = payload.size();
54 
55   // Find a "chunk size" >= 20 ms and < 40 ms.
56   const size_t min_chunk_size = bytes_per_ms * 20;
57   if (min_chunk_size >= payload.size()) {
58     std::unique_ptr<LegacyEncodedAudioFrame> frame(
59         new LegacyEncodedAudioFrame(decoder, std::move(payload)));
60     results.emplace_back(timestamp, 0, std::move(frame));
61   } else {
62     // Reduce the split size by half as long as `split_size_bytes` is at least
63     // twice the minimum chunk size (so that the resulting size is at least as
64     // large as the minimum chunk size).
65     while (split_size_bytes >= 2 * min_chunk_size) {
66       split_size_bytes /= 2;
67     }
68 
69     const uint32_t timestamps_per_chunk = static_cast<uint32_t>(
70         split_size_bytes * timestamps_per_ms / bytes_per_ms);
71     size_t byte_offset;
72     uint32_t timestamp_offset;
73     for (byte_offset = 0, timestamp_offset = 0; byte_offset < payload.size();
74          byte_offset += split_size_bytes,
75         timestamp_offset += timestamps_per_chunk) {
76       split_size_bytes =
77           std::min(split_size_bytes, payload.size() - byte_offset);
78       rtc::Buffer new_payload(payload.data() + byte_offset, split_size_bytes);
79       std::unique_ptr<LegacyEncodedAudioFrame> frame(
80           new LegacyEncodedAudioFrame(decoder, std::move(new_payload)));
81       results.emplace_back(timestamp + timestamp_offset, 0, std::move(frame));
82     }
83   }
84 
85   return results;
86 }
87 
88 }  // namespace webrtc
89