niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
xians@webrtc.org | 79af734 | 2012-01-31 12:22:14 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 "voe_external_media_impl.h" |
| 12 | |
| 13 | #include "channel.h" |
| 14 | #include "critical_section_wrapper.h" |
| 15 | #include "output_mixer.h" |
| 16 | #include "trace.h" |
| 17 | #include "transmit_mixer.h" |
| 18 | #include "voice_engine_impl.h" |
| 19 | #include "voe_errors.h" |
| 20 | |
| 21 | namespace webrtc { |
| 22 | |
| 23 | VoEExternalMedia* VoEExternalMedia::GetInterface(VoiceEngine* voiceEngine) |
| 24 | { |
| 25 | #ifndef WEBRTC_VOICE_ENGINE_EXTERNAL_MEDIA_API |
| 26 | return NULL; |
| 27 | #else |
| 28 | if (NULL == voiceEngine) |
| 29 | { |
| 30 | return NULL; |
| 31 | } |
tommi@webrtc.org | 0989fb7 | 2013-02-15 15:07:32 +0000 | [diff] [blame] | 32 | VoiceEngineImpl* s = static_cast<VoiceEngineImpl*>(voiceEngine); |
tommi@webrtc.org | a990e12 | 2012-04-26 15:28:22 +0000 | [diff] [blame] | 33 | s->AddRef(); |
| 34 | return s; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 35 | #endif |
| 36 | } |
| 37 | |
| 38 | #ifdef WEBRTC_VOICE_ENGINE_EXTERNAL_MEDIA_API |
| 39 | |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 40 | VoEExternalMediaImpl::VoEExternalMediaImpl(voe::SharedData* shared) |
wjia@webrtc.org | 3f9db37 | 2013-01-12 01:09:03 +0000 | [diff] [blame] | 41 | : |
| 42 | #ifdef WEBRTC_VOE_EXTERNAL_REC_AND_PLAYOUT |
| 43 | playout_delay_ms_(0), |
| 44 | #endif |
| 45 | shared_(shared) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 46 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 47 | WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(shared_->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 48 | "VoEExternalMediaImpl() - ctor"); |
| 49 | } |
| 50 | |
| 51 | VoEExternalMediaImpl::~VoEExternalMediaImpl() |
| 52 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 53 | WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(shared_->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 54 | "~VoEExternalMediaImpl() - dtor"); |
| 55 | } |
| 56 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 57 | int VoEExternalMediaImpl::RegisterExternalMediaProcessing( |
| 58 | int channel, |
| 59 | ProcessingTypes type, |
| 60 | VoEMediaProcess& processObject) |
| 61 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 62 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(shared_->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 63 | "RegisterExternalMediaProcessing(channel=%d, type=%d, " |
| 64 | "processObject=0x%x)", channel, type, &processObject); |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 65 | if (!shared_->statistics().Initialized()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 66 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 67 | shared_->SetLastError(VE_NOT_INITED, kTraceError); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 68 | return -1; |
| 69 | } |
| 70 | switch (type) |
| 71 | { |
| 72 | case kPlaybackPerChannel: |
| 73 | case kRecordingPerChannel: |
| 74 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 75 | voe::ScopedChannel sc(shared_->channel_manager(), channel); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 76 | voe::Channel* channelPtr = sc.ChannelPtr(); |
| 77 | if (channelPtr == NULL) |
| 78 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 79 | shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 80 | "RegisterExternalMediaProcessing() failed to locate " |
| 81 | "channel"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 82 | return -1; |
| 83 | } |
| 84 | return channelPtr->RegisterExternalMediaProcessing(type, |
| 85 | processObject); |
| 86 | } |
| 87 | case kPlaybackAllChannelsMixed: |
| 88 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 89 | return shared_->output_mixer()->RegisterExternalMediaProcessing( |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 90 | processObject); |
| 91 | } |
| 92 | case kRecordingAllChannelsMixed: |
andrew@webrtc.org | 21ab3ba | 2012-10-19 17:30:56 +0000 | [diff] [blame] | 93 | case kRecordingPreprocessing: |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 94 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 95 | return shared_->transmit_mixer()->RegisterExternalMediaProcessing( |
andrew@webrtc.org | 21ab3ba | 2012-10-19 17:30:56 +0000 | [diff] [blame] | 96 | &processObject, type); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 97 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 98 | } |
mflodman@webrtc.org | c80d9d9 | 2012-02-06 10:11:25 +0000 | [diff] [blame] | 99 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | int VoEExternalMediaImpl::DeRegisterExternalMediaProcessing( |
| 103 | int channel, |
| 104 | ProcessingTypes type) |
| 105 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 106 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(shared_->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 107 | "DeRegisterExternalMediaProcessing(channel=%d)", channel); |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 108 | if (!shared_->statistics().Initialized()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 109 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 110 | shared_->SetLastError(VE_NOT_INITED, kTraceError); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 111 | return -1; |
| 112 | } |
| 113 | switch (type) |
| 114 | { |
| 115 | case kPlaybackPerChannel: |
| 116 | case kRecordingPerChannel: |
| 117 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 118 | voe::ScopedChannel sc(shared_->channel_manager(), channel); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 119 | voe::Channel* channelPtr = sc.ChannelPtr(); |
| 120 | if (channelPtr == NULL) |
| 121 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 122 | shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 123 | "RegisterExternalMediaProcessing() " |
| 124 | "failed to locate channel"); |
| 125 | return -1; |
| 126 | } |
| 127 | return channelPtr->DeRegisterExternalMediaProcessing(type); |
| 128 | } |
| 129 | case kPlaybackAllChannelsMixed: |
| 130 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 131 | return shared_->output_mixer()-> |
| 132 | DeRegisterExternalMediaProcessing(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 133 | } |
| 134 | case kRecordingAllChannelsMixed: |
andrew@webrtc.org | 21ab3ba | 2012-10-19 17:30:56 +0000 | [diff] [blame] | 135 | case kRecordingPreprocessing: |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 136 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 137 | return shared_->transmit_mixer()-> |
andrew@webrtc.org | 21ab3ba | 2012-10-19 17:30:56 +0000 | [diff] [blame] | 138 | DeRegisterExternalMediaProcessing(type); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 139 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 140 | } |
mflodman@webrtc.org | c80d9d9 | 2012-02-06 10:11:25 +0000 | [diff] [blame] | 141 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | int VoEExternalMediaImpl::SetExternalRecordingStatus(bool enable) |
| 145 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 146 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(shared_->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 147 | "SetExternalRecordingStatus(enable=%d)", enable); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 148 | #ifdef WEBRTC_VOE_EXTERNAL_REC_AND_PLAYOUT |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 149 | if (shared_->audio_device()->Recording()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 150 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 151 | shared_->SetLastError(VE_ALREADY_SENDING, kTraceError, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 152 | "SetExternalRecordingStatus() cannot set state while sending"); |
| 153 | return -1; |
| 154 | } |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 155 | shared_->set_ext_recording(enable); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 156 | return 0; |
| 157 | #else |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 158 | shared_->SetLastError(VE_FUNC_NOT_SUPPORTED, kTraceError, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 159 | "SetExternalRecordingStatus() external recording is not supported"); |
| 160 | return -1; |
| 161 | #endif |
| 162 | } |
| 163 | |
| 164 | int VoEExternalMediaImpl::ExternalRecordingInsertData( |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 165 | const int16_t speechData10ms[], |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 166 | int lengthSamples, |
| 167 | int samplingFreqHz, |
| 168 | int current_delay_ms) |
| 169 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 170 | WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(shared_->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 171 | "ExternalRecordingInsertData(speechData10ms=0x%x," |
| 172 | " lengthSamples=%u, samplingFreqHz=%d, current_delay_ms=%d)", |
| 173 | &speechData10ms[0], lengthSamples, samplingFreqHz, |
| 174 | current_delay_ms); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 175 | #ifdef WEBRTC_VOE_EXTERNAL_REC_AND_PLAYOUT |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 176 | if (!shared_->statistics().Initialized()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 177 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 178 | shared_->SetLastError(VE_NOT_INITED, kTraceError); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 179 | return -1; |
| 180 | } |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 181 | if (!shared_->ext_recording()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 182 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 183 | shared_->SetLastError(VE_INVALID_OPERATION, kTraceError, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 184 | "ExternalRecordingInsertData() external recording is not enabled"); |
| 185 | return -1; |
| 186 | } |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 187 | if (shared_->NumOfSendingChannels() == 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 188 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 189 | shared_->SetLastError(VE_ALREADY_SENDING, kTraceError, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 190 | "SetExternalRecordingStatus() no channel is sending"); |
| 191 | return -1; |
| 192 | } |
| 193 | if ((16000 != samplingFreqHz) && (32000 != samplingFreqHz) && |
| 194 | (48000 != samplingFreqHz) && (44000 != samplingFreqHz)) |
| 195 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 196 | shared_->SetLastError(VE_INVALID_ARGUMENT, kTraceError, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 197 | "SetExternalRecordingStatus() invalid sample rate"); |
| 198 | return -1; |
| 199 | } |
| 200 | if ((0 == lengthSamples) || |
| 201 | ((lengthSamples % (samplingFreqHz / 100)) != 0)) |
| 202 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 203 | shared_->SetLastError(VE_INVALID_ARGUMENT, kTraceError, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 204 | "SetExternalRecordingStatus() invalid buffer size"); |
| 205 | return -1; |
| 206 | } |
| 207 | if (current_delay_ms < 0) |
| 208 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 209 | shared_->SetLastError(VE_INVALID_ARGUMENT, kTraceError, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 210 | "SetExternalRecordingStatus() invalid delay)"); |
| 211 | return -1; |
| 212 | } |
| 213 | |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 214 | uint16_t blockSize = samplingFreqHz / 100; |
| 215 | uint32_t nBlocks = lengthSamples / blockSize; |
| 216 | int16_t totalDelayMS = 0; |
| 217 | uint16_t playoutDelayMS = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 218 | |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 219 | for (uint32_t i = 0; i < nBlocks; i++) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 220 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 221 | if (!shared_->ext_playout()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 222 | { |
| 223 | // Use real playout delay if external playout is not enabled. |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 224 | if (shared_->audio_device()->PlayoutDelay(&playoutDelayMS) != 0) { |
| 225 | shared_->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceWarning, |
xians@webrtc.org | 79af734 | 2012-01-31 12:22:14 +0000 | [diff] [blame] | 226 | "PlayoutDelay() unable to get the playout delay"); |
| 227 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 228 | totalDelayMS = current_delay_ms + playoutDelayMS; |
| 229 | } |
| 230 | else |
| 231 | { |
| 232 | // Use stored delay value given the last call |
| 233 | // to ExternalPlayoutGetData. |
| 234 | totalDelayMS = current_delay_ms + playout_delay_ms_; |
| 235 | // Compensate for block sizes larger than 10ms |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 236 | totalDelayMS -= (int16_t)(i*10); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 237 | if (totalDelayMS < 0) |
| 238 | totalDelayMS = 0; |
| 239 | } |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 240 | shared_->transmit_mixer()->PrepareDemux( |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 241 | (const int8_t*)(&speechData10ms[i*blockSize]), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 242 | blockSize, |
| 243 | 1, |
| 244 | samplingFreqHz, |
| 245 | totalDelayMS, |
| 246 | 0, |
| 247 | 0); |
| 248 | |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 249 | shared_->transmit_mixer()->DemuxAndMix(); |
| 250 | shared_->transmit_mixer()->EncodeAndSend(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 251 | } |
| 252 | return 0; |
| 253 | #else |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 254 | shared_->SetLastError(VE_FUNC_NOT_SUPPORTED, kTraceError, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 255 | "ExternalRecordingInsertData() external recording is not supported"); |
| 256 | return -1; |
| 257 | #endif |
| 258 | } |
| 259 | |
| 260 | int VoEExternalMediaImpl::SetExternalPlayoutStatus(bool enable) |
| 261 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 262 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(shared_->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 263 | "SetExternalPlayoutStatus(enable=%d)", enable); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 264 | #ifdef WEBRTC_VOE_EXTERNAL_REC_AND_PLAYOUT |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 265 | if (shared_->audio_device()->Playing()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 266 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 267 | shared_->SetLastError(VE_ALREADY_SENDING, kTraceError, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 268 | "SetExternalPlayoutStatus() cannot set state while playing"); |
| 269 | return -1; |
| 270 | } |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 271 | shared_->set_ext_playout(enable); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 272 | return 0; |
| 273 | #else |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 274 | shared_->SetLastError(VE_FUNC_NOT_SUPPORTED, kTraceError, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 275 | "SetExternalPlayoutStatus() external playout is not supported"); |
| 276 | return -1; |
| 277 | #endif |
| 278 | } |
| 279 | |
| 280 | int VoEExternalMediaImpl::ExternalPlayoutGetData( |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 281 | int16_t speechData10ms[], |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 282 | int samplingFreqHz, |
| 283 | int current_delay_ms, |
| 284 | int& lengthSamples) |
| 285 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 286 | WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(shared_->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 287 | "ExternalPlayoutGetData(speechData10ms=0x%x, samplingFreqHz=%d" |
| 288 | ", current_delay_ms=%d)", &speechData10ms[0], samplingFreqHz, |
| 289 | current_delay_ms); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 290 | #ifdef WEBRTC_VOE_EXTERNAL_REC_AND_PLAYOUT |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 291 | if (!shared_->statistics().Initialized()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 292 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 293 | shared_->SetLastError(VE_NOT_INITED, kTraceError); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 294 | return -1; |
| 295 | } |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 296 | if (!shared_->ext_playout()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 297 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 298 | shared_->SetLastError(VE_INVALID_OPERATION, kTraceError, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 299 | "ExternalPlayoutGetData() external playout is not enabled"); |
| 300 | return -1; |
| 301 | } |
| 302 | if ((16000 != samplingFreqHz) && (32000 != samplingFreqHz) && |
| 303 | (48000 != samplingFreqHz) && (44000 != samplingFreqHz)) |
| 304 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 305 | shared_->SetLastError(VE_INVALID_ARGUMENT, kTraceError, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 306 | "ExternalPlayoutGetData() invalid sample rate"); |
| 307 | return -1; |
| 308 | } |
| 309 | if (current_delay_ms < 0) |
| 310 | { |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 311 | shared_->SetLastError(VE_INVALID_ARGUMENT, kTraceError, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 312 | "ExternalPlayoutGetData() invalid delay)"); |
| 313 | return -1; |
| 314 | } |
| 315 | |
| 316 | AudioFrame audioFrame; |
| 317 | |
| 318 | // Retrieve mixed output at the specified rate |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 319 | shared_->output_mixer()->MixActiveChannels(); |
| 320 | shared_->output_mixer()->DoOperationsOnCombinedSignal(); |
andrew@webrtc.org | 4ecea3e | 2012-06-27 03:25:31 +0000 | [diff] [blame] | 321 | shared_->output_mixer()->GetMixedAudio(samplingFreqHz, 1, &audioFrame); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 322 | |
| 323 | // Deliver audio (PCM) samples to the external sink |
| 324 | memcpy(speechData10ms, |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 325 | audioFrame.data_, |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 326 | sizeof(int16_t)*(audioFrame.samples_per_channel_)); |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 327 | lengthSamples = audioFrame.samples_per_channel_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 328 | |
| 329 | // Store current playout delay (to be used by ExternalRecordingInsertData). |
| 330 | playout_delay_ms_ = current_delay_ms; |
| 331 | |
| 332 | return 0; |
| 333 | #else |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 334 | shared_->SetLastError(VE_FUNC_NOT_SUPPORTED, kTraceError, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 335 | "ExternalPlayoutGetData() external playout is not supported"); |
| 336 | return -1; |
| 337 | #endif |
| 338 | } |
| 339 | |
roosa@google.com | 1b60ceb | 2012-12-12 23:00:29 +0000 | [diff] [blame] | 340 | int VoEExternalMediaImpl::GetAudioFrame(int channel, int desired_sample_rate_hz, |
| 341 | AudioFrame* frame) { |
| 342 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, |
| 343 | VoEId(shared_->instance_id(), channel), |
| 344 | "GetAudioFrame(channel=%d, desired_sample_rate_hz=%d)", |
| 345 | channel, desired_sample_rate_hz); |
| 346 | if (!shared_->statistics().Initialized()) |
| 347 | { |
| 348 | shared_->SetLastError(VE_NOT_INITED, kTraceError); |
| 349 | return -1; |
| 350 | } |
| 351 | voe::ScopedChannel sc(shared_->channel_manager(), channel); |
| 352 | voe::Channel* channelPtr = sc.ChannelPtr(); |
| 353 | if (channelPtr == NULL) |
| 354 | { |
| 355 | shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 356 | "GetAudioFrame() failed to locate channel"); |
| 357 | return -1; |
| 358 | } |
| 359 | if (!channelPtr->ExternalMixing()) { |
| 360 | shared_->SetLastError(VE_INVALID_OPERATION, kTraceError, |
| 361 | "GetAudioFrame() was called on channel that is not" |
| 362 | " externally mixed."); |
| 363 | return -1; |
| 364 | } |
| 365 | if (!channelPtr->Playing()) { |
| 366 | shared_->SetLastError(VE_INVALID_OPERATION, kTraceError, |
| 367 | "GetAudioFrame() was called on channel that is not playing."); |
| 368 | return -1; |
| 369 | } |
| 370 | if (desired_sample_rate_hz == -1) { |
| 371 | shared_->SetLastError(VE_BAD_ARGUMENT, kTraceError, |
| 372 | "GetAudioFrame() was called with bad sample rate."); |
| 373 | return -1; |
| 374 | } |
| 375 | frame->sample_rate_hz_ = desired_sample_rate_hz == 0 ? -1 : |
| 376 | desired_sample_rate_hz; |
| 377 | return channelPtr->GetAudioFrame(channel, *frame); |
| 378 | } |
| 379 | |
| 380 | int VoEExternalMediaImpl::SetExternalMixing(int channel, bool enable) { |
| 381 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, |
| 382 | VoEId(shared_->instance_id(), channel), |
| 383 | "SetExternalMixing(channel=%d, enable=%d)", channel, enable); |
| 384 | if (!shared_->statistics().Initialized()) |
| 385 | { |
| 386 | shared_->SetLastError(VE_NOT_INITED, kTraceError); |
| 387 | return -1; |
| 388 | } |
| 389 | voe::ScopedChannel sc(shared_->channel_manager(), channel); |
| 390 | voe::Channel* channelPtr = sc.ChannelPtr(); |
| 391 | if (channelPtr == NULL) |
| 392 | { |
| 393 | shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 394 | "SetExternalMixing() failed to locate channel"); |
| 395 | return -1; |
| 396 | } |
| 397 | return channelPtr->SetExternalMixing(enable); |
| 398 | } |
| 399 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 400 | #endif // WEBRTC_VOICE_ENGINE_EXTERNAL_MEDIA_API |
| 401 | |
| 402 | } // namespace webrtc |