blob: d628fc5e772bdfbf6617b25cdafe2bf1766ce44c [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
mflodman@webrtc.orgc80d9d92012-02-06 10:11:25 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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
pbos@webrtc.org956aa7e2013-05-21 13:52:32 +000011#include "webrtc/voice_engine/voe_codec_impl.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
pbos@webrtc.org956aa7e2013-05-21 13:52:32 +000013#include "webrtc/modules/audio_coding/main/interface/audio_coding_module.h"
14#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
15#include "webrtc/system_wrappers/interface/trace.h"
16#include "webrtc/voice_engine/channel.h"
17#include "webrtc/voice_engine/include/voe_errors.h"
18#include "webrtc/voice_engine/voice_engine_impl.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000019
20namespace webrtc
21{
22
23VoECodec* VoECodec::GetInterface(VoiceEngine* voiceEngine)
24{
25#ifndef WEBRTC_VOICE_ENGINE_CODEC_API
26 return NULL;
27#else
28 if (NULL == voiceEngine)
29 {
30 return NULL;
31 }
tommi@webrtc.org0989fb72013-02-15 15:07:32 +000032 VoiceEngineImpl* s = static_cast<VoiceEngineImpl*>(voiceEngine);
tommi@webrtc.orga990e122012-04-26 15:28:22 +000033 s->AddRef();
34 return s;
niklase@google.com470e71d2011-07-07 08:21:25 +000035#endif
36}
37
38#ifdef WEBRTC_VOICE_ENGINE_CODEC_API
39
tommi@webrtc.org851becd2012-04-04 14:57:19 +000040VoECodecImpl::VoECodecImpl(voe::SharedData* shared) : _shared(shared)
niklase@google.com470e71d2011-07-07 08:21:25 +000041{
tommi@webrtc.org851becd2012-04-04 14:57:19 +000042 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +000043 "VoECodecImpl() - ctor");
44}
45
46VoECodecImpl::~VoECodecImpl()
47{
tommi@webrtc.org851becd2012-04-04 14:57:19 +000048 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +000049 "~VoECodecImpl() - dtor");
50}
51
niklase@google.com470e71d2011-07-07 08:21:25 +000052int VoECodecImpl::NumOfCodecs()
53{
tommi@webrtc.org851becd2012-04-04 14:57:19 +000054 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +000055 "NumOfCodecs()");
56
57 // Number of supported codecs in the ACM
pbos@webrtc.org6141e132013-04-09 10:09:10 +000058 uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +000059
tommi@webrtc.org851becd2012-04-04 14:57:19 +000060 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
61 VoEId(_shared->instance_id(), -1),
62 "NumOfCodecs() => %u", nSupportedCodecs);
niklase@google.com470e71d2011-07-07 08:21:25 +000063 return (nSupportedCodecs);
64}
65
66int VoECodecImpl::GetCodec(int index, CodecInst& codec)
67{
tommi@webrtc.org851becd2012-04-04 14:57:19 +000068 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +000069 "GetCodec(index=%d, codec=?)", index);
70 CodecInst acmCodec;
tina.legrand@webrtc.org7a7a0082013-02-21 10:27:48 +000071 if (AudioCodingModule::Codec(index, &acmCodec)
niklase@google.com470e71d2011-07-07 08:21:25 +000072 == -1)
73 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +000074 _shared->SetLastError(VE_INVALID_LISTNR, kTraceError,
75 "GetCodec() invalid index");
niklase@google.com470e71d2011-07-07 08:21:25 +000076 return -1;
77 }
78 ACMToExternalCodecRepresentation(codec, acmCodec);
tommi@webrtc.org851becd2012-04-04 14:57:19 +000079 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
80 VoEId(_shared->instance_id(), -1),
81 "GetCodec() => plname=%s, pacsize=%d, plfreq=%d, pltype=%d, "
82 "channels=%d, rate=%d", codec.plname, codec.pacsize,
83 codec.plfreq, codec.pltype, codec.channels, codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +000084 return 0;
85}
86
87int VoECodecImpl::SetSendCodec(int channel, const CodecInst& codec)
88{
89 CodecInst copyCodec;
90 ExternalToACMCodecRepresentation(copyCodec, codec);
91
tommi@webrtc.org851becd2012-04-04 14:57:19 +000092 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +000093 "SetSendCodec(channel=%d, codec)", channel);
tommi@webrtc.org851becd2012-04-04 14:57:19 +000094 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +000095 "codec: plname=%s, pacsize=%d, plfreq=%d, pltype=%d, "
96 "channels=%d, rate=%d", codec.plname, codec.pacsize,
97 codec.plfreq, codec.pltype, codec.channels, codec.rate);
tommi@webrtc.org851becd2012-04-04 14:57:19 +000098 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +000099 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000100 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +0000101 return -1;
102 }
103 // External sanity checks performed outside the ACM
104 if ((STR_CASE_CMP(copyCodec.plname, "L16") == 0) &&
105 (copyCodec.pacsize >= 960))
106 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000107 _shared->SetLastError(VE_INVALID_ARGUMENT, kTraceError,
108 "SetSendCodec() invalid L16 packet size");
niklase@google.com470e71d2011-07-07 08:21:25 +0000109 return -1;
110 }
111 if (!STR_CASE_CMP(copyCodec.plname, "CN")
112 || !STR_CASE_CMP(copyCodec.plname, "TELEPHONE-EVENT")
113 || !STR_CASE_CMP(copyCodec.plname, "RED"))
114 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000115 _shared->SetLastError(VE_INVALID_ARGUMENT, kTraceError,
116 "SetSendCodec() invalid codec name");
niklase@google.com470e71d2011-07-07 08:21:25 +0000117 return -1;
118 }
niklas.enbom@webrtc.orge33a1022011-11-16 10:33:53 +0000119 if ((copyCodec.channels != 1) && (copyCodec.channels != 2))
niklase@google.com470e71d2011-07-07 08:21:25 +0000120 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000121 _shared->SetLastError(VE_INVALID_ARGUMENT, kTraceError,
122 "SetSendCodec() invalid number of channels");
niklase@google.com470e71d2011-07-07 08:21:25 +0000123 return -1;
124 }
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000125 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
126 voe::Channel* channelPtr = ch.channel();
niklase@google.com470e71d2011-07-07 08:21:25 +0000127 if (channelPtr == NULL)
128 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000129 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
130 "GetSendCodec() failed to locate channel");
niklase@google.com470e71d2011-07-07 08:21:25 +0000131 return -1;
132 }
133 if (!AudioCodingModule::IsCodecValid(
134 (CodecInst&) copyCodec))
135 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000136 _shared->SetLastError(VE_INVALID_ARGUMENT, kTraceError,
137 "SetSendCodec() invalid codec");
niklase@google.com470e71d2011-07-07 08:21:25 +0000138 return -1;
139 }
140 if (channelPtr->SetSendCodec(copyCodec) != 0)
141 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000142 _shared->SetLastError(VE_CANNOT_SET_SEND_CODEC, kTraceError,
143 "SetSendCodec() failed to set send codec");
niklase@google.com470e71d2011-07-07 08:21:25 +0000144 return -1;
145 }
146
niklase@google.com470e71d2011-07-07 08:21:25 +0000147 return 0;
148}
149
150int VoECodecImpl::GetSendCodec(int channel, CodecInst& codec)
151{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000152 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000153 "GetSendCodec(channel=%d, codec=?)", channel);
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000154 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +0000155 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000156 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +0000157 return -1;
158 }
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000159 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
160 voe::Channel* channelPtr = ch.channel();
niklase@google.com470e71d2011-07-07 08:21:25 +0000161 if (channelPtr == NULL)
162 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000163 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
164 "GetSendCodec() failed to locate channel");
niklase@google.com470e71d2011-07-07 08:21:25 +0000165 return -1;
166 }
167 CodecInst acmCodec;
168 if (channelPtr->GetSendCodec(acmCodec) != 0)
169 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000170 _shared->SetLastError(VE_CANNOT_GET_SEND_CODEC, kTraceError,
171 "GetSendCodec() failed to get send codec");
niklase@google.com470e71d2011-07-07 08:21:25 +0000172 return -1;
173 }
174 ACMToExternalCodecRepresentation(codec, acmCodec);
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000175 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
176 VoEId(_shared->instance_id(), -1),
177 "GetSendCodec() => plname=%s, pacsize=%d, plfreq=%d, "
178 "channels=%d, rate=%d", codec.plname, codec.pacsize,
179 codec.plfreq, codec.channels, codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +0000180 return 0;
181}
182
Ivo Creusenadf89b72015-04-29 16:03:33 +0200183int VoECodecImpl::SetBitRate(int channel, int bitrate_bps) {
184 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
185 "SetBitRate(bitrate_bps=%d)", bitrate_bps);
186 if (!_shared->statistics().Initialized()) {
187 _shared->SetLastError(VE_NOT_INITED, kTraceError);
188 return -1;
189 }
190 _shared->channel_manager().GetChannel(channel).channel()->SetBitRate(
191 bitrate_bps);
192 return 0;
193}
194
niklase@google.com470e71d2011-07-07 08:21:25 +0000195int VoECodecImpl::GetRecCodec(int channel, CodecInst& codec)
196{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000197 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000198 "GetRecCodec(channel=%d, codec=?)", channel);
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000199 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +0000200 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000201 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +0000202 return -1;
203 }
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000204 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
205 voe::Channel* channelPtr = ch.channel();
niklase@google.com470e71d2011-07-07 08:21:25 +0000206 if (channelPtr == NULL)
207 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000208 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
209 "GetRecCodec() failed to locate channel");
niklase@google.com470e71d2011-07-07 08:21:25 +0000210 return -1;
211 }
212 CodecInst acmCodec;
213 if (channelPtr->GetRecCodec(acmCodec) != 0)
214 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000215 _shared->SetLastError(VE_CANNOT_GET_REC_CODEC, kTraceError,
216 "GetRecCodec() failed to get received codec");
niklase@google.com470e71d2011-07-07 08:21:25 +0000217 return -1;
218 }
219 ACMToExternalCodecRepresentation(codec, acmCodec);
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000220 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
221 VoEId(_shared->instance_id(), -1),
222 "GetRecCodec() => plname=%s, pacsize=%d, plfreq=%d, "
223 "channels=%d, rate=%d", codec.plname, codec.pacsize,
224 codec.plfreq, codec.channels, codec.rate);
niklase@google.com470e71d2011-07-07 08:21:25 +0000225 return 0;
226}
227
niklase@google.com470e71d2011-07-07 08:21:25 +0000228int VoECodecImpl::SetRecPayloadType(int channel, const CodecInst& codec)
229{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000230 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000231 "SetRecPayloadType(channel=%d, codec)", channel);
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000232 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000233 "codec: plname=%s, plfreq=%d, pltype=%d, channels=%u, "
234 "pacsize=%d, rate=%d", codec.plname, codec.plfreq, codec.pltype,
235 codec.channels, codec.pacsize, codec.rate);
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000236 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +0000237 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000238 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +0000239 return -1;
240 }
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000241 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
242 voe::Channel* channelPtr = ch.channel();
niklase@google.com470e71d2011-07-07 08:21:25 +0000243 if (channelPtr == NULL)
244 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000245 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
246 "GetRecPayloadType() failed to locate channel");
niklase@google.com470e71d2011-07-07 08:21:25 +0000247 return -1;
248 }
249 return channelPtr->SetRecPayloadType(codec);
250}
251
252int VoECodecImpl::GetRecPayloadType(int channel, CodecInst& codec)
253{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000254 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000255 "GetRecPayloadType(channel=%d, codec)", channel);
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000256 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +0000257 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000258 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +0000259 return -1;
260 }
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000261 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
262 voe::Channel* channelPtr = ch.channel();
niklase@google.com470e71d2011-07-07 08:21:25 +0000263 if (channelPtr == NULL)
264 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000265 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
266 "GetRecPayloadType() failed to locate channel");
niklase@google.com470e71d2011-07-07 08:21:25 +0000267 return -1;
268 }
269 return channelPtr->GetRecPayloadType(codec);
270}
271
272int VoECodecImpl::SetSendCNPayloadType(int channel, int type,
273 PayloadFrequencies frequency)
274{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000275 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000276 "SetSendCNPayloadType(channel=%d, type=%d, frequency=%d)",
277 channel, type, frequency);
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000278 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +0000279 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000280 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +0000281 return -1;
282 }
283 if (type < 96 || type > 127)
284 {
285 // Only allow dynamic range: 96 to 127
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000286 _shared->SetLastError(VE_INVALID_PLTYPE, kTraceError,
287 "SetSendCNPayloadType() invalid payload type");
niklase@google.com470e71d2011-07-07 08:21:25 +0000288 return -1;
289 }
290 if ((frequency != kFreq16000Hz) && (frequency != kFreq32000Hz))
291 {
292 // It is not possible to modify the payload type for CN/8000.
293 // We only allow modification of the CN payload type for CN/16000
294 // and CN/32000.
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000295 _shared->SetLastError(VE_INVALID_PLFREQ, kTraceError,
296 "SetSendCNPayloadType() invalid payload frequency");
niklase@google.com470e71d2011-07-07 08:21:25 +0000297 return -1;
298 }
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000299 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
300 voe::Channel* channelPtr = ch.channel();
niklase@google.com470e71d2011-07-07 08:21:25 +0000301 if (channelPtr == NULL)
302 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000303 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
304 "SetSendCNPayloadType() failed to locate channel");
niklase@google.com470e71d2011-07-07 08:21:25 +0000305 return -1;
306 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000307 return channelPtr->SetSendCNPayloadType(type, frequency);
308}
309
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000310int VoECodecImpl::SetFECStatus(int channel, bool enable) {
311 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
312 "SetCodecFECStatus(channel=%d, enable=%d)", channel, enable);
313 if (!_shared->statistics().Initialized()) {
314 _shared->SetLastError(VE_NOT_INITED, kTraceError);
315 return -1;
316 }
317 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
318 voe::Channel* channelPtr = ch.channel();
319 if (channelPtr == NULL) {
320 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
321 "SetCodecFECStatus() failed to locate channel");
322 return -1;
323 }
324 return channelPtr->SetCodecFECStatus(enable);
325}
326
327int VoECodecImpl::GetFECStatus(int channel, bool& enabled) {
328 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
329 "GetCodecFECStatus(channel=%d)", channel);
330 if (!_shared->statistics().Initialized()) {
331 _shared->SetLastError(VE_NOT_INITED, kTraceError);
332 return -1;
333 }
334 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
335 voe::Channel* channelPtr = ch.channel();
336 if (channelPtr == NULL) {
337 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
338 "GetFECStatus() failed to locate channel");
339 return -1;
340 }
341 enabled = channelPtr->GetCodecFECStatus();
342 return 0;
343}
344
niklase@google.com470e71d2011-07-07 08:21:25 +0000345int VoECodecImpl::SetVADStatus(int channel, bool enable, VadModes mode,
346 bool disableDTX)
347{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000348 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000349 "SetVADStatus(channel=%i, enable=%i, mode=%i, disableDTX=%i)",
350 channel, enable, mode, disableDTX);
351
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000352 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +0000353 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000354 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +0000355 return -1;
356 }
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000357 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
358 voe::Channel* channelPtr = ch.channel();
niklase@google.com470e71d2011-07-07 08:21:25 +0000359 if (channelPtr == NULL)
360 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000361 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
362 "SetVADStatus failed to locate channel");
niklase@google.com470e71d2011-07-07 08:21:25 +0000363 return -1;
364 }
365
366 ACMVADMode vadMode(VADNormal);
367 switch (mode)
368 {
369 case kVadConventional:
370 vadMode = VADNormal;
371 break;
372 case kVadAggressiveLow:
373 vadMode = VADLowBitrate;
374 break;
375 case kVadAggressiveMid:
376 vadMode = VADAggr;
377 break;
378 case kVadAggressiveHigh:
379 vadMode = VADVeryAggr;
380 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000381 }
382 return channelPtr->SetVADStatus(enable, vadMode, disableDTX);
383}
384
385int VoECodecImpl::GetVADStatus(int channel, bool& enabled, VadModes& mode,
386 bool& disabledDTX)
387{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000388 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000389 "GetVADStatus(channel=%i)", channel);
390
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000391 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +0000392 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000393 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +0000394 return -1;
395 }
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000396 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
397 voe::Channel* channelPtr = ch.channel();
niklase@google.com470e71d2011-07-07 08:21:25 +0000398 if (channelPtr == NULL)
399 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000400 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
401 "GetVADStatus failed to locate channel");
niklase@google.com470e71d2011-07-07 08:21:25 +0000402 return -1;
403 }
404
405 ACMVADMode vadMode;
406 int ret = channelPtr->GetVADStatus(enabled, vadMode, disabledDTX);
407
408 if (ret != 0)
409 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000410 _shared->SetLastError(VE_INVALID_OPERATION, kTraceError,
411 "GetVADStatus failed to get VAD mode");
niklase@google.com470e71d2011-07-07 08:21:25 +0000412 return -1;
413 }
414 switch (vadMode)
415 {
416 case VADNormal:
417 mode = kVadConventional;
418 break;
419 case VADLowBitrate:
420 mode = kVadAggressiveLow;
421 break;
422 case VADAggr:
423 mode = kVadAggressiveMid;
424 break;
425 case VADVeryAggr:
426 mode = kVadAggressiveHigh;
427 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000428 }
429
430 return 0;
431}
432
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +0000433int VoECodecImpl::SetOpusMaxPlaybackRate(int channel, int frequency_hz) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +0000434 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +0000435 "SetOpusMaxPlaybackRate(channel=%d, frequency_hz=%d)", channel,
436 frequency_hz);
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +0000437 if (!_shared->statistics().Initialized()) {
438 _shared->SetLastError(VE_NOT_INITED, kTraceError);
439 return -1;
440 }
441 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
442 voe::Channel* channelPtr = ch.channel();
443 if (channelPtr == NULL) {
444 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +0000445 "SetOpusMaxPlaybackRate failed to locate channel");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +0000446 return -1;
447 }
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +0000448 return channelPtr->SetOpusMaxPlaybackRate(frequency_hz);
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +0000449}
450
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +0000451int VoECodecImpl::SetOpusDtx(int channel, bool enable_dtx) {
452 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
453 "SetOpusDtx(channel=%d, enable_dtx=%d)", channel, enable_dtx);
454 if (!_shared->statistics().Initialized()) {
455 _shared->SetLastError(VE_NOT_INITED, kTraceError);
456 return -1;
457 }
458 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
459 voe::Channel* channelPtr = ch.channel();
460 if (channelPtr == NULL) {
461 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
462 "SetOpusDtx failed to locate channel");
463 return -1;
464 }
465 return channelPtr->SetOpusDtx(enable_dtx);
466}
467
niklase@google.com470e71d2011-07-07 08:21:25 +0000468void VoECodecImpl::ACMToExternalCodecRepresentation(CodecInst& toInst,
469 const CodecInst& fromInst)
470{
471 toInst = fromInst;
472 if (STR_CASE_CMP(fromInst.plname,"SILK") == 0)
473 {
474 if (fromInst.plfreq == 12000)
475 {
476 if (fromInst.pacsize == 320)
477 {
478 toInst.pacsize = 240;
479 }
480 else if (fromInst.pacsize == 640)
481 {
482 toInst.pacsize = 480;
483 }
484 else if (fromInst.pacsize == 960)
485 {
486 toInst.pacsize = 720;
487 }
488 }
489 else if (fromInst.plfreq == 24000)
490 {
491 if (fromInst.pacsize == 640)
492 {
493 toInst.pacsize = 480;
494 }
495 else if (fromInst.pacsize == 1280)
496 {
497 toInst.pacsize = 960;
498 }
499 else if (fromInst.pacsize == 1920)
500 {
501 toInst.pacsize = 1440;
502 }
503 }
504 }
505}
506
507void VoECodecImpl::ExternalToACMCodecRepresentation(CodecInst& toInst,
508 const CodecInst& fromInst)
509{
510 toInst = fromInst;
511 if (STR_CASE_CMP(fromInst.plname,"SILK") == 0)
512 {
513 if (fromInst.plfreq == 12000)
514 {
515 if (fromInst.pacsize == 240)
516 {
517 toInst.pacsize = 320;
518 }
519 else if (fromInst.pacsize == 480)
520 {
521 toInst.pacsize = 640;
522 }
523 else if (fromInst.pacsize == 720)
524 {
525 toInst.pacsize = 960;
526 }
527 }
528 else if (fromInst.plfreq == 24000)
529 {
530 if (fromInst.pacsize == 480)
531 {
532 toInst.pacsize = 640;
533 }
534 else if (fromInst.pacsize == 960)
535 {
536 toInst.pacsize = 1280;
537 }
538 else if (fromInst.pacsize == 1440)
539 {
540 toInst.pacsize = 1920;
541 }
542 }
543 }
544}
545
niklase@google.com470e71d2011-07-07 08:21:25 +0000546#endif // WEBRTC_VOICE_ENGINE_CODEC_API
547
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000548} // namespace webrtc