• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2011 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 #ifndef MEDIA_BASE_FAKE_VIDEO_RENDERER_H_
12 #define MEDIA_BASE_FAKE_VIDEO_RENDERER_H_
13 
14 #include <stdint.h>
15 
16 #include "api/scoped_refptr.h"
17 #include "api/video/video_frame.h"
18 #include "api/video/video_frame_buffer.h"
19 #include "api/video/video_rotation.h"
20 #include "api/video/video_sink_interface.h"
21 #include "rtc_base/event.h"
22 #include "rtc_base/synchronization/mutex.h"
23 
24 namespace cricket {
25 
26 // Faked video renderer that has a callback for actions on rendering.
27 class FakeVideoRenderer : public rtc::VideoSinkInterface<webrtc::VideoFrame> {
28  public:
29   FakeVideoRenderer();
30 
31   void OnFrame(const webrtc::VideoFrame& frame) override;
32 
errors()33   int errors() const { return errors_; }
34 
width()35   int width() const {
36     webrtc::MutexLock lock(&mutex_);
37     return width_;
38   }
height()39   int height() const {
40     webrtc::MutexLock lock(&mutex_);
41     return height_;
42   }
43 
rotation()44   webrtc::VideoRotation rotation() const {
45     webrtc::MutexLock lock(&mutex_);
46     return rotation_;
47   }
48 
timestamp_us()49   int64_t timestamp_us() const {
50     webrtc::MutexLock lock(&mutex_);
51     return timestamp_us_;
52   }
53 
num_rendered_frames()54   int num_rendered_frames() const {
55     webrtc::MutexLock lock(&mutex_);
56     return num_rendered_frames_;
57   }
58 
black_frame()59   bool black_frame() const {
60     webrtc::MutexLock lock(&mutex_);
61     return black_frame_;
62   }
63 
ntp_time_ms()64   int64_t ntp_time_ms() const {
65     webrtc::MutexLock lock(&mutex_);
66     return ntp_timestamp_ms_;
67   }
68 
color_space()69   absl::optional<webrtc::ColorSpace> color_space() const {
70     webrtc::MutexLock lock(&mutex_);
71     return color_space_;
72   }
73 
packet_infos()74   webrtc::RtpPacketInfos packet_infos() const {
75     webrtc::MutexLock lock(&mutex_);
76     return packet_infos_;
77   }
78 
79   bool WaitForRenderedFrame(int64_t timeout_ms);
80 
81  private:
CheckFrameColorYuv(uint8_t y_min,uint8_t y_max,uint8_t u_min,uint8_t u_max,uint8_t v_min,uint8_t v_max,const webrtc::VideoFrame * frame)82   static bool CheckFrameColorYuv(uint8_t y_min,
83                                  uint8_t y_max,
84                                  uint8_t u_min,
85                                  uint8_t u_max,
86                                  uint8_t v_min,
87                                  uint8_t v_max,
88                                  const webrtc::VideoFrame* frame) {
89     if (!frame || !frame->video_frame_buffer()) {
90       return false;
91     }
92     rtc::scoped_refptr<const webrtc::I420BufferInterface> i420_buffer =
93         frame->video_frame_buffer()->ToI420();
94     // Y
95     int y_width = frame->width();
96     int y_height = frame->height();
97     const uint8_t* y_plane = i420_buffer->DataY();
98     const uint8_t* y_pos = y_plane;
99     int32_t y_pitch = i420_buffer->StrideY();
100     for (int i = 0; i < y_height; ++i) {
101       for (int j = 0; j < y_width; ++j) {
102         uint8_t y_value = *(y_pos + j);
103         if (y_value < y_min || y_value > y_max) {
104           return false;
105         }
106       }
107       y_pos += y_pitch;
108     }
109     // U and V
110     int chroma_width = i420_buffer->ChromaWidth();
111     int chroma_height = i420_buffer->ChromaHeight();
112     const uint8_t* u_plane = i420_buffer->DataU();
113     const uint8_t* v_plane = i420_buffer->DataV();
114     const uint8_t* u_pos = u_plane;
115     const uint8_t* v_pos = v_plane;
116     int32_t u_pitch = i420_buffer->StrideU();
117     int32_t v_pitch = i420_buffer->StrideV();
118     for (int i = 0; i < chroma_height; ++i) {
119       for (int j = 0; j < chroma_width; ++j) {
120         uint8_t u_value = *(u_pos + j);
121         if (u_value < u_min || u_value > u_max) {
122           return false;
123         }
124         uint8_t v_value = *(v_pos + j);
125         if (v_value < v_min || v_value > v_max) {
126           return false;
127         }
128       }
129       u_pos += u_pitch;
130       v_pos += v_pitch;
131     }
132     return true;
133   }
134 
135   int errors_ = 0;
136   int width_ = 0;
137   int height_ = 0;
138   webrtc::VideoRotation rotation_ = webrtc::kVideoRotation_0;
139   int64_t timestamp_us_ = 0;
140   int num_rendered_frames_ = 0;
141   int64_t ntp_timestamp_ms_ = 0;
142   bool black_frame_ = false;
143   mutable webrtc::Mutex mutex_;
144   rtc::Event frame_rendered_event_;
145   absl::optional<webrtc::ColorSpace> color_space_;
146   webrtc::RtpPacketInfos packet_infos_;
147 };
148 
149 }  // namespace cricket
150 
151 #endif  // MEDIA_BASE_FAKE_VIDEO_RENDERER_H_
152