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/system_wrappers/include/file_wrapper.h"
12 #include "webrtc/system_wrappers/include/trace.h"
13 #include "webrtc/voice_engine/include/voe_errors.h"
14 #include "webrtc/voice_engine/voe_rtp_rtcp_impl.h"
15 #include "webrtc/voice_engine/voice_engine_impl.h"
16 
17 #include "webrtc/voice_engine/channel.h"
18 #include "webrtc/voice_engine/transmit_mixer.h"
19 
20 namespace webrtc {
21 
GetInterface(VoiceEngine * voiceEngine)22 VoERTP_RTCP* VoERTP_RTCP::GetInterface(VoiceEngine* voiceEngine) {
23   if (NULL == voiceEngine) {
24     return NULL;
25   }
26   VoiceEngineImpl* s = static_cast<VoiceEngineImpl*>(voiceEngine);
27   s->AddRef();
28   return s;
29 }
30 
VoERTP_RTCPImpl(voe::SharedData * shared)31 VoERTP_RTCPImpl::VoERTP_RTCPImpl(voe::SharedData* shared) : _shared(shared) {
32   WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_shared->instance_id(), -1),
33                "VoERTP_RTCPImpl::VoERTP_RTCPImpl() - ctor");
34 }
35 
~VoERTP_RTCPImpl()36 VoERTP_RTCPImpl::~VoERTP_RTCPImpl() {
37   WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_shared->instance_id(), -1),
38                "VoERTP_RTCPImpl::~VoERTP_RTCPImpl() - dtor");
39 }
40 
SetLocalMID(int channel,const char * mid)41 int VoERTP_RTCPImpl::SetLocalMID(int channel, const char* mid) {
42   WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
43                "SetLocalMID(channel=%d, %s)", channel, mid);
44   if (!_shared->statistics().Initialized()) {
45     _shared->SetLastError(VE_NOT_INITED, kTraceError);
46     return -1;
47   }
48   voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
49   voe::Channel* channelPtr = ch.channel();
50   if (channelPtr == NULL) {
51     _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
52                           "SetLocalMID() failed to locate channel");
53     return -1;
54   }
55   return channelPtr->SetLocalMID(mid);
56 }
57 
SetLocalSSRC(int channel,unsigned int ssrc)58 int VoERTP_RTCPImpl::SetLocalSSRC(int channel, unsigned int ssrc) {
59   WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
60                "SetLocalSSRC(channel=%d, %lu)", channel, ssrc);
61   if (!_shared->statistics().Initialized()) {
62     _shared->SetLastError(VE_NOT_INITED, kTraceError);
63     return -1;
64   }
65   voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
66   voe::Channel* channelPtr = ch.channel();
67   if (channelPtr == NULL) {
68     _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
69                           "SetLocalSSRC() failed to locate channel");
70     return -1;
71   }
72   return channelPtr->SetLocalSSRC(ssrc);
73 }
74 
GetLocalSSRC(int channel,unsigned int & ssrc)75 int VoERTP_RTCPImpl::GetLocalSSRC(int channel, unsigned int& ssrc) {
76   if (!_shared->statistics().Initialized()) {
77     _shared->SetLastError(VE_NOT_INITED, kTraceError);
78     return -1;
79   }
80   voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
81   voe::Channel* channelPtr = ch.channel();
82   if (channelPtr == NULL) {
83     _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
84                           "GetLocalSSRC() failed to locate channel");
85     return -1;
86   }
87   return channelPtr->GetLocalSSRC(ssrc);
88 }
89 
GetRemoteSSRC(int channel,unsigned int & ssrc)90 int VoERTP_RTCPImpl::GetRemoteSSRC(int channel, unsigned int& ssrc) {
91   if (!_shared->statistics().Initialized()) {
92     _shared->SetLastError(VE_NOT_INITED, kTraceError);
93     return -1;
94   }
95   voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
96   voe::Channel* channelPtr = ch.channel();
97   if (channelPtr == NULL) {
98     _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
99                           "GetRemoteSSRC() failed to locate channel");
100     return -1;
101   }
102   return channelPtr->GetRemoteSSRC(ssrc);
103 }
104 
SetSendAudioLevelIndicationStatus(int channel,bool enable,unsigned char id)105 int VoERTP_RTCPImpl::SetSendAudioLevelIndicationStatus(int channel,
106                                                        bool enable,
107                                                        unsigned char id) {
108   WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
109                "SetSendAudioLevelIndicationStatus(channel=%d, enable=%d,"
110                " ID=%u)",
111                channel, enable, id);
112   if (!_shared->statistics().Initialized()) {
113     _shared->SetLastError(VE_NOT_INITED, kTraceError);
114     return -1;
115   }
116   if (enable && (id < kVoiceEngineMinRtpExtensionId ||
117                  id > kVoiceEngineMaxRtpExtensionId)) {
118     // [RFC5285] The 4-bit id is the local identifier of this element in
119     // the range 1-14 inclusive.
120     _shared->SetLastError(
121         VE_INVALID_ARGUMENT, kTraceError,
122         "SetSendAudioLevelIndicationStatus() invalid ID parameter");
123     return -1;
124   }
125 
126   // Set state and id for the specified channel.
127   voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
128   voe::Channel* channelPtr = ch.channel();
129   if (channelPtr == NULL) {
130     _shared->SetLastError(
131         VE_CHANNEL_NOT_VALID, kTraceError,
132         "SetSendAudioLevelIndicationStatus() failed to locate channel");
133     return -1;
134   }
135   return channelPtr->SetSendAudioLevelIndicationStatus(enable, id);
136 }
137 
SetSendMIDStatus(int channel,bool enable,unsigned char id)138 int VoERTP_RTCPImpl::SetSendMIDStatus(int channel,
139                                       bool enable,
140                                       unsigned char id) {
141   WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
142                "SetSendMIDStatus(channel=%d, enable=%d,"
143                " ID=%u)",
144                channel, enable, id);
145   if (!_shared->statistics().Initialized()) {
146     _shared->SetLastError(VE_NOT_INITED, kTraceError);
147     return -1;
148   }
149   if (enable && (id < kVoiceEngineMinRtpExtensionId ||
150                  id > kVoiceEngineMaxRtpExtensionId)) {
151     // [RFC5285] The 4-bit id is the local identifier of this element in
152     // the range 1-14 inclusive.
153     _shared->SetLastError(
154         VE_INVALID_ARGUMENT, kTraceError,
155         "SetSendMIDStatus() invalid ID parameter");
156     return -1;
157   }
158 
159   // Set state and id for the specified channel.
160   voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
161   voe::Channel* channelPtr = ch.channel();
162   if (channelPtr == NULL) {
163     _shared->SetLastError(
164         VE_CHANNEL_NOT_VALID, kTraceError,
165         "SetSendMIDStatus() failed to locate channel");
166     return -1;
167   }
168   return channelPtr->SetSendMIDStatus(enable, id);
169 }
170 
171 
SetReceiveAudioLevelIndicationStatus(int channel,bool enable,unsigned char id,bool isLevelSsrc)172 int VoERTP_RTCPImpl::SetReceiveAudioLevelIndicationStatus(int channel,
173                                                           bool enable,
174                                                           unsigned char id,
175                                                           bool isLevelSsrc) {
176   WEBRTC_TRACE(
177       kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
178       "SetReceiveAudioLevelIndicationStatus(channel=%d, enable=%d, id=%u)",
179       channel, enable, id);
180   if (!_shared->statistics().Initialized()) {
181     _shared->SetLastError(VE_NOT_INITED, kTraceError);
182     return -1;
183   }
184   if (enable && (id < kVoiceEngineMinRtpExtensionId ||
185                  id > kVoiceEngineMaxRtpExtensionId)) {
186     // [RFC5285] The 4-bit id is the local identifier of this element in
187     // the range 1-14 inclusive.
188     _shared->SetLastError(
189         VE_INVALID_ARGUMENT, kTraceError,
190         "SetReceiveAudioLevelIndicationStatus() invalid id parameter");
191     return -1;
192   }
193   // Set state and id for the specified channel.
194   voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
195   voe::Channel* channel_ptr = ch.channel();
196   if (channel_ptr == NULL) {
197     _shared->SetLastError(
198         VE_CHANNEL_NOT_VALID, kTraceError,
199         "SetReceiveAudioLevelIndicationStatus() failed to locate channel");
200     return -1;
201   }
202   return channel_ptr->SetReceiveAudioLevelIndicationStatus(enable, id,
203                                                            isLevelSsrc);
204 }
205 
SetRTCPStatus(int channel,bool enable)206 int VoERTP_RTCPImpl::SetRTCPStatus(int channel, bool enable) {
207   WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
208                "SetRTCPStatus(channel=%d, enable=%d)", channel, enable);
209   if (!_shared->statistics().Initialized()) {
210     _shared->SetLastError(VE_NOT_INITED, kTraceError);
211     return -1;
212   }
213   voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
214   voe::Channel* channelPtr = ch.channel();
215   if (channelPtr == NULL) {
216     _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
217                           "SetRTCPStatus() failed to locate channel");
218     return -1;
219   }
220   channelPtr->SetRTCPStatus(enable);
221   return 0;
222 }
223 
GetRTCPStatus(int channel,bool & enabled)224 int VoERTP_RTCPImpl::GetRTCPStatus(int channel, bool& enabled) {
225   if (!_shared->statistics().Initialized()) {
226     _shared->SetLastError(VE_NOT_INITED, kTraceError);
227     return -1;
228   }
229   voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
230   voe::Channel* channelPtr = ch.channel();
231   if (channelPtr == NULL) {
232     _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
233                           "GetRTCPStatus() failed to locate channel");
234     return -1;
235   }
236   return channelPtr->GetRTCPStatus(enabled);
237 }
238 
SetRTCP_CNAME(int channel,const char cName[256])239 int VoERTP_RTCPImpl::SetRTCP_CNAME(int channel, const char cName[256]) {
240   WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
241                "SetRTCP_CNAME(channel=%d, cName=%s)", channel, cName);
242   if (!_shared->statistics().Initialized()) {
243     _shared->SetLastError(VE_NOT_INITED, kTraceError);
244     return -1;
245   }
246   voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
247   voe::Channel* channelPtr = ch.channel();
248   if (channelPtr == NULL) {
249     _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
250                           "SetRTCP_CNAME() failed to locate channel");
251     return -1;
252   }
253   return channelPtr->SetRTCP_CNAME(cName);
254 }
255 
GetRemoteRTCP_CNAME(int channel,char cName[256])256 int VoERTP_RTCPImpl::GetRemoteRTCP_CNAME(int channel, char cName[256]) {
257   if (!_shared->statistics().Initialized()) {
258     _shared->SetLastError(VE_NOT_INITED, kTraceError);
259     return -1;
260   }
261   voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
262   voe::Channel* channelPtr = ch.channel();
263   if (channelPtr == NULL) {
264     _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
265                           "GetRemoteRTCP_CNAME() failed to locate channel");
266     return -1;
267   }
268   return channelPtr->GetRemoteRTCP_CNAME(cName);
269 }
270 
GetRTPStatistics(int channel,unsigned int & averageJitterMs,unsigned int & maxJitterMs,unsigned int & discardedPackets,unsigned int & cumulativeLost)271 int VoERTP_RTCPImpl::GetRTPStatistics(int channel,
272                                       unsigned int& averageJitterMs,
273                                       unsigned int& maxJitterMs,
274                                       unsigned int& discardedPackets,
275                                       unsigned int& cumulativeLost)
276 {
277   if (!_shared->statistics().Initialized()) {
278     _shared->SetLastError(VE_NOT_INITED, kTraceError);
279     return -1;
280   }
281   voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
282   voe::Channel* channelPtr = ch.channel();
283   if (channelPtr == NULL) {
284     _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
285                           "GetRTPStatistics() failed to locate channel");
286     return -1;
287   }
288   return channelPtr->GetRTPStatistics(averageJitterMs,
289                                       maxJitterMs,
290                                       discardedPackets,
291                                       cumulativeLost);
292 }
293 
GetRTCPStatistics(int channel,CallStatistics & stats)294 int VoERTP_RTCPImpl::GetRTCPStatistics(int channel, CallStatistics& stats) {
295   if (!_shared->statistics().Initialized()) {
296     _shared->SetLastError(VE_NOT_INITED, kTraceError);
297     return -1;
298   }
299   voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
300   voe::Channel* channelPtr = ch.channel();
301   if (channelPtr == NULL) {
302     _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
303                           "GetRTPStatistics() failed to locate channel");
304     return -1;
305   }
306   return channelPtr->GetRTPStatistics(stats);
307 }
308 
GetRTCPPacketTypeCounters(int channel,RtcpPacketTypeCounter & stats)309 int VoERTP_RTCPImpl::GetRTCPPacketTypeCounters(int channel,
310                                                RtcpPacketTypeCounter& stats) {
311   if (!_shared->statistics().Initialized()) {
312    _shared->SetLastError(VE_NOT_INITED, kTraceError);
313    return -1;
314   }
315   voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
316   voe::Channel* channelPtr = ch.channel();
317   if (channelPtr == NULL) {
318     _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
319                           "GetRTCPPacketTypeCounters() failed to locate channel");
320     return -1;
321   }
322   return channelPtr->GetRTCPPacketTypeCounters(stats);
323 }
324 
GetRemoteRTCPReportBlocks(int channel,std::vector<ReportBlock> * report_blocks)325 int VoERTP_RTCPImpl::GetRemoteRTCPReportBlocks(
326     int channel, std::vector<ReportBlock>* report_blocks) {
327   if (!_shared->statistics().Initialized()) {
328     _shared->SetLastError(VE_NOT_INITED, kTraceError);
329     return -1;
330   }
331   voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
332   voe::Channel* channel_ptr = ch.channel();
333   if (channel_ptr == NULL) {
334     _shared->SetLastError(
335         VE_CHANNEL_NOT_VALID, kTraceError,
336         "GetRemoteRTCPReportBlocks() failed to locate channel");
337     return -1;
338   }
339   return channel_ptr->GetRemoteRTCPReportBlocks(report_blocks);
340 }
341 
SetNACKStatus(int channel,bool enable,int maxNoPackets)342 int VoERTP_RTCPImpl::SetNACKStatus(int channel, bool enable, int maxNoPackets) {
343   WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
344                "SetNACKStatus(channel=%d, enable=%d, maxNoPackets=%d)", channel,
345                enable, maxNoPackets);
346 
347   voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
348   voe::Channel* channelPtr = ch.channel();
349   if (channelPtr == NULL) {
350     _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
351                           "SetNACKStatus() failed to locate channel");
352     return -1;
353   }
354   channelPtr->SetNACKStatus(enable, maxNoPackets);
355   return 0;
356 }
357 
358 }  // namespace webrtc
359