blob: 4dea6fa1696330aeb8a01cc2d8f9b5a567d3a8c7 [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) {
Jelena Marusic0d266052015-05-04 14:15:32 +020048 if (!_shared->statistics().Initialized()) {
49 _shared->SetLastError(VE_NOT_INITED, kTraceError);
50 return -1;
51 }
52 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
53 voe::Channel* channel_ptr = ch.channel();
54 if (channel_ptr == NULL) {
55 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
56 "GetPlayoutTimestamp() failed to locate channel");
57 return -1;
58 }
59 return channel_ptr->GetPlayoutTimestamp(timestamp);
niklase@google.com470e71d2011-07-07 08:21:25 +000060}
61
Jelena Marusic0d266052015-05-04 14:15:32 +020062int VoEVideoSyncImpl::SetInitTimestamp(int channel, unsigned int timestamp) {
63 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
64 "SetInitTimestamp(channel=%d, timestamp=%lu)", channel,
65 timestamp);
niklase@google.com470e71d2011-07-07 08:21:25 +000066
Jelena Marusic0d266052015-05-04 14:15:32 +020067 if (!_shared->statistics().Initialized()) {
68 _shared->SetLastError(VE_NOT_INITED, kTraceError);
69 return -1;
70 }
71 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
72 voe::Channel* channelPtr = ch.channel();
73 if (channelPtr == NULL) {
74 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
75 "SetInitTimestamp() failed to locate channel");
76 return -1;
77 }
78 return channelPtr->SetInitTimestamp(timestamp);
niklase@google.com470e71d2011-07-07 08:21:25 +000079}
80
Jelena Marusic0d266052015-05-04 14:15:32 +020081int VoEVideoSyncImpl::SetInitSequenceNumber(int channel, short sequenceNumber) {
82 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
83 "SetInitSequenceNumber(channel=%d, sequenceNumber=%hd)", channel,
84 sequenceNumber);
niklase@google.com470e71d2011-07-07 08:21:25 +000085
Jelena Marusic0d266052015-05-04 14:15:32 +020086 if (!_shared->statistics().Initialized()) {
87 _shared->SetLastError(VE_NOT_INITED, kTraceError);
88 return -1;
89 }
90 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
91 voe::Channel* channelPtr = ch.channel();
92 if (channelPtr == NULL) {
93 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
94 "SetInitSequenceNumber() failed to locate channel");
95 return -1;
96 }
97 return channelPtr->SetInitSequenceNumber(sequenceNumber);
niklase@google.com470e71d2011-07-07 08:21:25 +000098}
99
Jelena Marusic0d266052015-05-04 14:15:32 +0200100int VoEVideoSyncImpl::SetMinimumPlayoutDelay(int channel, int delayMs) {
101 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
102 "SetMinimumPlayoutDelay(channel=%d, delayMs=%d)", channel,
103 delayMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000104
Jelena Marusic0d266052015-05-04 14:15:32 +0200105 if (!_shared->statistics().Initialized()) {
106 _shared->SetLastError(VE_NOT_INITED, kTraceError);
107 return -1;
108 }
109 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
110 voe::Channel* channelPtr = ch.channel();
111 if (channelPtr == NULL) {
112 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
113 "SetMinimumPlayoutDelay() failed to locate channel");
114 return -1;
115 }
116 return channelPtr->SetMinimumPlayoutDelay(delayMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000117}
118
Jelena Marusic0d266052015-05-04 14:15:32 +0200119int VoEVideoSyncImpl::SetInitialPlayoutDelay(int channel, int delay_ms) {
120 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
121 "SetInitialPlayoutDelay(channel=%d, delay_ms=%d)", channel,
122 delay_ms);
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000123
Jelena Marusic0d266052015-05-04 14:15:32 +0200124 if (!_shared->statistics().Initialized()) {
125 _shared->SetLastError(VE_NOT_INITED, kTraceError);
126 return -1;
127 }
128 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
129 voe::Channel* channelPtr = ch.channel();
130 if (channelPtr == NULL) {
131 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
132 "SetInitialPlayoutDelay() failed to locate channel");
133 return -1;
134 }
135 return channelPtr->SetInitialPlayoutDelay(delay_ms);
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +0000136}
137
pwestin@webrtc.org1de01352013-04-11 20:23:35 +0000138int VoEVideoSyncImpl::GetDelayEstimate(int channel,
139 int* jitter_buffer_delay_ms,
140 int* playout_buffer_delay_ms) {
pwestin@webrtc.org1de01352013-04-11 20:23:35 +0000141 if (!_shared->statistics().Initialized()) {
142 _shared->SetLastError(VE_NOT_INITED, kTraceError);
143 return -1;
144 }
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000145 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
146 voe::Channel* channelPtr = ch.channel();
pwestin@webrtc.org1de01352013-04-11 20:23:35 +0000147 if (channelPtr == NULL) {
148 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
149 "GetDelayEstimate() failed to locate channel");
150 return -1;
151 }
152 if (!channelPtr->GetDelayEstimate(jitter_buffer_delay_ms,
153 playout_buffer_delay_ms)) {
154 return -1;
155 }
156 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000157}
158
Jelena Marusic0d266052015-05-04 14:15:32 +0200159int VoEVideoSyncImpl::GetPlayoutBufferSize(int& bufferMs) {
Jelena Marusic0d266052015-05-04 14:15:32 +0200160 if (!_shared->statistics().Initialized()) {
161 _shared->SetLastError(VE_NOT_INITED, kTraceError);
162 return -1;
163 }
164 AudioDeviceModule::BufferType type(AudioDeviceModule::kFixedBufferSize);
165 uint16_t sizeMS(0);
166 if (_shared->audio_device()->PlayoutBuffer(&type, &sizeMS) != 0) {
167 _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError,
168 "GetPlayoutBufferSize() failed to read buffer size");
169 return -1;
170 }
171 bufferMs = sizeMS;
Jelena Marusic0d266052015-05-04 14:15:32 +0200172 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000173}
174
Jelena Marusic0d266052015-05-04 14:15:32 +0200175int VoEVideoSyncImpl::GetRtpRtcp(int channel,
176 RtpRtcp** rtpRtcpModule,
177 RtpReceiver** rtp_receiver) {
Jelena Marusic0d266052015-05-04 14:15:32 +0200178 if (!_shared->statistics().Initialized()) {
179 _shared->SetLastError(VE_NOT_INITED, kTraceError);
180 return -1;
181 }
182 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
183 voe::Channel* channelPtr = ch.channel();
184 if (channelPtr == NULL) {
185 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
186 "GetPlayoutTimestamp() failed to locate channel");
187 return -1;
188 }
189 return channelPtr->GetRtpRtcp(rtpRtcpModule, rtp_receiver);
niklase@google.com470e71d2011-07-07 08:21:25 +0000190}
191
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +0000192int VoEVideoSyncImpl::GetLeastRequiredDelayMs(int channel) const {
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +0000193 if (!_shared->statistics().Initialized()) {
194 _shared->SetLastError(VE_NOT_INITED, kTraceError);
195 return -1;
196 }
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000197 voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
198 voe::Channel* channel_ptr = ch.channel();
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +0000199 if (channel_ptr == NULL) {
200 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
201 "GetLeastRequiredDelayMs() failed to locate channel");
202 return -1;
203 }
deadbeef74375882015-08-13 12:09:10 -0700204 return channel_ptr->LeastRequiredDelayMs();
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +0000205}
niklase@google.com470e71d2011-07-07 08:21:25 +0000206
207#endif // #ifdef WEBRTC_VOICE_ENGINE_VIDEO_SYNC_API
208
209} // namespace webrtc