Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright © 2011 Mozilla Foundation |
michael@0 | 3 | * |
michael@0 | 4 | * This program is made available under an ISC-style license. See the |
michael@0 | 5 | * accompanying file LICENSE for details. |
michael@0 | 6 | */ |
michael@0 | 7 | #if !defined(CUBEB_c2f983e9_c96f_e71c_72c3_bbf62992a382) |
michael@0 | 8 | #define CUBEB_c2f983e9_c96f_e71c_72c3_bbf62992a382 |
michael@0 | 9 | |
michael@0 | 10 | #include <cubeb/cubeb-stdint.h> |
michael@0 | 11 | |
michael@0 | 12 | #if defined(__cplusplus) |
michael@0 | 13 | extern "C" { |
michael@0 | 14 | #endif |
michael@0 | 15 | |
michael@0 | 16 | /** @mainpage |
michael@0 | 17 | |
michael@0 | 18 | @section intro Introduction |
michael@0 | 19 | |
michael@0 | 20 | This is the documentation for the <tt>libcubeb</tt> C API. |
michael@0 | 21 | <tt>libcubeb</tt> is a callback-based audio API library allowing the |
michael@0 | 22 | authoring of portable multiplatform audio playback. |
michael@0 | 23 | |
michael@0 | 24 | @section example Example code |
michael@0 | 25 | |
michael@0 | 26 | @code |
michael@0 | 27 | cubeb * app_ctx; |
michael@0 | 28 | cubeb_init(&app_ctx, "Example Application"); |
michael@0 | 29 | |
michael@0 | 30 | cubeb_stream_params params; |
michael@0 | 31 | params.format = CUBEB_SAMPLE_S16NE; |
michael@0 | 32 | params.rate = 48000; |
michael@0 | 33 | params.channels = 2; |
michael@0 | 34 | |
michael@0 | 35 | unsigned int latency_ms = 250; |
michael@0 | 36 | |
michael@0 | 37 | cubeb_stream * stm; |
michael@0 | 38 | cubeb_stream_init(app_ctx, &stm, "Example Stream 1", params, |
michael@0 | 39 | latency_ms, data_cb, state_cb, NULL); |
michael@0 | 40 | |
michael@0 | 41 | cubeb_stream_start(stm); |
michael@0 | 42 | for (;;) { |
michael@0 | 43 | cubeb_stream_get_position(stm, &ts); |
michael@0 | 44 | printf("time=%llu\n", ts); |
michael@0 | 45 | sleep(1); |
michael@0 | 46 | } |
michael@0 | 47 | cubeb_stream_stop(stm); |
michael@0 | 48 | |
michael@0 | 49 | cubeb_stream_destroy(stm); |
michael@0 | 50 | cubeb_destroy(app_ctx); |
michael@0 | 51 | @endcode |
michael@0 | 52 | |
michael@0 | 53 | @code |
michael@0 | 54 | long data_cb(cubeb_stream * stm, void * user, void * buffer, long nframes) |
michael@0 | 55 | { |
michael@0 | 56 | short * buf = buffer; |
michael@0 | 57 | for (i = 0; i < nframes; ++i) { |
michael@0 | 58 | for (c = 0; c < params.channels; ++c) { |
michael@0 | 59 | buf[i][c] = 0; |
michael@0 | 60 | } |
michael@0 | 61 | } |
michael@0 | 62 | return nframes; |
michael@0 | 63 | } |
michael@0 | 64 | @endcode |
michael@0 | 65 | |
michael@0 | 66 | @code |
michael@0 | 67 | void state_cb(cubeb_stream * stm, void * user, cubeb_state state) |
michael@0 | 68 | { |
michael@0 | 69 | printf("state=%d\n", state); |
michael@0 | 70 | } |
michael@0 | 71 | @endcode |
michael@0 | 72 | */ |
michael@0 | 73 | |
michael@0 | 74 | /** @file |
michael@0 | 75 | The <tt>libcubeb</tt> C API. */ |
michael@0 | 76 | |
michael@0 | 77 | typedef struct cubeb cubeb; /**< Opaque handle referencing the application state. */ |
michael@0 | 78 | typedef struct cubeb_stream cubeb_stream; /**< Opaque handle referencing the stream state. */ |
michael@0 | 79 | |
michael@0 | 80 | /** Sample format enumeration. */ |
michael@0 | 81 | typedef enum { |
michael@0 | 82 | /**< Little endian 16-bit signed PCM. */ |
michael@0 | 83 | CUBEB_SAMPLE_S16LE, |
michael@0 | 84 | /**< Big endian 16-bit signed PCM. */ |
michael@0 | 85 | CUBEB_SAMPLE_S16BE, |
michael@0 | 86 | /**< Little endian 32-bit IEEE floating point PCM. */ |
michael@0 | 87 | CUBEB_SAMPLE_FLOAT32LE, |
michael@0 | 88 | /**< Big endian 32-bit IEEE floating point PCM. */ |
michael@0 | 89 | CUBEB_SAMPLE_FLOAT32BE, |
michael@0 | 90 | #if defined(WORDS_BIGENDIAN) || defined(__BIG_ENDIAN__) |
michael@0 | 91 | /**< Native endian 16-bit signed PCM. */ |
michael@0 | 92 | CUBEB_SAMPLE_S16NE = CUBEB_SAMPLE_S16BE, |
michael@0 | 93 | /**< Native endian 32-bit IEEE floating point PCM. */ |
michael@0 | 94 | CUBEB_SAMPLE_FLOAT32NE = CUBEB_SAMPLE_FLOAT32BE |
michael@0 | 95 | #else |
michael@0 | 96 | /**< Native endian 16-bit signed PCM. */ |
michael@0 | 97 | CUBEB_SAMPLE_S16NE = CUBEB_SAMPLE_S16LE, |
michael@0 | 98 | /**< Native endian 32-bit IEEE floating point PCM. */ |
michael@0 | 99 | CUBEB_SAMPLE_FLOAT32NE = CUBEB_SAMPLE_FLOAT32LE |
michael@0 | 100 | #endif |
michael@0 | 101 | } cubeb_sample_format; |
michael@0 | 102 | |
michael@0 | 103 | #if defined(__ANDROID__) |
michael@0 | 104 | typedef enum { |
michael@0 | 105 | CUBEB_STREAM_TYPE_VOICE_CALL = 0, |
michael@0 | 106 | CUBEB_STREAM_TYPE_SYSTEM = 1, |
michael@0 | 107 | CUBEB_STREAM_TYPE_RING = 2, |
michael@0 | 108 | CUBEB_STREAM_TYPE_MUSIC = 3, |
michael@0 | 109 | CUBEB_STREAM_TYPE_ALARM = 4, |
michael@0 | 110 | CUBEB_STREAM_TYPE_NOTIFICATION = 5, |
michael@0 | 111 | CUBEB_STREAM_TYPE_BLUETOOTH_SCO = 6, |
michael@0 | 112 | CUBEB_STREAM_TYPE_ENFORCED_AUDIBLE = 7, |
michael@0 | 113 | CUBEB_STREAM_TYPE_DTMF = 8, |
michael@0 | 114 | CUBEB_STREAM_TYPE_TTS = 9, |
michael@0 | 115 | CUBEB_STREAM_TYPE_FM = 10, |
michael@0 | 116 | |
michael@0 | 117 | CUBEB_STREAM_TYPE_MAX |
michael@0 | 118 | } cubeb_stream_type; |
michael@0 | 119 | #endif |
michael@0 | 120 | |
michael@0 | 121 | /** Stream format initialization parameters. */ |
michael@0 | 122 | typedef struct { |
michael@0 | 123 | cubeb_sample_format format; /**< Requested sample format. One of |
michael@0 | 124 | #cubeb_sample_format. */ |
michael@0 | 125 | unsigned int rate; /**< Requested sample rate. Valid range is [1, 192000]. */ |
michael@0 | 126 | unsigned int channels; /**< Requested channel count. Valid range is [1, 32]. */ |
michael@0 | 127 | #if defined(__ANDROID__) |
michael@0 | 128 | cubeb_stream_type stream_type; /**< Used to map Android audio stream types */ |
michael@0 | 129 | #endif |
michael@0 | 130 | } cubeb_stream_params; |
michael@0 | 131 | |
michael@0 | 132 | /** Stream states signaled via state_callback. */ |
michael@0 | 133 | typedef enum { |
michael@0 | 134 | CUBEB_STATE_STARTED, /**< Stream started. */ |
michael@0 | 135 | CUBEB_STATE_STOPPED, /**< Stream stopped. */ |
michael@0 | 136 | CUBEB_STATE_DRAINED, /**< Stream drained. */ |
michael@0 | 137 | CUBEB_STATE_ERROR /**< Stream disabled due to error. */ |
michael@0 | 138 | } cubeb_state; |
michael@0 | 139 | |
michael@0 | 140 | /** Result code enumeration. */ |
michael@0 | 141 | enum { |
michael@0 | 142 | CUBEB_OK = 0, /**< Success. */ |
michael@0 | 143 | CUBEB_ERROR = -1, /**< Unclassified error. */ |
michael@0 | 144 | CUBEB_ERROR_INVALID_FORMAT = -2, /**< Unsupported #cubeb_stream_params requested. */ |
michael@0 | 145 | CUBEB_ERROR_INVALID_PARAMETER = -3 /**< Invalid parameter specified. */ |
michael@0 | 146 | }; |
michael@0 | 147 | |
michael@0 | 148 | /** User supplied data callback. |
michael@0 | 149 | @param stream |
michael@0 | 150 | @param user_ptr |
michael@0 | 151 | @param buffer |
michael@0 | 152 | @param nframes |
michael@0 | 153 | @retval Number of frames written to buffer, which must equal nframes except at end of stream. |
michael@0 | 154 | @retval CUBEB_ERROR on error, in which case the data callback will stop |
michael@0 | 155 | and the stream will enter a shutdown state. */ |
michael@0 | 156 | typedef long (* cubeb_data_callback)(cubeb_stream * stream, |
michael@0 | 157 | void * user_ptr, |
michael@0 | 158 | void * buffer, |
michael@0 | 159 | long nframes); |
michael@0 | 160 | |
michael@0 | 161 | /** User supplied state callback. |
michael@0 | 162 | @param stream |
michael@0 | 163 | @param user_ptr |
michael@0 | 164 | @param state */ |
michael@0 | 165 | typedef void (* cubeb_state_callback)(cubeb_stream * stream, |
michael@0 | 166 | void * user_ptr, |
michael@0 | 167 | cubeb_state state); |
michael@0 | 168 | |
michael@0 | 169 | /** Initialize an application context. This will perform any library or |
michael@0 | 170 | application scoped initialization. |
michael@0 | 171 | @param context |
michael@0 | 172 | @param context_name |
michael@0 | 173 | @retval CUBEB_OK |
michael@0 | 174 | @retval CUBEB_ERROR */ |
michael@0 | 175 | int cubeb_init(cubeb ** context, char const * context_name); |
michael@0 | 176 | |
michael@0 | 177 | /** Get a read-only string identifying this context's current backend. |
michael@0 | 178 | @param context |
michael@0 | 179 | @retval Read-only string identifying current backend. */ |
michael@0 | 180 | char const * cubeb_get_backend_id(cubeb * context); |
michael@0 | 181 | |
michael@0 | 182 | /** Get the maximum possible number of channels. |
michael@0 | 183 | @param context |
michael@0 | 184 | @param max_channels The maximum number of channels. |
michael@0 | 185 | @retval CUBEB_OK |
michael@0 | 186 | @retval CUBEB_ERROR_INVALID_PARAMETER |
michael@0 | 187 | @retval CUBEB_ERROR */ |
michael@0 | 188 | int cubeb_get_max_channel_count(cubeb * context, uint32_t * max_channels); |
michael@0 | 189 | |
michael@0 | 190 | /** Get the minimal latency value, in milliseconds, that is guaranteed to work |
michael@0 | 191 | when creating a stream for the specified sample rate. This is platform and |
michael@0 | 192 | backend dependant. |
michael@0 | 193 | @param context |
michael@0 | 194 | @param params On some backends, the minimum achievable latency depends on |
michael@0 | 195 | the characteristics of the stream. |
michael@0 | 196 | @param latency The latency value, in ms, to pass to cubeb_stream_init. |
michael@0 | 197 | @retval CUBEB_ERROR_INVALID_PARAMETER |
michael@0 | 198 | @retval CUBEB_OK */ |
michael@0 | 199 | int cubeb_get_min_latency(cubeb * context, cubeb_stream_params params, uint32_t * latency_ms); |
michael@0 | 200 | |
michael@0 | 201 | /** Get the preferred sample rate for this backend: this is hardware and platform |
michael@0 | 202 | dependant, and can avoid resampling, and/or trigger fastpaths. |
michael@0 | 203 | @param context |
michael@0 | 204 | @param samplerate The samplerate (in Hz) the current configuration prefers. |
michael@0 | 205 | @return CUBEB_ERROR_INVALID_PARAMETER |
michael@0 | 206 | @return CUBEB_OK */ |
michael@0 | 207 | int cubeb_get_preferred_sample_rate(cubeb * context, uint32_t * rate); |
michael@0 | 208 | |
michael@0 | 209 | /** Destroy an application context. |
michael@0 | 210 | @param context */ |
michael@0 | 211 | void cubeb_destroy(cubeb * context); |
michael@0 | 212 | |
michael@0 | 213 | /** Initialize a stream associated with the supplied application context. |
michael@0 | 214 | @param context |
michael@0 | 215 | @param stream |
michael@0 | 216 | @param stream_name |
michael@0 | 217 | @param stream_params |
michael@0 | 218 | @param latency Approximate stream latency in milliseconds. Valid range is [1, 2000]. |
michael@0 | 219 | @param data_callback Will be called to preroll data before playback is |
michael@0 | 220 | started by cubeb_stream_start. |
michael@0 | 221 | @param state_callback |
michael@0 | 222 | @param user_ptr |
michael@0 | 223 | @retval CUBEB_OK |
michael@0 | 224 | @retval CUBEB_ERROR |
michael@0 | 225 | @retval CUBEB_ERROR_INVALID_FORMAT */ |
michael@0 | 226 | int cubeb_stream_init(cubeb * context, cubeb_stream ** stream, char const * stream_name, |
michael@0 | 227 | cubeb_stream_params stream_params, unsigned int latency, |
michael@0 | 228 | cubeb_data_callback data_callback, |
michael@0 | 229 | cubeb_state_callback state_callback, |
michael@0 | 230 | void * user_ptr); |
michael@0 | 231 | |
michael@0 | 232 | /** Destroy a stream. |
michael@0 | 233 | @param stream */ |
michael@0 | 234 | void cubeb_stream_destroy(cubeb_stream * stream); |
michael@0 | 235 | |
michael@0 | 236 | /** Start playback. |
michael@0 | 237 | @param stream |
michael@0 | 238 | @retval CUBEB_OK |
michael@0 | 239 | @retval CUBEB_ERROR */ |
michael@0 | 240 | int cubeb_stream_start(cubeb_stream * stream); |
michael@0 | 241 | |
michael@0 | 242 | /** Stop playback. |
michael@0 | 243 | @param stream |
michael@0 | 244 | @retval CUBEB_OK |
michael@0 | 245 | @retval CUBEB_ERROR */ |
michael@0 | 246 | int cubeb_stream_stop(cubeb_stream * stream); |
michael@0 | 247 | |
michael@0 | 248 | /** Get the current stream playback position. |
michael@0 | 249 | @param stream |
michael@0 | 250 | @param position Playback position in frames. |
michael@0 | 251 | @retval CUBEB_OK |
michael@0 | 252 | @retval CUBEB_ERROR */ |
michael@0 | 253 | int cubeb_stream_get_position(cubeb_stream * stream, uint64_t * position); |
michael@0 | 254 | |
michael@0 | 255 | /** Get the latency for this stream, in frames. This is the number of frames |
michael@0 | 256 | between the time cubeb acquires the data in the callback and the listener |
michael@0 | 257 | can hear the sound. |
michael@0 | 258 | @param stream |
michael@0 | 259 | @param latency Current approximate stream latency in ms |
michael@0 | 260 | @retval CUBEB_OK |
michael@0 | 261 | @retval CUBEB_ERROR */ |
michael@0 | 262 | int cubeb_stream_get_latency(cubeb_stream * stream, uint32_t * latency); |
michael@0 | 263 | |
michael@0 | 264 | #if defined(__cplusplus) |
michael@0 | 265 | } |
michael@0 | 266 | #endif |
michael@0 | 267 | |
michael@0 | 268 | #endif /* CUBEB_c2f983e9_c96f_e71c_72c3_bbf62992a382 */ |