1/*
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
11#include "webrtc/voice_engine/voe_video_sync_impl.h"
12
13#include "webrtc/system_wrappers/include/critical_section_wrapper.h"
14#include "webrtc/system_wrappers/include/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"
18
19namespace webrtc {
20
21VoEVideoSync* VoEVideoSync::GetInterface(VoiceEngine* voiceEngine) {
22#ifndef WEBRTC_VOICE_ENGINE_VIDEO_SYNC_API
23  return NULL;
24#else
25  if (NULL == voiceEngine) {
26    return NULL;
27  }
28  VoiceEngineImpl* s = static_cast<VoiceEngineImpl*>(voiceEngine);
29  s->AddRef();
30  return s;
31#endif
32}
33
34#ifdef WEBRTC_VOICE_ENGINE_VIDEO_SYNC_API
35
36VoEVideoSyncImpl::VoEVideoSyncImpl(voe::SharedData* shared) : _shared(shared) {
37  WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_shared->instance_id(), -1),
38               "VoEVideoSyncImpl::VoEVideoSyncImpl() - ctor");
39}
40
41VoEVideoSyncImpl::~VoEVideoSyncImpl() {
42  WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_shared->instance_id(), -1),
43               "VoEVideoSyncImpl::~VoEVideoSyncImpl() - dtor");
44}
45
46int VoEVideoSyncImpl::GetPlayoutTimestamp(int channel,
47                                          unsigned int& timestamp) {
48  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);
60}
61
62int 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);
66
67  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);
79}
80
81int 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);
85
86  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);
98}
99
100int 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);
104
105  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);
117}
118
119int VoEVideoSyncImpl::GetDelayEstimate(int channel,
120                                       int* jitter_buffer_delay_ms,
121                                       int* playout_buffer_delay_ms) {
122  if (!_shared->statistics().Initialized()) {
123    _shared->SetLastError(VE_NOT_INITED, kTraceError);
124    return -1;
125  }
126  voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
127  voe::Channel* channelPtr = ch.channel();
128  if (channelPtr == NULL) {
129    _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
130                          "GetDelayEstimate() failed to locate channel");
131    return -1;
132  }
133  if (!channelPtr->GetDelayEstimate(jitter_buffer_delay_ms,
134                                    playout_buffer_delay_ms)) {
135    return -1;
136  }
137  return 0;
138}
139
140int VoEVideoSyncImpl::GetPlayoutBufferSize(int& bufferMs) {
141  if (!_shared->statistics().Initialized()) {
142    _shared->SetLastError(VE_NOT_INITED, kTraceError);
143    return -1;
144  }
145  AudioDeviceModule::BufferType type(AudioDeviceModule::kFixedBufferSize);
146  uint16_t sizeMS(0);
147  if (_shared->audio_device()->PlayoutBuffer(&type, &sizeMS) != 0) {
148    _shared->SetLastError(VE_AUDIO_DEVICE_MODULE_ERROR, kTraceError,
149                          "GetPlayoutBufferSize() failed to read buffer size");
150    return -1;
151  }
152  bufferMs = sizeMS;
153  return 0;
154}
155
156int VoEVideoSyncImpl::GetRtpRtcp(int channel,
157                                 RtpRtcp** rtpRtcpModule,
158                                 RtpReceiver** rtp_receiver) {
159  if (!_shared->statistics().Initialized()) {
160    _shared->SetLastError(VE_NOT_INITED, kTraceError);
161    return -1;
162  }
163  voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
164  voe::Channel* channelPtr = ch.channel();
165  if (channelPtr == NULL) {
166    _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
167                          "GetPlayoutTimestamp() failed to locate channel");
168    return -1;
169  }
170  return channelPtr->GetRtpRtcp(rtpRtcpModule, rtp_receiver);
171}
172
173int VoEVideoSyncImpl::GetLeastRequiredDelayMs(int channel) const {
174  if (!_shared->statistics().Initialized()) {
175    _shared->SetLastError(VE_NOT_INITED, kTraceError);
176    return -1;
177  }
178  voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
179  voe::Channel* channel_ptr = ch.channel();
180  if (channel_ptr == NULL) {
181    _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
182                          "GetLeastRequiredDelayMs() failed to locate channel");
183    return -1;
184  }
185  return channel_ptr->LeastRequiredDelayMs();
186}
187
188#endif  // #ifdef WEBRTC_VOICE_ENGINE_VIDEO_SYNC_API
189
190}  // namespace webrtc
191