diff options
Diffstat (limited to 'src/sound.cpp')
-rw-r--r-- | src/sound.cpp | 213 |
1 files changed, 166 insertions, 47 deletions
diff --git a/src/sound.cpp b/src/sound.cpp index d0f2a80..b500a57 100644 --- a/src/sound.cpp +++ b/src/sound.cpp @@ -42,34 +42,79 @@ struct pa_handle { int operation_success; double latency_seconds; + + uint32_t combined_sink_module_index; }; -static void pa_sound_device_free(pa_handle *s) { - assert(s); +static void destroy_combined_sink(pa_handle *p) { + // TODO: error handling + pa_operation *module_pa = pa_context_unload_module(p->context, p->combined_sink_module_index, NULL, NULL); + for(;;) { + if(pa_operation_get_state(module_pa) == PA_OPERATION_DONE) { + pa_operation_unref(module_pa); + break; + } + pa_mainloop_iterate(p->mainloop, 1, NULL); + } +} + +static void pa_sound_device_free(pa_handle *p) { + assert(p); - if (s->stream) - pa_stream_unref(s->stream); + if(p->combined_sink_module_index != PA_INVALID_INDEX) { + destroy_combined_sink(p); + p->combined_sink_module_index = PA_INVALID_INDEX; + } - if (s->context) { - pa_context_disconnect(s->context); - pa_context_unref(s->context); + if (p->stream) { + pa_stream_unref(p->stream); + p->stream = NULL; } - if (s->mainloop) - pa_mainloop_free(s->mainloop); + if (p->context) { + pa_context_disconnect(p->context); + pa_context_unref(p->context); + p->context = NULL; + } - if (s->output_data) { - free(s->output_data); - s->output_data = NULL; + if (p->mainloop) { + pa_mainloop_free(p->mainloop); + p->mainloop = NULL; } - pa_xfree(s); + if (p->output_data) { + free(p->output_data); + p->output_data = NULL; + } + + pa_xfree(p); +} + +static void module_index_callback(pa_context*, uint32_t idx, void *userdata) { + pa_handle *p = (pa_handle*)userdata; + p->combined_sink_module_index = idx; +} + +static bool create_null_sink(pa_handle *p, const char *null_sink_name) { + // TODO: Error handling + char module_argument[256]; + snprintf(module_argument, sizeof(module_argument), "sink_name=\"%s\" slaves= adjust_time=0", null_sink_name); + pa_operation *module_pa = pa_context_load_module(p->context, "module-null-sink", module_argument, module_index_callback, p); + for(;;) { + if(pa_operation_get_state(module_pa) == PA_OPERATION_DONE) { + pa_operation_unref(module_pa); + break; + } + pa_mainloop_iterate(p->mainloop, 1, NULL); + } + return p->combined_sink_module_index != PA_INVALID_INDEX; } static pa_handle* pa_sound_device_new(const char *server, const char *name, const char *dev, const char *stream_name, + const char *combined_sink_name, const pa_sample_spec *ss, const pa_buffer_attr *attr, int *rerror) { @@ -77,10 +122,7 @@ static pa_handle* pa_sound_device_new(const char *server, int error = PA_ERR_INTERNAL, r; p = pa_xnew0(pa_handle, 1); - p->read_data = NULL; - p->read_length = 0; - p->read_index = 0; - p->latency_seconds = 0.0; + p->combined_sink_module_index = PA_INVALID_INDEX; const int buffer_size = attr->fragsize; void *buffer = malloc(buffer_size); @@ -119,12 +161,23 @@ static pa_handle* pa_sound_device_new(const char *server, pa_mainloop_iterate(p->mainloop, 1, NULL); } + char device_to_record[256]; + if(combined_sink_name) { + if(!create_null_sink(p, combined_sink_name)) { + fprintf(stderr, "gsr error: pa_sound_device_new: failed to create module-combine-sink\n"); + goto fail; + } + snprintf(device_to_record, sizeof(device_to_record), "%s.monitor", combined_sink_name); + } else { + snprintf(device_to_record, sizeof(device_to_record), "%s", dev); + } + if (!(p->stream = pa_stream_new(p->context, stream_name, ss, NULL))) { error = pa_context_errno(p->context); goto fail; } - r = pa_stream_connect_record(p->stream, dev, attr, + r = pa_stream_connect_record(p->stream, device_to_record, attr, (pa_stream_flags_t)(PA_STREAM_INTERPOLATE_TIMING|PA_STREAM_ADJUST_LATENCY|PA_STREAM_AUTO_TIMING_UPDATE)); if (r < 0) { @@ -259,7 +312,7 @@ static int audio_format_to_get_bytes_per_sample(AudioFormat audio_format) { return 2; } -int sound_device_get_by_name(SoundDevice *device, const char *device_name, const char *description, unsigned int num_channels, unsigned int period_frame_size, AudioFormat audio_format) { +static int sound_device_setup_record(SoundDevice *device, const char *device_name, const char *description, unsigned int num_channels, unsigned int period_frame_size, AudioFormat audio_format, const char *combined_sink_name) { pa_sample_spec ss; ss.format = audio_format_to_pulse_audio_format(audio_format); ss.rate = 48000; @@ -273,7 +326,7 @@ int sound_device_get_by_name(SoundDevice *device, const char *device_name, const buffer_attr.maxlength = buffer_attr.fragsize; int error = 0; - pa_handle *handle = pa_sound_device_new(nullptr, description, device_name, description, &ss, &buffer_attr, &error); + pa_handle *handle = pa_sound_device_new(nullptr, description, device_name, description, combined_sink_name, &ss, &buffer_attr, &error); if(!handle) { fprintf(stderr, "pa_sound_device_new() failed: %s. Audio input device %s might not be valid\n", pa_strerror(error), description); return -1; @@ -284,6 +337,14 @@ int sound_device_get_by_name(SoundDevice *device, const char *device_name, const return 0; } +int sound_device_get_by_name(SoundDevice *device, const char *device_name, const char *description, unsigned int num_channels, unsigned int period_frame_size, AudioFormat audio_format) { + return sound_device_setup_record(device, device_name, description, num_channels, period_frame_size, audio_format, NULL); +} + +int sound_device_create_combined_sink_connect(SoundDevice *device, const char *combined_sink_name, unsigned int num_channels, unsigned int period_frame_size, AudioFormat audio_format) { + return sound_device_setup_record(device, "gpu-screen-recorder", "gpu-screen-recorder", num_channels, period_frame_size, audio_format, combined_sink_name); +} + void sound_device_close(SoundDevice *device) { if(device->handle) pa_sound_device_free((pa_handle*)device->handle); @@ -322,8 +383,7 @@ static void pa_state_cb(pa_context *c, void *userdata) { } } -static void pa_sourcelist_cb(pa_context *ctx, const pa_source_info *source_info, int eol, void *userdata) { - (void)ctx; +static void pa_sourcelist_cb(pa_context*, const pa_source_info *source_info, int eol, void *userdata) { if(eol > 0) return; @@ -339,17 +399,27 @@ static void pa_server_info_cb(pa_context*, const pa_server_info *server_info, vo audio_devices->default_input = server_info->default_source_name; } -static void get_pulseaudio_default_inputs(AudioDevices &audio_devices) { - pa_mainloop *main_loop = pa_mainloop_new(); +static void server_info_callback(pa_context*, const pa_server_info *server_info, void *userdata) { + bool *is_server_pipewire = (bool*)userdata; + if(server_info->server_name && strstr(server_info->server_name, "PipeWire")) + *is_server_pipewire = true; +} - pa_context *ctx = pa_context_new(pa_mainloop_get_api(main_loop), "gpu-screen-recorder-gtk"); - pa_context_connect(ctx, NULL, PA_CONTEXT_NOFLAGS, NULL); +static void get_pulseaudio_default_inputs(AudioDevices &audio_devices) { int state = 0; int pa_ready = 0; - pa_context_set_state_callback(ctx, pa_state_cb, &pa_ready); - pa_operation *pa_op = NULL; + pa_mainloop *main_loop = pa_mainloop_new(); + if(!main_loop) + return; + + pa_context *ctx = pa_context_new(pa_mainloop_get_api(main_loop), "gpu-screen-recorder"); + if(pa_context_connect(ctx, NULL, PA_CONTEXT_NOFLAGS, NULL) < 0) + goto done; + + pa_context_set_state_callback(ctx, pa_state_cb, &pa_ready); + for(;;) { // Not ready if(pa_ready == 0) { @@ -366,36 +436,38 @@ static void get_pulseaudio_default_inputs(AudioDevices &audio_devices) { } // Couldn't get connection to the server - if(pa_ready == 2 || (state == 1 && pa_op && pa_operation_get_state(pa_op) == PA_OPERATION_DONE)) { - if(pa_op) - pa_operation_unref(pa_op); - pa_context_disconnect(ctx); - pa_context_unref(ctx); - pa_mainloop_free(main_loop); - return; - } + if(pa_ready == 2 || (state == 1 && pa_op && pa_operation_get_state(pa_op) == PA_OPERATION_DONE)) + break; pa_mainloop_iterate(main_loop, 1, NULL); } + done: + if(pa_op) + pa_operation_unref(pa_op); + pa_context_disconnect(ctx); + pa_context_unref(ctx); pa_mainloop_free(main_loop); } AudioDevices get_pulseaudio_inputs() { AudioDevices audio_devices; + int state = 0; + int pa_ready = 0; + pa_operation *pa_op = NULL; // TODO: Do this in the same connection below instead of two separate connections get_pulseaudio_default_inputs(audio_devices); pa_mainloop *main_loop = pa_mainloop_new(); + if(!main_loop) + return audio_devices; pa_context *ctx = pa_context_new(pa_mainloop_get_api(main_loop), "gpu-screen-recorder"); - pa_context_connect(ctx, NULL, PA_CONTEXT_NOFLAGS, NULL); - int state = 0; - int pa_ready = 0; - pa_context_set_state_callback(ctx, pa_state_cb, &pa_ready); + if(pa_context_connect(ctx, NULL, PA_CONTEXT_NOFLAGS, NULL) < 0) + goto done; - pa_operation *pa_op = NULL; + pa_context_set_state_callback(ctx, pa_state_cb, &pa_ready); for(;;) { // Not ready @@ -413,17 +485,64 @@ AudioDevices get_pulseaudio_inputs() { } // Couldn't get connection to the server - if(pa_ready == 2 || (state == 1 && pa_op && pa_operation_get_state(pa_op) == PA_OPERATION_DONE)) { - if(pa_op) - pa_operation_unref(pa_op); - pa_context_disconnect(ctx); - pa_context_unref(ctx); + if(pa_ready == 2 || (state == 1 && pa_op && pa_operation_get_state(pa_op) == PA_OPERATION_DONE)) break; - } pa_mainloop_iterate(main_loop, 1, NULL); } + done: + if(pa_op) + pa_operation_unref(pa_op); + pa_context_disconnect(ctx); + pa_context_unref(ctx); pa_mainloop_free(main_loop); return audio_devices; } + +bool pulseaudio_server_is_pipewire() { + int state = 0; + int pa_ready = 0; + pa_operation *pa_op = NULL; + bool is_server_pipewire = false; + + pa_mainloop *main_loop = pa_mainloop_new(); + if(!main_loop) + return is_server_pipewire; + + pa_context *ctx = pa_context_new(pa_mainloop_get_api(main_loop), "gpu-screen-recorder"); + if(pa_context_connect(ctx, NULL, PA_CONTEXT_NOFLAGS, NULL) < 0) + goto done; + + pa_context_set_state_callback(ctx, pa_state_cb, &pa_ready); + + for(;;) { + // Not ready + if(pa_ready == 0) { + pa_mainloop_iterate(main_loop, 1, NULL); + continue; + } + + switch(state) { + case 0: { + pa_op = pa_context_get_server_info(ctx, server_info_callback, &is_server_pipewire); + ++state; + break; + } + } + + // Couldn't get connection to the server + if(pa_ready == 2 || (state == 1 && pa_op && pa_operation_get_state(pa_op) == PA_OPERATION_DONE)) + break; + + pa_mainloop_iterate(main_loop, 1, NULL); + } + + done: + if(pa_op) + pa_operation_unref(pa_op); + pa_context_disconnect(ctx); + pa_context_unref(ctx); + pa_mainloop_free(main_loop); + return is_server_pipewire; +} |