blob: 6863311f2dda77a86c69a6f7b8ee2efc7aee2eaa [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2004 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "talk/media/base/hybridvideoengine.h"
29
30#include "talk/base/logging.h"
31
32namespace cricket {
33
34HybridVideoMediaChannel::HybridVideoMediaChannel(
35 HybridVideoEngineInterface* engine,
36 VideoMediaChannel* channel1,
37 VideoMediaChannel* channel2)
38 : engine_(engine),
39 channel1_(channel1),
40 channel2_(channel2),
41 active_channel_(NULL),
42 sending_(false) {
43}
44
45HybridVideoMediaChannel::~HybridVideoMediaChannel() {
46}
47
48void HybridVideoMediaChannel::SetInterface(NetworkInterface* iface) {
49 if (channel1_) {
50 channel1_->SetInterface(iface);
51 }
52 if (channel2_) {
53 channel2_->SetInterface(iface);
54 }
55}
56
57bool HybridVideoMediaChannel::SetOptions(const VideoOptions &options) {
58 bool ret = true;
59 if (channel1_) {
60 ret = channel1_->SetOptions(options);
61 }
62 if (channel2_ && ret) {
63 ret = channel2_->SetOptions(options);
64 }
65 return ret;
66}
67
68bool HybridVideoMediaChannel::GetOptions(VideoOptions *options) const {
69 if (active_channel_) {
70 return active_channel_->GetOptions(options);
71 }
72 if (channel1_) {
73 return channel1_->GetOptions(options);
74 }
75 if (channel2_) {
76 return channel2_->GetOptions(options);
77 }
78 return false;
79}
80
81bool HybridVideoMediaChannel::SetRecvCodecs(
82 const std::vector<VideoCodec>& codecs) {
83 // Only give each channel the codecs it knows about.
84 bool ret = true;
85 std::vector<VideoCodec> codecs1, codecs2;
86 SplitCodecs(codecs, &codecs1, &codecs2);
87 if (channel1_) {
88 ret = channel1_->SetRecvCodecs(codecs1);
89 }
90 if (channel2_ && ret) {
91 ret = channel2_->SetRecvCodecs(codecs2);
92 }
93 return ret;
94}
95
96bool HybridVideoMediaChannel::SetRecvRtpHeaderExtensions(
97 const std::vector<RtpHeaderExtension>& extensions) {
98 bool ret = true;
99 if (channel1_) {
100 ret = channel1_->SetRecvRtpHeaderExtensions(extensions);
101 }
102 if (channel2_ && ret) {
103 ret = channel2_->SetRecvRtpHeaderExtensions(extensions);
104 }
105 return ret;
106}
107
108bool HybridVideoMediaChannel::SetRenderer(uint32 ssrc,
109 VideoRenderer* renderer) {
110 bool ret = true;
111 if (channel1_) {
112 ret = channel1_->SetRenderer(ssrc, renderer);
113 }
114 if (channel2_ && ret) {
115 ret = channel2_->SetRenderer(ssrc, renderer);
116 }
117 return ret;
118}
119
120bool HybridVideoMediaChannel::SetRender(bool render) {
121 bool ret = true;
122 if (channel1_) {
123 ret = channel1_->SetRender(render);
124 }
125 if (channel2_ && ret) {
126 ret = channel2_->SetRender(render);
127 }
128 return ret;
129}
130
131bool HybridVideoMediaChannel::MuteStream(uint32 ssrc, bool muted) {
132 bool ret = true;
133 if (channel1_) {
134 ret = channel1_->MuteStream(ssrc, muted);
135 }
136 if (channel2_ && ret) {
137 ret = channel2_->MuteStream(ssrc, muted);
138 }
139 return ret;
140}
141
142bool HybridVideoMediaChannel::SetSendCodecs(
143 const std::vector<VideoCodec>& codecs) {
144 // Use the input to this function to decide what impl we're going to use.
145 if (!active_channel_ && !SelectActiveChannel(codecs)) {
146 LOG(LS_WARNING) << "Failed to select active channel";
147 return false;
148 }
149 // Only give the active channel the codecs it knows about.
150 std::vector<VideoCodec> codecs1, codecs2;
151 SplitCodecs(codecs, &codecs1, &codecs2);
152 const std::vector<VideoCodec>& codecs_to_set =
153 (active_channel_ == channel1_.get()) ? codecs1 : codecs2;
154 bool return_value = active_channel_->SetSendCodecs(codecs_to_set);
155 if (!return_value) {
156 return false;
157 }
158 VideoCodec send_codec;
159 return_value = active_channel_->GetSendCodec(&send_codec);
160 if (!return_value) {
161 return false;
162 }
163 engine_->OnNewSendResolution(send_codec.width, send_codec.height);
164 active_channel_->UpdateAspectRatio(send_codec.width, send_codec.height);
165 return true;
166}
167
168bool HybridVideoMediaChannel::GetSendCodec(VideoCodec* send_codec) {
169 if (!active_channel_) {
170 return false;
171 }
172 return active_channel_->GetSendCodec(send_codec);
173}
174
175bool HybridVideoMediaChannel::SetSendStreamFormat(uint32 ssrc,
176 const VideoFormat& format) {
177 return active_channel_ && active_channel_->SetSendStreamFormat(ssrc, format);
178}
179
180bool HybridVideoMediaChannel::SetSendRtpHeaderExtensions(
181 const std::vector<RtpHeaderExtension>& extensions) {
182 return active_channel_ &&
183 active_channel_->SetSendRtpHeaderExtensions(extensions);
184}
185
186bool HybridVideoMediaChannel::SetSendBandwidth(bool autobw, int bps) {
187 return active_channel_ &&
188 active_channel_->SetSendBandwidth(autobw, bps);
189}
190
191bool HybridVideoMediaChannel::SetSend(bool send) {
192 if (send == sending()) {
193 return true; // no action required if already set.
194 }
195
196 bool ret = active_channel_ &&
197 active_channel_->SetSend(send);
198
199 // Returns error and don't connect the signal if starting up.
200 // Disconnects the signal anyway if shutting down.
201 if (ret || !send) {
202 // TODO(juberti): Remove this hack that connects the WebRTC channel
203 // to the capturer.
204 if (active_channel_ == channel1_.get()) {
205 engine_->OnSendChange1(channel1_.get(), send);
206 } else {
207 engine_->OnSendChange2(channel2_.get(), send);
208 }
209 // If succeeded, remember the state as is.
210 // If failed to open, sending_ should be false.
211 // If failed to stop, sending_ should also be false, as we disconnect the
212 // capture anyway.
213 // The failure on SetSend(false) is a known issue in webrtc.
214 sending_ = send;
215 }
216 return ret;
217}
218
219bool HybridVideoMediaChannel::SetCapturer(uint32 ssrc,
220 VideoCapturer* capturer) {
221 bool ret = true;
222 if (channel1_.get()) {
223 ret = channel1_->SetCapturer(ssrc, capturer);
224 }
225 if (channel2_.get() && ret) {
226 ret = channel2_->SetCapturer(ssrc, capturer);
227 }
228 return ret;
229}
230
231bool HybridVideoMediaChannel::AddSendStream(const StreamParams& sp) {
232 bool ret = true;
233 if (channel1_) {
234 ret = channel1_->AddSendStream(sp);
235 }
236 if (channel2_ && ret) {
237 ret = channel2_->AddSendStream(sp);
238 }
239 return ret;
240}
241
242bool HybridVideoMediaChannel::RemoveSendStream(uint32 ssrc) {
243 bool ret = true;
244 if (channel1_) {
245 ret = channel1_->RemoveSendStream(ssrc);
246 }
247 if (channel2_ && ret) {
248 ret = channel2_->RemoveSendStream(ssrc);
249 }
250 return ret;
251}
252
253bool HybridVideoMediaChannel::AddRecvStream(const StreamParams& sp) {
254 return active_channel_ &&
255 active_channel_->AddRecvStream(sp);
256}
257
258bool HybridVideoMediaChannel::RemoveRecvStream(uint32 ssrc) {
259 return active_channel_ &&
260 active_channel_->RemoveRecvStream(ssrc);
261}
262
263bool HybridVideoMediaChannel::SendIntraFrame() {
264 return active_channel_ &&
265 active_channel_->SendIntraFrame();
266}
267
268bool HybridVideoMediaChannel::RequestIntraFrame() {
269 return active_channel_ &&
270 active_channel_->RequestIntraFrame();
271}
272
273bool HybridVideoMediaChannel::GetStats(VideoMediaInfo* info) {
274 // TODO(juberti): Ensure that returning no stats until SetSendCodecs is OK.
275 return active_channel_ &&
276 active_channel_->GetStats(info);
277}
278
wu@webrtc.orga9890802013-12-13 00:21:03 +0000279void HybridVideoMediaChannel::OnPacketReceived(
280 talk_base::Buffer* packet, const talk_base::PacketTime& packet_time) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000281 // Eat packets until we have an active channel;
282 if (active_channel_) {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000283 active_channel_->OnPacketReceived(packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000284 } else {
285 LOG(LS_INFO) << "HybridVideoChannel: Eating early RTP packet";
286 }
287}
288
wu@webrtc.orga9890802013-12-13 00:21:03 +0000289void HybridVideoMediaChannel::OnRtcpReceived(
290 talk_base::Buffer* packet, const talk_base::PacketTime& packet_time) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000291 // Eat packets until we have an active channel;
292 if (active_channel_) {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000293 active_channel_->OnRtcpReceived(packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000294 } else {
295 LOG(LS_INFO) << "HybridVideoChannel: Eating early RTCP packet";
296 }
297}
298
299void HybridVideoMediaChannel::OnReadyToSend(bool ready) {
300 if (channel1_) {
301 channel1_->OnReadyToSend(ready);
302 }
303 if (channel2_) {
304 channel2_->OnReadyToSend(ready);
305 }
306}
307
308void HybridVideoMediaChannel::UpdateAspectRatio(int ratio_w, int ratio_h) {
309 if (active_channel_) active_channel_->UpdateAspectRatio(ratio_w, ratio_h);
310}
311
312bool HybridVideoMediaChannel::SelectActiveChannel(
313 const std::vector<VideoCodec>& codecs) {
314 if (!active_channel_ && !codecs.empty()) {
315 if (engine_->HasCodec1(codecs[0])) {
316 channel2_.reset();
317 active_channel_ = channel1_.get();
318 } else if (engine_->HasCodec2(codecs[0])) {
319 channel1_.reset();
320 active_channel_ = channel2_.get();
321 }
322 }
323 if (NULL == active_channel_) {
324 return false;
325 }
326 // Connect signals from the active channel.
327 active_channel_->SignalMediaError.connect(
328 this,
329 &HybridVideoMediaChannel::OnMediaError);
330 return true;
331}
332
333void HybridVideoMediaChannel::SplitCodecs(
334 const std::vector<VideoCodec>& codecs,
335 std::vector<VideoCodec>* codecs1, std::vector<VideoCodec>* codecs2) {
336 codecs1->clear();
337 codecs2->clear();
338 for (size_t i = 0; i < codecs.size(); ++i) {
339 if (engine_->HasCodec1(codecs[i])) {
340 codecs1->push_back(codecs[i]);
341 }
342 if (engine_->HasCodec2(codecs[i])) {
343 codecs2->push_back(codecs[i]);
344 }
345 }
346}
347
348void HybridVideoMediaChannel::OnMediaError(uint32 ssrc, Error error) {
349 SignalMediaError(ssrc, error);
350}
351
352} // namespace cricket