blob: 83f156dccf2e7999c497ead2981e30a84304436f [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
mflodman@webrtc.org1a739ba2012-02-28 16:11:33 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000011#include "webrtc/video_engine/vie_base_impl.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
mflodman@webrtc.org9ba151b2012-06-21 10:02:13 +000013#include <sstream>
14#include <string>
15
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000016#include "webrtc/engine_configurations.h"
17#include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp.h"
18#include "webrtc/modules/video_coding/main/interface/video_coding.h"
19#include "webrtc/modules/video_processing/main/interface/video_processing.h"
andrew@webrtc.org9841d922012-10-31 05:22:11 +000020#include "webrtc/modules/video_render/include/video_render.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000021#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
22#include "webrtc/system_wrappers/interface/trace.h"
23#include "webrtc/video_engine/include/vie_errors.h"
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +000024#include "webrtc/video_engine/vie_capturer.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000025#include "webrtc/video_engine/vie_channel.h"
26#include "webrtc/video_engine/vie_channel_manager.h"
27#include "webrtc/video_engine/vie_defines.h"
28#include "webrtc/video_engine/vie_encoder.h"
29#include "webrtc/video_engine/vie_impl.h"
30#include "webrtc/video_engine/vie_input_manager.h"
31#include "webrtc/video_engine/vie_shared_data.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000032
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +000033namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000034
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +000035ViEBase* ViEBase::GetInterface(VideoEngine* video_engine) {
36 if (!video_engine) {
37 return NULL;
38 }
andrew@webrtc.orgd72262d2013-05-09 02:12:07 +000039 VideoEngineImpl* vie_impl = static_cast<VideoEngineImpl*>(video_engine);
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +000040 ViEBaseImpl* vie_base_impl = vie_impl;
41 (*vie_base_impl)++; // Increase ref count.
niklase@google.com470e71d2011-07-07 08:21:25 +000042
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +000043 return vie_base_impl;
niklase@google.com470e71d2011-07-07 08:21:25 +000044}
45
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +000046int ViEBaseImpl::Release() {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000047 WEBRTC_TRACE(kTraceApiCall, kTraceVideo, shared_data_.instance_id(),
niklase@google.com470e71d2011-07-07 08:21:25 +000048 "ViEBase::Release()");
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +000049 (*this)--; // Decrease ref count.
niklase@google.com470e71d2011-07-07 08:21:25 +000050
pbos@webrtc.orgb238d122013-04-09 13:41:51 +000051 int32_t ref_count = GetCount();
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +000052 if (ref_count < 0) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000053 WEBRTC_TRACE(kTraceWarning, kTraceVideo, shared_data_.instance_id(),
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +000054 "ViEBase release too many times");
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000055 shared_data_.SetLastError(kViEAPIDoesNotExist);
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +000056 return -1;
57 }
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000058 WEBRTC_TRACE(kTraceInfo, kTraceVideo, shared_data_.instance_id(),
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +000059 "ViEBase reference count: %d", ref_count);
60 return ref_count;
niklase@google.com470e71d2011-07-07 08:21:25 +000061}
62
andresp@webrtc.org7707d062013-05-13 10:50:50 +000063ViEBaseImpl::ViEBaseImpl(const Config& config)
64 : shared_data_(config) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000065 WEBRTC_TRACE(kTraceMemory, kTraceVideo, shared_data_.instance_id(),
niklase@google.com470e71d2011-07-07 08:21:25 +000066 "ViEBaseImpl::ViEBaseImpl() Ctor");
67}
68
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +000069ViEBaseImpl::~ViEBaseImpl() {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000070 WEBRTC_TRACE(kTraceMemory, kTraceVideo, shared_data_.instance_id(),
niklase@google.com470e71d2011-07-07 08:21:25 +000071 "ViEBaseImpl::ViEBaseImpl() Dtor");
niklase@google.com470e71d2011-07-07 08:21:25 +000072}
73
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +000074int ViEBaseImpl::Init() {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000075 WEBRTC_TRACE(kTraceApiCall, kTraceVideo, shared_data_.instance_id(),
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +000076 "Init");
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +000077 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000078}
79
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +000080int ViEBaseImpl::SetVoiceEngine(VoiceEngine* voice_engine) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000081 WEBRTC_TRACE(kTraceApiCall, kTraceVideo, ViEId(shared_data_.instance_id()),
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +000082 "%s", __FUNCTION__);
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000083 if (shared_data_.channel_manager()->SetVoiceEngine(voice_engine) != 0) {
84 shared_data_.SetLastError(kViEBaseVoEFailure);
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +000085 return -1;
86 }
87 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000088}
89
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +000090int ViEBaseImpl::RegisterCpuOveruseObserver(int video_channel,
91 CpuOveruseObserver* observer) {
92 ViEChannelManagerScoped cs(*(shared_data_.channel_manager()));
93 ViEChannel* vie_channel = cs.Channel(video_channel);
94 if (!vie_channel) {
95 WEBRTC_TRACE(kTraceError,
96 kTraceVideo,
97 ViEId(shared_data_.instance_id()),
98 "%s: channel %d doesn't exist",
99 __FUNCTION__,
100 video_channel);
101 shared_data_.SetLastError(kViEBaseInvalidChannelId);
102 return -1;
103 }
104 ViEEncoder* vie_encoder = cs.Encoder(video_channel);
105 assert(vie_encoder);
106
107 ViEInputManagerScoped is(*(shared_data_.input_manager()));
108 ViEFrameProviderBase* provider = is.FrameProvider(vie_encoder);
109 if (provider) {
110 ViECapturer* capturer = is.Capture(provider->Id());
111 assert(capturer);
112 capturer->RegisterCpuOveruseObserver(observer);
113 }
114
115 shared_data_.overuse_observers()->insert(
116 std::pair<int, CpuOveruseObserver*>(video_channel, observer));
117 return 0;
118}
119
asapersson@webrtc.orgc7ff8f92013-11-26 11:12:33 +0000120int ViEBaseImpl::CpuOveruseMeasures(int video_channel,
121 int* capture_jitter_ms,
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000122 int* avg_encode_time_ms,
123 int* encode_usage_percent,
124 int* capture_queue_delay_ms_per_s) {
asapersson@webrtc.orgb24d3352013-11-20 13:51:40 +0000125 ViEChannelManagerScoped cs(*(shared_data_.channel_manager()));
126 ViEChannel* vie_channel = cs.Channel(video_channel);
127 if (!vie_channel) {
128 WEBRTC_TRACE(kTraceError,
129 kTraceVideo,
130 ViEId(shared_data_.instance_id()),
131 "%s: channel %d doesn't exist",
132 __FUNCTION__,
133 video_channel);
134 shared_data_.SetLastError(kViEBaseInvalidChannelId);
135 return -1;
136 }
137 ViEEncoder* vie_encoder = cs.Encoder(video_channel);
138 assert(vie_encoder);
139
140 ViEInputManagerScoped is(*(shared_data_.input_manager()));
141 ViEFrameProviderBase* provider = is.FrameProvider(vie_encoder);
142 if (provider) {
143 ViECapturer* capturer = is.Capture(provider->Id());
144 if (capturer) {
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000145 capturer->CpuOveruseMeasures(capture_jitter_ms,
146 avg_encode_time_ms,
147 encode_usage_percent,
148 capture_queue_delay_ms_per_s);
asapersson@webrtc.orgb24d3352013-11-20 13:51:40 +0000149 return 0;
150 }
151 }
152 return -1;
153}
154
mflodman@webrtc.org9ba151b2012-06-21 10:02:13 +0000155int ViEBaseImpl::CreateChannel(int& video_channel) { // NOLINT
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000156 WEBRTC_TRACE(kTraceApiCall, kTraceVideo, ViEId(shared_data_.instance_id()),
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000157 "%s", __FUNCTION__);
mflodman@webrtc.orgab2610f2012-06-29 10:05:28 +0000158 if (shared_data_.channel_manager()->CreateChannel(&video_channel) == -1) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000159 WEBRTC_TRACE(kTraceError, kTraceVideo, ViEId(shared_data_.instance_id()),
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000160 "%s: Could not create channel", __FUNCTION__);
161 video_channel = -1;
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000162 shared_data_.SetLastError(kViEBaseChannelCreationFailed);
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000163 return -1;
164 }
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000165 WEBRTC_TRACE(kTraceInfo, kTraceVideo, ViEId(shared_data_.instance_id()),
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000166 "%s: channel created: %d", __FUNCTION__, video_channel);
167 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000168}
169
mflodman@webrtc.org9ba151b2012-06-21 10:02:13 +0000170int ViEBaseImpl::CreateChannel(int& video_channel, // NOLINT
171 int original_channel) {
mflodman@webrtc.org9ec883e2012-03-05 17:12:41 +0000172 return CreateChannel(video_channel, original_channel, true);
173}
niklase@google.com470e71d2011-07-07 08:21:25 +0000174
mflodman@webrtc.org9ba151b2012-06-21 10:02:13 +0000175int ViEBaseImpl::CreateReceiveChannel(int& video_channel, // NOLINT
mflodman@webrtc.org9ec883e2012-03-05 17:12:41 +0000176 int original_channel) {
177 return CreateChannel(video_channel, original_channel, false);
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000178}
niklase@google.com470e71d2011-07-07 08:21:25 +0000179
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000180int ViEBaseImpl::DeleteChannel(const int video_channel) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000181 WEBRTC_TRACE(kTraceApiCall, kTraceVideo, ViEId(shared_data_.instance_id()),
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000182 "%s(%d)", __FUNCTION__, video_channel);
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000183 {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000184 ViEChannelManagerScoped cs(*(shared_data_.channel_manager()));
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000185 ViEChannel* vie_channel = cs.Channel(video_channel);
186 if (!vie_channel) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000187 WEBRTC_TRACE(kTraceError, kTraceVideo,
188 ViEId(shared_data_.instance_id()),
189 "%s: channel %d doesn't exist", __FUNCTION__, video_channel);
190 shared_data_.SetLastError(kViEBaseInvalidChannelId);
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000191 return -1;
192 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000193
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000194 // Deregister the ViEEncoder if no other channel is using it.
195 ViEEncoder* vie_encoder = cs.Encoder(video_channel);
196 if (cs.ChannelUsingViEEncoder(video_channel) == false) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000197 ViEInputManagerScoped is(*(shared_data_.input_manager()));
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000198 ViEFrameProviderBase* provider = is.FrameProvider(vie_encoder);
199 if (provider) {
200 provider->DeregisterFrameCallback(vie_encoder);
201 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000202 }
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000203 }
204
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000205 if (shared_data_.channel_manager()->DeleteChannel(video_channel) == -1) {
206 WEBRTC_TRACE(kTraceError, kTraceVideo, ViEId(shared_data_.instance_id()),
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000207 "%s: Could not delete channel %d", __FUNCTION__,
208 video_channel);
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000209 shared_data_.SetLastError(kViEBaseUnknownError);
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000210 return -1;
211 }
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000212 WEBRTC_TRACE(kTraceInfo, kTraceVideo, ViEId(shared_data_.instance_id()),
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000213 "%s: channel deleted: %d", __FUNCTION__, video_channel);
214 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000215}
216
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000217int ViEBaseImpl::ConnectAudioChannel(const int video_channel,
218 const int audio_channel) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000219 WEBRTC_TRACE(kTraceApiCall, kTraceVideo, ViEId(shared_data_.instance_id()),
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000220 "%s(%d)", __FUNCTION__, video_channel);
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000221 ViEChannelManagerScoped cs(*(shared_data_.channel_manager()));
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000222 if (!cs.Channel(video_channel)) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000223 WEBRTC_TRACE(kTraceError, kTraceVideo, ViEId(shared_data_.instance_id()),
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000224 "%s: channel %d doesn't exist", __FUNCTION__, video_channel);
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000225 shared_data_.SetLastError(kViEBaseInvalidChannelId);
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000226 return -1;
227 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000228
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000229 if (shared_data_.channel_manager()->ConnectVoiceChannel(video_channel,
230 audio_channel) != 0) {
231 shared_data_.SetLastError(kViEBaseVoEFailure);
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000232 return -1;
233 }
234 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000235}
236
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000237int ViEBaseImpl::DisconnectAudioChannel(const int video_channel) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000238 WEBRTC_TRACE(kTraceApiCall, kTraceVideo, ViEId(shared_data_.instance_id()),
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000239 "%s(%d)", __FUNCTION__, video_channel);
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000240 ViEChannelManagerScoped cs(*(shared_data_.channel_manager()));
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000241 if (!cs.Channel(video_channel)) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000242 WEBRTC_TRACE(kTraceError, kTraceVideo, ViEId(shared_data_.instance_id()),
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000243 "%s: channel %d doesn't exist", __FUNCTION__, video_channel);
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000244 shared_data_.SetLastError(kViEBaseInvalidChannelId);
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000245 return -1;
246 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000247
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000248 if (shared_data_.channel_manager()->DisconnectVoiceChannel(
249 video_channel) != 0) {
250 shared_data_.SetLastError(kViEBaseVoEFailure);
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000251 return -1;
252 }
253 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000254}
255
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000256int ViEBaseImpl::StartSend(const int video_channel) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000257 WEBRTC_TRACE(kTraceApiCall, kTraceVideo,
258 ViEId(shared_data_.instance_id(), video_channel),
259 "%s(channel: %d)", __FUNCTION__, video_channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000260
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000261 ViEChannelManagerScoped cs(*(shared_data_.channel_manager()));
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000262 ViEChannel* vie_channel = cs.Channel(video_channel);
263 if (!vie_channel) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000264 WEBRTC_TRACE(kTraceError, kTraceVideo,
265 ViEId(shared_data_.instance_id(), video_channel),
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000266 "%s: Channel %d does not exist", __FUNCTION__, video_channel);
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000267 shared_data_.SetLastError(kViEBaseInvalidChannelId);
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000268 return -1;
269 }
mflodman@webrtc.org1a739ba2012-02-28 16:11:33 +0000270
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000271 ViEEncoder* vie_encoder = cs.Encoder(video_channel);
mflodman@webrtc.org9ec883e2012-03-05 17:12:41 +0000272 assert(vie_encoder != NULL);
273 if (vie_encoder->Owner() != video_channel) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000274 WEBRTC_TRACE(kTraceError, kTraceVideo,
275 ViEId(shared_data_.instance_id(), video_channel),
mflodman@webrtc.org9ec883e2012-03-05 17:12:41 +0000276 "Can't start ssend on a receive only channel.");
277 shared_data_.SetLastError(kViEBaseReceiveOnlyChannel);
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000278 return -1;
279 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000280
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000281 // Pause and trigger a key frame.
282 vie_encoder->Pause();
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000283 int32_t error = vie_channel->StartSend();
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000284 if (error != 0) {
285 vie_encoder->Restart();
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000286 WEBRTC_TRACE(kTraceError, kTraceVideo,
287 ViEId(shared_data_.instance_id(), video_channel),
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000288 "%s: Could not start sending on channel %d", __FUNCTION__,
289 video_channel);
290 if (error == kViEBaseAlreadySending) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000291 shared_data_.SetLastError(kViEBaseAlreadySending);
niklase@google.com470e71d2011-07-07 08:21:25 +0000292 }
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000293 shared_data_.SetLastError(kViEBaseUnknownError);
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000294 return -1;
295 }
296 vie_encoder->SendKeyFrame();
297 vie_encoder->Restart();
298 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000299}
300
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000301int ViEBaseImpl::StopSend(const int video_channel) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000302 WEBRTC_TRACE(kTraceApiCall, kTraceVideo,
303 ViEId(shared_data_.instance_id(), video_channel),
304 "%s(channel: %d)", __FUNCTION__, video_channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000305
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000306 ViEChannelManagerScoped cs(*(shared_data_.channel_manager()));
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000307 ViEChannel* vie_channel = cs.Channel(video_channel);
308 if (!vie_channel) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000309 WEBRTC_TRACE(kTraceError, kTraceVideo,
310 ViEId(shared_data_.instance_id(), video_channel),
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000311 "%s: Channel %d does not exist", __FUNCTION__, video_channel);
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000312 shared_data_.SetLastError(kViEBaseInvalidChannelId);
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000313 return -1;
314 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000315
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000316 int32_t error = vie_channel->StopSend();
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000317 if (error != 0) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000318 WEBRTC_TRACE(kTraceError, kTraceVideo,
319 ViEId(shared_data_.instance_id(), video_channel),
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000320 "%s: Could not stop sending on channel %d", __FUNCTION__,
321 video_channel);
322 if (error == kViEBaseNotSending) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000323 shared_data_.SetLastError(kViEBaseNotSending);
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000324 } else {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000325 shared_data_.SetLastError(kViEBaseUnknownError);
niklase@google.com470e71d2011-07-07 08:21:25 +0000326 }
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000327 return -1;
328 }
329 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000330}
331
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000332int ViEBaseImpl::StartReceive(const int video_channel) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000333 WEBRTC_TRACE(kTraceApiCall, kTraceVideo,
334 ViEId(shared_data_.instance_id(), video_channel),
335 "%s(channel: %d)", __FUNCTION__, video_channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000336
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000337 ViEChannelManagerScoped cs(*(shared_data_.channel_manager()));
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000338 ViEChannel* vie_channel = cs.Channel(video_channel);
339 if (!vie_channel) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000340 WEBRTC_TRACE(kTraceError, kTraceVideo,
341 ViEId(shared_data_.instance_id(), video_channel),
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000342 "%s: Channel %d does not exist", __FUNCTION__, video_channel);
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000343 shared_data_.SetLastError(kViEBaseInvalidChannelId);
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000344 return -1;
345 }
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000346 if (vie_channel->StartReceive() != 0) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000347 shared_data_.SetLastError(kViEBaseUnknownError);
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000348 return -1;
349 }
350 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000351}
352
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000353int ViEBaseImpl::StopReceive(const int video_channel) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000354 WEBRTC_TRACE(kTraceApiCall, kTraceVideo,
355 ViEId(shared_data_.instance_id(), video_channel),
356 "%s(channel: %d)", __FUNCTION__, video_channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000357
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000358 ViEChannelManagerScoped cs(*(shared_data_.channel_manager()));
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000359 ViEChannel* vie_channel = cs.Channel(video_channel);
360 if (!vie_channel) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000361 WEBRTC_TRACE(kTraceError, kTraceVideo,
362 ViEId(shared_data_.instance_id(), video_channel),
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000363 "%s: Channel %d does not exist", __FUNCTION__, video_channel);
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000364 shared_data_.SetLastError(kViEBaseInvalidChannelId);
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000365 return -1;
366 }
367 if (vie_channel->StopReceive() != 0) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000368 shared_data_.SetLastError(kViEBaseUnknownError);
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000369 return -1;
370 }
371 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000372}
373
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000374int ViEBaseImpl::GetVersion(char version[1024]) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000375 WEBRTC_TRACE(kTraceApiCall, kTraceVideo, ViEId(shared_data_.instance_id()),
niklase@google.com470e71d2011-07-07 08:21:25 +0000376 "GetVersion(version=?)");
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000377 assert(kViEVersionMaxMessageSize == 1024);
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000378 if (!version) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000379 shared_data_.SetLastError(kViEBaseInvalidArgument);
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000380 return -1;
381 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000382
mflodman@webrtc.org9ba151b2012-06-21 10:02:13 +0000383 // Add WebRTC Version.
384 std::stringstream version_stream;
elham@webrtc.org326bcff2013-11-25 17:19:46 +0000385 version_stream << "VideoEngine 3.47.0" << std::endl;
niklase@google.com470e71d2011-07-07 08:21:25 +0000386
mflodman@webrtc.org9ba151b2012-06-21 10:02:13 +0000387 // Add build info.
andrew@webrtc.org3054ba62013-12-04 17:00:44 +0000388 version_stream << "Build: " << BUILDINFO << std::endl;
niklase@google.com470e71d2011-07-07 08:21:25 +0000389
mflodman@webrtc.org9ba151b2012-06-21 10:02:13 +0000390#ifdef WEBRTC_EXTERNAL_TRANSPORT
391 version_stream << "External transport build" << std::endl;
392#endif
393 int version_length = version_stream.tellp();
394 assert(version_length < 1024);
395 memcpy(version, version_stream.str().c_str(), version_length);
leozwang@webrtc.org6c08f262012-07-13 22:00:16 +0000396 version[version_length] = '\0';
niklase@google.com470e71d2011-07-07 08:21:25 +0000397
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000398 WEBRTC_TRACE(kTraceStateInfo, kTraceVideo,
399 ViEId(shared_data_.instance_id()), "GetVersion() => %s",
400 version);
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000401 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000402}
403
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000404int ViEBaseImpl::LastError() {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000405 return shared_data_.LastErrorInternal();
niklase@google.com470e71d2011-07-07 08:21:25 +0000406}
407
mflodman@webrtc.org9ba151b2012-06-21 10:02:13 +0000408int ViEBaseImpl::CreateChannel(int& video_channel, // NOLINT
409 int original_channel, bool sender) {
mflodman@webrtc.org9ec883e2012-03-05 17:12:41 +0000410 ViEChannelManagerScoped cs(*(shared_data_.channel_manager()));
411 if (!cs.Channel(original_channel)) {
412 WEBRTC_TRACE(kTraceError, kTraceVideo, ViEId(shared_data_.instance_id()),
413 "%s - original_channel does not exist.", __FUNCTION__,
414 shared_data_.instance_id());
415 shared_data_.SetLastError(kViEBaseInvalidChannelId);
416 return -1;
417 }
418
mflodman@webrtc.orgab2610f2012-06-29 10:05:28 +0000419 if (shared_data_.channel_manager()->CreateChannel(&video_channel,
mflodman@webrtc.org9ec883e2012-03-05 17:12:41 +0000420 original_channel,
421 sender) == -1) {
422 WEBRTC_TRACE(kTraceError, kTraceVideo, ViEId(shared_data_.instance_id()),
423 "%s: Could not create channel", __FUNCTION__);
424 video_channel = -1;
425 shared_data_.SetLastError(kViEBaseChannelCreationFailed);
426 return -1;
427 }
428 WEBRTC_TRACE(kTraceInfo, kTraceVideo, ViEId(shared_data_.instance_id()),
429 "%s: channel created: %d", __FUNCTION__, video_channel);
430 return 0;
431}
432
mflodman@webrtc.org27a82a62011-11-30 18:04:26 +0000433} // namespace webrtc