• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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 #define LOG_TAG "audio_hw_hikey"
18 //#define LOG_NDEBUG 0
19 
20 #include <errno.h>
21 #include <malloc.h>
22 #include <pthread.h>
23 #include <stdint.h>
24 #include <sys/time.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 
28 #include <log/log.h>
29 #include <cutils/str_parms.h>
30 #include <cutils/properties.h>
31 
32 #include <hardware/hardware.h>
33 #include <system/audio.h>
34 #include <hardware/audio.h>
35 
36 #include <sound/asound.h>
37 #include <tinyalsa/asoundlib.h>
38 #include <audio_utils/resampler.h>
39 #include <audio_utils/echo_reference.h>
40 #include <hardware/audio_effect.h>
41 #include <hardware/audio_alsaops.h>
42 #include <audio_effects/effect_aec.h>
43 
44 #include <sys/ioctl.h>
45 #include <linux/audio_hifi.h>
46 
47 #define CARD_OUT 0
48 #define PORT_CODEC 0
49 /* Minimum granularity - Arbitrary but small value */
50 #define CODEC_BASE_FRAME_COUNT 32
51 
52 /* number of base blocks in a short period (low latency) */
53 #define PERIOD_MULTIPLIER 32  /* 21 ms */
54 /* number of frames per short period (low latency) */
55 #define PERIOD_SIZE (CODEC_BASE_FRAME_COUNT * PERIOD_MULTIPLIER)
56 /* number of pseudo periods for low latency playback */
57 #define PLAYBACK_PERIOD_COUNT 4
58 #define PLAYBACK_PERIOD_START_THRESHOLD 2
59 #define CODEC_SAMPLING_RATE 48000
60 #define CHANNEL_STEREO 2
61 #define MIN_WRITE_SLEEP_US      5000
62 
63 #ifdef ENABLE_XAF_DSP_DEVICE
64 #include "xaf-utils-test.h"
65 #include "audio/xa_vorbis_dec_api.h"
66 #include "audio/xa-audio-decoder-api.h"
67 #define NUM_COMP_IN_GRAPH   1
68 
69 struct alsa_audio_device;
70 
71 struct xaf_dsp_device {
72     void *p_adev;
73     void *p_decoder;
74     xaf_info_t comp_info;
75     /* ...playback format */
76     xaf_format_t pb_format;
77     xaf_comp_status dec_status;
78     int dec_info[4];
79     void *dec_inbuf[2];
80     int read_length;
81     xf_id_t dec_id;
82     int xaf_started;
83     mem_obj_t* mem_handle;
84     int num_comp;
85     int (*dec_setup)(void *p_comp, struct alsa_audio_device *audio_device);
86     int xafinitdone;
87 };
88 #endif
89 
90 struct stub_stream_in {
91     struct audio_stream_in stream;
92 };
93 
94 struct alsa_audio_device {
95     struct audio_hw_device hw_device;
96 
97     pthread_mutex_t lock;   /* see note below on mutex acquisition order */
98     int devices;
99     struct alsa_stream_in *active_input;
100     struct alsa_stream_out *active_output;
101     bool mic_mute;
102 #ifdef ENABLE_XAF_DSP_DEVICE
103     struct xaf_dsp_device dsp_device;
104     int hifi_dsp_fd;
105 #endif
106 };
107 
108 struct alsa_stream_out {
109     struct audio_stream_out stream;
110 
111     pthread_mutex_t lock;   /* see note below on mutex acquisition order */
112     struct pcm_config config;
113     struct pcm *pcm;
114     bool unavailable;
115     int standby;
116     struct alsa_audio_device *dev;
117     int write_threshold;
118     unsigned int written;
119 };
120 
121 #ifdef ENABLE_XAF_DSP_DEVICE
pcm_setup(void * p_pcm,struct alsa_audio_device * audio_device)122 static int pcm_setup(void *p_pcm, struct alsa_audio_device *audio_device)
123 {
124     int param[6];
125 
126     param[0] = XA_CODEC_CONFIG_PARAM_SAMPLE_RATE;
127     param[1] = audio_device->dsp_device.pb_format.sample_rate;
128     param[2] = XA_CODEC_CONFIG_PARAM_CHANNELS;
129     param[3] = audio_device->dsp_device.pb_format.channels;
130     param[4] = XA_CODEC_CONFIG_PARAM_PCM_WIDTH;
131     param[5] = audio_device->dsp_device.pb_format.pcm_width;
132 
133     XF_CHK_API(xaf_comp_set_config(p_pcm, 3, &param[0]));
134 
135     return 0;
136 }
137 
xa_thread_exit_handler(int sig)138 void xa_thread_exit_handler(int sig)
139 {
140     /* ...unused arg */
141     (void) sig;
142 
143     pthread_exit(0);
144 }
145 
146 /*xtensa audio device init*/
xa_device_init(struct alsa_audio_device * audio_device)147 static int xa_device_init(struct alsa_audio_device *audio_device)
148 {
149     /* ...initialize playback format */
150     audio_device->dsp_device.p_adev = NULL;
151     audio_device->dsp_device.pb_format.sample_rate = 48000;
152     audio_device->dsp_device.pb_format.channels    = 2;
153     audio_device->dsp_device.pb_format.pcm_width   = 16;
154     audio_device->dsp_device.xafinitdone = 0;
155     audio_frmwk_buf_size = 0; //unused
156     audio_comp_buf_size  = 0; //unused
157     audio_device->dsp_device.num_comp = NUM_COMP_IN_GRAPH;
158     struct sigaction actions;
159     memset(&actions, 0, sizeof(actions));
160     sigemptyset(&actions.sa_mask);
161     actions.sa_flags = 0;
162     actions.sa_handler = xa_thread_exit_handler;
163     sigaction(SIGUSR1,&actions,NULL);
164     /* ...initialize tracing facility */
165     audio_device->dsp_device.xaf_started =1;
166     audio_device->dsp_device.dec_id    = "audio-decoder/pcm";
167     audio_device->dsp_device.dec_setup = pcm_setup;
168     audio_device->dsp_device.mem_handle = mem_init(); //initialize memory handler
169     XF_CHK_API(xaf_adev_open(&audio_device->dsp_device.p_adev, audio_frmwk_buf_size, audio_comp_buf_size, mem_malloc, mem_free));
170     /* ...create decoder component */
171     XF_CHK_API(xaf_comp_create(audio_device->dsp_device.p_adev, &audio_device->dsp_device.p_decoder, audio_device->dsp_device.dec_id, 1, 1, &audio_device->dsp_device.dec_inbuf[0], XAF_DECODER));
172     XF_CHK_API(audio_device->dsp_device.dec_setup(audio_device->dsp_device.p_decoder,audio_device));
173 
174     /* ...start decoder component */
175     XF_CHK_API(xaf_comp_process(audio_device->dsp_device.p_adev, audio_device->dsp_device.p_decoder, NULL, 0, XAF_START_FLAG));
176     return 0;
177 }
178 
xa_device_run(struct audio_stream_out * stream,const void * buffer,size_t frame_size,size_t out_frames,size_t bytes)179 static int xa_device_run(struct audio_stream_out *stream, const void *buffer, size_t frame_size, size_t out_frames, size_t bytes)
180 {
181     struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
182     struct alsa_audio_device *adev = out->dev;
183     int ret=0;
184     void *p_comp=adev->dsp_device.p_decoder;
185     xaf_comp_status comp_status;
186     memcpy(adev->dsp_device.dec_inbuf[0],buffer,bytes);
187     adev->dsp_device.read_length=bytes;
188 
189     if (adev->dsp_device.xafinitdone == 0) {
190         XF_CHK_API(xaf_comp_process(adev->dsp_device.p_adev, adev->dsp_device.p_decoder, adev->dsp_device.dec_inbuf[0], adev->dsp_device.read_length, XAF_INPUT_READY_FLAG));
191         XF_CHK_API(xaf_comp_get_status(adev->dsp_device.p_adev, adev->dsp_device.p_decoder, &adev->dsp_device.dec_status, &adev->dsp_device.comp_info));
192         ALOGE("PROXY:%s xaf_comp_get_status %d\n",__func__,adev->dsp_device.dec_status);
193         if (adev->dsp_device.dec_status == XAF_INIT_DONE) {
194             adev->dsp_device.xafinitdone = 1;
195             out->written += out_frames;
196             XF_CHK_API(xaf_comp_process(NULL, p_comp, NULL, 0, XAF_EXEC_FLAG));
197         }
198     } else {
199         XF_CHK_API(xaf_comp_process(NULL, adev->dsp_device.p_decoder, adev->dsp_device.dec_inbuf[0], adev->dsp_device.read_length, XAF_INPUT_READY_FLAG));
200         while (1) {
201             XF_CHK_API(xaf_comp_get_status(NULL, p_comp, &comp_status, &adev->dsp_device.comp_info));
202             if (comp_status == XAF_EXEC_DONE) break;
203             if (comp_status == XAF_NEED_INPUT) {
204                  ALOGV("PROXY:%s loop:XAF_NEED_INPUT\n",__func__);
205                  break;
206             }
207             if (comp_status == XAF_OUTPUT_READY) {
208                 void *p_buf = (void *)adev->dsp_device.comp_info.buf;
209                 int size    = adev->dsp_device.comp_info.length;
210                 ret = pcm_mmap_write(out->pcm, p_buf, size);
211                 if (ret == 0) {
212                     out->written += out_frames;
213                 }
214                 XF_CHK_API(xaf_comp_process(NULL, adev->dsp_device.p_decoder, (void *)adev->dsp_device.comp_info.buf, adev->dsp_device.comp_info.length, XAF_NEED_OUTPUT_FLAG));
215             }
216         }
217     }
218     return ret;
219 }
220 
xa_device_close(struct alsa_audio_device * audio_device)221 static int xa_device_close(struct alsa_audio_device *audio_device)
222 {
223     if (audio_device->dsp_device.xaf_started) {
224         xaf_comp_status comp_status;
225         audio_device->dsp_device.xaf_started=0;
226         while (1) {
227             XF_CHK_API(xaf_comp_get_status(NULL, audio_device->dsp_device.p_decoder, &comp_status, &audio_device->dsp_device.comp_info));
228             ALOGV("PROXY:comp_status:%d,audio_device->dsp_device.comp_info.length:%d\n",(int)comp_status,audio_device->dsp_device.comp_info.length);
229             if (comp_status == XAF_EXEC_DONE)
230                 break;
231             if (comp_status == XAF_NEED_INPUT) {
232                 XF_CHK_API(xaf_comp_process(NULL, audio_device->dsp_device.p_decoder, NULL, 0, XAF_INPUT_OVER_FLAG));
233             }
234 
235             if (comp_status == XAF_OUTPUT_READY) {
236                 XF_CHK_API(xaf_comp_process(NULL, audio_device->dsp_device.p_decoder, (void *)audio_device->dsp_device.comp_info.buf, audio_device->dsp_device.comp_info.length, XAF_NEED_OUTPUT_FLAG));
237             }
238         }
239 
240         /* ...exec done, clean-up */
241         XF_CHK_API(xaf_comp_delete(audio_device->dsp_device.p_decoder));
242         XF_CHK_API(xaf_adev_close(audio_device->dsp_device.p_adev, 0 /*unused*/));
243         mem_exit();
244         XF_CHK_API(print_mem_mcps_info(audio_device->dsp_device.mem_handle, audio_device->dsp_device.num_comp));
245     }
246     return 0;
247 }
248 #endif
249 
250 /* must be called with hw device and output stream mutexes locked */
start_output_stream(struct alsa_stream_out * out)251 static int start_output_stream(struct alsa_stream_out *out)
252 {
253     struct alsa_audio_device *adev = out->dev;
254 
255     if (out->unavailable)
256         return -ENODEV;
257 
258     /* default to low power: will be corrected in out_write if necessary before first write to
259      * tinyalsa.
260      */
261     out->write_threshold = PLAYBACK_PERIOD_COUNT * PERIOD_SIZE;
262     out->config.start_threshold = PLAYBACK_PERIOD_START_THRESHOLD * PERIOD_SIZE;
263     out->config.avail_min = PERIOD_SIZE;
264 
265     out->pcm = pcm_open(CARD_OUT, PORT_CODEC, PCM_OUT | PCM_MMAP | PCM_NOIRQ | PCM_MONOTONIC, &out->config);
266 
267     if (!pcm_is_ready(out->pcm)) {
268         ALOGE("cannot open pcm_out driver: %s", pcm_get_error(out->pcm));
269         pcm_close(out->pcm);
270         adev->active_output = NULL;
271         out->unavailable = true;
272         return -ENODEV;
273     }
274 
275     adev->active_output = out;
276     return 0;
277 }
278 
out_get_sample_rate(const struct audio_stream * stream)279 static uint32_t out_get_sample_rate(const struct audio_stream *stream)
280 {
281     struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
282     return out->config.rate;
283 }
284 
out_set_sample_rate(struct audio_stream * stream,uint32_t rate)285 static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
286 {
287     ALOGV("out_set_sample_rate: %d", 0);
288     return -ENOSYS;
289 }
290 
out_get_buffer_size(const struct audio_stream * stream)291 static size_t out_get_buffer_size(const struct audio_stream *stream)
292 {
293     ALOGV("out_get_buffer_size: %d", 4096);
294 
295     /* return the closest majoring multiple of 16 frames, as
296      * audioflinger expects audio buffers to be a multiple of 16 frames */
297     size_t size = PERIOD_SIZE;
298     size = ((size + 15) / 16) * 16;
299     return size * audio_stream_out_frame_size((struct audio_stream_out *)stream);
300 }
301 
out_get_channels(const struct audio_stream * stream)302 static audio_channel_mask_t out_get_channels(const struct audio_stream *stream)
303 {
304     ALOGV("out_get_channels");
305     struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
306     return audio_channel_out_mask_from_count(out->config.channels);
307 }
308 
out_get_format(const struct audio_stream * stream)309 static audio_format_t out_get_format(const struct audio_stream *stream)
310 {
311     ALOGV("out_get_format");
312     struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
313     return audio_format_from_pcm_format(out->config.format);
314 }
315 
out_set_format(struct audio_stream * stream,audio_format_t format)316 static int out_set_format(struct audio_stream *stream, audio_format_t format)
317 {
318     ALOGV("out_set_format: %d",format);
319     return -ENOSYS;
320 }
321 
do_output_standby(struct alsa_stream_out * out)322 static int do_output_standby(struct alsa_stream_out *out)
323 {
324     struct alsa_audio_device *adev = out->dev;
325 
326     if (!out->standby) {
327         pcm_close(out->pcm);
328         out->pcm = NULL;
329         adev->active_output = NULL;
330         out->standby = 1;
331     }
332     return 0;
333 }
334 
out_standby(struct audio_stream * stream)335 static int out_standby(struct audio_stream *stream)
336 {
337     ALOGV("out_standby");
338     struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
339     int status;
340 
341     pthread_mutex_lock(&out->dev->lock);
342     pthread_mutex_lock(&out->lock);
343 #ifdef ENABLE_XAF_DSP_DEVICE
344     xa_device_close(out->dev);
345 #endif
346     status = do_output_standby(out);
347     pthread_mutex_unlock(&out->lock);
348     pthread_mutex_unlock(&out->dev->lock);
349     return status;
350 }
351 
out_dump(const struct audio_stream * stream,int fd)352 static int out_dump(const struct audio_stream *stream, int fd)
353 {
354     ALOGV("out_dump");
355     return 0;
356 }
357 
out_set_parameters(struct audio_stream * stream,const char * kvpairs)358 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
359 {
360     ALOGV("out_set_parameters");
361     struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
362     struct alsa_audio_device *adev = out->dev;
363     struct str_parms *parms;
364     char value[32];
365     int ret, val = 0;
366 
367     parms = str_parms_create_str(kvpairs);
368 
369     ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
370     if (ret >= 0) {
371         val = atoi(value);
372         pthread_mutex_lock(&adev->lock);
373         pthread_mutex_lock(&out->lock);
374         if (((adev->devices & AUDIO_DEVICE_OUT_ALL) != val) && (val != 0)) {
375             adev->devices &= ~AUDIO_DEVICE_OUT_ALL;
376             adev->devices |= val;
377         }
378         pthread_mutex_unlock(&out->lock);
379         pthread_mutex_unlock(&adev->lock);
380     }
381 
382     str_parms_destroy(parms);
383     return ret;
384 }
385 
out_get_parameters(const struct audio_stream * stream,const char * keys)386 static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
387 {
388     ALOGV("out_get_parameters");
389     return strdup("");
390 }
391 
out_get_latency(const struct audio_stream_out * stream)392 static uint32_t out_get_latency(const struct audio_stream_out *stream)
393 {
394     ALOGV("out_get_latency");
395     struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
396     return (PERIOD_SIZE * PLAYBACK_PERIOD_COUNT * 1000) / out->config.rate;
397 }
398 
out_set_volume(struct audio_stream_out * stream,float left,float right)399 static int out_set_volume(struct audio_stream_out *stream, float left,
400         float right)
401 {
402     ALOGV("out_set_volume: Left:%f Right:%f", left, right);
403     return 0;
404 }
405 
out_write(struct audio_stream_out * stream,const void * buffer,size_t bytes)406 static ssize_t out_write(struct audio_stream_out *stream, const void* buffer,
407         size_t bytes)
408 {
409     int ret;
410     struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
411     struct alsa_audio_device *adev = out->dev;
412     size_t frame_size = audio_stream_out_frame_size(stream);
413     size_t out_frames = bytes / frame_size;
414 
415     /* acquiring hw device mutex systematically is useful if a low priority thread is waiting
416      * on the output stream mutex - e.g. executing select_mode() while holding the hw device
417      * mutex
418      */
419     pthread_mutex_lock(&adev->lock);
420     pthread_mutex_lock(&out->lock);
421     if (out->standby) {
422 #ifdef ENABLE_XAF_DSP_DEVICE
423         if (adev->hifi_dsp_fd >= 0) {
424             xa_device_init(adev);
425         }
426 #endif
427         ret = start_output_stream(out);
428         if (ret != 0) {
429             pthread_mutex_unlock(&adev->lock);
430             goto exit;
431         }
432         out->standby = 0;
433     }
434 
435     pthread_mutex_unlock(&adev->lock);
436 
437 #ifdef ENABLE_XAF_DSP_DEVICE
438     /*fallback to original audio processing*/
439     if (adev->dsp_device.p_adev != NULL) {
440         ret = xa_device_run(stream, buffer,frame_size, out_frames, bytes);
441     } else {
442 #endif
443         ret = pcm_mmap_write(out->pcm, buffer, out_frames * frame_size);
444         if (ret == 0) {
445             out->written += out_frames;
446         }
447 #ifdef ENABLE_XAF_DSP_DEVICE
448     }
449 #endif
450 exit:
451     pthread_mutex_unlock(&out->lock);
452 
453     if (ret != 0) {
454         usleep((int64_t)bytes * 1000000 / audio_stream_out_frame_size(stream) /
455                 out_get_sample_rate(&stream->common));
456     }
457 
458     return bytes;
459 }
460 
out_get_render_position(const struct audio_stream_out * stream,uint32_t * dsp_frames)461 static int out_get_render_position(const struct audio_stream_out *stream,
462         uint32_t *dsp_frames)
463 {
464     *dsp_frames = 0;
465     ALOGV("out_get_render_position: dsp_frames: %p", dsp_frames);
466     return -EINVAL;
467 }
468 
out_get_presentation_position(const struct audio_stream_out * stream,uint64_t * frames,struct timespec * timestamp)469 static int out_get_presentation_position(const struct audio_stream_out *stream,
470                                    uint64_t *frames, struct timespec *timestamp)
471 {
472     struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
473     int ret = -1;
474 
475         if (out->pcm) {
476             unsigned int avail;
477             if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
478                 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
479                 int64_t signed_frames = out->written - kernel_buffer_size + avail;
480                 if (signed_frames >= 0) {
481                     *frames = signed_frames;
482                     ret = 0;
483                 }
484             }
485         }
486 
487     return ret;
488 }
489 
490 
out_add_audio_effect(const struct audio_stream * stream,effect_handle_t effect)491 static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
492 {
493     ALOGV("out_add_audio_effect: %p", effect);
494     return 0;
495 }
496 
out_remove_audio_effect(const struct audio_stream * stream,effect_handle_t effect)497 static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
498 {
499     ALOGV("out_remove_audio_effect: %p", effect);
500     return 0;
501 }
502 
out_get_next_write_timestamp(const struct audio_stream_out * stream,int64_t * timestamp)503 static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
504         int64_t *timestamp)
505 {
506     *timestamp = 0;
507     ALOGV("out_get_next_write_timestamp: %ld", (long int)(*timestamp));
508     return -EINVAL;
509 }
510 
511 /** audio_stream_in implementation **/
in_get_sample_rate(const struct audio_stream * stream)512 static uint32_t in_get_sample_rate(const struct audio_stream *stream)
513 {
514     ALOGV("in_get_sample_rate");
515     return 8000;
516 }
517 
in_set_sample_rate(struct audio_stream * stream,uint32_t rate)518 static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
519 {
520     ALOGV("in_set_sample_rate: %d", rate);
521     return -ENOSYS;
522 }
523 
in_get_buffer_size(const struct audio_stream * stream)524 static size_t in_get_buffer_size(const struct audio_stream *stream)
525 {
526     ALOGV("in_get_buffer_size: %d", 320);
527     return 320;
528 }
529 
in_get_channels(const struct audio_stream * stream)530 static audio_channel_mask_t in_get_channels(const struct audio_stream *stream)
531 {
532     ALOGV("in_get_channels: %d", AUDIO_CHANNEL_IN_MONO);
533     return AUDIO_CHANNEL_IN_MONO;
534 }
535 
in_get_format(const struct audio_stream * stream)536 static audio_format_t in_get_format(const struct audio_stream *stream)
537 {
538     return AUDIO_FORMAT_PCM_16_BIT;
539 }
540 
in_set_format(struct audio_stream * stream,audio_format_t format)541 static int in_set_format(struct audio_stream *stream, audio_format_t format)
542 {
543     return -ENOSYS;
544 }
545 
in_standby(struct audio_stream * stream)546 static int in_standby(struct audio_stream *stream)
547 {
548     return 0;
549 }
550 
in_dump(const struct audio_stream * stream,int fd)551 static int in_dump(const struct audio_stream *stream, int fd)
552 {
553     return 0;
554 }
555 
in_set_parameters(struct audio_stream * stream,const char * kvpairs)556 static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
557 {
558     return 0;
559 }
560 
in_get_parameters(const struct audio_stream * stream,const char * keys)561 static char * in_get_parameters(const struct audio_stream *stream,
562         const char *keys)
563 {
564     return strdup("");
565 }
566 
in_set_gain(struct audio_stream_in * stream,float gain)567 static int in_set_gain(struct audio_stream_in *stream, float gain)
568 {
569     return 0;
570 }
571 
in_read(struct audio_stream_in * stream,void * buffer,size_t bytes)572 static ssize_t in_read(struct audio_stream_in *stream, void* buffer,
573         size_t bytes)
574 {
575     ALOGV("in_read: bytes %zu", bytes);
576     /* XXX: fake timing for audio input */
577     usleep((int64_t)bytes * 1000000 / audio_stream_in_frame_size(stream) /
578             in_get_sample_rate(&stream->common));
579     memset(buffer, 0, bytes);
580     return bytes;
581 }
582 
in_get_input_frames_lost(struct audio_stream_in * stream)583 static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
584 {
585     return 0;
586 }
587 
in_add_audio_effect(const struct audio_stream * stream,effect_handle_t effect)588 static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
589 {
590     return 0;
591 }
592 
in_remove_audio_effect(const struct audio_stream * stream,effect_handle_t effect)593 static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
594 {
595     return 0;
596 }
597 
adev_open_output_stream(struct audio_hw_device * dev,audio_io_handle_t handle,audio_devices_t devices,audio_output_flags_t flags,struct audio_config * config,struct audio_stream_out ** stream_out,const char * address __unused)598 static int adev_open_output_stream(struct audio_hw_device *dev,
599         audio_io_handle_t handle,
600         audio_devices_t devices,
601         audio_output_flags_t flags,
602         struct audio_config *config,
603         struct audio_stream_out **stream_out,
604         const char *address __unused)
605 {
606     ALOGV("adev_open_output_stream...");
607 
608     struct alsa_audio_device *ladev = (struct alsa_audio_device *)dev;
609     struct alsa_stream_out *out;
610     struct pcm_params *params;
611     int ret = 0;
612 
613     params = pcm_params_get(CARD_OUT, PORT_CODEC, PCM_OUT);
614     if (!params)
615         return -ENOSYS;
616 
617     out = (struct alsa_stream_out *)calloc(1, sizeof(struct alsa_stream_out));
618     if (!out)
619         return -ENOMEM;
620 
621     out->stream.common.get_sample_rate = out_get_sample_rate;
622     out->stream.common.set_sample_rate = out_set_sample_rate;
623     out->stream.common.get_buffer_size = out_get_buffer_size;
624     out->stream.common.get_channels = out_get_channels;
625     out->stream.common.get_format = out_get_format;
626     out->stream.common.set_format = out_set_format;
627     out->stream.common.standby = out_standby;
628     out->stream.common.dump = out_dump;
629     out->stream.common.set_parameters = out_set_parameters;
630     out->stream.common.get_parameters = out_get_parameters;
631     out->stream.common.add_audio_effect = out_add_audio_effect;
632     out->stream.common.remove_audio_effect = out_remove_audio_effect;
633     out->stream.get_latency = out_get_latency;
634     out->stream.set_volume = out_set_volume;
635     out->stream.write = out_write;
636     out->stream.get_render_position = out_get_render_position;
637     out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
638     out->stream.get_presentation_position = out_get_presentation_position;
639 
640     out->config.channels = CHANNEL_STEREO;
641     out->config.rate = CODEC_SAMPLING_RATE;
642     out->config.format = PCM_FORMAT_S16_LE;
643     out->config.period_size = PERIOD_SIZE;
644     out->config.period_count = PLAYBACK_PERIOD_COUNT;
645 
646     if (out->config.rate != config->sample_rate ||
647            audio_channel_count_from_out_mask(config->channel_mask) != CHANNEL_STEREO ||
648                out->config.format !=  pcm_format_from_audio_format(config->format) ) {
649         config->sample_rate = out->config.rate;
650         config->format = audio_format_from_pcm_format(out->config.format);
651         config->channel_mask = audio_channel_out_mask_from_count(CHANNEL_STEREO);
652         ret = -EINVAL;
653     }
654 
655     ALOGI("adev_open_output_stream selects channels=%d rate=%d format=%d",
656                 out->config.channels, out->config.rate, out->config.format);
657 
658     out->dev = ladev;
659     out->standby = 1;
660     out->unavailable = false;
661 
662     config->format = out_get_format(&out->stream.common);
663     config->channel_mask = out_get_channels(&out->stream.common);
664     config->sample_rate = out_get_sample_rate(&out->stream.common);
665 
666     *stream_out = &out->stream;
667 
668     /* TODO The retry mechanism isn't implemented in AudioPolicyManager/AudioFlinger. */
669     ret = 0;
670 
671     return ret;
672 }
673 
adev_close_output_stream(struct audio_hw_device * dev,struct audio_stream_out * stream)674 static void adev_close_output_stream(struct audio_hw_device *dev,
675         struct audio_stream_out *stream)
676 {
677     ALOGV("adev_close_output_stream...");
678     free(stream);
679 }
680 
adev_set_parameters(struct audio_hw_device * dev,const char * kvpairs)681 static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
682 {
683     ALOGV("adev_set_parameters");
684     return -ENOSYS;
685 }
686 
adev_get_parameters(const struct audio_hw_device * dev,const char * keys)687 static char * adev_get_parameters(const struct audio_hw_device *dev,
688         const char *keys)
689 {
690     ALOGV("adev_get_parameters");
691     return strdup("");
692 }
693 
adev_init_check(const struct audio_hw_device * dev)694 static int adev_init_check(const struct audio_hw_device *dev)
695 {
696     ALOGV("adev_init_check");
697     return 0;
698 }
699 
adev_set_voice_volume(struct audio_hw_device * dev,float volume)700 static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
701 {
702     ALOGV("adev_set_voice_volume: %f", volume);
703     return -ENOSYS;
704 }
705 
adev_set_master_volume(struct audio_hw_device * dev,float volume)706 static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
707 {
708     ALOGV("adev_set_master_volume: %f", volume);
709     return -ENOSYS;
710 }
711 
adev_get_master_volume(struct audio_hw_device * dev,float * volume)712 static int adev_get_master_volume(struct audio_hw_device *dev, float *volume)
713 {
714     ALOGV("adev_get_master_volume: %f", *volume);
715     return -ENOSYS;
716 }
717 
adev_set_master_mute(struct audio_hw_device * dev,bool muted)718 static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
719 {
720     ALOGV("adev_set_master_mute: %d", muted);
721     return -ENOSYS;
722 }
723 
adev_get_master_mute(struct audio_hw_device * dev,bool * muted)724 static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
725 {
726     ALOGV("adev_get_master_mute: %d", *muted);
727     return -ENOSYS;
728 }
729 
adev_set_mode(struct audio_hw_device * dev,audio_mode_t mode)730 static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
731 {
732     ALOGV("adev_set_mode: %d", mode);
733     return 0;
734 }
735 
adev_set_mic_mute(struct audio_hw_device * dev,bool state)736 static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
737 {
738     ALOGV("adev_set_mic_mute: %d",state);
739     return -ENOSYS;
740 }
741 
adev_get_mic_mute(const struct audio_hw_device * dev,bool * state)742 static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
743 {
744     ALOGV("adev_get_mic_mute");
745     return -ENOSYS;
746 }
747 
adev_get_input_buffer_size(const struct audio_hw_device * dev,const struct audio_config * config)748 static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
749         const struct audio_config *config)
750 {
751     ALOGV("adev_get_input_buffer_size: %d", 320);
752     return 320;
753 }
754 
adev_open_input_stream(struct audio_hw_device __unused * dev,audio_io_handle_t handle,audio_devices_t devices,struct audio_config * config,struct audio_stream_in ** stream_in,audio_input_flags_t flags __unused,const char * address __unused,audio_source_t source __unused)755 static int adev_open_input_stream(struct audio_hw_device __unused *dev,
756         audio_io_handle_t handle,
757         audio_devices_t devices,
758         struct audio_config *config,
759         struct audio_stream_in **stream_in,
760         audio_input_flags_t flags __unused,
761         const char *address __unused,
762         audio_source_t source __unused)
763 {
764     struct stub_stream_in *in;
765 
766     ALOGV("adev_open_input_stream...");
767 
768     in = (struct stub_stream_in *)calloc(1, sizeof(struct stub_stream_in));
769     if (!in)
770         return -ENOMEM;
771 
772     in->stream.common.get_sample_rate = in_get_sample_rate;
773     in->stream.common.set_sample_rate = in_set_sample_rate;
774     in->stream.common.get_buffer_size = in_get_buffer_size;
775     in->stream.common.get_channels = in_get_channels;
776     in->stream.common.get_format = in_get_format;
777     in->stream.common.set_format = in_set_format;
778     in->stream.common.standby = in_standby;
779     in->stream.common.dump = in_dump;
780     in->stream.common.set_parameters = in_set_parameters;
781     in->stream.common.get_parameters = in_get_parameters;
782     in->stream.common.add_audio_effect = in_add_audio_effect;
783     in->stream.common.remove_audio_effect = in_remove_audio_effect;
784     in->stream.set_gain = in_set_gain;
785     in->stream.read = in_read;
786     in->stream.get_input_frames_lost = in_get_input_frames_lost;
787 
788     *stream_in = &in->stream;
789     return 0;
790 }
791 
adev_close_input_stream(struct audio_hw_device * dev,struct audio_stream_in * in)792 static void adev_close_input_stream(struct audio_hw_device *dev,
793         struct audio_stream_in *in)
794 {
795     ALOGV("adev_close_input_stream...");
796     return;
797 }
798 
adev_dump(const audio_hw_device_t * device,int fd)799 static int adev_dump(const audio_hw_device_t *device, int fd)
800 {
801     ALOGV("adev_dump");
802     return 0;
803 }
804 
adev_close(hw_device_t * device)805 static int adev_close(hw_device_t *device)
806 {
807 #ifdef ENABLE_XAF_DSP_DEVICE
808     struct alsa_audio_device *adev = (struct alsa_audio_device *)device;
809 #endif
810     ALOGV("adev_close");
811 #ifdef ENABLE_XAF_DSP_DEVICE
812     if (adev->hifi_dsp_fd >= 0)
813         close(adev->hifi_dsp_fd);
814 #endif
815     free(device);
816     return 0;
817 }
818 
adev_open(const hw_module_t * module,const char * name,hw_device_t ** device)819 static int adev_open(const hw_module_t* module, const char* name,
820         hw_device_t** device)
821 {
822     struct alsa_audio_device *adev;
823 
824     ALOGV("adev_open: %s", name);
825 
826     if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
827         return -EINVAL;
828 
829     adev = calloc(1, sizeof(struct alsa_audio_device));
830     if (!adev)
831         return -ENOMEM;
832 
833     adev->hw_device.common.tag = HARDWARE_DEVICE_TAG;
834     adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
835     adev->hw_device.common.module = (struct hw_module_t *) module;
836     adev->hw_device.common.close = adev_close;
837     adev->hw_device.init_check = adev_init_check;
838     adev->hw_device.set_voice_volume = adev_set_voice_volume;
839     adev->hw_device.set_master_volume = adev_set_master_volume;
840     adev->hw_device.get_master_volume = adev_get_master_volume;
841     adev->hw_device.set_master_mute = adev_set_master_mute;
842     adev->hw_device.get_master_mute = adev_get_master_mute;
843     adev->hw_device.set_mode = adev_set_mode;
844     adev->hw_device.set_mic_mute = adev_set_mic_mute;
845     adev->hw_device.get_mic_mute = adev_get_mic_mute;
846     adev->hw_device.set_parameters = adev_set_parameters;
847     adev->hw_device.get_parameters = adev_get_parameters;
848     adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size;
849     adev->hw_device.open_output_stream = adev_open_output_stream;
850     adev->hw_device.close_output_stream = adev_close_output_stream;
851     adev->hw_device.open_input_stream = adev_open_input_stream;
852     adev->hw_device.close_input_stream = adev_close_input_stream;
853     adev->hw_device.dump = adev_dump;
854 
855     adev->devices = AUDIO_DEVICE_NONE;
856 
857     *device = &adev->hw_device.common;
858 #ifdef ENABLE_XAF_DSP_DEVICE
859     adev->hifi_dsp_fd = open(HIFI_DSP_MISC_DRIVER, O_WRONLY, 0);
860     if (adev->hifi_dsp_fd < 0) {
861         ALOGW("hifi_dsp: Error opening device %d", errno);
862     } else {
863         ALOGI("hifi_dsp: Open device");
864     }
865 #endif
866     return 0;
867 }
868 
869 static struct hw_module_methods_t hal_module_methods = {
870     .open = adev_open,
871 };
872 
873 struct audio_module HAL_MODULE_INFO_SYM = {
874     .common = {
875         .tag = HARDWARE_MODULE_TAG,
876         .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
877         .hal_api_version = HARDWARE_HAL_API_VERSION,
878         .id = AUDIO_HARDWARE_MODULE_ID,
879         .name = "Hikey audio HW HAL",
880         .author = "The Android Open Source Project",
881         .methods = &hal_module_methods,
882     },
883 };
884