audio_callback.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 = 1;
  19. static uint32_t audio_pcm_capture(struct frames_st *frames) {
  20. static struct pcm *pcm = NULL;
  21. static int firstEntry = 0;
  22. uint32_t *buf = frames->buf;
  23. if(!firstEntry) {
  24. firstEntry++;
  25. unsigned int card = 0;
  26. unsigned int device = 1;
  27. int flags = PCM_OUT | PCM_MMAP;
  28. const struct pcm_config config = {
  29. .channels = 1,
  30. .rate = 16000,
  31. .format = PCM_FORMAT_S16_LE,
  32. .period_size = 1024,
  33. .period_count = 4,
  34. .start_threshold = 320,
  35. .silence_threshold = 0,
  36. .silence_size = 0,
  37. .stop_threshold = 320 * 4
  38. };
  39. pcm = pcm_open(card, device, flags, &config);
  40. if(pcm == NULL) {
  41. fprintf(stderr, "failed to allocate memory for PCM\n");
  42. } else if(!pcm_is_ready(pcm)) {
  43. pcm_close(pcm);
  44. fprintf(stderr, "failed to open PCM\n");
  45. }
  46. }
  47. if(pcm && AudioCaptureEnable) {
  48. int avail = pcm_mmap_avail(pcm);
  49. int delay = pcm_get_delay(pcm);
  50. int ready = pcm_is_ready(pcm);
  51. int err = pcm_writei(pcm, buf, pcm_bytes_to_frames(pcm, frames->length));
  52. if(err < 0) fprintf(stderr, "pcm_writei err=%d\n", err);
  53. }
  54. return ((framecb)audio_pcm_cb)(frames);
  55. }
  56. uint32_t local_sdk_audio_set_pcm_frame_callback(int ch, void *callback) {
  57. fprintf(stderr, "local_sdk_audio_set_pcm_frame_callback streamChId=%d, callback=0x%x\n", ch, callback);
  58. if(ch == 0) {
  59. audio_pcm_cb = callback;
  60. fprintf(stderr,"enc func injection save audio_pcm_cb=0x%x\n", audio_pcm_cb);
  61. callback = audio_pcm_capture;
  62. }
  63. return real_local_sdk_audio_set_pcm_frame_callback(ch, callback);
  64. }
  65. static void __attribute ((constructor)) audio_callback_init(void) {
  66. real_local_sdk_audio_set_pcm_frame_callback = dlsym(dlopen("/system/lib/liblocalsdk.so", RTLD_LAZY), "local_sdk_audio_set_pcm_frame_callback");
  67. }