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.

126 lines
4.2 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2015 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. #import "RTCMediaStream+Private.h"
  11. #include <vector>
  12. #import "RTCAudioTrack+Private.h"
  13. #import "RTCMediaStreamTrack+Private.h"
  14. #import "RTCPeerConnectionFactory+Private.h"
  15. #import "RTCVideoTrack+Private.h"
  16. #import "helpers/NSString+StdString.h"
  17. @implementation RTCMediaStream {
  18. RTCPeerConnectionFactory *_factory;
  19. NSMutableArray *_audioTracks;
  20. NSMutableArray *_videoTracks;
  21. rtc::scoped_refptr<webrtc::MediaStreamInterface> _nativeMediaStream;
  22. }
  23. - (instancetype)initWithFactory:(RTCPeerConnectionFactory *)factory
  24. streamId:(NSString *)streamId {
  25. NSParameterAssert(factory);
  26. NSParameterAssert(streamId.length);
  27. std::string nativeId = [NSString stdStringForString:streamId];
  28. rtc::scoped_refptr<webrtc::MediaStreamInterface> stream =
  29. factory.nativeFactory->CreateLocalMediaStream(nativeId);
  30. return [self initWithFactory:factory nativeMediaStream:stream];
  31. }
  32. - (NSArray<RTCAudioTrack *> *)audioTracks {
  33. return [_audioTracks copy];
  34. }
  35. - (NSArray<RTCVideoTrack *> *)videoTracks {
  36. return [_videoTracks copy];
  37. }
  38. - (NSString *)streamId {
  39. return [NSString stringForStdString:_nativeMediaStream->id()];
  40. }
  41. - (void)addAudioTrack:(RTCAudioTrack *)audioTrack {
  42. if (_nativeMediaStream->AddTrack(audioTrack.nativeAudioTrack)) {
  43. [_audioTracks addObject:audioTrack];
  44. }
  45. }
  46. - (void)addVideoTrack:(RTCVideoTrack *)videoTrack {
  47. if (_nativeMediaStream->AddTrack(videoTrack.nativeVideoTrack)) {
  48. [_videoTracks addObject:videoTrack];
  49. }
  50. }
  51. - (void)removeAudioTrack:(RTCAudioTrack *)audioTrack {
  52. NSUInteger index = [_audioTracks indexOfObjectIdenticalTo:audioTrack];
  53. NSAssert(index != NSNotFound,
  54. @"|removeAudioTrack| called on unexpected RTCAudioTrack");
  55. if (index != NSNotFound &&
  56. _nativeMediaStream->RemoveTrack(audioTrack.nativeAudioTrack)) {
  57. [_audioTracks removeObjectAtIndex:index];
  58. }
  59. }
  60. - (void)removeVideoTrack:(RTCVideoTrack *)videoTrack {
  61. NSUInteger index = [_videoTracks indexOfObjectIdenticalTo:videoTrack];
  62. NSAssert(index != NSNotFound,
  63. @"|removeVideoTrack| called on unexpected RTCVideoTrack");
  64. if (index != NSNotFound &&
  65. _nativeMediaStream->RemoveTrack(videoTrack.nativeVideoTrack)) {
  66. [_videoTracks removeObjectAtIndex:index];
  67. }
  68. }
  69. - (NSString *)description {
  70. return [NSString stringWithFormat:@"RTCMediaStream:\n%@\nA=%lu\nV=%lu",
  71. self.streamId,
  72. (unsigned long)self.audioTracks.count,
  73. (unsigned long)self.videoTracks.count];
  74. }
  75. #pragma mark - Private
  76. - (rtc::scoped_refptr<webrtc::MediaStreamInterface>)nativeMediaStream {
  77. return _nativeMediaStream;
  78. }
  79. - (instancetype)initWithFactory:(RTCPeerConnectionFactory *)factory
  80. nativeMediaStream:
  81. (rtc::scoped_refptr<webrtc::MediaStreamInterface>)nativeMediaStream {
  82. NSParameterAssert(nativeMediaStream);
  83. if (self = [super init]) {
  84. _factory = factory;
  85. webrtc::AudioTrackVector audioTracks = nativeMediaStream->GetAudioTracks();
  86. webrtc::VideoTrackVector videoTracks = nativeMediaStream->GetVideoTracks();
  87. _audioTracks = [NSMutableArray arrayWithCapacity:audioTracks.size()];
  88. _videoTracks = [NSMutableArray arrayWithCapacity:videoTracks.size()];
  89. _nativeMediaStream = nativeMediaStream;
  90. for (auto &track : audioTracks) {
  91. RTCMediaStreamTrackType type = RTCMediaStreamTrackTypeAudio;
  92. RTCAudioTrack *audioTrack =
  93. [[RTCAudioTrack alloc] initWithFactory:_factory nativeTrack:track type:type];
  94. [_audioTracks addObject:audioTrack];
  95. }
  96. for (auto &track : videoTracks) {
  97. RTCMediaStreamTrackType type = RTCMediaStreamTrackTypeVideo;
  98. RTCVideoTrack *videoTrack =
  99. [[RTCVideoTrack alloc] initWithFactory:_factory nativeTrack:track type:type];
  100. [_videoTracks addObject:videoTrack];
  101. }
  102. }
  103. return self;
  104. }
  105. @end