You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
2.7 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #include "pc/media_stream.h"
  11. #include <stddef.h>
  12. #include <vector>
  13. #include "rtc_base/checks.h"
  14. #include "rtc_base/ref_counted_object.h"
  15. namespace webrtc {
  16. template <class V>
  17. static typename V::iterator FindTrack(V* vector, const std::string& track_id) {
  18. typename V::iterator it = vector->begin();
  19. for (; it != vector->end(); ++it) {
  20. if ((*it)->id() == track_id) {
  21. break;
  22. }
  23. }
  24. return it;
  25. }
  26. rtc::scoped_refptr<MediaStream> MediaStream::Create(const std::string& id) {
  27. rtc::RefCountedObject<MediaStream>* stream =
  28. new rtc::RefCountedObject<MediaStream>(id);
  29. return stream;
  30. }
  31. MediaStream::MediaStream(const std::string& id) : id_(id) {}
  32. bool MediaStream::AddTrack(AudioTrackInterface* track) {
  33. return AddTrack<AudioTrackVector, AudioTrackInterface>(&audio_tracks_, track);
  34. }
  35. bool MediaStream::AddTrack(VideoTrackInterface* track) {
  36. return AddTrack<VideoTrackVector, VideoTrackInterface>(&video_tracks_, track);
  37. }
  38. bool MediaStream::RemoveTrack(AudioTrackInterface* track) {
  39. return RemoveTrack<AudioTrackVector>(&audio_tracks_, track);
  40. }
  41. bool MediaStream::RemoveTrack(VideoTrackInterface* track) {
  42. return RemoveTrack<VideoTrackVector>(&video_tracks_, track);
  43. }
  44. rtc::scoped_refptr<AudioTrackInterface> MediaStream::FindAudioTrack(
  45. const std::string& track_id) {
  46. AudioTrackVector::iterator it = FindTrack(&audio_tracks_, track_id);
  47. if (it == audio_tracks_.end())
  48. return NULL;
  49. return *it;
  50. }
  51. rtc::scoped_refptr<VideoTrackInterface> MediaStream::FindVideoTrack(
  52. const std::string& track_id) {
  53. VideoTrackVector::iterator it = FindTrack(&video_tracks_, track_id);
  54. if (it == video_tracks_.end())
  55. return NULL;
  56. return *it;
  57. }
  58. template <typename TrackVector, typename Track>
  59. bool MediaStream::AddTrack(TrackVector* tracks, Track* track) {
  60. typename TrackVector::iterator it = FindTrack(tracks, track->id());
  61. if (it != tracks->end())
  62. return false;
  63. tracks->push_back(track);
  64. FireOnChanged();
  65. return true;
  66. }
  67. template <typename TrackVector>
  68. bool MediaStream::RemoveTrack(TrackVector* tracks,
  69. MediaStreamTrackInterface* track) {
  70. RTC_DCHECK(tracks != NULL);
  71. if (!track)
  72. return false;
  73. typename TrackVector::iterator it = FindTrack(tracks, track->id());
  74. if (it == tracks->end())
  75. return false;
  76. tracks->erase(it);
  77. FireOnChanged();
  78. return true;
  79. }
  80. } // namespace webrtc