nisse | 6f5a6c3 | 2016-09-22 01:25:59 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 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 | #ifndef MEDIA_BASE_ADAPTED_VIDEO_TRACK_SOURCE_H_ |
| 12 | #define MEDIA_BASE_ADAPTED_VIDEO_TRACK_SOURCE_H_ |
nisse | 6f5a6c3 | 2016-09-22 01:25:59 -0700 | [diff] [blame] | 13 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 14 | #include <stdint.h> |
| 15 | |
| 16 | #include "absl/types/optional.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 17 | #include "api/media_stream_interface.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "api/notifier.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 19 | #include "api/video/video_frame.h" |
| 20 | #include "api/video/video_sink_interface.h" |
| 21 | #include "api/video/video_source_interface.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 22 | #include "media/base/video_adapter.h" |
| 23 | #include "media/base/video_broadcaster.h" |
| 24 | #include "rtc_base/critical_section.h" |
Mirko Bonadei | 66e7679 | 2019-04-02 11:33:59 +0200 | [diff] [blame] | 25 | #include "rtc_base/system/rtc_export.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 26 | #include "rtc_base/thread_annotations.h" |
nisse | 6f5a6c3 | 2016-09-22 01:25:59 -0700 | [diff] [blame] | 27 | |
| 28 | namespace rtc { |
| 29 | |
| 30 | // Base class for sources which needs video adaptation, e.g., video |
| 31 | // capture sources. Sinks must be added and removed on one and only |
| 32 | // one thread, while AdaptFrame and OnFrame may be called on any |
| 33 | // thread. |
Mirko Bonadei | 66e7679 | 2019-04-02 11:33:59 +0200 | [diff] [blame] | 34 | class RTC_EXPORT AdaptedVideoTrackSource |
nisse | 6f5a6c3 | 2016-09-22 01:25:59 -0700 | [diff] [blame] | 35 | : public webrtc::Notifier<webrtc::VideoTrackSourceInterface> { |
| 36 | public: |
| 37 | AdaptedVideoTrackSource(); |
Paulina Hensman | a680a6a | 2018-04-05 11:42:24 +0200 | [diff] [blame] | 38 | ~AdaptedVideoTrackSource() override; |
nisse | 6f5a6c3 | 2016-09-22 01:25:59 -0700 | [diff] [blame] | 39 | |
| 40 | protected: |
kthelgason | c847417 | 2016-12-08 08:04:51 -0800 | [diff] [blame] | 41 | // Allows derived classes to initialize |video_adapter_| with a custom |
| 42 | // alignment. |
Steve Anton | e78bcb9 | 2017-10-31 09:53:08 -0700 | [diff] [blame] | 43 | explicit AdaptedVideoTrackSource(int required_alignment); |
nisse | 6f5a6c3 | 2016-09-22 01:25:59 -0700 | [diff] [blame] | 44 | // Checks the apply_rotation() flag. If the frame needs rotation, and it is a |
| 45 | // plain memory frame, it is rotated. Subclasses producing native frames must |
| 46 | // handle apply_rotation() themselves. |
nisse | acd935b | 2016-11-11 03:55:13 -0800 | [diff] [blame] | 47 | void OnFrame(const webrtc::VideoFrame& frame); |
nisse | 6f5a6c3 | 2016-09-22 01:25:59 -0700 | [diff] [blame] | 48 | |
| 49 | // Reports the appropriate frame size after adaptation. Returns true |
| 50 | // if a frame is wanted. Returns false if there are no interested |
| 51 | // sinks, or if the VideoAdapter decides to drop the frame. |
| 52 | bool AdaptFrame(int width, |
| 53 | int height, |
| 54 | int64_t time_us, |
| 55 | int* out_width, |
| 56 | int* out_height, |
| 57 | int* crop_width, |
| 58 | int* crop_height, |
| 59 | int* crop_x, |
| 60 | int* crop_y); |
| 61 | |
| 62 | // Returns the current value of the apply_rotation flag, derived |
| 63 | // from the VideoSinkWants of registered sinks. The value is derived |
| 64 | // from sinks' wants, in AddOrUpdateSink and RemoveSink. Beware that |
| 65 | // when using this method from a different thread, the value may |
| 66 | // become stale before it is used. |
| 67 | bool apply_rotation(); |
| 68 | |
| 69 | cricket::VideoAdapter* video_adapter() { return &video_adapter_; } |
| 70 | |
| 71 | private: |
| 72 | // Implements rtc::VideoSourceInterface. |
nisse | acd935b | 2016-11-11 03:55:13 -0800 | [diff] [blame] | 73 | void AddOrUpdateSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink, |
nisse | 6f5a6c3 | 2016-09-22 01:25:59 -0700 | [diff] [blame] | 74 | const rtc::VideoSinkWants& wants) override; |
nisse | acd935b | 2016-11-11 03:55:13 -0800 | [diff] [blame] | 75 | void RemoveSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) override; |
nisse | 6f5a6c3 | 2016-09-22 01:25:59 -0700 | [diff] [blame] | 76 | |
| 77 | // Part of VideoTrackSourceInterface. |
| 78 | bool GetStats(Stats* stats) override; |
| 79 | |
| 80 | void OnSinkWantsChanged(const rtc::VideoSinkWants& wants); |
| 81 | |
nisse | 6f5a6c3 | 2016-09-22 01:25:59 -0700 | [diff] [blame] | 82 | cricket::VideoAdapter video_adapter_; |
| 83 | |
| 84 | rtc::CriticalSection stats_crit_; |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame] | 85 | absl::optional<Stats> stats_ RTC_GUARDED_BY(stats_crit_); |
nisse | 6f5a6c3 | 2016-09-22 01:25:59 -0700 | [diff] [blame] | 86 | |
| 87 | VideoBroadcaster broadcaster_; |
| 88 | }; |
| 89 | |
| 90 | } // namespace rtc |
| 91 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 92 | #endif // MEDIA_BASE_ADAPTED_VIDEO_TRACK_SOURCE_H_ |