blob: 8a05536c8345a7f1b01d1336056ff9eeda73d8a7 [file] [log] [blame]
Niels Möllera6cc0f92018-02-12 17:14:55 +01001/*
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
Steve Anton10542f22019-01-11 09:11:00 -080011#include "media/base/fake_frame_source.h"
Niels Möllera6cc0f92018-02-12 17:14:55 +010012
Mirko Bonadeid9708072019-01-25 20:26:48 +010013#include "api/scoped_refptr.h"
Niels Möllera6cc0f92018-02-12 17:14:55 +010014#include "api/video/i420_buffer.h"
Yves Gerey3e707812018-11-28 16:47:49 +010015#include "api/video/video_frame_buffer.h"
Niels Möller5c7efe72018-05-11 10:34:46 +020016#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080017#include "rtc_base/time_utils.h"
Niels Möllera6cc0f92018-02-12 17:14:55 +010018
19namespace cricket {
20
Johannes Kron965e7942018-09-13 15:36:20 +020021FakeFrameSource::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) {
Niels Möllera6cc0f92018-02-12 17:14:55 +010029 RTC_CHECK_GT(width_, 0);
30 RTC_CHECK_GT(height_, 0);
31 RTC_CHECK_GT(interval_us_, 0);
Johannes Kron965e7942018-09-13 15:36:20 +020032 RTC_CHECK_GE(next_timestamp_us_, 0);
Niels Möllera6cc0f92018-02-12 17:14:55 +010033}
34
Johannes Kron965e7942018-09-13 15:36:20 +020035FakeFrameSource::FakeFrameSource(int width, int height, int interval_us)
Niels Möller805a27e2019-01-21 12:21:27 +010036 : FakeFrameSource(width, height, interval_us, rtc::TimeMicros()) {}
Johannes Kron965e7942018-09-13 15:36:20 +020037
Niels Möller5c7efe72018-05-11 10:34:46 +020038webrtc::VideoRotation FakeFrameSource::GetRotation() const {
Niels Möllera6cc0f92018-02-12 17:14:55 +010039 return rotation_;
40}
41
42void FakeFrameSource::SetRotation(webrtc::VideoRotation rotation) {
43 rotation_ = rotation;
44}
45
Niels Möller5c7efe72018-05-11 10:34:46 +020046webrtc::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_NOTREACHED() << "Invalid rotation value: " << static_cast<int>(rotation_);
56 // Without this return, the Windows Visual Studio compiler complains
57 // "not all control paths return a value".
58 return GetFrame();
59}
60
Niels Möllera6cc0f92018-02-12 17:14:55 +010061webrtc::VideoFrame FakeFrameSource::GetFrame() {
Niels Möller5c7efe72018-05-11 10:34:46 +020062 return GetFrame(width_, height_, rotation_, interval_us_);
Niels Möllera6cc0f92018-02-12 17:14:55 +010063}
64
65webrtc::VideoFrame FakeFrameSource::GetFrame(int width,
66 int height,
Niels Möller5c7efe72018-05-11 10:34:46 +020067 webrtc::VideoRotation rotation,
Niels Möllera6cc0f92018-02-12 17:14:55 +010068 int interval_us) {
69 RTC_CHECK_GT(width, 0);
70 RTC_CHECK_GT(height, 0);
71 RTC_CHECK_GT(interval_us, 0);
72
73 rtc::scoped_refptr<webrtc::I420Buffer> buffer(
74 webrtc::I420Buffer::Create(width, height));
75
76 buffer->InitializeData();
Artem Titov1ebfb6a2019-01-03 23:49:37 +010077 webrtc::VideoFrame frame = webrtc::VideoFrame::Builder()
78 .set_video_frame_buffer(buffer)
79 .set_rotation(rotation)
80 .set_timestamp_us(next_timestamp_us_)
81 .build();
Niels Möllera6cc0f92018-02-12 17:14:55 +010082
83 next_timestamp_us_ += interval_us;
84 return frame;
85}
86
87} // namespace cricket