Custom audio source
Updated
Integrate a custom video or audio capture into your client
The default audio module of Voice SDK meets the need of using basic audio functions in your app. For adding advanced audio functions, Voice SDK supports using custom audio sources and custom audio rendering modules.
Video SDK uses the basic audio module on the device your app runs on by default. However, there are certain use-cases where you want to integrate a custom audio source into your app, such as:
- Your app has its own audio module.
- You need to process the captured audio with a pre-processing library for audio enhancement.
- You need flexible device resource allocation to avoid conflicts with other services.
This page shows you how to capture and render audio from custom sources.
Understand the tech
To set an external audio source, you configure the Agora Engine before joining a channel. To manage the capture and processing of audio frames, you use methods from outside the Video SDK that are specific to your custom source. Video SDK enables you to push processed audio data to the subscribers in a channel.
Capture custom audio
The following figure illustrates the process of custom audio capture.
-
You implement the capture module using external methods provided by the SDK.
-
You call
pushExternalAudioFrameto send the captured audio frames to the SDK.
Render custom audio
The following figure illustrates the process of custom audio rendering.
-
You implement the rendering module using external methods provided by the SDK.
-
You call
pullPlaybackAudioFrameto retrieve the audio data sent by remote users.
Prerequisites
Ensure that you have implemented the SDK quickstart in your project.
Implementation
This section shows you how to implement custom audio capture and render audio from a custom source.
Customize the audio source
Refer to the following call sequence diagram to implement custom audio capture in your app:
Custom audio capture
Follow these steps to implement custom audio capture in your project:
-
After initializing
AgoraRtcEngineKit, callcreateCustomAudioTrackto create a custom audio track and retrieve the audio track ID.let audioTrack = AgoraAudioTrackConfig() audioTrack.enableLocalPlayback = true trackId = agoraKit.createCustomAudioTrack(.mixable, config: audioTrack) -
Use
joinChannelByTokento join the channel. InAgoraRtcChannelMediaOptions, set thepublishCustomAudioTrackIdparameter to the audio track ID you obtained. SetpublishCustomAudioTracktoYESto publish the custom audio track in the channel.To play an external audio source locally using
enableCustomAudioLocalPlayback, or to adjust the local playback volume of a custom audio track usingadjustCustomAudioPlayoutVolume, setenableAudioRecordingOrPlayouttoYESinAgoraRtcChannelMediaOptions.let option = AgoraRtcChannelMediaOptions() // Disable microphone track option.publishMicrophoneTrack = false // Enable the custom audio track option.publishCustomAudioTrack = true // Set the custom audio track ID option.publishCustomAudioTrackId = Int(trackId) option.clientRoleType = GlobalSettings.shared.getUserRole() NetworkManager.shared.generateToken(channelName: channelName, success: { token in // Join the channel let result = self.agoraKit.joinChannel(byToken: token, channelId: channelName, uid: 0, mediaOptions: option) if result != 0 { self.showAlert(title: "Error", message: "Failed to join the channel: \(result). Check your parameters.") } }) -
Implement the custom audio acquisition module
Agora provides a sample project CustomAudioSource.swift to demonstrate how to read PCM audio data from a local file. In production, create a custom audio acquisition module tailored to your business needs.
-
Call
pushExternalAudioFrameRawDatato send captured audio frames to the SDK via the custom audio track. EnsuretrackIdmatches the one used when joining the channel in step 2. SetsampleRate,channels, andsamplesto configure the external audio frame.- For audio-video synchronization, Agora recommends using
getCurrentMonotonicTimeInMsto obtain the system’s monotonic time and set thetimestamp. - To push audio frames in
CMSampleBufferformat, usepushExternalAudioFrameSampleBuffer.
- For audio-video synchronization, Agora recommends using
extension CustomAudioSource: AgoraPcmSourcePushDelegate {
func onAudioFrame(data: UnsafeMutablePointer<UInt8>) {
agoraKit.pushExternalAudioFrameRawData(data,
samples: samples,
sampleRate: Int(sampleRate),
channels: Int(audioChannel),
trackId: Int(trackId),
timestamp: 0)
}
}-
To stop publishing custom audio, call
destroyCustomAudioTrackto remove the custom audio track.agoraKit.destroyCustomAudioTrack(Int(trackId))
Custom audio rendering
This section explains how to implement custom audio rendering.
To retrieve audio data for playback, implement collection and processing of raw audio data. Refer to Raw audio processing.
Follow these steps to call the raw audio data API and implement custom audio rendering in your project:
-
Retrieve the audio data to be played from
onRecordAudioFrame,onPlaybackAudioFrame,onMixedAudioFrame, oronPlaybackAudioFrameBeforeMixing. -
Independently render and play the audio data.
Reference
This section explains how to implement different sound effects and audio mixing in your app, covering essential steps and code snippets.
Sample projects
Agora provides the following open-source sample projects for audio self-capture and audio self-rendering for your reference:
