1 /* 2 * libjingle 3 * Copyright 2011 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 #ifndef TALK_MEDIA_BASE_FAKEVIDEORENDERER_H_ 29 #define TALK_MEDIA_BASE_FAKEVIDEORENDERER_H_ 30 31 #include "talk/base/logging.h" 32 #include "talk/base/sigslot.h" 33 #include "talk/media/base/videoframe.h" 34 #include "talk/media/base/videorenderer.h" 35 36 namespace cricket { 37 38 // Faked video renderer that has a callback for actions on rendering. 39 class FakeVideoRenderer : public VideoRenderer { 40 public: 41 FakeVideoRenderer() 42 : errors_(0), 43 width_(0), 44 height_(0), 45 num_set_sizes_(0), 46 num_rendered_frames_(0), 47 black_frame_(false) { 48 } 49 50 virtual bool SetSize(int width, int height, int reserved) { 51 width_ = width; 52 height_ = height; 53 ++num_set_sizes_; 54 SignalSetSize(width, height, reserved); 55 return true; 56 } 57 58 virtual bool RenderFrame(const VideoFrame* frame) { 59 // TODO(zhurunz) Check with VP8 team to see if we can remove this 60 // tolerance on Y values. 61 black_frame_ = CheckFrameColorYuv(6, 48, 128, 128, 128, 128, frame); 62 // Treat unexpected frame size as error. 63 if (!frame || 64 frame->GetWidth() != static_cast<size_t>(width_) || 65 frame->GetHeight() != static_cast<size_t>(height_)) { 66 if (!frame) { 67 LOG(LS_WARNING) << "RenderFrame expected non-null frame."; 68 } else { 69 LOG(LS_WARNING) << "RenderFrame expected frame of size " << width_ 70 << "x" << height_ << " but received frame of size " 71 << frame->GetWidth() << "x" << frame->GetHeight(); 72 } 73 ++errors_; 74 return false; 75 } 76 ++num_rendered_frames_; 77 SignalRenderFrame(frame); 78 return true; 79 } 80 81 int errors() const { return errors_; } 82 int width() const { return width_; } 83 int height() const { return height_; } 84 int num_set_sizes() const { return num_set_sizes_; } 85 int num_rendered_frames() const { return num_rendered_frames_; } 86 bool black_frame() const { return black_frame_; } 87 88 sigslot::signal3<int, int, int> SignalSetSize; 89 sigslot::signal1<const VideoFrame*> SignalRenderFrame; 90 91 private: 92 static bool CheckFrameColorYuv(uint8 y_min, uint8 y_max, 93 uint8 u_min, uint8 u_max, 94 uint8 v_min, uint8 v_max, 95 const cricket::VideoFrame* frame) { 96 if (!frame) { 97 return false; 98 } 99 // Y 100 size_t y_width = frame->GetWidth(); 101 size_t y_height = frame->GetHeight(); 102 const uint8* y_plane = frame->GetYPlane(); 103 const uint8* y_pos = y_plane; 104 int32 y_pitch = frame->GetYPitch(); 105 for (size_t i = 0; i < y_height; ++i) { 106 for (size_t j = 0; j < y_width; ++j) { 107 uint8 y_value = *(y_pos + j); 108 if (y_value < y_min || y_value > y_max) { 109 return false; 110 } 111 } 112 y_pos += y_pitch; 113 } 114 // U and V 115 size_t chroma_width = frame->GetChromaWidth(); 116 size_t chroma_height = frame->GetChromaHeight(); 117 const uint8* u_plane = frame->GetUPlane(); 118 const uint8* v_plane = frame->GetVPlane(); 119 const uint8* u_pos = u_plane; 120 const uint8* v_pos = v_plane; 121 int32 u_pitch = frame->GetUPitch(); 122 int32 v_pitch = frame->GetVPitch(); 123 for (size_t i = 0; i < chroma_height; ++i) { 124 for (size_t j = 0; j < chroma_width; ++j) { 125 uint8 u_value = *(u_pos + j); 126 if (u_value < u_min || u_value > u_max) { 127 return false; 128 } 129 uint8 v_value = *(v_pos + j); 130 if (v_value < v_min || v_value > v_max) { 131 return false; 132 } 133 } 134 u_pos += u_pitch; 135 v_pos += v_pitch; 136 } 137 return true; 138 } 139 140 int errors_; 141 int width_; 142 int height_; 143 int num_set_sizes_; 144 int num_rendered_frames_; 145 bool black_frame_; 146 }; 147 148 } // namespace cricket 149 150 #endif // TALK_MEDIA_BASE_FAKEVIDEORENDERER_H_ 151