Menu

RTMP Streaming with TRTC

Last updated: 2023-09-28 11:52:37Download PDF

Overview

To facilitate your integration of TRTC, we support publishing and playback over the standard protocol RTMP. You can install OBS, FFmpeg, or other RTMP libraries to publish streams with TRTC. OBS is a third-party open-source tool for live streaming. It’s easy to use and free of charge, and it supports OS X, Windows, and Linux. OBS can be used in a wide range of scenarios and is capable of meeting most live streaming needs without requiring additional plugins. You can download its latest version at the OBS website. TRTC also supports RTMP playback.
This feature was officially launched on March 7, 2023. To use it, you need to purchase a TRTC Standard or Pro package. An RTMP stream will be considered a user in a TRTC room, which will incur call duration fees. For details, see Billing Overview.

Use Cases

Scenario
Description
Online education
Use the desktop edition of OBS or FFmpeg to publish learning materials (most media formats are supported) over RTMP to a TRTC room. Students in the room can play the stream via the TRTC SDK and see the same learning materials as the teacher controls the playback progress/speed or switches between chapters. Excellent synchronization across multiple devices ensures better teaching quality.
Sports watching
Sports event organizers provide content in the form of RTMP streams. You can publish the streams to TRTC rooms so that users in the rooms can watch the event with ultra-low latency. With TRTC’s interaction capability, users can also audio/video chat with each other throughout an event.
Others
You can also use the RTMP publishing feature to implement other real-time interactive applications based on streaming.

Architecture

An RTMP client is a module of TRTC and can communicate with other TRTC clients. It can also use TRTC capabilities such as recording and relaying.


Publishing and Playback URLs

Publishing URLs

rtmp://intl-rtmp.rtc.qq.com/push/Room ID?sdkappid=Application ID&userid=User ID&usersig=Signature
For RTMP publishing, appName is push.
Replace "Room ID", "Application ID", "User ID", and "Signature" with their actual values.
For the sake of simplicity, we support only string-type room IDs. A room ID can contain numbers, letters, and underscores and cannot exceed 64 characters.
Warning:
To play an RTMP stream on other TRTC clients, when entering the room, make sure you use a string-type room ID.
For how to generate UserSig, see UserSig. Make sure your signature is within the validity period.
Example:
rtmp://intl-rtmp.rtc.qq.com/push/hello-string-room?sdkappid=140*****66&userid=******rtmp2&usersig=eJw1jdE********RBZ8qKGRj8Yp-wVbv*mGMVZqS7w-mMDQL

Playback URLs

rtmp://intl-rtmp.rtc.qq.com/pull/Room ID?sdkappid=Application ID&userid=User ID&usersig=Signature&remoteuserid=Remote user ID&sessionid=Session ID
For RTMP playback, appName is pull.
Set Remote user ID to the user to whom the RMTP stream belongs. Session ID uniquely identifies a playback request and is used for troubleshooting. You can use a UUID for the session ID. The requirements for the other fields are the same as those for publishing URLs.
Example:
rtmp://intl-rtmp.rtc.qq.com/pull/hello-string-room?sdkappid=140*****66&userid=******rtmp3&usersig=eJw1jdE***************ZLgi5UAgOziXOnEhswHu1iUy8MmMDQL&remoteuserid=strin*****IDtest&sessionid=7CF03***D7134473B3F99A3FC152EBD3

Example

You can use software or a programming library that supports RTMP to publish RTMP streams. The section below shows you how to do this.

Using OBS to publish streams

Prerequisites

You have installed OBS.

Step 1. Select a source

In the Sources panel at the bottom, click **+**, and select a source based on your needs. Common sources include the following:
Source
Note
Image
Publishes a single image
Image Slide Show
Publishes multiple images (you can determine the order of playback and whether to loop the playback)
Scene
Inserts an entire scene to enable various streaming effects
Media Source
Uploads a local file and publishes it as a live stream
Text
Adds real-time text to your stream
Window Capture
Captures and publishes the window you select in real time
Video Capture Device
Captures and publishes the images captured by a camera in real time
Audio Input Capture
Audio live streaming (audio input device)
Audio Output Capture
Audio live streaming (audio output device)


Step 2. Set publishing parameters

1. In the Controls panel at the bottom, click Settings.
img

2. Click Stream and select Custom for Service.
3. Enter rtmp://intl-rtmp.rtc.qq.com/push/ for Server.
4. Enter a stream key in the following format:
Room ID?sdkappid=Application&userid=User ID&usersig=Signature
Replace "Room ID", "Application ID", "User ID", and "Signature" with the actual values, for example:
hello-string-room?sdkappid=140*****66&userid=******rtmp2&usersig=eJw1jdE***************ZLgi5UAgOzoMhrayt*cjbmiCJ699T09juc833IMT94Ld7I0iHZqVDzvVAqkZsG-IKlzLiXOnEhswHu1iUyTc9pv*****D8MQwoA496Ke6U1ip4EAH4UMc5H9pSmv6MeTBWLamhwFnWRBZ8qKGRj8Yp-wVbv*mGMVZqS7w-mMDQL


Step 3. Configure the output

Because RTMP does not support B-frames, set the video encoding parameters as follows to remove B-frames.
1. Go to Controls > Settings > Output.
2. Select Advanced for Output Mode. The recommended Keyframe Interval is 1 or 2. For CPU Usage Preset, select ultrafast. For Profile, select baseline. For Tune, select zerolatency. And for x264 Options, enter threads=1, and then click OK.
Warning:
You need to remove the B-frames in RTMP streams, otherwise the connection will be disconnected after pushing. To achieve this, select baseline for Profile.


Step 4. Set video parameters

You can set video resolution and frame rate under the Video section of Settings. Resolution determines the clarity of video shown to audience members. The higher the resolution, the clearer the video. Frame rate (frames per second) determines playback smoothness. Typical frame rate falls in the range of 24 fps to 30 fps. Playback may stutter if frame rate is lower than 16 fps. Video games require higher frame rate and tend to stutter at a frame rate lower than 30 fps.


Step 5. Configure advanced settings

To reduce end-to-end delay, we recommend you do not enable Stream Delay.
Keep Automatically Reconnect enabled and make Retry Delay as short as possible so that the publisher can be reconnected quickly after a disconnection occurs due to network jitter.


Step 6. Publish the stream

1. In the Controls panel at the bottom, click Start Streaming.

2. If streaming is successful, the bottom bar will show streaming statistics, and the entry of a user will be recorded by the TRTC monitoring dashboard.


Step 7. Play the stream on other clients

As mentioned above in Set publishing parameters, to play the RTMP stream on other TRTC clients, you need to use a string-type room ID when entering the room. The screenshot below is an example of playing the RTMP stream on a browser. You can also play the stream on other clients.


Using FFmpeg to publish streams

To publish RMTP streams using FFmpeg commands or other RTMP libraries, you need to use the full URL, the H.264 video codec, and the AAC audio codec. For the container format, FLV is recommended. For GOP, one or two seconds is recommended. The configuration of FFmpeg parameters varies with different scenarios, so you need to have some knowledge of FFmpeg in order to use it to publish streams. The table below lists some common FFmpeg commands. For more options, see FFmpeg documentation.

FFmpeg commands

ffmpeg [global_options] {[input_file_options] -i input_url} ... {[output_file_options] output_url}

Common FFmpeg options

Option
Note
-re
Reads input at the native frame rate. This is usually used to read local files.
Options for output_file_options include:
Option
Note
-c:v
The video encoding library. libx264 is recommended.
-b:v
The video bitrate. For example, 1500k means 1,500 Kbps.
-r
The video frame rate.
-profile:v
The video profile. If you set it to baseline, B-frames will not be encoded. The TRTC backend does not support B-frames.
-g
The GOP (keyframe interval).
-c:a
The audio encoding library. libfdk_aac is recommended.
-ac
The number of sound channels. Valid values: 1, 2.
-b:a
The audio bitrate.
-f
The container format. Set it to flv. The FLV container format is required to publish to TRTC.
Below is an example of reading a local file and publishing it to TRTC (note that quotation marks are required for the URL):
ffmpeg -loglevel debug -re -i sample.flv -c:v libx264 -preset ultrafast -profile:v baseline -g 30 -sc_threshold 0 -b:v 1500k -c:a libfdk_aac -ac 2 -b:a 128k -f flv 'rtmp://intl-rtmp.rtc.qq.com/push/hello-string-room?userid=rtmpForFfmpeg&sdkappid=140xxxxxx&usersig=xxxxxxxxxx'

Playback on other clients

The screenshot below is an example of playback on a browser. You can also play the stream on other clients.


FFplay playback

Below is an example of playing the stream using FFplay (note that quotations marks are required for the URL):
ffplay -loglevel debug -fflags nobuffer -flags low_delay 'rtmp://intl-rtmp.rtc.qq.com/pull/hello-string-room?userid=rtmpForFfmpeg&sdkappid=140xxxxxx&usersig=xxxxxxxxxx&remoteuserid=bob_anchor&sessionid=7CF036F0***34473B3F99A3FC152EBD3'