blob: f1c734764409c160f23a11357db46902f7d51bce [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
Jelena Marusic0d266052015-05-04 14:15:32 +020021VoEVideoSync* VoEVideoSync::GetInterface(VoiceEngine* voiceEngine) {
niklase@google.com470e71d2011-07-07 08:21:25 +000022#ifndef WEBRTC_VOICE_ENGINE_VIDEO_SYNC_API
Jelena Marusic0d266052015-05-04 14:15:32 +020023 return NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +000024#else
Jelena Marusic0d266052015-05-04 14:15:32 +020025 if (NULL == voiceEngine) {
26 return NULL;
27 }
28 VoiceEngineImpl* s = static_cast<VoiceEngineImpl*>(voiceEngine);
29 s->AddRef();
30 return s;
niklase@google.com470e71d2011-07-07 08:21:25 +000031#endif
32}
33
34#ifdef WEBRTC_VOICE_ENGINE_VIDEO_SYNC_API
35
Jelena Marusic0d266052015-05-04 14:15:32 +020036VoEVideoSyncImpl::VoEVideoSyncImpl(voe::SharedData* shared) : _shared(shared) {
37 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_shared->instance_id(), -1),
38 "VoEVideoSyncImpl::VoEVideoSyncImpl() - ctor");
niklase@google.com470e71d2011-07-07 08:21:25 +000039}
40
Jelena Marusic0d266052015-05-04 14:15:32 +020041VoEVideoSyncImpl::~VoEVideoSyncImpl() {
42 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_shared->instance_id(), -1),
43 "VoEVideoSyncImpl::~VoEVideoSyncImpl() - dtor");
niklase@google.com470e71d2011-07-07 08:21:25 +000044}
45
Jelena Marusic0d266052015-05-04 14:15:32 +020046int VoEVideoSyncImpl::GetPlayoutTimestamp(int channel,
47 unsigned int& timestamp) {
48 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
49 "GetPlayoutTimestamp(channel=%d, timestamp=?)", channel);
niklase@google.com470e71d2011-07-07 08:21:25 +000050
Jelena Marusic0d266052015-05-04 14:15:32 +020051 if (!_shared->statistics().Initialized()) {
52 _shared->SetLastError(VE_NOT_INITED, kTraceError);
53 return -1;
54 }
55 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
56 voe::Channel* channel_ptr = ch.channel();
57 if (channel_ptr == NULL) {
58 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
59 "GetPlayoutTimestamp() failed to locate channel");
60 return -1;
61 }
62 return channel_ptr->GetPlayoutTimestamp(timestamp);
niklase@google.com470e71d2011-07-07 08:21:25 +000063}
64
Jelena Marusic0d266052015-05-04 14:15:32 +020065int VoEVideoSyncImpl::SetInitTimestamp(int channel, unsigned int timestamp) {
66 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
67 "SetInitTimestamp(channel=%d, timestamp=%lu)", channel,
68 timestamp);
niklase@google.com470e71d2011-07-07 08:21:25 +000069
Jelena Marusic0d266052015-05-04 14:15:32 +020070 if (!_shared->statistics().Initialized()) {
71 _shared->SetLastError(VE_NOT_INITED, kTraceError);
72 return -1;
73 }
74 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
75 voe::Channel* channelPtr = ch.channel();
76 if (channelPtr == NULL) {
77 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
78 "SetInitTimestamp() failed to locate channel");
79 return -1;
80 }
81 return channelPtr->SetInitTimestamp(timestamp);
niklase@google.com470e71d2011-07-07 08:21:25 +000082}
83
Jelena Marusic0d266052015-05-04 14:15:32 +020084int VoEVideoSyncImpl::SetInitSequenceNumber(int channel, short sequenceNumber) {
85 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
86 "SetInitSequenceNumber(channel=%d, sequenceNumber=%hd)", channel,
87 sequenceNumber);
niklase@google.com470e71d2011-07-07 08:21:25 +000088
Jelena Marusic0d266052015-05-04 14:15:32 +020089 if (!_shared->statistics().Initialized()) {
90 _shared->SetLastError(VE_NOT_INITED, kTraceError);
91 return -1;
92 }
93 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
94 voe::Channel* channelPtr = ch.channel();
95 if (channelPtr == NULL) {
96 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
97 "SetInitSequenceNumber() failed to locate channel");
98 return -1;
99 }
100 return channelPtr->SetInitSequenceNumber(sequenceNumber);
niklase@google.com470e71d2011-07-07 08:21:25 +0000101}
102
Jelena Marusic0d266052015-05-04 14:15:32 +0200103int VoEVideoSyncImpl::SetMinimumPlayoutDelay(int channel, int delayMs) {
104 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
105 "SetMinimumPlayoutDelay(channel=%d, delayMs=%d)", channel,
106 delayMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000107
Jelena Marusic0d266052015-05-04 14:15:32 +0200108 if (!_shared->statistics().Initialized()) {
109 _shared->SetLastError(VE_NOT_INITED, kTraceError);
110 return -1;
111 }
112 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
113 voe::Channel* channelPtr = ch.channel();
114 if (channelPtr == NULL) {
115 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
116 "SetMinimumPlayoutDelay() failed to locate channel");
117 return -1;
118 }
119 return channelPtr->SetMinimumPlayoutDelay(delayMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000120}
121
Jelena Marusic0d266052015-05-04 14:15:32 +0200122int VoEVideoSyncImpl::SetInitialPlayoutDelay(int channel, int delay_ms) {
123 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
124 "SetInitialPlayoutDelay(channel=%d, delay_ms=%d)", channel,
125 delay_ms);
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000126
Jelena Marusic0d266052015-05-04 14:15:32 +0200127 if (!_shared->statistics().Initialized()) {
128 _shared->SetLastError(VE_NOT_INITED, kTraceError);
129 return -1;
130 }
131 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
132 voe::Channel* channelPtr = ch.channel();
133 if (channelPtr == NULL) {
134 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
135 "SetInitialPlayoutDelay() failed to locate channel");
136 return -1;
137 }
138 return channelPtr->SetInitialPlayoutDelay(delay_ms);
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000139}
140
pwestin@webrtc.org1de01352013-04-11 20:23:35 +0000141int VoEVideoSyncImpl::GetDelayEstimate(int channel,
142 int* jitter_buffer_delay_ms,
143 int* playout_buffer_delay_ms) {
144 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
145 "GetDelayEstimate(channel=%d, delayMs=?)", channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000146
pwestin@webrtc.org1de01352013-04-11 20:23:35 +0000147 if (!_shared->statistics().Initialized()) {
148 _shared->SetLastError(VE_NOT_INITED, kTraceError);
149 return -1;
150 }
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000151 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
152 voe::Channel* channelPtr = ch.channel();
pwestin@webrtc.org1de01352013-04-11 20:23:35 +0000153 if (channelPtr == NULL) {
154 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
155 "GetDelayEstimate() failed to locate channel");
156 return -1;
157 }
158 if (!channelPtr->GetDelayEstimate(jitter_buffer_delay_ms,
159 playout_buffer_delay_ms)) {
160 return -1;
161 }
162 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000163}
164
Jelena Marusic0d266052015-05-04 14:15:32 +0200165int VoEVideoSyncImpl::GetPlayoutBufferSize(int& bufferMs) {
166 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000167 "GetPlayoutBufferSize(bufferMs=?)");
niklase@google.com470e71d2011-07-07 08:21:25 +0000168
Jelena Marusic0d266052015-05-04 14:15:32 +0200169 if (!_shared->statistics().Initialized()) {
170 _shared->SetLastError(VE_NOT_INITED, kTraceError);
171 return -1;
172 }
173 AudioDeviceModule::BufferType type(AudioDeviceModule::kFixedBufferSize);
174 uint16_t sizeMS(0);
175 if (_shared->audio_device()->PlayoutBuffer(&type, &sizeMS) != 0) {
176 _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError,
177 "GetPlayoutBufferSize() failed to read buffer size");
178 return -1;
179 }
180 bufferMs = sizeMS;
181 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_shared->instance_id(), -1),
182 "GetPlayoutBufferSize() => bufferMs=%d", bufferMs);
183 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000184}
185
Jelena Marusic0d266052015-05-04 14:15:32 +0200186int VoEVideoSyncImpl::GetRtpRtcp(int channel,
187 RtpRtcp** rtpRtcpModule,
188 RtpReceiver** rtp_receiver) {
189 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
190 "GetRtpRtcp(channel=%i)", channel);
fischman@webrtc.orgf61e02c2013-02-20 23:13:46 +0000191
Jelena Marusic0d266052015-05-04 14:15:32 +0200192 if (!_shared->statistics().Initialized()) {
193 _shared->SetLastError(VE_NOT_INITED, kTraceError);
194 return -1;
195 }
196 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
197 voe::Channel* channelPtr = ch.channel();
198 if (channelPtr == NULL) {
199 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
200 "GetPlayoutTimestamp() failed to locate channel");
201 return -1;
202 }
203 return channelPtr->GetRtpRtcp(rtpRtcpModule, rtp_receiver);
niklase@google.com470e71d2011-07-07 08:21:25 +0000204}
205
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +0000206int VoEVideoSyncImpl::GetLeastRequiredDelayMs(int channel) const {
207 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
208 "GetLeastRequiredDelayMS(channel=%d)", channel);
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +0000209
210 if (!_shared->statistics().Initialized()) {
211 _shared->SetLastError(VE_NOT_INITED, kTraceError);
212 return -1;
213 }
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000214 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
215 voe::Channel* channel_ptr = ch.channel();
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +0000216 if (channel_ptr == NULL) {
217 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
218 "GetLeastRequiredDelayMs() failed to locate channel");
219 return -1;
220 }
221 return channel_ptr->least_required_delay_ms();
222}
niklase@google.com470e71d2011-07-07 08:21:25 +0000223
224#endif // #ifdef WEBRTC_VOICE_ENGINE_VIDEO_SYNC_API
225
226} // namespace webrtc