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
11#include "webrtc/modules/audio_coding/include/audio_coding_module.h"
12
13#include "webrtc/base/checks.h"
14#include "webrtc/common_types.h"
15#include "webrtc/modules/audio_coding/acm2/audio_coding_module_impl.h"
16#include "webrtc/modules/audio_coding/acm2/rent_a_codec.h"
17#include "webrtc/system_wrappers/include/clock.h"
18#include "webrtc/system_wrappers/include/trace.h"
19
20namespace webrtc {
21
22// Create module
23AudioCodingModule* AudioCodingModule::Create(int id) {
24  Config config;
25  config.id = id;
26  config.clock = Clock::GetRealTimeClock();
27  return Create(config);
28}
29
30AudioCodingModule* AudioCodingModule::Create(int id, Clock* clock) {
31  Config config;
32  config.id = id;
33  config.clock = clock;
34  return Create(config);
35}
36
37AudioCodingModule* AudioCodingModule::Create(const Config& config) {
38  return new acm2::AudioCodingModuleImpl(config);
39}
40
41int AudioCodingModule::NumberOfCodecs() {
42  return static_cast<int>(acm2::RentACodec::NumberOfCodecs());
43}
44
45int AudioCodingModule::Codec(int list_id, CodecInst* codec) {
46  auto codec_id = acm2::RentACodec::CodecIdFromIndex(list_id);
47  if (!codec_id)
48    return -1;
49  auto ci = acm2::RentACodec::CodecInstById(*codec_id);
50  if (!ci)
51    return -1;
52  *codec = *ci;
53  return 0;
54}
55
56int AudioCodingModule::Codec(const char* payload_name,
57                             CodecInst* codec,
58                             int sampling_freq_hz,
59                             size_t channels) {
60  rtc::Optional<CodecInst> ci = acm2::RentACodec::CodecInstByParams(
61      payload_name, sampling_freq_hz, channels);
62  if (ci) {
63    *codec = *ci;
64    return 0;
65  } else {
66    // We couldn't find a matching codec, so set the parameters to unacceptable
67    // values and return.
68    codec->plname[0] = '\0';
69    codec->pltype = -1;
70    codec->pacsize = 0;
71    codec->rate = 0;
72    codec->plfreq = 0;
73    return -1;
74  }
75}
76
77int AudioCodingModule::Codec(const char* payload_name,
78                             int sampling_freq_hz,
79                             size_t channels) {
80  rtc::Optional<acm2::RentACodec::CodecId> ci =
81      acm2::RentACodec::CodecIdByParams(payload_name, sampling_freq_hz,
82                                        channels);
83  if (!ci)
84    return -1;
85  rtc::Optional<int> i = acm2::RentACodec::CodecIndexFromId(*ci);
86  return i ? *i : -1;
87}
88
89// Checks the validity of the parameters of the given codec
90bool AudioCodingModule::IsCodecValid(const CodecInst& codec) {
91  bool valid = acm2::RentACodec::IsCodecValid(codec);
92  if (!valid)
93    WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, -1,
94                 "Invalid codec setting");
95  return valid;
96}
97
98}  // namespace webrtc
99