pveselov

parec_muilti.c

Jan 1st, 2022
1,483
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.48 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <string.h>
  4. #include <errno.h>
  5. #include <fcntl.h>
  6. #include <pthread.h>
  7.  
  8. #include <pulse/simple.h>
  9. #include <pulse/error.h>
  10. #include <pulse/gccmacro.h>
  11.  
  12. #define BUFSIZE 1024
  13.  
  14. static char * argv0;
  15.  
  16. /* A simple routine calling UNIX write() in a loop */
  17. static ssize_t loop_write(int fd, const void*data, size_t size) {
  18.     ssize_t ret = 0;
  19.  
  20.     while (size > 0) {
  21.         ssize_t r;
  22.  
  23.         if ((r = write(fd, data, size)) < 0)
  24.             return r;
  25.  
  26.         if (r == 0)
  27.             break;
  28.  
  29.         ret += r;
  30.         data = (const uint8_t*) data + r;
  31.         size -= (size_t) r;
  32.     }
  33.  
  34.     return ret;
  35. }
  36.  
  37. static void rec(char const * file) {
  38.  
  39.     static const pa_sample_spec ss = {
  40.         .format = PA_SAMPLE_S16LE,
  41.         .rate = 44100,
  42.         .channels = 2
  43.     };
  44.     pa_simple *s = NULL;
  45.     int error;
  46.     int fd = -1;
  47.  
  48.     /* Create the recording stream */
  49.     if (!(s = pa_simple_new(NULL, argv0, PA_STREAM_RECORD, NULL, "record", &ss, NULL, NULL, &error))) {
  50.         fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error));
  51.         goto finish;
  52.     }
  53.  
  54.     fd = open(file, O_WRONLY|O_TRUNC|O_CREAT, 0644);
  55.     if (fd < 0) {
  56.         perror("create");
  57.         goto finish;
  58.     }
  59.  
  60.     for (;;) {
  61.         uint8_t buf[BUFSIZE];
  62.  
  63.         /* Record some data ... */
  64.         if (pa_simple_read(s, buf, sizeof(buf), &error) < 0) {
  65.             fprintf(stderr, __FILE__": pa_simple_read() failed: %s\n", pa_strerror(error));
  66.             goto finish;
  67.         }
  68.  
  69.         /* And write it to STDOUT */
  70.         if (loop_write(fd, buf, sizeof(buf)) != sizeof(buf)) {
  71.             fprintf(stderr, __FILE__": write() failed: %s\n", strerror(errno));
  72.             goto finish;
  73.         }
  74.     }
  75.  
  76. finish:
  77.  
  78.     if (s) { pa_simple_free(s); }
  79.     if (fd >= 0) { close(fd); }
  80.  
  81. }
  82.  
  83. static void * stream(void * arg) {
  84.     rec((char const*)arg);
  85.     return 0;
  86. }
  87.  
  88. static int start_thread(char const * file, pthread_t * p) {
  89.     int rc;
  90.     if (rc = pthread_create(p, NULL, stream, (void*)file)) {
  91.         errno = rc;
  92.         perror("thread_create()");
  93.         return 1;
  94.     }
  95. }
  96.  
  97. int main(int argc, char*argv[]) {
  98.     /* The sample type to use */
  99.     argv0 = argv[0];
  100.     pthread_t p1;
  101.     pthread_t p2;
  102.  
  103.     if (start_thread("1", &p1) || start_thread("2", &p2)) {
  104.         return 1;
  105.     }
  106.  
  107.     void * dummy;
  108.  
  109.     pthread_join(p1, &dummy);
  110.     pthread_join(p2, &dummy);
  111.  
  112.     return 0;
  113. }
  114.  
Advertisement
Add Comment
Please, Sign In to add comment