xref: /aosp_15_r20/external/webrtc/modules/audio_mixer/default_output_rate_calculator.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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_mixer/default_output_rate_calculator.h"
12 
13 #include <algorithm>
14 #include <iterator>
15 
16 #include "modules/audio_processing/include/audio_processing.h"
17 #include "rtc_base/checks.h"
18 
19 namespace webrtc {
20 
CalculateOutputRateFromRange(rtc::ArrayView<const int> preferred_sample_rates)21 int DefaultOutputRateCalculator::CalculateOutputRateFromRange(
22     rtc::ArrayView<const int> preferred_sample_rates) {
23   if (preferred_sample_rates.empty()) {
24     return DefaultOutputRateCalculator::kDefaultFrequency;
25   }
26   using NativeRate = AudioProcessing::NativeRate;
27   const int maximal_frequency = *std::max_element(
28       preferred_sample_rates.cbegin(), preferred_sample_rates.cend());
29 
30   RTC_DCHECK_LE(NativeRate::kSampleRate8kHz, maximal_frequency);
31   RTC_DCHECK_GE(NativeRate::kSampleRate48kHz, maximal_frequency);
32 
33   static constexpr NativeRate native_rates[] = {
34       NativeRate::kSampleRate8kHz, NativeRate::kSampleRate16kHz,
35       NativeRate::kSampleRate32kHz, NativeRate::kSampleRate48kHz};
36   const auto* rounded_up_index = std::lower_bound(
37       std::begin(native_rates), std::end(native_rates), maximal_frequency);
38   RTC_DCHECK(rounded_up_index != std::end(native_rates));
39   return *rounded_up_index;
40 }
41 }  // namespace webrtc
42