fake_video_renderer.h 4.3 KB

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