The Agora SDK provides the functionality to convert the audio and video streams of hosts from Agora private protocols to standard protocols (RTMP and RTMPS) and then push those streams to CDN. The CDN audience can watch live streams by clicking the corresponding URLs. This function can enrich the distribution channels of live media streams and facilitate the promotion of live streaming.
Ensure that you enable the Media Push service before using this function:
Log in to Agora Console, and click the Project Management icon on the left navigation panel.
On the Project Management page, find the project for which you want to enable the Media Push service, and click the edit icon.
Under Real-time engagement extensions, find Media Push, and click Enable.
Click the Status button to enable the Media Push Client-side SDK API service. Read the pop-up prompt carefully, and then click Save.
To reduce the difficulty of integrating Media Push, as of v3.6.0, Agora optimizes the API design of Media Push and improves the handling of network issues within Media Push clients and servers, as detailed in the release notes. This section describes how to use the SDK v3.6.0 or later to implement Media Push. If you need to implement Media Push using versions of the SDK earlier than v3.6.0, see how to implement an earlier version.
You can implement different types of Media Push depending on the live streaming scenario requires transcoded or non-transcoded streaming. Transcoding is essentially an encoding and decoding function used to mix multiple audio and video streams into one audio and video stream, which can guarantee the synchronization of multiple hosts' live streams seen by the audience. Therefore, generally speaking, in the case of multiple-hosts live streaming, you need to enable transcoding when pushing streams to CDN; in the case of single-host live streaming, only one media stream is pushed to the CDN, so you do not need to enable transcoding.
You can refer to the following API call sequence diagram and steps to implement streaming with transcoding:
Refer to Start Interactive Live Video Streaming or Start Interactive Live Audio Streaming to implement the basic audio and video interaction.
Call startRtmpStreamWithTranscoding
and pass in the address of the Media Push and the transcoding configuration to start pushing transcoded streams to a CDN address.
userCount
and transcodingUsers
fields in the transcoding configuration are required. You can customize or use default values for the remaining fields in the configuration.start
multiple times.start
, make sure to wait until you receive the onRtmpStreamingStateChanged
callback reporting the streaming state as RUNNING(2)
before performing any other actions. If you receive another streaming state, see Exception Handling for Media Push.You can call updateRtmpTranscoding
to dynamically update the transcoding configuration according to a scenario change, such as adjusting the number of hosts or the video layout of the hosts. After the transcoding configuration is updated successfully, the SDK triggers the onTranscodingUpdated
callback.
Call stopRtmpStream
and pass in the CDN address to stop the current live streaming.
stop
multiple times and pass in the corresponding CDN addresses.onRtmpStreamingStateChanged
callback reporting the streaming state as IDLE(0)
. If you receive another streaming state, see Exception Handling for Media Push.You can refer to the following API call sequence diagram and steps to implement streaming without transcoding:
Refer to Start Interactive Live Video Streaming or Start Interactive Live Audio Streaming to implement the basic audio and video interaction. The uid
parameter in the joinChannel
method cannot be empty or 0; otherwise, the hosts in the channel cannot push any media streams to CDN.
Call startRtmpStreamWithoutTranscoding
and pass in the address of the Media Push to start pushing non-transcoded streams to a CDN address.
start
multiple times.start
, make sure to wait until you receive the onRtmpStreamingStateChanged
callback reporting the streaming state as RUNNING(2)
before performing any other actions. If you receive another streaming state, see Exception Handling for Media Push.stopRtmpStream
and pass in the CDN address to stop the current live streaming.stop
multiple times and pass in the corresponding CDN addresses.onRtmpStreamingStateChanged
callback reporting the streaming state as IDLE(0)
. If you receive another streaming state, see Exception Handling for Media Push.To show the code logic for starting the streaming, dynamically updating the transcoding configuration, and retrying the stream push in case of an exception, Agora provides an open-source sample project on GitHub, see RTMPStreaming.
This section provides references commonly used when using Media Push function.
Agora recommends that you use the default values when setting the video resolution, frame rate, and bitrate of the output transcoded stream. You can also refer to the following table to set the values. If you set a bitrate beyond a reasonable range, the Agora server automatically adjusts the bitrate to stay within a reasonable range.
Resolution | Frame rate (fps) | Bitrate (Kbps) |
---|---|---|
160 × 120 | 15 | 130 |
120 × 120 | 15 | 100 |
320 × 180 | 15 | 280 |
180 × 180 | 15 | 200 |
240 × 180 | 15 | 240 |
320 × 240 | 15 | 400 |
240 × 240 | 15 | 280 |
424 × 240 | 15 | 440 |
640 × 360 | 15 | 800 |
360 × 360 | 15 | 520 |
640 × 360 | 30 | 1200 |
360 × 360 | 30 | 800 |
480 × 360 | 15 | 640 |
480 × 360 | 30 | 980 |
640 × 480 | 15 | 1000 |
480 × 480 | 15 | 800 |
640 × 480 | 30 | 1500 |
480 × 480 | 30 | 1200 |
848 × 480 | 15 | 1220 |
848 × 480 | 30 | 1860 |
640 × 480 | 10 | 800 |
1280 × 720 | 15 | 2260 |
1280 × 720 | 30 | 3420 |
960 × 720 | 15 | 1820 |
960 × 720 | 30 | 2760 |
This section describes how to implement Media Push using versions of the SDK earlier than v3.6.0.
Refer to Start Interactive Live Video Streaming or Start Interactive Live Audio Streaming to implement the basic audio and video interaction.
The host in a channel calls the setLiveTranscoding
method to set the transcoding configuration of media streams (LiveTranscoding
), such as the resolution, bitrate, frame rate, and position of any watermark/background. If you need a transcoded picture, set the picture-in-picture layout for each user in the TranscodingUser
class.
addPublishStreamUrl
, and set transcodingEnabled (false)
.The host in a channel calls the addPublishStreamUrl
method to add a media stream to the CDN. The address can be dynamically added or deleted after the stream is pushed.
transcodingEnabled
to set whether transcoding is enabled or not.(Optional) The host in a channel calls the setLiveTranscoding
method again to update the transcoding configuration of media streams.
onTranscodingUpdated
callback and reports the update information to the host.The host in a channel calls the removePublishStreamUrl
method to remove a media stream from the Media Push.
When the streaming state changes, the SDK triggers the onRtmpStreamingStateChanged
callback to report the current streaming state to the host. Make sure to wait until you receive the onRtmpStreamingStateChanged
callback before performing any other actions. If adding or removing a CDN address fails, see Exception Handling for Media Push.
// Java
// CDN transcoding settings.
LiveTranscoding config = new LiveTranscoding();
config.audioSampleRate = TYPE_44100;
config.audioChannels = 2;
config.audioBitrate = 48;
// Width of the video (px). The default value is 360.
config.width = 360;
// Height of the video (px). The default value is 640.
config.height = 640;
// Video bitrate of the video (Kbps). The default value is 400.
config.videoBitrate = 400;
// Video framerate of the video (fps). The default value is 15. Agora adjusts all values over 30 to 30.
config.videoFramerate = 15;
// If userCount > 1,set the layout for each user with transcodingUser.
config.userCount = 1;
// Video codec profile. Choose to set as Baseline (66), Main (77), or High (100). If you set this parameter to other values, Agora adjusts it to the default value 100.
config.videoCodecProfile = HIGH;
// Sets the output layout for each user.
LiveTranscoding transcoding = new LiveTranscoding();
LiveTranscoding.TranscodingUser user = new LiveTranscoding.TranscodingUser();
user.uid = 123456;
transcoding.addUser(user);
user.x = 0;
user.audioChannel = 0;
user.y = 0;
user.width = 640;
user.height = 720;
// CDN transcoding settings when using transcoding.
rtcEngine.setLiveTranscoding(transcoding);
// Adds a URL to which the host pushes a stream. Set the transcodingEnabled parameter as true to enable the transcoding service. Once transcoding is enabled, you need to set the live transcoding configurations by calling the setLiveTranscoding method. Agora does not recommend transcoding in the case of a single host.
rtcEngine.addPublishStreamUrl(url, true);
// Removes a URL to which the host pushes a stream.
rtcEngine.removePublishStreamUrl(url);
The following APIs are recommended by Agora when implementing Media Push:
startRtmpStreamWithoutTranscoding
startRtmpStreamWithTranscoding
updateRtmpTranscoding
stopRtmpStream
onTranscodingUpdated
onRtmpStreamingStateChanged
onRtmpStreamingEvent
The following APIs are still functional but not recommended by Agora when implementing Media Push:
When using the Media Push function, you may also need to refer to the following docs: