MixedReality-WebRTC icon indicating copy to clipboard operation
MixedReality-WebRTC copied to clipboard

Suggestion to encapsulate ExternalVideoTrackSource into LocalVideoTrack

Open drejx opened this issue 6 years ago • 2 comments

Hello,

I've been working on using ExternalVideoTrackSource to provide my own video frame feed into the MixedReality SDK and got burnt by a silly mistake that I think can easily bite others. I thought that ExternalVideoTrackSource represents the track and external source together and used it like so:

Init

await peer.InitializeAsync(peerConfig);                                
ExternalVideoTrackSource externalVideoFrameSource = ExternalVideoTrackSource.CreateFromI420ACallback(I420ExternalFrameRequestCallback);
peer.AddCustomLocalVideoTrack("shmoo", externalVideoFrameSource);

And then on shutdown did cleanup like so:

Shutdown

externalVideoFrameSource.Dispose();
externalVideoFrameSource = null;
peer.Dispose();
peer = null;

The next time I ran/played I crashed in the native DLL:

0x00000144120627A7 (Microsoft.MixedReality.WebRTC) Microsoft.MixedReality.WebRTC.Interop.LocalVideoTrackInterop.LocalVideoTrack_RemoveRef()
0x000001441206265B (Microsoft.MixedReality.WebRTC) Microsoft.MixedReality.WebRTC.Interop.LocalVideoTrackHandle.ReleaseHandle()
0x000001447A43CDC3 (mscorlib) System.Runtime.InteropServices.SafeHandle.DangerousReleaseInternal()
0x0000014411FC449B (mscorlib) System.Runtime.InteropServices.SafeHandle.InternalFinalize()
0x0000014411ED1813 (mscorlib) System.Runtime.InteropServices.SafeHandle.Dispose()
0x0000014411FC42DC (mscorlib) System.Runtime.InteropServices.SafeHandle.Finalize()
0x000001447B4A94DC (mscorlib) System.Object.runtime_invoke_virtual_void__this__()
0x00007FF9F85F37F9 (mono-2.0-bdwgc) mono_gc_reference_queue_new
0x00007FF9F85F26E5 (mono-2.0-bdwgc) mono_callspec_parse
0x00007FF9F85B58F6 (mono-2.0-bdwgc) mono_threads_set_shutting_down

I was starting to dig into the native code when I figured out that even though I created ExternalVideoTrackSource I still needed to keep a LocalVideoTrack reference around and dispose it. So changing my code like so does not crash:

Init2

await peer.InitializeAsync(peerConfig);
ExternalVideoTrackSource externalVideoFrameSource = ExternalVideoTrackSource.CreateFromI420ACallback(I420ExternalFrameRequestCallback);
LocalVideoTrack localVideoTrack = peer.AddCustomLocalVideoTrack("shmoo", externalVideoFrameSource);  // <<<< STORE RETURNED LocalVideoTrack

Shutdown2

localVideoTrack.Dispose();  // <<<< DISPOSE IT HERE
localVideoTrack = null;
externalVideoFrameSource.Dispose();
externalVideoFrameSource = null;
peer.Dispose();
peer = null;

But I think to simplify things for the caller and avoid running into this crash situation it would be useful to simply encapsulate ExternalVideoTrackSource into 'LocalVideoTrack' because:

  • I don't think users need to actually interact/use ExternalVideoTrackSource
  • 'LocalVideoTrack' already has the I420AVideoFrameReady delegate to send video frames to the caller, so it makes sense there is a complimentary delegate for receiving external video frames from the caller.
  • 'LocalVideoTrack''s Dispose() method can also handle disposing ExternalVideoTrackSource so the caller doesn't have to worry about it (keeps the code path the same as when not using externally provided video frames)
  • You have to use 'LocalVideoTrack' anyway so it's a great home base for ExternalVideoTrackSource.

I imagine the flow being something akin to:

Init

await peer.InitializeAsync(peerConfig);
LocalVideoTrack localVideoTrack = peer.AddCustomLocalVideoTrack("shmoo", externalVideoFrameSource);  // internally creates ExternalVideoFrameSource
localVideoTrack.OnI420ExternalVideoFrameRequest += I420ExternalFrameRequestCallback;

Shutdown

peer.RemoveLocalVideoTrack(localVideoTrack);  // internally disposes ExternalVideoFrameSource instance
localVideoTrack.Dispose();
localVideoTrack = null;
peer.Dispose();
peer = null;

This was supposed to be a short post, oops :) Hopefully it will at least help someone if they're running into the same silly issue I had. There might be reasons I'm unaware of why it was separated, but I think it's safe to say that there should definitely be an error logged rather than a crash in native code.

Cheers, Andrej

drejx avatar Feb 27 '20 22:02 drejx

Hi @drejx,

Sorry about that. Thanks for the feedback and letting other users know about it.

I see the simplicity value of merging the local video track and its source. However those are two separate concepts, and are in fact implemented as two separate objects under the hood. More importantly, I don't think we will make any change for the following reasons:

  • The multi-track feature is being merged (see #202), and with it the new transceiver-based API which will change this because the track will need to be assigned to a transceiver, so you will have to store the return value of AddCustomLocalVideoTrack(). That will I think prevent this kind of error.

  • LocalVideoTrack is an IDisposable object, therefore by design this tells the user they need to keep a reference to dispose it. I know this is error-prone for some users (including me), but this is I think a design flaw of C# itself and the disposable pattern, so C# users familiar with it shouldn't be surprised.

  • Going forward there is a good chance that we introduce other kind of media sources, and possibly allow the user to build their own (for example to support a new device or to access capture options not exposed), and this design makes it easier to use those sources with the existing LocalVideoTrack. The alternative of merging them means that we would have to change the LocalVideoTrack API, and also that extending with user implementations would be more difficult I think.

Any thought on that?

jehumb-havok avatar Feb 28 '20 10:02 jehumb-havok

Hi @djee-ms,

Nice on the multi-track and transceiver change!

I definitely agree that LocalVideoTrack should remain and held closely by the caller. I was more thinking that ExternalVideoSource doesn't need to be exposed to the caller. Or at least not to have it a requirement of creating a custom local source, but it could optionally be available by a method/property on LocalVideoTrack if you wished to access it. Another option is creating a LocalCustomVideoTrack class that inherits from LocalVideoTrack and it has all the nuts and bolts for handling custom video sources.

Mind you, I'm looking at it from a high level point of view and certainly don't have the nuts and bolts view of you and your colleagues ;)

Cheers, Andrej

drejx avatar Feb 29 '20 00:02 drejx