Niels Möller | a6cc0f9 | 2018-02-12 17:14:55 +0100 | [diff] [blame] | 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 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame^] | 11 | #include "media/base/fake_frame_source.h" |
Niels Möller | a6cc0f9 | 2018-02-12 17:14:55 +0100 | [diff] [blame] | 12 | |
| 13 | #include "api/video/i420_buffer.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 14 | #include "api/video/video_frame_buffer.h" |
Niels Möller | 5c7efe7 | 2018-05-11 10:34:46 +0200 | [diff] [blame] | 15 | #include "rtc_base/checks.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 16 | #include "rtc_base/scoped_ref_ptr.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame^] | 17 | #include "rtc_base/time_utils.h" |
Niels Möller | a6cc0f9 | 2018-02-12 17:14:55 +0100 | [diff] [blame] | 18 | |
| 19 | namespace cricket { |
| 20 | |
Johannes Kron | 965e794 | 2018-09-13 15:36:20 +0200 | [diff] [blame] | 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) { |
Niels Möller | a6cc0f9 | 2018-02-12 17:14:55 +0100 | [diff] [blame] | 29 | RTC_CHECK_GT(width_, 0); |
| 30 | RTC_CHECK_GT(height_, 0); |
| 31 | RTC_CHECK_GT(interval_us_, 0); |
Johannes Kron | 965e794 | 2018-09-13 15:36:20 +0200 | [diff] [blame] | 32 | RTC_CHECK_GE(next_timestamp_us_, 0); |
Niels Möller | a6cc0f9 | 2018-02-12 17:14:55 +0100 | [diff] [blame] | 33 | } |
| 34 | |
Johannes Kron | 965e794 | 2018-09-13 15:36:20 +0200 | [diff] [blame] | 35 | FakeFrameSource::FakeFrameSource(int width, int height, int interval_us) |
| 36 | : FakeFrameSource(width, |
| 37 | height, |
| 38 | interval_us, |
| 39 | rtc::kNumMicrosecsPerMillisec) {} |
| 40 | |
Niels Möller | 5c7efe7 | 2018-05-11 10:34:46 +0200 | [diff] [blame] | 41 | webrtc::VideoRotation FakeFrameSource::GetRotation() const { |
Niels Möller | a6cc0f9 | 2018-02-12 17:14:55 +0100 | [diff] [blame] | 42 | return rotation_; |
| 43 | } |
| 44 | |
| 45 | void FakeFrameSource::SetRotation(webrtc::VideoRotation rotation) { |
| 46 | rotation_ = rotation; |
| 47 | } |
| 48 | |
Niels Möller | 5c7efe7 | 2018-05-11 10:34:46 +0200 | [diff] [blame] | 49 | webrtc::VideoFrame FakeFrameSource::GetFrameRotationApplied() { |
| 50 | switch (rotation_) { |
| 51 | case webrtc::kVideoRotation_0: |
| 52 | case webrtc::kVideoRotation_180: |
| 53 | return GetFrame(width_, height_, webrtc::kVideoRotation_0, interval_us_); |
| 54 | case webrtc::kVideoRotation_90: |
| 55 | case webrtc::kVideoRotation_270: |
| 56 | return GetFrame(height_, width_, webrtc::kVideoRotation_0, interval_us_); |
| 57 | } |
| 58 | RTC_NOTREACHED() << "Invalid rotation value: " << static_cast<int>(rotation_); |
| 59 | // Without this return, the Windows Visual Studio compiler complains |
| 60 | // "not all control paths return a value". |
| 61 | return GetFrame(); |
| 62 | } |
| 63 | |
Niels Möller | a6cc0f9 | 2018-02-12 17:14:55 +0100 | [diff] [blame] | 64 | webrtc::VideoFrame FakeFrameSource::GetFrame() { |
Niels Möller | 5c7efe7 | 2018-05-11 10:34:46 +0200 | [diff] [blame] | 65 | return GetFrame(width_, height_, rotation_, interval_us_); |
Niels Möller | a6cc0f9 | 2018-02-12 17:14:55 +0100 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | webrtc::VideoFrame FakeFrameSource::GetFrame(int width, |
| 69 | int height, |
Niels Möller | 5c7efe7 | 2018-05-11 10:34:46 +0200 | [diff] [blame] | 70 | webrtc::VideoRotation rotation, |
Niels Möller | a6cc0f9 | 2018-02-12 17:14:55 +0100 | [diff] [blame] | 71 | int interval_us) { |
| 72 | RTC_CHECK_GT(width, 0); |
| 73 | RTC_CHECK_GT(height, 0); |
| 74 | RTC_CHECK_GT(interval_us, 0); |
| 75 | |
| 76 | rtc::scoped_refptr<webrtc::I420Buffer> buffer( |
| 77 | webrtc::I420Buffer::Create(width, height)); |
| 78 | |
| 79 | buffer->InitializeData(); |
Artem Titov | 1ebfb6a | 2019-01-03 23:49:37 +0100 | [diff] [blame] | 80 | webrtc::VideoFrame frame = webrtc::VideoFrame::Builder() |
| 81 | .set_video_frame_buffer(buffer) |
| 82 | .set_rotation(rotation) |
| 83 | .set_timestamp_us(next_timestamp_us_) |
| 84 | .build(); |
Niels Möller | a6cc0f9 | 2018-02-12 17:14:55 +0100 | [diff] [blame] | 85 | |
| 86 | next_timestamp_us_ += interval_us; |
| 87 | return frame; |
| 88 | } |
| 89 | |
| 90 | } // namespace cricket |