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