xref: /aosp_15_r20/external/webrtc/media/base/adapted_video_track_source.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 "absl/types/optional.h"
17 #include "api/media_stream_interface.h"
18 #include "api/notifier.h"
19 #include "api/video/video_frame.h"
20 #include "api/video/video_sink_interface.h"
21 #include "api/video/video_source_interface.h"
22 #include "media/base/video_adapter.h"
23 #include "media/base/video_broadcaster.h"
24 #include "rtc_base/synchronization/mutex.h"
25 #include "rtc_base/system/rtc_export.h"
26 #include "rtc_base/thread_annotations.h"
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.
34 class RTC_EXPORT AdaptedVideoTrackSource
35     : public webrtc::Notifier<webrtc::VideoTrackSourceInterface> {
36  public:
37   AdaptedVideoTrackSource();
38   ~AdaptedVideoTrackSource() override;
39 
40  protected:
41   // Allows derived classes to initialize `video_adapter_` with a custom
42   // alignment.
43   explicit AdaptedVideoTrackSource(int required_alignment);
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.
47   void OnFrame(const webrtc::VideoFrame& frame);
48   // Indication from source that a frame was dropped.
49   void OnFrameDropped();
50 
51   // Reports the appropriate frame size after adaptation. Returns true
52   // if a frame is wanted. Returns false if there are no interested
53   // sinks, or if the VideoAdapter decides to drop the frame.
54   bool AdaptFrame(int width,
55                   int height,
56                   int64_t time_us,
57                   int* out_width,
58                   int* out_height,
59                   int* crop_width,
60                   int* crop_height,
61                   int* crop_x,
62                   int* crop_y);
63 
64   // Returns the current value of the apply_rotation flag, derived
65   // from the VideoSinkWants of registered sinks. The value is derived
66   // from sinks' wants, in AddOrUpdateSink and RemoveSink. Beware that
67   // when using this method from a different thread, the value may
68   // become stale before it is used.
69   bool apply_rotation();
70 
video_adapter()71   cricket::VideoAdapter* video_adapter() { return &video_adapter_; }
72 
73  private:
74   // Implements rtc::VideoSourceInterface.
75   void AddOrUpdateSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink,
76                        const rtc::VideoSinkWants& wants) override;
77   void RemoveSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) override;
78 
79   // Part of VideoTrackSourceInterface.
80   bool GetStats(Stats* stats) override;
81 
82   void OnSinkWantsChanged(const rtc::VideoSinkWants& wants);
83 
84   // Encoded sinks not implemented for AdaptedVideoTrackSource.
SupportsEncodedOutput()85   bool SupportsEncodedOutput() const override { return false; }
GenerateKeyFrame()86   void GenerateKeyFrame() override {}
AddEncodedSink(rtc::VideoSinkInterface<webrtc::RecordableEncodedFrame> * sink)87   void AddEncodedSink(
88       rtc::VideoSinkInterface<webrtc::RecordableEncodedFrame>* sink) override {}
RemoveEncodedSink(rtc::VideoSinkInterface<webrtc::RecordableEncodedFrame> * sink)89   void RemoveEncodedSink(
90       rtc::VideoSinkInterface<webrtc::RecordableEncodedFrame>* sink) override {}
91   void ProcessConstraints(
92       const webrtc::VideoTrackSourceConstraints& constraints) override;
93 
94   cricket::VideoAdapter video_adapter_;
95 
96   webrtc::Mutex stats_mutex_;
97   absl::optional<Stats> stats_ RTC_GUARDED_BY(stats_mutex_);
98 
99   VideoBroadcaster broadcaster_;
100 };
101 
102 }  // namespace rtc
103 
104 #endif  // MEDIA_BASE_ADAPTED_VIDEO_TRACK_SOURCE_H_
105