forked from projectceladon/audio-vhal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio_hw.c
executable file
·1664 lines (1515 loc) · 54.4 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) 2011 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 "audio_hw_virtual"
#define ATRACE_TAG ATRACE_TAG_AUDIO
// #define LOG_NDEBUG 0
#include <errno.h>
#include <malloc.h>
#include <pthread.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/epoll.h>
#include <log/log.h>
#include <hardware/audio.h>
#include <hardware/hardware.h>
#include <system/audio.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/stat.h>
#include <cutils/properties.h>
#include <cutils/sockets.h>
#include <cutils/trace.h>
#include <sys/system_properties.h>
#include <pthread.h>
#define STUB_DEFAULT_SAMPLE_RATE 48000
#define STUB_DEFAULT_AUDIO_FORMAT AUDIO_FORMAT_PCM_16_BIT
#define STUB_INPUT_BUFFER_MILLISECONDS 10
#define STUB_INPUT_DEFAULT_CHANNEL_MASK AUDIO_CHANNEL_IN_STEREO
#define STUB_OUTPUT_BUFFER_MILLISECONDS 10
#define STUB_OUTPUT_DEFAULT_CHANNEL_MASK AUDIO_CHANNEL_OUT_STEREO
enum
{
CMD_OPEN = 0,
CMD_CLOSE = 1,
CMD_DATA = 2,
CMD_STREAM_START = 3,
CMD_STREAM_STOP = 4
};
enum
{
AUDIO_IN = 0,
AUDIO_OUT = 1
};
struct audio_socket_configuration_info
{
uint32_t sample_rate;
uint32_t channel; // 0; The number of channel 1: The mask of channel
uint32_t format;
uint32_t frame_count;
};
struct audio_socket_info
{
uint32_t cmd;
union
{
struct audio_socket_configuration_info asci;
uint32_t data_size;
uint32_t offset;
};
};
struct stub_audio_device
{
struct audio_hw_device device;
bool mic_mute;
};
struct stub_stream_out
{
struct audio_stream_out stream;
int64_t last_write_time_us;
uint32_t sample_rate;
audio_channel_mask_t channel_mask;
audio_format_t format;
size_t frame_count;
};
struct stub_stream_in
{
struct audio_stream_in stream;
int64_t last_read_time_us;
uint32_t sample_rate;
audio_channel_mask_t channel_mask;
audio_format_t format;
size_t frame_count;
struct stub_audio_device *dev;
};
struct audio_server_socket
{
int container_id;
int audio_mask; // 0; The number of channel 1: The mask of channel
//Audio out socket
struct stub_stream_out *sso;
int out_fd;
bool out_stream_standby;
pthread_t oss_thread; // out socket server thread
int oss_exit; // out socket server thread exit
int oss_fd; // out socket server fd
//INET socket
int out_tcp_port;
int oss_epoll_fd;
struct epoll_event oss_epoll_event[1];
bool oss_is_sent_open_cmd;
pthread_mutex_t mutexlock_out;
int64_t oss_write_count;
//Audio in socket
struct stub_stream_in *ssi;
int in_fd;
pthread_t iss_thread; // in socket server thread
int iss_exit; // in socket server thread exit
int iss_fd; // iut socket server fd
//INET socket
int in_tcp_port;
int iss_epoll_fd;
struct epoll_event iss_epoll_event[1];
bool iss_read_flag;
int input_buffer_milliseconds; // INPUT_BUFFER_MILLISECONDS
pthread_mutex_t mutexlock_in;
};
static struct audio_server_socket ass;
static void sighandler(int signum)
{
if (signum == SIGPIPE)
{
ALOGW("SIGPIPE will issue when writing data to the closing client socket. "
"Virtual audio will crash without stack and cannot recovery. "
"Catch SIGPIPE");
}
else
{
ALOGW("sig %d is caught.", signum);
}
}
static int close_socket_fd(int *psd)
{
if (*psd > 0)
{
ALOGV("Close %d", *psd);
shutdown(*psd, SHUT_RDWR);
close(*psd);
*psd = -1;
return 0;
}
else
{
ALOGV("sd is %d. Do not need close anymore.", *psd);
return 0;
}
return -1;
}
static int send_open_cmd(struct audio_server_socket *pass, int audio_type)
{
if (!pass)
{
ALOGE("pass pointer is NULL.");
return -1;
}
int ret;
int client_fd = -1;
struct audio_socket_info asi;
memset(&asi, 0, sizeof(struct audio_socket_info));
asi.cmd = CMD_OPEN;
switch (audio_type)
{
case AUDIO_IN:
ALOGV("%s pass->in_fd = %d ", __func__, pass->in_fd);
client_fd = pass->in_fd;
if (pass->ssi)
{
asi.asci.sample_rate = pass->ssi->sample_rate;
if (ass.audio_mask == 1)
{
asi.asci.channel = pass->ssi->channel_mask;
}
else
{
asi.asci.channel = audio_channel_count_from_in_mask(pass->ssi->channel_mask);
}
asi.asci.format = pass->ssi->format;
asi.asci.frame_count = pass->ssi->frame_count;
ALOGV("%s AUDIO_IN asi.asci.sample_rate: %d asi.asci.channel: %d "
"asi.asci.format: %d asi.asci.frame_count: %d\n",
__func__, asi.asci.sample_rate, asi.asci.channel,
asi.asci.format, asi.asci.frame_count);
}
break;
case AUDIO_OUT:
client_fd = pass->out_fd;
if (pass->sso)
{
asi.asci.sample_rate = pass->sso->sample_rate;
if (ass.audio_mask == 1)
{
asi.asci.channel = pass->sso->channel_mask;
}
else
{
asi.asci.channel = audio_channel_count_from_out_mask(pass->sso->channel_mask);
}
asi.asci.format = pass->sso->format;
asi.asci.frame_count = pass->sso->frame_count;
ALOGV("%s AUDIO_OUT asi.asci.sample_rate: %d asi.asci.channel: %d "
"asi.asci.format: %d asi.asci.frame_count: %d\n",
__func__, asi.asci.sample_rate, asi.asci.channel,
asi.asci.format, asi.asci.frame_count);
}
break;
default:
ALOGW("Unkown type: %d", audio_type);
return -1;
break;
}
if (client_fd < 0)
{
ALOGW("client_fd is %d. Do not send open command to client.", client_fd);
return -1;
}
do
{
ret = write(client_fd, &asi, sizeof(struct audio_socket_info));
} while (ret < 0 && errno == EINTR);
if (ret != sizeof(struct audio_socket_info))
{
ALOGE("%s: could not notify the client(%d) to open: ret=%d: %s.",
__FUNCTION__, client_fd, ret, strerror(errno));
return -1;
}
ALOGV("%s Notify the audio client(%d) to open.", __func__, client_fd);
return 0;
}
static int send_close_cmd(int client_fd)
{
ALOGV("%s client_fd = %d", __func__, client_fd);
int ret;
struct audio_socket_info asi;
asi.cmd = CMD_CLOSE;
asi.data_size = 0;
if (client_fd > 0)
{
do
{
ret = write(client_fd, &asi, sizeof(struct audio_socket_info));
} while (ret < 0 && errno == EINTR);
if (ret != sizeof(struct audio_socket_info))
{
ALOGE("%s: could not notify the client(%d) to "
"close: ret=%d: %s.",
__FUNCTION__, client_fd, ret, strerror(errno));
return -1;
}
else
{
ALOGW("%s Notify the client(%d) to close.", __func__, client_fd);
return 0;
}
}
else
{
ALOGW("Client is %d. Do not set close command to client.", client_fd);
return 0;
}
}
static uint32_t out_get_sample_rate(const struct audio_stream *stream)
{
const struct stub_stream_out *out = (const struct stub_stream_out *)stream;
ALOGV("out_get_sample_rate: %u", out->sample_rate);
return out->sample_rate;
}
static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
{
struct stub_stream_out *out = (struct stub_stream_out *)stream;
ALOGV("out_set_sample_rate: %d", rate);
out->sample_rate = rate;
return 0;
}
static size_t out_get_buffer_size(const struct audio_stream *stream)
{
const struct stub_stream_out *out = (const struct stub_stream_out *)stream;
size_t buffer_size = out->frame_count *
audio_stream_out_frame_size(&out->stream);
ALOGV("out_get_buffer_size: %zu", buffer_size);
return buffer_size;
}
static audio_channel_mask_t out_get_channels(const struct audio_stream *stream)
{
const struct stub_stream_out *out = (const struct stub_stream_out *)stream;
ALOGV("out_get_channels: %x", out->channel_mask);
return out->channel_mask;
}
static audio_format_t out_get_format(const struct audio_stream *stream)
{
const struct stub_stream_out *out = (const struct stub_stream_out *)stream;
ALOGV("out_get_format: %d", out->format);
return out->format;
}
static int out_set_format(struct audio_stream *stream, audio_format_t format)
{
struct stub_stream_out *out = (struct stub_stream_out *)stream;
ALOGV("out_set_format: %d", format);
out->format = format;
return 0;
}
static int out_standby(struct audio_stream *stream)
{
ALOGV("out_standby");
int ret;
if (ass.out_fd > 0)
{
struct audio_socket_info asi;
memset(&asi, 0, sizeof(struct audio_socket_info));
asi.cmd = CMD_STREAM_STOP;
do
{
ret = write(ass.out_fd, &asi, sizeof(struct audio_socket_info));
} while (ret < 0 && errno == EINTR);
if (ret != sizeof(struct audio_socket_info))
{
ALOGE("%s: could not notify the client(%d) to stop streaming: ret=%d: %s.",
__FUNCTION__, ass.out_fd, ret, strerror(errno));
return -1;
}
ass.out_stream_standby = true;
}
else
{
ALOGE("%s: Audio out client is not connected. "
"port(%d) ass.out_fd(%d).",
__FUNCTION__, ass.out_tcp_port, ass.out_fd);
return -1;
}
return ret;
// out->last_write_time_us = 0; unnecessary as a stale write time has same effect
}
static int out_dump(const struct audio_stream *stream, int fd)
{
ALOGV("out_dump");
return 0;
}
static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
{
ALOGV("out_set_parameters");
return 0;
}
static char *out_get_parameters(const struct audio_stream *stream, const char *keys)
{
ALOGV("out_get_parameters");
return strdup("");
}
static uint32_t out_get_latency(const struct audio_stream_out *stream)
{
ALOGV("out_get_latency");
return STUB_OUTPUT_BUFFER_MILLISECONDS;
}
static int out_set_volume(struct audio_stream_out *stream, float left,
float right)
{
ALOGV("out_set_volume: Left:%f Right:%f", left, right);
return 0;
}
static void out_update_source_metadata(struct audio_stream_out *stream,
const struct source_metadata *source_metadata)
{
ALOGV("update_source_metadata called. Do nothing as of now.");
}
static ssize_t out_write_to_client(struct audio_stream_out *stream, const void *buffer,
size_t bytes, int timeout)
{
ssize_t ret = -1;
ssize_t result = 0;
int nevents = 0;
int ne;
if (ass.out_fd > 0)
{
if (ass.out_stream_standby == true)
{
struct audio_socket_info asi;
int ret;
asi.cmd = CMD_STREAM_START;
do
{
ret = write(ass.out_fd, &asi, sizeof(struct audio_socket_info));
} while (ret < 0 && errno == EINTR);
if (ret != sizeof(struct audio_socket_info))
{
ALOGE("%s: could not notify the client(%d) to start streaming: ret=%d: %s.",
__FUNCTION__, ass.out_fd, ret, strerror(errno));
}
ass.out_stream_standby = false;
}
nevents = epoll_wait(ass.oss_epoll_fd, ass.oss_epoll_event, 1, timeout);
if (nevents < 0)
{
if (errno != EINTR)
ALOGE("epoll_wait() unexpected error: %s", strerror(errno));
return errno;
}
else if (nevents == 0)
{
ALOGW("out_write_to_client: Client cannot be written in given time.");
return -1;
}
else if (nevents > 0)
{
for (ne = 0; ne < nevents; ne++) //In fact, only one event.
{
if (ass.oss_epoll_event[ne].data.fd == ass.out_fd)
{
if ((ass.oss_epoll_event[ne].events & (EPOLLERR | EPOLLHUP)) != 0)
{
ALOGE("EPOLLERR or EPOLLHUP after epoll_wait() !?");
if (epoll_ctl(ass.oss_epoll_fd, EPOLL_CTL_DEL, ass.out_fd, NULL))
{
ALOGE("Failed to delete audio in file descriptor to epoll");
}
pthread_mutex_lock(&ass.mutexlock_out);
close_socket_fd(&(ass.out_fd)); // Try to clear the cache data in socket.
ass.oss_is_sent_open_cmd = 0;
if (ATRACE_ENABLED())
{
ATRACE_INT("avh_out_client_EPOLLERR_or_EPOLLHUP", ass.oss_is_sent_open_cmd);
}
pthread_mutex_unlock(&ass.mutexlock_out);
}
else if ((ass.oss_epoll_event[ne].events & EPOLLOUT) != 0)
{
struct audio_socket_info asi;
asi.cmd = CMD_DATA;
asi.data_size = bytes;
ALOGV("%s asi.data_size: %d\n", __func__, asi.data_size);
do
{
if (ATRACE_ENABLED())
{
ATRACE_INT("avh_CMD_DATA_count_before_write", ass.oss_write_count);
}
result = write(ass.out_fd, &asi, sizeof(struct audio_socket_info));
if (ATRACE_ENABLED())
{
ATRACE_INT("avh_CMD_DATA_count_after_write", ass.oss_write_count);
}
} while (result < 0 && errno == EINTR);
if (result != sizeof(struct audio_socket_info))
{
ALOGE("%s: could not notify the audio out client(%d) "
"to receive: result=%zd: %s.",
__FUNCTION__, ass.out_fd, result, strerror(errno));
continue;
}
ALOGV("%s Notify the audio out client(%d) to receive.", __func__, ass.out_fd);
ALOGV("out_write_to_client: write buffer to socket.");
if (ATRACE_ENABLED())
{
ATRACE_INT("avh_data_count_before_write", ass.oss_write_count);
}
result = write(ass.out_fd, buffer, bytes);
if (ATRACE_ENABLED())
{
ATRACE_INT("avh_data_count_after_write", ass.oss_write_count);
}
ass.oss_write_count++;
if (result < 0)
{
ALOGE("out_write_to_client: Fail to write to audio out client(%d)"
" with error(%s)",
ass.out_fd, strerror(errno));
}
else if (result == 0)
{
ALOGW("out_write_to_client: audio out client(%d) is closed.", ass.out_fd);
}
else
{
ALOGV("out_write_to_client: Write to audio out client. "
"ass.out_fd: %d bytes: %zu",
ass.out_fd, bytes);
if (bytes != (size_t)result)
{
ALOGW("out_write_to_client: (!^!) result(%zd) data is written. "
"But bytes(%zu) is expected.",
result, bytes);
}
}
ret = result;
}
else
{
ALOGW("out_write_to_client: epoll unknown event. port(%d) "
"ass.out_fd(%d). Return bytes(%zu) directly.",
ass.out_tcp_port, ass.out_fd, bytes);
}
}
else
{
ALOGV("out_write_to_client: epoll_wait unknown source fd.");
}
}
return ret;
}
}
else
{
ALOGV("out_write_to_client: (->v->) Audio out client is not connected. "
"port(%d) ass.out_fd(%d). Return bytes(%zu) directly.",
ass.out_tcp_port, ass.out_fd, bytes);
return -1;
}
return ret;
}
static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
size_t bytes)
{
if (ATRACE_ENABLED())
{
ATRACE_BEGIN("avh_out_write");
}
ALOGV("out_write: %p, bytes: %zu", buffer, bytes);
struct stub_stream_out *out = (struct stub_stream_out *)stream;
ssize_t ret = bytes;
ssize_t result = -1;
/* XXX: fake timing for audio output */
struct timespec t = {.tv_sec = 0, .tv_nsec = 0};
clock_gettime(CLOCK_MONOTONIC, &t);
const int64_t now = (t.tv_sec * 1000000000LL + t.tv_nsec) / 1000;
const int64_t elapsed_time_since_last_write = now - out->last_write_time_us;
int64_t sleep_time = bytes * 1000000LL / audio_stream_out_frame_size(stream) /
out_get_sample_rate(&stream->common) -
elapsed_time_since_last_write;
int64_t frame_time_ms = bytes * 1000LL / audio_stream_out_frame_size(stream) /
out_get_sample_rate(&stream->common); // ms
int64_t timeout = sleep_time / 1000LL; // ms
if (timeout < 1)
{
timeout = 1;
}
if (timeout > frame_time_ms)
{
timeout = frame_time_ms;
}
if (bytes > 0)
{
result = out_write_to_client(stream, buffer, bytes, timeout);
if (result < 0)
{
ALOGV("The result of out_write_to_client is %zd", result);
}
else if (result > 0)
{
ret = result;
}
}
clock_gettime(CLOCK_MONOTONIC, &t);
const int64_t new_now = (t.tv_sec * 1000000000LL + t.tv_nsec) / 1000;
sleep_time = sleep_time - (new_now - now);
int64_t frame_time_us = bytes * 1000000LL / audio_stream_out_frame_size(stream) /
out_get_sample_rate(&stream->common); // us
if (sleep_time > 0 && sleep_time <= frame_time_us)
{
usleep(sleep_time);
}
else
{
// we don't sleep when we exit standby (this is typical for a real alsa buffer).
sleep_time = 0;
}
out->last_write_time_us = new_now + sleep_time;
// last_write_time_us is an approximation of when the (simulated) alsa
// buffer is believed completely full. The usleep above waits for more space
// in the buffer, but by the end of the sleep the buffer is considered
// topped-off.
//
// On the subsequent out_write(), we measure the elapsed time spent in
// the mixer. This is subtracted from the sleep estimate based on frames,
// thereby accounting for drain in the alsa buffer during mixing.
// This is a crude approximation; we don't handle underruns precisely.
if (ATRACE_ENABLED())
{
ATRACE_END();
}
return ret;
}
static int out_get_render_position(const struct audio_stream_out *stream,
uint32_t *dsp_frames)
{
*dsp_frames = 0;
ALOGV("out_get_render_position: dsp_frames: %p", dsp_frames);
return -EINVAL;
}
static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
{
ALOGV("out_add_audio_effect: %p", effect);
return 0;
}
static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
{
ALOGV("out_remove_audio_effect: %p", effect);
return 0;
}
static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
int64_t *timestamp)
{
*timestamp = 0;
ALOGV("out_get_next_write_timestamp: %ld", (long int)(*timestamp));
return -EINVAL;
}
static void *out_socket_sever_thread(void *args)
{
struct audio_server_socket *pass = (struct audio_server_socket *)args;
int ret = 0;
int so_reuseaddr = 1;
int new_client_fd = -1;
struct sockaddr_in addr_in;
ALOGV("%s Constructing audio out socket server...", __func__);
pass->oss_fd = socket(AF_INET, SOCK_STREAM, 0);
if (pass->oss_fd < 0)
{
ALOGE("%s:%d Fail to construct audio out socket with error: %s",
__func__, __LINE__, strerror(errno));
return NULL;
}
if (setsockopt(pass->oss_fd, SOL_SOCKET, SO_REUSEADDR, &so_reuseaddr, sizeof(int)) < 0)
{
ALOGE("%s setsockopt(SO_REUSEADDR) failed. pass->oss_fd: %d\n", __func__, pass->oss_fd);
return NULL;
}
memset(&addr_in, 0, sizeof(addr_in));
addr_in.sin_family = AF_INET;
addr_in.sin_addr.s_addr = htonl(INADDR_ANY);
addr_in.sin_port = htons(pass->out_tcp_port);
ret = bind(pass->oss_fd, (struct sockaddr *)&addr_in, sizeof(struct sockaddr_in));
if (ret < 0)
{
ALOGE("%s Failed to bind port(%d). ret: %d %s", __func__, pass->out_tcp_port, ret, strerror(errno));
return NULL;
}
ret = listen(pass->oss_fd, 5);
if (ret < 0)
{
ALOGE("%s Failed to listen on port %d", __func__, pass->out_tcp_port);
return NULL;
}
while (!pass->oss_exit)
{
ALOGW("%s Wait a audio out client to connect...", __func__);
socklen_t alen;
alen = sizeof(struct sockaddr_in);
new_client_fd = accept(pass->oss_fd, (struct sockaddr *)&addr_in, &alen);
if (new_client_fd < 0)
{
ALOGE("%s The audio in socket server maybe shutdown as quit command is got. "
"Or else, error happen. port: %d %s",
__func__, pass->out_tcp_port, strerror(errno));
}
else
{
ALOGW("%s Currently only receive one out client. Close previous "
"client(%d)",
__func__, pass->out_fd);
if (pass->out_fd > 0)
{
pthread_mutex_lock(&ass.mutexlock_out);
if (ATRACE_ENABLED())
{
ATRACE_INT("avh_osst_before_send_close_cmd", pass->oss_is_sent_open_cmd);
}
pass->oss_is_sent_open_cmd = 0;
if (send_close_cmd(pass->out_fd) < 0)
{
ALOGE("Fail to notify audio out client(%d) to close.", pass->out_fd);
}
if (ATRACE_ENABLED())
{
ATRACE_INT("avh_osst_after_send_close_cmd", pass->oss_is_sent_open_cmd);
}
pthread_mutex_unlock(&ass.mutexlock_out);
if (epoll_ctl(pass->oss_epoll_fd, EPOLL_CTL_DEL, pass->out_fd, NULL))
{
ALOGE("Failed to delete audio out file descriptor to epoll");
}
close_socket_fd(&(pass->out_fd));
}
ALOGW("%s A new audio out client connected to server. "
"new_client_fd = %d",
__func__, new_client_fd);
pass->out_fd = new_client_fd;
pass->out_stream_standby = true;
if (pass->out_fd > 0)
{
pthread_mutex_lock(&ass.mutexlock_out);
pass->oss_write_count = 0;
if (ATRACE_ENABLED())
{
ATRACE_INT("avh_osst_before_send_open_cmd", pass->oss_is_sent_open_cmd);
}
if (pass->sso && pass->oss_is_sent_open_cmd == 0) // Make sure parameters are ready.
{
if (send_open_cmd(pass, AUDIO_OUT) < 0)
{
ALOGE("Fail to send OPEN command to audio out client(%d)", pass->out_fd);
}
else
{
pass->oss_is_sent_open_cmd = 1;
ALOGE("pass->oss_is_sent_open_cmd is set to %d", pass->oss_is_sent_open_cmd);
}
}
if (ATRACE_ENABLED())
{
ATRACE_INT("avh_osst_after_send_open_cmd", pass->oss_is_sent_open_cmd);
}
pthread_mutex_unlock(&ass.mutexlock_out);
struct epoll_event event;
event.events = EPOLLOUT;
event.data.fd = pass->out_fd;
if (epoll_ctl(pass->oss_epoll_fd, EPOLL_CTL_ADD, pass->out_fd, &event))
{
ALOGE("Failed to add audio out file descriptor to epoll");
}
}
}
}
ALOGW("%s Quit. port %d(%d)", __func__, pass->out_tcp_port, pass->out_fd);
pthread_mutex_lock(&ass.mutexlock_out);
pass->oss_is_sent_open_cmd = 0;
close_socket_fd(&(pass->out_fd));
close_socket_fd(&(pass->oss_fd));
pthread_mutex_unlock(&ass.mutexlock_out);
return NULL;
}
/** audio_stream_in implementation **/
static uint32_t in_get_sample_rate(const struct audio_stream *stream)
{
const struct stub_stream_in *in = (const struct stub_stream_in *)stream;
ALOGV("in_get_sample_rate: %u", in->sample_rate);
return in->sample_rate;
}
static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
{
struct stub_stream_in *in = (struct stub_stream_in *)stream;
ALOGV("in_set_sample_rate: %u", rate);
in->sample_rate = rate;
return 0;
}
static size_t in_get_buffer_size(const struct audio_stream *stream)
{
const struct stub_stream_in *in = (const struct stub_stream_in *)stream;
size_t buffer_size = in->frame_count *
audio_stream_in_frame_size(&in->stream);
ALOGV("in_get_buffer_size: %zu", buffer_size);
return buffer_size;
}
static audio_channel_mask_t in_get_channels(const struct audio_stream *stream)
{
const struct stub_stream_in *in = (const struct stub_stream_in *)stream;
ALOGV("in_get_channels: %x", in->channel_mask);
return in->channel_mask;
}
static audio_format_t in_get_format(const struct audio_stream *stream)
{
const struct stub_stream_in *in = (const struct stub_stream_in *)stream;
ALOGV("in_get_format: %d", in->format);
return in->format;
}
static int in_set_format(struct audio_stream *stream, audio_format_t format)
{
struct stub_stream_in *in = (struct stub_stream_in *)stream;
ALOGV("in_set_format: %d", format);
in->format = format;
return 0;
}
static int in_standby(struct audio_stream *stream)
{
struct stub_stream_in *in = (struct stub_stream_in *)stream;
in->last_read_time_us = 0;
return 0;
}
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 ssize_t in_read_from_client(struct audio_stream_in *stream, void *buffer,
size_t bytes, int timeout, uint32_t offset)
{
ssize_t ret = -1;
ssize_t result = 0;
int nevents = 0;
int ne;
if (ass.in_fd > 0)
{
ALOGV("%s epoll_wait %d.", __func__, ass.iss_epoll_fd);
nevents = epoll_wait(ass.iss_epoll_fd, ass.iss_epoll_event, 1, timeout);
if (nevents < 0)
{
if (errno != EINTR)
ALOGE("epoll_wait() unexpected error: %s", strerror(errno));
return errno;
}
else if (nevents == 0)
{
ALOGW("in_read_from_client: Client cannot be read in given time.");
memset(buffer, 0, bytes);
return bytes;
}
else if (nevents > 0)
{
for (ne = 0; ne < nevents; ne++) // In fact, only one event.
{
if (ass.iss_epoll_event[ne].data.fd == ass.in_fd)
{
if ((ass.iss_epoll_event[ne].events & (EPOLLERR | EPOLLHUP)) != 0)
{
ALOGE("EPOLLERR or EPOLLHUP after epoll_wait() !?");
if (epoll_ctl(ass.iss_epoll_fd, EPOLL_CTL_DEL, ass.in_fd, NULL))
{
ALOGE("Failed to delete audio in file descriptor to epoll");
}
close(ass.in_fd);
ass.in_fd = -1;
}
else if ((ass.iss_epoll_event[ne].events & EPOLLIN) != 0)
{
result = read(ass.in_fd, buffer, bytes);
if (result < 0)
{
ALOGE("in_read_from_client: Fail to read from audio in client(%d) "
"with error (%s)",
ass.in_fd, strerror(errno));
}
else if (result == 0)
{
ALOGE("in_read_from_client: Audio in client(%d) is closed.", ass.in_fd);
}
else
{
ALOGV("in_read_from_client: Read from port %d ass.in_fd %d bytes "
"%zu, result: %zd",
ass.in_tcp_port, ass.in_fd, bytes, result);
if (bytes != (size_t)result)
{
ALOGW("in_read_from_client: (!^!) result(%zd) data is read. But "
"bytes(%zu) is expected.",
result, bytes);
}
ret = result;
}
}
else
{
ALOGW("in_read_from_client: epoll unknown event. port(%d) ass.in_fd(%d)"
". Memset data to 0. Return bytes(%zu) directly.",
ass.in_tcp_port, ass.in_fd, bytes);
}
}
else
{
ALOGV("in_read_from_client: epoll_wait unknown");
}
}
return ret;
}
}
else
{
ALOGV("in_read_from_client: (->v->) Audio in client is not connected. port(%d)"
" ass.in_fd(%d). Memset data to 0. Return bytes(%zu) directly.",
ass.in_tcp_port, ass.in_fd, bytes);
memset(buffer, 0, bytes);
return bytes;
}
return ret;
}
static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
size_t bytes)
{
ALOGV("in_read: %p, bytes %zu", buffer, bytes);
if (!ass.iss_read_flag)
{
if (ass.in_fd > 0)
{
pthread_mutex_lock(&ass.mutexlock_in);
ALOGV("in_read: send_open_cmd pthread_mutex_lock");
if (send_open_cmd(&ass, AUDIO_IN) < 0)
{
ALOGE("%s: Fail to send OPEN command to audio in client(%d)", __func__, ass.in_fd);
}
pthread_mutex_unlock(&ass.mutexlock_in);
}
ass.iss_read_flag = true;
}
struct stub_stream_in *in = (struct stub_stream_in *)stream;
struct stub_audio_device *adev = in->dev;
ssize_t ret = bytes;