OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright (c) 2017 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_mixer/frame_combiner.h" | |
12 | |
13 #include <algorithm> | |
14 #include <array> | |
15 #include <functional> | |
16 #include <memory> | |
17 | |
18 #include "webrtc/audio/utility/audio_frame_operations.h" | |
19 #include "webrtc/base/logging.h" | |
20 #include "webrtc/modules/audio_mixer/audio_frame_manipulator.h" | |
21 #include "webrtc/modules/audio_mixer/audio_mixer_impl.h" | |
22 | |
23 namespace webrtc { | |
24 namespace { | |
25 | |
26 // Stereo, 48 kHz, 10 ms. | |
27 constexpr int kMaximalFrameSize = 2 * 48 * 10; | |
28 | |
29 void CombineZeroFrames(AudioFrame* audio_frame_for_mixing) { | |
30 audio_frame_for_mixing->elapsed_time_ms_ = -1; | |
31 AudioFrameOperations::Mute(audio_frame_for_mixing); | |
32 } | |
33 | |
34 void CombineOneFrame(const AudioFrame* input_frame, | |
35 AudioFrame* audio_frame_for_mixing) { | |
36 audio_frame_for_mixing->timestamp_ = input_frame->timestamp_; | |
37 audio_frame_for_mixing->elapsed_time_ms_ = input_frame->elapsed_time_ms_; | |
38 std::copy(input_frame->data_, | |
39 input_frame->data_ + | |
40 input_frame->num_channels_ * input_frame->samples_per_channel_, | |
41 audio_frame_for_mixing->data_); | |
42 } | |
43 | |
44 std::unique_ptr<AudioProcessing> CreateLimiter() { | |
45 Config config; | |
46 config.Set<ExperimentalAgc>(new ExperimentalAgc(false)); | |
47 std::unique_ptr<AudioProcessing> limiter(AudioProcessing::Create(config)); | |
48 if (!limiter.get()) { | |
hlundin-webrtc
2017/02/17 11:04:13
Under what conditions can this happen? Wouldn't th
aleloi
2017/02/20 10:41:33
Done.
| |
49 return nullptr; | |
50 } | |
51 | |
52 if (limiter->gain_control()->set_mode(GainControl::kFixedDigital) != | |
hlundin-webrtc
2017/02/17 11:04:13
Same here.
aleloi
2017/02/20 10:41:33
Done.
| |
53 limiter->kNoError) { | |
54 return nullptr; | |
55 } | |
56 | |
57 // We smoothly limit the mixed frame to -7 dbFS. -6 would correspond to the | |
58 // divide-by-2 but -7 is used instead to give a bit of headroom since the | |
59 // AGC is not a hard limiter. | |
60 if (limiter->gain_control()->set_target_level_dbfs(7) != limiter->kNoError) { | |
hlundin-webrtc
2017/02/17 11:04:13
Same here.
aleloi
2017/02/20 10:41:33
Done.
| |
61 return nullptr; | |
62 } | |
63 | |
64 if (limiter->gain_control()->set_compression_gain_db(0) != | |
hlundin-webrtc
2017/02/17 11:04:13
Same here.
aleloi
2017/02/20 10:41:33
Done.
| |
65 limiter->kNoError) { | |
66 return nullptr; | |
67 } | |
68 | |
69 if (limiter->gain_control()->enable_limiter(true) != limiter->kNoError) { | |
hlundin-webrtc
2017/02/17 11:04:13
Same here.
aleloi
2017/02/20 10:41:33
Done.
| |
70 return nullptr; | |
71 } | |
72 | |
73 if (limiter->gain_control()->Enable(true) != limiter->kNoError) { | |
hlundin-webrtc
2017/02/17 11:04:12
Same here.
aleloi
2017/02/20 10:41:33
Done.
| |
74 return nullptr; | |
75 } | |
76 return limiter; | |
77 } | |
78 } // namespace | |
79 | |
80 FrameCombiner::FrameCombiner(bool use_apm_limiter) | |
81 : use_apm_limiter_(use_apm_limiter), | |
82 limiter_(use_apm_limiter ? CreateLimiter() : nullptr) {} | |
83 | |
84 FrameCombiner::~FrameCombiner() = default; | |
85 | |
86 void FrameCombiner::Combine(const std::vector<AudioFrame*>& mix_list, | |
87 size_t number_of_channels, | |
88 int sample_rate, | |
89 AudioFrame* audio_frame_for_mixing) const { | |
hlundin-webrtc
2017/02/17 11:04:13
RTC_DCHECK(audio_frame_for_mixing);
aleloi
2017/02/20 10:41:33
Done.
| |
90 const size_t kSamplesPerChannel = static_cast<size_t>( | |
hlundin-webrtc
2017/02/17 11:04:12
I don't think it is correct to use kCamelCase nami
aleloi
2017/02/20 10:41:33
Good find! Fixed.
| |
91 (sample_rate * webrtc::AudioMixerImpl::kFrameDurationInMs) / 1000); | |
92 | |
93 for (const auto* frame : mix_list) { | |
94 RTC_DCHECK_EQ(kSamplesPerChannel, frame->samples_per_channel_); | |
hlundin-webrtc
2017/02/17 11:04:13
A for loop only for DCHECKing? Why don't you perfo
aleloi
2017/02/20 10:41:33
It's two against one now!
I wanted to separate ch
hlundin-webrtc
2017/02/20 14:16:31
Alright. I'll allow it.
| |
95 RTC_DCHECK_EQ(sample_rate, frame->sample_rate_hz_); | |
96 } | |
97 | |
98 // Frames could be both stereo and mono. | |
99 for (auto* frame : mix_list) { | |
100 RemixFrame(number_of_channels, frame); | |
101 } | |
102 | |
103 // TODO(aleloi): Issue bugs.webrtc.org/3390. | |
104 // Audio frame timestamp. The 'timestamp_' field is set to dummy | |
105 // value '0', because it is only supported in the one channel case and | |
106 // is then updated in the helper functions. | |
107 audio_frame_for_mixing->UpdateFrame( | |
108 -1, 0, nullptr, kSamplesPerChannel, sample_rate, AudioFrame::kUndefined, | |
109 AudioFrame::kVadUnknown, number_of_channels); | |
110 | |
111 if (mix_list.size() == 0) { | |
112 CombineZeroFrames(audio_frame_for_mixing); | |
113 } else if (mix_list.size() == 1) { | |
114 CombineOneFrame(mix_list.front(), audio_frame_for_mixing); | |
115 } else { | |
116 std::vector<rtc::ArrayView<const int16_t>> input_frames; | |
117 for (size_t i = 0; i < mix_list.size(); ++i) { | |
118 input_frames.push_back(rtc::ArrayView<const int16_t>( | |
119 mix_list[i]->data_, kSamplesPerChannel * number_of_channels)); | |
120 } | |
121 CombineMultipleFrames(input_frames, audio_frame_for_mixing); | |
122 } | |
123 } | |
124 | |
125 void FrameCombiner::CombineMultipleFrames( | |
126 const std::vector<rtc::ArrayView<const int16_t>>& input_frames, | |
127 AudioFrame* audio_frame_for_mixing) const { | |
128 RTC_DCHECK(!input_frames.empty()); | |
hlundin-webrtc
2017/02/17 11:04:13
RTC_DCHECK(audio_frame_for_mixing);
aleloi
2017/02/20 10:41:33
Done.
| |
129 | |
130 const size_t frame_length = input_frames.front().size(); | |
131 for (const auto& frame : input_frames) { | |
hlundin-webrtc
2017/02/17 11:04:13
Again, incorporate this DCHECK into another loop.
aleloi
2017/02/20 10:41:33
I think it's more readable as is. Do you still wan
hlundin-webrtc
2017/02/20 14:16:31
Leave it as it is.
| |
132 RTC_DCHECK_EQ(frame_length, frame.size()); | |
133 } | |
134 | |
135 // Algorithm: int16 frames are added to a sufficiently large | |
136 // statically allocated int32 buffer. For > 2 participants this is | |
137 // more efficient than addition in place in the int16 audio | |
138 // frame. The audio quality loss due to halving the samples is | |
139 // smaller than 16-bit addition in place. | |
140 RTC_DCHECK_GE(kMaximalFrameSize, frame_length); | |
141 std::array<int32_t, kMaximalFrameSize> add_buffer; | |
142 | |
143 add_buffer.fill(0); | |
144 | |
145 for (const auto& frame : input_frames) { | |
146 std::transform(frame.begin(), frame.end(), add_buffer.begin(), | |
147 add_buffer.begin(), std::plus<int32_t>()); | |
148 } | |
149 | |
150 if (use_apm_limiter_) { | |
151 // Halve all samples to avoid saturation before limiting. | |
152 std::transform(add_buffer.begin(), add_buffer.begin() + frame_length, | |
153 audio_frame_for_mixing->data_, [](int32_t a) { | |
154 return rtc::saturated_cast<int16_t>(a / 2); | |
155 }); | |
156 | |
157 // Smoothly limit the audio. | |
158 RTC_DCHECK(limiter_); | |
hlundin-webrtc
2017/02/17 11:04:13
With your current implementation of CreateLimiter(
aleloi
2017/02/20 10:41:33
Acknowledged.
| |
159 const int error = limiter_->ProcessStream(audio_frame_for_mixing); | |
160 if (error != limiter_->kNoError) { | |
161 LOG_F(LS_ERROR) << "Error from AudioProcessing: " << error; | |
162 RTC_NOTREACHED(); | |
163 } | |
164 | |
165 // And now we can safely restore the level. This procedure results in | |
166 // some loss of resolution, deemed acceptable. | |
167 // | |
168 // It's possible to apply the gain in the AGC (with a target level of 0 dbFS | |
169 // and compression gain of 6 dB). However, in the transition frame when this | |
170 // is enabled (moving from one to two audio sources) it has the potential to | |
171 // create discontinuities in the mixed frame. | |
172 // | |
173 // Instead we double the frame (with addition since left-shifting a | |
174 // negative value is undefined). | |
175 AudioFrameOperations::Add(*audio_frame_for_mixing, audio_frame_for_mixing); | |
176 } else { | |
177 std::transform(add_buffer.begin(), add_buffer.begin() + frame_length, | |
178 audio_frame_for_mixing->data_, | |
179 [](int32_t a) { return rtc::saturated_cast<int16_t>(a); }); | |
180 } | |
181 } | |
182 } // namespace webrtc | |
OLD | NEW |