media_stream_track.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. #ifndef PC_MEDIA_STREAM_TRACK_H_
  11. #define PC_MEDIA_STREAM_TRACK_H_
  12. #include <string>
  13. #include "api/media_stream_interface.h"
  14. #include "api/notifier.h"
  15. #include "rtc_base/logging.h"
  16. namespace webrtc {
  17. // MediaTrack implements the interface common to AudioTrackInterface and
  18. // VideoTrackInterface.
  19. template <typename T>
  20. class MediaStreamTrack : public Notifier<T> {
  21. public:
  22. typedef typename T::TrackState TypedTrackState;
  23. std::string id() const override { return id_; }
  24. MediaStreamTrackInterface::TrackState state() const override {
  25. return state_;
  26. }
  27. bool enabled() const override { return enabled_; }
  28. bool set_enabled(bool enable) override {
  29. bool fire_on_change = (enable != enabled_);
  30. enabled_ = enable;
  31. if (fire_on_change) {
  32. Notifier<T>::FireOnChanged();
  33. }
  34. return fire_on_change;
  35. }
  36. void set_ended() { set_state(MediaStreamTrackInterface::TrackState::kEnded); }
  37. protected:
  38. explicit MediaStreamTrack(const std::string& id)
  39. : enabled_(true), id_(id), state_(MediaStreamTrackInterface::kLive) {}
  40. bool set_state(MediaStreamTrackInterface::TrackState new_state) {
  41. bool fire_on_change = (state_ != new_state);
  42. state_ = new_state;
  43. if (fire_on_change)
  44. Notifier<T>::FireOnChanged();
  45. return true;
  46. }
  47. private:
  48. bool enabled_;
  49. const std::string id_;
  50. MediaStreamTrackInterface::TrackState state_;
  51. };
  52. } // namespace webrtc
  53. #endif // PC_MEDIA_STREAM_TRACK_H_