OLD | NEW |
| (Empty) |
1 /* | |
2 * libjingle | |
3 * Copyright 2012 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/capturerenderadapter.h" | |
29 | |
30 #include "talk/media/base/videocapturer.h" | |
31 #include "talk/media/base/videorenderer.h" | |
32 #include "webrtc/base/logging.h" | |
33 | |
34 namespace cricket { | |
35 | |
36 CaptureRenderAdapter::CaptureRenderAdapter(VideoCapturer* video_capturer) | |
37 : video_capturer_(video_capturer) { | |
38 } | |
39 | |
40 CaptureRenderAdapter::~CaptureRenderAdapter() { | |
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 | |
46 // take care of it because we need to do this *before* sinks_ is | |
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(); | |
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 | |
64 void CaptureRenderAdapter::AddSink(rtc::VideoSinkInterface<VideoFrame>* sink) { | |
65 RTC_DCHECK(sink); | |
66 | |
67 rtc::CritScope cs(&capture_crit_); | |
68 // This implements set semantics, the same renderer can only be | |
69 // added once. | |
70 // TODO(nisse): Is this really needed? | |
71 if (std::find(sinks_.begin(), sinks_.end(), sink) == sinks_.end()) | |
72 sinks_.push_back(sink); | |
73 } | |
74 | |
75 void CaptureRenderAdapter::RemoveSink( | |
76 rtc::VideoSinkInterface<VideoFrame>* sink) { | |
77 RTC_DCHECK(sink); | |
78 | |
79 rtc::CritScope cs(&capture_crit_); | |
80 sinks_.erase(std::remove(sinks_.begin(), sinks_.end(), sink), sinks_.end()); | |
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) { | |
91 rtc::CritScope cs(&capture_crit_); | |
92 if (sinks_.empty()) { | |
93 return; | |
94 } | |
95 | |
96 for (auto* sink : sinks_) | |
97 sink->OnFrame(*video_frame); | |
98 } | |
99 | |
100 } // namespace cricket | |
OLD | NEW |