blob: 4645e2529f338e461aeafd527c4f521950ae6b9f [file] [log] [blame]
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001/*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
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.org471ae722013-05-21 13:52:32 +000011#include "webrtc/voice_engine/voe_video_sync_impl.h"
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000012
turaj@webrtc.orgead8a5b2013-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"
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +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.orgb9e5a3d2013-02-15 15:07:32 +000030 VoiceEngineImpl* s = static_cast<VoiceEngineImpl*>(voiceEngine);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000031 s->AddRef();
32 return s;
33#endif
34}
35
36#ifdef WEBRTC_VOICE_ENGINE_VIDEO_SYNC_API
37
38VoEVideoSyncImpl::VoEVideoSyncImpl(voe::SharedData* shared) : _shared(shared)
39{
40 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_shared->instance_id(), -1),
41 "VoEVideoSyncImpl::VoEVideoSyncImpl() - ctor");
42}
43
44VoEVideoSyncImpl::~VoEVideoSyncImpl()
45{
46 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_shared->instance_id(), -1),
47 "VoEVideoSyncImpl::~VoEVideoSyncImpl() - dtor");
48}
49
50int VoEVideoSyncImpl::GetPlayoutTimestamp(int channel, unsigned int& timestamp)
51{
52 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
53 "GetPlayoutTimestamp(channel=%d, timestamp=?)", channel);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000054 IPHONE_NOT_SUPPORTED(_shared->statistics());
55
56 if (!_shared->statistics().Initialized())
57 {
58 _shared->SetLastError(VE_NOT_INITED, kTraceError);
59 return -1;
60 }
pbos@webrtc.orgb3ada152013-08-07 17:57:36 +000061 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
62 voe::Channel* channel_ptr = ch.channel();
63 if (channel_ptr == NULL)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000064 {
65 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
66 "GetPlayoutTimestamp() failed to locate channel");
67 return -1;
68 }
pbos@webrtc.orgb3ada152013-08-07 17:57:36 +000069 return channel_ptr->GetPlayoutTimestamp(timestamp);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000070}
71
72int VoEVideoSyncImpl::SetInitTimestamp(int channel,
73 unsigned int timestamp)
74{
75 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
76 "SetInitTimestamp(channel=%d, timestamp=%lu)",
77 channel, timestamp);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000078 IPHONE_NOT_SUPPORTED(_shared->statistics());
79
80 if (!_shared->statistics().Initialized())
81 {
82 _shared->SetLastError(VE_NOT_INITED, kTraceError);
83 return -1;
84 }
pbos@webrtc.orgb3ada152013-08-07 17:57:36 +000085 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
86 voe::Channel* channelPtr = ch.channel();
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000087 if (channelPtr == NULL)
88 {
89 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
90 "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{
99 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
100 "SetInitSequenceNumber(channel=%d, sequenceNumber=%hd)",
101 channel, sequenceNumber);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000102 IPHONE_NOT_SUPPORTED(_shared->statistics());
103
104 if (!_shared->statistics().Initialized())
105 {
106 _shared->SetLastError(VE_NOT_INITED, kTraceError);
107 return -1;
108 }
pbos@webrtc.orgb3ada152013-08-07 17:57:36 +0000109 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
110 voe::Channel* channelPtr = ch.channel();
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000111 if (channelPtr == NULL)
112 {
113 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
114 "SetInitSequenceNumber() failed to locate channel");
115 return -1;
116 }
117 return channelPtr->SetInitSequenceNumber(sequenceNumber);
118}
119
120int VoEVideoSyncImpl::SetMinimumPlayoutDelay(int channel,int delayMs)
121{
122 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
123 "SetMinimumPlayoutDelay(channel=%d, delayMs=%d)",
124 channel, delayMs);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000125 IPHONE_NOT_SUPPORTED(_shared->statistics());
126
127 if (!_shared->statistics().Initialized())
128 {
129 _shared->SetLastError(VE_NOT_INITED, kTraceError);
130 return -1;
131 }
pbos@webrtc.orgb3ada152013-08-07 17:57:36 +0000132 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
133 voe::Channel* channelPtr = ch.channel();
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000134 if (channelPtr == NULL)
135 {
136 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
137 "SetMinimumPlayoutDelay() failed to locate channel");
138 return -1;
139 }
140 return channelPtr->SetMinimumPlayoutDelay(delayMs);
141}
142
turaj@webrtc.orgead8a5b2013-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);
turaj@webrtc.orgead8a5b2013-02-12 21:42:18 +0000148 IPHONE_NOT_SUPPORTED(_shared->statistics());
149
150 if (!_shared->statistics().Initialized())
151 {
152 _shared->SetLastError(VE_NOT_INITED, kTraceError);
153 return -1;
154 }
pbos@webrtc.orgb3ada152013-08-07 17:57:36 +0000155 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
156 voe::Channel* channelPtr = ch.channel();
157 if (channelPtr == NULL)
turaj@webrtc.orgead8a5b2013-02-12 21:42:18 +0000158 {
159 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
160 "SetInitialPlayoutDelay() failed to locate channel");
161 return -1;
162 }
pbos@webrtc.orgb3ada152013-08-07 17:57:36 +0000163 return channelPtr->SetInitialPlayoutDelay(delay_ms);
turaj@webrtc.orgead8a5b2013-02-12 21:42:18 +0000164}
165
pwestin@webrtc.orgf2724972013-04-11 20:23:35 +0000166int VoEVideoSyncImpl::GetDelayEstimate(int channel,
167 int* jitter_buffer_delay_ms,
168 int* playout_buffer_delay_ms) {
169 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
170 "GetDelayEstimate(channel=%d, delayMs=?)", channel);
171 IPHONE_NOT_SUPPORTED(_shared->statistics());
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000172
pwestin@webrtc.orgf2724972013-04-11 20:23:35 +0000173 if (!_shared->statistics().Initialized()) {
174 _shared->SetLastError(VE_NOT_INITED, kTraceError);
175 return -1;
176 }
pbos@webrtc.orgb3ada152013-08-07 17:57:36 +0000177 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
178 voe::Channel* channelPtr = ch.channel();
pwestin@webrtc.orgf2724972013-04-11 20:23:35 +0000179 if (channelPtr == NULL) {
180 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
181 "GetDelayEstimate() failed to locate channel");
182 return -1;
183 }
184 if (!channelPtr->GetDelayEstimate(jitter_buffer_delay_ms,
185 playout_buffer_delay_ms)) {
186 return -1;
187 }
188 return 0;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000189}
190
191int VoEVideoSyncImpl::GetPlayoutBufferSize(int& bufferMs)
192{
193 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
194 "GetPlayoutBufferSize(bufferMs=?)");
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000195 IPHONE_NOT_SUPPORTED(_shared->statistics());
196
197 if (!_shared->statistics().Initialized())
198 {
199 _shared->SetLastError(VE_NOT_INITED, kTraceError);
200 return -1;
201 }
202 AudioDeviceModule::BufferType type
203 (AudioDeviceModule::kFixedBufferSize);
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +0000204 uint16_t sizeMS(0);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000205 if (_shared->audio_device()->PlayoutBuffer(&type, &sizeMS) != 0)
206 {
207 _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError,
208 "GetPlayoutBufferSize() failed to read buffer size");
209 return -1;
210 }
211 bufferMs = sizeMS;
212 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
213 VoEId(_shared->instance_id(), -1),
214 "GetPlayoutBufferSize() => bufferMs=%d", bufferMs);
215 return 0;
216}
217
wu@webrtc.org7fc75bb2013-08-15 23:38:54 +0000218int VoEVideoSyncImpl::GetRtpRtcp(int channel, RtpRtcp** rtpRtcpModule,
219 RtpReceiver** rtp_receiver)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000220{
221 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
222 "GetRtpRtcp(channel=%i)", channel);
fischman@webrtc.org59b3a4c2013-02-20 23:13:46 +0000223
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000224 if (!_shared->statistics().Initialized())
225 {
226 _shared->SetLastError(VE_NOT_INITED, kTraceError);
227 return -1;
228 }
pbos@webrtc.orgb3ada152013-08-07 17:57:36 +0000229 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
230 voe::Channel* channelPtr = ch.channel();
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000231 if (channelPtr == NULL)
232 {
233 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
234 "GetPlayoutTimestamp() failed to locate channel");
235 return -1;
236 }
wu@webrtc.org7fc75bb2013-08-15 23:38:54 +0000237 return channelPtr->GetRtpRtcp(rtpRtcpModule, rtp_receiver);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000238}
239
turaj@webrtc.orgd5577342013-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 }
pbos@webrtc.orgb3ada152013-08-07 17:57:36 +0000249 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
250 voe::Channel* channel_ptr = ch.channel();
turaj@webrtc.orgd5577342013-05-22 20:39:43 +0000251 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}
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000258
259#endif // #ifdef WEBRTC_VOICE_ENGINE_VIDEO_SYNC_API
260
261} // namespace webrtc