henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 +0000 | [diff] [blame] | 3 | * Copyright 2012 Google Inc. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 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 | |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame^] | 28 | #include "webrtc/media/base/capturerenderadapter.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 29 | |
buildbot@webrtc.org | a09a999 | 2014-08-13 17:26:08 +0000 | [diff] [blame] | 30 | #include "webrtc/base/logging.h" |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame^] | 31 | #include "webrtc/media/base/videocapturer.h" |
| 32 | #include "webrtc/media/base/videorenderer.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 33 | |
| 34 | namespace cricket { |
| 35 | |
| 36 | CaptureRenderAdapter::CaptureRenderAdapter(VideoCapturer* video_capturer) |
| 37 | : video_capturer_(video_capturer) { |
| 38 | } |
| 39 | |
| 40 | CaptureRenderAdapter::~CaptureRenderAdapter() { |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 41 | // 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 |
nisse | e73afba | 2016-01-28 04:47:08 -0800 | [diff] [blame] | 46 | // take care of it because we need to do this *before* sinks_ is |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 47 | // 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | CaptureRenderAdapter* 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 | |
nisse | e73afba | 2016-01-28 04:47:08 -0800 | [diff] [blame] | 64 | void CaptureRenderAdapter::AddSink(rtc::VideoSinkInterface<VideoFrame>* sink) { |
| 65 | RTC_DCHECK(sink); |
nisse | 0b98cf7 | 2016-01-21 03:04:30 -0800 | [diff] [blame] | 66 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 67 | rtc::CritScope cs(&capture_crit_); |
nisse | 0b98cf7 | 2016-01-21 03:04:30 -0800 | [diff] [blame] | 68 | // This implements set semantics, the same renderer can only be |
| 69 | // added once. |
| 70 | // TODO(nisse): Is this really needed? |
nisse | e73afba | 2016-01-28 04:47:08 -0800 | [diff] [blame] | 71 | if (std::find(sinks_.begin(), sinks_.end(), sink) == sinks_.end()) |
| 72 | sinks_.push_back(sink); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 73 | } |
| 74 | |
nisse | e73afba | 2016-01-28 04:47:08 -0800 | [diff] [blame] | 75 | void CaptureRenderAdapter::RemoveSink( |
| 76 | rtc::VideoSinkInterface<VideoFrame>* sink) { |
| 77 | RTC_DCHECK(sink); |
nisse | 0b98cf7 | 2016-01-21 03:04:30 -0800 | [diff] [blame] | 78 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 79 | rtc::CritScope cs(&capture_crit_); |
nisse | e73afba | 2016-01-28 04:47:08 -0800 | [diff] [blame] | 80 | sinks_.erase(std::remove(sinks_.begin(), sinks_.end(), sink), sinks_.end()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | void CaptureRenderAdapter::Init() { |
| 84 | video_capturer_->SignalVideoFrame.connect( |
| 85 | this, |
| 86 | &CaptureRenderAdapter::OnVideoFrame); |
| 87 | } |
| 88 | |
| 89 | void CaptureRenderAdapter::OnVideoFrame(VideoCapturer* capturer, |
| 90 | const VideoFrame* video_frame) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 91 | rtc::CritScope cs(&capture_crit_); |
nisse | e73afba | 2016-01-28 04:47:08 -0800 | [diff] [blame] | 92 | if (sinks_.empty()) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 93 | return; |
| 94 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 95 | |
nisse | e73afba | 2016-01-28 04:47:08 -0800 | [diff] [blame] | 96 | for (auto* sink : sinks_) |
| 97 | sink->OnFrame(*video_frame); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | } // namespace cricket |