blob: f4c5a6fe0bee6b9884fd245239efdc4cd942d201 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
tommi@webrtc.org851becd2012-04-04 14:57:19 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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.org956aa7e2013-05-21 13:52:32 +000011#include "webrtc/voice_engine/voe_video_sync_impl.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000013#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.com470e71d2011-07-07 08:21:25 +000018
19namespace webrtc {
20
21VoEVideoSync* VoEVideoSync::GetInterface(VoiceEngine* voiceEngine)
22{
23#ifndef WEBRTC_VOICE_ENGINE_VIDEO_SYNC_API
24 return NULL;
25#else
26 if (NULL == voiceEngine)
27 {
28 return NULL;
29 }
tommi@webrtc.org0989fb72013-02-15 15:07:32 +000030 VoiceEngineImpl* s = static_cast<VoiceEngineImpl*>(voiceEngine);
tommi@webrtc.orga990e122012-04-26 15:28:22 +000031 s->AddRef();
32 return s;
niklase@google.com470e71d2011-07-07 08:21:25 +000033#endif
34}
35
36#ifdef WEBRTC_VOICE_ENGINE_VIDEO_SYNC_API
37
tommi@webrtc.org851becd2012-04-04 14:57:19 +000038VoEVideoSyncImpl::VoEVideoSyncImpl(voe::SharedData* shared) : _shared(shared)
niklase@google.com470e71d2011-07-07 08:21:25 +000039{
tommi@webrtc.org851becd2012-04-04 14:57:19 +000040 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +000041 "VoEVideoSyncImpl::VoEVideoSyncImpl() - ctor");
42}
43
44VoEVideoSyncImpl::~VoEVideoSyncImpl()
45{
tommi@webrtc.org851becd2012-04-04 14:57:19 +000046 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +000047 "VoEVideoSyncImpl::~VoEVideoSyncImpl() - dtor");
48}
49
niklase@google.com470e71d2011-07-07 08:21:25 +000050int VoEVideoSyncImpl::GetPlayoutTimestamp(int channel, unsigned int& timestamp)
51{
tommi@webrtc.org851becd2012-04-04 14:57:19 +000052 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +000053 "GetPlayoutTimestamp(channel=%d, timestamp=?)", channel);
niklase@google.com470e71d2011-07-07 08:21:25 +000054
tommi@webrtc.org851becd2012-04-04 14:57:19 +000055 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +000056 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +000057 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +000058 return -1;
59 }
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000060 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
61 voe::Channel* channel_ptr = ch.channel();
62 if (channel_ptr == NULL)
niklase@google.com470e71d2011-07-07 08:21:25 +000063 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +000064 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
niklase@google.com470e71d2011-07-07 08:21:25 +000065 "GetPlayoutTimestamp() failed to locate channel");
66 return -1;
67 }
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000068 return channel_ptr->GetPlayoutTimestamp(timestamp);
niklase@google.com470e71d2011-07-07 08:21:25 +000069}
70
71int VoEVideoSyncImpl::SetInitTimestamp(int channel,
72 unsigned int timestamp)
73{
tommi@webrtc.org851becd2012-04-04 14:57:19 +000074 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +000075 "SetInitTimestamp(channel=%d, timestamp=%lu)",
76 channel, timestamp);
niklase@google.com470e71d2011-07-07 08:21:25 +000077
tommi@webrtc.org851becd2012-04-04 14:57:19 +000078 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +000079 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +000080 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +000081 return -1;
82 }
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000083 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
84 voe::Channel* channelPtr = ch.channel();
niklase@google.com470e71d2011-07-07 08:21:25 +000085 if (channelPtr == NULL)
86 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +000087 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
niklase@google.com470e71d2011-07-07 08:21:25 +000088 "SetInitTimestamp() failed to locate channel");
89 return -1;
90 }
91 return channelPtr->SetInitTimestamp(timestamp);
92}
93
94int VoEVideoSyncImpl::SetInitSequenceNumber(int channel,
95 short sequenceNumber)
96{
tommi@webrtc.org851becd2012-04-04 14:57:19 +000097 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +000098 "SetInitSequenceNumber(channel=%d, sequenceNumber=%hd)",
99 channel, sequenceNumber);
niklase@google.com470e71d2011-07-07 08:21:25 +0000100
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000101 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +0000102 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000103 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +0000104 return -1;
105 }
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000106 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
107 voe::Channel* channelPtr = ch.channel();
niklase@google.com470e71d2011-07-07 08:21:25 +0000108 if (channelPtr == NULL)
109 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000110 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
niklase@google.com470e71d2011-07-07 08:21:25 +0000111 "SetInitSequenceNumber() failed to locate channel");
112 return -1;
113 }
114 return channelPtr->SetInitSequenceNumber(sequenceNumber);
115}
116
117int VoEVideoSyncImpl::SetMinimumPlayoutDelay(int channel,int delayMs)
118{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000119 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000120 "SetMinimumPlayoutDelay(channel=%d, delayMs=%d)",
121 channel, delayMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000122
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000123 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +0000124 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000125 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +0000126 return -1;
127 }
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000128 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
129 voe::Channel* channelPtr = ch.channel();
niklase@google.com470e71d2011-07-07 08:21:25 +0000130 if (channelPtr == NULL)
131 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000132 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
niklase@google.com470e71d2011-07-07 08:21:25 +0000133 "SetMinimumPlayoutDelay() failed to locate channel");
134 return -1;
135 }
136 return channelPtr->SetMinimumPlayoutDelay(delayMs);
137}
138
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000139int VoEVideoSyncImpl::SetInitialPlayoutDelay(int channel, int delay_ms)
140{
141 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
142 "SetInitialPlayoutDelay(channel=%d, delay_ms=%d)",
143 channel, delay_ms);
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000144
145 if (!_shared->statistics().Initialized())
146 {
147 _shared->SetLastError(VE_NOT_INITED, kTraceError);
148 return -1;
149 }
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000150 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
151 voe::Channel* channelPtr = ch.channel();
152 if (channelPtr == NULL)
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000153 {
154 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
155 "SetInitialPlayoutDelay() failed to locate channel");
156 return -1;
157 }
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000158 return channelPtr->SetInitialPlayoutDelay(delay_ms);
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000159}
160
pwestin@webrtc.org1de01352013-04-11 20:23:35 +0000161int 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);
niklase@google.com470e71d2011-07-07 08:21:25 +0000166
pwestin@webrtc.org1de01352013-04-11 20:23:35 +0000167 if (!_shared->statistics().Initialized()) {
168 _shared->SetLastError(VE_NOT_INITED, kTraceError);
169 return -1;
170 }
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000171 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
172 voe::Channel* channelPtr = ch.channel();
pwestin@webrtc.org1de01352013-04-11 20:23:35 +0000173 if (channelPtr == NULL) {
174 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
175 "GetDelayEstimate() failed to locate channel");
176 return -1;
177 }
178 if (!channelPtr->GetDelayEstimate(jitter_buffer_delay_ms,
179 playout_buffer_delay_ms)) {
180 return -1;
181 }
182 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000183}
184
185int VoEVideoSyncImpl::GetPlayoutBufferSize(int& bufferMs)
186{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000187 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000188 "GetPlayoutBufferSize(bufferMs=?)");
niklase@google.com470e71d2011-07-07 08:21:25 +0000189
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000190 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +0000191 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000192 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +0000193 return -1;
194 }
195 AudioDeviceModule::BufferType type
196 (AudioDeviceModule::kFixedBufferSize);
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000197 uint16_t sizeMS(0);
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000198 if (_shared->audio_device()->PlayoutBuffer(&type, &sizeMS) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +0000199 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000200 _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError,
tommi@webrtc.org9ff87db2012-01-19 15:05:36 +0000201 "GetPlayoutBufferSize() failed to read buffer size");
niklase@google.com470e71d2011-07-07 08:21:25 +0000202 return -1;
203 }
204 bufferMs = sizeMS;
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000205 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
206 VoEId(_shared->instance_id(), -1),
207 "GetPlayoutBufferSize() => bufferMs=%d", bufferMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000208 return 0;
209}
210
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000211int VoEVideoSyncImpl::GetRtpRtcp(int channel, RtpRtcp** rtpRtcpModule,
212 RtpReceiver** rtp_receiver)
niklase@google.com470e71d2011-07-07 08:21:25 +0000213{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000214 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000215 "GetRtpRtcp(channel=%i)", channel);
fischman@webrtc.orgf61e02c2013-02-20 23:13:46 +0000216
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000217 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +0000218 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000219 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +0000220 return -1;
221 }
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000222 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
223 voe::Channel* channelPtr = ch.channel();
niklase@google.com470e71d2011-07-07 08:21:25 +0000224 if (channelPtr == NULL)
225 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000226 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
niklase@google.com470e71d2011-07-07 08:21:25 +0000227 "GetPlayoutTimestamp() failed to locate channel");
228 return -1;
229 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000230 return channelPtr->GetRtpRtcp(rtpRtcpModule, rtp_receiver);
niklase@google.com470e71d2011-07-07 08:21:25 +0000231}
232
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +0000233int VoEVideoSyncImpl::GetLeastRequiredDelayMs(int channel) const {
234 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
235 "GetLeastRequiredDelayMS(channel=%d)", channel);
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +0000236
237 if (!_shared->statistics().Initialized()) {
238 _shared->SetLastError(VE_NOT_INITED, kTraceError);
239 return -1;
240 }
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000241 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
242 voe::Channel* channel_ptr = ch.channel();
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +0000243 if (channel_ptr == NULL) {
244 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
245 "GetLeastRequiredDelayMs() failed to locate channel");
246 return -1;
247 }
248 return channel_ptr->least_required_delay_ms();
249}
niklase@google.com470e71d2011-07-07 08:21:25 +0000250
251#endif // #ifdef WEBRTC_VOICE_ENGINE_VIDEO_SYNC_API
252
253} // namespace webrtc