xref: /aosp_15_r20/external/webrtc/pc/video_track.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright 2012 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 PC_VIDEO_TRACK_H_
12 #define PC_VIDEO_TRACK_H_
13 
14 #include <string>
15 
16 #include "absl/types/optional.h"
17 #include "api/media_stream_interface.h"
18 #include "api/media_stream_track.h"
19 #include "api/scoped_refptr.h"
20 #include "api/sequence_checker.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 "media/base/video_source_base.h"
25 #include "pc/video_track_source_proxy.h"
26 #include "rtc_base/system/no_unique_address.h"
27 #include "rtc_base/thread.h"
28 #include "rtc_base/thread_annotations.h"
29 
30 namespace webrtc {
31 
32 // TODO(tommi): Instead of inheriting from `MediaStreamTrack<>`, implement the
33 // properties directly in this class. `MediaStreamTrack` doesn't guard against
34 // conflicting access, so we'd need to override those methods anyway in this
35 // class in order to make sure things are correctly checked.
36 class VideoTrack : public MediaStreamTrack<VideoTrackInterface>,
37                    public rtc::VideoSourceBaseGuarded,
38                    public ObserverInterface {
39  public:
40   static rtc::scoped_refptr<VideoTrack> Create(
41       absl::string_view label,
42       rtc::scoped_refptr<VideoTrackSourceInterface> source,
43       rtc::Thread* worker_thread);
44 
45   void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
46                        const rtc::VideoSinkWants& wants) override;
47   void RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) override;
48   void RequestRefreshFrame() override;
49   VideoTrackSourceInterface* GetSource() const override;
50 
51   ContentHint content_hint() const override;
52   void set_content_hint(ContentHint hint) override;
53   bool set_enabled(bool enable) override;
54   bool enabled() const override;
55   MediaStreamTrackInterface::TrackState state() const override;
56   std::string kind() const override;
57 
58   // Direct access to the non-proxied source object for internal implementation.
59   VideoTrackSourceInterface* GetSourceInternal() const;
60 
61  protected:
62   VideoTrack(
63       absl::string_view id,
64       rtc::scoped_refptr<
65           VideoTrackSourceProxyWithInternal<VideoTrackSourceInterface>> source,
66       rtc::Thread* worker_thread);
67   ~VideoTrack();
68 
69  private:
70   // Implements ObserverInterface. Observes `video_source_` state.
71   void OnChanged() override;
72 
73   RTC_NO_UNIQUE_ADDRESS webrtc::SequenceChecker signaling_thread_;
74   rtc::Thread* const worker_thread_;
75   const rtc::scoped_refptr<
76       VideoTrackSourceProxyWithInternal<VideoTrackSourceInterface>>
77       video_source_;
78   ContentHint content_hint_ RTC_GUARDED_BY(&signaling_thread_);
79   // Cached `enabled` state for the worker thread. This is kept in sync with
80   // the state maintained on the signaling thread via set_enabled() but can
81   // be queried without blocking on the worker thread by callers that don't
82   // use an api proxy to call the `enabled()` method.
83   bool enabled_w_ RTC_GUARDED_BY(worker_thread_) = true;
84 };
85 
86 }  // namespace webrtc
87 
88 #endif  // PC_VIDEO_TRACK_H_
89