OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright 2012 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/api/videotrackrenderers.h" | 11 #include "webrtc/api/videotrackrenderers.h" |
12 #include "webrtc/media/engine/webrtcvideoframe.h" | 12 #include "webrtc/media/engine/webrtcvideoframe.h" |
13 | 13 |
14 namespace webrtc { | 14 namespace webrtc { |
15 | 15 |
16 VideoTrackRenderers::VideoTrackRenderers() : enabled_(true) { | 16 VideoTrackRenderers::VideoTrackRenderers() : enabled_(true) { |
17 } | 17 } |
18 | 18 |
19 VideoTrackRenderers::~VideoTrackRenderers() { | 19 VideoTrackRenderers::~VideoTrackRenderers() { |
20 } | 20 } |
21 | 21 |
22 void VideoTrackRenderers::AddRenderer(VideoRendererInterface* renderer) { | 22 void VideoTrackRenderers::AddOrUpdateSink( |
23 if (!renderer) { | 23 VideoSinkInterface<cricket::VideoFrame>* sink, |
24 return; | 24 const rtc::VideoSinkWants& wants) { |
25 } | 25 // TODO(nisse): Currently ignores wants. We should somehow use |
| 26 // VideoBroadcaster, but we need to sort out its threading issues |
| 27 // first. |
26 rtc::CritScope cs(&critical_section_); | 28 rtc::CritScope cs(&critical_section_); |
27 renderers_.insert(renderer); | 29 if (std::find(sinks_.begin(), sinks_.end(), sink) == sinks_.end()) |
| 30 sinks_.push_back(sink); |
28 } | 31 } |
29 | 32 |
30 void VideoTrackRenderers::RemoveRenderer(VideoRendererInterface* renderer) { | 33 void VideoTrackRenderers::RemoveSink( |
| 34 VideoSinkInterface<cricket::VideoFrame>* sink) { |
31 rtc::CritScope cs(&critical_section_); | 35 rtc::CritScope cs(&critical_section_); |
32 renderers_.erase(renderer); | 36 sinks_.erase(std::remove(sinks_.begin(), sinks_.end(), sink), sinks_.end()); |
33 } | 37 } |
34 | 38 |
35 void VideoTrackRenderers::SetEnabled(bool enable) { | 39 void VideoTrackRenderers::SetEnabled(bool enable) { |
36 rtc::CritScope cs(&critical_section_); | 40 rtc::CritScope cs(&critical_section_); |
37 enabled_ = enable; | 41 enabled_ = enable; |
38 } | 42 } |
39 | 43 |
40 bool VideoTrackRenderers::RenderFrame(const cricket::VideoFrame* frame) { | 44 bool VideoTrackRenderers::RenderFrame(const cricket::VideoFrame* frame) { |
41 { | 45 { |
42 rtc::CritScope cs(&critical_section_); | 46 rtc::CritScope cs(&critical_section_); |
43 if (enabled_) { | 47 if (enabled_) { |
44 RenderFrameToRenderers(frame); | 48 RenderFrameToSinks(*frame); |
45 return true; | 49 return true; |
46 } | 50 } |
47 } | 51 } |
48 | 52 |
49 // Generate the black frame outside of the critical section. Note | 53 // Generate the black frame outside of the critical section. Note |
50 // that this may result in unexpected frame order, in the unlikely | 54 // that this may result in unexpected frame order, in the unlikely |
51 // case that RenderFrame is called from multiple threads without | 55 // case that RenderFrame is called from multiple threads without |
52 // proper serialization, and the track is switched from disabled to | 56 // proper serialization, and the track is switched from disabled to |
53 // enabled in the middle of the first call. | 57 // enabled in the middle of the first call. |
54 cricket::WebRtcVideoFrame black(new rtc::RefCountedObject<I420Buffer>( | 58 cricket::WebRtcVideoFrame black(new rtc::RefCountedObject<I420Buffer>( |
55 static_cast<int>(frame->GetWidth()), | 59 static_cast<int>(frame->GetWidth()), |
56 static_cast<int>(frame->GetHeight())), | 60 static_cast<int>(frame->GetHeight())), |
57 frame->GetTimeStamp(), | 61 frame->GetTimeStamp(), |
58 frame->GetVideoRotation()); | 62 frame->GetVideoRotation()); |
59 black.SetToBlack(); | 63 black.SetToBlack(); |
60 | 64 |
61 { | 65 { |
62 rtc::CritScope cs(&critical_section_); | 66 rtc::CritScope cs(&critical_section_); |
63 // Check enabled_ flag again, since the track might have been | 67 // Check enabled_ flag again, since the track might have been |
64 // enabled while we generated the black frame. I think the | 68 // enabled while we generated the black frame. I think the |
65 // enabled-ness ought to be applied at the track output, and hence | 69 // enabled-ness ought to be applied at the track output, and hence |
66 // an enabled track shouldn't send any blacked out frames. | 70 // an enabled track shouldn't send any blacked out frames. |
67 RenderFrameToRenderers(enabled_ ? frame : &black); | 71 RenderFrameToSinks(enabled_ ? *frame : black); |
68 | 72 |
69 return true; | 73 return true; |
70 } | 74 } |
71 } | 75 } |
72 | 76 |
73 // Called with critical_section_ already locked | 77 // Called with critical_section_ already locked |
74 void VideoTrackRenderers::RenderFrameToRenderers( | 78 void VideoTrackRenderers::RenderFrameToSinks(const cricket::VideoFrame& frame) { |
75 const cricket::VideoFrame* frame) { | 79 for (auto sink : sinks_) { |
76 for (VideoRendererInterface* renderer : renderers_) { | 80 sink->OnFrame(frame); |
77 renderer->RenderFrame(frame); | |
78 } | 81 } |
79 } | 82 } |
80 | 83 |
81 } // namespace webrtc | 84 } // namespace webrtc |
OLD | NEW |