Live stream over WebRTC

WebRTC, a standardized API that simplifies video streaming, is supported by the VideoCoin network. Below are the steps to create a RTC connection and send it to the VideoCoin Network for processing.

List available streaming profiles

Below is the endpoint to retrieve all existing output profiles. Determine which profile will best suit your desired output stream and note the id property:

$ curl -X GET https://console.videocoin.network/api/v1/profiles
{
"items": [
{
"id": "190b9d72-208d-4fa2-90b7-5b203c0025d2",
"name": "SD",
"description": "SD",
"is_enabled": true
},
{
"id": "45d5ef05-efef-4606-6fa3-48f42d3f0b96",
"name": "FullHD",
"description": "FullHD",
"is_enabled": true
},
{
"id": "e53e377f-2621-4ef1-8004-7d782e9608eb",
"name": "HD",
"description": "HD",
"is_enabled": true
}
]
}

Create the stream object

Create the stream and include the required name, input_type, output_type and profile_id values. profile_id is the value of the desired profile in the previous step. Note that this request requires authorization in the form of a Bearer token. See "API Authorization" for more:

$ curl -X POST -H 'Authorization: Bearer <TOKEN>' -H 'Content-Type: application/json' -d '{"name": "live", "profile_id": "190b9d72-208d-4fa2-90b7-5b203c0025d2", "input_type": "INPUT_TYPE_WEBRTC", "output_type": "OUTPUT_TYPE_HLS"}' https://console.videocoin.network/api/v1/streams
{
"id": "3f18785d-b757-4f18-77e2-36680043fff6",
"name": "live",
"output_url": "https://streams.videocoin.network/3f18785d-b757-4f18-77e2-36680043fff6/index.m3u8",
"stream_contract_id": "1959998627787229139",
"stream_contract_address": "",
"status": "STREAM_STATUS_NEW",
"input_status": "INPUT_STATUS_NONE",
"created_at": "2019-11-06T22:54:06.866402573Z",
"updated_at": "2019-11-06T22:54:06.866452401Z",
"ready_at": null,
"completed_at": null,
"rtmp_url": "rtmp://rtmp.console.videocoin.network:1935/live/3f18785d-b757-4f18-77e2-36680043fff6",
"input_type": "INPUT_TYPE_WEBRTC",
"output_type": "OUTPUT_TYPE_HLS"
}

Start preparation of the stream

Using the id value of the previous operation, begin running the stream to prepare its input and output destinations:

$ curl -X POST -H 'Authorization: Bearer <TOKEN>' https://console.videocoin.network/api/v1/streams/3f18785d-b757-4f18-77e2-36680043fff6/run

Sending video over WebRTC to the stream's ingest

The stream is not yet ready to receive video until its status property is equal to STREAM_STATUS_PREPARED. For more about the various values of a stream's status property, see the "List of stream statuses (status)" section in the "Streams" page:

$ curl -X GET -H 'Authorization: Bearer <TOKEN>' https://console.videocoin.network/api/v1/streams/3f18785d-b757-4f18-77e2-36680043fff6
{
"id": "3f18785d-b757-4f18-77e2-36680043fff6",
"name": "live",
"output_url": "https://streams.videocoin.network/3f18785d-b757-4f18-77e2-36680043fff6/index.m3u8",
"stream_contract_id": "1959998627787229139",
"stream_contract_address": "0x548CEb2CD19Df313e2aA65c9301CBb9d264dBB8F",
"status": "STREAM_STATUS_PREPARED",
"input_status": "INPUT_STATUS_NONE",
"created_at": "2019-11-06T22:54:07Z",
"updated_at": "2019-11-06T22:54:15Z",
"ready_at": null,
"completed_at": null,
"rtmp_url": "rtmp://rtmp.console.videocoin.network:1935/live/3f18785d-b757-4f18-77e2-36680043fff6",
"input_type": "INPUT_TYPE_WEBRTC",
"output_type": "OUTPUT_TYPE_HLS"
}

The stream is now ready receive video. In your web application, make the following JavaScript calls to interact with the browser and VideoCoin's media server API:

  1. Create a new RTCPeerConnection instance:

    const pc = new RTCPeerConnection();
  2. Use the browser MediaDevices API to get media tracks:

    const mediaStream = navigator.mediaDevices.getUserMedia();
  3. Add audio/video tracks to the peer connection:

    mediaStream.getTracks().forEach(track => { pc.addTrack(track); });
  4. Create an SDP offer, then pass that offer object to the peer connection's setLocalDescription method:

    const offer = await pc.createOffer() pc.setLocalDescription(offer);
  5. Send a POST request to VideoCoin's Media Server with the stream id and the offer object's SDP value:

    $ curl -X POST -H 'Authorization: Bearer <TOKEN>' -H 'Content-Type: application/json' -d '{"stream_id": stream.id, "sdp": offer.sdp}' https://console.videocoin.network/api/v1/ms/streams/webrtc
  6. From the POST response get the sdp field value and pass an answer object to the setRemoteDescription method of the peer connection:

    const answer = new RTCSessionDescription({ type: 'answer', sdp: response.sdp });
    pc.setRemoteDescription(answer);

Using HLS output of stream

The HLS output is not ready for use until the stream's status property is equal to STREAM_STATUS_READY:

$ curl -X GET -H 'Authorization: Bearer <TOKEN>' https://console.videocoin.network/api/v1/streams/3f18785d-b757-4f18-77e2-36680043fff6
{
"id": "3f18785d-b757-4f18-77e2-36680043fff6",
"name": "live",
"output_url": "https://streams.videocoin.network/3f18785d-b757-4f18-77e2-36680043fff6/index.m3u8",
"stream_contract_id": "1959998627787229139",
"stream_contract_address": "0x548CEb2CD19Df313e2aA65c9301CBb9d264dBB8F",
"status": "STREAM_STATUS_READY",
"input_status": "INPUT_STATUS_ACTIVE",
"created_at": "2019-11-06T22:54:07Z",
"updated_at": "2019-11-06T22:54:15Z",
"ready_at": "2019-11-06T22:58:15Z",
"completed_at": null,
"rtmp_url": "rtmp://rtmp.console.videocoin.network:1935/live/3f18785d-b757-4f18-77e2-36680043fff6",
"input_type": "INPUT_TYPE_WEBRTC",
"output_type": "OUTPUT_TYPE_HLS"
}

Once the stream is in STREAM_STATUS_READY state, use the output_url property with any HLS-compatible video player to view your stream's output:

$ ffplay https://streams.videocoin.network/3f18785d-b757-4f18-77e2-36680043fff6/index.m3u8

Ending your stream

The stream will complete automatically if the encoder it is receiving data from has stopped. The stream can also be stopped manually as shown below:

$ curl -X POST -H 'Authorization: Bearer <TOKEN>' https://console.videocoin.network/ap