Fixed crash when PCF is destroyed before MediaSource/Track in ObjC

Bug: webrtc:9231
Change-Id: I31b86aa560f4ad230c9a94fedebebf320e0370a4
Reviewed-on: https://webrtc-review.googlesource.com/88221
Reviewed-by: Kári Helgason <kthelgason@webrtc.org>
Commit-Queue: Kári Helgason <kthelgason@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#23981}
diff --git a/sdk/objc/Framework/Classes/PeerConnection/RTCAudioSource+Private.h b/sdk/objc/Framework/Classes/PeerConnection/RTCAudioSource+Private.h
index 752eb66..63fff68 100644
--- a/sdk/objc/Framework/Classes/PeerConnection/RTCAudioSource+Private.h
+++ b/sdk/objc/Framework/Classes/PeerConnection/RTCAudioSource+Private.h
@@ -21,12 +21,12 @@
 @property(nonatomic, readonly) rtc::scoped_refptr<webrtc::AudioSourceInterface> nativeAudioSource;
 
 /** Initialize an RTCAudioSource from a native AudioSourceInterface. */
-- (instancetype)initWithNativeAudioSource:
-        (rtc::scoped_refptr<webrtc::AudioSourceInterface>)nativeAudioSource
+- (instancetype)initWithFactory:(RTCPeerConnectionFactory*)factory
+              nativeAudioSource:(rtc::scoped_refptr<webrtc::AudioSourceInterface>)nativeAudioSource
     NS_DESIGNATED_INITIALIZER;
 
-- (instancetype)initWithNativeMediaSource:
-                    (rtc::scoped_refptr<webrtc::MediaSourceInterface>)nativeMediaSource
-                                     type:(RTCMediaSourceType)type NS_UNAVAILABLE;
+- (instancetype)initWithFactory:(RTCPeerConnectionFactory*)factory
+              nativeMediaSource:(rtc::scoped_refptr<webrtc::MediaSourceInterface>)nativeMediaSource
+                           type:(RTCMediaSourceType)type NS_UNAVAILABLE;
 
 @end
diff --git a/sdk/objc/Framework/Classes/PeerConnection/RTCAudioSource.mm b/sdk/objc/Framework/Classes/PeerConnection/RTCAudioSource.mm
index 310171d..a6822f6 100644
--- a/sdk/objc/Framework/Classes/PeerConnection/RTCAudioSource.mm
+++ b/sdk/objc/Framework/Classes/PeerConnection/RTCAudioSource.mm
@@ -18,19 +18,23 @@
 @synthesize volume = _volume;
 @synthesize nativeAudioSource = _nativeAudioSource;
 
-- (instancetype)initWithNativeAudioSource:
-    (rtc::scoped_refptr<webrtc::AudioSourceInterface>)nativeAudioSource {
+- (instancetype)initWithFactory:(RTCPeerConnectionFactory *)factory
+              nativeAudioSource:
+                  (rtc::scoped_refptr<webrtc::AudioSourceInterface>)nativeAudioSource {
+  RTC_DCHECK(factory);
   RTC_DCHECK(nativeAudioSource);
-  if (self = [super initWithNativeMediaSource:nativeAudioSource
-                                         type:RTCMediaSourceTypeAudio]) {
+
+  if (self = [super initWithFactory:factory
+                  nativeMediaSource:nativeAudioSource
+                               type:RTCMediaSourceTypeAudio]) {
     _nativeAudioSource = nativeAudioSource;
   }
   return self;
 }
 
-- (instancetype)initWithNativeMediaSource:
-    (rtc::scoped_refptr<webrtc::MediaSourceInterface>)nativeMediaSource
-                                     type:(RTCMediaSourceType)type {
+- (instancetype)initWithFactory:(RTCPeerConnectionFactory *)factory
+              nativeMediaSource:(rtc::scoped_refptr<webrtc::MediaSourceInterface>)nativeMediaSource
+                           type:(RTCMediaSourceType)type {
   RTC_NOTREACHED();
   return nil;
 }
diff --git a/sdk/objc/Framework/Classes/PeerConnection/RTCAudioTrack.mm b/sdk/objc/Framework/Classes/PeerConnection/RTCAudioTrack.mm
index e26088f..73de401 100644
--- a/sdk/objc/Framework/Classes/PeerConnection/RTCAudioTrack.mm
+++ b/sdk/objc/Framework/Classes/PeerConnection/RTCAudioTrack.mm
@@ -31,18 +31,19 @@
   std::string nativeId = [NSString stdStringForString:trackId];
   rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
       factory.nativeFactory->CreateAudioTrack(nativeId, source.nativeAudioSource);
-  if (self = [self initWithNativeTrack:track type:RTCMediaStreamTrackTypeAudio]) {
+  if (self = [self initWithFactory:factory nativeTrack:track type:RTCMediaStreamTrackTypeAudio]) {
     _source = source;
   }
   return self;
 }
 
-- (instancetype)initWithNativeTrack:
-    (rtc::scoped_refptr<webrtc::MediaStreamTrackInterface>)nativeTrack
-                               type:(RTCMediaStreamTrackType)type {
+- (instancetype)initWithFactory:(RTCPeerConnectionFactory *)factory
+                    nativeTrack:(rtc::scoped_refptr<webrtc::MediaStreamTrackInterface>)nativeTrack
+                           type:(RTCMediaStreamTrackType)type {
+  NSParameterAssert(factory);
   NSParameterAssert(nativeTrack);
   NSParameterAssert(type == RTCMediaStreamTrackTypeAudio);
-  return [super initWithNativeTrack:nativeTrack type:type];
+  return [super initWithFactory:factory nativeTrack:nativeTrack type:type];
 }
 
 
@@ -51,7 +52,8 @@
     rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
         self.nativeAudioTrack->GetSource();
     if (source) {
-      _source = [[RTCAudioSource alloc] initWithNativeAudioSource:source.get()];
+      _source =
+          [[RTCAudioSource alloc] initWithFactory:self.factory nativeAudioSource:source.get()];
     }
   }
   return _source;
diff --git a/sdk/objc/Framework/Classes/PeerConnection/RTCMediaSource+Private.h b/sdk/objc/Framework/Classes/PeerConnection/RTCMediaSource+Private.h
index f638249..9883faf 100644
--- a/sdk/objc/Framework/Classes/PeerConnection/RTCMediaSource+Private.h
+++ b/sdk/objc/Framework/Classes/PeerConnection/RTCMediaSource+Private.h
@@ -14,6 +14,8 @@
 
 NS_ASSUME_NONNULL_BEGIN
 
+@class RTCPeerConnectionFactory;
+
 typedef NS_ENUM(NSInteger, RTCMediaSourceType) {
   RTCMediaSourceTypeAudio,
   RTCMediaSourceTypeVideo,
@@ -23,9 +25,9 @@
 
 @property(nonatomic, readonly) rtc::scoped_refptr<webrtc::MediaSourceInterface> nativeMediaSource;
 
-- (instancetype)initWithNativeMediaSource:
-                    (rtc::scoped_refptr<webrtc::MediaSourceInterface>)nativeMediaSource
-                                     type:(RTCMediaSourceType)type NS_DESIGNATED_INITIALIZER;
+- (instancetype)initWithFactory:(RTCPeerConnectionFactory *)factory
+              nativeMediaSource:(rtc::scoped_refptr<webrtc::MediaSourceInterface>)nativeMediaSource
+                           type:(RTCMediaSourceType)type NS_DESIGNATED_INITIALIZER;
 
 + (webrtc::MediaSourceInterface::SourceState)nativeSourceStateForState:(RTCSourceState)state;
 
diff --git a/sdk/objc/Framework/Classes/PeerConnection/RTCMediaSource.mm b/sdk/objc/Framework/Classes/PeerConnection/RTCMediaSource.mm
index 0f8cadc..6ec41c3 100644
--- a/sdk/objc/Framework/Classes/PeerConnection/RTCMediaSource.mm
+++ b/sdk/objc/Framework/Classes/PeerConnection/RTCMediaSource.mm
@@ -13,16 +13,19 @@
 #include "rtc_base/checks.h"
 
 @implementation RTCMediaSource {
+  RTCPeerConnectionFactory *_factory;
   RTCMediaSourceType _type;
 }
 
 @synthesize nativeMediaSource = _nativeMediaSource;
 
-- (instancetype)initWithNativeMediaSource:
-   (rtc::scoped_refptr<webrtc::MediaSourceInterface>)nativeMediaSource
-                                     type:(RTCMediaSourceType)type {
+- (instancetype)initWithFactory:(RTCPeerConnectionFactory *)factory
+              nativeMediaSource:(rtc::scoped_refptr<webrtc::MediaSourceInterface>)nativeMediaSource
+                           type:(RTCMediaSourceType)type {
+  RTC_DCHECK(factory);
   RTC_DCHECK(nativeMediaSource);
   if (self = [super init]) {
+    _factory = factory;
     _nativeMediaSource = nativeMediaSource;
     _type = type;
   }
diff --git a/sdk/objc/Framework/Classes/PeerConnection/RTCMediaStream.mm b/sdk/objc/Framework/Classes/PeerConnection/RTCMediaStream.mm
index 52f1771..c8bcfd9 100644
--- a/sdk/objc/Framework/Classes/PeerConnection/RTCMediaStream.mm
+++ b/sdk/objc/Framework/Classes/PeerConnection/RTCMediaStream.mm
@@ -109,14 +109,14 @@
     for (auto &track : audioTracks) {
       RTCMediaStreamTrackType type = RTCMediaStreamTrackTypeAudio;
       RTCAudioTrack *audioTrack =
-          [[RTCAudioTrack alloc] initWithNativeTrack:track type:type];
+          [[RTCAudioTrack alloc] initWithFactory:_factory nativeTrack:track type:type];
       [_audioTracks addObject:audioTrack];
     }
 
     for (auto &track : videoTracks) {
       RTCMediaStreamTrackType type = RTCMediaStreamTrackTypeVideo;
       RTCVideoTrack *videoTrack =
-          [[RTCVideoTrack alloc] initWithNativeTrack:track type:type];
+          [[RTCVideoTrack alloc] initWithFactory:_factory nativeTrack:track type:type];
       [_videoTracks addObject:videoTrack];
     }
   }
diff --git a/sdk/objc/Framework/Classes/PeerConnection/RTCMediaStreamTrack+Private.h b/sdk/objc/Framework/Classes/PeerConnection/RTCMediaStreamTrack+Private.h
index bb24216..6effeaa 100644
--- a/sdk/objc/Framework/Classes/PeerConnection/RTCMediaStreamTrack+Private.h
+++ b/sdk/objc/Framework/Classes/PeerConnection/RTCMediaStreamTrack+Private.h
@@ -19,8 +19,12 @@
 
 NS_ASSUME_NONNULL_BEGIN
 
+@class RTCPeerConnectionFactory;
+
 @interface RTCMediaStreamTrack ()
 
+@property(nonatomic, readonly) RTCPeerConnectionFactory *factory;
+
 /**
  * The native MediaStreamTrackInterface passed in or created during
  * construction.
@@ -30,12 +34,12 @@
 /**
  * Initialize an RTCMediaStreamTrack from a native MediaStreamTrackInterface.
  */
-- (instancetype)initWithNativeTrack:
-                    (rtc::scoped_refptr<webrtc::MediaStreamTrackInterface>)nativeTrack
-                               type:(RTCMediaStreamTrackType)type NS_DESIGNATED_INITIALIZER;
+- (instancetype)initWithFactory:(RTCPeerConnectionFactory *)factory
+                    nativeTrack:(rtc::scoped_refptr<webrtc::MediaStreamTrackInterface>)nativeTrack
+                           type:(RTCMediaStreamTrackType)type NS_DESIGNATED_INITIALIZER;
 
-- (instancetype)initWithNativeTrack:
-        (rtc::scoped_refptr<webrtc::MediaStreamTrackInterface>)nativeTrack;
+- (instancetype)initWithFactory:(RTCPeerConnectionFactory *)factory
+                    nativeTrack:(rtc::scoped_refptr<webrtc::MediaStreamTrackInterface>)nativeTrack;
 
 - (BOOL)isEqualToTrack:(RTCMediaStreamTrack *)track;
 
@@ -48,7 +52,8 @@
 + (NSString *)stringForState:(RTCMediaStreamTrackState)state;
 
 + (RTCMediaStreamTrack *)mediaTrackForNativeTrack:
-        (rtc::scoped_refptr<webrtc::MediaStreamTrackInterface>)nativeTrack;
+                             (rtc::scoped_refptr<webrtc::MediaStreamTrackInterface>)nativeTrack
+                                          factory:(RTCPeerConnectionFactory *)factory;
 
 @end
 
diff --git a/sdk/objc/Framework/Classes/PeerConnection/RTCMediaStreamTrack.mm b/sdk/objc/Framework/Classes/PeerConnection/RTCMediaStreamTrack.mm
index 5c1b5a3..07bb009 100644
--- a/sdk/objc/Framework/Classes/PeerConnection/RTCMediaStreamTrack.mm
+++ b/sdk/objc/Framework/Classes/PeerConnection/RTCMediaStreamTrack.mm
@@ -20,6 +20,7 @@
     @(webrtc::MediaStreamTrackInterface::kVideoKind);
 
 @implementation RTCMediaStreamTrack {
+  RTCPeerConnectionFactory *_factory;
   rtc::scoped_refptr<webrtc::MediaStreamTrackInterface> _nativeTrack;
   RTCMediaStreamTrackType _type;
 }
@@ -73,29 +74,31 @@
   return _nativeTrack;
 }
 
-- (instancetype)initWithNativeTrack:
-    (rtc::scoped_refptr<webrtc::MediaStreamTrackInterface>)nativeTrack
-                               type:(RTCMediaStreamTrackType)type {
+@synthesize factory = _factory;
+
+- (instancetype)initWithFactory:(RTCPeerConnectionFactory *)factory
+                    nativeTrack:(rtc::scoped_refptr<webrtc::MediaStreamTrackInterface>)nativeTrack
+                           type:(RTCMediaStreamTrackType)type {
   NSParameterAssert(nativeTrack);
+  NSParameterAssert(factory);
   if (self = [super init]) {
+    _factory = factory;
     _nativeTrack = nativeTrack;
     _type = type;
   }
   return self;
 }
 
-- (instancetype)initWithNativeTrack:
-    (rtc::scoped_refptr<webrtc::MediaStreamTrackInterface>)nativeTrack {
+- (instancetype)initWithFactory:(RTCPeerConnectionFactory *)factory
+                    nativeTrack:(rtc::scoped_refptr<webrtc::MediaStreamTrackInterface>)nativeTrack {
   NSParameterAssert(nativeTrack);
   if (nativeTrack->kind() ==
       std::string(webrtc::MediaStreamTrackInterface::kAudioKind)) {
-    return [self initWithNativeTrack:nativeTrack
-                                type:RTCMediaStreamTrackTypeAudio];
+    return [self initWithFactory:factory nativeTrack:nativeTrack type:RTCMediaStreamTrackTypeAudio];
   }
   if (nativeTrack->kind() ==
       std::string(webrtc::MediaStreamTrackInterface::kVideoKind)) {
-    return [self initWithNativeTrack:nativeTrack
-                                type:RTCMediaStreamTrackTypeVideo];
+    return [self initWithFactory:factory nativeTrack:nativeTrack type:RTCMediaStreamTrackTypeVideo];
   }
   return nil;
 }
@@ -137,16 +140,20 @@
 }
 
 + (RTCMediaStreamTrack *)mediaTrackForNativeTrack:
-        (rtc::scoped_refptr<webrtc::MediaStreamTrackInterface>)nativeTrack {
+                             (rtc::scoped_refptr<webrtc::MediaStreamTrackInterface>)nativeTrack
+                                          factory:(RTCPeerConnectionFactory *)factory {
   NSParameterAssert(nativeTrack);
+  NSParameterAssert(factory);
   if (nativeTrack->kind() == webrtc::MediaStreamTrackInterface::kAudioKind) {
-    return
-        [[RTCAudioTrack alloc] initWithNativeTrack:nativeTrack type:RTCMediaStreamTrackTypeAudio];
+    return [[RTCAudioTrack alloc] initWithFactory:factory
+                                      nativeTrack:nativeTrack
+                                             type:RTCMediaStreamTrackTypeAudio];
   } else if (nativeTrack->kind() == webrtc::MediaStreamTrackInterface::kVideoKind) {
-    return
-        [[RTCVideoTrack alloc] initWithNativeTrack:nativeTrack type:RTCMediaStreamTrackTypeVideo];
+    return [[RTCVideoTrack alloc] initWithFactory:factory
+                                      nativeTrack:nativeTrack
+                                             type:RTCMediaStreamTrackTypeVideo];
   } else {
-    return [[RTCMediaStreamTrack alloc] initWithNativeTrack:nativeTrack];
+    return [[RTCMediaStreamTrack alloc] initWithFactory:factory nativeTrack:nativeTrack];
   }
 }
 
diff --git a/sdk/objc/Framework/Classes/PeerConnection/RTCPeerConnectionFactory.mm b/sdk/objc/Framework/Classes/PeerConnection/RTCPeerConnectionFactory.mm
index 4061c02..4c801f0 100644
--- a/sdk/objc/Framework/Classes/PeerConnection/RTCPeerConnectionFactory.mm
+++ b/sdk/objc/Framework/Classes/PeerConnection/RTCPeerConnectionFactory.mm
@@ -180,7 +180,7 @@
 
   rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
       _nativeFactory->CreateAudioSource(options);
-  return [[RTCAudioSource alloc] initWithNativeAudioSource:source];
+  return [[RTCAudioSource alloc] initWithFactory:self nativeAudioSource:source];
 }
 
 - (RTCAudioTrack *)audioTrackWithTrackId:(NSString *)trackId {
@@ -196,8 +196,9 @@
 }
 
 - (RTCVideoSource *)videoSource {
-  return [[RTCVideoSource alloc] initWithSignalingThread:_signalingThread.get()
-                                            workerThread:_workerThread.get()];
+  return [[RTCVideoSource alloc] initWithFactory:self
+                                 signalingThread:_signalingThread.get()
+                                    workerThread:_workerThread.get()];
 }
 
 - (RTCVideoTrack *)videoTrackWithSource:(RTCVideoSource *)source
diff --git a/sdk/objc/Framework/Classes/PeerConnection/RTCRtpReceiver.mm b/sdk/objc/Framework/Classes/PeerConnection/RTCRtpReceiver.mm
index 1342c16..895c451 100644
--- a/sdk/objc/Framework/Classes/PeerConnection/RTCRtpReceiver.mm
+++ b/sdk/objc/Framework/Classes/PeerConnection/RTCRtpReceiver.mm
@@ -63,7 +63,7 @@
   rtc::scoped_refptr<webrtc::MediaStreamTrackInterface> nativeTrack(
     _nativeRtpReceiver->track());
   if (nativeTrack) {
-    return [RTCMediaStreamTrack mediaTrackForNativeTrack:nativeTrack];
+    return [RTCMediaStreamTrack mediaTrackForNativeTrack:nativeTrack factory:_factory];
   }
   return nil;
 }
diff --git a/sdk/objc/Framework/Classes/PeerConnection/RTCRtpSender.mm b/sdk/objc/Framework/Classes/PeerConnection/RTCRtpSender.mm
index 1df7ae5..6a46edf 100644
--- a/sdk/objc/Framework/Classes/PeerConnection/RTCRtpSender.mm
+++ b/sdk/objc/Framework/Classes/PeerConnection/RTCRtpSender.mm
@@ -45,7 +45,7 @@
   rtc::scoped_refptr<webrtc::MediaStreamTrackInterface> nativeTrack(
     _nativeRtpSender->track());
   if (nativeTrack) {
-    return [RTCMediaStreamTrack mediaTrackForNativeTrack:nativeTrack];
+    return [RTCMediaStreamTrack mediaTrackForNativeTrack:nativeTrack factory:_factory];
   }
   return nil;
 }
diff --git a/sdk/objc/Framework/Classes/PeerConnection/RTCVideoSource+Private.h b/sdk/objc/Framework/Classes/PeerConnection/RTCVideoSource+Private.h
index fdb4522..5eea2f9 100644
--- a/sdk/objc/Framework/Classes/PeerConnection/RTCVideoSource+Private.h
+++ b/sdk/objc/Framework/Classes/PeerConnection/RTCVideoSource+Private.h
@@ -26,16 +26,18 @@
     nativeVideoSource;
 
 /** Initialize an RTCVideoSource from a native VideoTrackSourceInterface. */
-- (instancetype)initWithNativeVideoSource:
-        (rtc::scoped_refptr<webrtc::VideoTrackSourceInterface>)nativeVideoSource
+- (instancetype)initWithFactory:(RTCPeerConnectionFactory *)factory
+              nativeVideoSource:
+                  (rtc::scoped_refptr<webrtc::VideoTrackSourceInterface>)nativeVideoSource
     NS_DESIGNATED_INITIALIZER;
 
-- (instancetype)initWithNativeMediaSource:
-                    (rtc::scoped_refptr<webrtc::MediaSourceInterface>)nativeMediaSource
-                                     type:(RTCMediaSourceType)type NS_UNAVAILABLE;
+- (instancetype)initWithFactory:(RTCPeerConnectionFactory *)factory
+              nativeMediaSource:(rtc::scoped_refptr<webrtc::MediaSourceInterface>)nativeMediaSource
+                           type:(RTCMediaSourceType)type NS_UNAVAILABLE;
 
-- (instancetype)initWithSignalingThread:(rtc::Thread *)signalingThread
-                           workerThread:(rtc::Thread *)workerThread;
+- (instancetype)initWithFactory:(RTCPeerConnectionFactory *)factory
+                signalingThread:(rtc::Thread *)signalingThread
+                   workerThread:(rtc::Thread *)workerThread;
 
 @end
 
diff --git a/sdk/objc/Framework/Classes/PeerConnection/RTCVideoSource.mm b/sdk/objc/Framework/Classes/PeerConnection/RTCVideoSource.mm
index f380af4..63b8014 100644
--- a/sdk/objc/Framework/Classes/PeerConnection/RTCVideoSource.mm
+++ b/sdk/objc/Framework/Classes/PeerConnection/RTCVideoSource.mm
@@ -28,30 +28,35 @@
   rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> _nativeVideoSource;
 }
 
-- (instancetype)initWithNativeVideoSource:
-    (rtc::scoped_refptr<webrtc::VideoTrackSourceInterface>)nativeVideoSource {
+- (instancetype)initWithFactory:(RTCPeerConnectionFactory *)factory
+              nativeVideoSource:
+                  (rtc::scoped_refptr<webrtc::VideoTrackSourceInterface>)nativeVideoSource {
+  RTC_DCHECK(factory);
   RTC_DCHECK(nativeVideoSource);
-  if (self = [super initWithNativeMediaSource:nativeVideoSource
-                                         type:RTCMediaSourceTypeVideo]) {
+  if (self = [super initWithFactory:factory
+                  nativeMediaSource:nativeVideoSource
+                               type:RTCMediaSourceTypeVideo]) {
     _nativeVideoSource = nativeVideoSource;
   }
   return self;
 }
 
-- (instancetype)initWithNativeMediaSource:
-    (rtc::scoped_refptr<webrtc::MediaSourceInterface>)nativeMediaSource
-                                     type:(RTCMediaSourceType)type {
+- (instancetype)initWithFactory:(RTCPeerConnectionFactory *)factory
+              nativeMediaSource:(rtc::scoped_refptr<webrtc::MediaSourceInterface>)nativeMediaSource
+                           type:(RTCMediaSourceType)type {
   RTC_NOTREACHED();
   return nil;
 }
 
-- (instancetype)initWithSignalingThread:(rtc::Thread *)signalingThread
-                           workerThread:(rtc::Thread *)workerThread {
+- (instancetype)initWithFactory:(RTCPeerConnectionFactory *)factory
+                signalingThread:(rtc::Thread *)signalingThread
+                   workerThread:(rtc::Thread *)workerThread {
   rtc::scoped_refptr<webrtc::ObjCVideoTrackSource> objCVideoTrackSource(
       new rtc::RefCountedObject<webrtc::ObjCVideoTrackSource>());
 
-  return [self initWithNativeVideoSource:webrtc::VideoTrackSourceProxy::Create(
-                                             signalingThread, workerThread, objCVideoTrackSource)];
+  return [self initWithFactory:factory
+             nativeVideoSource:webrtc::VideoTrackSourceProxy::Create(
+                                   signalingThread, workerThread, objCVideoTrackSource)];
 }
 
 - (NSString *)description {
diff --git a/sdk/objc/Framework/Classes/PeerConnection/RTCVideoTrack.mm b/sdk/objc/Framework/Classes/PeerConnection/RTCVideoTrack.mm
index 2f03110..c9eb35c 100644
--- a/sdk/objc/Framework/Classes/PeerConnection/RTCVideoTrack.mm
+++ b/sdk/objc/Framework/Classes/PeerConnection/RTCVideoTrack.mm
@@ -32,18 +32,20 @@
   rtc::scoped_refptr<webrtc::VideoTrackInterface> track =
       factory.nativeFactory->CreateVideoTrack(nativeId,
                                               source.nativeVideoSource);
-  if (self = [self initWithNativeTrack:track type:RTCMediaStreamTrackTypeVideo]) {
+  if (self = [self initWithFactory:factory nativeTrack:track type:RTCMediaStreamTrackTypeVideo]) {
     _source = source;
   }
   return self;
 }
 
-- (instancetype)initWithNativeTrack:
-    (rtc::scoped_refptr<webrtc::MediaStreamTrackInterface>)nativeMediaTrack
-                               type:(RTCMediaStreamTrackType)type {
+- (instancetype)initWithFactory:(RTCPeerConnectionFactory *)factory
+                    nativeTrack:
+                        (rtc::scoped_refptr<webrtc::MediaStreamTrackInterface>)nativeMediaTrack
+                           type:(RTCMediaStreamTrackType)type {
+  NSParameterAssert(factory);
   NSParameterAssert(nativeMediaTrack);
   NSParameterAssert(type == RTCMediaStreamTrackTypeVideo);
-  if (self = [super initWithNativeTrack:nativeMediaTrack type:type]) {
+  if (self = [super initWithFactory:factory nativeTrack:nativeMediaTrack type:type]) {
     _adapters = [NSMutableArray array];
   }
   return self;
@@ -60,7 +62,8 @@
     rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> source =
         self.nativeVideoTrack->GetSource();
     if (source) {
-      _source = [[RTCVideoSource alloc] initWithNativeVideoSource:source.get()];
+      _source =
+          [[RTCVideoSource alloc] initWithFactory:self.factory nativeVideoSource:source.get()];
     }
   }
   return _source;