1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef AUDIO_HW_H
18 #define AUDIO_HW_H
19 
20 #include <pthread.h>
21 
22 #include <cutils/hashmap.h>
23 #include <hardware/audio.h>
24 #include <tinyalsa/asoundlib.h>
25 
26 #include "audio_vbuffer.h"
27 
28 struct generic_audio_device {
29   struct audio_hw_device device;  // Constant after init
30   pthread_mutex_t lock;
31   unsigned int last_patch_id;   // Protected by this->lock
32   bool master_mute;             // Protected by this->lock
33   bool mic_mute;                // Protected by this->lock
34   struct mixer *mixer;          // Protected by this->lock
35   Hashmap *out_bus_stream_map;  // Extended field. Constant after init
36 };
37 
38 enum output_channel_enable {
39   LEFT_CHANNEL = 1,
40   RIGHT_CHANNEL = 1 << 1,
41   BOTH_CHANNELS = LEFT_CHANNEL | RIGHT_CHANNEL
42 };
43 
44 struct generic_stream_out {
45   struct audio_stream_out stream;  // Constant after init
46   pthread_mutex_t lock;
47   struct generic_audio_device *dev;  // Constant after init
48   audio_devices_t device;            // Protected by this->lock
49   struct audio_config req_config;    // Constant after init
50   struct pcm_config pcm_config;      // Constant after init
51   audio_vbuffer_t buffer;            // Constant after init
52   const char *bus_address;           // Extended field. Constant after init
53   struct audio_gain gain_stage;      // Constant after init
54   float amplitude_ratio;             // Protected by this->lock
55   enum output_channel_enable enabled_channels;  // Constant after init
56 
57   // Time & Position Keeping
58   bool standby;                    // Protected by this->lock
59   uint64_t underrun_position;      // Protected by this->lock
60   struct timespec underrun_time;   // Protected by this->lock
61   uint64_t last_write_time_us;     // Protected by this->lock
62   uint64_t frames_total_buffered;  // Protected by this->lock
63   uint64_t frames_written;         // Protected by this->lock
64   uint64_t frames_rendered;        // Protected by this->lock
65 
66   // Worker
67   pthread_t worker_thread;     // Constant after init
68   pthread_cond_t worker_wake;  // Protected by this->lock
69   bool worker_standby;         // Protected by this->lock
70   bool worker_exit;            // Protected by this->lock
71 };
72 
73 struct generic_stream_in {
74   struct audio_stream_in stream;  // Constant after init
75   pthread_mutex_t lock;
76   struct generic_audio_device *dev;  // Constant after init
77   audio_devices_t device;            // Protected by this->lock
78   struct audio_config req_config;    // Constant after init
79   struct pcm *pcm;                   // Protected by this->lock
80   struct pcm_config pcm_config;      // Constant after init
81   int16_t *stereo_to_mono_buf;       // Protected by this->lock
82   size_t stereo_to_mono_buf_size;    // Protected by this->lock
83   audio_vbuffer_t buffer;            // Protected by this->lock
84   const char *bus_address;           // Extended field. Constant after init
85 
86   // Time & Position Keeping
87   bool standby;                       // Protected by this->lock
88   int64_t standby_position;           // Protected by this->lock
89   struct timespec standby_exit_time;  // Protected by this->lock
90   int64_t standby_frames_read;        // Protected by this->lock
91 
92   // Worker
93   pthread_t worker_thread;     // Constant after init
94   pthread_cond_t worker_wake;  // Protected by this->lock
95   bool worker_standby;         // Protected by this->lock
96   bool worker_exit;            // Protected by this->lock
97 };
98 
99 #endif  // AUDIO_HW_H
100