henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1 | /* |
kjellander | 1afca73 | 2016-02-08 04:46:45 | [diff] [blame] | 2 | * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 3 | * |
kjellander | 1afca73 | 2016-02-08 04:46:45 | [diff] [blame] | 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. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 9 | */ |
| 10 | |
kjellander | a96e2d7 | 2016-02-05 07:52:28 | [diff] [blame] | 11 | #ifndef WEBRTC_MEDIA_BASE_FAKEVIDEORENDERER_H_ |
| 12 | #define WEBRTC_MEDIA_BASE_FAKEVIDEORENDERER_H_ |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 13 | |
nisse | af91689 | 2017-01-10 15:44:26 | [diff] [blame] | 14 | #include "webrtc/api/video/video_frame.h" |
nisse | 1509fa1 | 2016-03-23 11:05:57 | [diff] [blame] | 15 | #include "webrtc/media/base/videosinkinterface.h" |
Edward Lemur | c20978e | 2017-07-06 17:44:34 | [diff] [blame^] | 16 | #include "webrtc/rtc_base/criticalsection.h" |
| 17 | #include "webrtc/rtc_base/logging.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 18 | |
| 19 | namespace cricket { |
| 20 | |
| 21 | // Faked video renderer that has a callback for actions on rendering. |
nisse | acd935b | 2016-11-11 11:55:13 | [diff] [blame] | 22 | class FakeVideoRenderer : public rtc::VideoSinkInterface<webrtc::VideoFrame> { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 23 | public: |
| 24 | FakeVideoRenderer() |
| 25 | : errors_(0), |
| 26 | width_(0), |
| 27 | height_(0), |
perkj | d6c3954 | 2016-03-17 09:35:23 | [diff] [blame] | 28 | rotation_(webrtc::kVideoRotation_0), |
nisse | 306d52b | 2016-09-06 14:52:40 | [diff] [blame] | 29 | timestamp_us_(0), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 30 | num_rendered_frames_(0), |
perkj | d6c3954 | 2016-03-17 09:35:23 | [diff] [blame] | 31 | black_frame_(false) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 32 | |
nisse | acd935b | 2016-11-11 11:55:13 | [diff] [blame] | 33 | virtual void OnFrame(const webrtc::VideoFrame& frame) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 | [diff] [blame] | 34 | rtc::CritScope cs(&crit_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 35 | // TODO(zhurunz) Check with VP8 team to see if we can remove this |
nisse | efec590 | 2016-06-09 07:31:39 | [diff] [blame] | 36 | // tolerance on Y values. Some unit tests produce Y values close |
| 37 | // to 16 rather than close to zero, for supposedly black frames. |
| 38 | // Largest value observed is 34, e.g., running |
deadbeef | 1dcb164 | 2017-03-30 04:08:16 | [diff] [blame] | 39 | // PeerConnectionIntegrationTest.SendAndReceive16To9AspectRatio. |
nisse | efec590 | 2016-06-09 07:31:39 | [diff] [blame] | 40 | black_frame_ = CheckFrameColorYuv(0, 48, 128, 128, 128, 128, &frame); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 41 | // Treat unexpected frame size as error. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 42 | ++num_rendered_frames_; |
nisse | 71a0c2f | 2016-04-04 07:57:29 | [diff] [blame] | 43 | width_ = frame.width(); |
| 44 | height_ = frame.height(); |
nisse | f386876 | 2016-04-13 10:29:16 | [diff] [blame] | 45 | rotation_ = frame.rotation(); |
nisse | 306d52b | 2016-09-06 14:52:40 | [diff] [blame] | 46 | timestamp_us_ = frame.timestamp_us(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | int errors() const { return errors_; } |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 | [diff] [blame] | 50 | int width() const { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 | [diff] [blame] | 51 | rtc::CritScope cs(&crit_); |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 | [diff] [blame] | 52 | return width_; |
| 53 | } |
| 54 | int height() const { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 | [diff] [blame] | 55 | rtc::CritScope cs(&crit_); |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 | [diff] [blame] | 56 | return height_; |
| 57 | } |
perkj | caafdba | 2016-03-20 14:34:29 | [diff] [blame] | 58 | webrtc::VideoRotation rotation() const { |
Per | a509241 | 2016-02-12 12:30:57 | [diff] [blame] | 59 | rtc::CritScope cs(&crit_); |
| 60 | return rotation_; |
| 61 | } |
perkj | d6c3954 | 2016-03-17 09:35:23 | [diff] [blame] | 62 | |
nisse | 306d52b | 2016-09-06 14:52:40 | [diff] [blame] | 63 | int64_t timestamp_us() const { |
perkj | d6c3954 | 2016-03-17 09:35:23 | [diff] [blame] | 64 | rtc::CritScope cs(&crit_); |
nisse | 306d52b | 2016-09-06 14:52:40 | [diff] [blame] | 65 | return timestamp_us_; |
perkj | d6c3954 | 2016-03-17 09:35:23 | [diff] [blame] | 66 | } |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 | [diff] [blame] | 67 | int num_rendered_frames() const { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 | [diff] [blame] | 68 | rtc::CritScope cs(&crit_); |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 | [diff] [blame] | 69 | return num_rendered_frames_; |
| 70 | } |
| 71 | bool black_frame() const { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 | [diff] [blame] | 72 | rtc::CritScope cs(&crit_); |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 | [diff] [blame] | 73 | return black_frame_; |
| 74 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 75 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 76 | private: |
Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [diff] [blame] | 77 | static bool CheckFrameColorYuv(uint8_t y_min, |
| 78 | uint8_t y_max, |
| 79 | uint8_t u_min, |
| 80 | uint8_t u_max, |
| 81 | uint8_t v_min, |
| 82 | uint8_t v_max, |
nisse | acd935b | 2016-11-11 11:55:13 | [diff] [blame] | 83 | const webrtc::VideoFrame* frame) { |
nisse | 0565451 | 2016-04-29 09:56:00 | [diff] [blame] | 84 | if (!frame || !frame->video_frame_buffer()) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 85 | return false; |
| 86 | } |
Magnus Jedvert | 7a721e8 | 2017-06-14 09:28:08 | [diff] [blame] | 87 | rtc::scoped_refptr<const webrtc::I420BufferInterface> i420_buffer = |
| 88 | frame->video_frame_buffer()->ToI420(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 89 | // Y |
nisse | 71a0c2f | 2016-04-04 07:57:29 | [diff] [blame] | 90 | int y_width = frame->width(); |
| 91 | int y_height = frame->height(); |
Magnus Jedvert | 7a721e8 | 2017-06-14 09:28:08 | [diff] [blame] | 92 | const uint8_t* y_plane = i420_buffer->DataY(); |
Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [diff] [blame] | 93 | const uint8_t* y_pos = y_plane; |
Magnus Jedvert | 7a721e8 | 2017-06-14 09:28:08 | [diff] [blame] | 94 | int32_t y_pitch = i420_buffer->StrideY(); |
nisse | 71a0c2f | 2016-04-04 07:57:29 | [diff] [blame] | 95 | for (int i = 0; i < y_height; ++i) { |
| 96 | for (int j = 0; j < y_width; ++j) { |
Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [diff] [blame] | 97 | uint8_t y_value = *(y_pos + j); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 98 | if (y_value < y_min || y_value > y_max) { |
| 99 | return false; |
| 100 | } |
| 101 | } |
| 102 | y_pos += y_pitch; |
| 103 | } |
| 104 | // U and V |
Magnus Jedvert | 7a721e8 | 2017-06-14 09:28:08 | [diff] [blame] | 105 | int chroma_width = i420_buffer->ChromaWidth(); |
| 106 | int chroma_height = i420_buffer->ChromaHeight(); |
| 107 | const uint8_t* u_plane = i420_buffer->DataU(); |
| 108 | const uint8_t* v_plane = i420_buffer->DataV(); |
Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [diff] [blame] | 109 | const uint8_t* u_pos = u_plane; |
| 110 | const uint8_t* v_pos = v_plane; |
Magnus Jedvert | 7a721e8 | 2017-06-14 09:28:08 | [diff] [blame] | 111 | int32_t u_pitch = i420_buffer->StrideU(); |
| 112 | int32_t v_pitch = i420_buffer->StrideV(); |
nisse | 71a0c2f | 2016-04-04 07:57:29 | [diff] [blame] | 113 | for (int i = 0; i < chroma_height; ++i) { |
| 114 | for (int j = 0; j < chroma_width; ++j) { |
Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [diff] [blame] | 115 | uint8_t u_value = *(u_pos + j); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 116 | if (u_value < u_min || u_value > u_max) { |
| 117 | return false; |
| 118 | } |
Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [diff] [blame] | 119 | uint8_t v_value = *(v_pos + j); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 120 | if (v_value < v_min || v_value > v_max) { |
| 121 | return false; |
| 122 | } |
| 123 | } |
| 124 | u_pos += u_pitch; |
| 125 | v_pos += v_pitch; |
| 126 | } |
| 127 | return true; |
| 128 | } |
| 129 | |
| 130 | int errors_; |
| 131 | int width_; |
| 132 | int height_; |
Per | a509241 | 2016-02-12 12:30:57 | [diff] [blame] | 133 | webrtc::VideoRotation rotation_; |
nisse | 306d52b | 2016-09-06 14:52:40 | [diff] [blame] | 134 | int64_t timestamp_us_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 135 | int num_rendered_frames_; |
| 136 | bool black_frame_; |
pbos | 5ad935c | 2016-01-25 11:52:44 | [diff] [blame] | 137 | rtc::CriticalSection crit_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | } // namespace cricket |
| 141 | |
kjellander | a96e2d7 | 2016-02-05 07:52:28 | [diff] [blame] | 142 | #endif // WEBRTC_MEDIA_BASE_FAKEVIDEORENDERER_H_ |