1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker * Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
3*d9f75844SAndroid Build Coastguard Worker *
4*d9f75844SAndroid Build Coastguard Worker * Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker * that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker * tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker * in the file PATENTS. All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker * be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker */
10*d9f75844SAndroid Build Coastguard Worker
11*d9f75844SAndroid Build Coastguard Worker #include "media/base/fake_frame_source.h"
12*d9f75844SAndroid Build Coastguard Worker
13*d9f75844SAndroid Build Coastguard Worker #include "api/scoped_refptr.h"
14*d9f75844SAndroid Build Coastguard Worker #include "api/video/i420_buffer.h"
15*d9f75844SAndroid Build Coastguard Worker #include "api/video/video_frame_buffer.h"
16*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/checks.h"
17*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/time_utils.h"
18*d9f75844SAndroid Build Coastguard Worker
19*d9f75844SAndroid Build Coastguard Worker namespace cricket {
20*d9f75844SAndroid Build Coastguard Worker
FakeFrameSource(int width,int height,int interval_us,int64_t timestamp_offset_us)21*d9f75844SAndroid Build Coastguard Worker FakeFrameSource::FakeFrameSource(int width,
22*d9f75844SAndroid Build Coastguard Worker int height,
23*d9f75844SAndroid Build Coastguard Worker int interval_us,
24*d9f75844SAndroid Build Coastguard Worker int64_t timestamp_offset_us)
25*d9f75844SAndroid Build Coastguard Worker : width_(width),
26*d9f75844SAndroid Build Coastguard Worker height_(height),
27*d9f75844SAndroid Build Coastguard Worker interval_us_(interval_us),
28*d9f75844SAndroid Build Coastguard Worker next_timestamp_us_(timestamp_offset_us) {
29*d9f75844SAndroid Build Coastguard Worker RTC_CHECK_GT(width_, 0);
30*d9f75844SAndroid Build Coastguard Worker RTC_CHECK_GT(height_, 0);
31*d9f75844SAndroid Build Coastguard Worker RTC_CHECK_GT(interval_us_, 0);
32*d9f75844SAndroid Build Coastguard Worker RTC_CHECK_GE(next_timestamp_us_, 0);
33*d9f75844SAndroid Build Coastguard Worker }
34*d9f75844SAndroid Build Coastguard Worker
FakeFrameSource(int width,int height,int interval_us)35*d9f75844SAndroid Build Coastguard Worker FakeFrameSource::FakeFrameSource(int width, int height, int interval_us)
36*d9f75844SAndroid Build Coastguard Worker : FakeFrameSource(width, height, interval_us, rtc::TimeMicros()) {}
37*d9f75844SAndroid Build Coastguard Worker
GetRotation() const38*d9f75844SAndroid Build Coastguard Worker webrtc::VideoRotation FakeFrameSource::GetRotation() const {
39*d9f75844SAndroid Build Coastguard Worker return rotation_;
40*d9f75844SAndroid Build Coastguard Worker }
41*d9f75844SAndroid Build Coastguard Worker
SetRotation(webrtc::VideoRotation rotation)42*d9f75844SAndroid Build Coastguard Worker void FakeFrameSource::SetRotation(webrtc::VideoRotation rotation) {
43*d9f75844SAndroid Build Coastguard Worker rotation_ = rotation;
44*d9f75844SAndroid Build Coastguard Worker }
45*d9f75844SAndroid Build Coastguard Worker
GetFrameRotationApplied()46*d9f75844SAndroid Build Coastguard Worker webrtc::VideoFrame FakeFrameSource::GetFrameRotationApplied() {
47*d9f75844SAndroid Build Coastguard Worker switch (rotation_) {
48*d9f75844SAndroid Build Coastguard Worker case webrtc::kVideoRotation_0:
49*d9f75844SAndroid Build Coastguard Worker case webrtc::kVideoRotation_180:
50*d9f75844SAndroid Build Coastguard Worker return GetFrame(width_, height_, webrtc::kVideoRotation_0, interval_us_);
51*d9f75844SAndroid Build Coastguard Worker case webrtc::kVideoRotation_90:
52*d9f75844SAndroid Build Coastguard Worker case webrtc::kVideoRotation_270:
53*d9f75844SAndroid Build Coastguard Worker return GetFrame(height_, width_, webrtc::kVideoRotation_0, interval_us_);
54*d9f75844SAndroid Build Coastguard Worker }
55*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK_NOTREACHED() << "Invalid rotation value: "
56*d9f75844SAndroid Build Coastguard Worker << static_cast<int>(rotation_);
57*d9f75844SAndroid Build Coastguard Worker // Without this return, the Windows Visual Studio compiler complains
58*d9f75844SAndroid Build Coastguard Worker // "not all control paths return a value".
59*d9f75844SAndroid Build Coastguard Worker return GetFrame();
60*d9f75844SAndroid Build Coastguard Worker }
61*d9f75844SAndroid Build Coastguard Worker
GetFrame()62*d9f75844SAndroid Build Coastguard Worker webrtc::VideoFrame FakeFrameSource::GetFrame() {
63*d9f75844SAndroid Build Coastguard Worker return GetFrame(width_, height_, rotation_, interval_us_);
64*d9f75844SAndroid Build Coastguard Worker }
65*d9f75844SAndroid Build Coastguard Worker
GetFrame(int width,int height,webrtc::VideoRotation rotation,int interval_us)66*d9f75844SAndroid Build Coastguard Worker webrtc::VideoFrame FakeFrameSource::GetFrame(int width,
67*d9f75844SAndroid Build Coastguard Worker int height,
68*d9f75844SAndroid Build Coastguard Worker webrtc::VideoRotation rotation,
69*d9f75844SAndroid Build Coastguard Worker int interval_us) {
70*d9f75844SAndroid Build Coastguard Worker RTC_CHECK_GT(width, 0);
71*d9f75844SAndroid Build Coastguard Worker RTC_CHECK_GT(height, 0);
72*d9f75844SAndroid Build Coastguard Worker RTC_CHECK_GT(interval_us, 0);
73*d9f75844SAndroid Build Coastguard Worker
74*d9f75844SAndroid Build Coastguard Worker rtc::scoped_refptr<webrtc::I420Buffer> buffer(
75*d9f75844SAndroid Build Coastguard Worker webrtc::I420Buffer::Create(width, height));
76*d9f75844SAndroid Build Coastguard Worker
77*d9f75844SAndroid Build Coastguard Worker buffer->InitializeData();
78*d9f75844SAndroid Build Coastguard Worker webrtc::VideoFrame frame = webrtc::VideoFrame::Builder()
79*d9f75844SAndroid Build Coastguard Worker .set_video_frame_buffer(buffer)
80*d9f75844SAndroid Build Coastguard Worker .set_rotation(rotation)
81*d9f75844SAndroid Build Coastguard Worker .set_timestamp_us(next_timestamp_us_)
82*d9f75844SAndroid Build Coastguard Worker .build();
83*d9f75844SAndroid Build Coastguard Worker
84*d9f75844SAndroid Build Coastguard Worker next_timestamp_us_ += interval_us;
85*d9f75844SAndroid Build Coastguard Worker return frame;
86*d9f75844SAndroid Build Coastguard Worker }
87*d9f75844SAndroid Build Coastguard Worker
88*d9f75844SAndroid Build Coastguard Worker } // namespace cricket
89