hiddenAudioPlayer.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #include <pthread.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <alsa/asoundlib.h>
  5. #include <unistd.h>
  6. #include <string.h>
  7. #include <fcntl.h>
  8. #include <sys/stat.h>
  9. #include <sys/types.h>
  10. #include <dirent.h>
  11. #include <signal.h>
  12. #define PCM_DEVICE "default"
  13. #define CMD_PIPE "/tmp/pcm_cmd"
  14. volatile sig_atomic_t stop_flag = 0;
  15. volatile sig_atomic_t pause_flag = 0;
  16. void *command_listener(void *arg) {
  17. char command[16];
  18. int fd;
  19. mkfifo(CMD_PIPE, 0666);
  20. while (!stop_flag) {
  21. fd = open(CMD_PIPE, O_RDONLY);
  22. if (fd < 0) {
  23. usleep(100000);
  24. continue;
  25. }
  26. ssize_t n = read(fd, command, sizeof(command) - 1);
  27. if (n > 0) {
  28. command[n] = '\0';
  29. if (strncmp(command, "haps", 4) == 0) {
  30. stop_flag = 1;
  31. } else if (strncmp(command, "pause", 5) == 0) {
  32. pause_flag = 1;
  33. } else if (strncmp(command, "resume", 6) == 0) {
  34. pause_flag = 0;
  35. }
  36. }
  37. close(fd);
  38. usleep(100000);
  39. }
  40. return NULL;
  41. }
  42. int play_pcm_file(const char *filename, snd_pcm_t *pcm_handle, int frame_bytes, int buffer_size, char *buffer) {
  43. FILE *fp = fopen(filename, "rb");
  44. if (!fp) return -1;
  45. while (!stop_flag) {
  46. if (pause_flag) {
  47. usleep(100000);
  48. continue;
  49. }
  50. size_t read_bytes = fread(buffer, 1, buffer_size, fp);
  51. if (read_bytes == 0) break;
  52. int written = snd_pcm_writei(pcm_handle, buffer, read_bytes / frame_bytes);
  53. if (written == -EPIPE) {
  54. snd_pcm_prepare(pcm_handle);
  55. } else if (written < 0) {
  56. break;
  57. }
  58. }
  59. fclose(fp);
  60. return 0;
  61. }
  62. int cmp_str(const void *a, const void *b) {
  63. return strcmp(*(const char **)a, *(const char **)b);
  64. }
  65. int main(int argc, char *argv[]) {
  66. if (argc != 2) return 1;
  67. pid_t pid = fork();
  68. if (pid < 0) return 1;
  69. if (pid > 0) return 0;
  70. setsid();
  71. pthread_t cmd_thread;
  72. pthread_create(&cmd_thread, NULL, command_listener, NULL);
  73. snd_pcm_t *pcm_handle;
  74. snd_pcm_hw_params_t *params;
  75. snd_pcm_uframes_t frames;
  76. int rc;
  77. rc = snd_pcm_open(&pcm_handle, PCM_DEVICE, SND_PCM_STREAM_PLAYBACK, 0);
  78. if (rc < 0) return 1;
  79. snd_pcm_hw_params_malloc(&params);
  80. snd_pcm_hw_params_any(pcm_handle, params);
  81. snd_pcm_hw_params_set_access(pcm_handle, params, SND_PCM_ACCESS_RW_INTERLEAVED);
  82. snd_pcm_hw_params_set_format(pcm_handle, params, SND_PCM_FORMAT_S16_LE);
  83. snd_pcm_hw_params_set_channels(pcm_handle, params, 2);
  84. unsigned int rate = 44100;
  85. snd_pcm_hw_params_set_rate_near(pcm_handle, params, &rate, NULL);
  86. snd_pcm_hw_params(pcm_handle, params);
  87. snd_pcm_hw_params_get_period_size(params, &frames, NULL);
  88. int frame_bytes = 2 * 2;
  89. int buffer_size = frames * frame_bytes;
  90. char *buffer = (char *) malloc(buffer_size);
  91. struct stat path_stat;
  92. stat(argv[1], &path_stat);
  93. if (S_ISDIR(path_stat.st_mode)) {
  94. DIR *dir = opendir(argv[1]);
  95. if (!dir) return 1;
  96. struct dirent *entry;
  97. char *files[1024];
  98. int count = 0;
  99. while ((entry = readdir(dir)) != NULL && count < 1024) {
  100. if (strstr(entry->d_name, ".pcm")) {
  101. char *full_path = malloc(4096);
  102. snprintf(full_path, 4096, "%s/%s", argv[1], entry->d_name);
  103. files[count++] = full_path;
  104. }
  105. }
  106. closedir(dir);
  107. qsort(files, count, sizeof(char *), cmp_str);
  108. for (int i = 0; i < count && !stop_flag; i++) {
  109. play_pcm_file(files[i], pcm_handle, frame_bytes, buffer_size, buffer);
  110. free(files[i]);
  111. }
  112. } else {
  113. play_pcm_file(argv[1], pcm_handle, frame_bytes, buffer_size, buffer);
  114. }
  115. snd_pcm_drain(pcm_handle);
  116. snd_pcm_close(pcm_handle);
  117. free(buffer);
  118. unlink(CMD_PIPE);
  119. return 0;
  120. }