Injecting an online media stream is the action of adding an external audio or video stream to an ongoing live-broadcast channel. It enables the hosts and audience in the channel to see and hear the additional stream while interacting with each other.
The host in a live-broadcast channel pulls an online media stream and pushes it through the Video Inject Server to the Agora SD-RTN™ and the channel.
- Only one online media stream can be injected into the same channel at the same time.
- Supported codec types: AAC for audio, and H.264 for video.
- Audio-only streams are also supported.
- Only the host can inject and remove an injected media stream. Neither the co-host nor the audience can do that.
Before proceeding, ensure that you have read the quickstart guides and implemented basic real-time audio and video functions in your project.
Ensure that you enable the Media Push service before using this function. See Prerequisites.
Follow these steps to inject an online media stream:
AgoraRTCClient.addInjectStreamUrl
to inject an online media stream to the live-broadcast channel. You can modify the parameter values of config
to set the resolution, bitrate, and frame rate of the injected stream. See InjectStreamConfig.
- You can inject only one online media stream into a channel at the same time.
- If the method call succeeds, this stream is added to the channel, and the SDK triggers the
AgoraRTCClient.on("user-joined")
andAgoraRTCClient.on("user-published")
callbacks to all the users in the channel.
AgoraRTCClient.removeInjectStreamUrl
to remove the injected media stream.
- If the method call is successful, the SDK triggers the
AgoraRTCClient.on("user-unpublished")
andAgoraRTCClient.on("user-left")
callbacks to all the users in the channel.- You do not need to call
removeInjectStreamUrl
if the host has left the channel.
The client
object in the following sample code is created by calling AgoraRTC.createClient
.
const injectStreamConfig = {
width: 0,
height: 0,
videoGop: 30,
videoFramerate: 15,
videoBitrate: 400,
audioSampleRate: 44100,
audioChannels: 1,
};
// Inject an online media stream.
client.addInjectStreamUrl("<YOUR URL>", injectStreamConfig).then(() => {
console.log("add inject stream url success");
}).catch(e => {
console.log("add inject stream failed", e);
});
client.removeInjectStreamUrl().then(() => {
console.log("remove inject stream url success");
}).catch(e => {
console.log("remove inject stream failed", e);
});
To receive the injected media stream, the audience need to subscribe to the host.