Audio receiver
The audio receiver feature provides a way to get audio data without affecting the actual audio stream. This feature allows your application to receive copies of audio data and use the data for various purposes.
| Supported call type | Minimum SDK version |
|---|---|
| 1-to-1 call, group call (conference) | PlanetKit 7.0 |
Audio data obtained through the audio receiver feature is an unprocessed audio stream before VQE control is applied.
Use cases
The main use cases for audio receivers are as follows.
- Audio analysis and transcription
- Voice activity detection
- Audio quality monitoring
- Call analytics and metrics collection
- Real-time audio visualization and level meters
- Recording call audio to files
Implementation steps
To implement the audio receiver feature, follow these steps.
Audio receiver for the local user
Implement an audio receiver for the local user
Create a class that conforms to PlanetKitAudioMicCaptureDelegate to receive the local user's audio data.
import PlanetKit
class MicrophoneAudioReceiver: NSObject, PlanetKitAudioMicCaptureDelegate {
func didCapture(frameCnt: UInt32,
channels: UInt32,
sampleRate: UInt32,
sampleType: PlanetKitAudioSampleType,
timestamp: AudioTimeStamp,
outData: UnsafeMutableRawPointer!,
outDataLen: UInt32) {
// Audio data parameters:
// - frameCnt: Number of audio frames in this callback
// - channels: Number of audio channels
// - sampleRate: Sample rate
// - sampleType: Sample type
// - timestamp: Audio timestamp structure
// - outData: Pointer to audio data
// - outDataLen: Size of audio data in bytes
// Use the audio data for your use case
// Note: Audio data pointer is only valid during this callback
// Copy the data if you need to process it asynchronously
}
}
Set the audio receiver
Register the audio receiver through the audio session to start receiving the local user's audio data.
let audioSession = PlanetKitAudioManager.shared.session
let micReceiver = MicrophoneAudioReceiver()
audioSession.addMicReceiver(micReceiver)
Clear the audio receiver
Unregister the audio receiver to stop receiving audio data.
audioSession.removeMicReceiver(micReceiver)
Audio receiver for peers
Implement an audio receiver for peers
Create a class that conforms to PlanetKitAudioSpkPlayDelegate to receive the peer's audio data.
import PlanetKit
class SpeakerAudioReceiver: NSObject, PlanetKitAudioSpkPlayDelegate {
func willPlay(frameCnt: UInt32,
channels: UInt32,
sampleRate: UInt32,
sampleType: PlanetKitAudioSampleType,
timestamp: AudioTimeStamp,
playBuf: UnsafeMutableRawPointer!,
playBufSize: UInt32) -> Int32 {
// Audio data parameters:
// - frameCnt: Number of audio frames in this callback
// - channels: Number of audio channels
// - sampleRate: Sample rate
// - sampleType: Sample type
// - timestamp: Audio timestamp structure
// - playBuf: Pointer to audio data to be played
// - playBufSize: Size of audio data in bytes
// Use the audio data for your use case
// Note: Audio data pointer is only valid during this callback
// Copy the data if you need to process it asynchronously
// Return the number of bytes consumed
// Typically, return the full buffer size
return Int32(playBufSize)
}
}
Set the audio receiver
Register the audio receiver through the audio session to start receiving audio data.
let audioSession = PlanetKitAudioManager.shared.session
let speakerReceiver = SpeakerAudioReceiver()
audioSession.addSpeakerReceiver(speakerReceiver)
Clear the audio receiver
Unregister the audio receiver to stop receiving audio data.
audioSession.removeSpeakerReceiver(speakerReceiver)
Related APIs
The APIs related to the audio receiver feature are as follows.