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   Hashmap *in_bus_tone_frequency_map;  // Extended field. Constant after init
37   int next_tone_frequency_to_assign; // Protected by this->lock
38   // Play on Speaker zone selection
39   int last_zone_selected_to_play; // Protected by this->lock
40   bool hfp_running;
41 };
42 
43 static struct generic_audio_device *device_handle;
44 
45 enum output_channel_enable {
46   LEFT_CHANNEL = 1,
47   RIGHT_CHANNEL = 1 << 1,
48   BOTH_CHANNELS = LEFT_CHANNEL | RIGHT_CHANNEL
49 };
50 
51 struct generic_stream_out {
52   struct audio_stream_out stream;  // Constant after init
53   pthread_mutex_t lock;
54   struct generic_audio_device *dev;  // Constant after init
55   audio_devices_t device;            // Protected by this->lock
56   struct audio_config req_config;    // Constant after init
57   struct pcm_config pcm_config;      // Constant after init
58   audio_vbuffer_t buffer;            // Constant after init
59   const char *bus_address;           // Extended field. Constant after init
60   struct audio_gain gain_stage;      // Constant after init
61   float amplitude_ratio;             // Protected by this->lock
62   enum output_channel_enable enabled_channels;  // Constant after init
63   bool is_ducked;                    // Protected by this->lock
64   bool is_muted;                     // Protected by this->lock
65 
66   // Time & Position Keeping
67   bool standby;                    // Protected by this->lock
68   uint64_t underrun_position;      // Protected by this->lock
69   struct timespec underrun_time;   // Protected by this->lock
70   uint64_t last_write_time_us;     // Protected by this->lock
71   uint64_t frames_total_buffered;  // Protected by this->lock
72   uint64_t frames_written;         // Protected by this->lock
73   uint64_t frames_rendered;        // Protected by this->lock
74 
75   // Worker
76   pthread_t worker_thread;     // Constant after init
77   pthread_cond_t worker_wake;  // Protected by this->lock
78   pthread_cond_t write_wake;   // Protected by this->lock
79   bool worker_standby;         // Protected by this->lock
80   bool worker_exit;            // Protected by this->lock
81 };
82 
83 struct oscillator {
84     float phase;
85     float phase_increment;
86 };
87 
88 struct generic_stream_in {
89   struct audio_stream_in stream;  // Constant after init
90   pthread_mutex_t lock;
91   struct generic_audio_device *dev;  // Constant after init
92   audio_devices_t device;            // Protected by this->lock
93   struct audio_config req_config;    // Constant after init
94   struct pcm *pcm;                   // Protected by this->lock
95   struct pcm_config pcm_config;      // Constant after init
96   int16_t *stereo_to_mono_buf;       // Protected by this->lock
97   size_t stereo_to_mono_buf_size;    // Protected by this->lock
98   audio_vbuffer_t buffer;            // Protected by this->lock
99   const char *bus_address;           // Extended field. Constant after init
100 
101   // Time & Position Keeping
102   bool standby;                       // Protected by this->lock
103   int64_t standby_position;           // Protected by this->lock
104   struct timespec standby_exit_time;  // Protected by this->lock
105   int64_t standby_frames_read;        // Protected by this->lock
106 
107   // Worker
108   pthread_t worker_thread;     // Constant after init
109   pthread_cond_t worker_wake;  // Protected by this->lock
110   bool worker_standby;         // Protected by this->lock
111   bool worker_exit;            // Protected by this->lock
112 
113    // Tone Oscillator
114    struct oscillator oscillator; // Protected by this->lock
115 };
116 
117 #endif  // AUDIO_HW_H
118