Cloud Media Relay
Cross-Channel Media Relay
pauseAllChannelMediaRelay
Pauses media stream relay to all destination channels.
public abstract int pauseAllChannelMediaRelay();
You can call this method to pause media stream relay to all destination channels. To resume relaying, call the resumeAllChannelMediaRelay method.
Return Values
- 0: Success.
- < 0: Failure.
- -5: The call is rejected. No media stream relay is currently running.
pauseAllChannelMediaRelayEx
Pauses media stream relay to all destination channels.
public abstract int pauseAllChannelMediaRelayEx(RtcConnection connection);
- Deprecated
- Deprecated since v4.2.0. Use startOrUpdateChannelMediaRelayEx instead.
You can call this method to pause media stream relay to all destination channels. To resume relay, call resumeAllChannelMediaRelay.
Scenario
This method is applicable in multi-channel scenarios.
Parameters
- connection
- Connection information. See RtcConnection.
Return Values
- 0: Success.
- < 0: Failure.
- -5: Call rejected; no ongoing media stream relay.
resumeAllChannelMediaRelay
Resumes media stream relay to all destination channels.
public abstract int resumeAllChannelMediaRelay();
After calling pauseAllChannelMediaRelay, you can call this method to resume media stream relay.
Return Values
- 0: Success.
- < 0: Failure.
- -5: The call is rejected. No paused media stream relay found.
resumeAllChannelMediaRelayEx
Resumes media stream relay to all destination channels.
public abstract int resumeAllChannelMediaRelayEx(RtcConnection connection);
After calling pauseAllChannelMediaRelayEx, you can call this method to resume media stream relay.
Scenario
This method is applicable in multi-channel scenarios.
Parameters
- connection
- Connection information. See RtcConnection.
Return Values
- 0: Success.
- < 0: Failure.
- -5: Call rejected. There is no media stream relay currently in a paused state.
startOrUpdateChannelMediaRelay
Starts or updates media stream relay across channels.
public abstract int startOrUpdateChannelMediaRelay(ChannelMediaRelayConfiguration channelMediaRelayConfiguration);
- Since
- Added since v4.2.0.
The first successful call to this method relays the media stream from the source channel to the destination channel. To relay to additional channels or stop relaying to a specific channel, call this method again to update the destination channels. This feature supports relaying to up to six destination channels.
Parameters
- channelMediaRelayConfiguration
- The configuration for media stream relay. See ChannelMediaRelayConfiguration.
Return Values
- 0: Success.
- < 0: Failure.
- -1: A general error occurred (no specific reason).
- -2: Invalid parameter.
- -8: Internal state error, possibly because the user is not a broadcaster.
startOrUpdateChannelMediaRelayEx
Starts or updates media stream relay across channels.
public abstract int startOrUpdateChannelMediaRelayEx(ChannelMediaRelayConfiguration channelMediaRelayConfiguration, RtcConnection connection);
- Since
- Added since v4.2.0.
The first successful call to this method starts relaying media streams from the source channel to the destination channel. To relay to other channels or stop relaying to a specific channel, call this method again to update the destination channels. This feature supports up to six destination channels.
- Call this method after joining a channel.
- This method is only effective when you are a broadcaster in a live streaming channel.
- Contact technical support to enable media stream relay across channels.
- This API does not support string user accounts.
Scenario
This method applies to multi-channel scenarios.
Parameters
- channelMediaRelayConfiguration
- The configuration for media stream relay. See ChannelMediaRelayConfiguration.
- connection
- Connection information. See RtcConnection.
Return Values
- 0: Success.
- < 0: Failure.
- -1: General error (no specific reason).
- -2: Invalid parameters.
- -8: Internal state error, possibly because the user is not a broadcaster.
stopChannelMediaRelay
Stops media stream relay across channels.
public abstract int stopChannelMediaRelay();
After calling this method, the broadcaster leaves all destination channels.
RELAY_ERROR_SERVER_NO_RESPONSE (2) or RELAY_ERROR_SERVER_CONNECTION_LOST (8) through the onChannelMediaRelayStateChanged callback. You can also call the leaveChannel method to leave the channel, and the media stream relay will automatically stop.Return Values
- 0: Success.
- < 0: Failure.
- -5: The call is rejected. No media stream relay is currently running.
stopChannelMediaRelayEx
Stops media stream relay across channels.
public abstract int stopChannelMediaRelayEx(RtcConnection connection);
Call this method to stop media stream relay across channels. Once stopped, the broadcaster exits all destination channels.
RELAY_ERROR_SERVER_NO_RESPONSE (2) or RELAY_ERROR_SERVER_CONNECTION_LOST (8) via the onChannelMediaRelayStateChanged callback. You can call leaveChannel to leave the channel, and the media stream relay will stop automatically.Scenario
This method is applicable in multi-channel scenarios.
Parameters
- connection
- Connection information. See RtcConnection.
Return Values
- 0: Success.
- < 0: Failure.
- -5: Call was rejected. No active media stream relay across channels.
onChannelMediaRelayStateChanged
Callback when the state of media relay across channels changes.
public void onChannelMediaRelayStateChanged(int state, int code)
This callback returns the error information of the current media relay across channels.
Trigger Timing
This callback is triggered when the state of media relay across channels changes.
Parameters
- state
- The state code of the media relay across channels:
RELAY_STATE_IDLE: The SDK is initializing.RELAY_STATE_CONNECTING: The SDK tries to relay the media stream to the target channel.RELAY_STATE_RUNNING: The SDK successfully relays the media stream to the target channel.RELAY_STATE_FAILURE: An error occurs. See code.
- code
- The error code of the media relay across channels:
RELAY_OK: Everything works fine.RELAY_ERROR_SERVER_ERROR_RESPONSE: Server error response.RELAY_ERROR_SERVER_NO_RESPONSE: No response from the server. This error may be caused by poor network connectivity. If this error occurs when starting media relay, you can try again later; if it occurs during relay, you can callleaveChannelto leave the channel. This error may also occur if the project has not enabled the media relay service. You can contact technical support to enable it.RELAY_ERROR_NO_RESOURCE_AVAILABLE: The SDK cannot access the service, possibly due to limited server resources.RELAY_ERROR_FAILED_JOIN_SRC: The server fails to send the relay request.RELAY_ERROR_FAILED_JOIN_DEST: The server fails to accept the relay request.RELAY_ERROR_FAILED_PACKET_RECEIVED_FROM_SRC: The server fails to receive the media stream.RELAY_ERROR_FAILED_PACKET_SENT_TO_DEST: The server fails to send the media stream.RELAY_ERROR_SERVER_CONNECTION_LOST: The SDK loses connection with the server due to poor network. You can callleaveChannelto leave the channel.RELAY_ERROR_INTERNAL_ERROR: Internal server error.RELAY_ERROR_SRC_TOKEN_EXPIRED: The token of the source channel has expired.RELAY_ERROR_DEST_TOKEN_EXPIRED: The token of the destination channel has expired.
Media Push
startRtmpStreamWithoutTranscoding
Starts pushing audio and video streams to the CDN without transcoding.
public abstract int startRtmpStreamWithoutTranscoding(String url);
Agora recommends using a more complete server-side streaming solution. See [Implement server-side live streaming](https://doc.shengwang.cn/doc/media-push/restful/landing-page). You can call this method to push audio or video streams to a specified CDN URL. This method can only push media streams to one CDN URL at a time. To push to multiple URLs, call this method multiple times.
- Call this method after joining a channel.
- Only users with the broadcaster role in
LIVE_BROADCASTINGmode can call this method. - If you need to retry after a streaming failure, first call stopRtmpStream to stop streaming, then call this method again; otherwise, the SDK will return the same error code as the previous failure.
Parameters
- url
- The streaming URL in RTMP or RTMPS format. The length must not exceed 1024 bytes. Chinese or other special characters are not supported.
Return Values
- 0: Success.
- < 0: Failure.
- -2: Invalid
url. - -7: SDK not initialized before calling this method.
- -19: Streaming URL already in use. Please use a different URL.
- -2: Invalid
startRtmpStreamWithoutTranscodingEx
Starts pushing the media stream to the CDN without transcoding.
public abstract int startRtmpStreamWithoutTranscodingEx(String url, RtcConnection connection);
Agora recommends using the more comprehensive server-side streaming service. See [Implement Server-side CDN Streaming](https://doc.shengwang.cn/doc/media-push/restful/landing-page). You can call this method to push audio or video streams to a specified CDN address. This method only supports pushing to one CDN address at a time. To push to multiple addresses, call this method multiple times.
- Call this method after joining a channel.
- Only users in the
LIVE_BROADCASTINGchannel profile and with broadcaster role can call this method. - If the stream fails and you need to retry, call stopRtmpStream first to stop the stream, then call this method again. Otherwise, the SDK returns the same error code as the previous failure.
Scenario
This method applies to multi-channel scenarios.
Parameters
- url
- The CDN streaming URL. Must be in RTMP or RTMPS format, and no longer than 1024 bytes. Special characters such as Chinese are not supported.
- connection
- Connection information. See RtcConnection.
Return Values
- 0: Success.
- < 0: Failure.
- -2: Invalid URL or transcoding configuration. Please check the URL and transcoding settings.
- -7: SDK not initialized before calling this method.
- -19: The CDN streaming URL is already in use. Please use a different URL.
startRtmpStreamWithTranscoding
Starts live streaming with transcoding configuration.
public abstract int startRtmpStreamWithTranscoding(String url, LiveTranscoding transcoding);
Agora recommends using a more complete server-side streaming solution. See [Implement server-side live streaming](https://doc.shengwang.cn/doc/media-push/restful/landing-page). You can call this method to push real-time audio and video streams to a specified CDN URL and set the transcoding configuration. This method can only push media streams to one CDN URL at a time. To push to multiple URLs, call this method multiple times. By default, a maximum of 200 streaming tasks can be run simultaneously under one Agora project. For higher limits, contact technical support.
- Call this method after joining a channel.
- Only users with the broadcaster role in an interactive live streaming channel can call this method.
- If you need to retry after a streaming failure, first call stopRtmpStream to stop streaming, then call this method again; otherwise, the SDK will return the same error code as the previous failure.
Parameters
- url
- The streaming URL in RTMP or RTMPS format. The length must not exceed 1024 bytes. Chinese or other special characters are not supported.
- transcoding
- The transcoding configuration for live streaming. See LiveTranscoding.
Return Values
- 0: Success.
- < 0: Failure.
- -2: Invalid
urlor transcoding configuration. Please checkurland transcoding settings. - -7: SDK not initialized before calling this method.
- -19: Streaming URL already in use. Please use a different URL.
- -2: Invalid
startRtmpStreamWithTranscodingEx
Starts CDN streaming with transcoding configuration.
public abstract int startRtmpStreamWithTranscodingEx(String url, LiveTranscoding transcoding, RtcConnection connection);
Agora recommends using the more comprehensive server-side streaming service. See [Implement Server-side CDN Streaming](https://doc.shengwang.cn/doc/media-push/restful/landing-page). You can call this method to push audio and video live streams to a specified CDN address and set the transcoding configuration. This method only supports pushing to one CDN address at a time. To push to multiple addresses, call this method multiple times.
- Before using this method, make sure CDN streaming is enabled.
- Call this method after joining a channel.
- Only broadcasters in a
LIVE_BROADCASTINGchannel can call this method. - If the stream fails and you need to retry, call stopRtmpStreamEx first to stop the stream, then call this method again. Otherwise, the SDK returns the same error code as the previous failure.
Scenario
This method applies to multi-channel scenarios.
Parameters
- url
- The CDN streaming URL. Must be in RTMP or RTMPS format, and no longer than 1024 bytes. Special characters such as Chinese are not supported.
- transcoding
- The transcoding configuration for CDN streaming. See LiveTranscoding.
- connection
- Connection information. See RtcConnection.
Return Values
- 0: Success.
- < 0: Failure.
- -2: Invalid URL or transcoding configuration. Please check the
urlandtranscodingparameters. - -7: SDK not initialized before calling this method.
- -19: The CDN streaming URL is already in use. Please use a different URL.
- -2: Invalid URL or transcoding configuration. Please check the
stopRtmpStream
Stops pushing media streams to the specified CDN URL.
public abstract int stopRtmpStream(String url);
Agora recommends using a more complete server-side streaming solution. See [Implement server-side live streaming](https://doc.shengwang.cn/doc/media-push/restful/landing-page). You can call this method to stop pushing media streams to a specified CDN URL. This method can only stop streaming to one URL at a time. To stop multiple streams, call this method multiple times.
Parameters
- url
- The streaming URL in RTMP or RTMPS format. The length must not exceed 1024 bytes. Chinese or other special characters are not supported.
Return Values
- 0: Success.
- < 0: Failure.
stopRtmpStreamEx
Stops pushing media streams to the specified CDN address.
public abstract int stopRtmpStreamEx(String url, RtcConnection connection);
Agora recommends using the more comprehensive server-side streaming service. See [Implement Server-side CDN Streaming](https://doc.shengwang.cn/doc/media-push/restful/landing-page). You can call this method to stop pushing live streams to a specified CDN address. This method only stops streaming to one address at a time. To stop multiple streams, call this method multiple times.
Scenario
This method applies to multi-channel scenarios.
Parameters
- url
- The CDN streaming URL. Must be in RTMP or RTMPS format, and no longer than 1024 bytes. Special characters such as Chinese are not supported.
- connection
- Connection information. See RtcConnection.
Return Values
- 0: Success.
- < 0: Failure.
updateRtmpTranscoding
Updates the transcoding configuration for live streaming.
public abstract int updateRtmpTranscoding(LiveTranscoding transcoding);
Agora recommends using a more complete server-side streaming solution. See [Implement server-side live streaming](https://doc.shengwang.cn/doc/media-push/restful/landing-page).
Parameters
- transcoding
- The transcoding configuration for live streaming. See LiveTranscoding.
Return Values
- 0: Success.
- < 0: Failure.
updateRtmpTranscodingEx
Updates the transcoding configuration for CDN streaming.
public abstract int updateRtmpTranscodingEx(LiveTranscoding transcoding, RtcConnection connection);
Agora recommends using the more comprehensive server-side streaming service. See [Implement Server-side CDN Streaming](https://doc.shengwang.cn/doc/media-push/restful/landing-page). After starting CDN streaming with transcoding, you can dynamically update the configuration as needed. After the update, the SDK triggers the onTranscodingUpdated callback.
Scenario
This method applies to multi-channel scenarios.
Parameters
- transcoding
- The transcoding configuration for CDN streaming. See LiveTranscoding.
- connection
- Connection information. See RtcConnection.
Return Values
- 0: Success.
- < 0: Failure.
onRtmpStreamingEvent
Callback for RTMP streaming events.
public void onRtmpStreamingEvent(String url, int event)
- Since
- Available since v3.1.0.
Parameters
- url
- The RTMP streaming URL.
- event
- The event code for RTMP streaming:
RTMP_STREAMING_EVENT_FAILED_LOAD_IMAGE(1): Error occurred when adding background or watermark image.RTMP_STREAMING_EVENT_URL_ALREADY_IN_USE(2): The streaming URL is already in use. Use a new URL to start a new stream.RTMP_STREAMING_EVENT_ADVANCED_FEATURE_NOT_SUPPORT(3): This feature is not supported.RTMP_STREAMING_EVENT_REQUEST_TOO_OFTEN(4): Reserved parameter.
onRtmpStreamingStateChanged
Callback for RTMP streaming state changes.
public void onRtmpStreamingStateChanged(String url, int state, int reason)
This callback indicates the state of RTMP streaming. When the state changes, the SDK triggers this callback and returns the corresponding URL and current streaming state. If an exception occurs, refer to the error code parameter for detailed error information.
Trigger Timing
This callback is triggered when the RTMP streaming state changes.
Parameters
- url
- The RTMP streaming URL whose state has changed.
- state
- The current RTMP streaming state:
RTMP_STREAM_PUBLISH_STATE_IDLE(0): RTMP streaming has not started or has ended.RTMP_STREAM_PUBLISH_STATE_CONNECTING(1): Connecting to the streaming and CDN servers.RTMP_STREAM_PUBLISH_STATE_RUNNING(2): Streaming is ongoing. The SDK successfully pushes the RTMP or RTMPS stream and returns this state.RTMP_STREAM_PUBLISH_STATE_RECOVERING(3): Attempting to recover the stream. When an exception occurs or streaming is interrupted, the SDK tries to recover and returns this state.- If recovery succeeds, returns
RTMP_STREAM_PUBLISH_STATE_RUNNING(2). - If recovery fails within 60 seconds or a server error occurs, returns
RTMP_STREAM_PUBLISH_STATE_FAILURE(4). You can also call startRtmpStreamWithTranscoding, startRtmpStreamWithoutTranscoding, or stopRtmpStream to reconnect.
- If recovery succeeds, returns
RTMP_STREAM_PUBLISH_STATE_FAILURE(4): Streaming failed. Refer to the error code for details or try streaming again.RTMP_STREAM_PUBLISH_STATE_DISCONNECTING(5): The SDK is disconnecting from the Agora streaming and CDN servers. When you call stopRtmpStream to stop streaming, the SDK returnsRTMP_STREAM_PUBLISH_STATE_DISCONNECTINGfollowed byRTMP_STREAM_PUBLISH_STATE_IDLE.
- reason
- The reason for the RTMP streaming state change:
RTMP_STREAM_PUBLISH_REASON_OK(0): Streaming succeeded.RTMP_STREAM_PUBLISH_REASON_INVALID_ARGUMENT(1): Invalid parameter. Check parameter settings.RTMP_STREAM_PUBLISH_REASON_ENCRYPTED_STREAM_NOT_ALLOWED(2): Encrypted stream cannot be pushed.RTMP_STREAM_PUBLISH_REASON_CONNECTION_TIMEOUT(3): Streaming timeout. Try streaming again.RTMP_STREAM_PUBLISH_REASON_INTERNAL_SERVER_ERROR(4): Internal error on Agora streaming server. Try streaming again.RTMP_STREAM_PUBLISH_REASON_RTMP_SERVER_ERROR(5): CDN server error.RTMP_STREAM_PUBLISH_REASON_TOO_OFTEN(6): Reserved parameter.RTMP_STREAM_PUBLISH_REASON_REACH_LIMIT(7): The number of pushed URLs exceeds 10. Stop pushing to unnecessary URLs.RTMP_STREAM_PUBLISH_REASON_NOT_AUTHORIZED(8): The broadcaster operated on another broadcaster’s URL, such as updating or stopping their stream. Check your app logic.RTMP_STREAM_PUBLISH_REASON_STREAM_NOT_FOUND(9): The Agora server could not find the media stream for RTMP streaming.RTMP_STREAM_PUBLISH_REASON_FORMAT_NOT_SUPPORTED(10): Unsupported RTMP streaming URL format. Check if the URL format is correct.RTMP_STREAM_PUBLISH_REASON_NOT_BROADCASTER(11): The user is not a broadcaster and cannot use RTMP streaming. Check your app code logic.RTMP_STREAM_PUBLISH_REASON_TRANSCODING_NO_MIX_STREAM(13): Called updateRtmpTranscoding in a non-transcoding RTMP streaming scenario. Check your app code logic.RTMP_STREAM_PUBLISH_REASON_NET_DOWN(14): Network issue on the broadcaster's side.RTMP_STREAM_PUBLISH_REASON_INVALID_PRIVILEGE(16): The project does not have permission to use RTMP streaming.RTMP_STREAM_UNPUBLISH_REASON_OK(100): RTMP streaming stopped normally. Returned after calling stopRtmpStream.
onTranscodingUpdated
Callback for updates to publisher's cloud transcoding configuration.
public void onTranscodingUpdated() {}
When calling startRtmpStreamWithTranscoding to update the LiveTranscoding configuration, the SDK triggers this callback to report the update.
Trigger Timing
This callback is triggered when the publisher's cloud transcoding configuration is updated.
CDN Streaming
setDirectCdnStreamingAudioConfiguration
Sets the audio configuration for direct CDN streaming by the host.
public abstract int setDirectCdnStreamingAudioConfiguration(int profile)
- Deprecated
- Deprecated since v4.6.0.
When you set publishMicrophoneTrack or publishCustomAudioTrack to true in DirectCdnStreamingMediaOptions, you can call this method to set the audio configuration.
Parameters
- profile
- Audio configuration including sample rate, bitrate, codec mode, and number of channels:
DEFAULT(0): Default configuration.- Live interactive: 48 kHz sample rate, music codec, mono, max 64 Kbps.
- Communication: 32 kHz sample rate, speech codec, mono, max 18 Kbps.
SPEECH_STANDARD(1): 32 kHz sample rate, speech codec, mono, max 18 Kbps.MUSIC_STANDARD(2): 48 kHz sample rate, music codec, mono, max 64 Kbps.MUSIC_STANDARD_STEREO(3): 48 kHz sample rate, music codec, stereo, max 80 Kbps.MUSIC_HIGH_QUALITY(4): 48 kHz sample rate, music codec, mono, max 96 Kbps.MUSIC_HIGH_QUALITY_STEREO(5): 48 kHz sample rate, music codec, stereo, max 128 Kbps.
Return Values
- 0: Success.
- < 0: Failure.
setDirectCdnStreamingVideoConfiguration
Sets the video property configuration for direct CDN streaming by the host.
public abstract int setDirectCdnStreamingVideoConfiguration(VideoEncoderConfiguration config);
- Deprecated
- Deprecated since v4.6.0.
This method only affects video streams captured by the camera, screen, or custom video source. That is, when you set publishCameraTrack or publishCustomVideoTrack to true in DirectCdnStreamingMediaOptions, you can call this method to set the video property configuration.
If the local camera does not support the resolution you set, the SDK automatically adjusts it to the closest resolution for capture, encoding, or streaming while maintaining the aspect ratio of your setting. You can get the actual resolution of the video stream through the onDirectCdnStreamingStats callback.
ORIENTATION_MODE_FIXED_LANDSCAPE or ORIENTATION_MODE_FIXED_PORTRAIT.Parameters
- config
- Video property configuration. See VideoEncoderConfiguration.
Note: During CDN live streaming, Agora only supports setting ORIENTATION_MODE to
ORIENTATION_MODE_FIXED_LANDSCAPEorORIENTATION_MODE_FIXED_PORTRAIT.
Return Values
- 0: Success.
- < 0: Failure.
startDirectCdnStreaming
Starts pushing media streams directly to the CDN.
public abstract int startDirectCdnStreaming(IDirectCdnStreamingEventHandler eventHandler, String publishUrl, DirectCdnStreamingMediaOptions options);
- Deprecated
- Deprecated since v4.6.0.
publishCameraTrack and publishCustomVideoTrack to true, or both publishMicrophoneTrack and publishCustomAudioTrack to true. When selecting media settings in DirectCdnStreamingMediaOptions, you can refer to the following example:
If you want to push audio and video streams captured by the host from a custom source, set the media options as follows:
- Set
publishCustomAudioTrackto true and call pushExternalAudioFrame; - Set
publishCustomVideoTrackto true and callpushExternalVideoFrameById; - Set
publishCameraTrackto false (default); - Set
publishMicrophoneTrackto false (default).
publishCustomAudioTrack or publishMicrophoneTrack to true in DirectCdnStreamingMediaOptions and call pushExternalAudioFrame to push audio streams.Parameters
- eventHandler
- Event callback interface. See IDirectCdnStreamingEventHandler.
- publishUrl
- CDN streaming URL.
- options
- Media settings for the host. See DirectCdnStreamingMediaOptions.
Return Values
- 0: Success.
- < 0: Failure.
stopDirectCdnStreaming
Stops pushing media streams directly to the CDN.
public abstract int stopDirectCdnStreaming()
- Deprecated
- Deprecated since v4.6.0.
Return Values
- 0: Success.
- < 0: Failure.
onDirectCdnStreamingStateChanged
Callback for changes in CDN streaming state.
void onDirectCdnStreamingStateChanged(DirectCdnStreamingState state, DirectCdnStreamingReason reason, String message)
When the host pushes streams directly to the CDN and the streaming state changes, the SDK triggers this callback to report the updated streaming state, error codes, and other information. You can refer to this callback for troubleshooting.
Trigger Timing
This callback is triggered when the streaming state changes during the process of pushing streams directly to the CDN by the host.
Parameters
- state
- Current CDN streaming state. See DirectCdnStreamingState.
- reason
- Reason for the change in CDN streaming state. See DirectCdnStreamingReason.
- message
- Description of the streaming state change.
onDirectCdnStreamingStats
Callback for reporting CDN streaming statistics.
void onDirectCdnStreamingStats(DirectCdnStreamingStats stats)
When the host pushes media streams directly to the CDN, the SDK triggers this callback once per second.
Trigger Timing
This callback is triggered once per second when the host pushes media streams directly to the CDN.
Parameters
- stats
- Current CDN streaming statistics. See DirectCdnStreamingStats.