-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaudio_hw.c
2090 lines (1711 loc) · 59.1 KB
/
audio_hw.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2012-13 Wolfson Microelectronics plc
*
* This code is heavily based on AOSP HAL for the asus/grouper
*
* Copyright (C) 2012 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define LOG_TAG "tinyhal"
/*#define LOG_NDEBUG 0*/
#include <errno.h>
#include <pthread.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/time.h>
#include <cutils/log.h>
#include <cutils/properties.h>
#include <cutils/str_parms.h>
#include <utils/Timers.h>
#include <hardware/audio.h>
#include <hardware/hardware.h>
#include <system/audio.h>
#include <tinyalsa/asoundlib.h>
#include <sound/compress_params.h>
#include <sound/compress_offload.h>
#include <tinycompress/tinycompress.h>
#include <audio_utils/resampler.h>
#include "audio_config.h"
#include "voice_trigger.h"
#include <math.h>
#define OUT_PERIOD_SIZE_DEFAULT 1024
#define OUT_PERIOD_COUNT_DEFAULT 4
#define OUT_SAMPLING_RATE_DEFAULT 44100
#define OUT_CHANNEL_MASK_DEFAULT AUDIO_CHANNEL_OUT_STEREO
#define IN_PERIOD_COUNT_DEFAULT 4
#define IN_PERIOD_SIZE_DEFAULT 1024
#define IN_SAMPLING_RATE_DEFAULT 44100
#define IN_CHANNEL_MASK_DEFAULT AUDIO_CHANNEL_IN_MONO
/* AudioFlinger does not re-read the buffer size after
* issuing a routing or input_source change so the
* default buffer size must be suitable for both PCM
* and compressed inputs
*/
#define IN_COMPRESS_BUFFER_SIZE_DEFAULT 8192
/* Maximum time we'll wait for data from a compress_pcm input */
#define MAX_COMPRESS_PCM_TIMEOUT_MS 2100
struct voice_control_trigger {
pthread_t thread;
pthread_mutex_t lock;
pthread_cond_t waitcv;
struct compress *compress;
void (*callback)(void *param);
void *callback_param;
bool own_compress;
volatile bool wait;
volatile bool terminate;
volatile bool triggered;
};
struct audio_device {
struct audio_hw_device hw_device;
pthread_mutex_t lock; /* see note below on mutex acquisition order */
bool standby;
bool mic_mute;
struct config_mgr *cm;
int orientation;
bool screen_off;
struct stream_out_pcm *active_out;
struct stream_in_pcm *active_in;
struct stream_in_pcm *active_voice_control;
};
typedef void(*close_fn)(struct audio_stream *);
/* Fields common to all types of output stream */
struct stream_out_common {
struct audio_stream_out stream;
close_fn close;
struct audio_device *dev;
const struct hw_stream* hw;
pthread_mutex_t lock; /* see note below on mutex acquisition order */
bool standby;
/* Stream parameters as seen by AudioFlinger
* If stream is resampling AudioFlinger buffers before
* passing them to hardware, these members refer to the
* _input_ data from AudioFlinger
*/
audio_format_t format;
uint32_t channel_mask;
int channel_count;
uint32_t sample_rate;
size_t frame_size;
uint32_t buffer_size;
struct {
uint32_t screen_off;
uint32_t screen_on;
} latency;
};
struct stream_out_pcm {
struct stream_out_common common;
struct pcm *pcm;
uint32_t hw_sample_rate; /* actual sample rate of hardware */
int hw_channel_count; /* actual number of output channels */
};
/* Fields common to all types of input stream */
struct stream_in_common {
struct audio_stream_in stream;
close_fn close;
struct audio_device *dev;
const struct hw_stream* hw;
pthread_mutex_t lock; /* see note below on mutex acquisition order */
bool standby;
/* Stream parameters as seen by AudioFlinger
* If stream is resampling AudioFlinger buffers before
* passing them to hardware, these members refer to the
* _input_ data from AudioFlinger
*/
audio_format_t format;
uint32_t channel_mask;
int channel_count;
uint32_t sample_rate;
size_t frame_size;
size_t buffer_size;
int input_source;
};
struct stream_in_pcm {
struct stream_in_common common;
union {
struct pcm *pcm;
struct compress *compress;
};
uint32_t hw_sample_rate; /* actual sample rate of hardware */
int hw_channel_count; /* actual number of input channels */
uint32_t period_size; /* ... of PCM input */
struct resampler_itfe *resampler;
struct resampler_buffer_provider buf_provider;
int16_t *buffer;
size_t in_buffer_size;
int in_buffer_frames;
size_t frames_in;
int read_status;
struct voice_control_trigger *vc_trigger;
};
enum {
ORIENTATION_LANDSCAPE,
ORIENTATION_PORTRAIT,
ORIENTATION_SQUARE,
ORIENTATION_UNDEFINED,
};
static uint32_t out_get_sample_rate(const struct audio_stream *stream);
static uint32_t in_get_sample_rate(const struct audio_stream *stream);
static int get_next_buffer(struct resampler_buffer_provider *buffer_provider,
struct resampler_buffer* buffer);
static void release_buffer(struct resampler_buffer_provider *buffer_provider,
struct resampler_buffer* buffer);
/*
* NOTE: when multiple mutexes have to be acquired, always take the
* audio_device mutex first, followed by the stream_in and/or
* stream_out mutexes.
*/
/*********************************************************************
* Stream common functions
*********************************************************************/
static int common_set_parameters_locked(const struct hw_stream *stream, const char *kvpairs)
{
char *parms = strdup(kvpairs);
char *p, *temp;
char *pval;
char value[32];
int ret;
ALOGV("+common_set_parameters(%p) '%s'", stream, kvpairs);
if (!parms) {
return -ENOMEM;
}
/* It's not obvious what we should do if multiple parameters
* are given and we only understand some. The action taken
* here is to process all that we understand and only return
* and error if we don't understand any
*/
ret = -ENOTSUP;
p = strtok_r(parms, ";", &temp);
while(p) {
pval = strchr(p, '=');
if (pval && (pval[1] != '\0')) {
*pval = '\0';
if (apply_use_case(stream, p, pval+1) >= 0) {
ret = 0;
}
*pval = '=';
}
p = strtok_r(NULL, ";", &temp);
}
return ret;
}
static int common_get_routing_param(uint32_t *vout, const char *kvpairs)
{
struct str_parms *parms;
char value[32];
int ret;
parms = str_parms_create_str(kvpairs);
ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING,
value, sizeof(value));
if (ret >= 0) {
*vout = atoi(value);
}
str_parms_destroy(parms);
return ret;
}
/*********************************************************************
* Output stream common functions
*********************************************************************/
static uint32_t out_get_sample_rate(const struct audio_stream *stream)
{
struct stream_out_common *out = (struct stream_out_common *)stream;
return out->sample_rate;
}
static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
{
return -ENOSYS;
}
static size_t out_get_buffer_size(const struct audio_stream *stream)
{
struct stream_out_common *out = (struct stream_out_common *)stream;
ALOGV("out_get_buffer_size(%p): %u", stream, out->buffer_size );
return out->buffer_size;
}
static uint32_t out_get_channels(const struct audio_stream *stream)
{
struct stream_out_common *out = (struct stream_out_common *)stream;
return out->channel_mask;
}
static audio_format_t out_get_format(const struct audio_stream *stream)
{
struct stream_out_common *out = (struct stream_out_common *)stream;
/*ALOGV("out_get_format(%p): 0x%x", stream, out->format );*/
return out->format;
}
static int out_set_format(struct audio_stream *stream, audio_format_t format)
{
return -ENOSYS;
}
static int out_dump(const struct audio_stream *stream, int fd)
{
return 0;
}
static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
{
ALOGV("+out_set_parameters(%p) '%s'", stream, kvpairs);
struct stream_out_common *out = (struct stream_out_common *)stream;
struct audio_device *adev = out->dev;
uint32_t v;
int ret;
ret = common_get_routing_param(&v, kvpairs);
pthread_mutex_lock(&adev->lock);
if (ret >= 0) {
apply_route(out->hw, v);
}
if (common_set_parameters_locked(out->hw, kvpairs) >= 0) {
ret = 0;
}
pthread_mutex_unlock(&adev->lock);
ALOGV("-out_set_parameters(%p):%d", out, ret);
return ret;
}
static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
{
return strdup("");
}
static uint32_t out_get_latency(const struct audio_stream_out *stream)
{
struct stream_out_common *out = (struct stream_out_common *)stream;
struct audio_device *adev = out->dev;
uint32_t latency;
pthread_mutex_lock(&adev->lock);
if (adev->screen_off && !adev->active_in) {
latency = out->latency.screen_off;
} else {
latency = out->latency.screen_on;
}
pthread_mutex_unlock(&adev->lock);
return latency;
}
static int volume_to_percent(float volume)
{
float decibels;
float percent;
/* Converting back to a decibel scale */
if(volume > 0) {
decibels = log(volume) / 0.115129f;
} else {
/* Use the maximum attenuation value 58 */
decibels = -58;
}
/* decibels range is -58..0, rescale to range 0..100 */
percent = ((decibels + 58.0) * (100.0/58.0));
return (int)percent;
}
static int out_set_volume(struct audio_stream_out *stream, float left, float right)
{
struct stream_out_common *out = (struct stream_out_common *)stream;
int l_pc = volume_to_percent(left);
int r_pc = volume_to_percent(right);
ALOGV("out_set_volume (%f,%f) -> (%d%%,%d%%)", left, right, l_pc, r_pc);
return set_hw_volume(out->hw, l_pc, r_pc);
}
static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
{
return 0;
}
static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
{
return 0;
}
static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
int64_t *timestamp)
{
return -EINVAL;
}
static void do_close_out_common(struct audio_stream *stream)
{
struct stream_out_common *out = (struct stream_out_common *)stream;
release_stream(out->hw);
free(stream);
}
static int do_init_out_common( struct stream_out_common *out,
const struct audio_config *config,
audio_devices_t devices )
{
int ret;
out->standby = true;
out->stream.common.get_sample_rate = out_get_sample_rate;
out->stream.common.set_sample_rate = out_set_sample_rate;
out->stream.common.get_buffer_size = out_get_buffer_size;
out->stream.common.get_channels = out_get_channels;
out->stream.common.get_format = out_get_format;
out->stream.common.set_format = out_set_format;
out->stream.common.dump = out_dump;
out->stream.common.set_parameters = out_set_parameters;
out->stream.common.get_parameters = out_get_parameters;
out->stream.common.add_audio_effect = out_add_audio_effect;
out->stream.common.remove_audio_effect = out_remove_audio_effect;
out->stream.get_latency = out_get_latency;
out->stream.set_volume = out_set_volume;
out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
/* init requested stream config */
out->format = config->format;
out->sample_rate = (config->sample_rate == 0)
? OUT_SAMPLING_RATE_DEFAULT
: config->sample_rate;
out->channel_mask = (config->channel_mask == 0)
? OUT_CHANNEL_MASK_DEFAULT
: config->channel_mask;
out->channel_count = popcount(out->channel_mask);
/* Default settings */
out->frame_size = audio_stream_frame_size(&out->stream.common);
/* Apply initial route */
apply_route(out->hw, devices);
return 0;
}
/*********************************************************************
* PCM output stream
*********************************************************************/
static unsigned int out_pcm_cfg_period_count(struct stream_out_pcm *out)
{
if (out->common.hw->period_count != 0) {
return out->common.hw->period_count;
} else {
return OUT_PERIOD_COUNT_DEFAULT;
}
}
static unsigned int out_pcm_cfg_period_size(struct stream_out_pcm *out)
{
if (out->common.hw->period_size != 0) {
return out->common.hw->period_size;
} else {
return OUT_PERIOD_SIZE_DEFAULT;
}
}
static unsigned int out_pcm_cfg_rate(struct stream_out_pcm *out)
{
if (out->common.hw->rate != 0) {
return out->common.hw->rate;
} else {
return OUT_SAMPLING_RATE_DEFAULT;
}
}
/* must be called with hw device and output stream mutexes locked */
static void do_out_pcm_standby(struct stream_out_pcm *out)
{
struct audio_device *adev = out->common.dev;
ALOGV("+do_out_standby(%p)", out);
if (!out->common.standby) {
pcm_close(out->pcm);
out->pcm = NULL;
adev->active_out = NULL;
out->common.standby = true;
}
ALOGV("-do_out_standby(%p)", out);
}
static void out_pcm_fill_params(struct stream_out_pcm *out,
const struct pcm_config *config )
{
out->hw_sample_rate = config->rate;
out->hw_channel_count = config->channels;
out->common.buffer_size = pcm_frames_to_bytes(out->pcm,
pcm_get_buffer_size(out->pcm));
out->common.latency.screen_on = (config->period_size * config->period_count * 1000)
/ config->rate;
out->common.latency.screen_off = out->common.latency.screen_on;
}
/* must be called with hw device and output stream mutexes locked */
static int start_output_pcm(struct stream_out_pcm *out)
{
struct audio_device *adev = out->common.dev;
int ret;
struct pcm_config config = {
.channels = out->common.channel_count,
.rate = out_pcm_cfg_rate(out),
.period_size = out_pcm_cfg_period_size(out),
.period_count = out_pcm_cfg_period_count(out),
.format = PCM_FORMAT_S16_LE,
.start_threshold = 0,
.stop_threshold = 0,
.silence_threshold = 0
};
ALOGV("+start_output_stream(%p)", out);
out->pcm = pcm_open(out->common.hw->card_number,
out->common.hw->device_number,
PCM_OUT,
&config);
if (out->pcm && !pcm_is_ready(out->pcm)) {
ALOGE("pcm_open(out) failed: %s", pcm_get_error(out->pcm));
pcm_close(out->pcm);
return -ENOMEM;
}
out_pcm_fill_params( out, &config );
adev->active_out = out;
ALOGV("-start_output_stream(%p)", out);
return 0;
}
static int out_pcm_standby(struct audio_stream *stream)
{
struct stream_out_pcm *out = (struct stream_out_pcm *)stream;
pthread_mutex_lock(&out->common.dev->lock);
pthread_mutex_lock(&out->common.lock);
do_out_pcm_standby(out);
pthread_mutex_unlock(&out->common.lock);
pthread_mutex_unlock(&out->common.dev->lock);
return 0;
}
static ssize_t out_pcm_write(struct audio_stream_out *stream, const void* buffer,
size_t bytes)
{
ALOGV("+out_pcm_write(%p) l=%u", stream, bytes);
int ret = 0;
struct stream_out_pcm *out = (struct stream_out_pcm *)stream;
struct audio_device *adev = out->common.dev;
/* Check that we are routed to something. Android can send routing
* commands that tell us to disconnect from everything and in that
* state we shouldn't issue any write commands because we can't be
* sure that the driver will accept a write to nowhere
*/
if (get_current_routes(out->common.hw) == 0) {
ALOGV("-out_pcm_write(%p) 0 (no routes)", stream);
return 0;
}
/*
* acquiring hw device mutex systematically is useful if a low
* priority thread is waiting on the output stream mutex - e.g.
* executing out_set_parameters() while holding the hw device
* mutex
*/
pthread_mutex_lock(&adev->lock);
pthread_mutex_lock(&out->common.lock);
if (out->common.standby) {
ret = start_output_pcm(out);
if (ret != 0) {
pthread_mutex_unlock(&adev->lock);
goto exit;
}
out->common.standby = false;
}
pthread_mutex_unlock(&adev->lock);
ret = pcm_write(out->pcm, buffer, bytes);
if (ret >= 0) {
ret = bytes;
}
exit:
pthread_mutex_unlock(&out->common.lock);
ALOGV("-out_pcm_write(%p) r=%u", stream, ret);
return ret;
}
static int out_pcm_get_render_position(const struct audio_stream_out *stream,
uint32_t *dsp_frames)
{
return -EINVAL;
}
static void do_close_out_pcm(struct audio_stream *stream)
{
out_pcm_standby(stream);
do_close_out_common(stream);
}
static int do_init_out_pcm( struct stream_out_pcm *out,
const struct audio_config *config )
{
if (config->sample_rate != out_pcm_cfg_rate(out)) {
ALOGE("AF requested rate %u not supported", config->sample_rate);
return -ENOTSUP;
}
out->common.close = do_close_out_pcm;
out->common.stream.common.standby = out_pcm_standby;
out->common.stream.write = out_pcm_write;
out->common.stream.get_render_position = out_pcm_get_render_position;
out->common.buffer_size = out_pcm_cfg_period_size(out)
* out_pcm_cfg_period_count(out)
* out->common.frame_size;
return 0;
}
/*********************************************************************
* Input stream common functions
*********************************************************************/
static uint32_t in_get_sample_rate(const struct audio_stream *stream)
{
const struct stream_in_common *in = (struct stream_in_common *)stream;
return in->sample_rate;
}
static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
{
const struct stream_in_common *in = (struct stream_in_common *)stream;
if (rate == in->sample_rate) {
return 0;
} else {
return -ENOTSUP;
}
}
static audio_channel_mask_t in_get_channels(const struct audio_stream *stream)
{
const struct stream_in_common *in = (struct stream_in_common *)stream;
return in->channel_mask;
}
static audio_format_t in_get_format(const struct audio_stream *stream)
{
const struct stream_in_common *in = (struct stream_in_common *)stream;
return in->format;
}
static int in_set_format(struct audio_stream *stream, audio_format_t format)
{
return -ENOSYS;
}
static size_t in_get_buffer_size(const struct audio_stream *stream)
{
const struct stream_in_common *in = (struct stream_in_common *)stream;
ALOGV("in_get_buffer_size(%p): %u", stream, in->buffer_size );
return in->buffer_size;
}
static int in_dump(const struct audio_stream *stream, int fd)
{
return 0;
}
static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
{
return 0;
}
static char * in_get_parameters(const struct audio_stream *stream,
const char *keys)
{
return strdup("");
}
static int in_set_gain(struct audio_stream_in *stream, float gain)
{
return 0;
}
static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
{
return 0;
}
static int in_add_audio_effect(const struct audio_stream *stream,
effect_handle_t effect)
{
return 0;
}
static int in_remove_audio_effect(const struct audio_stream *stream,
effect_handle_t effect)
{
return 0;
}
static void do_close_in_common(struct audio_stream *stream)
{
struct stream_in_common *in = (struct stream_in_common *)stream;
in->stream.common.standby(stream);
/* active_voice_control is not cleared by standby so we must
* clear it here when stream is closed
*/
if ((struct stream_in_common *)in->dev->active_voice_control == in) {
in->dev->active_voice_control = NULL;
}
release_stream(in->hw);
free(stream);
}
static int do_init_in_common( struct stream_in_common *in,
const struct audio_config *config,
audio_devices_t devices )
{
in->standby = true;
in->close = do_close_in_common;
in->stream.common.get_sample_rate = in_get_sample_rate;
in->stream.common.set_sample_rate = in_set_sample_rate;
in->stream.common.get_buffer_size = in_get_buffer_size;
in->stream.common.get_channels = in_get_channels;
in->stream.common.get_format = in_get_format;
in->stream.common.set_format = in_set_format;
in->stream.common.dump = in_dump;
in->stream.common.set_parameters = in_set_parameters;
in->stream.common.get_parameters = in_get_parameters;
in->stream.common.add_audio_effect = in_add_audio_effect;
in->stream.common.remove_audio_effect = in_remove_audio_effect;
in->stream.set_gain = in_set_gain;
in->stream.get_input_frames_lost = in_get_input_frames_lost;
/* init requested stream config */
in->format = config->format;
in->sample_rate = (config->sample_rate == 0)
? IN_SAMPLING_RATE_DEFAULT
: config->sample_rate;
in->channel_mask = (config->channel_mask == 0)
? IN_CHANNEL_MASK_DEFAULT
: config->channel_mask;
in->channel_count = popcount(in->channel_mask);
in->frame_size = audio_stream_frame_size(&in->stream.common);
/* Apply initial routing */
apply_route(in->hw, devices);
return 0;
}
/*********************************************************************
* Voice control triggering
* Currently assumes a compressed channel
*********************************************************************/
static void* voice_control_trigger_thread(void *param)
{
struct voice_control_trigger *self = param;
struct compress *compress;
int ret;
/* signal that we're alive and ready */
pthread_cond_signal(&self->waitcv);
while (!self->terminate) {
pthread_mutex_lock(&self->lock);
if (!self->wait) {
pthread_cond_wait(&self->waitcv, &self->lock);
}
self->wait = false;
pthread_mutex_unlock(&self->lock);
if (self->terminate) {
break;
}
/* We must protect against any failure by the main thread to open
* the compressed channel
*/
compress = self->compress;
if (compress != NULL) {
ALOGV("VC wait");
ret = compress_wait(compress, -1);
if (self->terminate) {
break;
}
if (ret == 0) {
self->triggered = true;
ALOGV("VC trigger %d", ret);
(self->callback)(self->callback_param);
}
}
}
if (self->own_compress && self->compress) {
ALOGV("VC trigger thread closes compress");
compress_close(self->compress);
}
free(self);
ALOGV("VC trigger thread terminates");
return NULL;
}
static void start_voice_control_wait(struct voice_control_trigger *self)
{
ALOGV("start_voice_control_wait");
pthread_mutex_lock(&self->lock);
self->triggered = false;
self->wait = true;
pthread_cond_signal(&self->waitcv);
pthread_mutex_unlock(&self->lock);
}
static void cancel_voice_control_wait(struct voice_control_trigger *in)
{
ALOGV("cancel_voice_control_wait");
}
static struct voice_control_trigger* create_voice_control_trigger()
{
static struct voice_control_trigger *t;
int ret;
t = calloc(1, sizeof(struct voice_control_trigger));
if (t != NULL) {
pthread_mutex_init(&t->lock, NULL);
pthread_cond_init(&t->waitcv, NULL);
pthread_mutex_lock(&t->lock);
ret = pthread_create(&t->thread, NULL, voice_control_trigger_thread, t);
if (ret != 0) {
goto fail;
}
/* Wait for thread to initialize */
pthread_cond_wait(&t->waitcv, &t->lock);
pthread_mutex_unlock(&t->lock);
}
return t;
fail:
free(t);
return NULL;
}
static void destroy_voice_control_trigger(struct voice_control_trigger *self,
bool close_channel)
{
ALOGV("+destroy_voice_control_trigger (close=%d)", close_channel);
if (self != NULL) {
pthread_mutex_lock(&self->lock);
if (close_channel && (self->compress != NULL)) {
/* Take ownership of the compressed channel and close */
/* it as we terminate */
/* Must stop the channel to force thread out of the poll */
self->own_compress = true;
self->terminate = true;
/* Kick the driver to create an error condition */
/* so wait thread will exit the poll */
if (!is_compress_running(self->compress)) {
/* If it's not running, do a dummy start so we */
/* can force a stop */
compress_start(self->compress);
}
compress_stop(self->compress);
}
pthread_cond_signal(&self->waitcv);
pthread_mutex_unlock(&self->lock);
/* A struct voice_control_trigger cannot be created without a thread */
/* so it is safe to just call pthread_join() here */
pthread_join(self->thread, NULL);
}
ALOGV("-destroy_voice_control_trigger");
}
static void voice_control_callback(void *param)
{
struct stream_in_pcm *in = param;
if (in->common.standby) {
ALOGV("VC trig");
send_voice_trigger();
} else {
ALOGV("VC trig ignored (not in standby)");
}
}
static int init_voice_control(struct stream_in_pcm *in)
{
struct audio_device *adev = in->common.dev;
struct voice_control_trigger *t;
int ret;
ALOGV("+init_voice_control");
if (!stream_is_compressed(in->common.hw)) {
/* We don't support triggering on PCM streams */
ret = -EINVAL;
goto out;
}
t = create_voice_control_trigger();
if (t == NULL) {
ret = -ENOMEM;
goto out;
}
t->compress = in->compress;
t->callback = voice_control_callback;
t->callback_param = in;
in->vc_trigger = t;
ret = 0;
out:
ALOGV("-init_voice_control %d", ret);
return ret;
}
/*********************************************************************
* PCM input stream via compressed channel
*********************************************************************/
/* must be called with hw device and input stream mutexes locked */
static int do_open_compress_pcm_in(struct stream_in_pcm *in)
{
struct snd_codec codec;
struct compress *compress;
int ret;
ALOGV("+do_open_compress_pcm_in");
memset(&codec, 0, sizeof(codec));
codec.id = SND_AUDIOCODEC_PCM;
codec.ch_in = in->common.channel_count;
codec.sample_rate = in->common.sample_rate;
codec.format = SNDRV_PCM_FORMAT_S16_LE;
/* Fragment and buffer sizes should be configurable or auto-detected
* but are currently just hardcoded
*/
struct compr_config config = {
.fragment_size = 4096,
.fragments = 1,
.codec = &codec
};
compress = compress_open(in->common.hw->card_number,
in->common.hw->device_number,