1 /*
2  * Copyright (C) 2019 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 _YUKAWA_AUDIO_HW_H_
18 #define _YUKAWA_AUDIO_HW_H_
19 
20 #include <hardware/audio.h>
21 #include <tinyalsa/asoundlib.h>
22 
23 #include "fir_filter.h"
24 
25 #define CARD_OUT 0
26 #define PORT_HDMI 0
27 #define PORT_INTERNAL_SPEAKER 1
28 #define CARD_IN 0
29 #define PORT_BUILTIN_MIC 3
30 
31 #define MIXER_XML_PATH "/vendor/etc/mixer_paths.xml"
32 /* Minimum granularity - Arbitrary but small value */
33 #define CODEC_BASE_FRAME_COUNT 32
34 
35 #define CHANNEL_STEREO 2
36 
37 #ifdef AEC_HAL
38 #define NUM_AEC_REFERENCE_CHANNELS 1
39 #else
40 /* App AEC uses 2-channel reference */
41 #define NUM_AEC_REFERENCE_CHANNELS 2
42 #endif /* #ifdef AEC_HAL */
43 
44 #define DEBUG_AEC 0
45 
46 #define PCM_OPEN_RETRIES 100
47 #define PCM_OPEN_WAIT_TIME_MS 20
48 
49 /* Capture codec parameters */
50 /* Set up a capture period of 32 ms:
51  * CAPTURE_PERIOD = PERIOD_SIZE / SAMPLE_RATE, so (32e-3) = PERIOD_SIZE / (16e3)
52  * => PERIOD_SIZE = 512 frames, where each "frame" consists of 1 sample of every channel (here, 2ch) */
53 #define CAPTURE_PERIOD_MULTIPLIER 16
54 #define CAPTURE_PERIOD_SIZE (CODEC_BASE_FRAME_COUNT * CAPTURE_PERIOD_MULTIPLIER)
55 #define CAPTURE_PERIOD_COUNT 4
56 #define CAPTURE_PERIOD_START_THRESHOLD 0
57 #define CAPTURE_CODEC_SAMPLING_RATE 16000
58 
59 /* Playback codec parameters */
60 /* number of base blocks in a short period (low latency) */
61 #define PLAYBACK_PERIOD_MULTIPLIER 32  /* 21 ms */
62 /* number of frames per short period (low latency) */
63 #define PLAYBACK_PERIOD_SIZE (CODEC_BASE_FRAME_COUNT * PLAYBACK_PERIOD_MULTIPLIER)
64 /* number of pseudo periods for low latency playback */
65 #define PLAYBACK_PERIOD_COUNT 4
66 #define PLAYBACK_PERIOD_START_THRESHOLD 2
67 #define PLAYBACK_CODEC_SAMPLING_RATE 48000
68 #define MIN_WRITE_SLEEP_US      5000
69 
70 #define SPEAKER_EQ_FILE "/vendor/etc/speaker_eq_sei610.fir"
71 #define SPEAKER_MAX_EQ_LENGTH 512
72 
73 struct alsa_audio_device {
74     struct audio_hw_device hw_device;
75 
76     pthread_mutex_t lock;   /* see notes in in_read/out_write on mutex acquisition order */
77     struct alsa_stream_in *active_input;
78     struct alsa_stream_out *active_output;
79     struct audio_route *audio_route;
80     struct mixer *mixer;
81     bool mic_mute;
82     struct aec_t *aec;
83 };
84 
85 struct alsa_stream_in {
86     struct audio_stream_in stream;
87 
88     pthread_mutex_t lock;   /* see note in in_read() on mutex acquisition order */
89     audio_devices_t devices;
90     struct pcm_config config;
91     struct pcm *pcm;
92     bool unavailable;
93     bool standby;
94     struct alsa_audio_device *dev;
95     int read_threshold;
96     unsigned int frames_read;
97     uint64_t timestamp_nsec;
98     audio_source_t source;
99 };
100 
101 struct alsa_stream_out {
102     struct audio_stream_out stream;
103 
104     pthread_mutex_t lock;   /* see note in out_write() on mutex acquisition order */
105     audio_devices_t devices;
106     struct pcm_config config;
107     struct pcm *pcm;
108     bool unavailable;
109     int standby;
110     struct alsa_audio_device *dev;
111     int write_threshold;
112     unsigned int frames_written;
113     struct timespec timestamp;
114     fir_filter_t* speaker_eq;
115 };
116 
117 /* 'bytes' are the number of bytes written to audio FIFO, for which 'timestamp' is valid.
118  * 'available' is the number of frames available to read (for input) or yet to be played
119  * (for output) frames in the PCM buffer.
120  * timestamp and available are updated by pcm_get_htimestamp(), so they use the same
121  * datatypes as the corresponding arguments to that function. */
122 struct aec_info {
123     struct timespec timestamp;
124     uint64_t timestamp_usec;
125     unsigned int available;
126     size_t bytes;
127 };
128 
129 #endif /* #ifndef _YUKAWA_AUDIO_HW_H_ */
130