audio_callback.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. char *AudioCapture(int fd, char *tokenPtr) {
  20. char *p = strtok_r(NULL, " \t\r\n", &tokenPtr);
  21. if(!p) return AudioCaptureEnable ? "on" : "off";
  22. if(!strcmp(p, "on")) {
  23. AudioCaptureEnable = 1;
  24. fprintf(stderr, "[command] audio capute on\n", p);
  25. return "ok";
  26. }
  27. if(!strcmp(p, "off")) {
  28. AudioCaptureEnable = 0;
  29. fprintf(stderr, "[command] audio capute off\n", p);
  30. return "ok";
  31. }
  32. return "error";
  33. }
  34. static uint32_t audio_pcm_capture(struct frames_st *frames) {
  35. static struct pcm *pcm = NULL;
  36. static int firstEntry = 0;
  37. uint32_t *buf = frames->buf;
  38. if(!firstEntry) {
  39. firstEntry++;
  40. unsigned int card = 0;
  41. unsigned int device = 1;
  42. int flags = PCM_OUT | PCM_MMAP;
  43. const struct pcm_config config = {
  44. .channels = 1,
  45. .rate = 16000,
  46. .format = PCM_FORMAT_S16_LE,
  47. .period_size = 128,
  48. .period_count = 8,
  49. .start_threshold = 320,
  50. .silence_threshold = 0,
  51. .silence_size = 0,
  52. .stop_threshold = 320 * 4
  53. };
  54. pcm = pcm_open(card, device, flags, &config);
  55. if(pcm == NULL) {
  56. fprintf(stderr, "failed to allocate memory for PCM\n");
  57. } else if(!pcm_is_ready(pcm)) {
  58. pcm_close(pcm);
  59. fprintf(stderr, "failed to open PCM\n");
  60. }
  61. }
  62. if(pcm && AudioCaptureEnable) {
  63. int avail = pcm_mmap_avail(pcm);
  64. int delay = pcm_get_delay(pcm);
  65. int ready = pcm_is_ready(pcm);
  66. int err = pcm_writei(pcm, buf, pcm_bytes_to_frames(pcm, frames->length));
  67. if(err < 0) fprintf(stderr, "pcm_writei err=%d\n", err);
  68. }
  69. return ((framecb)audio_pcm_cb)(frames);
  70. }
  71. uint32_t local_sdk_audio_set_pcm_frame_callback(int ch, void *callback) {
  72. fprintf(stderr, "local_sdk_audio_set_pcm_frame_callback streamChId=%d, callback=0x%x\n", ch, callback);
  73. static int ch_count = 0;
  74. if( (ch == 0) && ch_count == 0) {
  75. audio_pcm_cb = callback;
  76. fprintf(stderr,"enc func injection save audio_pcm_cb=0x%x\n", audio_pcm_cb);
  77. callback = audio_pcm_capture;
  78. }
  79. ch_count=ch_count+1
  80. return real_local_sdk_audio_set_pcm_frame_callback(ch, callback);
  81. }
  82. static void __attribute ((constructor)) audio_callback_init(void) {
  83. real_local_sdk_audio_set_pcm_frame_callback = dlsym(dlopen("/system/lib/liblocalsdk.so", RTLD_LAZY), "local_sdk_audio_set_pcm_frame_callback");
  84. }