2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
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.
11 #include "webrtc/voice_engine/voe_video_sync_impl.h"
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"
21 VoEVideoSync* VoEVideoSync::GetInterface(VoiceEngine* voiceEngine)
23 #ifndef WEBRTC_VOICE_ENGINE_VIDEO_SYNC_API
26 if (NULL == voiceEngine)
30 VoiceEngineImpl* s = static_cast<VoiceEngineImpl*>(voiceEngine);
36 #ifdef WEBRTC_VOICE_ENGINE_VIDEO_SYNC_API
38 VoEVideoSyncImpl::VoEVideoSyncImpl(voe::SharedData* shared) : _shared(shared)
40 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_shared->instance_id(), -1),
41 "VoEVideoSyncImpl::VoEVideoSyncImpl() - ctor");
44 VoEVideoSyncImpl::~VoEVideoSyncImpl()
46 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_shared->instance_id(), -1),
47 "VoEVideoSyncImpl::~VoEVideoSyncImpl() - dtor");
50 int VoEVideoSyncImpl::GetPlayoutTimestamp(int channel, unsigned int& timestamp)
52 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
53 "GetPlayoutTimestamp(channel=%d, timestamp=?)", channel);
55 if (!_shared->statistics().Initialized())
57 _shared->SetLastError(VE_NOT_INITED, kTraceError);
60 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
61 voe::Channel* channel_ptr = ch.channel();
62 if (channel_ptr == NULL)
64 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
65 "GetPlayoutTimestamp() failed to locate channel");
68 return channel_ptr->GetPlayoutTimestamp(timestamp);
71 int VoEVideoSyncImpl::SetInitTimestamp(int channel,
72 unsigned int timestamp)
74 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
75 "SetInitTimestamp(channel=%d, timestamp=%lu)",
78 if (!_shared->statistics().Initialized())
80 _shared->SetLastError(VE_NOT_INITED, kTraceError);
83 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
84 voe::Channel* channelPtr = ch.channel();
85 if (channelPtr == NULL)
87 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
88 "SetInitTimestamp() failed to locate channel");
91 return channelPtr->SetInitTimestamp(timestamp);
94 int VoEVideoSyncImpl::SetInitSequenceNumber(int channel,
97 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
98 "SetInitSequenceNumber(channel=%d, sequenceNumber=%hd)",
99 channel, sequenceNumber);
101 if (!_shared->statistics().Initialized())
103 _shared->SetLastError(VE_NOT_INITED, kTraceError);
106 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
107 voe::Channel* channelPtr = ch.channel();
108 if (channelPtr == NULL)
110 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
111 "SetInitSequenceNumber() failed to locate channel");
114 return channelPtr->SetInitSequenceNumber(sequenceNumber);
117 int VoEVideoSyncImpl::SetMinimumPlayoutDelay(int channel,int delayMs)
119 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
120 "SetMinimumPlayoutDelay(channel=%d, delayMs=%d)",
123 if (!_shared->statistics().Initialized())
125 _shared->SetLastError(VE_NOT_INITED, kTraceError);
128 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
129 voe::Channel* channelPtr = ch.channel();
130 if (channelPtr == NULL)
132 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
133 "SetMinimumPlayoutDelay() failed to locate channel");
136 return channelPtr->SetMinimumPlayoutDelay(delayMs);
139 int VoEVideoSyncImpl::SetInitialPlayoutDelay(int channel, int delay_ms)
141 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
142 "SetInitialPlayoutDelay(channel=%d, delay_ms=%d)",
145 if (!_shared->statistics().Initialized())
147 _shared->SetLastError(VE_NOT_INITED, kTraceError);
150 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
151 voe::Channel* channelPtr = ch.channel();
152 if (channelPtr == NULL)
154 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
155 "SetInitialPlayoutDelay() failed to locate channel");
158 return channelPtr->SetInitialPlayoutDelay(delay_ms);
161 int VoEVideoSyncImpl::GetDelayEstimate(int channel,
162 int* jitter_buffer_delay_ms,
163 int* playout_buffer_delay_ms) {
164 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
165 "GetDelayEstimate(channel=%d, delayMs=?)", channel);
167 if (!_shared->statistics().Initialized()) {
168 _shared->SetLastError(VE_NOT_INITED, kTraceError);
171 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
172 voe::Channel* channelPtr = ch.channel();
173 if (channelPtr == NULL) {
174 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
175 "GetDelayEstimate() failed to locate channel");
178 if (!channelPtr->GetDelayEstimate(jitter_buffer_delay_ms,
179 playout_buffer_delay_ms)) {
185 int VoEVideoSyncImpl::GetPlayoutBufferSize(int& bufferMs)
187 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
188 "GetPlayoutBufferSize(bufferMs=?)");
190 if (!_shared->statistics().Initialized())
192 _shared->SetLastError(VE_NOT_INITED, kTraceError);
195 AudioDeviceModule::BufferType type
196 (AudioDeviceModule::kFixedBufferSize);
198 if (_shared->audio_device()->PlayoutBuffer(&type, &sizeMS) != 0)
200 _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError,
201 "GetPlayoutBufferSize() failed to read buffer size");
205 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
206 VoEId(_shared->instance_id(), -1),
207 "GetPlayoutBufferSize() => bufferMs=%d", bufferMs);
211 int VoEVideoSyncImpl::GetRtpRtcp(int channel, RtpRtcp** rtpRtcpModule,
212 RtpReceiver** rtp_receiver)
214 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
215 "GetRtpRtcp(channel=%i)", channel);
217 if (!_shared->statistics().Initialized())
219 _shared->SetLastError(VE_NOT_INITED, kTraceError);
222 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
223 voe::Channel* channelPtr = ch.channel();
224 if (channelPtr == NULL)
226 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
227 "GetPlayoutTimestamp() failed to locate channel");
230 return channelPtr->GetRtpRtcp(rtpRtcpModule, rtp_receiver);
233 int VoEVideoSyncImpl::GetLeastRequiredDelayMs(int channel) const {
234 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
235 "GetLeastRequiredDelayMS(channel=%d)", channel);
237 if (!_shared->statistics().Initialized()) {
238 _shared->SetLastError(VE_NOT_INITED, kTraceError);
241 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
242 voe::Channel* channel_ptr = ch.channel();
243 if (channel_ptr == NULL) {
244 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
245 "GetLeastRequiredDelayMs() failed to locate channel");
248 return channel_ptr->least_required_delay_ms();
251 #endif // #ifdef WEBRTC_VOICE_ENGINE_VIDEO_SYNC_API
253 } // namespace webrtc