简体   繁体   中英

Check if microphone is already being used with C on Linux

I was playing around with the Pulseaudio API and due to my little knowledge on how the sound system works, I'm not really understanding why it's possible having multiple applications using the mic at the same time.

Or to better phrase it: why the fprintf is not called I have 2 applications that are actively recording stuffs and I start the following program?

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <pulse/simple.h>
#include <pulse/error.h>
#define BUFSIZE 1024


int main(int argc, char*argv[]) {
    /* The sample type to use */
    static const pa_sample_spec ss = {
        .format = PA_SAMPLE_S16LE,
        .rate = 44100,
        .channels = 2
    };
    pa_simple *s = NULL;
    int ret = 1;
    int error;

    /* Create the recording stream */
    if (!(s = pa_simple_new(NULL, argv[0], PA_STREAM_RECORD, NULL, "record", &ss, NULL, NULL, &error)))
        fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error));

    return 0;
}

I'd guess you're not getting an error message because the program is successfully creating a pa_simple connection to the pulse audio server.

You may wish to add pa_simple_free(s); to the end of your main() before you return.

Also, here is a link to an example pa_simple record program: parec-simple_8c-example

See Pulse Audio API at freedesktop.org


EDIT: Your question "why the fprintf is not called" is because the Pulse Audio Simple API doesn't guarantee exclusive access to the Microphone. Therefor, when two other applications are already "using" the microphone, and you create a simple stream connection to the server, there is no error generated. Is your question really "How to determine if the microphone is being used by any program - and which one"?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM