voe_network_impl.cc revision 6141e13873d0fdea626de08dfec2efa2c9171c76
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 "voe_network_impl.h"
12
13#include "channel.h"
14#include "critical_section_wrapper.h"
15#include "trace.h"
16#include "voe_errors.h"
17#include "voice_engine_impl.h"
18
19namespace webrtc
20{
21
22VoENetwork* VoENetwork::GetInterface(VoiceEngine* voiceEngine)
23{
24    if (NULL == voiceEngine)
25    {
26        return NULL;
27    }
28    VoiceEngineImpl* s = static_cast<VoiceEngineImpl*>(voiceEngine);
29    s->AddRef();
30    return s;
31}
32
33VoENetworkImpl::VoENetworkImpl(voe::SharedData* shared) : _shared(shared)
34{
35    WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_shared->instance_id(), -1),
36                 "VoENetworkImpl() - ctor");
37}
38
39VoENetworkImpl::~VoENetworkImpl()
40{
41    WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_shared->instance_id(), -1),
42                 "~VoENetworkImpl() - dtor");
43}
44
45int VoENetworkImpl::RegisterExternalTransport(int channel,
46                                              Transport& transport)
47{
48    WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
49                 "SetExternalTransport(channel=%d, transport=0x%x)",
50                 channel, &transport);
51    if (!_shared->statistics().Initialized())
52    {
53        _shared->SetLastError(VE_NOT_INITED, kTraceError);
54        return -1;
55    }
56    voe::ScopedChannel sc(_shared->channel_manager(), channel);
57    voe::Channel* channelPtr = sc.ChannelPtr();
58    if (channelPtr == NULL)
59    {
60        _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
61            "SetExternalTransport() failed to locate channel");
62        return -1;
63    }
64    return channelPtr->RegisterExternalTransport(transport);
65}
66
67int VoENetworkImpl::DeRegisterExternalTransport(int channel)
68{
69    WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
70                 "DeRegisterExternalTransport(channel=%d)", channel);
71    if (!_shared->statistics().Initialized())
72    {
73        _shared->SetLastError(VE_NOT_INITED, kTraceError);
74        return -1;
75    }
76    voe::ScopedChannel sc(_shared->channel_manager(), channel);
77    voe::Channel* channelPtr = sc.ChannelPtr();
78    if (channelPtr == NULL)
79    {
80        _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
81            "DeRegisterExternalTransport() failed to locate channel");
82        return -1;
83    }
84    return channelPtr->DeRegisterExternalTransport();
85}
86
87int VoENetworkImpl::ReceivedRTPPacket(int channel,
88                                      const void* data,
89                                      unsigned int length)
90{
91    WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_shared->instance_id(), -1),
92                 "ReceivedRTPPacket(channel=%d, length=%u)", channel, length);
93    if (!_shared->statistics().Initialized())
94    {
95        _shared->SetLastError(VE_NOT_INITED, kTraceError);
96        return -1;
97    }
98    if ((length < 12) || (length > 807))
99    {
100        _shared->SetLastError(VE_INVALID_PACKET, kTraceError,
101            "ReceivedRTPPacket() invalid packet length");
102        return -1;
103    }
104    if (NULL == data)
105    {
106        _shared->SetLastError(VE_INVALID_ARGUMENT, kTraceError,
107            "ReceivedRTPPacket() invalid data vector");
108        return -1;
109    }
110    voe::ScopedChannel sc(_shared->channel_manager(), channel);
111    voe::Channel* channelPtr = sc.ChannelPtr();
112    if (channelPtr == NULL)
113    {
114        _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
115            "ReceivedRTPPacket() failed to locate channel");
116        return -1;
117    }
118
119    if (!channelPtr->ExternalTransport())
120    {
121        _shared->SetLastError(VE_INVALID_OPERATION, kTraceError,
122            "ReceivedRTPPacket() external transport is not enabled");
123        return -1;
124    }
125    return channelPtr->ReceivedRTPPacket((const int8_t*) data, length);
126}
127
128int VoENetworkImpl::ReceivedRTCPPacket(int channel, const void* data,
129                                       unsigned int length)
130{
131    WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_shared->instance_id(), -1),
132                 "ReceivedRTCPPacket(channel=%d, length=%u)", channel, length);
133    if (!_shared->statistics().Initialized())
134    {
135        _shared->SetLastError(VE_NOT_INITED, kTraceError);
136        return -1;
137    }
138    if (length < 4)
139    {
140        _shared->SetLastError(VE_INVALID_PACKET, kTraceError,
141            "ReceivedRTCPPacket() invalid packet length");
142        return -1;
143    }
144    if (NULL == data)
145    {
146        _shared->SetLastError(VE_INVALID_ARGUMENT, kTraceError,
147            "ReceivedRTCPPacket() invalid data vector");
148        return -1;
149    }
150    voe::ScopedChannel sc(_shared->channel_manager(), channel);
151    voe::Channel* channelPtr = sc.ChannelPtr();
152    if (channelPtr == NULL)
153    {
154        _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
155            "ReceivedRTCPPacket() failed to locate channel");
156        return -1;
157    }
158    if (!channelPtr->ExternalTransport())
159    {
160        _shared->SetLastError(VE_INVALID_OPERATION, kTraceError,
161            "ReceivedRTCPPacket() external transport is not enabled");
162        return -1;
163    }
164    return channelPtr->ReceivedRTCPPacket((const int8_t*) data, length);
165}
166
167int VoENetworkImpl::SetPacketTimeoutNotification(int channel,
168                                                 bool enable,
169                                                 int timeoutSeconds)
170{
171    WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
172                 "SetPacketTimeoutNotification(channel=%d, enable=%d, "
173                 "timeoutSeconds=%d)",
174                 channel, (int) enable, timeoutSeconds);
175    if (!_shared->statistics().Initialized())
176    {
177        _shared->SetLastError(VE_NOT_INITED, kTraceError);
178        return -1;
179    }
180    if (enable &&
181        ((timeoutSeconds < kVoiceEngineMinPacketTimeoutSec) ||
182        (timeoutSeconds > kVoiceEngineMaxPacketTimeoutSec)))
183    {
184        _shared->SetLastError(VE_INVALID_ARGUMENT, kTraceError,
185            "SetPacketTimeoutNotification() invalid timeout size");
186        return -1;
187    }
188    voe::ScopedChannel sc(_shared->channel_manager(), channel);
189    voe::Channel* channelPtr = sc.ChannelPtr();
190    if (channelPtr == NULL)
191    {
192        _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
193            "SetPacketTimeoutNotification() failed to locate channel");
194        return -1;
195    }
196    return channelPtr->SetPacketTimeoutNotification(enable, timeoutSeconds);
197}
198
199int VoENetworkImpl::GetPacketTimeoutNotification(int channel,
200                                                 bool& enabled,
201                                                 int& timeoutSeconds)
202{
203    WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
204                 "GetPacketTimeoutNotification(channel=%d, enabled=?,"
205                 " timeoutSeconds=?)", channel);
206    if (!_shared->statistics().Initialized())
207    {
208        _shared->SetLastError(VE_NOT_INITED, kTraceError);
209        return -1;
210    }
211    voe::ScopedChannel sc(_shared->channel_manager(), channel);
212    voe::Channel* channelPtr = sc.ChannelPtr();
213    if (channelPtr == NULL)
214    {
215        _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
216            "GetPacketTimeoutNotification() failed to locate channel");
217        return -1;
218    }
219    return channelPtr->GetPacketTimeoutNotification(enabled, timeoutSeconds);
220}
221
222int VoENetworkImpl::RegisterDeadOrAliveObserver(int channel,
223                                                VoEConnectionObserver&
224                                                observer)
225{
226    WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
227                 "RegisterDeadOrAliveObserver(channel=%d, observer=0x%x)",
228                 channel, &observer);
229    if (!_shared->statistics().Initialized())
230    {
231        _shared->SetLastError(VE_NOT_INITED, kTraceError);
232        return -1;
233    }
234    voe::ScopedChannel sc(_shared->channel_manager(), channel);
235    voe::Channel* channelPtr = sc.ChannelPtr();
236    if (channelPtr == NULL)
237    {
238        _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
239            "RegisterDeadOrAliveObserver() failed to locate channel");
240        return -1;
241    }
242    return channelPtr->RegisterDeadOrAliveObserver(observer);
243}
244
245int VoENetworkImpl::DeRegisterDeadOrAliveObserver(int channel)
246{
247    WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
248                 "DeRegisterDeadOrAliveObserver(channel=%d)", channel);
249    if (!_shared->statistics().Initialized())
250    {
251        _shared->SetLastError(VE_NOT_INITED, kTraceError);
252        return -1;
253    }
254    voe::ScopedChannel sc(_shared->channel_manager(), channel);
255    voe::Channel* channelPtr = sc.ChannelPtr();
256    if (channelPtr == NULL)
257    {
258        _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
259            "DeRegisterDeadOrAliveObserver() failed to locate channel");
260        return -1;
261    }
262    return channelPtr->DeRegisterDeadOrAliveObserver();
263}
264
265int VoENetworkImpl::SetPeriodicDeadOrAliveStatus(int channel, bool enable,
266                                                 int sampleTimeSeconds)
267{
268    WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
269                 "SetPeriodicDeadOrAliveStatus(channel=%d, enable=%d,"
270                 " sampleTimeSeconds=%d)",
271                 channel, enable, sampleTimeSeconds);
272    if (!_shared->statistics().Initialized())
273    {
274        _shared->SetLastError(VE_NOT_INITED, kTraceError);
275        return -1;
276    }
277    if (enable &&
278        ((sampleTimeSeconds < kVoiceEngineMinSampleTimeSec) ||
279        (sampleTimeSeconds > kVoiceEngineMaxSampleTimeSec)))
280    {
281        _shared->SetLastError(VE_INVALID_ARGUMENT, kTraceError,
282            "SetPeriodicDeadOrAliveStatus() invalid sample time");
283        return -1;
284    }
285    voe::ScopedChannel sc(_shared->channel_manager(), channel);
286    voe::Channel* channelPtr = sc.ChannelPtr();
287    if (channelPtr == NULL)
288    {
289        _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
290            "SetPeriodicDeadOrAliveStatus() failed to locate channel");
291        return -1;
292    }
293    return channelPtr->SetPeriodicDeadOrAliveStatus(enable, sampleTimeSeconds);
294}
295
296int VoENetworkImpl::GetPeriodicDeadOrAliveStatus(int channel,
297                                                 bool& enabled,
298                                                 int& sampleTimeSeconds)
299{
300    WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
301                 "GetPeriodicDeadOrAliveStatus(channel=%d, enabled=?,"
302                 " sampleTimeSeconds=?)", channel);
303    if (!_shared->statistics().Initialized())
304    {
305        _shared->SetLastError(VE_NOT_INITED, kTraceError);
306        return -1;
307    }
308    voe::ScopedChannel sc(_shared->channel_manager(), channel);
309    voe::Channel* channelPtr = sc.ChannelPtr();
310    if (channelPtr == NULL)
311    {
312        _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
313            "GetPeriodicDeadOrAliveStatus() failed to locate channel");
314        return -1;
315    }
316    return channelPtr->GetPeriodicDeadOrAliveStatus(enabled,
317                                                    sampleTimeSeconds);
318}
319
320} // namespace webrtc
321