| 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" | |
| 13 | 12 |
| 14 namespace webrtc { | 13 // TODO(perkj): Remove this file once Chrome builds doesnt depend on it. |
| 15 | |
| 16 VideoTrackRenderers::VideoTrackRenderers() : enabled_(true) { | |
| 17 } | |
| 18 | |
| 19 VideoTrackRenderers::~VideoTrackRenderers() { | |
| 20 } | |
| 21 | |
| 22 void VideoTrackRenderers::AddOrUpdateSink( | |
| 23 VideoSinkInterface<cricket::VideoFrame>* sink, | |
| 24 const rtc::VideoSinkWants& wants) { | |
| 25 // TODO(nisse): Currently ignores wants. We should somehow use | |
| 26 // VideoBroadcaster, but we need to sort out its threading issues | |
| 27 // first. | |
| 28 rtc::CritScope cs(&critical_section_); | |
| 29 if (std::find(sinks_.begin(), sinks_.end(), sink) == sinks_.end()) | |
| 30 sinks_.push_back(sink); | |
| 31 } | |
| 32 | |
| 33 void VideoTrackRenderers::RemoveSink( | |
| 34 VideoSinkInterface<cricket::VideoFrame>* sink) { | |
| 35 rtc::CritScope cs(&critical_section_); | |
| 36 sinks_.erase(std::remove(sinks_.begin(), sinks_.end(), sink), sinks_.end()); | |
| 37 } | |
| 38 | |
| 39 void VideoTrackRenderers::SetEnabled(bool enable) { | |
| 40 rtc::CritScope cs(&critical_section_); | |
| 41 enabled_ = enable; | |
| 42 } | |
| 43 | |
| 44 bool VideoTrackRenderers::RenderFrame(const cricket::VideoFrame* frame) { | |
| 45 { | |
| 46 rtc::CritScope cs(&critical_section_); | |
| 47 if (enabled_) { | |
| 48 RenderFrameToSinks(*frame); | |
| 49 return true; | |
| 50 } | |
| 51 } | |
| 52 | |
| 53 // Generate the black frame outside of the critical section. Note | |
| 54 // that this may result in unexpected frame order, in the unlikely | |
| 55 // case that RenderFrame is called from multiple threads without | |
| 56 // proper serialization, and the track is switched from disabled to | |
| 57 // enabled in the middle of the first call. | |
| 58 cricket::WebRtcVideoFrame black(new rtc::RefCountedObject<I420Buffer>( | |
| 59 static_cast<int>(frame->GetWidth()), | |
| 60 static_cast<int>(frame->GetHeight())), | |
| 61 frame->GetTimeStamp(), | |
| 62 frame->GetVideoRotation()); | |
| 63 black.SetToBlack(); | |
| 64 | |
| 65 { | |
| 66 rtc::CritScope cs(&critical_section_); | |
| 67 // Check enabled_ flag again, since the track might have been | |
| 68 // enabled while we generated the black frame. I think the | |
| 69 // enabled-ness ought to be applied at the track output, and hence | |
| 70 // an enabled track shouldn't send any blacked out frames. | |
| 71 RenderFrameToSinks(enabled_ ? *frame : black); | |
| 72 | |
| 73 return true; | |
| 74 } | |
| 75 } | |
| 76 | |
| 77 // Called with critical_section_ already locked | |
| 78 void VideoTrackRenderers::RenderFrameToSinks(const cricket::VideoFrame& frame) { | |
| 79 for (auto sink : sinks_) { | |
| 80 sink->OnFrame(frame); | |
| 81 } | |
| 82 } | |
| 83 | |
| 84 } // namespace webrtc | |
| OLD | NEW |