The Quick Start Console is the smallest WebRTC 2.0 program: one copy sends a media file into a channel, another copy (or a web browser) receives it. This article shows how to run it and explains the calls that do the work.
The signaling server only introduces the peers to each other. The video and audio go directly between them, or through a TURN server when the network does not allow a direct connection.
Four terms
| Term | What it is |
|---|---|
| Channel | A meeting point identified by a URL, https://<server>/<room>/<channel>. Peers that use the same URL are connected to each other. |
| Host and Guest | The peer that creates a channel is its Host; the peers that connect to it are Guests. The Host sends to all Guests, a Guest sends to the Host. Here the sender is the Host and the receiver is a Guest. |
| Peer ID | The signaling server gives every peer an ID. A Guest receives from the Host without naming it; a Host that receives from a Guest names the Guest by its ID. |
| Frame | The unit that goes through a channel: one video picture with its audio. It is an ordinary SDK frame (MFFrame, in MPlatform MFrame), so everything the SDK can do with a frame works with it. |
Before you start
- The MFormats SDK or the MPlatform SDK is installed and licensed.
- A media file with video and audio, for example an MP4 file.
- Network access to the signaling server. The sample offers the Medialooks server
https://meet.medialooks.com:443;https://videosdk.medialooks.com:8080works too. Both are for tests, see Signaling server WebRTC 2.0 usage for your own server.
The sample comes with both SDKs in C#, VB.NET, C++ and Delphi, in the SDK folder Samples WebRTC\<language>\WebRTC 2.0\Quick Start Console. Build the project for x64.
1. Start the sender
Run the program without arguments and choose 1 Send a media file. It suggests a channel URL with a random name, asks for the file (you can paste the path or drag the file onto the console) and asks whether to open a browser viewer. The same can be done with arguments (C# and VB.NET: "Quick Start Console.exe", C++ and Delphi: QuickStartConsole.exe):
QuickStartConsole send https://videosdk.medialooks.com:8080/QuickStart/MyChannel C:\Media\clip.mp4
Choose a channel name that is hard to guess: everybody who knows the URL can join. The sender prints something like this:
Channel is created: https://videosdk.medialooks.com:8080/QuickStart/MyChannel
Sending C:\Media\clip.mp4. Press Ctrl+C to stop.
Watch in a browser: https://videosdk.medialooks.com:8080/Oju5gSKPQ1wb8e
Join from a browser: https://videosdk.medialooks.com:8080/wg2/Oju5gSKPQ1wb8e
sent 25 frames, connected peers: 0
sent 25 frames, connected peers: 1
-
Watch in a browseropens a page that only shows the video;Join from a browseralso sends the camera and the microphone of the visitor. -
sent N framesis the number of frames sent in the last second;connected peersis the number of receivers connected now.
2. Start the receiver
On the same or another computer choose 2 Receive a stream and paste the channel URL (not the browser link), or run:
QuickStartConsole receive https://videosdk.medialooks.com:8080/QuickStart/MyChannel 30
The last argument is the duration in seconds; 0 or no value means until Ctrl+C.
Connected to https://videosdk.medialooks.com:8080/QuickStart/MyChannel
Waiting for the first frame...
received 25 frames in the last second, 720x576, audio 48000 Hz
Connected means that the receiver found the channel. The first frames can arrive a few seconds later, when the two peers have set up a connection.
The sender code
The code below is from the C# sample of the MFormats SDK.
Create the channel. The application that creates a channel is its Host:
var connect = new MConnectClass();
connect.Channel_Create(url, "", "", null, out MCChannel channel);
| Parameter | Meaning |
|---|---|
| URL | The channel URL, https://<server>/<room>/<channel>. |
| Properties | Properties of this peer, for example id=MyHost for a constant ID (the web links then stay the same) or secured='true' to ask web guests for a login and a password. |
| Hints | Reserved for additional options; leave it empty. |
| Callback | An object that receives the events of the channel; not needed here. |
| Channel | The new channel. ((IMCChannelSGN)channel).VTSG_InfoGet(...) returns the Host ID that is used in the browser links. To set or read its properties, cast it to MCChannelClass. |
Get a frame and send it:
reader.SourceFrameGet(100 * 10000, out MFFrame frame, ""); // MFReader: wait up to 100 ms
channel.ChannelSend(-1, frame, ""); // -1 is the default stream
Marshal.ReleaseComObject(frame); // release every frame
A timeout of SourceFrameGet (error 0x800705B4) is not the end of the file; the sample tries again. MFReader returns frames as fast as it can, so the sample waits for the duration of each frame (MFTimeGet) and opens the file again after the frame with the eMFF_Last flag. The MPlatform version uses MFile with the loop property, which delivers the frames at the speed of the file.
The frame can come from any SDK source: a file, a camera, a capture card or a mixer.
Check the state and close:
channel.ChannelInfoGet(out CHANNEL_INFO info);
Console.WriteLine("connected peers: {0}", info.nChannelsConnected);
channel.ChannelClose("");
ChannelInfoGet returns the signaling state (eSGNState) and the number of connected peers. The traffic counters of this structure are not filled; read the traffic from the stat:: properties, see the FAQ.
The receiver code
Connect to the channel. The call fails while the channel does not exist, so the sample repeats it every 2 seconds for up to 30 seconds:
var connect = new MConnectClass();
connect.Channel_Connect(url, "", "", null, out MCChannel channel);
Receive frames:
object received = null;
try
{
// timeout in ms, stream (-1 = default), peer ID ("" = the Host)
channel.ChannelReceive(1000, -1, "", out received, "");
}
catch (Exception)
{
Thread.Sleep(100); // no frame yet
}
if (received != null)
{
var frame = (MFFrame)received;
frame.MFAllGet(out MF_FRAME_INFO info);
Console.WriteLine("{0}x{1}, audio {2} Hz", info.avProps.vidProps.nWidth,
Math.Abs(info.avProps.vidProps.nHeight), info.avProps.audProps.nSamplesPerSec);
Marshal.ReleaseComObject(received);
}
- A Guest receives from the Host and passes an empty peer ID. A Host passes the ID of a Guest, from the property
connected[i]::remote::idof the channel. - No frame within the timeout gives the error
0x800705B4. While the connection between the peers is not complete, the call fails at once. Wait a little before the next call, otherwise the loop uses a whole processor core. - Catch
Exception, not onlyCOMException: .NET can turn some of these errors into other exception types.
Leave the channel:
channel.ChannelDisconnect("", "");
channel.ChannelClose("");
ChannelDisconnect with an empty ID leaves the Host. A Host can pass the ID of a Guest to disconnect that Guest.
The MPlatform versions
The channel calls are the same. The sender plays the file with MFile instead of reading it with MFReader, and the frames are MFrame objects:
var file = new MFileClass();
file.FileNameSet(path, "");
file.PropsSet("loop", "true");
file.FilePlayStart();
// the sender loop
file.SourceFrameGet(100 * 10000, out MFrame frame, 0);
channel.ChannelSend(-1, frame, "");
Marshal.ReleaseComObject(frame);
// when finished
file.FilePlayStop(0);
file.ObjectClose();
// the receiver reads the format of a received frame with
((MFrame)received).FrameAVPropsGet(out M_AV_PROPS props);
What happens when you run it
-
Channel_Createconnects to the signaling server and registers the channel under the URL. The server gives the Host its ID, which the web links contain. -
Channel_Connectasks the signaling server for the channel, and the two peers exchange their connection data through the server. - The peers test the possible network routes, which takes the seconds before the first frame, and connect directly, or through a TURN server when a firewall or a NAT blocks the direct route.
- From then on the frames go from
ChannelSendof one peer toChannelReceiveof the other. The signaling server does not carry the media.
The same calls in C++ and Delphi
| Action | C++ | Delphi |
|---|---|---|
| Create the object | connect.CoCreateInstance(__uuidof(MConnect)) |
Connect := CoMConnect.Create |
| Create a channel | connect->Channel_Create(url, CComBSTR(L""), CComBSTR(L""), NULL, &channel) |
Connect.Channel_Create(Url, '', '', nil, Channel) |
| Join a channel | connect->Channel_Connect(url, CComBSTR(L""), CComBSTR(L""), NULL, &channel) |
Connect.Channel_Connect(Url, '', '', nil, Channel) |
| Send | channel->ChannelSend(-1, frame, CComBSTR(L"")) |
Channel.ChannelSend(-1, Frame, '') |
| Receive |
channel->ChannelReceive(1000, -1, CComBSTR(L""), &received, CComBSTR(L"")), then CComQIPtr<IMFFrame> frame(received)
|
Channel.ChannelReceive(1000, -1, '', Received, ''), then Received.QueryInterface(IMFFrame, Frame)
|
| Release a frame | automatic (CComPtr) |
Frame := nil |
| Errors | The calls return an HRESULT
|
The calls return an HResult; OleCheck turns it into an exception |
MFormats and MPlatform applications use the same MConnect objects, so a sender built with one SDK and language can be used with a receiver built with another one.
Change it for your application
| You want to | What to do |
|---|---|
| Send a camera or a capture card | Take the frames from MFLive (MPlatform: MLive) and pass them to ChannelSend. |
| Change the codec and the bitrate |
((MCChannelClass)channel).PropsSet("video_encoder", "gpu_h264"), ((MCChannelClass)channel).PropsSet("video_bitrate", "5M"), see MConnect properties. |
| Show a window with previews and a list of guests | WebRTC 2.0 Duplex Sample: step by step |
| Mix several guests into one program | WebRTC Meeting Sample |
| Remove the echo in a conference | WebRTC Mix-Minus |
| Receive in a browser or on a phone | Open the Watch link, or the Join link for a visitor with a camera. See Web pages configuration parameters. |
| Have several guests | Connect more receivers. To receive from them, the Host calls ChannelReceive with the ID of each Guest (connected[i]::remote::id), or mixes them as the Meeting Sample does. |
| Show the state of the connection | Read ChannelInfoGet and the stat:: properties of the channel. |
| Protect the channel | Create it with secured='true' and check web guests in the remote_offer event, see the FAQ. |
| Move an existing WebRTC 1.0 application | WebRTC 1.0 and WebRTC 2.0: the difference and how to switch |
If something does not work
- Start the sender first: the receiver can connect only to a channel that exists.
- Wait a few seconds for the first frame.
- A firewall can block the connection to the signaling server (TCP port of its URL) or the connection between the peers.
- Release every frame; frames that are kept use up the memory.
The errors and their codes are listed in the WebRTC 2.0 FAQ and troubleshooting.
Next
- WebRTC 2.0 Duplex Sample: step by step: an application with a window, previews, codec and bitrate, the links of the Host and the list of peers.
- WebRTC Meeting Sample: a meeting with many guests and a program output.
- WebRTC 2.0 Overview
Still have questions?
Tell us what didn’t work or what you’d like clarified.