xref: /aosp_15_r20/external/webrtc/api/media_stream_track.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright 2011 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 API_MEDIA_STREAM_TRACK_H_
12 #define API_MEDIA_STREAM_TRACK_H_
13 
14 #include <string>
15 
16 #include "absl/strings/string_view.h"
17 #include "api/media_stream_interface.h"
18 #include "api/notifier.h"
19 
20 namespace webrtc {
21 
22 // MediaTrack implements the interface common to AudioTrackInterface and
23 // VideoTrackInterface.
24 template <typename T>
25 class MediaStreamTrack : public Notifier<T> {
26  public:
27   typedef typename T::TrackState TypedTrackState;
28 
id()29   std::string id() const override { return id_; }
state()30   MediaStreamTrackInterface::TrackState state() const override {
31     return state_;
32   }
enabled()33   bool enabled() const override { return enabled_; }
set_enabled(bool enable)34   bool set_enabled(bool enable) override {
35     bool fire_on_change = (enable != enabled_);
36     enabled_ = enable;
37     if (fire_on_change) {
38       Notifier<T>::FireOnChanged();
39     }
40     return fire_on_change;
41   }
set_ended()42   void set_ended() { set_state(MediaStreamTrackInterface::TrackState::kEnded); }
43 
44  protected:
MediaStreamTrack(absl::string_view id)45   explicit MediaStreamTrack(absl::string_view id)
46       : enabled_(true), id_(id), state_(MediaStreamTrackInterface::kLive) {}
47 
set_state(MediaStreamTrackInterface::TrackState new_state)48   bool set_state(MediaStreamTrackInterface::TrackState new_state) {
49     bool fire_on_change = (state_ != new_state);
50     state_ = new_state;
51     if (fire_on_change)
52       Notifier<T>::FireOnChanged();
53     return true;
54   }
55 
56  private:
57   bool enabled_;
58   const std::string id_;
59   MediaStreamTrackInterface::TrackState state_;
60 };
61 
62 }  // namespace webrtc
63 
64 #endif  // API_MEDIA_STREAM_TRACK_H_
65