blob: 31e19fc72c530314bb79bb5fcba7be2cc8c26624 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00003 * Copyright 2012 Google Inc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004 *
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
kjellandera96e2d72016-02-04 23:52:28 -080028#include "webrtc/media/base/capturerenderadapter.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000029
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000030#include "webrtc/base/logging.h"
kjellandera96e2d72016-02-04 23:52:28 -080031#include "webrtc/media/base/videocapturer.h"
32#include "webrtc/media/base/videorenderer.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000033
34namespace cricket {
35
36CaptureRenderAdapter::CaptureRenderAdapter(VideoCapturer* video_capturer)
37 : video_capturer_(video_capturer) {
38}
39
40CaptureRenderAdapter::~CaptureRenderAdapter() {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000041 // Since the signal we're connecting to is multi-threaded,
42 // disconnect_all() will block until all calls are serviced, meaning any
43 // outstanding calls to OnVideoFrame will be done when this is done, and no
44 // more calls will be serviced by this.
45 // We do this explicitly instead of just letting the has_slots<> destructor
nissee73afba2016-01-28 04:47:08 -080046 // take care of it because we need to do this *before* sinks_ is
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000047 // cleared by the destructor; otherwise we could mess with it while
48 // OnVideoFrame is running.
49 // We *don't* take capture_crit_ here since it could deadlock with the lock
50 // taken by the video frame signal.
51 disconnect_all();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000052}
53
54CaptureRenderAdapter* CaptureRenderAdapter::Create(
55 VideoCapturer* video_capturer) {
56 if (!video_capturer) {
57 return NULL;
58 }
59 CaptureRenderAdapter* return_value = new CaptureRenderAdapter(video_capturer);
60 return_value->Init(); // Can't fail.
61 return return_value;
62}
63
nissee73afba2016-01-28 04:47:08 -080064void CaptureRenderAdapter::AddSink(rtc::VideoSinkInterface<VideoFrame>* sink) {
65 RTC_DCHECK(sink);
nisse0b98cf72016-01-21 03:04:30 -080066
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000067 rtc::CritScope cs(&capture_crit_);
nisse0b98cf72016-01-21 03:04:30 -080068 // This implements set semantics, the same renderer can only be
69 // added once.
70 // TODO(nisse): Is this really needed?
nissee73afba2016-01-28 04:47:08 -080071 if (std::find(sinks_.begin(), sinks_.end(), sink) == sinks_.end())
72 sinks_.push_back(sink);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000073}
74
nissee73afba2016-01-28 04:47:08 -080075void CaptureRenderAdapter::RemoveSink(
76 rtc::VideoSinkInterface<VideoFrame>* sink) {
77 RTC_DCHECK(sink);
nisse0b98cf72016-01-21 03:04:30 -080078
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000079 rtc::CritScope cs(&capture_crit_);
nissee73afba2016-01-28 04:47:08 -080080 sinks_.erase(std::remove(sinks_.begin(), sinks_.end(), sink), sinks_.end());
henrike@webrtc.org28e20752013-07-10 00:45:36 +000081}
82
83void CaptureRenderAdapter::Init() {
84 video_capturer_->SignalVideoFrame.connect(
85 this,
86 &CaptureRenderAdapter::OnVideoFrame);
87}
88
89void CaptureRenderAdapter::OnVideoFrame(VideoCapturer* capturer,
90 const VideoFrame* video_frame) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000091 rtc::CritScope cs(&capture_crit_);
nissee73afba2016-01-28 04:47:08 -080092 if (sinks_.empty()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000093 return;
94 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000095
nissee73afba2016-01-28 04:47:08 -080096 for (auto* sink : sinks_)
97 sink->OnFrame(*video_frame);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000098}
99
100} // namespace cricket