audio_callback.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #define _GNU_SOURCE
  2. #include <dlfcn.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <stdint.h>
  7. #include <fcntl.h>
  8. #include <sys/ioctl.h>
  9. #include <sys/time.h>
  10. #include <tinyalsa/pcm.h>
  11. struct frames_st {
  12. void *buf;
  13. size_t length;
  14. };
  15. typedef int (* framecb)(struct frames_st *);
  16. static uint32_t (*real_local_sdk_audio_set_pcm_frame_callback)(int ch, void *callback);
  17. static void *audio_pcm_cb = NULL;
  18. static int AudioCaptureEnable = 0;
  19. static void *audio_pcm_cb1 = NULL;
  20. static int AudioCaptureEnable1 = 0;
  21. char *AudioCapture(int fd, char *tokenPtr) {
  22. char *p = strtok_r(NULL, " \t\r\n", &tokenPtr);
  23. if(!p) return AudioCaptureEnable ? "on" : "off";
  24. if(!strcmp(p, "on")) {
  25. AudioCaptureEnable = 1;
  26. fprintf(stderr, "[command] audio capute on\n", p);
  27. return "ok";
  28. }
  29. if(!strcmp(p, "on1")) {
  30. AudioCaptureEnable1 = 1;
  31. fprintf(stderr, "[command] audio capute on\n", p);
  32. return "ok";
  33. }
  34. if(!strcmp(p, "off")) {
  35. AudioCaptureEnable = 0;
  36. fprintf(stderr, "[command] audio capute off\n", p);
  37. return "ok";
  38. }
  39. if(!strcmp(p, "off1")) {
  40. AudioCaptureEnable1 = 0;
  41. fprintf(stderr, "[command] audio capute off\n", p);
  42. return "ok";
  43. }
  44. return "error";
  45. }
  46. //channel 0
  47. static uint32_t audio_pcm_capture(struct frames_st *frames) {
  48. static struct pcm *pcm = NULL;
  49. static int firstEntry = 0;
  50. uint32_t *buf = frames->buf;
  51. if(!firstEntry) {
  52. firstEntry++;
  53. unsigned int card = 0;
  54. unsigned int device = 1;
  55. int flags = PCM_OUT | PCM_MMAP;
  56. const struct pcm_config config = {
  57. .channels = 1,
  58. .rate = 16000,
  59. .format = PCM_FORMAT_S16_LE,
  60. .period_size = 128,
  61. .period_count = 8,
  62. .start_threshold = 320,
  63. .silence_threshold = 0,
  64. .silence_size = 0,
  65. .stop_threshold = 320 * 4
  66. };
  67. pcm = pcm_open(card, device, flags, &config);
  68. if(pcm == NULL) {
  69. fprintf(stderr, "failed to allocate memory for PCM CH0\n");
  70. } else if(!pcm_is_ready(pcm)) {
  71. pcm_close(pcm);
  72. fprintf(stderr, "failed to open PCM CH0\n");
  73. }
  74. }
  75. if(pcm && AudioCaptureEnable) {
  76. int avail = pcm_mmap_avail(pcm);
  77. int delay = pcm_get_delay(pcm);
  78. int ready = pcm_is_ready(pcm);
  79. int err = pcm_writei(pcm, buf, pcm_bytes_to_frames(pcm, frames->length));
  80. if(err < 0) fprintf(stderr, "pcm_writei err=%d\n", err);
  81. }
  82. return ((framecb)audio_pcm_cb)(frames);
  83. }
  84. //channel1
  85. static uint32_t audio_pcm_capture1(struct frames_st *frames) {
  86. static struct pcm *pcm = NULL;
  87. static int firstEntry = 0;
  88. uint32_t *buf = frames->buf;
  89. if(!firstEntry) {
  90. firstEntry++;
  91. unsigned int card = 0;
  92. unsigned int device = 0;
  93. int flags = PCM_OUT | PCM_MMAP;
  94. const struct pcm_config config = {
  95. .channels = 1,
  96. .rate = 8000,
  97. .format = PCM_FORMAT_S16_LE,
  98. .period_size = 128,
  99. .period_count = 8,
  100. .start_threshold = 320,
  101. .silence_threshold = 0,
  102. .silence_size = 0,
  103. .stop_threshold = 320 * 4
  104. };
  105. pcm = pcm_open(card, device, flags, &config);
  106. if(pcm == NULL) {
  107. fprintf(stderr, "failed to allocate memory for PCM CH1\n");
  108. } else if(!pcm_is_ready(pcm)) {
  109. pcm_close(pcm);
  110. fprintf(stderr, "failed to open PCM CH1\n");
  111. }
  112. }
  113. if(pcm && AudioCaptureEnable1) {
  114. int avail = pcm_mmap_avail(pcm);
  115. int delay = pcm_get_delay(pcm);
  116. int ready = pcm_is_ready(pcm);
  117. int err = pcm_writei(pcm, buf, pcm_bytes_to_frames(pcm, frames->length));
  118. if(err < 0) fprintf(stderr, "pcm_writei err=%d\n", err);
  119. }
  120. return ((framecb)audio_pcm_cb1)(frames);
  121. }
  122. uint32_t local_sdk_audio_set_pcm_frame_callback(int ch, void *callback) {
  123. fprintf(stderr, "local_sdk_audio_set_pcm_frame_callback streamChId=%d, callback=0x%x\n", ch, callback);
  124. static int ch_count = 0;
  125. if( (ch == 0) && ch_count == 0) {
  126. audio_pcm_cb = callback;
  127. fprintf(stderr,"enc func injection CH0 save audio_pcm_cb=0x%x\n", audio_pcm_cb);
  128. callback = audio_pcm_capture;
  129. }
  130. if( (ch == 1) && ch_count == 1) {
  131. audio_pcm_cb1 = callback;
  132. fprintf(stderr,"enc func injection CH1 save audio_pcm_cb=0x%x\n", audio_pcm_cb1);
  133. callback = audio_pcm_capture1;
  134. }
  135. ch_count=ch_count+1;
  136. return real_local_sdk_audio_set_pcm_frame_callback(ch, callback);
  137. }
  138. static void __attribute ((constructor)) audio_callback_init(void) {
  139. real_local_sdk_audio_set_pcm_frame_callback = dlsym(dlopen("/system/lib/liblocalsdk.so", RTLD_LAZY), "local_sdk_audio_set_pcm_frame_callback");
  140. }