niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +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 | |
pbos@webrtc.org | 956aa7e | 2013-05-21 13:52:32 +0000 | [diff] [blame] | 11 | #include "webrtc/voice_engine/voe_video_sync_impl.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
turaj@webrtc.org | 6388c3e | 2013-02-12 21:42:18 +0000 | [diff] [blame] | 13 | #include "webrtc/system_wrappers/interface/critical_section_wrapper.h" |
| 14 | #include "webrtc/system_wrappers/interface/trace.h" |
| 15 | #include "webrtc/voice_engine/channel.h" |
| 16 | #include "webrtc/voice_engine/include/voe_errors.h" |
| 17 | #include "webrtc/voice_engine/voice_engine_impl.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 18 | |
| 19 | namespace webrtc { |
| 20 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 21 | VoEVideoSync* VoEVideoSync::GetInterface(VoiceEngine* voiceEngine) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 22 | #ifndef WEBRTC_VOICE_ENGINE_VIDEO_SYNC_API |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 23 | return NULL; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 24 | #else |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 25 | if (NULL == voiceEngine) { |
| 26 | return NULL; |
| 27 | } |
| 28 | VoiceEngineImpl* s = static_cast<VoiceEngineImpl*>(voiceEngine); |
| 29 | s->AddRef(); |
| 30 | return s; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 31 | #endif |
| 32 | } |
| 33 | |
| 34 | #ifdef WEBRTC_VOICE_ENGINE_VIDEO_SYNC_API |
| 35 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 36 | VoEVideoSyncImpl::VoEVideoSyncImpl(voe::SharedData* shared) : _shared(shared) { |
| 37 | WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 38 | "VoEVideoSyncImpl::VoEVideoSyncImpl() - ctor"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 41 | VoEVideoSyncImpl::~VoEVideoSyncImpl() { |
| 42 | WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 43 | "VoEVideoSyncImpl::~VoEVideoSyncImpl() - dtor"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 46 | int VoEVideoSyncImpl::GetPlayoutTimestamp(int channel, |
| 47 | unsigned int& timestamp) { |
| 48 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 49 | "GetPlayoutTimestamp(channel=%d, timestamp=?)", channel); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 50 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 51 | if (!_shared->statistics().Initialized()) { |
| 52 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 53 | return -1; |
| 54 | } |
| 55 | voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel); |
| 56 | voe::Channel* channel_ptr = ch.channel(); |
| 57 | if (channel_ptr == NULL) { |
| 58 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 59 | "GetPlayoutTimestamp() failed to locate channel"); |
| 60 | return -1; |
| 61 | } |
| 62 | return channel_ptr->GetPlayoutTimestamp(timestamp); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 65 | int VoEVideoSyncImpl::SetInitTimestamp(int channel, unsigned int timestamp) { |
| 66 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 67 | "SetInitTimestamp(channel=%d, timestamp=%lu)", channel, |
| 68 | timestamp); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 69 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 70 | if (!_shared->statistics().Initialized()) { |
| 71 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 72 | return -1; |
| 73 | } |
| 74 | voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel); |
| 75 | voe::Channel* channelPtr = ch.channel(); |
| 76 | if (channelPtr == NULL) { |
| 77 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 78 | "SetInitTimestamp() failed to locate channel"); |
| 79 | return -1; |
| 80 | } |
| 81 | return channelPtr->SetInitTimestamp(timestamp); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 84 | int VoEVideoSyncImpl::SetInitSequenceNumber(int channel, short sequenceNumber) { |
| 85 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 86 | "SetInitSequenceNumber(channel=%d, sequenceNumber=%hd)", channel, |
| 87 | sequenceNumber); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 88 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 89 | if (!_shared->statistics().Initialized()) { |
| 90 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 91 | return -1; |
| 92 | } |
| 93 | voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel); |
| 94 | voe::Channel* channelPtr = ch.channel(); |
| 95 | if (channelPtr == NULL) { |
| 96 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 97 | "SetInitSequenceNumber() failed to locate channel"); |
| 98 | return -1; |
| 99 | } |
| 100 | return channelPtr->SetInitSequenceNumber(sequenceNumber); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 103 | int VoEVideoSyncImpl::SetMinimumPlayoutDelay(int channel, int delayMs) { |
| 104 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 105 | "SetMinimumPlayoutDelay(channel=%d, delayMs=%d)", channel, |
| 106 | delayMs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 107 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 108 | if (!_shared->statistics().Initialized()) { |
| 109 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 110 | return -1; |
| 111 | } |
| 112 | voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel); |
| 113 | voe::Channel* channelPtr = ch.channel(); |
| 114 | if (channelPtr == NULL) { |
| 115 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 116 | "SetMinimumPlayoutDelay() failed to locate channel"); |
| 117 | return -1; |
| 118 | } |
| 119 | return channelPtr->SetMinimumPlayoutDelay(delayMs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 122 | int VoEVideoSyncImpl::SetInitialPlayoutDelay(int channel, int delay_ms) { |
| 123 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 124 | "SetInitialPlayoutDelay(channel=%d, delay_ms=%d)", channel, |
| 125 | delay_ms); |
turaj@webrtc.org | 6388c3e | 2013-02-12 21:42:18 +0000 | [diff] [blame] | 126 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 127 | if (!_shared->statistics().Initialized()) { |
| 128 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 129 | return -1; |
| 130 | } |
| 131 | voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel); |
| 132 | voe::Channel* channelPtr = ch.channel(); |
| 133 | if (channelPtr == NULL) { |
| 134 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 135 | "SetInitialPlayoutDelay() failed to locate channel"); |
| 136 | return -1; |
| 137 | } |
| 138 | return channelPtr->SetInitialPlayoutDelay(delay_ms); |
turaj@webrtc.org | 6388c3e | 2013-02-12 21:42:18 +0000 | [diff] [blame] | 139 | } |
| 140 | |
pwestin@webrtc.org | 1de0135 | 2013-04-11 20:23:35 +0000 | [diff] [blame] | 141 | int VoEVideoSyncImpl::GetDelayEstimate(int channel, |
| 142 | int* jitter_buffer_delay_ms, |
| 143 | int* playout_buffer_delay_ms) { |
| 144 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 145 | "GetDelayEstimate(channel=%d, delayMs=?)", channel); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 146 | |
pwestin@webrtc.org | 1de0135 | 2013-04-11 20:23:35 +0000 | [diff] [blame] | 147 | if (!_shared->statistics().Initialized()) { |
| 148 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 149 | return -1; |
| 150 | } |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame] | 151 | voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel); |
| 152 | voe::Channel* channelPtr = ch.channel(); |
pwestin@webrtc.org | 1de0135 | 2013-04-11 20:23:35 +0000 | [diff] [blame] | 153 | if (channelPtr == NULL) { |
| 154 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 155 | "GetDelayEstimate() failed to locate channel"); |
| 156 | return -1; |
| 157 | } |
| 158 | if (!channelPtr->GetDelayEstimate(jitter_buffer_delay_ms, |
| 159 | playout_buffer_delay_ms)) { |
| 160 | return -1; |
| 161 | } |
| 162 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 165 | int VoEVideoSyncImpl::GetPlayoutBufferSize(int& bufferMs) { |
| 166 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 167 | "GetPlayoutBufferSize(bufferMs=?)"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 168 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 169 | if (!_shared->statistics().Initialized()) { |
| 170 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 171 | return -1; |
| 172 | } |
| 173 | AudioDeviceModule::BufferType type(AudioDeviceModule::kFixedBufferSize); |
| 174 | uint16_t sizeMS(0); |
| 175 | if (_shared->audio_device()->PlayoutBuffer(&type, &sizeMS) != 0) { |
| 176 | _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError, |
| 177 | "GetPlayoutBufferSize() failed to read buffer size"); |
| 178 | return -1; |
| 179 | } |
| 180 | bufferMs = sizeMS; |
| 181 | WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 182 | "GetPlayoutBufferSize() => bufferMs=%d", bufferMs); |
| 183 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 186 | int VoEVideoSyncImpl::GetRtpRtcp(int channel, |
| 187 | RtpRtcp** rtpRtcpModule, |
| 188 | RtpReceiver** rtp_receiver) { |
| 189 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 190 | "GetRtpRtcp(channel=%i)", channel); |
fischman@webrtc.org | f61e02c | 2013-02-20 23:13:46 +0000 | [diff] [blame] | 191 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 192 | if (!_shared->statistics().Initialized()) { |
| 193 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 194 | return -1; |
| 195 | } |
| 196 | voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel); |
| 197 | voe::Channel* channelPtr = ch.channel(); |
| 198 | if (channelPtr == NULL) { |
| 199 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 200 | "GetPlayoutTimestamp() failed to locate channel"); |
| 201 | return -1; |
| 202 | } |
| 203 | return channelPtr->GetRtpRtcp(rtpRtcpModule, rtp_receiver); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 204 | } |
| 205 | |
turaj@webrtc.org | e46c8d3 | 2013-05-22 20:39:43 +0000 | [diff] [blame] | 206 | int VoEVideoSyncImpl::GetLeastRequiredDelayMs(int channel) const { |
| 207 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 208 | "GetLeastRequiredDelayMS(channel=%d)", channel); |
turaj@webrtc.org | e46c8d3 | 2013-05-22 20:39:43 +0000 | [diff] [blame] | 209 | |
| 210 | if (!_shared->statistics().Initialized()) { |
| 211 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 212 | return -1; |
| 213 | } |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame] | 214 | voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel); |
| 215 | voe::Channel* channel_ptr = ch.channel(); |
turaj@webrtc.org | e46c8d3 | 2013-05-22 20:39:43 +0000 | [diff] [blame] | 216 | if (channel_ptr == NULL) { |
| 217 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 218 | "GetLeastRequiredDelayMs() failed to locate channel"); |
| 219 | return -1; |
| 220 | } |
| 221 | return channel_ptr->least_required_delay_ms(); |
| 222 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 223 | |
| 224 | #endif // #ifdef WEBRTC_VOICE_ENGINE_VIDEO_SYNC_API |
| 225 | |
| 226 | } // namespace webrtc |