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.

113 lines
3.8 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 "RTCVideoTrack+Private.h"
  11. #import "RTCMediaStreamTrack+Private.h"
  12. #import "RTCPeerConnectionFactory+Private.h"
  13. #import "RTCVideoSource+Private.h"
  14. #import "api/RTCVideoRendererAdapter+Private.h"
  15. #import "helpers/NSString+StdString.h"
  16. @implementation RTCVideoTrack {
  17. NSMutableArray *_adapters;
  18. }
  19. @synthesize source = _source;
  20. - (instancetype)initWithFactory:(RTCPeerConnectionFactory *)factory
  21. source:(RTCVideoSource *)source
  22. trackId:(NSString *)trackId {
  23. NSParameterAssert(factory);
  24. NSParameterAssert(source);
  25. NSParameterAssert(trackId.length);
  26. std::string nativeId = [NSString stdStringForString:trackId];
  27. rtc::scoped_refptr<webrtc::VideoTrackInterface> track =
  28. factory.nativeFactory->CreateVideoTrack(nativeId,
  29. source.nativeVideoSource);
  30. if (self = [self initWithFactory:factory nativeTrack:track type:RTCMediaStreamTrackTypeVideo]) {
  31. _source = source;
  32. }
  33. return self;
  34. }
  35. - (instancetype)initWithFactory:(RTCPeerConnectionFactory *)factory
  36. nativeTrack:
  37. (rtc::scoped_refptr<webrtc::MediaStreamTrackInterface>)nativeMediaTrack
  38. type:(RTCMediaStreamTrackType)type {
  39. NSParameterAssert(factory);
  40. NSParameterAssert(nativeMediaTrack);
  41. NSParameterAssert(type == RTCMediaStreamTrackTypeVideo);
  42. if (self = [super initWithFactory:factory nativeTrack:nativeMediaTrack type:type]) {
  43. _adapters = [NSMutableArray array];
  44. }
  45. return self;
  46. }
  47. - (void)dealloc {
  48. for (RTCVideoRendererAdapter *adapter in _adapters) {
  49. self.nativeVideoTrack->RemoveSink(adapter.nativeVideoRenderer);
  50. }
  51. }
  52. - (RTCVideoSource *)source {
  53. if (!_source) {
  54. rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> source =
  55. self.nativeVideoTrack->GetSource();
  56. if (source) {
  57. _source =
  58. [[RTCVideoSource alloc] initWithFactory:self.factory nativeVideoSource:source.get()];
  59. }
  60. }
  61. return _source;
  62. }
  63. - (void)addRenderer:(id<RTCVideoRenderer>)renderer {
  64. // Make sure we don't have this renderer yet.
  65. for (RTCVideoRendererAdapter *adapter in _adapters) {
  66. if (adapter.videoRenderer == renderer) {
  67. NSAssert(NO, @"|renderer| is already attached to this track");
  68. return;
  69. }
  70. }
  71. // Create a wrapper that provides a native pointer for us.
  72. RTCVideoRendererAdapter* adapter =
  73. [[RTCVideoRendererAdapter alloc] initWithNativeRenderer:renderer];
  74. [_adapters addObject:adapter];
  75. self.nativeVideoTrack->AddOrUpdateSink(adapter.nativeVideoRenderer,
  76. rtc::VideoSinkWants());
  77. }
  78. - (void)removeRenderer:(id<RTCVideoRenderer>)renderer {
  79. __block NSUInteger indexToRemove = NSNotFound;
  80. [_adapters enumerateObjectsUsingBlock:^(RTCVideoRendererAdapter *adapter,
  81. NSUInteger idx,
  82. BOOL *stop) {
  83. if (adapter.videoRenderer == renderer) {
  84. indexToRemove = idx;
  85. *stop = YES;
  86. }
  87. }];
  88. if (indexToRemove == NSNotFound) {
  89. return;
  90. }
  91. RTCVideoRendererAdapter *adapterToRemove =
  92. [_adapters objectAtIndex:indexToRemove];
  93. self.nativeVideoTrack->RemoveSink(adapterToRemove.nativeVideoRenderer);
  94. [_adapters removeObjectAtIndex:indexToRemove];
  95. }
  96. #pragma mark - Private
  97. - (rtc::scoped_refptr<webrtc::VideoTrackInterface>)nativeVideoTrack {
  98. return static_cast<webrtc::VideoTrackInterface *>(self.nativeTrack.get());
  99. }
  100. @end