• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2017 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  * `dev_io` Handles playback to and capture from open devices.  It runs only on
6  * the audio thread.
7  */
8 
9 #ifndef DEV_IO_H_
10 #define DEV_IO_H_
11 
12 #include "cras_iodev.h"
13 #include "cras_types.h"
14 #include "polled_interval_checker.h"
15 
16 /*
17  * Open input/output devices.
18  *    dev - The device.
19  *    wake_ts - When callback is needed to avoid xrun.
20  *    last_non_empty_ts - The last time we know the device played/captured
21  *        non-empty (zero) audio.
22  *    coarse_rate_adjust - Hack for when the sample rate needs heavy correction.
23  */
24 struct open_dev {
25 	struct cras_iodev *dev;
26 	struct timespec wake_ts;
27 	struct polled_interval *non_empty_check_pi;
28 	struct polled_interval *empty_pi;
29 	int coarse_rate_adjust;
30 	struct open_dev *prev, *next;
31 };
32 
33 /*
34  * Fetches streams from each device in `odev_list`.
35  *    odev_list - The list of open devices.
36  */
37 void dev_io_playback_fetch(struct open_dev *odev_list);
38 
39 /*
40  * Writes the samples fetched from the streams to the playback devices.
41  *    odev_list - The list of open devices.  Devices will be removed when
42  *                writing returns an error.
43  */
44 int dev_io_playback_write(struct open_dev **odevs,
45 			  struct cras_fmt_conv *output_converter);
46 
47 /* Only public for testing. */
48 int write_output_samples(struct open_dev **odevs,
49 			 struct open_dev *adev,
50 			 struct cras_fmt_conv *output_converter);
51 
52 /*
53  * Captures samples from each device in the list.
54  *    list - Pointer to the list of input devices.  Devices that fail to read
55  *           will be removed from the list.
56  */
57 int dev_io_capture(struct open_dev **list);
58 
59 /*
60  * Send samples that have been captured to their streams.
61  */
62 int dev_io_send_captured_samples(struct open_dev *idev_list);
63 
64 /* Reads and/or writes audio samples from/to the devices. */
65 void dev_io_run(struct open_dev **odevs, struct open_dev **idevs,
66 		struct cras_fmt_conv *output_converter);
67 
68 /*
69  * Fills min_ts with the next time the system should wake to service input.
70  * Returns the number of devices waiting.
71  */
72 int dev_io_next_input_wake(struct open_dev **idevs, struct timespec *min_ts);
73 
74 /*
75  * Removes a device from a list of devices.
76  *    odev_list - A pointer to the list to modify.
77  *    dev_to_rm - Find this device in the list and remove it.
78  */
79 void dev_io_rm_open_dev(struct open_dev **odev_list,
80 			struct open_dev *dev_to_rm);
81 
82 /* Returns a pointer to an open_dev if it is in the list, otherwise NULL. */
83 struct open_dev *dev_io_find_open_dev(struct open_dev *odev_list,
84                                       const struct cras_iodev *dev);
85 
86 /* Remove a stream from the provided list of devices. */
87 int dev_io_remove_stream(struct open_dev **dev_list,
88 			 struct cras_rstream *stream,
89 			 struct cras_iodev *dev);
90 
91 #endif /* DEV_IO_H_ */
92