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