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 
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 
video_adapter()69   cricket::VideoAdapter* video_adapter() { return &video_adapter_; }
70 
71  private:
72   // Implements rtc::VideoSourceInterface.
73   void AddOrUpdateSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink,
74                        const rtc::VideoSinkWants& wants) override;
75   void RemoveSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) override;
76 
77   // Part of VideoTrackSourceInterface.
78   bool GetStats(Stats* stats) override;
79 
80   void OnSinkWantsChanged(const rtc::VideoSinkWants& wants);
81 
82   // Encoded sinks not implemented for AdaptedVideoTrackSource.
SupportsEncodedOutput()83   bool SupportsEncodedOutput() const override { return false; }
GenerateKeyFrame()84   void GenerateKeyFrame() override {}
AddEncodedSink(rtc::VideoSinkInterface<webrtc::RecordableEncodedFrame> * sink)85   void AddEncodedSink(
86       rtc::VideoSinkInterface<webrtc::RecordableEncodedFrame>* sink) override {}
RemoveEncodedSink(rtc::VideoSinkInterface<webrtc::RecordableEncodedFrame> * sink)87   void RemoveEncodedSink(
88       rtc::VideoSinkInterface<webrtc::RecordableEncodedFrame>* sink) override {}
89 
90   cricket::VideoAdapter video_adapter_;
91 
92   webrtc::Mutex stats_mutex_;
93   absl::optional<Stats> stats_ RTC_GUARDED_BY(stats_mutex_);
94 
95   VideoBroadcaster broadcaster_;
96 };
97 
98 }  // namespace rtc
99 
100 #endif  // MEDIA_BASE_ADAPTED_VIDEO_TRACK_SOURCE_H_
101