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/base/checks.h"
14#include "webrtc/base/format_macros.h"
15#include "webrtc/base/logging.h"
16#include "webrtc/system_wrappers/include/critical_section_wrapper.h"
17#include "webrtc/system_wrappers/include/trace.h"
18#include "webrtc/voice_engine/channel.h"
19#include "webrtc/voice_engine/include/voe_errors.h"
20#include "webrtc/voice_engine/voice_engine_impl.h"
21
22namespace webrtc {
23
24VoENetwork* VoENetwork::GetInterface(VoiceEngine* voiceEngine) {
25  if (!voiceEngine) {
26    return nullptr;
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
36VoENetworkImpl::~VoENetworkImpl() = default;
37
38int VoENetworkImpl::RegisterExternalTransport(int channel,
39                                              Transport& transport) {
40  RTC_DCHECK(_shared->statistics().Initialized());
41  voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
42  voe::Channel* channelPtr = ch.channel();
43  if (!channelPtr) {
44    LOG_F(LS_ERROR) << "Failed to locate channel: " << channel;
45    return -1;
46  }
47  return channelPtr->RegisterExternalTransport(transport);
48}
49
50int VoENetworkImpl::DeRegisterExternalTransport(int channel) {
51  RTC_CHECK(_shared->statistics().Initialized());
52  voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
53  voe::Channel* channelPtr = ch.channel();
54  if (!channelPtr) {
55    LOG_F(LS_ERROR) << "Failed to locate channel: " << channel;
56    return -1;
57  }
58  return channelPtr->DeRegisterExternalTransport();
59}
60
61int VoENetworkImpl::ReceivedRTPPacket(int channel,
62                                      const void* data,
63                                      size_t length) {
64  return ReceivedRTPPacket(channel, data, length, webrtc::PacketTime());
65}
66
67int VoENetworkImpl::ReceivedRTPPacket(int channel,
68                                      const void* data,
69                                      size_t length,
70                                      const PacketTime& packet_time) {
71  RTC_CHECK(_shared->statistics().Initialized());
72  RTC_CHECK(data);
73  // L16 at 32 kHz, stereo, 10 ms frames (+12 byte RTP header) -> 1292 bytes
74  if ((length < 12) || (length > 1292)) {
75    LOG_F(LS_ERROR) << "Invalid packet length: " << length;
76    return -1;
77  }
78  voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
79  voe::Channel* channelPtr = ch.channel();
80  if (!channelPtr) {
81    LOG_F(LS_ERROR) << "Failed to locate channel: " << channel;
82    return -1;
83  }
84  if (!channelPtr->ExternalTransport()) {
85    LOG_F(LS_ERROR) << "No external transport for channel: " << channel;
86    return -1;
87  }
88  return channelPtr->ReceivedRTPPacket((const int8_t*)data, length,
89                                       packet_time);
90}
91
92int VoENetworkImpl::ReceivedRTCPPacket(int channel,
93                                       const void* data,
94                                       size_t length) {
95  RTC_CHECK(_shared->statistics().Initialized());
96  RTC_CHECK(data);
97  if (length < 4) {
98    LOG_F(LS_ERROR) << "Invalid packet length: " << length;
99    return -1;
100  }
101  voe::ChannelOwner ch = _shared->channel_manager().GetChannel(channel);
102  voe::Channel* channelPtr = ch.channel();
103  if (!channelPtr) {
104    LOG_F(LS_ERROR) << "Failed to locate channel: " << channel;
105    return -1;
106  }
107  if (!channelPtr->ExternalTransport()) {
108    LOG_F(LS_ERROR) << "No external transport for channel: " << channel;
109    return -1;
110  }
111  return channelPtr->ReceivedRTCPPacket((const int8_t*)data, length);
112}
113
114}  // namespace webrtc
115