📜 ⬆️ ⬇️

We pick up an audio stream from Twilio via SIP and send it to RTMP CDN for further distribution

image

As you know, in nature there are CDN networks that are used to deliver streaming video to end users. Take for example the online broadcast of a football match. Historically, Adobe developed the RTMP protocol at a certain time has become very popular for delivering live video content, and there is support for this protocol in every self-respecting Encoder device or program that captures video from the camera and sends it to the server or CDN for later distribution. This article describes how to make SIP a stream source for a CDN.

Methods of further delivery of video to end users depend on the specific CDN and technologies used, which allow you to display the stream on a variety of devices and platforms: Apple HTTP Live Streaming, RTSP, MPEG-DASH, WebRTC, Websockets, etc.

image
')
Also, historically, the SIP protocol (Session Initiation Protocol) is known mainly as a protocol for audio calls and telephony, and most likely for this reason this protocol did not fall into the area of ​​interest of CDN providers and vendors of server software for CDN networks.

image

Although many SIPs are associated only with telephony, the SIP protocol was originally planned and developed as a universal connection setup protocol for the subsequent transfer of arbitrary data: audio, video, files, messages, etc. To date, the protocol is completely obsolete and a large number of software and Internet services support the SIP protocol in one form or another. This is mainly software and equipment associated with video conferencing, video chats, communications.

As a result, at this technological interface: SIP and CDN appeared an integration task of using SIP devices or a telecommunication service as a source of a video stream for a CDN network with the subsequent distribution of this video stream to end users.

image

The simplest example is a videoconference or a webinar of educational or public topics, deployed on the basis of software with SIP support and requiring broadcast to the CDN network for a broad presentation of the event.

image

In this article, for simplicity, we restrict ourselves to capturing an audio stream from a SIP conference and further broadcasting this stream via RTMP. Strictly speaking, such an action is rather a SIP call with further conversion and redirection of traffic rather than video capture.

image

In the capture and broadcast diagram, you can see the “command" arrow. In fact, if we have three participants: a SIP device, an integrating server and an RTMP server, then there must be an interface or API through which you can give a command to start the capture of a SIP stream and further translation, i.e., launch the case itself.

Another requirement is that calls and broadcasts should be able to be installed in parallel and a lot. Those. You can install SIP Call1 - RTMP Stream1, SIP Call2 - RTMP Stream2 pairs, etc. For example in the case when there are several webinars (rooms) that go at the same time.

image

As a server / service SIP conferences, we will use the service Twilio.
As an RTMP server, on the basis of which a CDN can be built, we will use the Wowza Streaming Engine.
The integrating server will be Web Call Server 4 with support for converting and translating SIP to RTMP.
The command to the server for a call to SIP and stream redirection to RTMP will be sent via REST / HTTP - perhaps the most widely used HTTP-based software interaction method on the Internet.

This article describes how to organize the broadcast of audio messages or audio calls using a simple REST / HTTP request.

This will require:


Twilio


Twilio is a cloud-based telephony platform that provides an API that allows you to integrate telephony services into mobile and web applications.
To use Twilio you need to register. After registration, you can select a phone number that can be assigned an application URL with Twilio instructions. An incoming call to this number will be processed in accordance with these instructions. For example, an audio message may be played or a connection to a telephone conference may be established.

In addition to the call to the phone number, you can establish a SIP connection with Twilio. To do this, you need to create and configure a Twilio SIP domain , which can also be assigned the “Voice Url” application with instructions for Twilio.

To create a Twilio SIP domain, you need to add a SIP Endpoint:

image

This is the list of SIP Endpoints:

image

You can call Twilio's SIP domain from a SIP device, for example, from a softphone. This will require:

1. Add an external IP device to the IP Access Control List:

image

* Instead of '192.168.1.5', the external IP of the device should be specified.

2. Add a name and password to the Credential List:

image

3. Create a registration-disabled softphone account, specifying both the Twilio SIP-domain domain and the name and password added to the Twilio SIP-domain account list as the user name and password. A similar X-Lite softphone account will look like this:

image

4. To initiate a call to Twilio SIP-domain (for example, if the domain name is mytwiliodomain.sip.twilio.com, you need to call mytwiliodomain.sip.twilio.com):

image

So we called Twilio from a SIP softphone and actually tested the SIP part, taking and losing the audio stream that participates in the call. Now it remains to make the same call from the WCS4 server and redirect the received audio traffic as RTMP.

image

Web Call Server 4


WCS4 will require a server with Linux 64-bit and java installed. You need the console command to display information about the Java machine:

java –version 

If java is not installed, you can install it with the command:

 yum install java 

WCS4 server can be downloaded here , and request a trial license here .
The only change that will need to be made in the server settings after its installation is an abbreviation of the codec list that is required for a SIP connection to the Twilio SIP domain with audio only.

Configuring codecs in the flashphoner.properties configuration file (in the / usr / local / FlashphonerWebCallServer / conf directory) should include only G.711 uLaw:

 codecs = ulaw 

The file can be edited, for example, using Midnight Commander (mc)

image

image

Wowza


Wowza Streaming Engine, available for download from wowza.com . It will require a license; You can request a free developer license . In the server control panel, you must enable publishing RTMP streams in the 'Applications' | 'live' | 'Incoming Security'.

image

Forming a REST / HTTP request to start a call


After completing the WCS, Twilio SIP domain and Wowza settings, you can proceed to establish a SIP connection between the WCS server and the Twilio SIP domain. In this case, to initiate a SIP call, you need to send an HTTP / REST / POST request to the WCS server. An example of a body of a similar REST request in JSON format.

 { "callId":"R2fhkll5Sw3lK", "callee":"mytwiliodomain.sip.twilio.com", "rtmpUrl":"rtmp://my_wowza_server.com:1935/live", "rtmpStream":"streamName", "hasAudio":"true", "hasVideo":"true", "connection":{ "sipLogin":"myusername", "sipPassword":"MyPasswordPassword1", "sipAuthenticationName":"myusername", "sipDomain":"mytwiliodomain.sip.twilio.com", "sipPort":"5060", "sipRegisterRequired":"false", "appKey":"callApp" } } 

The values ​​of the “connection” subobject parameters in the request are selected in the same way as in the softphone account:


Among these parameters, there is also an “appKey” parameter indicating the name of the standard application on the server side ('callApp'), which receives the status of the SIP call and the associated RTMP stream.

Other options:


“Sendrecv” and “recvonly” parameters in SIP


The hasAudio and hasVideo parameters are important. The case assumes that we do not send traffic to a SIP device, but only receive traffic from it. If hasAudio and hasVideo are set to “true”, then in SDP it will be marked “sendrecv” (send and receive) for audio and video. Some SIP devices in this case will wait for traffic and reset the SIP connection on timeout if there is no traffic. Therefore, in this case, you will have to raise the waiting time on the SIP device itself or set hasAudio and hasVideo to “false” to be sent to SDP “recvonly”. In this case, the SIP side will know that we are only going to receive the video (receive only) and the timeout should not work.

Sending a REST / HTTP request from the REST Console


Request URI for a similar REST call will be

my_wcs_server.flashphoner.com:9091/RESTCall/call


where my_wcs_server.flashphoner.com is the WCS server address.

To send a REST call, you need a REST client. You can use Chrome browser extension, for example, REST Console or Advanced REST client, or SIP as RTMP REST client for WCS server (requires installation of the Allow-Control-Allow-Origin extension: * for Google Chrome browser).

An example of a REST Console interface with fields required for a REST request:

image

If the WCS server is running, Twilio accepts SIP calls and all data is entered correctly, press the "POST" button in the REST Console and send a request to the server. As a result, a SIP connection is established between WCS and Twilio and the audio stream will be sent to the specified RTMP address.

Play stream in RTMP-player


To play the published RTMP stream, you can use the Flash RTMP player in the Wowza examples, for example: my_wowza_server.com/FlashRTMPPlayer/player.html


image

Before starting playback of a stream, the “rtmpUrl” and “rtmpStream” values ​​specified in the REST request must be entered into the “Server” and “Stream” fields.

image

When playing an RTMP stream associated with a SIP call, in the RTMP player, you can listen to the audio that the application is accessing via the Voice Url of the Twilio SIP domain.

Broadcast video, additional information


You can broadcast not only audio, as is the case with Twilio, but also video with the H.264 codec, if SIP Endpoint supports and answers video calls.

Full list of supported codecs:


The diagram below illustrates the transfer of calls and media.

1. Challenges (black arrows):


2. Media (red arrows):


image

Videos can be broadcast, for example, when a video call is established with a softphone (for example, Bria). In this case, the values ​​of the parameters of the REST call:


Conclusion


Thus, we managed to integrate the Twilio service and the RTMP server, by redirecting the SIP audio stream to RTMP for further distribution by any technologically accessible means.

Source: https://habr.com/ru/post/271233/


All Articles