MixedRealityToolkit-Unity icon indicating copy to clipboard operation
MixedRealityToolkit-Unity copied to clipboard

Proposal: Webrtc Extension

Open bengreenier opened this issue 7 years ago • 21 comments

Overview

This is a proposal (and intent to implement) basic webrtc facilities as an MRTK extension. The goal is to enable audio, video, and data channels for use from managed code, without the need to write custom native plugins (however, do note that native plugins will be implemented under the hood for the caller, as they are necessary to bind to webrtc on a given target platform).

The goal at this time is to begin with support the following targets:

  • Win32 Standalone
  • Android
  • WinUWP (non-ARM at this time)

Support for other targets (with ARM being the likely next candidate) can be added in the future. The planned approach will mirror the webrtc unityplugin example, and will be largely a refactor from bengreenier/webrtc-unity-plugin which supports webrtc (m71) facilities and includes some examples.

The native plugin work (a slight modification from google source, to support WINUWP and il2cpp) is based on webrtc-uwp, and can be seen in this PR.

In my opinion it's worth including a disclaimer here that this is a basic, best-effort solution and is not intended for high-scale production use at this time, but is designed to get us closer to a production story. I'm thinking of it as one of the core pieces in a larger puzzle. Hopefully that makes sense to folks.

Licensing

Managed code

MIT licensed by Microsoft CSE team.

Native Plugins

Webrtc is licensed pretty permissively, and the example codebase on which this work is built is explicitly licensed the same way. Hopefully this makes it relatively straightforward for us to depend on the native code that makes this possible.

bengreenier avatar Mar 09 '19 17:03 bengreenier

This sounds like a great initiative! Do we know whether MS has any plans in this area, seeing that they are already quite active developing the underlying WebRTC for UWP repository? Maybe it’s possible to coordinate our efforts? Either way, I’d be happy to contribute if I can, I’m currently building a similar library that works with HoloLens + iOS, however it is currently built to work with HTK, so some work would be needed there as well.

polyramus avatar Mar 09 '19 19:03 polyramus

Hi @anders-lundgren - I actually work at Microsoft 😸 @jamescadd (who is driving the webrtc-uwp work) is super looped in on this work, as well as the MRTK Microsoft folks. As I mentioned we think this is likely to be one of the building blocks of a larger webrtc solution for MRTK folks (though we are not fully committed to this approach at this point, to be transparent).

Happy to collaborate going forward, though my immediate goal is to submit a PR into a feature branch for the existing work I've done.

bengreenier avatar Mar 11 '19 15:03 bengreenier

@anders-lundgren another benefit to this ending up as a MRTK extension is that it doesn't necessarily have to take dependencies on the core/foundational MRTK components. We should be able to deliver a WebRTC component that can be packaged/distributed independent of the rest of the MRTK, which hopefully will be compatible with your HTK project as well. :)

chrisfromwork avatar Mar 11 '19 15:03 chrisfromwork

Assigning to Jerome, he will be driving webrtc work.

chrisfromwork avatar May 31 '19 12:05 chrisfromwork

@anders-lundgren just to update you quickly I am currently working on extending the work @bengreenier did beyond its original scope, to provide a more polished library which comes with added features (ARM support being a notable one), which is why Ben's PR #3664 was closed in the meantime. This was a coordinated decision with him. I am also in contact with @jamescadd to leverage the work done by the WebRTC UWP team. Please bear with me for a little while until we finalize our plans to make our repository public. This will not be published inside the current MRTK-Unity repository though.

Do you have any example of interaction with the existing MRTK components you would be interested about? Or are you simply looking for an audio and video streaming solution in general, to use in an app which also makes use of MRTK? What do you mean when you say your library "works with HTK"? We do have plans to make any integration between the two as smooth as possible, though nothing concrete in practice yet. I'm open for suggestions!

jehumb-havok avatar May 31 '19 15:05 jehumb-havok

@djee-ms I've been using the code from Ben's code request on and off for a bit now.

Couple things I've had to fix/adjust:

  • Data Channels were not setup correctly. Reliable needs to be false or change data transfer mode.
  • Added hooks to register Ice state changes and signal state changes. Otherwise I am not sure how you would tell if the other user dropped the call.

Couple things I've had to work around but haven't touched:

  • Order of operations is fickle wtih Data channels. The simple_peer_connection has a data_channel property separate from the peerconnection.cc's list of data channels. Sometimes it checks the local data_channel, sometimes it checks the list and that can cause issues. Example: Peerconnection automatically creates data channels based on the RemoteDescription/offer but the simple_peer_connection does not. So you can easily get into a state where the data channel already exists and you can't add it to simple_peer_connection.
  • Setting audio mode (ismute, isRecord) twice with the same value will cause Unity to crash (at least in debug mode). There is a check to see if the sink already exists and if so boom.

If this is at all helpful I can keep adding more notes as I find them. I'm currently having issues with performance and having a delay on the video/audio on the device. I was trying to set a min/max framerate of 1 frame per second (we don't need fast video for our use case) but I haven't had much luck yet.

Our use case is the remote assist skype like use case for 1 on 1 help that we are adding natively to our app.

markgrossnickle avatar May 31 '19 18:05 markgrossnickle

@markgrossnickle thanks for the info!

  • I have rewritten some of the data channel code to get support for adding/removing multiple channels, and not open any by default, so the existing issues should be fixed. I also exposed both the ordered and reliable options, as those can be of use depending on the use case one has for data channels. Also Ben's code uses SimplePeerConnection which has RTP data channels but support for this is being deprecated and removed by Google as I understand. The alternative, SCTP, allows both reliable and unreliable data channels. And I enabled DTLS/SRTP too.

  • I added some hook on signaling change to get notified when the connection has been established, so that e.g. the local media pipeline can start to render the video/audio feeds. I use the transition to stable for that. My interpretation is that you should avoid using the ICE/DTLS state, which tells you if internally a low-level connection was established at the network level, but not if the WebRTC connection itself has completed its establishing steps.

  • I didn't look at connection dropped yet, thanks for the reminder. I think that you should probably also use the closed signaling state for that. My intuition is that again the ICE/DTLS state is for internal network level connection (socket connected), but not the WebRTC peer connection itself. Though in the case of dropping it's probably almost the same.

  • Incidentally, note that currently the code in Ben's plugin and WebRTC UWP SDK is mixing the ICE and DTLS states, as explained in this bug. This has been fixed here, and now those are 2 separate states, but the change didn't reach WebRTC UWP SDK yet.

jehumb-havok avatar Jun 04 '19 09:06 jehumb-havok

@djee-ms This is fantastic. Any chance I can check out the source code and help bang on it?

markgrossnickle avatar Jun 04 '19 14:06 markgrossnickle

@markgrossnickle I wish, but unfortunately not yet. At the moment the repository I am working on is private. I am working to get it public as soon as possible, even if the code is not ready for production, at least to get some feedback. But the administrative part takes some time so please bear with me. I'll post here as soon as the source code is publicly available.

jehumb-havok avatar Jun 04 '19 14:06 jehumb-havok

@djee-ms
Understood. Looking forward to when you can share.

Two last questions and I'll let you be ;)

  1. Performance isn't great on the Hololens, at least on the branch I am working on. Would it be possible to expose the frame rate of the video capture and/or playback? My current use case just needs to show a frame about every second or so as not much will be changing. I tried setting kMinFrameRate and kMaxFrameRate to both be 1 and pass those constraints to CreateVideoSource in the AddStream function of simple_peer_connection but it doesn't seem to do anything so perhaps I am in the wrong spot. I'm faily new to WebRTC.

  2. Large file sharing. Perhaps this is tackled with changing to SCTP, but with the current setup the file size limit on the data channel seems low and I am unable to share a photo. I was debating fragmenting the photo and then putting it back together or looking for another way to share it such as uploading to our server and then passing the URL. Do you have any plans for larger data sharing? I know the sharing demos seem to share anchors and other positioning data. Are anchors and everything else being demo'd pretty small or do you guys have a work around for larger files in the works?

markgrossnickle avatar Jun 04 '19 17:06 markgrossnickle

@markgrossnickle You can use FramesPerSecondChanged event on Conductor object. Event handler could be added in ControlScript.cs script. Please check how it is handled in PeerCC XAML application. It could be used as a reference. https://github.com/webrtc-uwp/PeerCC/blob/master/Client/ViewModels/MainViewModel.cs#L296

morosev avatar Jun 06 '19 18:06 morosev

@morosev We don't have a conductor in the Unity build (at least the example I have access to). But thanks for the link, this should point me in the right direction for implementing it in the Unity build!

markgrossnickle avatar Jun 06 '19 18:06 markgrossnickle

@markgrossnickle You can find the framerate counter implementation in WebRTC UWP wrapper here: https://github.com/webrtc-uwp/webrtc-apis/blob/master/windows/wrapper/impl_webrtc_MediaStreamSource.cpp#L510

morosev avatar Jun 06 '19 21:06 morosev

Right now I am evaluating the current capabilities of webRTC and would like to ask a few things about this PR:

  • does it have any reference to the Microsoft webRTC universal samples project?
  • does it have people actively working on it, and preferably a roadmap of upcoming supported features?
  • What's the current range of features? (video-, audio-streaming,... Data distribution (avatar movement))...

can someone elaborate?

Alexees avatar Jun 11 '19 10:06 Alexees

Hi @Alexees,

To answer you questions:

  • Not that I know of. However those samples are authored by the same team which works on the WebRTC UWP SDK project, and the new library I am working on is taking a dependency on that project to provide support on UWP platforms like HoloLens 2.
  • Yes, I took over this work from @bengreenier and I am working on that project full time. I am currently working on a separate repository, and I intend to make it public as soon as possible, hopefully in the next few weeks, as I finalize some administration around that task. This will be an experimental preview version, so the API may still change, but I believe can be of help for people doing similar things to take example from. I do not have a roadmap yet, though I intend to get one eventually based on feedback.
  • Initial features will support audio and video streaming to a single remote peer, with hopefully Mixed Reality Capture working for HoloLens devices. I also added ARM (32-bit) support. The libraries will not be tied to Unity, so can be used in any C++ or C# application. Some Unity integration will be provided though. Data distribution as you call it will be supported in the sense that you can open one or more data channels per peer connection, but automatic state synchronization is out of scope of this project.

I am welcoming any suggestions about features you are interested in which are specific or closely related to WebRTC, like multi-peer support (MCU, SFU), access to specific parts of the underlying WebRTC API that have not been mentioned yet on this discussion thread, etc. However any technology built on top of WebRTC data channels is unfortunately out of scope for this project.

jehumb-havok avatar Jun 11 '19 15:06 jehumb-havok

@djee-ms thanks for the thorough answer

Alexees avatar Jun 12 '19 06:06 Alexees

This is in the works, please join the sharing weekly shiproom for the latest updates.

Yoyozilla avatar Jul 24 '19 04:07 Yoyozilla

New repo coming

keveleigh avatar Jul 25 '19 19:07 keveleigh

The link seems to be broken

Alexees avatar Jul 25 '19 19:07 Alexees

@Alexees Ah sorry, looks like I jumped the gun. I'll update here when the link works.

keveleigh avatar Jul 25 '19 20:07 keveleigh