stream.h 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  1. #ifndef foostreamhfoo
  2. #define foostreamhfoo
  3. /***
  4. This file is part of PulseAudio.
  5. Copyright 2004-2006 Lennart Poettering
  6. Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
  7. PulseAudio is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU Lesser General Public License as published
  9. by the Free Software Foundation; either version 2.1 of the License,
  10. or (at your option) any later version.
  11. PulseAudio is distributed in the hope that it will be useful, but
  12. WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. General Public License for more details.
  15. You should have received a copy of the GNU Lesser General Public License
  16. along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
  17. ***/
  18. #include <sys/types.h>
  19. #include <pulse/sample.h>
  20. #include <pulse/format.h>
  21. #include <pulse/channelmap.h>
  22. #include <pulse/volume.h>
  23. #include <pulse/def.h>
  24. #include <pulse/cdecl.h>
  25. #include <pulse/operation.h>
  26. #include <pulse/context.h>
  27. #include <pulse/proplist.h>
  28. /** \page streams Audio Streams
  29. *
  30. * \section overv_sec Overview
  31. *
  32. * Audio streams form the central functionality of the sound server. Data is
  33. * routed, converted and mixed from several sources before it is passed along
  34. * to a final output. Currently, there are three forms of audio streams:
  35. *
  36. * \li Playback streams - Data flows from the client to the server.
  37. * \li Record streams - Data flows from the server to the client.
  38. * \li Upload streams - Similar to playback streams, but the data is stored in
  39. * the sample cache. See \ref scache for more information
  40. * about controlling the sample cache.
  41. *
  42. * \section create_sec Creating
  43. *
  44. * To access a stream, a pa_stream object must be created using
  45. * pa_stream_new() or pa_stream_new_extended(). pa_stream_new() is for PCM
  46. * streams only, while pa_stream_new_extended() can be used for both PCM and
  47. * compressed audio streams. At this point the application must specify what
  48. * stream format(s) it supports. See \ref sample and \ref channelmap for more
  49. * information on the stream format parameters. FIXME: Those references only
  50. * talk about PCM parameters, we should also have an overview page for how the
  51. * pa_format_info based stream format configuration works. Bug filed:
  52. * https://bugs.freedesktop.org/show_bug.cgi?id=72265
  53. *
  54. * This first step will only create a client-side object, representing the
  55. * stream. To use the stream, a server-side object must be created and
  56. * associated with the local object. Depending on which type of stream is
  57. * desired, a different function is needed:
  58. *
  59. * \li Playback stream - pa_stream_connect_playback()
  60. * \li Record stream - pa_stream_connect_record()
  61. * \li Upload stream - pa_stream_connect_upload() (see \ref scache)
  62. *
  63. * Similar to how connections are done in contexts, connecting a stream will
  64. * not generate a pa_operation object. Also like contexts, the application
  65. * should register a state change callback, using
  66. * pa_stream_set_state_callback(), and wait for the stream to enter an active
  67. * state.
  68. *
  69. * Note: there is a user-controllable slider in mixer applications such as
  70. * pavucontrol corresponding to each of the created streams. Multiple
  71. * (especially identically named) volume sliders for the same application might
  72. * confuse the user. Also, the server supports only a limited number of
  73. * simultaneous streams. Because of this, it is not always appropriate to
  74. * create multiple streams in one application that needs to output multiple
  75. * sounds. The rough guideline is: if there is no use case that would require
  76. * separate user-initiated volume changes for each stream, perform the mixing
  77. * inside the application.
  78. *
  79. * \subsection bufattr_subsec Buffer Attributes
  80. *
  81. * Playback and record streams always have a server-side buffer as
  82. * part of the data flow. The size of this buffer needs to be chosen
  83. * in a compromise between low latency and sensitivity for buffer
  84. * overflows/underruns.
  85. *
  86. * The buffer metrics may be controlled by the application. They are
  87. * described with a pa_buffer_attr structure which contains a number
  88. * of fields:
  89. *
  90. * \li maxlength - The absolute maximum number of bytes that can be
  91. * stored in the buffer. If this value is exceeded
  92. * then data will be lost. It is recommended to pass
  93. * (uint32_t) -1 here which will cause the server to
  94. * fill in the maximum possible value.
  95. *
  96. * \li tlength - The target fill level of the playback buffer. The
  97. * server will only send requests for more data as long
  98. * as the buffer has less than this number of bytes of
  99. * data. If you pass (uint32_t) -1 (which is
  100. * recommended) here the server will choose the longest
  101. * target buffer fill level possible to minimize the
  102. * number of necessary wakeups and maximize drop-out
  103. * safety. This can exceed 2s of buffering. For
  104. * low-latency applications or applications where
  105. * latency matters you should pass a proper value here.
  106. *
  107. * \li prebuf - Number of bytes that need to be in the buffer before
  108. * playback will commence. Start of playback can be
  109. * forced using pa_stream_trigger() even though the
  110. * prebuffer size hasn't been reached. If a buffer
  111. * underrun occurs, this prebuffering will be again
  112. * enabled. If the playback shall never stop in case of a
  113. * buffer underrun, this value should be set to 0. In
  114. * that case the read index of the output buffer
  115. * overtakes the write index, and hence the fill level of
  116. * the buffer is negative. If you pass (uint32_t) -1 here
  117. * (which is recommended) the server will choose the same
  118. * value as tlength here.
  119. *
  120. * \li minreq - Minimum number of free bytes in the playback
  121. * buffer before the server will request more data. It is
  122. * recommended to fill in (uint32_t) -1 here. This value
  123. * influences how much time the sound server has to move
  124. * data from the per-stream server-side playback buffer
  125. * to the hardware playback buffer.
  126. *
  127. * \li fragsize - Maximum number of bytes that the server will push in
  128. * one chunk for record streams. If you pass (uint32_t)
  129. * -1 (which is recommended) here, the server will
  130. * choose the longest fragment setting possible to
  131. * minimize the number of necessary wakeups and
  132. * maximize drop-out safety. This can exceed 2s of
  133. * buffering. For low-latency applications or
  134. * applications where latency matters you should pass a
  135. * proper value here.
  136. *
  137. * If PA_STREAM_ADJUST_LATENCY is set, then the tlength/fragsize
  138. * parameters will be interpreted slightly differently than described
  139. * above when passed to pa_stream_connect_record() and
  140. * pa_stream_connect_playback(): the overall latency that is comprised
  141. * of both the server side playback buffer length, the hardware
  142. * playback buffer length and additional latencies will be adjusted in
  143. * a way that it matches tlength resp. fragsize. Set
  144. * PA_STREAM_ADJUST_LATENCY if you want to control the overall
  145. * playback latency for your stream. Unset it if you want to control
  146. * only the latency induced by the server-side, rewritable playback
  147. * buffer. The server will try to fulfill the client's latency requests
  148. * as good as possible. However if the underlying hardware cannot
  149. * change the hardware buffer length or only in a limited range, the
  150. * actually resulting latency might be different from what the client
  151. * requested. Thus, for synchronization clients always need to check
  152. * the actual measured latency via pa_stream_get_latency() or a
  153. * similar call, and not make any assumptions about the latency
  154. * available. The function pa_stream_get_buffer_attr() will always
  155. * return the actual size of the server-side per-stream buffer in
  156. * tlength/fragsize, regardless whether PA_STREAM_ADJUST_LATENCY is
  157. * set or not.
  158. *
  159. * The server-side per-stream playback buffers are indexed by a write and a read
  160. * index. The application writes to the write index and the sound
  161. * device reads from the read index. The read index is increased
  162. * monotonically, while the write index may be freely controlled by
  163. * the application. Subtracting the read index from the write index
  164. * will give you the current fill level of the buffer. The read/write
  165. * indexes are 64bit values and measured in bytes, they will never
  166. * wrap. The current read/write index may be queried using
  167. * pa_stream_get_timing_info() (see below for more information). In
  168. * case of a buffer underrun the read index is equal or larger than
  169. * the write index. Unless the prebuf value is 0, PulseAudio will
  170. * temporarily pause playback in such a case, and wait until the
  171. * buffer is filled up to prebuf bytes again. If prebuf is 0, the
  172. * read index may be larger than the write index, in which case
  173. * silence is played. If the application writes data to indexes lower
  174. * than the read index, the data is immediately lost.
  175. *
  176. * \section transfer_sec Transferring Data
  177. *
  178. * Once the stream is up, data can start flowing between the client and the
  179. * server. Two different access models can be used to transfer the data:
  180. *
  181. * \li Asynchronous - The application register a callback using
  182. * pa_stream_set_write_callback() and
  183. * pa_stream_set_read_callback() to receive notifications
  184. * that data can either be written or read.
  185. * \li Polled - Query the library for available data/space using
  186. * pa_stream_writable_size() and pa_stream_readable_size() and
  187. * transfer data as needed. The sizes are stored locally, in the
  188. * client end, so there is no delay when reading them.
  189. *
  190. * It is also possible to mix the two models freely.
  191. *
  192. * Once there is data/space available, it can be transferred using either
  193. * pa_stream_write() for playback, or pa_stream_peek() / pa_stream_drop() for
  194. * record. Make sure you do not overflow the playback buffers as data will be
  195. * dropped.
  196. *
  197. * \section bufctl_sec Buffer Control
  198. *
  199. * The transfer buffers can be controlled through a number of operations:
  200. *
  201. * \li pa_stream_cork() - Start or stop the playback or recording.
  202. * \li pa_stream_trigger() - Start playback immediately and do not wait for
  203. * the buffer to fill up to the set trigger level.
  204. * \li pa_stream_prebuf() - Reenable the playback trigger level.
  205. * \li pa_stream_drain() - Wait for the playback buffer to go empty. Will
  206. * return a pa_operation object that will indicate when
  207. * the buffer is completely drained.
  208. * \li pa_stream_flush() - Drop all data from the playback or record buffer. Do not
  209. * wait for it to finish playing.
  210. *
  211. * \section seek_modes Seeking in the Playback Buffer
  212. *
  213. * A client application may freely seek in the playback buffer. To
  214. * accomplish that the pa_stream_write() function takes a seek mode
  215. * and an offset argument. The seek mode is one of:
  216. *
  217. * \li PA_SEEK_RELATIVE - seek relative to the current write index
  218. * \li PA_SEEK_ABSOLUTE - seek relative to the beginning of the playback buffer, (i.e. the first that was ever played in the stream)
  219. * \li PA_SEEK_RELATIVE_ON_READ - seek relative to the current read index. Use this to write data to the output buffer that should be played as soon as possible
  220. * \li PA_SEEK_RELATIVE_END - seek relative to the last byte ever written.
  221. *
  222. * If an application just wants to append some data to the output
  223. * buffer, PA_SEEK_RELATIVE and an offset of 0 should be used.
  224. *
  225. * After a call to pa_stream_write() the write index will be left at
  226. * the position right after the last byte of the written data.
  227. *
  228. * \section latency_sec Latency
  229. *
  230. * A major problem with networked audio is the increased latency caused by
  231. * the network. To remedy this, PulseAudio supports an advanced system of
  232. * monitoring the current latency.
  233. *
  234. * To get the raw data needed to calculate latencies, call
  235. * pa_stream_get_timing_info(). This will give you a pa_timing_info
  236. * structure that contains everything that is known about the server
  237. * side buffer transport delays and the backend active in the
  238. * server. (Besides other things it contains the write and read index
  239. * values mentioned above.)
  240. *
  241. * This structure is updated every time a
  242. * pa_stream_update_timing_info() operation is executed. (i.e. before
  243. * the first call to this function the timing information structure is
  244. * not available!) Since it is a lot of work to keep this structure
  245. * up-to-date manually, PulseAudio can do that automatically for you:
  246. * if PA_STREAM_AUTO_TIMING_UPDATE is passed when connecting the
  247. * stream PulseAudio will automatically update the structure every
  248. * 100ms and every time a function is called that might invalidate the
  249. * previously known timing data (such as pa_stream_write() or
  250. * pa_stream_flush()). Please note however, that there always is a
  251. * short time window when the data in the timing information structure
  252. * is out-of-date. PulseAudio tries to mark these situations by
  253. * setting the write_index_corrupt and read_index_corrupt fields
  254. * accordingly.
  255. *
  256. * The raw timing data in the pa_timing_info structure is usually hard
  257. * to deal with. Therefore a simpler interface is available:
  258. * you can call pa_stream_get_time() or pa_stream_get_latency(). The
  259. * former will return the current playback time of the hardware since
  260. * the stream has been started. The latter returns the overall time a sample
  261. * that you write now takes to be played by the hardware. These two
  262. * functions base their calculations on the same data that is returned
  263. * by pa_stream_get_timing_info(). Hence the same rules for keeping
  264. * the timing data up-to-date apply here. In case the write or read
  265. * index is corrupted, these two functions will fail with
  266. * -PA_ERR_NODATA set.
  267. *
  268. * Since updating the timing info structure usually requires a full
  269. * network round trip and some applications monitor the timing very
  270. * often PulseAudio offers a timing interpolation system. If
  271. * PA_STREAM_INTERPOLATE_TIMING is passed when connecting the stream,
  272. * pa_stream_get_time() and pa_stream_get_latency() will try to
  273. * interpolate the current playback time/latency by estimating the
  274. * number of samples that have been played back by the hardware since
  275. * the last regular timing update. It is especially useful to combine
  276. * this option with PA_STREAM_AUTO_TIMING_UPDATE, which will enable
  277. * you to monitor the current playback time/latency very precisely and
  278. * very frequently without requiring a network round trip every time.
  279. *
  280. * \section flow_sec Overflow and underflow
  281. *
  282. * Even with the best precautions, buffers will sometime over - or
  283. * underflow. To handle this gracefully, the application can be
  284. * notified when this happens. Callbacks are registered using
  285. * pa_stream_set_overflow_callback() and
  286. * pa_stream_set_underflow_callback().
  287. *
  288. * \section sync_streams Synchronizing Multiple Playback Streams
  289. *
  290. * PulseAudio allows applications to fully synchronize multiple
  291. * playback streams that are connected to the same output device. That
  292. * means the streams will always be played back sample-by-sample
  293. * synchronously. If stream operations like pa_stream_cork() are
  294. * issued on one of the synchronized streams, they are simultaneously
  295. * issued on the others.
  296. *
  297. * To synchronize a stream to another, just pass the "master" stream
  298. * as last argument to pa_stream_connect_playback(). To make sure that
  299. * the freshly created stream doesn't start playback right-away, make
  300. * sure to pass PA_STREAM_START_CORKED and -- after all streams have
  301. * been created -- uncork them all with a single call to
  302. * pa_stream_cork() for the master stream.
  303. *
  304. * To make sure that a particular stream doesn't stop to play when a
  305. * server side buffer underrun happens on it while the other
  306. * synchronized streams continue playing and hence deviate, you need to
  307. * pass a "prebuf" pa_buffer_attr of 0 when connecting it.
  308. *
  309. * \section disc_sec Disconnecting
  310. *
  311. * When a stream has served is purpose it must be disconnected with
  312. * pa_stream_disconnect(). If you only unreference it, then it will live on
  313. * and eat resources both locally and on the server until you disconnect the
  314. * context.
  315. *
  316. */
  317. /** \file
  318. * Audio streams for input, output and sample upload
  319. *
  320. * See also \subpage streams
  321. */
  322. PA_C_DECL_BEGIN
  323. /** An opaque stream for playback or recording */
  324. typedef struct pa_stream pa_stream;
  325. /** A generic callback for operation completion */
  326. typedef void (*pa_stream_success_cb_t) (pa_stream*s, int success, void *userdata);
  327. /** A generic request callback */
  328. typedef void (*pa_stream_request_cb_t)(pa_stream *p, size_t nbytes, void *userdata);
  329. /** A generic notification callback */
  330. typedef void (*pa_stream_notify_cb_t)(pa_stream *p, void *userdata);
  331. /** A callback for asynchronous meta/policy event messages. Well known
  332. * event names are PA_STREAM_EVENT_REQUEST_CORK and
  333. * PA_STREAM_EVENT_REQUEST_UNCORK. The set of defined events can be
  334. * extended at any time. Also, server modules may introduce additional
  335. * message types so make sure that your callback function ignores messages
  336. * it doesn't know. \since 0.9.15 */
  337. typedef void (*pa_stream_event_cb_t)(pa_stream *p, const char *name, pa_proplist *pl, void *userdata);
  338. /** Create a new, unconnected stream with the specified name and
  339. * sample type. It is recommended to use pa_stream_new_with_proplist()
  340. * instead and specify some initial properties. */
  341. pa_stream* pa_stream_new(
  342. pa_context *c /**< The context to create this stream in */,
  343. const char *name /**< A name for this stream */,
  344. const pa_sample_spec *ss /**< The desired sample format */,
  345. const pa_channel_map *map /**< The desired channel map, or NULL for default */);
  346. /** Create a new, unconnected stream with the specified name and
  347. * sample type, and specify the initial stream property
  348. * list. \since 0.9.11 */
  349. pa_stream* pa_stream_new_with_proplist(
  350. pa_context *c /**< The context to create this stream in */,
  351. const char *name /**< A name for this stream */,
  352. const pa_sample_spec *ss /**< The desired sample format */,
  353. const pa_channel_map *map /**< The desired channel map, or NULL for default */,
  354. pa_proplist *p /**< The initial property list */);
  355. /** Create a new, unconnected stream with the specified name, the set of formats
  356. * this client can provide, and an initial list of properties. While
  357. * connecting, the server will select the most appropriate format which the
  358. * client must then provide. \since 1.0 */
  359. pa_stream *pa_stream_new_extended(
  360. pa_context *c /**< The context to create this stream in */,
  361. const char *name /**< A name for this stream */,
  362. pa_format_info * const * formats /**< The list of formats that can be provided */,
  363. unsigned int n_formats /**< The number of formats being passed in */,
  364. pa_proplist *p /**< The initial property list */);
  365. /** Decrease the reference counter by one. */
  366. void pa_stream_unref(pa_stream *s);
  367. /** Increase the reference counter by one. */
  368. pa_stream *pa_stream_ref(pa_stream *s);
  369. /** Return the current state of the stream. */
  370. pa_stream_state_t pa_stream_get_state(pa_stream *p);
  371. /** Return the context this stream is attached to. */
  372. pa_context* pa_stream_get_context(pa_stream *p);
  373. /** Return the sink input resp.\ source output index this stream is
  374. * identified in the server with. This is useful with the
  375. * introspection functions such as pa_context_get_sink_input_info()
  376. * or pa_context_get_source_output_info(). */
  377. uint32_t pa_stream_get_index(pa_stream *s);
  378. /** Return the index of the sink or source this stream is connected to
  379. * in the server. This is useful with the introspection
  380. * functions such as pa_context_get_sink_info_by_index() or
  381. * pa_context_get_source_info_by_index().
  382. *
  383. * Please note that streams may be moved between sinks/sources and thus
  384. * it is recommended to use pa_stream_set_moved_callback() to be notified
  385. * about this. This function will return with -PA_ERR_NOTSUPPORTED when the
  386. * server is older than 0.9.8. \since 0.9.8 */
  387. uint32_t pa_stream_get_device_index(pa_stream *s);
  388. /** Return the name of the sink or source this stream is connected to
  389. * in the server. This is useful with the introspection
  390. * functions such as pa_context_get_sink_info_by_name()
  391. * or pa_context_get_source_info_by_name().
  392. *
  393. * Please note that streams may be moved between sinks/sources and thus
  394. * it is recommended to use pa_stream_set_moved_callback() to be notified
  395. * about this. This function will return with -PA_ERR_NOTSUPPORTED when the
  396. * server is older than 0.9.8. \since 0.9.8 */
  397. const char *pa_stream_get_device_name(pa_stream *s);
  398. /** Return 1 if the sink or source this stream is connected to has
  399. * been suspended. This will return 0 if not, and a negative value on
  400. * error. This function will return with -PA_ERR_NOTSUPPORTED when the
  401. * server is older than 0.9.8. \since 0.9.8 */
  402. int pa_stream_is_suspended(pa_stream *s);
  403. /** Return 1 if the this stream has been corked. This will return 0 if
  404. * not, and a negative value on error. \since 0.9.11 */
  405. int pa_stream_is_corked(pa_stream *s);
  406. /** Connect the stream to a sink. It is strongly recommended to pass
  407. * NULL in both \a dev and \a volume and to set neither
  408. * PA_STREAM_START_MUTED nor PA_STREAM_START_UNMUTED -- unless these
  409. * options are directly dependent on user input or configuration.
  410. *
  411. * If you follow this rule then the sound server will have the full
  412. * flexibility to choose the device, volume and mute status
  413. * automatically, based on server-side policies, heuristics and stored
  414. * information from previous uses. Also the server may choose to
  415. * reconfigure audio devices to make other sinks/sources or
  416. * capabilities available to be able to accept the stream.
  417. *
  418. * Before 0.9.20 it was not defined whether the \a volume parameter was
  419. * interpreted relative to the sink's current volume or treated as
  420. * an absolute device volume. Since 0.9.20 it is an absolute volume when
  421. * the sink is in flat volume mode, and relative otherwise, thus
  422. * making sure the volume passed here has always the same semantics as
  423. * the volume passed to pa_context_set_sink_input_volume(). It is possible
  424. * to figure out whether flat volume mode is in effect for a given sink
  425. * by calling pa_context_get_sink_info_by_name().
  426. *
  427. * Since 5.0, it's possible to specify a single-channel volume even if the
  428. * stream has multiple channels. In that case the same volume is applied to all
  429. * channels. */
  430. int pa_stream_connect_playback(
  431. pa_stream *s /**< The stream to connect to a sink */,
  432. const char *dev /**< Name of the sink to connect to, or NULL for default */ ,
  433. const pa_buffer_attr *attr /**< Buffering attributes, or NULL for default */,
  434. pa_stream_flags_t flags /**< Additional flags, or 0 for default */,
  435. const pa_cvolume *volume /**< Initial volume, or NULL for default */,
  436. pa_stream *sync_stream /**< Synchronize this stream with the specified one, or NULL for a standalone stream */);
  437. /** Connect the stream to a source. */
  438. int pa_stream_connect_record(
  439. pa_stream *s /**< The stream to connect to a source */ ,
  440. const char *dev /**< Name of the source to connect to, or NULL for default */,
  441. const pa_buffer_attr *attr /**< Buffer attributes, or NULL for default */,
  442. pa_stream_flags_t flags /**< Additional flags, or 0 for default */);
  443. /** Disconnect a stream from a source/sink. */
  444. int pa_stream_disconnect(pa_stream *s);
  445. /** Prepare writing data to the server (for playback streams). This
  446. * function may be used to optimize the number of memory copies when
  447. * doing playback ("zero-copy"). It is recommended to call this
  448. * function before each call to pa_stream_write().
  449. *
  450. * Pass in the address to a pointer and an address of the number of
  451. * bytes you want to write. On return the two values will contain a
  452. * pointer where you can place the data to write and the maximum number
  453. * of bytes you can write. \a *nbytes can be smaller or have the same
  454. * value as you passed in. You need to be able to handle both cases.
  455. * Accessing memory beyond the returned \a *nbytes value is invalid.
  456. * Accessing the memory returned after the following pa_stream_write()
  457. * or pa_stream_cancel_write() is invalid.
  458. *
  459. * On invocation only \a *nbytes needs to be initialized, on return both
  460. * *data and *nbytes will be valid. If you place (size_t) -1 in *nbytes
  461. * on invocation the memory size will be chosen automatically (which is
  462. * recommended to do). After placing your data in the memory area
  463. * returned, call pa_stream_write() with \a data set to an address
  464. * within this memory area and an \a nbytes value that is smaller or
  465. * equal to what was returned by this function to actually execute the
  466. * write.
  467. *
  468. * An invocation of pa_stream_write() should follow "quickly" on
  469. * pa_stream_begin_write(). It is not recommended letting an unbounded
  470. * amount of time pass after calling pa_stream_begin_write() and
  471. * before calling pa_stream_write(). If you want to cancel a
  472. * previously called pa_stream_begin_write() without calling
  473. * pa_stream_write() use pa_stream_cancel_write(). Calling
  474. * pa_stream_begin_write() twice without calling pa_stream_write() or
  475. * pa_stream_cancel_write() in between will return exactly the same
  476. * \a data pointer and \a nbytes values. \since 0.9.16 */
  477. int pa_stream_begin_write(
  478. pa_stream *p,
  479. void **data,
  480. size_t *nbytes);
  481. /** Reverses the effect of pa_stream_begin_write() dropping all data
  482. * that has already been placed in the memory area returned by
  483. * pa_stream_begin_write(). Only valid to call if
  484. * pa_stream_begin_write() was called before and neither
  485. * pa_stream_cancel_write() nor pa_stream_write() have been called
  486. * yet. Accessing the memory previously returned by
  487. * pa_stream_begin_write() after this call is invalid. Any further
  488. * explicit freeing of the memory area is not necessary. \since
  489. * 0.9.16 */
  490. int pa_stream_cancel_write(
  491. pa_stream *p);
  492. /** Write some data to the server (for playback streams).
  493. * If \a free_cb is non-NULL this routine is called when all data has
  494. * been written out. An internal reference to the specified data is
  495. * kept, the data is not copied. If NULL, the data is copied into an
  496. * internal buffer.
  497. *
  498. * The client may freely seek around in the output buffer. For
  499. * most applications it is typical to pass 0 and PA_SEEK_RELATIVE
  500. * as values for the arguments \a offset and \a seek. After the write
  501. * call succeeded the write index will be at the position after where
  502. * this chunk of data has been written to.
  503. *
  504. * As an optimization for avoiding needless memory copies you may call
  505. * pa_stream_begin_write() before this call and then place your audio
  506. * data directly in the memory area returned by that call. Then, pass
  507. * a pointer to that memory area to pa_stream_write(). After the
  508. * invocation of pa_stream_write() the memory area may no longer be
  509. * accessed. Any further explicit freeing of the memory area is not
  510. * necessary. It is OK to write the memory area returned by
  511. * pa_stream_begin_write() only partially with this call, skipping
  512. * bytes both at the end and at the beginning of the reserved memory
  513. * area.*/
  514. int pa_stream_write(
  515. pa_stream *p /**< The stream to use */,
  516. const void *data /**< The data to write */,
  517. size_t nbytes /**< The length of the data to write in bytes, must be in multiples of the stream's sample spec frame size */,
  518. pa_free_cb_t free_cb /**< A cleanup routine for the data or NULL to request an internal copy */,
  519. int64_t offset /**< Offset for seeking, must be 0 for upload streams, must be in multiples of the stream's sample spec frame size */,
  520. pa_seek_mode_t seek /**< Seek mode, must be PA_SEEK_RELATIVE for upload streams */);
  521. /** Function does exactly the same as pa_stream_write() with the difference
  522. * that free_cb_data is passed to free_cb instead of data. \since 6.0 */
  523. int pa_stream_write_ext_free(
  524. pa_stream *p /**< The stream to use */,
  525. const void *data /**< The data to write */,
  526. size_t nbytes /**< The length of the data to write in bytes */,
  527. pa_free_cb_t free_cb /**< A cleanup routine for the data or NULL to request an internal copy */,
  528. void *free_cb_data /**< Argument passed to free_cb function */,
  529. int64_t offset /**< Offset for seeking, must be 0 for upload streams */,
  530. pa_seek_mode_t seek /**< Seek mode, must be PA_SEEK_RELATIVE for upload streams */);
  531. /** Read the next fragment from the buffer (for recording streams).
  532. * If there is data at the current read index, \a data will point to
  533. * the actual data and \a nbytes will contain the size of the data in
  534. * bytes (which can be less or more than a complete fragment).
  535. *
  536. * If there is no data at the current read index, it means that either
  537. * the buffer is empty or it contains a hole (that is, the write index
  538. * is ahead of the read index but there's no data where the read index
  539. * points at). If the buffer is empty, \a data will be NULL and
  540. * \a nbytes will be 0. If there is a hole, \a data will be NULL and
  541. * \a nbytes will contain the length of the hole.
  542. *
  543. * Use pa_stream_drop() to actually remove the data from the buffer
  544. * and move the read index forward. pa_stream_drop() should not be
  545. * called if the buffer is empty, but it should be called if there is
  546. * a hole. */
  547. int pa_stream_peek(
  548. pa_stream *p /**< The stream to use */,
  549. const void **data /**< Pointer to pointer that will point to data */,
  550. size_t *nbytes /**< The length of the data read in bytes */);
  551. /** Remove the current fragment on record streams. It is invalid to do this without first
  552. * calling pa_stream_peek(). */
  553. int pa_stream_drop(pa_stream *p);
  554. /** Return the number of bytes requested by the server that have not yet
  555. * been written.
  556. *
  557. * It is possible to write more than this amount, up to the stream's
  558. * buffer_attr.maxlength bytes. This is usually not desirable, though, as
  559. * it would increase stream latency to be higher than requested
  560. * (buffer_attr.tlength).
  561. */
  562. size_t pa_stream_writable_size(pa_stream *p);
  563. /** Return the number of bytes that may be read using pa_stream_peek(). */
  564. size_t pa_stream_readable_size(pa_stream *p);
  565. /** Drain a playback stream. Use this for notification when the
  566. * playback buffer is empty after playing all the audio in the buffer.
  567. * Please note that only one drain operation per stream may be issued
  568. * at a time. */
  569. pa_operation* pa_stream_drain(pa_stream *s, pa_stream_success_cb_t cb, void *userdata);
  570. /** Request a timing info structure update for a stream. Use
  571. * pa_stream_get_timing_info() to get access to the raw timing data,
  572. * or pa_stream_get_time() or pa_stream_get_latency() to get cleaned
  573. * up values. */
  574. pa_operation* pa_stream_update_timing_info(pa_stream *p, pa_stream_success_cb_t cb, void *userdata);
  575. /** Set the callback function that is called whenever the state of the stream changes. */
  576. void pa_stream_set_state_callback(pa_stream *s, pa_stream_notify_cb_t cb, void *userdata);
  577. /** Set the callback function that is called when new data may be
  578. * written to the stream. */
  579. void pa_stream_set_write_callback(pa_stream *p, pa_stream_request_cb_t cb, void *userdata);
  580. /** Set the callback function that is called when new data is available from the stream. */
  581. void pa_stream_set_read_callback(pa_stream *p, pa_stream_request_cb_t cb, void *userdata);
  582. /** Set the callback function that is called when a buffer overflow happens. (Only for playback streams) */
  583. void pa_stream_set_overflow_callback(pa_stream *p, pa_stream_notify_cb_t cb, void *userdata);
  584. /** Return at what position the latest underflow occurred, or -1 if this information is not
  585. * known (e.g.\ if no underflow has occurred, or server is older than 1.0).
  586. * Can be used inside the underflow callback to get information about the current underflow.
  587. * (Only for playback streams) \since 1.0 */
  588. int64_t pa_stream_get_underflow_index(pa_stream *p);
  589. /** Set the callback function that is called when a buffer underflow happens. (Only for playback streams) */
  590. void pa_stream_set_underflow_callback(pa_stream *p, pa_stream_notify_cb_t cb, void *userdata);
  591. /** Set the callback function that is called when a the server starts
  592. * playback after an underrun or on initial startup. This only informs
  593. * that audio is flowing again, it is no indication that audio started
  594. * to reach the speakers already. (Only for playback streams) \since
  595. * 0.9.11 */
  596. void pa_stream_set_started_callback(pa_stream *p, pa_stream_notify_cb_t cb, void *userdata);
  597. /** Set the callback function that is called whenever a latency
  598. * information update happens. Useful on PA_STREAM_AUTO_TIMING_UPDATE
  599. * streams only. */
  600. void pa_stream_set_latency_update_callback(pa_stream *p, pa_stream_notify_cb_t cb, void *userdata);
  601. /** Set the callback function that is called whenever the stream is
  602. * moved to a different sink/source. Use pa_stream_get_device_name() or
  603. * pa_stream_get_device_index() to query the new sink/source. This
  604. * notification is only generated when the server is at least
  605. * 0.9.8. \since 0.9.8 */
  606. void pa_stream_set_moved_callback(pa_stream *p, pa_stream_notify_cb_t cb, void *userdata);
  607. /** Set the callback function that is called whenever the sink/source
  608. * this stream is connected to is suspended or resumed. Use
  609. * pa_stream_is_suspended() to query the new suspend status. Please
  610. * note that the suspend status might also change when the stream is
  611. * moved between devices. Thus if you call this function you very
  612. * likely want to call pa_stream_set_moved_callback() too. This
  613. * notification is only generated when the server is at least
  614. * 0.9.8. \since 0.9.8 */
  615. void pa_stream_set_suspended_callback(pa_stream *p, pa_stream_notify_cb_t cb, void *userdata);
  616. /** Set the callback function that is called whenever a meta/policy
  617. * control event is received. \since 0.9.15 */
  618. void pa_stream_set_event_callback(pa_stream *p, pa_stream_event_cb_t cb, void *userdata);
  619. /** Set the callback function that is called whenever the buffer
  620. * attributes on the server side change. Please note that the buffer
  621. * attributes can change when moving a stream to a different
  622. * sink/source too, hence if you use this callback you should use
  623. * pa_stream_set_moved_callback() as well. \since 0.9.15 */
  624. void pa_stream_set_buffer_attr_callback(pa_stream *p, pa_stream_notify_cb_t cb, void *userdata);
  625. /** Pause (or resume) playback of this stream temporarily. Available
  626. * on both playback and recording streams. If \a b is 1 the stream is
  627. * paused. If \a b is 0 the stream is resumed. The pause/resume operation
  628. * is executed as quickly as possible. If a cork is very quickly
  629. * followed by an uncork or the other way round, this might not
  630. * actually have any effect on the stream that is output. You can use
  631. * pa_stream_is_corked() to find out whether the stream is currently
  632. * paused or not. Normally a stream will be created in uncorked
  633. * state. If you pass PA_STREAM_START_CORKED as a flag when connecting
  634. * the stream, it will be created in corked state. */
  635. pa_operation* pa_stream_cork(pa_stream *s, int b, pa_stream_success_cb_t cb, void *userdata);
  636. /** Flush the playback or record buffer of this stream. This discards any audio data
  637. * in the buffer. Most of the time you're better off using the parameter
  638. * \a seek of pa_stream_write() instead of this function. */
  639. pa_operation* pa_stream_flush(pa_stream *s, pa_stream_success_cb_t cb, void *userdata);
  640. /** Reenable prebuffering if specified in the pa_buffer_attr
  641. * structure. Available for playback streams only. */
  642. pa_operation* pa_stream_prebuf(pa_stream *s, pa_stream_success_cb_t cb, void *userdata);
  643. /** Request immediate start of playback on this stream. This disables
  644. * prebuffering temporarily if specified in the pa_buffer_attr structure.
  645. * Available for playback streams only. */
  646. pa_operation* pa_stream_trigger(pa_stream *s, pa_stream_success_cb_t cb, void *userdata);
  647. /** Rename the stream. */
  648. pa_operation* pa_stream_set_name(pa_stream *s, const char *name, pa_stream_success_cb_t cb, void *userdata);
  649. /** Return the current playback/recording time. This is based on the
  650. * data in the timing info structure returned by
  651. * pa_stream_get_timing_info().
  652. *
  653. * This function will usually only return new data if a timing info
  654. * update has been received. Only if timing interpolation has been
  655. * requested (PA_STREAM_INTERPOLATE_TIMING) the data from the last
  656. * timing update is used for an estimation of the current
  657. * playback/recording time based on the local time that passed since
  658. * the timing info structure has been acquired.
  659. *
  660. * The time value returned by this function is guaranteed to increase
  661. * monotonically (the returned value is always greater
  662. * or equal to the value returned by the last call). This behaviour
  663. * can be disabled by using PA_STREAM_NOT_MONOTONIC. This may be
  664. * desirable to better deal with bad estimations of transport
  665. * latencies, but may have strange effects if the application is not
  666. * able to deal with time going 'backwards'.
  667. *
  668. * The time interpolator activated by PA_STREAM_INTERPOLATE_TIMING
  669. * favours 'smooth' time graphs over accurate ones to improve the
  670. * smoothness of UI operations that are tied to the audio clock. If
  671. * accuracy is more important to you, you might need to estimate your
  672. * timing based on the data from pa_stream_get_timing_info() yourself
  673. * or not work with interpolated timing at all and instead always
  674. * query the server side for the most up to date timing with
  675. * pa_stream_update_timing_info().
  676. *
  677. * If no timing information has been
  678. * received yet this call will return -PA_ERR_NODATA. For more details
  679. * see pa_stream_get_timing_info(). */
  680. int pa_stream_get_time(pa_stream *s, pa_usec_t *r_usec);
  681. /** Determine the total stream latency. This function is based on
  682. * pa_stream_get_time().
  683. *
  684. * The latency is stored in \a *r_usec. In case the stream is a
  685. * monitoring stream the result can be negative, i.e. the captured
  686. * samples are not yet played. In this case \a *negative is set to 1.
  687. *
  688. * If no timing information has been received yet, this call will
  689. * return -PA_ERR_NODATA. On success, it will return 0.
  690. *
  691. * For more details see pa_stream_get_timing_info() and
  692. * pa_stream_get_time(). */
  693. int pa_stream_get_latency(pa_stream *s, pa_usec_t *r_usec, int *negative);
  694. /** Return the latest raw timing data structure. The returned pointer
  695. * refers to an internal read-only instance of the timing
  696. * structure. The user should make a copy of this structure if he
  697. * wants to modify it. An in-place update to this data structure may
  698. * be requested using pa_stream_update_timing_info().
  699. *
  700. * If no timing information has been received before (i.e. by
  701. * requesting pa_stream_update_timing_info() or by using
  702. * PA_STREAM_AUTO_TIMING_UPDATE), this function will fail with
  703. * -PA_ERR_NODATA.
  704. *
  705. * Please note that the write_index member field (and only this field)
  706. * is updated on each pa_stream_write() call, not just when a timing
  707. * update has been received. */
  708. const pa_timing_info* pa_stream_get_timing_info(pa_stream *s);
  709. /** Return a pointer to the stream's sample specification. */
  710. const pa_sample_spec* pa_stream_get_sample_spec(pa_stream *s);
  711. /** Return a pointer to the stream's channel map. */
  712. const pa_channel_map* pa_stream_get_channel_map(pa_stream *s);
  713. /** Return a pointer to the stream's format. \since 1.0 */
  714. const pa_format_info* pa_stream_get_format_info(pa_stream *s);
  715. /** Return the per-stream server-side buffer metrics of the
  716. * stream. Only valid after the stream has been connected successfully
  717. * and if the server is at least PulseAudio 0.9. This will return the
  718. * actual configured buffering metrics, which may differ from what was
  719. * requested during pa_stream_connect_record() or
  720. * pa_stream_connect_playback(). This call will always return the
  721. * actual per-stream server-side buffer metrics, regardless whether
  722. * PA_STREAM_ADJUST_LATENCY is set or not. \since 0.9.0 */
  723. const pa_buffer_attr* pa_stream_get_buffer_attr(pa_stream *s);
  724. /** Change the buffer metrics of the stream during playback. The
  725. * server might have chosen different buffer metrics then
  726. * requested. The selected metrics may be queried with
  727. * pa_stream_get_buffer_attr() as soon as the callback is called. Only
  728. * valid after the stream has been connected successfully and if the
  729. * server is at least PulseAudio 0.9.8. Please be aware of the
  730. * slightly different semantics of the call depending whether
  731. * PA_STREAM_ADJUST_LATENCY is set or not. \since 0.9.8 */
  732. pa_operation *pa_stream_set_buffer_attr(pa_stream *s, const pa_buffer_attr *attr, pa_stream_success_cb_t cb, void *userdata);
  733. /** Change the stream sampling rate during playback. You need to pass
  734. * PA_STREAM_VARIABLE_RATE in the flags parameter of
  735. * pa_stream_connect_playback() if you plan to use this function. Only valid
  736. * after the stream has been connected successfully and if the server
  737. * is at least PulseAudio 0.9.8. \since 0.9.8 */
  738. pa_operation *pa_stream_update_sample_rate(pa_stream *s, uint32_t rate, pa_stream_success_cb_t cb, void *userdata);
  739. /** Update the property list of the sink input/source output of this
  740. * stream, adding new entries. Please note that it is highly
  741. * recommended to set as many properties initially via
  742. * pa_stream_new_with_proplist() as possible instead a posteriori with
  743. * this function, since that information may be used to route
  744. * this stream to the right device. \since 0.9.11 */
  745. pa_operation *pa_stream_proplist_update(pa_stream *s, pa_update_mode_t mode, pa_proplist *p, pa_stream_success_cb_t cb, void *userdata);
  746. /** Update the property list of the sink input/source output of this
  747. * stream, remove entries. \since 0.9.11 */
  748. pa_operation *pa_stream_proplist_remove(pa_stream *s, const char *const keys[], pa_stream_success_cb_t cb, void *userdata);
  749. /** For record streams connected to a monitor source: monitor only a
  750. * very specific sink input of the sink. This function needs to be
  751. * called before pa_stream_connect_record() is called. \since
  752. * 0.9.11 */
  753. int pa_stream_set_monitor_stream(pa_stream *s, uint32_t sink_input_idx);
  754. /** Return the sink input index previously set with
  755. * pa_stream_set_monitor_stream().
  756. * \since 0.9.11 */
  757. uint32_t pa_stream_get_monitor_stream(pa_stream *s);
  758. PA_C_DECL_END
  759. #endif