• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /* Callback function to be handled in main loop in audio thread.
49  * Args:
50  *    data - The data for callback function.
51  */
52 typedef int (*thread_callback)(void *data);
53 
54 /* Creates an audio thread.
55  * Returns:
56  *    A pointer to the newly created audio thread.  It must be freed by calling
57  *    audio_thread_destroy().  Returns NULL on error.
58  */
59 struct audio_thread *audio_thread_create();
60 
61 /* Adds an open device.
62  * Args:
63  *    thread - The thread to add open device to.
64  *    dev - The open device to add.
65  */
66 int audio_thread_add_open_dev(struct audio_thread *thread,
67 			      struct cras_iodev *dev);
68 
69 /* Removes an open device.
70  * Args:
71  *    thread - The thread to remove open device from.
72  *    dev - The open device to remove.
73  */
74 int audio_thread_rm_open_dev(struct audio_thread *thread,
75 			     struct cras_iodev *dev);
76 
77 /* Checks if dev is open and used by audio thread.
78  * Args:
79  *    thread - The thread accessing open devs.
80  *    dev - The device to check if it has already been open.
81  */
82 int audio_thread_is_dev_open(struct audio_thread *thread,
83 			     struct cras_iodev *dev);
84 
85 /* Adds an thread_callback to audio thread.
86  * Args:
87  *    fd - The file descriptor to be polled for the callback.
88  *      The callback will be called when fd is readable.
89  *    cb - The callback function.
90  *    data - The data for the callback function.
91  */
92 void audio_thread_add_callback(int fd, thread_callback cb,
93                                void *data);
94 
95 /* Adds an thread_callback to audio thread.
96  * Args:
97  *    fd - The file descriptor to be polled for the callback.
98  *      The callback will be called when fd is writeable.
99  *    cb - The callback function.
100  *    data - The data for the callback function.
101  */
102 void audio_thread_add_write_callback(int fd, thread_callback cb,
103 				     void *data);
104 
105 /* Removes an thread_callback from audio thread.
106  * Args:
107  *    fd - The file descriptor of the previous added callback.
108  */
109 void audio_thread_rm_callback(int fd);
110 
111 /* Removes a thread_callback from main thread.
112  * Args:
113  *     thread - The thread to remove callback from.
114  *     fd - The file descriptor of the previous added callback.
115  */
116 int audio_thread_rm_callback_sync(struct audio_thread *thread, int fd);
117 
118 
119 /* Enables or Disabled the callback associated with fd. */
120 void audio_thread_enable_callback(int fd, int enabled);
121 
122 /* Starts a thread created with audio_thread_create.
123  * Args:
124  *    thread - The thread to start.
125  * Returns:
126  *    0 on success, return code from pthread_crate on failure.
127  */
128 int audio_thread_start(struct audio_thread *thread);
129 
130 /* Frees an audio thread created with audio_thread_create(). */
131 void audio_thread_destroy(struct audio_thread *thread);
132 
133 /* Add a stream to the thread. After this call, the ownership of the stream will
134  * be passed to the audio thread. Audio thread is responsible to release the
135  * stream's resources.
136  * Args:
137  *    thread - a pointer to the audio thread.
138  *    stream - the new stream to add.
139  *    devs - an array of devices to attach stream.
140  *    num_devs - number of devices in the array pointed by devs
141  * Returns:
142  *    zero on success, negative error from the AUDIO_THREAD enum above when an
143  *    the thread can't be added.
144  */
145 int audio_thread_add_stream(struct audio_thread *thread,
146 			    struct cras_rstream *stream,
147 			    struct cras_iodev **devs,
148 			    unsigned int num_devs);
149 
150 /* Begin draining a stream and check the draining status.
151  * Args:
152  *    thread - a pointer to the audio thread.
153  *    stream - the stream to drain/remove.
154  * Returns:
155  *    zero if the stream is drained and can be deleted.  If the stream is not
156  *    completely drained, then the number of milliseconds until is is drained
157  *    are returned.
158  */
159 int audio_thread_drain_stream(struct audio_thread *thread,
160 			      struct cras_rstream *stream);
161 
162 /* Disconnect a stream from the client.
163  * Args:
164  *    thread - a pointer to the audio thread.
165  *    stream - the stream to be disconnected.
166  *    iodev - the device to disconnect from.
167  * Returns:
168  *    0 on success, negative if error.
169  */
170 int audio_thread_disconnect_stream(struct audio_thread *thread,
171 				   struct cras_rstream *stream,
172 				   struct cras_iodev *iodev);
173 
174 /* Dumps information about all active streams to syslog. */
175 int audio_thread_dump_thread_info(struct audio_thread *thread,
176 				  struct audio_debug_info *info);
177 
178 /* Starts or stops the aec dump task.
179  * Args:
180  *    thread - pointer to the audio thread.
181  *    stream_id - id of the target stream for aec dump.
182  *    start - True to start the aec dump, false to stop.
183  *    fd - File to store aec dump result.
184  */
185 int audio_thread_set_aec_dump(struct audio_thread *thread,
186 			      cras_stream_id_t stream_id,
187 			      unsigned int start,
188 			      int fd);
189 
190 /* Configures the global converter for output remixing. Called by main
191  * thread. */
192 int audio_thread_config_global_remix(struct audio_thread *thread,
193 				     unsigned int num_channels,
194 				     const float *coefficient);
195 
196 /* Start ramping on a device.
197  *
198  * Ramping is started/updated in audio thread. This function lets the main
199  * thread request that the audio thread start ramping.
200  *
201  * Args:
202  *   thread - a pointer to the audio thread.
203  *   dev - the device to start ramping.
204  *   request - Check the docstrings of CRAS_IODEV_RAMP_REQUEST.
205  * Returns:
206  *    0 on success, negative if error.
207  */
208 int audio_thread_dev_start_ramp(struct audio_thread *thread,
209 				struct cras_iodev *dev,
210 				enum CRAS_IODEV_RAMP_REQUEST request);
211 #endif /* AUDIO_THREAD_H_ */
212