OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license | |
5 * that can be found in the LICENSE file in the root of the source | |
6 * tree. An additional intellectual property rights grant can be found | |
7 * in the file PATENTS. All contributing project authors may | |
8 * be found in the AUTHORS file in the root of the source tree. | |
9 */ | |
10 | |
11 #include "webrtc/media/base/capturerenderadapter.h" | |
12 | |
13 #include "webrtc/base/logging.h" | |
14 #include "webrtc/media/base/videocapturer.h" | |
15 #include "webrtc/media/base/videorenderer.h" | |
16 | |
17 namespace cricket { | |
18 | |
19 CaptureRenderAdapter::CaptureRenderAdapter(VideoCapturer* video_capturer) | |
20 : video_capturer_(video_capturer) { | |
21 } | |
22 | |
23 CaptureRenderAdapter::~CaptureRenderAdapter() { | |
24 // Since the signal we're connecting to is multi-threaded, | |
25 // disconnect_all() will block until all calls are serviced, meaning any | |
26 // outstanding calls to OnVideoFrame will be done when this is done, and no | |
27 // more calls will be serviced by this. | |
28 // We do this explicitly instead of just letting the has_slots<> destructor | |
29 // take care of it because we need to do this *before* sinks_ is | |
30 // cleared by the destructor; otherwise we could mess with it while | |
31 // OnVideoFrame is running. | |
32 // We *don't* take capture_crit_ here since it could deadlock with the lock | |
33 // taken by the video frame signal. | |
34 disconnect_all(); | |
35 } | |
36 | |
37 CaptureRenderAdapter* CaptureRenderAdapter::Create( | |
38 VideoCapturer* video_capturer) { | |
39 if (!video_capturer) { | |
40 return NULL; | |
41 } | |
42 CaptureRenderAdapter* return_value = new CaptureRenderAdapter(video_capturer); | |
43 return_value->Init(); // Can't fail. | |
44 return return_value; | |
45 } | |
46 | |
47 void CaptureRenderAdapter::AddSink(rtc::VideoSinkInterface<VideoFrame>* sink) { | |
48 RTC_DCHECK(sink); | |
49 | |
50 rtc::CritScope cs(&capture_crit_); | |
51 // This implements set semantics, the same renderer can only be | |
52 // added once. | |
53 // TODO(nisse): Is this really needed? | |
54 if (std::find(sinks_.begin(), sinks_.end(), sink) == sinks_.end()) | |
55 sinks_.push_back(sink); | |
56 } | |
57 | |
58 void CaptureRenderAdapter::RemoveSink( | |
59 rtc::VideoSinkInterface<VideoFrame>* sink) { | |
60 RTC_DCHECK(sink); | |
61 | |
62 rtc::CritScope cs(&capture_crit_); | |
63 sinks_.erase(std::remove(sinks_.begin(), sinks_.end(), sink), sinks_.end()); | |
64 } | |
65 | |
66 void CaptureRenderAdapter::Init() { | |
67 video_capturer_->SignalVideoFrame.connect( | |
68 this, | |
69 &CaptureRenderAdapter::OnVideoFrame); | |
70 } | |
71 | |
72 void CaptureRenderAdapter::OnVideoFrame(VideoCapturer* capturer, | |
73 const VideoFrame* video_frame) { | |
74 rtc::CritScope cs(&capture_crit_); | |
75 if (sinks_.empty()) { | |
76 return; | |
77 } | |
78 | |
79 for (auto* sink : sinks_) | |
80 sink->OnFrame(*video_frame); | |
81 } | |
82 | |
83 } // namespace cricket | |
OLD | NEW |