Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- #include <string.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <pthread.h>
- #include <pulse/simple.h>
- #include <pulse/error.h>
- #include <pulse/gccmacro.h>
- #define BUFSIZE 1024
- static char * argv0;
- /* A simple routine calling UNIX write() in a loop */
- static ssize_t loop_write(int fd, const void*data, size_t size) {
- ssize_t ret = 0;
- while (size > 0) {
- ssize_t r;
- if ((r = write(fd, data, size)) < 0)
- return r;
- if (r == 0)
- break;
- ret += r;
- data = (const uint8_t*) data + r;
- size -= (size_t) r;
- }
- return ret;
- }
- static void rec(char const * file) {
- static const pa_sample_spec ss = {
- .format = PA_SAMPLE_S16LE,
- .rate = 44100,
- .channels = 2
- };
- pa_simple *s = NULL;
- int error;
- int fd = -1;
- /* Create the recording stream */
- if (!(s = pa_simple_new(NULL, argv0, PA_STREAM_RECORD, NULL, "record", &ss, NULL, NULL, &error))) {
- fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error));
- goto finish;
- }
- fd = open(file, O_WRONLY|O_TRUNC|O_CREAT, 0644);
- if (fd < 0) {
- perror("create");
- goto finish;
- }
- for (;;) {
- uint8_t buf[BUFSIZE];
- /* Record some data ... */
- if (pa_simple_read(s, buf, sizeof(buf), &error) < 0) {
- fprintf(stderr, __FILE__": pa_simple_read() failed: %s\n", pa_strerror(error));
- goto finish;
- }
- /* And write it to STDOUT */
- if (loop_write(fd, buf, sizeof(buf)) != sizeof(buf)) {
- fprintf(stderr, __FILE__": write() failed: %s\n", strerror(errno));
- goto finish;
- }
- }
- finish:
- if (s) { pa_simple_free(s); }
- if (fd >= 0) { close(fd); }
- }
- static void * stream(void * arg) {
- rec((char const*)arg);
- return 0;
- }
- static int start_thread(char const * file, pthread_t * p) {
- int rc;
- if (rc = pthread_create(p, NULL, stream, (void*)file)) {
- errno = rc;
- perror("thread_create()");
- return 1;
- }
- }
- int main(int argc, char*argv[]) {
- /* The sample type to use */
- argv0 = argv[0];
- pthread_t p1;
- pthread_t p2;
- if (start_thread("1", &p1) || start_thread("2", &p2)) {
- return 1;
- }
- void * dummy;
- pthread_join(p1, &dummy);
- pthread_join(p2, &dummy);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment