docs
Loading...
Searching...
No Matches
AudioOutputabstract

#include <Ultralight/platform/AudioOutput.h>

Overview

User-defined audio output interface.

The library uses this to play the sound of <video> and <audio> elements. It decodes the audio and pushes the samples to your implementation, which plays them on a device.

You can provide the library with your own AudioOutput implementation so that media audio plays through your own audio engine.

Default Implementation

A platform-specific implementation of AudioOutput is provided for you when you call App::Create(). Its source is in the SDK's platform folder if you want a starting point.

If you are using Renderer::Create() and don't provide one, media plays without sound.

Setting the Audio Output

To provide your own custom AudioOutput implementation, you should inherit from this class, handle the virtual member functions, and then pass an instance of your class to Platform::set_audio_output() before calling Renderer::Create() or App::Create(). It must stay alive until the Renderer is destroyed.

Implementing an Audio Output

Each loaded media file plays through its own stream, identified by a stream_id. A stream goes through these calls:

  1. CreateStream() gives you the stream's sample rate and channel count.
  2. PushSamples() delivers audio as it's decoded. SetVolume() and SetPaused() can come at any time.
  3. Flush() discards the buffered audio when the media seeks, and new samples follow.
  4. Stop() tells you no more samples are coming. A later Flush() can start the stream again (eg, when the media loops).
  5. DestroyStream() frees the stream. No more calls use its stream_id.

Samples are interleaved 32-bit floats in the range [-1.0, 1.0]. If your device wants a different sample rate or channel count, resample and downmix in your implementation.

Warning
The library calls these methods from several of its own threads, not only the Renderer's thread. PushSamples() can still be running when Flush() or Stop() is called for the same stream, so protect each stream's state.
Warning
When your buffer is full, PushSamples() should wait for room instead of dropping samples (the wait keeps decoding in step with playback). A waiting PushSamples() must return as soon as Flush() or Stop() is called for its stream, or the library can hang.
Note
GetPlaybackPosition() is optional. Implement it for the most accurate audio-video sync.
Precondition
Requires the Pro edition or higher.
See also
Platform::set_audio_output()

Public Member Functions

virtual ~AudioOutput ()
virtual void CreateStream (uint32_t stream_id, const AudioStreamFormat &format)=0
 Create a new audio stream.
virtual void PushSamples (uint32_t stream_id, const float *samples, uint32_t num_frames)=0
 Push decoded samples into a stream.
virtual void SetVolume (uint32_t stream_id, float volume)=0
 Set the volume of a stream.
virtual void SetPaused (uint32_t stream_id, bool paused)=0
 Pause or resume a stream.
virtual void Flush (uint32_t stream_id)=0
 Discard a stream's buffered audio.
virtual void Stop (uint32_t stream_id)=0
 Tell a stream that no more samples are coming.
virtual void DestroyStream (uint32_t stream_id)=0
 Destroy a stream and free its resources.
virtual uint64_t GetPlaybackPosition (uint32_t stream_id)
 Get the number of frames of a stream that have been played.
virtual AudioFormat preferred_output_format () const
 Get the device's preferred audio format.

Constructor & Destructor Documentation

◆ ~AudioOutput()

virtual ~AudioOutput ( )
virtual

Member Function Documentation

◆ CreateStream()

virtual void CreateStream ( uint32_t stream_id,
const AudioStreamFormat & format )
pure virtual

Create a new audio stream.

Parameters
stream_idThe id the library uses for this stream in every later call.
formatThe sample rate and channel count of the samples you'll receive.
Note
There's no way to report a failure. If you can't create the stream, ignore later calls with this stream_id.

◆ DestroyStream()

virtual void DestroyStream ( uint32_t stream_id)
pure virtual

Destroy a stream and free its resources.

The library calls Stop() first and waits for its pushes to finish, so no PushSamples() call is running for this stream.

Parameters
stream_idThe stream to destroy.

◆ Flush()

virtual void Flush ( uint32_t stream_id)
pure virtual

Discard a stream's buffered audio.

Called when the media seeks. The next pushed sample is from the new position.

Parameters
stream_idThe stream to flush.
Note
Leave a paused stream paused. The library resumes it when the new position is ready.

◆ GetPlaybackPosition()

virtual uint64_t GetPlaybackPosition ( uint32_t stream_id)
inlinevirtual

Get the number of frames of a stream that have been played.

The library uses this to keep the video in sync with the audio. Follow these rules:

  • Count played frames. Count frames the device has played (after its output latency, if your backend reports it), not frames you've pushed or buffered.
  • Reset on Flush(). Flush() sets the count to zero.
  • Hold while paused. The count must not advance while the stream is paused.
Parameters
stream_idThe stream to query.
Returns
Returns the frame count, or 0 if your backend can't report it (the library then times the video by the wall clock, which is less precise).

◆ preferred_output_format()

virtual AudioFormat preferred_output_format ( ) const
inlinevirtual

Get the device's preferred audio format.

Returns
Returns the device's preferred channel count and sample rate.

◆ PushSamples()

virtual void PushSamples ( uint32_t stream_id,
const float * samples,
uint32_t num_frames )
pure virtual

Push decoded samples into a stream.

Parameters
stream_idThe stream to push samples to.
samplesnum_frames * channels interleaved float values.
num_framesThe number of frames (one sample per channel).
Warning
When your buffer is full, wait for room instead of dropping samples. Return as soon as Flush() or Stop() is called for this stream.

◆ SetPaused()

virtual void SetPaused ( uint32_t stream_id,
bool paused )
pure virtual

Pause or resume a stream.

Keep the buffered samples while paused so playback resumes where it left off.

Parameters
stream_idThe stream to pause or resume.
pausedtrue to pause, false to resume.

◆ SetVolume()

virtual void SetVolume ( uint32_t stream_id,
float volume )
pure virtual

Set the volume of a stream.

Parameters
stream_idThe stream to change.
volumeThe volume, from 0.0 (silent) to 1.0 (full volume).

◆ Stop()

virtual void Stop ( uint32_t stream_id)
pure virtual

Tell a stream that no more samples are coming.

Samples already in your buffer can finish playing. The stream stays valid until DestroyStream().

Parameters
stream_idThe stream to stop.
Warning
A PushSamples() call waiting for room must return without writing its samples.

The documentation for this class was generated from the following file: