blob: f855417b3c1e3948fa1870201413fa5a7b8c7456 [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
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000186bool HybridVideoMediaChannel::SetStartSendBandwidth(int bps) {
187 return active_channel_ && active_channel_->SetStartSendBandwidth(bps);
188}
189
190bool HybridVideoMediaChannel::SetMaxSendBandwidth(int bps) {
191 return active_channel_ && active_channel_->SetMaxSendBandwidth(bps);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000192}
193
194bool HybridVideoMediaChannel::SetSend(bool send) {
195 if (send == sending()) {
196 return true; // no action required if already set.
197 }
198
199 bool ret = active_channel_ &&
200 active_channel_->SetSend(send);
201
202 // Returns error and don't connect the signal if starting up.
203 // Disconnects the signal anyway if shutting down.
204 if (ret || !send) {
205 // TODO(juberti): Remove this hack that connects the WebRTC channel
206 // to the capturer.
207 if (active_channel_ == channel1_.get()) {
208 engine_->OnSendChange1(channel1_.get(), send);
209 } else {
210 engine_->OnSendChange2(channel2_.get(), send);
211 }
212 // If succeeded, remember the state as is.
213 // If failed to open, sending_ should be false.
214 // If failed to stop, sending_ should also be false, as we disconnect the
215 // capture anyway.
216 // The failure on SetSend(false) is a known issue in webrtc.
217 sending_ = send;
218 }
219 return ret;
220}
221
222bool HybridVideoMediaChannel::SetCapturer(uint32 ssrc,
223 VideoCapturer* capturer) {
224 bool ret = true;
225 if (channel1_.get()) {
226 ret = channel1_->SetCapturer(ssrc, capturer);
227 }
228 if (channel2_.get() && ret) {
229 ret = channel2_->SetCapturer(ssrc, capturer);
230 }
231 return ret;
232}
233
234bool HybridVideoMediaChannel::AddSendStream(const StreamParams& sp) {
235 bool ret = true;
236 if (channel1_) {
237 ret = channel1_->AddSendStream(sp);
238 }
239 if (channel2_ && ret) {
240 ret = channel2_->AddSendStream(sp);
241 }
242 return ret;
243}
244
245bool HybridVideoMediaChannel::RemoveSendStream(uint32 ssrc) {
246 bool ret = true;
247 if (channel1_) {
248 ret = channel1_->RemoveSendStream(ssrc);
249 }
250 if (channel2_ && ret) {
251 ret = channel2_->RemoveSendStream(ssrc);
252 }
253 return ret;
254}
255
256bool HybridVideoMediaChannel::AddRecvStream(const StreamParams& sp) {
257 return active_channel_ &&
258 active_channel_->AddRecvStream(sp);
259}
260
261bool HybridVideoMediaChannel::RemoveRecvStream(uint32 ssrc) {
262 return active_channel_ &&
263 active_channel_->RemoveRecvStream(ssrc);
264}
265
266bool HybridVideoMediaChannel::SendIntraFrame() {
267 return active_channel_ &&
268 active_channel_->SendIntraFrame();
269}
270
271bool HybridVideoMediaChannel::RequestIntraFrame() {
272 return active_channel_ &&
273 active_channel_->RequestIntraFrame();
274}
275
276bool HybridVideoMediaChannel::GetStats(VideoMediaInfo* info) {
277 // TODO(juberti): Ensure that returning no stats until SetSendCodecs is OK.
278 return active_channel_ &&
279 active_channel_->GetStats(info);
280}
281
wu@webrtc.orga9890802013-12-13 00:21:03 +0000282void HybridVideoMediaChannel::OnPacketReceived(
283 talk_base::Buffer* packet, const talk_base::PacketTime& packet_time) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000284 // Eat packets until we have an active channel;
285 if (active_channel_) {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000286 active_channel_->OnPacketReceived(packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000287 } else {
288 LOG(LS_INFO) << "HybridVideoChannel: Eating early RTP packet";
289 }
290}
291
wu@webrtc.orga9890802013-12-13 00:21:03 +0000292void HybridVideoMediaChannel::OnRtcpReceived(
293 talk_base::Buffer* packet, const talk_base::PacketTime& packet_time) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000294 // Eat packets until we have an active channel;
295 if (active_channel_) {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000296 active_channel_->OnRtcpReceived(packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000297 } else {
298 LOG(LS_INFO) << "HybridVideoChannel: Eating early RTCP packet";
299 }
300}
301
302void HybridVideoMediaChannel::OnReadyToSend(bool ready) {
303 if (channel1_) {
304 channel1_->OnReadyToSend(ready);
305 }
306 if (channel2_) {
307 channel2_->OnReadyToSend(ready);
308 }
309}
310
311void HybridVideoMediaChannel::UpdateAspectRatio(int ratio_w, int ratio_h) {
312 if (active_channel_) active_channel_->UpdateAspectRatio(ratio_w, ratio_h);
313}
314
315bool HybridVideoMediaChannel::SelectActiveChannel(
316 const std::vector<VideoCodec>& codecs) {
317 if (!active_channel_ && !codecs.empty()) {
318 if (engine_->HasCodec1(codecs[0])) {
319 channel2_.reset();
320 active_channel_ = channel1_.get();
321 } else if (engine_->HasCodec2(codecs[0])) {
322 channel1_.reset();
323 active_channel_ = channel2_.get();
324 }
325 }
326 if (NULL == active_channel_) {
327 return false;
328 }
329 // Connect signals from the active channel.
330 active_channel_->SignalMediaError.connect(
331 this,
332 &HybridVideoMediaChannel::OnMediaError);
333 return true;
334}
335
336void HybridVideoMediaChannel::SplitCodecs(
337 const std::vector<VideoCodec>& codecs,
338 std::vector<VideoCodec>* codecs1, std::vector<VideoCodec>* codecs2) {
339 codecs1->clear();
340 codecs2->clear();
341 for (size_t i = 0; i < codecs.size(); ++i) {
342 if (engine_->HasCodec1(codecs[i])) {
343 codecs1->push_back(codecs[i]);
344 }
345 if (engine_->HasCodec2(codecs[i])) {
346 codecs2->push_back(codecs[i]);
347 }
348 }
349}
350
351void HybridVideoMediaChannel::OnMediaError(uint32 ssrc, Error error) {
352 SignalMediaError(ssrc, error);
353}
354
355} // namespace cricket