OLD | NEW |
---|---|
(Empty) | |
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 "webrtc/modules/audio_coding/codecs/legacy_encoded_audio_frame.h" | |
12 | |
13 #include <algorithm> | |
14 #include <memory> | |
15 #include <utility> | |
16 | |
17 namespace webrtc { | |
18 | |
19 LegacyEncodedAudioFrame::LegacyEncodedAudioFrame(AudioDecoder* decoder, | |
20 rtc::Buffer* payload, | |
hlundin-webrtc
2016/09/15 08:49:14
As discussed offline, and simply for the record, I
ossu
2016/09/15 08:58:11
I'd like that too. It's almost (but not completely
kwiberg-webrtc
2016/09/15 09:03:43
Pass it by value, then.
For cheap-to-move move-on
ossu
2016/09/15 09:15:13
Of course I don't _prefer_ the style guide over ea
kwiberg-webrtc
2016/09/15 09:29:53
Sorry if I sounded a bit aggressive. But it is the
hlundin-webrtc
2016/09/15 09:43:38
I tricked you into discussing the merits of the st
ossu
2016/09/15 11:56:21
So are we saying rtc::Buffer&& in LegacyEncodedAud
kwiberg-webrtc
2016/09/15 12:06:43
Yes, I'd say taking an rtc::Buffer&& argument is b
ossu
2016/09/15 14:41:05
FOR GLORY!
| |
21 bool is_primary_payload) | |
22 : decoder_(decoder), | |
23 payload_(std::move(*payload)), | |
24 is_primary_payload_(is_primary_payload) {} | |
25 | |
26 LegacyEncodedAudioFrame::~LegacyEncodedAudioFrame() = default; | |
27 | |
28 size_t LegacyEncodedAudioFrame::Duration() const { | |
29 int ret; | |
30 if (is_primary_payload_) { | |
31 ret = decoder_->PacketDuration(payload_.data(), payload_.size()); | |
32 } else { | |
33 ret = decoder_->PacketDurationRedundant(payload_.data(), payload_.size()); | |
34 } | |
35 return (ret < 0) ? 0 : static_cast<size_t>(ret); | |
36 } | |
37 | |
38 rtc::Optional<AudioDecoder::EncodedAudioFrame::DecodeResult> | |
39 LegacyEncodedAudioFrame::Decode(rtc::ArrayView<int16_t> decoded) const { | |
40 AudioDecoder::SpeechType speech_type = AudioDecoder::kSpeech; | |
41 int ret; | |
42 if (is_primary_payload_) { | |
43 ret = decoder_->Decode( | |
44 payload_.data(), payload_.size(), decoder_->SampleRateHz(), | |
45 decoded.size() * sizeof(int16_t), decoded.data(), &speech_type); | |
46 } else { | |
47 ret = decoder_->DecodeRedundant( | |
48 payload_.data(), payload_.size(), decoder_->SampleRateHz(), | |
49 decoded.size() * sizeof(int16_t), decoded.data(), &speech_type); | |
50 } | |
51 | |
52 if (ret < 0) | |
53 return rtc::Optional<DecodeResult>(); | |
54 | |
55 return rtc::Optional<DecodeResult>({static_cast<size_t>(ret), speech_type}); | |
56 } | |
57 | |
58 std::vector<AudioDecoder::ParseResult> LegacyEncodedAudioFrame::SplitBySamples( | |
59 AudioDecoder* decoder, | |
60 rtc::Buffer* payload, | |
61 uint32_t timestamp, | |
62 bool is_primary, | |
63 size_t bytes_per_ms, | |
64 uint32_t timestamps_per_ms) { | |
65 RTC_DCHECK(payload->data()); | |
66 std::vector<AudioDecoder::ParseResult> results; | |
67 size_t split_size_bytes = payload->size(); | |
68 | |
69 // Find a "chunk size" >= 20 ms and < 40 ms. | |
70 const size_t min_chunk_size = bytes_per_ms * 20; | |
71 if (min_chunk_size >= payload->size()) { | |
72 std::unique_ptr<LegacyEncodedAudioFrame> frame( | |
73 new LegacyEncodedAudioFrame(decoder, payload, is_primary)); | |
74 results.emplace_back(timestamp, is_primary, std::move(frame)); | |
75 } else { | |
76 // Reduce the split size by half as long as |split_size_bytes| is at least | |
77 // twice the minimum chunk size (so that the resulting size is at least as | |
78 // large as the minimum chunk size). | |
79 while (split_size_bytes >= 2 * min_chunk_size) { | |
80 split_size_bytes >>= 1; | |
hlundin-webrtc
2016/09/15 08:49:14
We could get rid of this very questionable "optimi
ossu
2016/09/15 08:58:11
Acknowledged.
kwiberg-webrtc
2016/09/15 13:01:25
+1. split_size_bytes is unsigned, so the compiler
| |
81 } | |
82 | |
83 const uint32_t timestamps_per_chunk = static_cast<uint32_t>( | |
84 split_size_bytes * timestamps_per_ms / bytes_per_ms); | |
85 for (size_t byte_offset = 0, timestamp_offset = 0; | |
86 byte_offset < payload->size(); | |
87 byte_offset += split_size_bytes, | |
88 timestamp_offset += timestamps_per_chunk) { | |
89 split_size_bytes = | |
90 std::min(split_size_bytes, payload->size() - byte_offset); | |
91 rtc::Buffer new_payload(payload->data() + byte_offset, split_size_bytes); | |
92 std::unique_ptr<LegacyEncodedAudioFrame> frame( | |
93 new LegacyEncodedAudioFrame(decoder, &new_payload, is_primary)); | |
94 results.emplace_back(timestamp + timestamp_offset, is_primary, | |
95 std::move(frame)); | |
96 } | |
97 } | |
98 | |
99 return results; | |
100 } | |
101 | |
102 } // namespace webrtc | |
OLD | NEW |