Home | History | Annotate | Download | only in server
      1 /* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
      2  * Use of this source code is governed by a BSD-style license that can be
      3  * found in the LICENSE file.
      4  */
      5 
      6 #ifndef AUDIO_THREAD_H_
      7 #define AUDIO_THREAD_H_
      8 
      9 #include <pthread.h>
     10 #include <stdint.h>
     11 
     12 #include "cras_iodev.h"
     13 #include "cras_types.h"
     14 
     15 struct buffer_share;
     16 struct cras_iodev;
     17 struct cras_rstream;
     18 struct dev_stream;
     19 
     20 /* Open input/output devices.
     21  *    dev - The device.
     22  *    wake_ts - When callback is needed to avoid xrun.
     23  *    coarse_rate_adjust - Hack for when the sample rate needs heavy correction.
     24  *    input_streaming - For capture, has the input started?
     25  */
     26 struct open_dev {
     27 	struct cras_iodev *dev;
     28 	struct timespec wake_ts;
     29 	int coarse_rate_adjust;
     30 	int input_streaming;
     31 	struct open_dev *prev, *next;
     32 };
     33 
     34 /* Hold communication pipes and pthread info for the thread used to play or
     35  * record audio.
     36  *    to_thread_fds - Send a message from main to running thread.
     37  *    to_main_fds - Send a synchronous response to main from running thread.
     38  *    tid - Thread ID of the running playback/capture thread.
     39  *    started - Non-zero if the thread has started successfully.
     40  *    suspended - Non-zero if the thread is suspended.
     41  *    open_devs - Lists of open input and output devices.
     42  */
     43 struct audio_thread {
     44 	int to_thread_fds[2];
     45 	int to_main_fds[2];
     46 	pthread_t tid;
     47 	int started;
     48 	int suspended;
     49 	struct open_dev *open_devs[CRAS_NUM_DIRECTIONS];
     50 
     51 };
     52 
     53 /* Callback function to be handled in main loop in audio thread.
     54  * Args:
     55  *    data - The data for callback function.
     56  */
     57 typedef int (*thread_callback)(void *data);
     58 
     59 /* Creates an audio thread.
     60  * Returns:
     61  *    A pointer to the newly create audio thread.  It must be freed by calling
     62  *    audio_thread_destroy().  Returns NULL on error.
     63  */
     64 struct audio_thread *audio_thread_create();
     65 
     66 /* Adds an open device.
     67  * Args:
     68  *    thread - The thread to add open device to.
     69  *    dev - The open device to add.
     70  */
     71 int audio_thread_add_open_dev(struct audio_thread *thread,
     72 			      struct cras_iodev *dev);
     73 
     74 /* Removes an open device.
     75  * Args:
     76  *    thread - The thread to remove open device from.
     77  *    dev - The open device to remove.
     78  */
     79 int audio_thread_rm_open_dev(struct audio_thread *thread,
     80 			     struct cras_iodev *dev);
     81 
     82 /* Adds an thread_callback to audio thread.
     83  * Args:
     84  *    fd - The file descriptor to be polled for the callback.
     85  *      The callback will be called when fd is readable.
     86  *    cb - The callback function.
     87  *    data - The data for the callback function.
     88  */
     89 void audio_thread_add_callback(int fd, thread_callback cb,
     90                                void *data);
     91 
     92 /* Adds an thread_callback to audio thread.
     93  * Args:
     94  *    fd - The file descriptor to be polled for the callback.
     95  *      The callback will be called when fd is writeable.
     96  *    cb - The callback function.
     97  *    data - The data for the callback function.
     98  */
     99 void audio_thread_add_write_callback(int fd, thread_callback cb,
    100 				     void *data);
    101 
    102 /* Removes an thread_callback from audio thread.
    103  * Args:
    104  *    fd - The file descriptor of the previous added callback.
    105  */
    106 void audio_thread_rm_callback(int fd);
    107 
    108 /* Removes a thread_callback from main thread.
    109  * Args:
    110  *     thread - The thread to remove callback from.
    111  *     fd - The file descriptor of the previous added callback.
    112  */
    113 int audio_thread_rm_callback_sync(struct audio_thread *thread, int fd);
    114 
    115 
    116 /* Enables or Disabled the callback associated with fd. */
    117 void audio_thread_enable_callback(int fd, int enabled);
    118 
    119 /* Starts a thread created with audio_thread_create.
    120  * Args:
    121  *    thread - The thread to start.
    122  * Returns:
    123  *    0 on success, return code from pthread_crate on failure.
    124  */
    125 int audio_thread_start(struct audio_thread *thread);
    126 
    127 /* Frees an audio thread created with audio_thread_create(). */
    128 void audio_thread_destroy(struct audio_thread *thread);
    129 
    130 /* Add a stream to the thread. After this call, the ownership of the stream will
    131  * be passed to the audio thread. Audio thread is responsible to release the
    132  * stream's resources.
    133  * Args:
    134  *    thread - a pointer to the audio thread.
    135  *    stream - the new stream to add.
    136  *    devs - an array of devices to attach stream.
    137  *    num_devs - number of devices in the array pointed by devs
    138  * Returns:
    139  *    zero on success, negative error from the AUDIO_THREAD enum above when an
    140  *    the thread can't be added.
    141  */
    142 int audio_thread_add_stream(struct audio_thread *thread,
    143 			    struct cras_rstream *stream,
    144 			    struct cras_iodev **devs,
    145 			    unsigned int num_devs);
    146 
    147 /* Begin draining a stream and check the draining status.
    148  * Args:
    149  *    thread - a pointer to the audio thread.
    150  *    stream - the stream to drain/remove.
    151  * Returns:
    152  *    zero if the stream is drained and can be deleted.  If the stream is not
    153  *    completely drained, then the number of milliseconds until is is drained
    154  *    are returned.
    155  */
    156 int audio_thread_drain_stream(struct audio_thread *thread,
    157 			      struct cras_rstream *stream);
    158 
    159 /* Disconnect a stream from the client.
    160  * Args:
    161  *    thread - a pointer to the audio thread.
    162  *    stream - the stream to be disconnected.
    163  *    iodev - the device to disconnect from.
    164  * Returns:
    165  *    0 on success, negative if error.
    166  */
    167 int audio_thread_disconnect_stream(struct audio_thread *thread,
    168 				   struct cras_rstream *stream,
    169 				   struct cras_iodev *iodev);
    170 
    171 /* Dumps information about all active streams to syslog. */
    172 int audio_thread_dump_thread_info(struct audio_thread *thread,
    173 				  struct audio_debug_info *info);
    174 
    175 /* Configures the global converter for output remixing. Called by main
    176  * thread. */
    177 int audio_thread_config_global_remix(struct audio_thread *thread,
    178 				     unsigned int num_channels,
    179 				     const float *coefficient);
    180 
    181 /* Gets the global remix converter. */
    182 struct cras_fmt_conv *audio_thread_get_global_remix_converter();
    183 
    184 
    185 /* Start ramping on a device.
    186  *
    187  * Ramping is started/updated in audio thread. This function lets the main
    188  * thread request that the audio thread start ramping.
    189  *
    190  * Args:
    191  *   thread - a pointer to the audio thread.
    192  *   dev - the device to start ramping.
    193  *   request - Check the docstrings of CRAS_IODEV_RAMP_REQUEST.
    194  * Returns:
    195  *    0 on success, negative if error.
    196  */
    197 int audio_thread_dev_start_ramp(struct audio_thread *thread,
    198 				struct cras_iodev *dev,
    199 				enum CRAS_IODEV_RAMP_REQUEST request);
    200 #endif /* AUDIO_THREAD_H_ */
    201