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 #include "dev_io.h" 15 16 struct buffer_share; 17 struct cras_fmt_conv; 18 struct cras_iodev; 19 struct cras_rstream; 20 struct dev_stream; 21 22 /* Hold communication pipes and pthread info for the thread used to play or 23 * record audio. 24 * to_thread_fds - Send a message from main to running thread. 25 * to_main_fds - Send a synchronous response to main from running thread. 26 * tid - Thread ID of the running playback/capture thread. 27 * started - Non-zero if the thread has started successfully. 28 * suspended - Non-zero if the thread is suspended. 29 * open_devs - Lists of open input and output devices. 30 * pollfds - What FDs wake up this thread. 31 * pollfds_size - Number of available poll fds. 32 * num_pollfds - Number of currently registered poll fds. 33 * remix_converter - Format converter used to remix output channels. 34 */ 35 struct audio_thread { 36 int to_thread_fds[2]; 37 int to_main_fds[2]; 38 pthread_t tid; 39 int started; 40 int suspended; 41 struct open_dev *open_devs[CRAS_NUM_DIRECTIONS]; 42 struct pollfd *pollfds; 43 size_t pollfds_size; 44 size_t num_pollfds; 45 struct cras_fmt_conv *remix_converter; 46 }; 47 48 /* 49 * Enum to specify how a registered event callback be triggered. 50 * TRIGGER_NONE - Callback will not be triggered. 51 * TRIGGER_POLL - Triggered by poll given fd and revent. 52 * TRIGGER_WAKEUP - Triggered everytime when audio thread wakes up. 53 */ 54 enum AUDIO_THREAD_EVENTS_CB_TRIGGER { 55 TRIGGER_NONE, 56 TRIGGER_POLL, 57 TRIGGER_WAKEUP, 58 }; 59 60 /* Callback function to be handled in main loop in audio thread. 61 * Args: 62 * data - The data for callback function. 63 * revent - The returned event from ppoll(). 64 */ 65 typedef int (*thread_callback)(void *data, int revent); 66 67 /* Creates an audio thread. 68 * Returns: 69 * A pointer to the newly created audio thread. It must be freed by calling 70 * audio_thread_destroy(). Returns NULL on error. 71 */ 72 struct audio_thread *audio_thread_create(); 73 74 /* Adds an open device. 75 * Args: 76 * thread - The thread to add open device to. 77 * dev - The open device to add. 78 */ 79 int audio_thread_add_open_dev(struct audio_thread *thread, 80 struct cras_iodev *dev); 81 82 /* Removes an open device. 83 * Args: 84 * thread - The thread to remove open device from. 85 * dev - The open device to remove. 86 */ 87 int audio_thread_rm_open_dev(struct audio_thread *thread, 88 enum CRAS_STREAM_DIRECTION dir, 89 unsigned int dev_idx); 90 91 /* Checks if dev is open and used by audio thread. 92 * Args: 93 * thread - The thread accessing open devs. 94 * dev - The device to check if it has already been open. 95 */ 96 int audio_thread_is_dev_open(struct audio_thread *thread, 97 struct cras_iodev *dev); 98 99 /* Adds a thread_callback to audio thread for requested events. By default 100 * the callback trigger is set to TRIGGER_POLL. 101 * Args: 102 * fd - The file descriptor to be polled for the callback. 103 * The callback will be called when any of requested events matched. 104 * cb - The callback function. 105 * data - The data for the callback function. 106 * events - The requested events to ppoll(). 107 */ 108 void audio_thread_add_events_callback(int fd, thread_callback cb, void *data, 109 int events); 110 111 /* Removes an thread_callback from audio thread. 112 * Args: 113 * fd - The file descriptor of the previous added callback. 114 */ 115 void audio_thread_rm_callback(int fd); 116 117 /* Removes a thread_callback from main thread. 118 * Args: 119 * thread - The thread to remove callback from. 120 * fd - The file descriptor of the previous added callback. 121 */ 122 int audio_thread_rm_callback_sync(struct audio_thread *thread, int fd); 123 124 /* Configures the callback associated with fd when it should be triggerred. 125 * Args: 126 * fd - The file descriptor associate to the callback. 127 * trigger - Specifies how the callback should be triggered. 128 */ 129 void audio_thread_config_events_callback( 130 int fd, enum AUDIO_THREAD_EVENTS_CB_TRIGGER trigger); 131 132 /* Starts a thread created with audio_thread_create. 133 * Args: 134 * thread - The thread to start. 135 * Returns: 136 * 0 on success, return code from pthread_crate on failure. 137 */ 138 int audio_thread_start(struct audio_thread *thread); 139 140 /* Frees an audio thread created with audio_thread_create(). */ 141 void audio_thread_destroy(struct audio_thread *thread); 142 143 /* Returns the shm fd for the ATlog. */ 144 int audio_thread_event_log_shm_fd(); 145 146 /* Add a stream to the thread. After this call, the ownership of the stream will 147 * be passed to the audio thread. Audio thread is responsible to release the 148 * stream's resources. 149 * Args: 150 * thread - a pointer to the audio thread. 151 * stream - the new stream to add. 152 * devs - an array of devices to attach stream. 153 * num_devs - number of devices in the array pointed by devs 154 * Returns: 155 * zero on success, negative error from the AUDIO_THREAD enum above when an 156 * the thread can't be added. 157 */ 158 int audio_thread_add_stream(struct audio_thread *thread, 159 struct cras_rstream *stream, 160 struct cras_iodev **devs, unsigned int num_devs); 161 162 /* Begin draining a stream and check the draining status. 163 * Args: 164 * thread - a pointer to the audio thread. 165 * stream - the stream to drain/remove. 166 * Returns: 167 * zero if the stream is drained and can be deleted. If the stream is not 168 * completely drained, then the number of milliseconds until is is drained 169 * are returned. 170 */ 171 int audio_thread_drain_stream(struct audio_thread *thread, 172 struct cras_rstream *stream); 173 174 /* Disconnect a stream from the client. 175 * Args: 176 * thread - a pointer to the audio thread. 177 * stream - the stream to be disconnected. 178 * iodev - the device to disconnect from. 179 * Returns: 180 * 0 on success, negative if error. 181 */ 182 int audio_thread_disconnect_stream(struct audio_thread *thread, 183 struct cras_rstream *stream, 184 struct cras_iodev *iodev); 185 186 /* Dumps information about all active streams to syslog. */ 187 int audio_thread_dump_thread_info(struct audio_thread *thread, 188 struct audio_debug_info *info); 189 190 /* Starts or stops the aec dump task. 191 * Args: 192 * thread - pointer to the audio thread. 193 * stream_id - id of the target stream for aec dump. 194 * start - True to start the aec dump, false to stop. 195 * fd - File to store aec dump result. 196 */ 197 int audio_thread_set_aec_dump(struct audio_thread *thread, 198 cras_stream_id_t stream_id, unsigned int start, 199 int fd); 200 201 /* Configures the global converter for output remixing. Called by main 202 * thread. */ 203 int audio_thread_config_global_remix(struct audio_thread *thread, 204 unsigned int num_channels, 205 const float *coefficient); 206 207 /* Start ramping on a device. 208 * 209 * Ramping is started/updated in audio thread. This function lets the main 210 * thread request that the audio thread start ramping. 211 * 212 * Args: 213 * thread - a pointer to the audio thread. 214 * dev_idx - Index of the the device to start ramping. 215 * request - Check the docstrings of CRAS_IODEV_RAMP_REQUEST. 216 * Returns: 217 * 0 on success, negative if error. 218 */ 219 int audio_thread_dev_start_ramp(struct audio_thread *thread, 220 unsigned int dev_idx, 221 enum CRAS_IODEV_RAMP_REQUEST request); 222 #endif /* AUDIO_THREAD_H_ */ 223