docs
Loading...
Searching...
No Matches
CAPI_AudioOutput.h
Go to the documentation of this file.
1///
2/// Copyright (C) 2026 Ultralight, Inc. All rights reserved.
3/// A license is required for commercial use. https://ultralig.ht
4///
5
6///
7/// @file CAPI_AudioOutput.h
8///
9/// User-defined audio output interface.
10///
11/// `#include <Ultralight/CAPI/CAPI_AudioOutput.h>`
12///
13/// The library uses this to play the sound of `<video>` and `<audio>` elements. It decodes the
14/// audio and pushes the samples to your callbacks, which play them on a device.
15///
16/// A platform-specific implementation is provided for you when you call ulCreateApp(). If you are
17/// using ulCreateRenderer() and don't provide one, media plays without sound.
18///
19/// Each callback matches the method of the same name in <Ultralight/platform/AudioOutput.h>, and
20/// the rules documented there apply here too. Read that header before implementing this one.
21///
22/// \parblock
23/// @warning The library calls these callbacks from several of its own threads. `push_samples` can
24/// still be running when `flush` or `stop` is called for the same stream, so protect
25/// each stream's state.
26/// \endparblock
27///
28/// \parblock
29/// @warning When your buffer is full, `push_samples` should wait for room instead of dropping
30/// samples. A waiting `push_samples` must return as soon as `flush` or `stop` is called
31/// for its stream, or the library can hang.
32/// \endparblock
33///
34/// @note `get_playback_position` is optional. Implement it for the most accurate audio-video
35/// sync.
36///
37/// @pre Requires the Pro edition or higher.
38///
39/// @see ulPlatformSetAudioOutput()
40///
41#ifndef ULTRALIGHT_CAPI_AUDIOOUTPUT_H
42#define ULTRALIGHT_CAPI_AUDIOOUTPUT_H
43
45
46#if UL_HAS(MEDIA)
47
48#ifdef __cplusplus
49extern "C" {
50#endif
51
52///
53/// An audio sample format (channel count and sample rate).
54///
55typedef struct {
56 unsigned int channels; ///< Number of channels (1 = mono, 2 = stereo, 6 = 5.1, 8 = 7.1).
57 unsigned int sample_rate; ///< Sample rate in Hz (eg, 48000).
59
60///
61/// The callback invoked to create a new audio stream (before its first samples are pushed).
62///
63/// Samples will arrive in `format`. If you can't create the stream, ignore later calls with its id.
64///
65typedef void (*ULAudioOutputCreateStreamCallback)(void* user_data, unsigned int stream_id,
66 ULAudioFormat format);
67
68///
69/// The callback invoked to push decoded PCM samples into a stream (`num_frames * channels`
70/// interleaved floats). When your buffer is full, wait for room instead of dropping samples.
71///
72typedef void (*ULAudioOutputPushSamplesCallback)(void* user_data, unsigned int stream_id,
73 const float* samples, unsigned int num_frames);
74
75///
76/// The callback invoked to set a stream's volume, from 0.0 (silent) to 1.0 (full volume).
77///
78typedef void (*ULAudioOutputSetVolumeCallback)(void* user_data, unsigned int stream_id,
79 float volume);
80
81///
82/// The callback invoked to pause or resume a stream. Keep the buffered samples while paused.
83///
84typedef void (*ULAudioOutputSetPausedCallback)(void* user_data, unsigned int stream_id,
85 bool paused);
86
87///
88/// The callback invoked when the media seeks, to discard a stream's buffered audio. Leave a paused
89/// stream paused.
90///
91typedef void (*ULAudioOutputFlushCallback)(void* user_data, unsigned int stream_id);
92
93///
94/// The callback invoked when no more samples are coming for a stream. The stream stays valid, and
95/// a `push_samples` call waiting for room must return.
96///
97typedef void (*ULAudioOutputStopCallback)(void* user_data, unsigned int stream_id);
98
99///
100/// The callback invoked to destroy a stream and free its resources.
101///
102typedef void (*ULAudioOutputDestroyStreamCallback)(void* user_data, unsigned int stream_id);
103
104///
105/// The callback invoked to get the number of frames of a stream the device has played since the
106/// last flush (or since the stream was created). The count must not advance while the stream is
107/// paused. Return 0 if your backend can't report this.
108///
109typedef unsigned long long (*ULAudioOutputGetPlaybackPositionCallback)(void* user_data,
110 unsigned int stream_id);
111
112///
113/// The callback invoked to get the device's preferred audio format.
114///
116
117///
118/// User-defined audio output interface.
119///
120/// You should implement each of these callbacks, then pass an instance of this struct containing
121/// your callbacks to ulPlatformSetAudioOutput().
122///
123typedef struct {
124 ///
125 /// A user-defined pointer passed to every callback (can be NULL).
126 ///
128
129 ///
130 /// Called to create a stream.
131 ///
133
134 ///
135 /// Called to push samples into a stream.
136 ///
138
139 ///
140 /// Called to set a stream's volume.
141 ///
143
144 ///
145 /// Called to pause or resume a stream.
146 ///
148
149 ///
150 /// Called to flush a stream on seek.
151 ///
153
154 ///
155 /// Called when no more samples are coming for a stream.
156 ///
158
159 ///
160 /// Called to destroy a stream.
161 ///
163
164 ///
165 /// Called to read a stream's audio clock (can be NULL, in which case the position reads 0).
166 ///
168
169 ///
170 /// Called to read the device's preferred format (can be NULL, in which case it is stereo at
171 /// 48000 Hz).
172 ///
175
176#ifdef __cplusplus
177} // extern "C"
178#endif
179
180#endif // UL_HAS(MEDIA)
181
182#endif // ULTRALIGHT_CAPI_AUDIOOUTPUT_H
void(*) ULAudioOutputFlushCallback(void *user_data, unsigned int stream_id)
The callback invoked when the media seeks, to discard a stream's buffered audio.
Definition CAPI_AudioOutput.h:91
void(*) ULAudioOutputSetPausedCallback(void *user_data, unsigned int stream_id, bool paused)
The callback invoked to pause or resume a stream.
Definition CAPI_AudioOutput.h:84
void(*) ULAudioOutputSetVolumeCallback(void *user_data, unsigned int stream_id, float volume)
The callback invoked to set a stream's volume, from 0.0 (silent) to 1.0 (full volume).
Definition CAPI_AudioOutput.h:78
void(*) ULAudioOutputStopCallback(void *user_data, unsigned int stream_id)
The callback invoked when no more samples are coming for a stream.
Definition CAPI_AudioOutput.h:97
ULAudioFormat(*) ULAudioOutputGetPreferredOutputFormatCallback(void *user_data)
The callback invoked to get the device's preferred audio format.
Definition CAPI_AudioOutput.h:115
void(*) ULAudioOutputPushSamplesCallback(void *user_data, unsigned int stream_id, const float *samples, unsigned int num_frames)
The callback invoked to push decoded PCM samples into a stream (num_frames * channels interleaved flo...
Definition CAPI_AudioOutput.h:72
unsigned long long(*) ULAudioOutputGetPlaybackPositionCallback(void *user_data, unsigned int stream_id)
The callback invoked to get the number of frames of a stream the device has played since the last flu...
Definition CAPI_AudioOutput.h:109
void(*) ULAudioOutputDestroyStreamCallback(void *user_data, unsigned int stream_id)
The callback invoked to destroy a stream and free its resources.
Definition CAPI_AudioOutput.h:102
void(*) ULAudioOutputCreateStreamCallback(void *user_data, unsigned int stream_id, ULAudioFormat format)
The callback invoked to create a new audio stream (before its first samples are pushed).
Definition CAPI_AudioOutput.h:65
Various defines and utility functions for the C API.
An audio sample format (channel count and sample rate).
Definition CAPI_AudioOutput.h:55
unsigned int sample_rate
Sample rate in Hz (eg, 48000).
Definition CAPI_AudioOutput.h:57
unsigned int channels
Number of channels (1 = mono, 2 = stereo, 6 = 5.1, 8 = 7.1).
Definition CAPI_AudioOutput.h:56
User-defined audio output interface.
Definition CAPI_AudioOutput.h:123
ULAudioOutputSetVolumeCallback set_volume
Called to set a stream's volume.
Definition CAPI_AudioOutput.h:142
void * user_data
A user-defined pointer passed to every callback (can be NULL).
Definition CAPI_AudioOutput.h:127
ULAudioOutputFlushCallback flush
Called to flush a stream on seek.
Definition CAPI_AudioOutput.h:152
ULAudioOutputStopCallback stop
Called when no more samples are coming for a stream.
Definition CAPI_AudioOutput.h:157
ULAudioOutputDestroyStreamCallback destroy_stream
Called to destroy a stream.
Definition CAPI_AudioOutput.h:162
ULAudioOutputSetPausedCallback set_paused
Called to pause or resume a stream.
Definition CAPI_AudioOutput.h:147
ULAudioOutputGetPlaybackPositionCallback get_playback_position
Called to read a stream's audio clock (can be NULL, in which case the position reads 0).
Definition CAPI_AudioOutput.h:167
ULAudioOutputGetPreferredOutputFormatCallback get_preferred_output_format
Called to read the device's preferred format (can be NULL, in which case it is stereo at 48000 Hz).
Definition CAPI_AudioOutput.h:173
ULAudioOutputCreateStreamCallback create_stream
Called to create a stream.
Definition CAPI_AudioOutput.h:132
ULAudioOutputPushSamplesCallback push_samples
Called to push samples into a stream.
Definition CAPI_AudioOutput.h:137