MediaEngine

The MediaEngine class.

pullAudioFrame

Pulls the remote audio data.

Future<void> pullAudioFrame(AudioFrame frame);

Details

Before calling this method, call setExternalAudioSink(enabled: true) to notify the app to enable and set the external audio rendering.

After a successful call of this method, the app pulls the decoded and mixed audio data for playback.

Attention:
  • Call this method after joining a channel.
  • Both this method and onPlaybackAudioFrame callback can be used to get audio data after remote mixing. Note that after calling setExternalAudioSink to enable external audio rendering, the app no longer receives data from the onPlaybackAudioFrame callback. Therefore, you should choose between this method and the onPlaybackAudioFrame callback based on your actual business requirements. The specific distinctions between them are as follows:
    • After calling this method, the app automatically pulls the audio data from the SDK. By setting the audio data parameters, the SDK adjusts the frame buffer to help the app handle latency, effectively avoiding audio playback jitter.
    • The SDK sends the audio data to the app through the onPlaybackAudioFrame callback. Any delay in processing the audio frames may result in audio jitter.
  • This method is only used for retrieving audio data after remote mixing. If you need to get audio data from different audio processing stages such as capture and playback, you can register the corresponding callbacks by calling registerAudioFrameObserver.

Parameters

frame
Pointers to AudioFrame.

Exceptions

When the method call succeeds, there is no return value; when fails, the AgoraRtcException exception is thrown. You need to catch the exception and handle it accordingly.

pushAudioFrame

Pushes the external audio frame.

Future<void> pushAudioFrame({required AudioFrame frame, int trackId = 0});

Details

Attention:
Before calling this method to push external audio data, perform the following steps:
  1. Call createCustomAudioTrack to create a custom audio track and get the audio track ID.
  2. Call joinChannel to join the channel. In ChannelMediaOptions, set publishCustomAudioTrackId to the audio track ID that you want to publish, and set publishCustomAudioTrack to true.

Parameters

frame
The external audio frame. See AudioFrame.
trackId
The audio track ID. If you want to publish a custom external audio source, set this parameter to the ID of the corresponding custom audio track you want to publish.

Exceptions

When the method call succeeds, there is no return value; when fails, the AgoraRtcException exception is thrown. You need to catch the exception and handle it accordingly.

  • < 0: Failure.

pushVideoFrame

Pushes the external raw video frame to the SDK through video tracks.

Future<void> pushVideoFrame(
    {required ExternalVideoFrame frame, int videoTrackId = 0});

Details

To publish a custom video source, see the following steps:
  1. Call createCustomVideoTrack to create a video track and get the video track ID.
  2. Call joinChannel to join the channel. In ChannelMediaOptions, set customVideoTrackId to the video track ID that you want to publish, and set publishCustomVideoTrack to true.
  3. Call this method and specify videoTrackId as the video track ID set in step 2. You can then publish the corresponding custom video source in the channel.
DANGER: After calling this method, even if you stop pushing external video frames to the SDK, the custom video stream will still be counted as the video duration usage and incur charges. Agora recommends that you take appropriate measures based on the actual situation to avoid such video billing.
  • If you no longer need to capture external video data, you can call destroyCustomVideoTrack to destroy the custom video track.
  • If you only want to use the external video data for local preview and not publish it in the channel, you can call muteLocalVideoStream to cancel sending video stream or call updateChannelMediaOptions to set publishCustomVideoTrack to false.

Parameters

frame

The external raw video frame to be pushed. See ExternalVideoFrame.

videoTrackId
The video track ID returned by calling the createCustomVideoTrack method. The default value is 0.

Exceptions

When the method call succeeds, there is no return value; when fails, the AgoraRtcException exception is thrown. You need to catch the exception and handle it accordingly.

registerAudioFrameObserver

Registers an audio frame observer object.

void registerAudioFrameObserver(AudioFrameObserver observer);

Details

Call this method to register an audio frame observer object (register a callback). When you need the SDK to trigger onMixedAudioFrame, onRecordAudioFrame, onPlaybackAudioFrame or onEarMonitoringAudioFrame callback, you need to use this method to register the callbacks.

Attention: Ensure that you call this method before joining a channel.

Parameters

observer

The observer instance. See AudioFrameObserver. Agora recommends calling this method after receiving onLeaveChannel to release the audio observer object.

Exceptions

When the method call succeeds, there is no return value; when fails, the AgoraRtcException exception is thrown. You need to catch the exception and handle it accordingly.

registerFaceInfoObserver

Registers a facial information observer.

void registerFaceInfoObserver(FaceInfoObserver observer);

Details

You can call this method to register the onFaceInfo callback to receive the facial information processed by Agora speech driven extension. When calling this method to register a facial information observer, you can register callbacks in the FaceInfoObserver class as needed. After successfully registering the facial information observer, the SDK triggers the callback you have registered when it captures the facial information converted by the speech driven extension.

Attention:
  • Ensure that you call this method before joining a channel.
  • Before calling this method, you need to make sure that the speech driven extension has been enabled by calling enableExtension.

Applicable scenarios

Facial information processed by the Agora speech driven extension is BS (Blend Shape) data that complies with ARkit standards. You can further process the BS data using third-party 3D rendering engines, such as driving avatar to make mouth movements corresponding to speech.

Parameters

observer
Facial information observer, see FaceInfoObserver.

Exceptions

When the method call succeeds, there is no return value; when fails, the AgoraRtcException exception is thrown. You need to catch the exception and handle it accordingly.

registerVideoEncodedFrameObserver

Registers a receiver object for the encoded video image.

void registerVideoEncodedFrameObserver(VideoEncodedFrameObserver observer);

Details

If you only want to observe encoded video frames (such as h.264 format) without decoding and rendering the video, Agora recommends that you implement one VideoEncodedFrameObserver class through this method.

If you want to obtain the original video data of some remote users (referred to as group A) and the encoded video data of other remote users (referred to as group B), you can refer to the following steps:
  1. Call registerVideoFrameObserver to register the raw video frame observer before joining the channel.
  2. Call registerVideoEncodedFrameObserver to register the encoded video frame observer before joining the channel.
  3. After joining the channel, get the user IDs of group B users through onUserJoined, and then call setRemoteVideoSubscriptionOptions to set the encodedFrameOnly of this group of users to true.
  4. Call muteAllRemoteVideoStreams(false)to start receiving the video streams of all remote users. Then:
    • The raw video data of group A users can be obtained through the callback in VideoFrameObserver, and the SDK renders the data by default.
    • The encoded video data of group B users can be obtained through the callback in VideoEncodedFrameObserver.
Attention:
  • Call this method before joining a channel.

Parameters

observer
The video frame observer object. See VideoEncodedFrameObserver.

Exceptions

When the method call succeeds, there is no return value; when fails, the AgoraRtcException exception is thrown. You need to catch the exception and handle it accordingly.

registerVideoFrameObserver

Registers a raw video frame observer object.

void registerVideoFrameObserver(VideoFrameObserver observer);

Details

If you want to observe raw video frames (such as YUV or RGBA format), Agora recommends that you implement one VideoFrameObserver class with this method.

When calling this method to register a video observer, you can register callbacks in the VideoFrameObserver class as needed. After you successfully register the video frame observer, the SDK triggers the registered callbacks each time a video frame is received.

If you want to obtain the original video data of some remote users (referred to as group A) and the encoded video data of other remote users (referred to as group B), you can refer to the following steps:
  1. Call registerVideoFrameObserver to register the raw video frame observer before joining the channel.
  2. Call registerVideoEncodedFrameObserver to register the encoded video frame observer before joining the channel.
  3. After joining the channel, get the user IDs of group B users through onUserJoined, and then call setRemoteVideoSubscriptionOptions to set the encodedFrameOnly of this group of users to true.
  4. Call muteAllRemoteVideoStreams(false)to start receiving the video streams of all remote users. Then:
    • The raw video data of group A users can be obtained through the callback in VideoFrameObserver, and the SDK renders the data by default.
    • The encoded video data of group B users can be obtained through the callback in VideoEncodedFrameObserver.
Attention:
  • Ensure that you call this method before joining a channel.
  • When handling the video data returned in the callbacks, pay attention to the changes in the width and height parameters, which may be adapted under the following circumstances:
    • When network conditions deteriorate, the video resolution decreases incrementally.
    • If the user adjusts the video profile, the resolution of the video returned in the callbacks also changes.

Parameters

observer
The observer instance. See VideoFrameObserver.

Exceptions

When the method call succeeds, there is no return value; when fails, the AgoraRtcException exception is thrown. You need to catch the exception and handle it accordingly.

  • < 0: Failure.

unregisterAudioFrameObserver

Unregisters an audio frame observer.

void unregisterAudioFrameObserver(AudioFrameObserver observer);

Parameters

observer
The audio frame observer, reporting the reception of each audio frame. See AudioFrameObserver.

Exceptions

When the method call succeeds, there is no return value; when fails, the AgoraRtcException exception is thrown. You need to catch the exception and handle it accordingly.

unregisterFaceInfoObserver

Unregisters a facial information observer.

void unregisterFaceInfoObserver(FaceInfoObserver observer);

Details

Parameters

observer
Facial information observer, see FaceInfoObserver.

Exceptions

When the method call succeeds, there is no return value; when fails, the AgoraRtcException exception is thrown. You need to catch the exception and handle it accordingly.

unregisterVideoEncodedFrameObserver

Unregisters a receiver object for the encoded video frame.

void unregisterVideoEncodedFrameObserver(VideoEncodedFrameObserver observer);

Parameters

observer
The video observer, reporting the reception of each video frame. See VideoEncodedFrameObserver.

Exceptions

When the method call succeeds, there is no return value; when fails, the AgoraRtcException exception is thrown. You need to catch the exception and handle it accordingly.

unregisterVideoFrameObserver

Unregisters the video frame observer.

void unregisterVideoFrameObserver(VideoFrameObserver observer);

Parameters

observer
The video observer, reporting the reception of each video frame. See VideoFrameObserver.

Exceptions

When the method call succeeds, there is no return value; when fails, the AgoraRtcException exception is thrown. You need to catch the exception and handle it accordingly.

setExternalAudioSink

Sets the external audio sink.

Future<void> setExternalAudioSink(
    {required bool enabled, required int sampleRate, required int channels});

Details

This method applies to scenarios where you want to use external audio data for playback. After you set the external audio sink, you can call pullAudioFrame to pull remote audio frames. The app can process the remote audio and play it with the audio effects that you want.

Parameters

enabled
Whether to enable or disable the external audio sink:
  • true: Enables the external audio sink.
  • false: (Default) Disables the external audio sink.
sampleRate

The sample rate (Hz) of the external audio sink, which can be set as 16000, 32000, 44100, or 48000.

channels
The number of audio channels of the external audio sink:
  • 1: Mono.
  • 2: Stereo.

Exceptions

When the method call succeeds, there is no return value; when fails, the AgoraRtcException exception is thrown. You need to catch the exception and handle it accordingly.

setExternalAudioSource

Sets the external audio source parameters.

Future<void> setExternalAudioSource(
    {required bool enabled,
    required int sampleRate,
    required int channels,
    bool localPlayback = false,
    bool publish = true});

Details

Deprecated:
This method is deprecated, use createCustomAudioTrack instead.
Attention: Call this method before joining a channel.

Parameters

enabled
Whether to enable the external audio source:
  • true: Enable the external audio source.
  • false: (Default) Disable the external audio source.
sampleRate
The sample rate (Hz) of the external audio source which can be set as 8000, 16000, 32000, 44100, or 48000.
channels
The number of channels of the external audio source, which can be set as 1 (Mono) or 2 (Stereo).
localPlayback
Whether to play the external audio source:
  • true: Play the external audio source.
  • false: (Default) Do not play the external source.
publish
Whether to publish audio to the remote users:
  • true: (Default) Publish audio to the remote users.
  • false: Do not publish audio to the remote users.

Exceptions

When the method call succeeds, there is no return value; when fails, the AgoraRtcException exception is thrown. You need to catch the exception and handle it accordingly.

  • < 0: Failure.

setExternalVideoSource

Configures the external video source.

Future<void> setExternalVideoSource(
    {required bool enabled,
    required bool useTexture,
    ExternalVideoSourceType sourceType = ExternalVideoSourceType.videoFrame,
    SenderOptions encodedVideoOption = const SenderOptions()});

Details

Attention: Call this method before joining a channel.

Parameters

enabled
Whether to use the external video source:
  • true: Use the external video source. The SDK prepares to accept the external video frame.
  • false: (Default) Do not use the external video source.
useTexture
Whether to use the external video frame in the Texture format.
  • true: Use the external video frame in the Texture format.
  • false: (Default) Do not use the external video frame in the Texture format.
sourceType
Whether the external video frame is encoded. See ExternalVideoSourceType.
encodedVideoOption
Video encoding options. This parameter needs to be set if sourceType is encodedVideoFrame. To set this parameter, contact technical support.

Exceptions

When the method call succeeds, there is no return value; when fails, the AgoraRtcException exception is thrown. You need to catch the exception and handle it accordingly.

  • < 0: Failure.