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) { |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 48 | if (!_shared->statistics().Initialized()) { |
| 49 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 50 | return -1; |
| 51 | } |
| 52 | voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel); |
| 53 | voe::Channel* channel_ptr = ch.channel(); |
| 54 | if (channel_ptr == NULL) { |
| 55 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 56 | "GetPlayoutTimestamp() failed to locate channel"); |
| 57 | return -1; |
| 58 | } |
| 59 | return channel_ptr->GetPlayoutTimestamp(timestamp); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 62 | int VoEVideoSyncImpl::SetInitTimestamp(int channel, unsigned int timestamp) { |
| 63 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 64 | "SetInitTimestamp(channel=%d, timestamp=%lu)", channel, |
| 65 | timestamp); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 66 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 67 | if (!_shared->statistics().Initialized()) { |
| 68 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 69 | return -1; |
| 70 | } |
| 71 | voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel); |
| 72 | voe::Channel* channelPtr = ch.channel(); |
| 73 | if (channelPtr == NULL) { |
| 74 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 75 | "SetInitTimestamp() failed to locate channel"); |
| 76 | return -1; |
| 77 | } |
| 78 | return channelPtr->SetInitTimestamp(timestamp); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 81 | int VoEVideoSyncImpl::SetInitSequenceNumber(int channel, short sequenceNumber) { |
| 82 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 83 | "SetInitSequenceNumber(channel=%d, sequenceNumber=%hd)", channel, |
| 84 | sequenceNumber); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 85 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 86 | if (!_shared->statistics().Initialized()) { |
| 87 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 88 | return -1; |
| 89 | } |
| 90 | voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel); |
| 91 | voe::Channel* channelPtr = ch.channel(); |
| 92 | if (channelPtr == NULL) { |
| 93 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 94 | "SetInitSequenceNumber() failed to locate channel"); |
| 95 | return -1; |
| 96 | } |
| 97 | return channelPtr->SetInitSequenceNumber(sequenceNumber); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 100 | int VoEVideoSyncImpl::SetMinimumPlayoutDelay(int channel, int delayMs) { |
| 101 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 102 | "SetMinimumPlayoutDelay(channel=%d, delayMs=%d)", channel, |
| 103 | delayMs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 104 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 105 | if (!_shared->statistics().Initialized()) { |
| 106 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 107 | return -1; |
| 108 | } |
| 109 | voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel); |
| 110 | voe::Channel* channelPtr = ch.channel(); |
| 111 | if (channelPtr == NULL) { |
| 112 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 113 | "SetMinimumPlayoutDelay() failed to locate channel"); |
| 114 | return -1; |
| 115 | } |
| 116 | return channelPtr->SetMinimumPlayoutDelay(delayMs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 119 | int VoEVideoSyncImpl::SetInitialPlayoutDelay(int channel, int delay_ms) { |
| 120 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 121 | "SetInitialPlayoutDelay(channel=%d, delay_ms=%d)", channel, |
| 122 | delay_ms); |
turaj@webrtc.org | 6388c3e | 2013-02-12 21:42:18 +0000 | [diff] [blame] | 123 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 124 | if (!_shared->statistics().Initialized()) { |
| 125 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 126 | return -1; |
| 127 | } |
| 128 | voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel); |
| 129 | voe::Channel* channelPtr = ch.channel(); |
| 130 | if (channelPtr == NULL) { |
| 131 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 132 | "SetInitialPlayoutDelay() failed to locate channel"); |
| 133 | return -1; |
| 134 | } |
| 135 | return channelPtr->SetInitialPlayoutDelay(delay_ms); |
turaj@webrtc.org | 6388c3e | 2013-02-12 21:42:18 +0000 | [diff] [blame] | 136 | } |
| 137 | |
pwestin@webrtc.org | 1de0135 | 2013-04-11 20:23:35 +0000 | [diff] [blame] | 138 | int VoEVideoSyncImpl::GetDelayEstimate(int channel, |
| 139 | int* jitter_buffer_delay_ms, |
| 140 | int* playout_buffer_delay_ms) { |
pwestin@webrtc.org | 1de0135 | 2013-04-11 20:23:35 +0000 | [diff] [blame] | 141 | if (!_shared->statistics().Initialized()) { |
| 142 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 143 | return -1; |
| 144 | } |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame] | 145 | voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel); |
| 146 | voe::Channel* channelPtr = ch.channel(); |
pwestin@webrtc.org | 1de0135 | 2013-04-11 20:23:35 +0000 | [diff] [blame] | 147 | if (channelPtr == NULL) { |
| 148 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 149 | "GetDelayEstimate() failed to locate channel"); |
| 150 | return -1; |
| 151 | } |
| 152 | if (!channelPtr->GetDelayEstimate(jitter_buffer_delay_ms, |
| 153 | playout_buffer_delay_ms)) { |
| 154 | return -1; |
| 155 | } |
| 156 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 159 | int VoEVideoSyncImpl::GetPlayoutBufferSize(int& bufferMs) { |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 160 | if (!_shared->statistics().Initialized()) { |
| 161 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 162 | return -1; |
| 163 | } |
| 164 | AudioDeviceModule::BufferType type(AudioDeviceModule::kFixedBufferSize); |
| 165 | uint16_t sizeMS(0); |
| 166 | if (_shared->audio_device()->PlayoutBuffer(&type, &sizeMS) != 0) { |
| 167 | _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError, |
| 168 | "GetPlayoutBufferSize() failed to read buffer size"); |
| 169 | return -1; |
| 170 | } |
| 171 | bufferMs = sizeMS; |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 172 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 175 | int VoEVideoSyncImpl::GetRtpRtcp(int channel, |
| 176 | RtpRtcp** rtpRtcpModule, |
| 177 | RtpReceiver** rtp_receiver) { |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 178 | if (!_shared->statistics().Initialized()) { |
| 179 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 180 | return -1; |
| 181 | } |
| 182 | voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel); |
| 183 | voe::Channel* channelPtr = ch.channel(); |
| 184 | if (channelPtr == NULL) { |
| 185 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 186 | "GetPlayoutTimestamp() failed to locate channel"); |
| 187 | return -1; |
| 188 | } |
| 189 | return channelPtr->GetRtpRtcp(rtpRtcpModule, rtp_receiver); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 190 | } |
| 191 | |
turaj@webrtc.org | e46c8d3 | 2013-05-22 20:39:43 +0000 | [diff] [blame] | 192 | int VoEVideoSyncImpl::GetLeastRequiredDelayMs(int channel) const { |
turaj@webrtc.org | e46c8d3 | 2013-05-22 20:39:43 +0000 | [diff] [blame] | 193 | if (!_shared->statistics().Initialized()) { |
| 194 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 195 | return -1; |
| 196 | } |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame] | 197 | voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel); |
| 198 | voe::Channel* channel_ptr = ch.channel(); |
turaj@webrtc.org | e46c8d3 | 2013-05-22 20:39:43 +0000 | [diff] [blame] | 199 | if (channel_ptr == NULL) { |
| 200 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 201 | "GetLeastRequiredDelayMs() failed to locate channel"); |
| 202 | return -1; |
| 203 | } |
deadbeef | 7437588 | 2015-08-13 12:09:10 -0700 | [diff] [blame] | 204 | return channel_ptr->LeastRequiredDelayMs(); |
turaj@webrtc.org | e46c8d3 | 2013-05-22 20:39:43 +0000 | [diff] [blame] | 205 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 206 | |
| 207 | #endif // #ifdef WEBRTC_VOICE_ENGINE_VIDEO_SYNC_API |
| 208 | |
| 209 | } // namespace webrtc |