Transmitting WebRTC streams and messages between native applications

This post explains how to transmit video and messages between native applications via WebRTC. For basics and signaling see the post on remote control for a playlist application.



Sending streams and messages

In order to send a stream the following needs to be done:

  • Create a MWebRTC class instance.
  • Logit to signaling server and join room (receivers will join the same room). 
  • Subscribe for it's events (we will need it to receive messages).
  • Add it as a plugin to the stream source object in case of MPlatform SDK usage.
  • Or start feeding frames to MWebrtc object in case of MFormats SDK usage.

The frames will be transmitted by MWebRTC class object automatically. 

// WebRTC class is used to send audio/video to a remote peer.
MPLATFORMLib.MWebRTCClass m_objWebRTC = new MWebRTCClass();

// Login to signaling server. We will use default Medialooks one and room named "room555"
m_objWebRTC.Login("https://medialooks.com:8889/room555", "", out myId);
 
// We need to handle WebRTC object events to be able to get messages.
m_objWebRTC.OnEvent += new IMEventsEvent_OnEventSafeEventHandler(m_objWebRTC_OnEvent); 
MPlatform SDK: 
// Add WebRTC plugin to start network streaming. Web clients can connect/disconnect at any time. 
m_objPlaylist.PluginsAdd(m_objWebRTC, 0);
MFormats SDK; 
// Feed frames to WebRTC object. 
m_objWRTC.ReceiverFramePut(pFrame, 0, "");

To send a message you will need to call the m_objWebRTC.SendMessageToPeer method:


// Send a message to a remote peer.
m_objWebRTC.SendMessageToPeer("destination_peer_id", "my message");

// Send a message to all connected peers.
m_objWebRTC.SendMessageToPeer("", "my message");

Receiving streams and messages

In order to receive a stream the following needs to be done:


  • Create a MWebRTC class instance.
  • Login to signaling server and join the room with the desired peer-sender.  
  • Subscribe for it's events (we will need it to receive messages).

  • Connect to the peer you want to receive the stream from.
  • Start a new thread that will take frames from the MWebRTC object and send them to the receiver object (like MProxy, MPreview, MWriter or MRenderer).

// WebRTC class is used to send audio/video to a remote peer.
MWebRTCClass m_objWebRTC = new MWebRTCClass(); 
 
// Login to signaling server. We will use default Medialooks one and room named "room555"
m_objWebRTC.Login("https://Medialooks.com:8889/room555", "Receiver", out myId);
 
// We need to handle WebRTC object events to be able to get messages.
m_objWebRTC.OnEvent += new IMEventsEvent_OnEventSafeEventHandler(m_objWebRTC_OnEvent); 
 
// Connect to the remote peer by its id or name (note that id may change
//in case if sender reconnects to signalling)
m_objWebRTC.ConnectToPeer("Sender"); 
 
// Create and start the thread that will receive frames from MWebRTC object.
isThread = true; 
m_objWorkerThread = new Thread(FrameProcessing); 
m_objWorkerThread.Start(); 
 
// Example of thread that receives frames and sends them to the preview.
private void FrameProcessing() 
{ 
    while (isThread) 
    { 
        try 
        { 
            MFrame sourceFrame; 
            // Get frame from source object.
            m_objWebRTC.SourceFrameGetEx(ref cookies, -1, out sourceFrame, 0); 
 
            try 
            { 
                // Send result frame to preview.
                m_objPreview.ReceiverPutFrame("", sourceFrame); 
            } 
            catch { } 
 
            // Release frame.
            Marshal.ReleaseComObject(sourceFrame); 
        } 
        catch (Exception) 
        { 
            cookies = 0; 
            continue; 
        } 
    } 
}

To receive messages you will need to handle MWebRTC object events:

void m_objWebRTC_OnEvent(string bsChannelID, string bsEventName, string bsEventParam, object pEventObject) 
{ 
    // Check that there is a message to receive.
    if (bsEventParam != null && bsEventParam.Length > 0) 
    { 
         // Log the message.
		textBoxMessages.AppendText("\r" + bsEventParam); 
    } 
}

Try this feature with our sample applications
  To evaluate this feature check the App to App Streamer and App to App Receiver sample applications.  
- MPlatform SDK: see the "...\Samples Basic\C#\WebRTC" folder.
- MFormats SDK: see the "...\Samples\C#\WebRTC" folder. 



See also