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* video_renderers_ 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 bool CaptureRenderAdapter::AddRenderer(VideoRenderer* video_renderer) { 65 if (!video_renderer) { 66 return false; 67 } 68 rtc::CritScope cs(&capture_crit_); 69 if (IsRendererRegistered(*video_renderer)) { 70 return false; 71 } 72 video_renderers_.push_back(VideoRendererInfo(video_renderer)); 73 return true; 74 } 75 76 bool CaptureRenderAdapter::RemoveRenderer(VideoRenderer* video_renderer) { 77 if (!video_renderer) { 78 return false; 79 } 80 rtc::CritScope cs(&capture_crit_); 81 for (VideoRenderers::iterator iter = video_renderers_.begin(); 82 iter != video_renderers_.end(); ++iter) { 83 if (video_renderer == iter->renderer) { 84 video_renderers_.erase(iter); 85 return true; 86 } 87 } 88 return false; 89 } 90 91 void CaptureRenderAdapter::Init() { 92 video_capturer_->SignalVideoFrame.connect( 93 this, 94 &CaptureRenderAdapter::OnVideoFrame); 95 } 96 97 void CaptureRenderAdapter::OnVideoFrame(VideoCapturer* capturer, 98 const VideoFrame* video_frame) { 99 rtc::CritScope cs(&capture_crit_); 100 if (video_renderers_.empty()) { 101 return; 102 } 103 MaybeSetRenderingSize(video_frame); 104 105 for (VideoRenderers::iterator iter = video_renderers_.begin(); 106 iter != video_renderers_.end(); ++iter) { 107 VideoRenderer* video_renderer = iter->renderer; 108 video_renderer->RenderFrame(video_frame); 109 } 110 } 111 112 // The renderer_crit_ lock needs to be taken when calling this function. 113 void CaptureRenderAdapter::MaybeSetRenderingSize(const VideoFrame* frame) { 114 for (VideoRenderers::iterator iter = video_renderers_.begin(); 115 iter != video_renderers_.end(); ++iter) { 116 const bool new_resolution = iter->render_width != frame->GetWidth() || 117 iter->render_height != frame->GetHeight(); 118 if (new_resolution) { 119 if (iter->renderer->SetSize(static_cast<int>(frame->GetWidth()), 120 static_cast<int>(frame->GetHeight()), 0)) { 121 iter->render_width = frame->GetWidth(); 122 iter->render_height = frame->GetHeight(); 123 } else { 124 LOG(LS_ERROR) << "Captured frame size not supported by renderer: " << 125 frame->GetWidth() << " x " << frame->GetHeight(); 126 } 127 } 128 } 129 } 130 131 // The renderer_crit_ lock needs to be taken when calling this function. 132 bool CaptureRenderAdapter::IsRendererRegistered( 133 const VideoRenderer& video_renderer) const { 134 for (VideoRenderers::const_iterator iter = video_renderers_.begin(); 135 iter != video_renderers_.end(); ++iter) { 136 if (&video_renderer == iter->renderer) { 137 return true; 138 } 139 } 140 return false; 141 } 142 143 } // namespace cricket 144