• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_frame_source.h"
12 
13 #include "api/scoped_refptr.h"
14 #include "api/video/i420_buffer.h"
15 #include "api/video/video_frame_buffer.h"
16 #include "rtc_base/checks.h"
17 #include "rtc_base/time_utils.h"
18 
19 namespace cricket {
20 
FakeFrameSource(int width,int height,int interval_us,int64_t timestamp_offset_us)21 FakeFrameSource::FakeFrameSource(int width,
22                                  int height,
23                                  int interval_us,
24                                  int64_t timestamp_offset_us)
25     : width_(width),
26       height_(height),
27       interval_us_(interval_us),
28       next_timestamp_us_(timestamp_offset_us) {
29   RTC_CHECK_GT(width_, 0);
30   RTC_CHECK_GT(height_, 0);
31   RTC_CHECK_GT(interval_us_, 0);
32   RTC_CHECK_GE(next_timestamp_us_, 0);
33 }
34 
FakeFrameSource(int width,int height,int interval_us)35 FakeFrameSource::FakeFrameSource(int width, int height, int interval_us)
36     : FakeFrameSource(width, height, interval_us, rtc::TimeMicros()) {}
37 
GetRotation() const38 webrtc::VideoRotation FakeFrameSource::GetRotation() const {
39   return rotation_;
40 }
41 
SetRotation(webrtc::VideoRotation rotation)42 void FakeFrameSource::SetRotation(webrtc::VideoRotation rotation) {
43   rotation_ = rotation;
44 }
45 
GetFrameRotationApplied()46 webrtc::VideoFrame FakeFrameSource::GetFrameRotationApplied() {
47   switch (rotation_) {
48     case webrtc::kVideoRotation_0:
49     case webrtc::kVideoRotation_180:
50       return GetFrame(width_, height_, webrtc::kVideoRotation_0, interval_us_);
51     case webrtc::kVideoRotation_90:
52     case webrtc::kVideoRotation_270:
53       return GetFrame(height_, width_, webrtc::kVideoRotation_0, interval_us_);
54   }
55   RTC_DCHECK_NOTREACHED() << "Invalid rotation value: "
56                           << static_cast<int>(rotation_);
57   // Without this return, the Windows Visual Studio compiler complains
58   // "not all control paths return a value".
59   return GetFrame();
60 }
61 
GetFrame()62 webrtc::VideoFrame FakeFrameSource::GetFrame() {
63   return GetFrame(width_, height_, rotation_, interval_us_);
64 }
65 
GetFrame(int width,int height,webrtc::VideoRotation rotation,int interval_us)66 webrtc::VideoFrame FakeFrameSource::GetFrame(int width,
67                                              int height,
68                                              webrtc::VideoRotation rotation,
69                                              int interval_us) {
70   RTC_CHECK_GT(width, 0);
71   RTC_CHECK_GT(height, 0);
72   RTC_CHECK_GT(interval_us, 0);
73 
74   rtc::scoped_refptr<webrtc::I420Buffer> buffer(
75       webrtc::I420Buffer::Create(width, height));
76 
77   buffer->InitializeData();
78   webrtc::VideoFrame frame = webrtc::VideoFrame::Builder()
79                                  .set_video_frame_buffer(buffer)
80                                  .set_rotation(rotation)
81                                  .set_timestamp_us(next_timestamp_us_)
82                                  .build();
83 
84   next_timestamp_us_ += interval_us;
85   return frame;
86 }
87 
88 }  // namespace cricket
89