diff --git a/webrtc/voice_engine/channel.cc b/webrtc/voice_engine/channel.cc index 8cc9141218..f6f496c628 100644 --- a/webrtc/voice_engine/channel.cc +++ b/webrtc/voice_engine/channel.cc @@ -5579,60 +5579,24 @@ Channel::GetRTPStatistics(CallStatistics& stats) return 0; } -int -Channel::SetFECStatus(bool enable, int redPayloadtype) -{ - WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId), - "Channel::SetFECStatus()"); +int Channel::SetFECStatus(bool enable, int redPayloadtype) { + WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId), + "Channel::SetFECStatus()"); - CodecInst codec; + if (SetRedPayloadType(redPayloadtype) < 0) { + _engineStatisticsPtr->SetLastError( + VE_CODEC_ERROR, kTraceError, + "SetSecondarySendCodec() Failed to register RED ACM"); + return -1; + } - // Get default RED settings from the ACM database - bool foundRED(false); - const WebRtc_UWord8 nSupportedCodecs = AudioCodingModule::NumberOfCodecs(); - for (int idx = 0; (!foundRED && idx < nSupportedCodecs); idx++) - { - _audioCodingModule.Codec(idx, codec); - if (!STR_CASE_CMP(codec.plname, "RED")) - { - foundRED = true; - } - } - if (!foundRED) - { - _engineStatisticsPtr->SetLastError( - VE_CODEC_ERROR, kTraceError, - "SetFECStatus() RED is not supported"); - return -1; - } - - if (redPayloadtype != -1) - { - codec.pltype = redPayloadtype; - } - - if (_audioCodingModule.RegisterSendCodec(codec) != 0) - { - _engineStatisticsPtr->SetLastError( - VE_AUDIO_CODING_MODULE_ERROR, kTraceError, - "SetFECStatus() RED registration in ACM module failed"); - return -1; - } - if (_rtpRtcpModule->SetSendREDPayloadType(codec.pltype) != 0) - { - _engineStatisticsPtr->SetLastError( - VE_RTP_RTCP_MODULE_ERROR, kTraceError, - "SetFECStatus() RED registration in RTP/RTCP module failed"); - return -1; - } - if (_audioCodingModule.SetFECStatus(enable) != 0) - { - _engineStatisticsPtr->SetLastError( - VE_AUDIO_CODING_MODULE_ERROR, kTraceError, - "SetFECStatus() failed to set FEC state in the ACM"); - return -1; - } - return 0; + if (_audioCodingModule.SetFECStatus(enable) != 0) { + _engineStatisticsPtr->SetLastError( + VE_AUDIO_CODING_MODULE_ERROR, kTraceError, + "SetFECStatus() failed to set FEC state in the ACM"); + return -1; + } + return 0; } int @@ -6632,5 +6596,83 @@ int Channel::ApmProcessRx(AudioFrame& frame) { return 0; } +int Channel::SetSecondarySendCodec(const CodecInst& codec, + int red_payload_type) { + if (SetRedPayloadType(red_payload_type) < 0) { + _engineStatisticsPtr->SetLastError( + VE_AUDIO_CODING_MODULE_ERROR, kTraceError, + "SetSecondarySendCodec() Failed to register RED ACM"); + return -1; + } + if (_audioCodingModule.RegisterSecondarySendCodec(codec) < 0) { + _engineStatisticsPtr->SetLastError( + VE_AUDIO_CODING_MODULE_ERROR, kTraceError, + "SetSecondarySendCodec() Failed to register secondary send codec in " + "ACM"); + return -1; + } + + return 0; +} + +void Channel::RemoveSecondarySendCodec() { + _audioCodingModule.UnregisterSecondarySendCodec(); +} + +int Channel::GetSecondarySendCodec(CodecInst* codec) { + if (_audioCodingModule.SecondarySendCodec(codec) < 0) { + _engineStatisticsPtr->SetLastError( + VE_AUDIO_CODING_MODULE_ERROR, kTraceError, + "GetSecondarySendCodec() Failed to get secondary sent codec from ACM"); + return -1; + } + return 0; +} + +int Channel::SetRedPayloadType(int red_payload_type) { + if (red_payload_type < 0) { + _engineStatisticsPtr->SetLastError( + VE_PLTYPE_ERROR, kTraceError, + "SetRedPayloadType() invalid RED paylaod type"); + return -1; + } + + CodecInst codec; + bool found_red = false; + + // Get default RED settings from the ACM database + const int num_codecs = AudioCodingModule::NumberOfCodecs(); + for (int idx = 0; idx < num_codecs; idx++) { + _audioCodingModule.Codec(idx, codec); + if (!STR_CASE_CMP(codec.plname, "RED")) { + found_red = true; + break; + } + } + + if (!found_red) { + _engineStatisticsPtr->SetLastError( + VE_CODEC_ERROR, kTraceError, + "SetRedPayloadType() RED is not supported"); + return -1; + } + + codec.pltype = red_payload_type; + if (_audioCodingModule.RegisterSendCodec(codec) < 0) { + _engineStatisticsPtr->SetLastError( + VE_AUDIO_CODING_MODULE_ERROR, kTraceError, + "SetRedPayloadType() RED registration in ACM module failed"); + return -1; + } + + if (_rtpRtcpModule->SetSendREDPayloadType(red_payload_type) != 0) { + _engineStatisticsPtr->SetLastError( + VE_RTP_RTCP_MODULE_ERROR, kTraceError, + "SetRedPayloadType() RED registration in RTP/RTCP module failed"); + return -1; + } + return 0; +} + } // namespace voe } // namespace webrtc diff --git a/webrtc/voice_engine/channel.h b/webrtc/voice_engine/channel.h index 8889bc233d..2aacf3b555 100644 --- a/webrtc/voice_engine/channel.h +++ b/webrtc/voice_engine/channel.h @@ -150,6 +150,11 @@ public: WebRtc_Word32 SetISACMaxRate(int rateBps); WebRtc_Word32 SetISACMaxPayloadSize(int sizeBytes); + // VoE dual-streaming. + int SetSecondarySendCodec(const CodecInst& codec, int red_payload_type); + void RemoveSecondarySendCodec(); + int GetSecondarySendCodec(CodecInst* codec); + // VoENetwork WebRtc_Word32 RegisterExternalTransport(Transport& transport); WebRtc_Word32 DeRegisterExternalTransport(); @@ -532,6 +537,7 @@ private: void RegisterReceiveCodecsToRTPModule(); int ApmProcessRx(AudioFrame& audioFrame); + int SetRedPayloadType(int red_payload_type); private: CriticalSectionWrapper& _fileCritSect; CriticalSectionWrapper& _callbackCritSect; diff --git a/webrtc/voice_engine/include/voe_codec.h b/webrtc/voice_engine/include/voe_codec.h index 37f8f68f45..0f43e92107 100644 --- a/webrtc/voice_engine/include/voe_codec.h +++ b/webrtc/voice_engine/include/voe_codec.h @@ -64,6 +64,23 @@ public: // |channel|. virtual int GetSendCodec(int channel, CodecInst& codec) = 0; + // Sets the |codec| as secondary codec for |channel|. Registering a + // secondary send codec enables dual-streaming. In dual-streaming mode, + // payloads of the primary and the secondary codecs are packed in RED + // payloads with |red_payload_type| as payload type. The Secondary codec + // MUST have the same sampling rate as the primary codec, otherwise the + // codec cannot be registered and -1 is returned. This method fails if a + // primary codec is not yet set. + virtual int SetSecondarySendCodec(int channel, const CodecInst& codec, + int red_payload_type) = 0; + + // Removes the secondary codec from |channel|. This will terminate + // dual-streaming. + virtual int RemoveSecondarySendCodec(int channel) = 0; + + // Gets |codec| which is used as secondary codec in |channel|. + virtual int GetSecondarySendCodec(int channel, CodecInst& codec) = 0; + // Gets the currently received |codec| for a specific |channel|. virtual int GetRecCodec(int channel, CodecInst& codec) = 0; diff --git a/webrtc/voice_engine/include/voe_errors.h b/webrtc/voice_engine/include/voe_errors.h index cc0597063a..32f5c9b1e2 100644 --- a/webrtc/voice_engine/include/voe_errors.h +++ b/webrtc/voice_engine/include/voe_errors.h @@ -85,6 +85,9 @@ #define VE_CANNOT_GET_SEND_CODEC 8110 #define VE_CANNOT_GET_REC_CODEC 8111 #define VE_ALREADY_INITED 8112 +#define VE_CANNOT_SET_SECONDARY_SEND_CODEC 8113 +#define VE_CANNOT_GET_SECONDARY_SEND_CODEC 8114 +#define VE_CANNOT_REMOVE_SECONDARY_SEND_CODEC 8115 // Errors causing limited functionality #define VE_RTCP_SOCKET_ERROR 9001 diff --git a/webrtc/voice_engine/voe_codec_impl.cc b/webrtc/voice_engine/voe_codec_impl.cc index 6414efc3db..993e7bdf53 100644 --- a/webrtc/voice_engine/voe_codec_impl.cc +++ b/webrtc/voice_engine/voe_codec_impl.cc @@ -657,6 +657,111 @@ void VoECodecImpl::ExternalToACMCodecRepresentation(CodecInst& toInst, } } +int VoECodecImpl::SetSecondarySendCodec(int channel, const CodecInst& codec, + int red_payload_type) { + CodecInst copy_codec; + ExternalToACMCodecRepresentation(copy_codec, codec); + + WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), + "SetSecondarySendCodec(channel=%d, codec)", channel); + WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_shared->instance_id(), -1), + "codec: plname=%s, pacsize=%d, plfreq=%d, pltype=%d, " + "channels=%d, rate=%d", codec.plname, codec.pacsize, + codec.plfreq, codec.pltype, codec.channels, codec.rate); + if (!_shared->statistics().Initialized()) { + _shared->SetLastError(VE_NOT_INITED, kTraceError); + return -1; + } + + // External sanity checks performed outside the ACM + if ((STR_CASE_CMP(copy_codec.plname, "L16") == 0) && + (copy_codec.pacsize >= 960)) { + _shared->SetLastError(VE_INVALID_ARGUMENT, kTraceError, + "SetSecondarySendCodec() invalid L16 packet size"); + return -1; + } + + // None of the following codecs can be registered as the secondary encoder. + if (!STR_CASE_CMP(copy_codec.plname, "CN") || + !STR_CASE_CMP(copy_codec.plname, "TELEPHONE-EVENT") || + !STR_CASE_CMP(copy_codec.plname, "RED")) { + _shared->SetLastError(VE_INVALID_ARGUMENT, kTraceError, + "SetSecondarySendCodec() invalid codec name"); + return -1; + } + + // Only mono and stereo are supported. + if ((copy_codec.channels != 1) && (copy_codec.channels != 2)) { + _shared->SetLastError(VE_INVALID_ARGUMENT, kTraceError, + "SetSecondarySendCodec() invalid number of channels"); + return -1; + } + voe::ScopedChannel sc(_shared->channel_manager(), channel); + voe::Channel* channelPtr = sc.ChannelPtr(); + if (channelPtr == NULL) { + _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, + "SetSecondarySendCodec() failed to locate channel"); + return -1; + } + if (!AudioCodingModule::IsCodecValid(copy_codec)) { + _shared->SetLastError(VE_INVALID_ARGUMENT, kTraceError, + "SetSecondarySendCodec() invalid codec"); + return -1; + } + if (channelPtr->SetSecondarySendCodec(copy_codec, red_payload_type) != 0) { + _shared->SetLastError(VE_CANNOT_SET_SECONDARY_SEND_CODEC, kTraceError, + "SetSecondarySendCodec() failed to set secondary " + "send codec"); + return -1; + } + return 0; +} + +int VoECodecImpl::GetSecondarySendCodec(int channel, CodecInst& codec) { + WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), + "GetSecondarySendCodec(channel=%d, codec=?)", channel); + if (!_shared->statistics().Initialized()) { + _shared->SetLastError(VE_NOT_INITED, kTraceError); + return -1; + } + voe::ScopedChannel sc(_shared->channel_manager(), channel); + voe::Channel* channelPtr = sc.ChannelPtr(); + if (channelPtr == NULL) { + _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, + "GetSecondarySendCodec() failed to locate channel"); + return -1; + } + CodecInst acm_codec; + if (channelPtr->GetSecondarySendCodec(&acm_codec) != 0) { + _shared->SetLastError(VE_CANNOT_GET_SECONDARY_SEND_CODEC, kTraceError, + "GetSecondarySendCodec() failed to get secondary " + "send codec"); + return -1; + } + ACMToExternalCodecRepresentation(codec, acm_codec); + WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, + VoEId(_shared->instance_id(), -1), + "GetSecondarySendCodec() => plname=%s, pacsize=%d, plfreq=%d, " + "channels=%d, rate=%d", codec.plname, codec.pacsize, + codec.plfreq, codec.channels, codec.rate); + return 0; +} + +int VoECodecImpl::RemoveSecondarySendCodec(int channel) { + WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), + "RemoveSecondarySendCodec(channel=%d)", channel); + voe::ScopedChannel sc(_shared->channel_manager(), channel); + voe::Channel* channelPtr = sc.ChannelPtr(); + if (channelPtr == NULL) { + _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, + "RemoveSecondarySendCodec() failed to locate " + "channel"); + return -1; + } + channelPtr->RemoveSecondarySendCodec(); + return 0; +} + #endif // WEBRTC_VOICE_ENGINE_CODEC_API } // namespace webrtc diff --git a/webrtc/voice_engine/voe_codec_impl.h b/webrtc/voice_engine/voe_codec_impl.h index eb955ece93..8c0c9a8913 100644 --- a/webrtc/voice_engine/voe_codec_impl.h +++ b/webrtc/voice_engine/voe_codec_impl.h @@ -70,6 +70,14 @@ public: VadModes& mode, bool& disabledDTX); + // Dual-streaming + virtual int SetSecondarySendCodec(int channel, const CodecInst& codec, + int red_payload_type); + + virtual int RemoveSecondarySendCodec(int channel); + + virtual int GetSecondarySendCodec(int channel, CodecInst& codec); + protected: VoECodecImpl(voe::SharedData* shared); virtual ~VoECodecImpl(); diff --git a/webrtc/voice_engine/voe_codec_unittest.cc b/webrtc/voice_engine/voe_codec_unittest.cc new file mode 100644 index 0000000000..648a1002e3 --- /dev/null +++ b/webrtc/voice_engine/voe_codec_unittest.cc @@ -0,0 +1,162 @@ +/* + * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#include "webrtc/voice_engine/include/voe_codec.h" + +#include "gtest/gtest.h" +#include "webrtc/voice_engine/include/voe_base.h" +#include "webrtc/voice_engine/voice_engine_defines.h" + +namespace webrtc { +namespace voe { +namespace { + +class VoECodecTest : public ::testing::Test { + protected: + VoECodecTest() + : voe_(VoiceEngine::Create()), + base_(VoEBase::GetInterface(voe_)), + voe_codec_(VoECodec::GetInterface(voe_)), + channel_(-1), + red_payload_type_(-1) { + } + + virtual ~VoECodecTest() { + base_->DeleteChannel(channel_); + base_->Terminate(); + voe_codec_->Release(); + base_->Release(); + VoiceEngine::Delete(voe_); + } + + void SetUp() { + // Check if all components are valid. + ASSERT_TRUE(voe_ != NULL); + ASSERT_TRUE(base_ != NULL); + ASSERT_TRUE(voe_codec_ != NULL); + ASSERT_EQ(0, base_->Init()); + channel_ = base_->CreateChannel(); + ASSERT_NE(-1, channel_); + + CodecInst my_codec; + + bool primary_found = false; + bool valid_secondary_found = false; + bool invalid_secondary_found = false; + + // Find primary and secondary codecs. + int num_codecs = voe_codec_->NumOfCodecs(); + int n = 0; + while (n < num_codecs && (!primary_found || !valid_secondary_found || + !invalid_secondary_found || red_payload_type_ < 0)) { + EXPECT_EQ(0, voe_codec_->GetCodec(n, my_codec)); + if (!STR_CASE_CMP(my_codec.plname, "isac") && my_codec.plfreq == 16000) { + memcpy(&valid_secondary_, &my_codec, sizeof(my_codec)); + valid_secondary_found = true; + } else if (!STR_CASE_CMP(my_codec.plname, "isac") && + my_codec.plfreq == 32000) { + memcpy(&invalid_secondary_, &my_codec, sizeof(my_codec)); + invalid_secondary_found = true; + } else if (!STR_CASE_CMP(my_codec.plname, "L16") && + my_codec.plfreq == 16000) { + memcpy(&primary_, &my_codec, sizeof(my_codec)); + primary_found = true; + } else if (!STR_CASE_CMP(my_codec.plname, "RED")) { + red_payload_type_ = my_codec.pltype; + } + n++; + } + + EXPECT_TRUE(primary_found); + EXPECT_TRUE(valid_secondary_found); + EXPECT_TRUE(invalid_secondary_found); + EXPECT_NE(-1, red_payload_type_); + } + + VoiceEngine* voe_; + VoEBase* base_; + VoECodec* voe_codec_; + int channel_; + CodecInst primary_; + CodecInst valid_secondary_; + + // A codec which is not valid to be registered as secondary codec. + CodecInst invalid_secondary_; + int red_payload_type_; +}; + + +TEST_F(VoECodecTest, DualStreamSetSecondaryBeforePrimaryFails) { + // Setting secondary before a primary is registered should fail. + EXPECT_EQ(-1, voe_codec_->SetSecondarySendCodec(channel_, valid_secondary_, + red_payload_type_)); +} + +TEST_F(VoECodecTest, DualStreamRegisterWithWrongInputsFails) { + // Register primary codec. + EXPECT_EQ(0, voe_codec_->SetSendCodec(channel_, primary_)); + + // Wrong secondary. + EXPECT_EQ(-1, voe_codec_->SetSecondarySendCodec(channel_, invalid_secondary_, + red_payload_type_)); + + // Wrong paylaod. + EXPECT_EQ(-1, voe_codec_->SetSecondarySendCodec(channel_, valid_secondary_, + -1)); + // Wrong channel. + EXPECT_EQ(-1, voe_codec_->SetSecondarySendCodec(channel_ + 1, + valid_secondary_, + red_payload_type_)); +} + +TEST_F(VoECodecTest, DualStreamGetSecodaryEncoder) { + // Register primary codec. + EXPECT_EQ(0, voe_codec_->SetSendCodec(channel_, primary_)); + + // Register a valid codec. + EXPECT_EQ(0, voe_codec_->SetSecondarySendCodec(channel_, valid_secondary_, + red_payload_type_)); + CodecInst my_codec; + + // Get secondary codec from wrong channel. + EXPECT_EQ(-1, voe_codec_->GetSecondarySendCodec(channel_ + 1, my_codec)); + + // Get secondary and compare. + memset(&my_codec, 0, sizeof(my_codec)); + EXPECT_EQ(0, voe_codec_->GetSecondarySendCodec(channel_, my_codec)); + + EXPECT_EQ(valid_secondary_.plfreq, my_codec.plfreq); + EXPECT_EQ(valid_secondary_.channels, my_codec.channels); + EXPECT_EQ(valid_secondary_.pacsize, my_codec.pacsize); + EXPECT_EQ(valid_secondary_.rate, my_codec.rate); + EXPECT_EQ(valid_secondary_.pltype, my_codec.pltype); + EXPECT_EQ(0, STR_CASE_CMP(valid_secondary_.plname, my_codec.plname)); +} + +TEST_F(VoECodecTest, DualStreamRemoveSecondaryCodec) { + // Register primary codec. + EXPECT_EQ(0, voe_codec_->SetSendCodec(channel_, primary_)); + + // Register a valid codec. + EXPECT_EQ(0, voe_codec_->SetSecondarySendCodec(channel_, valid_secondary_, + red_payload_type_)); + // Remove from wrong channel. + EXPECT_EQ(-1, voe_codec_->RemoveSecondarySendCodec(channel_ + 1)); + EXPECT_EQ(0, voe_codec_->RemoveSecondarySendCodec(channel_)); + + CodecInst my_codec; + + // Get should fail, if secondary is removed. + EXPECT_EQ(-1, voe_codec_->GetSecondarySendCodec(channel_, my_codec)); +} + +} // namespace +} // namespace voe +} // namespace webrtc diff --git a/webrtc/voice_engine/voice_engine_core.gypi b/webrtc/voice_engine/voice_engine_core.gypi index 0478a710db..40f4c31234 100644 --- a/webrtc/voice_engine/voice_engine_core.gypi +++ b/webrtc/voice_engine/voice_engine_core.gypi @@ -146,6 +146,7 @@ 'output_mixer_unittest.cc', 'transmit_mixer_unittest.cc', 'voe_audio_processing_unittest.cc', + 'voe_codec_unittest.cc', ], }, ], # targets