fake_video_renderer.cc (2990B)
1 /* 2 * Copyright (c) 2018 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 "media/base/fake_video_renderer.h" 12 13 #include <cstdint> 14 15 #include "api/scoped_refptr.h" 16 #include "api/video/video_frame.h" 17 #include "api/video/video_frame_buffer.h" 18 #include "rtc_base/synchronization/mutex.h" 19 20 namespace webrtc { 21 namespace { 22 bool CheckFrameColorYuv(const VideoFrame& frame) { 23 // TODO(zhurunz) Check with VP8 team to see if we can remove this 24 // tolerance on Y values. Some unit tests produce Y values close 25 // to 16 rather than close to zero, for supposedly black frames. 26 // Largest value observed is 34, e.g., running 27 // PeerConnectionIntegrationTest.SendAndReceive16To9AspectRatio. 28 static constexpr uint8_t y_min = 0; 29 static constexpr uint8_t y_max = 48; 30 static constexpr uint8_t u_min = 128; 31 static constexpr uint8_t u_max = 128; 32 static constexpr uint8_t v_min = 128; 33 static constexpr uint8_t v_max = 128; 34 35 if (!frame.video_frame_buffer()) { 36 return false; 37 } 38 scoped_refptr<const I420BufferInterface> i420_buffer = 39 frame.video_frame_buffer()->ToI420(); 40 // Y 41 int y_width = frame.width(); 42 int y_height = frame.height(); 43 const uint8_t* y_plane = i420_buffer->DataY(); 44 const uint8_t* y_pos = y_plane; 45 int32_t y_pitch = i420_buffer->StrideY(); 46 for (int i = 0; i < y_height; ++i) { 47 for (int j = 0; j < y_width; ++j) { 48 uint8_t y_value = *(y_pos + j); 49 if (y_value < y_min || y_value > y_max) { 50 return false; 51 } 52 } 53 y_pos += y_pitch; 54 } 55 // U and V 56 int chroma_width = i420_buffer->ChromaWidth(); 57 int chroma_height = i420_buffer->ChromaHeight(); 58 const uint8_t* u_plane = i420_buffer->DataU(); 59 const uint8_t* v_plane = i420_buffer->DataV(); 60 const uint8_t* u_pos = u_plane; 61 const uint8_t* v_pos = v_plane; 62 int32_t u_pitch = i420_buffer->StrideU(); 63 int32_t v_pitch = i420_buffer->StrideV(); 64 for (int i = 0; i < chroma_height; ++i) { 65 for (int j = 0; j < chroma_width; ++j) { 66 uint8_t u_value = *(u_pos + j); 67 if (u_value < u_min || u_value > u_max) { 68 return false; 69 } 70 uint8_t v_value = *(v_pos + j); 71 if (v_value < v_min || v_value > v_max) { 72 return false; 73 } 74 } 75 u_pos += u_pitch; 76 v_pos += v_pitch; 77 } 78 return true; 79 } 80 } // namespace 81 82 FakeVideoRenderer::FakeVideoRenderer() = default; 83 84 void FakeVideoRenderer::OnFrame(const VideoFrame& frame) { 85 MutexLock lock(&mutex_); 86 black_frame_ = CheckFrameColorYuv(frame); 87 ++num_rendered_frames_; 88 width_ = frame.width(); 89 height_ = frame.height(); 90 rotation_ = frame.rotation(); 91 timestamp_us_ = frame.timestamp_us(); 92 } 93 94 } // namespace webrtc