-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathaudio_hw.c
1855 lines (1622 loc) · 68.5 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
#define LOG_TAG "scr_audio"
#define LOG_NDEBUG 0
#include <errno.h>
#include <pthread.h>
#include <stdint.h>
#include <sys/time.h>
#include <dirent.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <ctype.h>
#include <stdlib.h>
#include <math.h>
#include <cutils/log.h>
#include <cutils/bitops.h>
#include <hardware/hardware.h>
#include <system/audio.h>
#include <hardware/audio.h>
#if SCR_SDK_VERSION < 16
#define ALOGV(...) LOGV(__VA_ARGS__)
#define ALOGD(...) LOGD(__VA_ARGS__)
#define ALOGI(...) LOGI(__VA_ARGS__)
#define ALOGW(...) LOGW(__VA_ARGS__)
#define ALOGE(...) LOGE(__VA_ARGS__)
#endif
#include <dlfcn.h>
#define BUFFER_SIZE (128 * 1024)
#define MAX_WAIT_READ_RETRY 20
#define READ_RETRY_WAIT 5000
// maximum number of buffers which may be passed one by one (with 1ms sleeps) on out wakeup
#define MAX_CONSECUTIVE_BUFFERS 3
// buffer size should be above 10ms to avoid scheduling related issues
#define MIN_BUFFER_SIZE 440
#define MAX_DEVICE_BUFFER_SIZE (8 * 1024)
#define MAX_LOGS 10
#if SCR_SDK_VERSION >= 16
struct audio_hw_device_qcom {
struct hw_device_t common;
uint32_t (*get_supported_devices)(const struct audio_hw_device *dev);
int (*init_check)(const struct audio_hw_device *dev);
int (*set_voice_volume)(struct audio_hw_device *dev, float volume);
int (*set_master_volume)(struct audio_hw_device *dev, float volume);
int (*get_master_volume)(struct audio_hw_device *dev, float *volume);
int padding;
int (*set_mode)(struct audio_hw_device *dev, audio_mode_t mode);
int (*set_mic_mute)(struct audio_hw_device *dev, bool state);
int (*get_mic_mute)(const struct audio_hw_device *dev, bool *state);
int (*set_parameters)(struct audio_hw_device *dev, const char *kv_pairs);
char * (*get_parameters)(const struct audio_hw_device *dev, const char *keys);
size_t (*get_input_buffer_size)(const struct audio_hw_device *dev, const struct audio_config *config);
int (*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);
void (*close_output_stream)(struct audio_hw_device *dev, struct audio_stream_out* stream_out);
int (*open_input_stream)(struct audio_hw_device *dev, audio_io_handle_t handle, audio_devices_t devices,
struct audio_config *config, struct audio_stream_in **stream_in);
void (*close_input_stream)(struct audio_hw_device *dev, struct audio_stream_in *stream_in);
int (*dump)(const struct audio_hw_device *dev, int fd);
};
typedef struct audio_hw_device_qcom audio_hw_device_qcom_t;
static void convert_to_qcom(struct audio_hw_device *device);
static void disable_qcom_detection(struct audio_hw_device *device);
#endif
struct scr_audio_device {
union audio_hw_device_union {
audio_hw_device_t current;
#if SCR_SDK_VERSION >= 16
audio_hw_device_qcom_t qcom;
#else
audio_hw_device_t qcom;
#endif
} device;
int padding[10];
union {
audio_hw_device_t *current;
#if SCR_SDK_VERSION >= 16
audio_hw_device_qcom_t *qcom;
#else
audio_hw_device_t *qcom;
#endif
} primary;
bool qcom;
// naive pipe implementation
int buffer_start;
int buffer_end;
union {
int16_t* int16;
int32_t* int32;
int8_t* int8;
} buffer;
bool out_active;
bool in_active;
bool in_open;
int out_channels;
audio_format_t out_format;
uint32_t out_sample_rate;
int out_frame_size;
bool verbose_logging;
int num_out_streams;
struct scr_stream_out *recorded_stream;
pthread_mutex_t lock;
};
struct scr_stream_out {
struct audio_stream_out stream;
int padding[10];
struct audio_stream_out *primary;
struct scr_audio_device *dev;
int stream_no;
int stats_overflows;
};
struct scr_stream_in {
struct audio_stream_in stream;
int padding[10];
struct audio_stream_in *primary;
struct scr_audio_device *dev;
uint32_t channel_mask;
uint32_t sample_rate;
int out_sample_rate_divider;
int volume_gain;
int64_t in_start_us;
int64_t frames_read;
bool recording_silence;
bool mix_mic;
int mic_gain;
int stats_data;
int stats_silence;
int stats_in_buffer_size;
int stats_out_buffer_size;
int64_t stats_latency;
int stats_latency_max;
int stats_late_buffers;
int stats_start_latency_tmp;
int stats_start_latency_max;
int stats_start_latency;
int stats_starts;
int stats_delays;
int stats_excess;
int stats_consecutive_silence;
};
static inline int64_t get_time_us() {
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
return (int64_t) now.tv_sec * 1000000ll + now.tv_nsec / 1000ll;
}
static int get_available_frames(struct scr_audio_device *device, struct scr_stream_in *scr_stream) {
if (device->out_frame_size == 0)
return 0;
return ((device->buffer_end + BUFFER_SIZE - device->buffer_start) % BUFFER_SIZE) / (device->out_frame_size * scr_stream->out_sample_rate_divider);
}
static uint32_t out_get_sample_rate(const struct audio_stream *stream)
{
struct scr_stream_out *scr_stream = (struct scr_stream_out *)stream;
struct audio_stream *primary = &scr_stream->primary->common;
return primary->get_sample_rate(primary);
}
static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
{
struct scr_stream_out *scr_stream = (struct scr_stream_out *)stream;
struct audio_stream *primary = &scr_stream->primary->common;
if (scr_stream == scr_stream->dev->recorded_stream) {
ALOGW("attempt to change sample rate of recorded steam %d", rate);
return -EINVAL;
}
return primary->set_sample_rate(primary, rate);
}
static size_t out_get_buffer_size(const struct audio_stream *stream)
{
struct scr_stream_out *scr_stream = (struct scr_stream_out *)stream;
struct audio_stream *primary = &scr_stream->primary->common;
return primary->get_buffer_size(primary);
}
static uint32_t out_get_channels(const struct audio_stream *stream)
{
struct scr_stream_out *scr_stream = (struct scr_stream_out *)stream;
struct audio_stream *primary = &scr_stream->primary->common;
return primary->get_channels(primary);
}
static audio_format_t out_get_format(const struct audio_stream *stream)
{
struct scr_stream_out *scr_stream = (struct scr_stream_out *)stream;
struct audio_stream *primary = &scr_stream->primary->common;
return primary->get_format(primary);
}
static int out_set_format(struct audio_stream *stream, audio_format_t format)
{
struct scr_stream_out *scr_stream = (struct scr_stream_out *)stream;
struct audio_stream *primary = &scr_stream->primary->common;
if (scr_stream == scr_stream->dev->recorded_stream) {
ALOGW("attempt to change format of recorded steam %d", format);
return -EINVAL;
}
return primary->set_format(primary, format);
}
static int out_standby(struct audio_stream *stream)
{
struct scr_stream_out *scr_stream = (struct scr_stream_out *)stream;
struct audio_stream *primary = &scr_stream->primary->common;
if (scr_stream == scr_stream->dev->recorded_stream) {
scr_stream->dev->out_active = false;
}
if (scr_stream->dev->verbose_logging) {
ALOGV("%s %p", __func__, stream);
}
return primary->standby(primary);
}
static int out_dump(const struct audio_stream *stream, int fd)
{
struct scr_stream_out *scr_stream = (struct scr_stream_out *)stream;
struct audio_stream *primary = &scr_stream->primary->common;
return primary->dump(primary, fd);
}
static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
{
struct scr_stream_out *scr_stream = (struct scr_stream_out *)stream;
struct audio_stream *primary = &scr_stream->primary->common;
ALOGV("%s %p %s", __func__, stream, kvpairs);
int result = primary->set_parameters(primary, kvpairs);
ALOGV("%s %p %s result: %d", __func__, stream, kvpairs, result);
return result;
}
static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
{
struct scr_stream_out *scr_stream = (struct scr_stream_out *)stream;
struct audio_stream *primary = &scr_stream->primary->common;
return primary->get_parameters(primary, keys);
}
static uint32_t out_get_latency(const struct audio_stream_out *stream)
{
struct scr_stream_out *scr_stream = (struct scr_stream_out *)stream;
struct audio_stream_out *primary = scr_stream->primary;
return primary->get_latency(primary);
}
static int out_set_volume(struct audio_stream_out *stream, float left,
float right)
{
struct scr_stream_out *scr_stream = (struct scr_stream_out *)stream;
struct audio_stream_out *primary = scr_stream->primary;
if (scr_stream->dev->verbose_logging) {
ALOGV("%s %p %f %f", __func__, stream, left, right);
}
return primary->set_volume(primary, left, right);
}
static void buffer_write(struct scr_audio_device *dev, const void *src, size_t bytes) {
size_t free_space = BUFFER_SIZE - dev->buffer_end + dev->buffer_start;
if (bytes > free_space) {
if (dev->in_active) {
ALOGE("SCR driver buffer overrun!");
dev->recorded_stream->stats_overflows++;
}
if (bytes > BUFFER_SIZE) { // very unlikely
ALOGE("output buffer too big %d", bytes);
bytes = BUFFER_SIZE;
}
dev->buffer_start = (dev->buffer_end + bytes + 1) % BUFFER_SIZE;
}
int buffer_end = (dev->buffer_end + bytes) % BUFFER_SIZE;
if (buffer_end > dev->buffer_end) {
memcpy(&dev->buffer.int8[dev->buffer_end], src, bytes);
} else {
size_t first_part_size = BUFFER_SIZE - dev->buffer_end;
memcpy(&dev->buffer.int8[dev->buffer_end], src, first_part_size);
memcpy(&dev->buffer.int8[0], (int8_t *) src + first_part_size, buffer_end);
}
dev->buffer_end = buffer_end;
}
static ssize_t out_write(struct audio_stream_out *stream, const void* buffer,
size_t bytes)
{
struct scr_stream_out *scr_stream = (struct scr_stream_out *)stream;
struct audio_stream_out *primary = scr_stream->primary;
struct scr_audio_device *device = scr_stream->dev;
if (device->verbose_logging) {
ALOGV("%s %p %p %d", __func__, stream, buffer, bytes);
}
pthread_mutex_lock(&device->lock);
if (!device->out_active) {
device->out_active = true;
device->buffer_start = 0;
device->buffer_end = 0;
ALOGD("Output marked active");
}
pthread_mutex_unlock(&device->lock);
ssize_t result = primary->write(primary, buffer, bytes);
if (device->verbose_logging) {
ALOGV("%s result: %d", __func__, (int) result);
}
pthread_mutex_lock(&device->lock);
if (result > 0 && device->recorded_stream == scr_stream && device->in_open) {
buffer_write(device, buffer, result);
}
pthread_mutex_unlock(&device->lock);
return result;
}
static int out_get_render_position(const struct audio_stream_out *stream,
uint32_t *dsp_frames)
{
struct scr_stream_out *scr_stream = (struct scr_stream_out *)stream;
struct audio_stream_out *primary = scr_stream->primary;
return primary->get_render_position(primary, dsp_frames);
}
static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
{
struct scr_stream_out *scr_stream = (struct scr_stream_out *)stream;
struct audio_stream *primary = &scr_stream->primary->common;
return primary->add_audio_effect(primary, effect);
}
static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
{
struct scr_stream_out *scr_stream = (struct scr_stream_out *)stream;
struct audio_stream *primary = &scr_stream->primary->common;
return primary->remove_audio_effect(primary, effect);
}
#if SCR_SDK_VERSION >= 16
static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
int64_t *timestamp)
{
struct scr_stream_out *scr_stream = (struct scr_stream_out *)stream;
struct audio_stream_out *primary = scr_stream->primary;
return primary->get_next_write_timestamp(primary, timestamp);
}
#endif // SCR_SDK_VERSION >= 16
/** audio_stream_in implementation **/
static uint32_t in_get_sample_rate(const struct audio_stream *stream)
{
struct scr_stream_in *scr_stream = (struct scr_stream_in *)stream;
struct audio_stream *primary = &scr_stream->primary->common;
if (!scr_stream->mix_mic && primary)
return primary->get_sample_rate(primary);
return scr_stream->sample_rate;
}
static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
{
struct scr_stream_in *scr_stream = (struct scr_stream_in *)stream;
struct audio_stream *primary = &scr_stream->primary->common;
if (!scr_stream->mix_mic && primary)
return primary->set_sample_rate(primary, rate);
if (rate != scr_stream->sample_rate) {
ALOGW("attempt to change sample rate to %d", rate);
return -EINVAL;
}
return 0;
}
static inline size_t stream_out_frame_size(const struct audio_stream_out *s)
{
#if SCR_SDK_VERSION >= 21
return audio_stream_out_frame_size(s);
#elif SCR_SDK_VERSION < 17
return audio_stream_frame_size((struct audio_stream *) s); // cast to remove warning
#else
return audio_stream_frame_size(&s->common);
#endif
}
static inline size_t stream_in_frame_size(const struct audio_stream_in *s) {
#if SCR_SDK_VERSION >= 21
return audio_stream_in_frame_size(s);
#elif SCR_SDK_VERSION < 17
return audio_stream_frame_size((struct audio_stream *) s); // cast to remove warning
#else
return audio_stream_frame_size(&s->common);
#endif
}
static inline size_t stream_out_bufer_frames(const struct audio_stream_out *s) {
return s->common.get_buffer_size(&s->common) / stream_out_frame_size(s);
}
#if SCR_SDK_VERSION < 16
typedef uint32_t audio_channel_mask_t;
#endif
#if SCR_SDK_VERSION < 21
static inline uint32_t audio_channel_count_from_in_mask(audio_channel_mask_t channel)
{
return popcount(channel);
}
static inline uint32_t audio_channel_count_from_out_mask(audio_channel_mask_t channel)
{
return popcount(channel);
}
#endif
static inline size_t stream_out_channel_count(const struct audio_stream_out *s)
{
audio_channel_mask_t channel = s->common.get_channels(&s->common);
return audio_channel_count_from_out_mask(channel);
}
static inline size_t stream_in_channel_count(const struct audio_stream_in *s)
{
audio_channel_mask_t channel = s->common.get_channels(&s->common);
return audio_channel_count_from_in_mask(channel);
}
static inline void log_input_stream_info(struct scr_stream_in *scr_stream) {
struct audio_stream_in *stream_in = &scr_stream->stream;
struct audio_stream *stream = &stream_in->common;
ALOGV("stream info %p channels: 0x%08X channel count: %d format: 0x%08X", scr_stream, stream->get_channels(stream), stream_in_channel_count(stream_in), stream->get_format(stream));
}
static size_t in_get_buffer_size(const struct audio_stream *stream)
{
struct scr_stream_in *scr_stream = (struct scr_stream_in *)stream;
struct audio_stream *primary = &scr_stream->primary->common;
if (primary)
return primary->get_buffer_size(primary);
struct scr_stream_out *scr_stream_out = scr_stream->dev->recorded_stream;
if (scr_stream_out == NULL) {
return 2048;
}
size_t out_size = stream_out_bufer_frames(&scr_stream_out->stream);
size_t in_size = out_size;
while (in_size < MIN_BUFFER_SIZE) {
in_size += out_size;
}
if (scr_stream->dev->verbose_logging) {
ALOGD("Setting buffer size to %d frames (output %d)", in_size, out_size);
}
return in_size * stream_in_frame_size((struct audio_stream_in *) stream);
}
static uint32_t in_get_channels(const struct audio_stream *stream)
{
struct scr_stream_in *scr_stream = (struct scr_stream_in *)stream;
struct audio_stream *primary = &scr_stream->primary->common;
if (primary)
return primary->get_channels(primary);
return scr_stream->channel_mask;
}
static audio_format_t in_get_format(const struct audio_stream *stream)
{
struct scr_stream_in *scr_stream = (struct scr_stream_in *)stream;
struct audio_stream *primary = &scr_stream->primary->common;
if (!scr_stream->mix_mic && primary)
return primary->get_format(primary);
return AUDIO_FORMAT_PCM_16_BIT;
}
static int in_set_format(struct audio_stream *stream, audio_format_t format)
{
struct scr_stream_in *scr_stream = (struct scr_stream_in *)stream;
struct audio_stream *primary = &scr_stream->primary->common;
if (!scr_stream->mix_mic && primary)
return primary->set_format(primary, format);
if (format != AUDIO_FORMAT_PCM_16_BIT) {
if (scr_stream->dev->verbose_logging) {
ALOGW("%s %p %d", __func__, stream, format);
}
return -EINVAL;
}
return 0;
}
static int in_standby(struct audio_stream *stream)
{
ALOGV("in standby %p", stream);
struct scr_stream_in *scr_stream = (struct scr_stream_in *)stream;
struct scr_audio_device *device = scr_stream->dev;
struct audio_stream *primary = &scr_stream->primary->common;
if (primary) {
if (scr_stream->mix_mic) {
int res = primary->standby(primary);
if (res != 0)
return res;
} else {
return primary->standby(primary);
}
}
pthread_mutex_lock(&device->lock);
device->in_active = false;
pthread_mutex_unlock(&device->lock);
return 0;
}
static int in_dump(const struct audio_stream *stream, int fd)
{
struct scr_stream_in *scr_stream = (struct scr_stream_in *)stream;
struct audio_stream *primary = &scr_stream->primary->common;
if (primary)
return primary->dump(primary, fd);
return 0;
}
static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
{
struct scr_stream_in *scr_stream = (struct scr_stream_in *)stream;
struct audio_stream *primary = &scr_stream->primary->common;
//TODO: make sure that sample rate and format is not changed here
if (primary) {
ALOGV("%s %p %s", __func__, stream, kvpairs);
int result = primary->set_parameters(primary, kvpairs);
ALOGV("%s %p %s result: %d", __func__, stream, kvpairs, result);
return result;
}
ALOGD("ignoring in_set_parameters %s", kvpairs);
return -EINVAL;
}
static char * in_get_parameters(const struct audio_stream *stream,
const char *keys)
{
struct scr_stream_in *scr_stream = (struct scr_stream_in *)stream;
struct audio_stream *primary = &scr_stream->primary->common;
if (primary)
return primary->get_parameters(primary, keys);
return strdup("");
}
static int in_set_gain(struct audio_stream_in *stream, float gain)
{
struct scr_stream_in *scr_stream = (struct scr_stream_in *)stream;
struct audio_stream_in *primary = scr_stream->primary;
if (primary)
return primary->set_gain(primary, gain);
return 0;
}
static int get_target_frames_count(struct scr_stream_in *scr_stream, int frames_to_read) {
int64_t delay_us = get_time_us() - scr_stream->in_start_us;
return (delay_us * scr_stream->sample_rate) / 1000000ll - scr_stream->frames_read + frames_to_read; // frames_to_read is an additional padding buffer
}
static void skip_to_frame_count(struct scr_audio_device *device, struct scr_stream_in *scr_stream, int frames_count) {
int available_frames = get_available_frames(device, scr_stream);
device->buffer_start = (device->buffer_start + (available_frames - frames_count) * device->out_frame_size) % BUFFER_SIZE;
}
static void skip_excess_frames(struct scr_audio_device *device, struct scr_stream_in *scr_stream, int frames_to_read) {
int target_frames_count = get_target_frames_count(scr_stream, frames_to_read);
int available_frames = get_available_frames(device, scr_stream);
if (available_frames > target_frames_count) {
skip_to_frame_count(device, scr_stream, target_frames_count);
}
}
static void activate_input(struct scr_audio_device *device, struct scr_stream_in *scr_stream, int frames_to_read, int64_t duration) {
ALOGD("Input active %p", scr_stream);
log_input_stream_info(scr_stream);
// in_active is set after initial sleep to avoid overflows at startup
scr_stream->in_start_us = get_time_us();
scr_stream->frames_read = 0L;
scr_stream->stats_in_buffer_size = frames_to_read;
pthread_mutex_unlock(&device->lock);
if (device->verbose_logging) {
ALOGV("sleep %lld ms", duration);
}
usleep(duration);
pthread_mutex_lock(&device->lock);
device->in_active = true;
skip_excess_frames(device, scr_stream, frames_to_read);
}
static void wait_for_ret_time(struct scr_audio_device *device, int64_t ret_time) {
int64_t now = get_time_us();
pthread_mutex_unlock(&device->lock);
if (ret_time > now) {
if (device->verbose_logging) {
ALOGV("sleep %lld ms", (ret_time - now) / 1000ll);
}
usleep(ret_time - now);
} else {
usleep(1000); // give 1ms time for consumer to catch up
}
pthread_mutex_lock(&device->lock);
}
static void wait_for_frames(struct scr_audio_device *device, struct scr_stream_in *scr_stream, int frames_to_read, int64_t duration) {
int available_frames = get_available_frames(device, scr_stream);
//TODO: replace loops below with single consistent loop
if (scr_stream->recording_silence) {
// output just started, allow some time for output streem initialization
// later the output stream may catch up by accepting couple buffers one by one
int buffers_waited = 0;
while (device->out_active && ++buffers_waited < MAX_CONSECUTIVE_BUFFERS && available_frames < frames_to_read) {
pthread_mutex_unlock(&device->lock);
if (device->verbose_logging) {
ALOGD("out wakeup wait %lld ms", buffers_waited * duration / 1000ll);
}
usleep(duration);
pthread_mutex_lock(&device->lock);
available_frames = get_available_frames(device, scr_stream);
}
} else {
int attempts = 0;
while (device->out_active && attempts < MAX_WAIT_READ_RETRY && available_frames < frames_to_read) {
pthread_mutex_unlock(&device->lock);
attempts++;
usleep(READ_RETRY_WAIT);
pthread_mutex_lock(&device->lock);
available_frames = get_available_frames(device, scr_stream);
}
int latency = (attempts * READ_RETRY_WAIT / 1000ll);
if (device->verbose_logging || scr_stream->stats_late_buffers < MAX_LOGS) {
ALOGW("waited extra %d ms", latency);
}
if (scr_stream->stats_latency_max < latency) {
scr_stream->stats_latency_max = latency;
}
scr_stream->stats_latency += latency;
scr_stream->stats_late_buffers++;
if (attempts == MAX_WAIT_READ_RETRY && available_frames < frames_to_read) {
scr_stream->stats_delays++;
if (device->verbose_logging || scr_stream->stats_delays < MAX_LOGS) {
ALOGE("output data not received on time. available %d", available_frames);
}
// will record remaining frames and start recording silence
}
}
}
static bool should_extend_silence(struct scr_audio_device *device, struct scr_stream_in *scr_stream, int sample_rate, int frames_to_read, int available_frames) {
if (scr_stream->recording_silence && device->out_active) {
int64_t duration = frames_to_read * 1000000ll / sample_rate;
int frames_to_catch_up = get_target_frames_count(scr_stream, frames_to_read);
if (available_frames < frames_to_catch_up) {
if (device->verbose_logging) {
ALOGD("not enough frames available %d should be %d, writing silence", available_frames, frames_to_catch_up);
}
scr_stream->stats_start_latency_tmp += (int64_t) duration / 1000ll;
return true;
}
}
return false;
}
static void log_start_stats(struct scr_stream_in *scr_stream, int64_t ret_time) {
scr_stream->stats_starts++;
int64_t now = get_time_us();
int latency = (int64_t)(now - ret_time) / 1000ll + scr_stream->stats_start_latency_tmp;
if (scr_stream->stats_start_latency_max < latency) {
scr_stream->stats_start_latency_max = latency;
}
scr_stream->stats_start_latency += latency;
scr_stream->stats_start_latency_tmp = 0;
}
static inline int32_t get_32bit_out_sample(struct scr_audio_device *device) {
int sample = 0;
if (device->out_format == AUDIO_FORMAT_PCM_16_BIT) {
sample = device->buffer.int16[device->buffer_start / 2] << 16;
device->buffer_start = (device->buffer_start + 2) % BUFFER_SIZE;
} else { // assume 32bit
sample = device->buffer.int32[device->buffer_start / 4];
device->buffer_start = (device->buffer_start + 4) % BUFFER_SIZE;
}
return sample;
}
static inline int32_t get_32bit_mono_out_sample(struct scr_audio_device *device) {
if (device->out_channels == 1) {
return get_32bit_out_sample(device);
} else { // out_channels == 2
return get_32bit_out_sample(device) / 2 + get_32bit_out_sample(device) / 2;
}
}
static inline int32_t get_32bit_mono_frame(struct scr_audio_device *device, struct scr_stream_in *scr_stream) {
switch (scr_stream->out_sample_rate_divider) {
case 1:
return get_32bit_mono_out_sample(device);
case 2:
return get_32bit_mono_out_sample(device) / 2
+ get_32bit_mono_out_sample(device) / 2;
case 3:
return get_32bit_mono_out_sample(device) / 3
+ get_32bit_mono_out_sample(device) / 3
+ get_32bit_mono_out_sample(device) / 3;
case 4:
return get_32bit_mono_out_sample(device) / 4
+ get_32bit_mono_out_sample(device) / 4
+ get_32bit_mono_out_sample(device) / 4
+ get_32bit_mono_out_sample(device) / 4;
default:
return 0;
}
}
static inline void get_32bit_stereo_frame(struct scr_audio_device *device, struct scr_stream_in *scr_stream, int32_t *left, int32_t *right) {
if (scr_stream->out_sample_rate_divider == 1) {
*left = get_32bit_out_sample(device);
*right = get_32bit_out_sample(device);
return;
}
int32_t l = 0, r = 0;
int i;
for (i = 0; i < scr_stream->out_sample_rate_divider; i++) {
l += get_32bit_out_sample(device) / scr_stream->out_sample_rate_divider;
r += get_32bit_out_sample(device) / scr_stream->out_sample_rate_divider;
}
*left = l;
*right = r;
}
static inline int32_t apply_gain(struct scr_stream_in *scr_stream, int32_t unscaled) {
int32_t sample = unscaled / ((1<<16) / scr_stream->volume_gain);
while ((sample > INT16_MAX || sample < INT16_MIN) && scr_stream->volume_gain > 1) {
scr_stream->volume_gain--;
ALOGW("Reducing volume gain to %d", scr_stream->volume_gain);
sample = unscaled / ((1<<16) / scr_stream->volume_gain);
}
return sample;
}
static int copy_frames(struct scr_audio_device *device, struct scr_stream_in *scr_stream, int16_t *dst, int frames_to_read, int available_frames) {
int frames_read = 0;
int channels_count = stream_in_channel_count(&scr_stream->stream);
if (channels_count == 1) { // mono
for (frames_read; frames_read < frames_to_read && frames_read < available_frames; frames_read++) {
dst[frames_read] = apply_gain(scr_stream, get_32bit_mono_frame(device, scr_stream));
}
} else { // stereo
int32_t l, r;
for (frames_read; frames_read < frames_to_read && frames_read < available_frames; frames_read++) {
get_32bit_stereo_frame(device, scr_stream, &l, &r);
dst[frames_read * 2] = apply_gain(scr_stream, l);
dst[frames_read * 2 + 1] = apply_gain(scr_stream, r);
}
}
return frames_read;
}
static inline int16_t clip_to_16bit(int32_t sample) {
if (sample > INT16_MAX) {
return INT16_MAX;
} else if (sample < INT16_MIN) {
return INT16_MIN;
}
return sample;
}
static int mix_frames(struct scr_audio_device *device, struct scr_stream_in *scr_stream, int16_t *dst, int frames_to_read, int available_frames) {
int i = 0;
int channels_count = stream_in_channel_count(&scr_stream->stream);
if (channels_count == 1) { // mono
for (i; i < frames_to_read; i++) {
if (i < available_frames) {
int32_t sample = dst[i] * scr_stream->mic_gain + apply_gain(scr_stream, get_32bit_mono_frame(device, scr_stream));
dst[i] = clip_to_16bit(sample);
} else {
dst[i] = clip_to_16bit(dst[i] * scr_stream->mic_gain);
}
}
} else { // stereo
int32_t l, r;
for (i; i < frames_to_read; i++) {
if (i < available_frames) {
get_32bit_stereo_frame(device, scr_stream, &l, &r);
l = apply_gain(scr_stream, l) + dst[i * 2] * scr_stream->mic_gain;
r = apply_gain(scr_stream, r) + dst[i * 2 + 1] * scr_stream->mic_gain;
dst[i * 2] = clip_to_16bit(l);
dst[i * 2 + 1] = clip_to_16bit(r);
} else {
dst[i * 2] = clip_to_16bit(dst[i * 2] * scr_stream->mic_gain);
dst[i * 2 + 1] = clip_to_16bit(dst[i * 2 + 1] * scr_stream->mic_gain);
}
}
}
return available_frames < frames_to_read ? available_frames : frames_to_read;
}
static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
size_t bytes) {
struct scr_stream_in *scr_stream = (struct scr_stream_in *) stream;
struct audio_stream_in *primary = scr_stream->primary;
struct scr_audio_device *device = scr_stream->dev;
if (primary)
return primary->read(primary, buffer, bytes);
int64_t stats_start_time = get_time_us();
if (device->verbose_logging) {
ALOGV("in_read %d bytes", bytes);
}
pthread_mutex_lock(&device->lock);
int frame_size = stream_in_frame_size(stream);
int frames_to_read = bytes / frame_size;
int sample_rate = scr_stream->sample_rate;
int64_t duration = frames_to_read * 1000000ll / sample_rate;
if (!device->in_active) {
activate_input(device, scr_stream, frames_to_read, duration);
}
int64_t ret_time = scr_stream->in_start_us + (scr_stream->frames_read + frames_to_read) * 1000000ll / sample_rate;
wait_for_ret_time(device, ret_time);
int available_frames = get_available_frames(device, scr_stream);
int target_frames_count = get_target_frames_count(scr_stream, frames_to_read); // ideally we should have that much frames in the buffer
// allow one full buffer excess and some small number (10) to compensate the time drift caused by in_read processing time
if (available_frames > target_frames_count + frames_to_read + 10) {
if (device->verbose_logging || scr_stream->stats_excess < MAX_LOGS) {
ALOGW("available excess frames %d > %d", available_frames, target_frames_count);
}
scr_stream->stats_excess++;
skip_excess_frames(device, scr_stream, frames_to_read);
} else if (available_frames < frames_to_read && device->out_active) {
wait_for_frames(device, scr_stream, frames_to_read, duration);
}
available_frames = get_available_frames(device, scr_stream);
if (should_extend_silence(device, scr_stream, sample_rate, frames_to_read, available_frames)) {
available_frames = 0;
}
if (scr_stream->recording_silence != (available_frames < frames_to_read)) {
if (scr_stream->recording_silence) {
ALOGD("Silence finished");
log_start_stats(scr_stream, ret_time);
skip_excess_frames(device, scr_stream, frames_to_read);
available_frames = get_available_frames(device, scr_stream);
} else {
ALOGD("Starting recording silence");
}
scr_stream->recording_silence = !scr_stream->recording_silence;
}
if (available_frames < frames_to_read) {
memset(buffer, 0, bytes);
}
int frames_read = copy_frames(device, scr_stream, (int16_t *) buffer, frames_to_read, available_frames);
scr_stream->frames_read += frames_to_read;
pthread_mutex_unlock(&device->lock);
if (available_frames > 0) {
scr_stream->stats_data++;
} else {
scr_stream->stats_silence++;
}
if (device->verbose_logging) {
int64_t now = get_time_us();
int duration_ms = (now - stats_start_time) / 1000ll;
int latency_ms = (now - ret_time) / 1000ll;
ALOGV("read [%d/%d] frames in %d ms. Latency %d ms. Remaining %d frames", frames_read, frames_to_read - frames_read, duration_ms, latency_ms, get_available_frames(device, scr_stream));
}
return bytes;
}
static ssize_t in_read_mix(struct audio_stream_in *stream, void *buffer, size_t bytes) {
struct scr_stream_in *scr_stream = (struct scr_stream_in *) stream;
struct audio_stream_in *primary = scr_stream->primary;
struct scr_audio_device *device = scr_stream->dev;
int64_t stats_start_time = get_time_us();
if (device->verbose_logging) {
ALOGV("in_read_mix %d bytes", bytes);
}
ssize_t result = primary->read(primary, buffer, bytes);
if (result > 0) {
pthread_mutex_lock(&device->lock);
int frame_size = stream_in_frame_size(stream);
int frames_to_read = result / frame_size;
int sample_rate = scr_stream->sample_rate;
int min_frames_available = frames_to_read * 2;
int max_frames_available = frames_to_read * 4;
int available_frames = get_available_frames(device, scr_stream);
if (!device->in_active) {
ALOGD("Input active");
device->in_active = true;
scr_stream->frames_read = 0L;
scr_stream->stats_in_buffer_size = frames_to_read;
if (available_frames > max_frames_available) {
skip_to_frame_count(device, scr_stream, min_frames_available);
available_frames = max_frames_available;
}
}
if (available_frames > max_frames_available) {
scr_stream->stats_excess++;
if (device->verbose_logging || scr_stream->stats_excess < MAX_LOGS) {
ALOGW("available excess frames %d > %d", available_frames, max_frames_available);
}
skip_to_frame_count(device, scr_stream, max_frames_available);
available_frames = max_frames_available;
}
if (scr_stream->recording_silence) {
// don't start if we don't have some extra padding frames
if (available_frames < min_frames_available) {
available_frames = 0;
} else {
ALOGD("Silence finished");
scr_stream->recording_silence = false;
scr_stream->stats_starts++;
if (scr_stream->stats_consecutive_silence < 5) {
scr_stream->stats_delays++;
if (device->verbose_logging || scr_stream->stats_delays < MAX_LOGS) {
ALOGE("Suspeciously short silense %d. Probably caused by output buffers delay", scr_stream->stats_consecutive_silence);
}
}
}
} else if (available_frames < frames_to_read) {
ALOGD("Starting recording silence");
scr_stream->recording_silence = true;
scr_stream->stats_consecutive_silence = 0;
}
int frames_read = mix_frames(device, scr_stream, (int16_t *) buffer, frames_to_read, available_frames);
scr_stream->frames_read += frames_to_read;
pthread_mutex_unlock(&device->lock);
if (scr_stream->recording_silence) {
scr_stream->stats_silence++;
scr_stream->stats_consecutive_silence++;
} else {
scr_stream->stats_data++;
}
if (device->verbose_logging) {
int64_t now = get_time_us();
int duration_ms = (now - stats_start_time) / 1000ll;
ALOGV("read [%d/%d] frames in %d ms. Remaining %d frames", frames_read, frames_to_read - frames_read, duration_ms, get_available_frames(device, scr_stream));
}
} else {
if (device->verbose_logging) {
int64_t now = get_time_us();
int duration_ms = (now - stats_start_time) / 1000ll;
ALOGV("in_read_mix primary driver returned %d in %d ms. Remaining %d frames", (int) result, duration_ms, get_available_frames(device, scr_stream));
}
}
return result;
}
static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
{
struct scr_stream_in *scr_stream = (struct scr_stream_in *)stream;
struct audio_stream_in *primary = scr_stream->primary;
if (primary)
return primary->get_input_frames_lost(primary);
return 0;
}
static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
{
struct scr_stream_in *scr_stream = (struct scr_stream_in *)stream;
struct audio_stream *primary = &scr_stream->primary->common;
if (primary)