blob: 316dd788ef7ca16df6ba4dc3c2d9c8d1d20824d7 [file] [log] [blame]
Harald Alvestrand9cb42c82020-10-11 13:03:471/*
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
Niels Möllerc397fc62022-05-30 09:26:4016#include "absl/strings/string_view.h"
Harald Alvestrand9cb42c82020-10-11 13:03:4717#include "api/media_stream_interface.h"
18#include "api/notifier.h"
19
20namespace webrtc {
21
22// MediaTrack implements the interface common to AudioTrackInterface and
23// VideoTrackInterface.
24template <typename T>
25class MediaStreamTrack : public Notifier<T> {
26 public:
27 typedef typename T::TrackState TypedTrackState;
28
29 std::string id() const override { return id_; }
30 MediaStreamTrackInterface::TrackState state() const override {
31 return state_;
32 }
33 bool enabled() const override { return enabled_; }
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 }
42 void set_ended() { set_state(MediaStreamTrackInterface::TrackState::kEnded); }
43
44 protected:
Niels Möllerc397fc62022-05-30 09:26:4045 explicit MediaStreamTrack(absl::string_view id)
Harald Alvestrand9cb42c82020-10-11 13:03:4746 : enabled_(true), id_(id), state_(MediaStreamTrackInterface::kLive) {}
47
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_