blob: 91c0750352ba4bc400f2c329035bb06f38097554 [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);
sjlee@webrtc.org4b425082012-09-10 17:58:21 +000054 IPHONE_NOT_SUPPORTED(_shared->statistics());
niklase@google.com470e71d2011-07-07 08:21:25 +000055
tommi@webrtc.org851becd2012-04-04 14:57:19 +000056 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +000057 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +000058 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +000059 return -1;
60 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +000061 voe::ScopedChannel sc(_shared->channel_manager(), channel);
tommi@webrtc.org9ff87db2012-01-19 15:05:36 +000062 voe::Channel* channelPtr = sc.ChannelPtr();
niklase@google.com470e71d2011-07-07 08:21:25 +000063 if (channelPtr == NULL)
64 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +000065 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
niklase@google.com470e71d2011-07-07 08:21:25 +000066 "GetPlayoutTimestamp() failed to locate channel");
67 return -1;
68 }
69 return channelPtr->GetPlayoutTimestamp(timestamp);
70}
71
72int VoEVideoSyncImpl::SetInitTimestamp(int channel,
73 unsigned int timestamp)
74{
tommi@webrtc.org851becd2012-04-04 14:57:19 +000075 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +000076 "SetInitTimestamp(channel=%d, timestamp=%lu)",
77 channel, timestamp);
sjlee@webrtc.org4b425082012-09-10 17:58:21 +000078 IPHONE_NOT_SUPPORTED(_shared->statistics());
niklase@google.com470e71d2011-07-07 08:21:25 +000079
tommi@webrtc.org851becd2012-04-04 14:57:19 +000080 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +000081 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +000082 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +000083 return -1;
84 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +000085 voe::ScopedChannel sc(_shared->channel_manager(), channel);
niklase@google.com470e71d2011-07-07 08:21:25 +000086 voe::Channel* channelPtr = sc.ChannelPtr();
87 if (channelPtr == NULL)
88 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +000089 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
niklase@google.com470e71d2011-07-07 08:21:25 +000090 "SetInitTimestamp() failed to locate channel");
91 return -1;
92 }
93 return channelPtr->SetInitTimestamp(timestamp);
94}
95
96int VoEVideoSyncImpl::SetInitSequenceNumber(int channel,
97 short sequenceNumber)
98{
tommi@webrtc.org851becd2012-04-04 14:57:19 +000099 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000100 "SetInitSequenceNumber(channel=%d, sequenceNumber=%hd)",
101 channel, sequenceNumber);
sjlee@webrtc.org4b425082012-09-10 17:58:21 +0000102 IPHONE_NOT_SUPPORTED(_shared->statistics());
niklase@google.com470e71d2011-07-07 08:21:25 +0000103
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000104 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +0000105 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000106 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +0000107 return -1;
108 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000109 voe::ScopedChannel sc(_shared->channel_manager(), channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000110 voe::Channel* channelPtr = sc.ChannelPtr();
111 if (channelPtr == NULL)
112 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000113 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
niklase@google.com470e71d2011-07-07 08:21:25 +0000114 "SetInitSequenceNumber() failed to locate channel");
115 return -1;
116 }
117 return channelPtr->SetInitSequenceNumber(sequenceNumber);
118}
119
120int VoEVideoSyncImpl::SetMinimumPlayoutDelay(int channel,int delayMs)
121{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000122 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000123 "SetMinimumPlayoutDelay(channel=%d, delayMs=%d)",
124 channel, delayMs);
sjlee@webrtc.org4b425082012-09-10 17:58:21 +0000125 IPHONE_NOT_SUPPORTED(_shared->statistics());
niklase@google.com470e71d2011-07-07 08:21:25 +0000126
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000127 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +0000128 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000129 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +0000130 return -1;
131 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000132 voe::ScopedChannel sc(_shared->channel_manager(), channel);
tommi@webrtc.org9ff87db2012-01-19 15:05:36 +0000133 voe::Channel* channelPtr = sc.ChannelPtr();
niklase@google.com470e71d2011-07-07 08:21:25 +0000134 if (channelPtr == NULL)
135 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000136 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
niklase@google.com470e71d2011-07-07 08:21:25 +0000137 "SetMinimumPlayoutDelay() failed to locate channel");
138 return -1;
139 }
140 return channelPtr->SetMinimumPlayoutDelay(delayMs);
141}
142
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000143int VoEVideoSyncImpl::SetInitialPlayoutDelay(int channel, int delay_ms)
144{
145 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
146 "SetInitialPlayoutDelay(channel=%d, delay_ms=%d)",
147 channel, delay_ms);
148 ANDROID_NOT_SUPPORTED(_shared->statistics());
149 IPHONE_NOT_SUPPORTED(_shared->statistics());
150
151 if (!_shared->statistics().Initialized())
152 {
153 _shared->SetLastError(VE_NOT_INITED, kTraceError);
154 return -1;
155 }
156 voe::ScopedChannel sc(_shared->channel_manager(), channel);
157 voe::Channel* channel_ptr = sc.ChannelPtr();
158 if (channel_ptr == NULL)
159 {
160 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
161 "SetInitialPlayoutDelay() failed to locate channel");
162 return -1;
163 }
164 return channel_ptr->SetInitialPlayoutDelay(delay_ms);
165}
166
pwestin@webrtc.org1de01352013-04-11 20:23:35 +0000167int VoEVideoSyncImpl::GetDelayEstimate(int channel,
168 int* jitter_buffer_delay_ms,
169 int* playout_buffer_delay_ms) {
170 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
171 "GetDelayEstimate(channel=%d, delayMs=?)", channel);
172 IPHONE_NOT_SUPPORTED(_shared->statistics());
niklase@google.com470e71d2011-07-07 08:21:25 +0000173
pwestin@webrtc.org1de01352013-04-11 20:23:35 +0000174 if (!_shared->statistics().Initialized()) {
175 _shared->SetLastError(VE_NOT_INITED, kTraceError);
176 return -1;
177 }
178 voe::ScopedChannel sc(_shared->channel_manager(), channel);
179 voe::Channel* channelPtr = sc.ChannelPtr();
180 if (channelPtr == NULL) {
181 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
182 "GetDelayEstimate() failed to locate channel");
183 return -1;
184 }
185 if (!channelPtr->GetDelayEstimate(jitter_buffer_delay_ms,
186 playout_buffer_delay_ms)) {
187 return -1;
188 }
189 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000190}
191
192int VoEVideoSyncImpl::GetPlayoutBufferSize(int& bufferMs)
193{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000194 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000195 "GetPlayoutBufferSize(bufferMs=?)");
sjlee@webrtc.org4b425082012-09-10 17:58:21 +0000196 IPHONE_NOT_SUPPORTED(_shared->statistics());
niklase@google.com470e71d2011-07-07 08:21:25 +0000197
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000198 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +0000199 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000200 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +0000201 return -1;
202 }
203 AudioDeviceModule::BufferType type
204 (AudioDeviceModule::kFixedBufferSize);
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000205 uint16_t sizeMS(0);
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000206 if (_shared->audio_device()->PlayoutBuffer(&type, &sizeMS) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +0000207 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000208 _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError,
tommi@webrtc.org9ff87db2012-01-19 15:05:36 +0000209 "GetPlayoutBufferSize() failed to read buffer size");
niklase@google.com470e71d2011-07-07 08:21:25 +0000210 return -1;
211 }
212 bufferMs = sizeMS;
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000213 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
214 VoEId(_shared->instance_id(), -1),
215 "GetPlayoutBufferSize() => bufferMs=%d", bufferMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000216 return 0;
217}
218
tommi@webrtc.org9ff87db2012-01-19 15:05:36 +0000219int VoEVideoSyncImpl::GetRtpRtcp(int channel, RtpRtcp* &rtpRtcpModule)
niklase@google.com470e71d2011-07-07 08:21:25 +0000220{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000221 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000222 "GetRtpRtcp(channel=%i)", channel);
fischman@webrtc.orgf61e02c2013-02-20 23:13:46 +0000223
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000224 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +0000225 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000226 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +0000227 return -1;
228 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000229 voe::ScopedChannel sc(_shared->channel_manager(), channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000230 voe::Channel* channelPtr = sc.ChannelPtr();
231 if (channelPtr == NULL)
232 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000233 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
niklase@google.com470e71d2011-07-07 08:21:25 +0000234 "GetPlayoutTimestamp() failed to locate channel");
235 return -1;
236 }
237 return channelPtr->GetRtpRtcp(rtpRtcpModule);
238}
239
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +0000240int VoEVideoSyncImpl::GetLeastRequiredDelayMs(int channel) const {
241 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
242 "GetLeastRequiredDelayMS(channel=%d)", channel);
243 IPHONE_NOT_SUPPORTED(_shared->statistics());
244
245 if (!_shared->statistics().Initialized()) {
246 _shared->SetLastError(VE_NOT_INITED, kTraceError);
247 return -1;
248 }
249 voe::ScopedChannel sc(_shared->channel_manager(), channel);
250 voe::Channel* channel_ptr = sc.ChannelPtr();
251 if (channel_ptr == NULL) {
252 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
253 "GetLeastRequiredDelayMs() failed to locate channel");
254 return -1;
255 }
256 return channel_ptr->least_required_delay_ms();
257}
niklase@google.com470e71d2011-07-07 08:21:25 +0000258
259#endif // #ifdef WEBRTC_VOICE_ENGINE_VIDEO_SYNC_API
260
261} // namespace webrtc