audio_callback.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. static int snd_rate = 16000;
  52. const char *productv2="/driver/sensor_jxf23.ko";
  53. //Change sample rate to 8000 if we are a V2 Camera
  54. if( access( productv2, F_OK ) == 0 ) {
  55. snd_rate = 8000;
  56. }
  57. if(!firstEntry) {
  58. firstEntry++;
  59. unsigned int card = 0;
  60. unsigned int device = 1;
  61. int flags = PCM_OUT | PCM_MMAP;
  62. const struct pcm_config config = {
  63. .channels = 1,
  64. .rate = snd_rate,
  65. .format = PCM_FORMAT_S16_LE,
  66. .period_size = 128,
  67. .period_count = 8,
  68. .start_threshold = 320,
  69. .silence_threshold = 0,
  70. .silence_size = 0,
  71. .stop_threshold = 320 * 4
  72. };
  73. pcm = pcm_open(card, device, flags, &config);
  74. if(pcm == NULL) {
  75. fprintf(stderr, "failed to allocate memory for PCM CH0\n");
  76. } else if(!pcm_is_ready(pcm)) {
  77. pcm_close(pcm);
  78. fprintf(stderr, "failed to open PCM CH0\n");
  79. }
  80. }
  81. if(pcm && AudioCaptureEnable) {
  82. int avail = pcm_mmap_avail(pcm);
  83. int delay = pcm_get_delay(pcm);
  84. int ready = pcm_is_ready(pcm);
  85. int err = pcm_writei(pcm, buf, pcm_bytes_to_frames(pcm, frames->length));
  86. if(err < 0) fprintf(stderr, "pcm_writei err=%d\n", err);
  87. }
  88. return ((framecb)audio_pcm_cb)(frames);
  89. }
  90. //channel1
  91. static uint32_t audio_pcm_capture1(struct frames_st *frames) {
  92. static struct pcm *pcm = NULL;
  93. static int firstEntry = 0;
  94. uint32_t *buf = frames->buf;
  95. if(!firstEntry) {
  96. firstEntry++;
  97. unsigned int card = 0;
  98. unsigned int device = 0;
  99. int flags = PCM_OUT | PCM_MMAP;
  100. const struct pcm_config config = {
  101. .channels = 1,
  102. .rate = 8000,
  103. .format = PCM_FORMAT_S16_LE,
  104. .period_size = 128,
  105. .period_count = 8,
  106. .start_threshold = 320,
  107. .silence_threshold = 0,
  108. .silence_size = 0,
  109. .stop_threshold = 320 * 4
  110. };
  111. pcm = pcm_open(card, device, flags, &config);
  112. if(pcm == NULL) {
  113. fprintf(stderr, "failed to allocate memory for PCM CH1\n");
  114. } else if(!pcm_is_ready(pcm)) {
  115. pcm_close(pcm);
  116. fprintf(stderr, "failed to open PCM CH1\n");
  117. }
  118. }
  119. if(pcm && AudioCaptureEnable1) {
  120. int avail = pcm_mmap_avail(pcm);
  121. int delay = pcm_get_delay(pcm);
  122. int ready = pcm_is_ready(pcm);
  123. int err = pcm_writei(pcm, buf, pcm_bytes_to_frames(pcm, frames->length));
  124. if(err < 0) fprintf(stderr, "pcm_writei err=%d\n", err);
  125. }
  126. return ((framecb)audio_pcm_cb1)(frames);
  127. }
  128. uint32_t local_sdk_audio_set_pcm_frame_callback(int ch, void *callback) {
  129. fprintf(stderr, "local_sdk_audio_set_pcm_frame_callback streamChId=%d, callback=0x%x\n", ch, callback);
  130. static int ch_count = 0;
  131. if( (ch == 0) && ch_count == 0) {
  132. audio_pcm_cb = callback;
  133. fprintf(stderr,"enc func injection CH0 save audio_pcm_cb=0x%x\n", audio_pcm_cb);
  134. callback = audio_pcm_capture;
  135. }
  136. if( (ch == 1) && ch_count == 1) {
  137. audio_pcm_cb1 = callback;
  138. fprintf(stderr,"enc func injection CH1 save audio_pcm_cb=0x%x\n", audio_pcm_cb1);
  139. callback = audio_pcm_capture1;
  140. }
  141. //if V2 here, we have to latch on to the same callback as CH0, since the V2's only have one audio callback
  142. const char *productv2="/driver/sensor_jxf23.ko";
  143. if( access( productv2, F_OK ) == 0 ) {
  144. if( (ch == 0) && ch_count == 1) {
  145. audio_pcm_cb1 = callback;
  146. fprintf(stderr,"enc func injection CH0 second callback for V2 save audio_pcm_cb=0x%x\n", audio_pcm_cb1);
  147. callback = audio_pcm_capture1;
  148. }
  149. }
  150. ch_count=ch_count+1;
  151. return real_local_sdk_audio_set_pcm_frame_callback(ch, callback);
  152. }
  153. static void __attribute ((constructor)) audio_callback_init(void) {
  154. real_local_sdk_audio_set_pcm_frame_callback = dlsym(dlopen("/system/lib/liblocalsdk.so", RTLD_LAZY), "local_sdk_audio_set_pcm_frame_callback");
  155. }