audio_callback.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 = 1024,
  48. .period_count = 4,
  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. if(ch == 0) {
  74. audio_pcm_cb = callback;
  75. fprintf(stderr,"enc func injection save audio_pcm_cb=0x%x\n", audio_pcm_cb);
  76. callback = audio_pcm_capture;
  77. }
  78. return real_local_sdk_audio_set_pcm_frame_callback(ch, callback);
  79. }
  80. static void __attribute ((constructor)) audio_callback_init(void) {
  81. real_local_sdk_audio_set_pcm_frame_callback = dlsym(dlopen("/system/lib/liblocalsdk.so", RTLD_LAZY), "local_sdk_audio_set_pcm_frame_callback");
  82. }