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_network_impl.h"
12
13#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
14#include "webrtc/system_wrappers/interface/logging.h"
15#include "webrtc/system_wrappers/interface/trace.h"
16#include "webrtc/voice_engine/channel.h"
17#include "webrtc/voice_engine/include/voe_errors.h"
18#include "webrtc/voice_engine/voice_engine_impl.h"
19
20namespace webrtc
21{
22
23VoENetwork* VoENetwork::GetInterface(VoiceEngine* voiceEngine)
24{
25    if (NULL == voiceEngine)
26    {
27        return NULL;
28    }
29    VoiceEngineImpl* s = static_cast<VoiceEngineImpl*>(voiceEngine);
30    s->AddRef();
31    return s;
32}
33
34VoENetworkImpl::VoENetworkImpl(voe::SharedData* shared) : _shared(shared)
35{
36    WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_shared->instance_id(), -1),
37                 "VoENetworkImpl() - ctor");
38}
39
40VoENetworkImpl::~VoENetworkImpl()
41{
42    WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_shared->instance_id(), -1),
43                 "~VoENetworkImpl() - dtor");
44}
45
46int VoENetworkImpl::RegisterExternalTransport(int channel,
47                                              Transport& transport)
48{
49    WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
50                 "SetExternalTransport(channel=%d, transport=0x%x)",
51                 channel, &transport);
52    if (!_shared->statistics().Initialized())
53    {
54        _shared->SetLastError(VE_NOT_INITED, kTraceError);
55        return -1;
56    }
57    voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
58    voe::Channel* channelPtr = ch.channel();
59    if (channelPtr == NULL)
60    {
61        _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
62            "SetExternalTransport() failed to locate channel");
63        return -1;
64    }
65    return channelPtr->RegisterExternalTransport(transport);
66}
67
68int VoENetworkImpl::DeRegisterExternalTransport(int channel)
69{
70    WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
71                 "DeRegisterExternalTransport(channel=%d)", channel);
72    if (!_shared->statistics().Initialized())
73    {
74        WEBRTC_TRACE(kTraceError, kTraceVoice,
75                     VoEId(_shared->instance_id(), -1),
76                     "DeRegisterExternalTransport() - invalid state");
77    }
78    voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
79    voe::Channel* channelPtr = ch.channel();
80    if (channelPtr == NULL)
81    {
82        _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
83            "DeRegisterExternalTransport() failed to locate channel");
84        return -1;
85    }
86    return channelPtr->DeRegisterExternalTransport();
87}
88
89int VoENetworkImpl::ReceivedRTPPacket(int channel,
90                                      const void* data,
91                                      unsigned int length) {
92  return ReceivedRTPPacket(channel, data, length, webrtc::PacketTime());
93}
94
95int VoENetworkImpl::ReceivedRTPPacket(int channel,
96                                      const void* data,
97                                      unsigned int length,
98                                      const PacketTime& packet_time)
99{
100    WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_shared->instance_id(), -1),
101                 "ReceivedRTPPacket(channel=%d, length=%u)", channel, length);
102    if (!_shared->statistics().Initialized())
103    {
104        _shared->SetLastError(VE_NOT_INITED, kTraceError);
105        return -1;
106    }
107    // L16 at 32 kHz, stereo, 10 ms frames (+12 byte RTP header) -> 1292 bytes
108    if ((length < 12) || (length > 1292))
109    {
110        _shared->SetLastError(VE_INVALID_PACKET);
111        LOG(LS_ERROR) << "Invalid packet length: " << length;
112        return -1;
113    }
114    if (NULL == data)
115    {
116        _shared->SetLastError(VE_INVALID_ARGUMENT, kTraceError,
117            "ReceivedRTPPacket() invalid data vector");
118        return -1;
119    }
120    voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
121    voe::Channel* channelPtr = ch.channel();
122    if (channelPtr == NULL)
123    {
124        _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
125            "ReceivedRTPPacket() failed to locate channel");
126        return -1;
127    }
128
129    if (!channelPtr->ExternalTransport())
130    {
131        _shared->SetLastError(VE_INVALID_OPERATION, kTraceError,
132            "ReceivedRTPPacket() external transport is not enabled");
133        return -1;
134    }
135    return channelPtr->ReceivedRTPPacket((const int8_t*) data, length,
136                                         packet_time);
137}
138
139int VoENetworkImpl::ReceivedRTCPPacket(int channel, const void* data,
140                                       unsigned int length)
141{
142    WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_shared->instance_id(), -1),
143                 "ReceivedRTCPPacket(channel=%d, length=%u)", channel, length);
144    if (!_shared->statistics().Initialized())
145    {
146        _shared->SetLastError(VE_NOT_INITED, kTraceError);
147        return -1;
148    }
149    if (length < 4)
150    {
151        _shared->SetLastError(VE_INVALID_PACKET, kTraceError,
152            "ReceivedRTCPPacket() invalid packet length");
153        return -1;
154    }
155    if (NULL == data)
156    {
157        _shared->SetLastError(VE_INVALID_ARGUMENT, kTraceError,
158            "ReceivedRTCPPacket() invalid data vector");
159        return -1;
160    }
161    voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
162    voe::Channel* channelPtr = ch.channel();
163    if (channelPtr == NULL)
164    {
165        _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
166            "ReceivedRTCPPacket() failed to locate channel");
167        return -1;
168    }
169    if (!channelPtr->ExternalTransport())
170    {
171        _shared->SetLastError(VE_INVALID_OPERATION, kTraceError,
172            "ReceivedRTCPPacket() external transport is not enabled");
173        return -1;
174    }
175    return channelPtr->ReceivedRTCPPacket((const int8_t*) data, length);
176}
177}  // namespace webrtc
178