adapted_video_track_source.h (3893B)
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 11 #ifndef MEDIA_BASE_ADAPTED_VIDEO_TRACK_SOURCE_H_ 12 #define MEDIA_BASE_ADAPTED_VIDEO_TRACK_SOURCE_H_ 13 14 #include <stdint.h> 15 16 #include <optional> 17 18 #include "api/media_stream_interface.h" 19 #include "api/notifier.h" 20 #include "api/video/recordable_encoded_frame.h" 21 #include "api/video/video_frame.h" 22 #include "api/video/video_sink_interface.h" 23 #include "api/video/video_source_interface.h" 24 #include "api/video_track_source_constraints.h" 25 #include "media/base/video_adapter.h" 26 #include "media/base/video_broadcaster.h" 27 #include "rtc_base/synchronization/mutex.h" 28 #include "rtc_base/system/rtc_export.h" 29 #include "rtc_base/thread_annotations.h" 30 31 namespace webrtc { 32 33 // Base class for sources which needs video adaptation, e.g., video 34 // capture sources. Sinks must be added and removed on one and only 35 // one thread, while AdaptFrame and OnFrame may be called on any 36 // thread. 37 class RTC_EXPORT AdaptedVideoTrackSource 38 : public Notifier<VideoTrackSourceInterface> { 39 public: 40 AdaptedVideoTrackSource(); 41 ~AdaptedVideoTrackSource() override; 42 43 protected: 44 // Allows derived classes to initialize `video_adapter_` with a custom 45 // alignment. 46 explicit AdaptedVideoTrackSource(int required_alignment); 47 // Checks the apply_rotation() flag. If the frame needs rotation, and it is a 48 // plain memory frame, it is rotated. Subclasses producing native frames must 49 // handle apply_rotation() themselves. 50 void OnFrame(const VideoFrame& frame); 51 // Indication from source that a frame was dropped. 52 void OnFrameDropped(); 53 54 // Reports the appropriate frame size after adaptation. Returns true 55 // if a frame is wanted. Returns false if there are no interested 56 // sinks, or if the VideoAdapter decides to drop the frame. 57 bool AdaptFrame(int width, 58 int height, 59 int64_t time_us, 60 int* out_width, 61 int* out_height, 62 int* crop_width, 63 int* crop_height, 64 int* crop_x, 65 int* crop_y); 66 67 // Returns the current value of the apply_rotation flag, derived 68 // from the VideoSinkWants of registered sinks. The value is derived 69 // from sinks' wants, in AddOrUpdateSink and RemoveSink. Beware that 70 // when using this method from a different thread, the value may 71 // become stale before it is used. 72 bool apply_rotation(); 73 74 VideoAdapter* video_adapter() { return &video_adapter_; } 75 76 private: 77 // Implements VideoSourceInterface. 78 void AddOrUpdateSink(VideoSinkInterface<VideoFrame>* sink, 79 const VideoSinkWants& wants) override; 80 void RemoveSink(VideoSinkInterface<VideoFrame>* sink) override; 81 82 // Part of VideoTrackSourceInterface. 83 bool GetStats(Stats* stats) override; 84 85 void OnSinkWantsChanged(const VideoSinkWants& wants); 86 87 // Encoded sinks not implemented for AdaptedVideoTrackSource. 88 bool SupportsEncodedOutput() const override { return false; } 89 void GenerateKeyFrame() override {} 90 void AddEncodedSink( 91 VideoSinkInterface<RecordableEncodedFrame>* /* sink */) override {} 92 void RemoveEncodedSink( 93 VideoSinkInterface<RecordableEncodedFrame>* /* sink */) override {} 94 void ProcessConstraints( 95 const VideoTrackSourceConstraints& constraints) override; 96 97 VideoAdapter video_adapter_; 98 99 Mutex stats_mutex_; 100 std::optional<Stats> stats_ RTC_GUARDED_BY(stats_mutex_); 101 102 VideoBroadcaster broadcaster_; 103 }; 104 105 } // namespace webrtc 106 107 108 #endif // MEDIA_BASE_ADAPTED_VIDEO_TRACK_SOURCE_H_