bool PulseHandler::Init(void) { if (m_initialised) return m_valid; m_initialised = true; // Initialse our connection to the server m_loop = pa_mainloop_new(); if (!m_loop) { LOG(VB_GENERAL, LOG_ERR, LOC + "Failed to get PulseAudio mainloop"); return m_valid; } pa_mainloop_api *api = pa_mainloop_get_api(m_loop); if (!api) { LOG(VB_GENERAL, LOG_ERR, LOC + "Failed to get PulseAudio api"); return m_valid; } if (pa_signal_init(api) != 0) { LOG(VB_GENERAL, LOG_ERR, LOC + "Failed to initialise signaling"); return m_valid; } const char *client = "mythtv"; m_ctx = pa_context_new(api, client); if (!m_ctx) { LOG(VB_GENERAL, LOG_ERR, LOC + "Failed to create context"); return m_valid; } // remember which thread created this object for later sanity debugging m_thread = QThread::currentThread(); // we set the callback, connect and then run the main loop 'by hand' // until we've successfully connected (or not) pa_context_set_state_callback(m_ctx, StatusCallback, this); pa_context_connect(m_ctx, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL); int ret = 0; int tries = 0; while ((tries++ < 100) && !IS_READY(m_ctx_state)) { pa_mainloop_iterate(m_loop, 0, &ret); usleep(10000); } if (PA_CONTEXT_READY != m_ctx_state) { LOG(VB_GENERAL, LOG_ERR, LOC + "Context not ready after 1000ms"); return m_valid; } LOG(VB_AUDIO, LOG_INFO, LOC + "Initialised handler"); m_valid = true; return m_valid; }
int main(int argc, char *argv[]) { pa_mainloop *m; m = pa_mainloop_new(); assert(m); pa_cpu_limit_init(pa_mainloop_get_api(m)); time(&start); #ifdef TEST2 pa_signal_init(pa_mainloop_get_api(m)); pa_signal_new(SIGUSR1, func, NULL); raise(SIGUSR1); pa_mainloop_run(m, NULL); pa_signal_done(); #else for (;;) { time_t now; time(&now); if ((now - start) >= 30) { fprintf(stderr, "Test failed\n"); break; } } #endif pa_cpu_limit_done(); pa_mainloop_free(m); return 0; }
/** * Pulsaudio init */ static void pa_init () { int r; int i; if (!pa_sample_spec_valid (&sample_spec)) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong Spec\n")); } /* set up main record loop */ if (!(m = pa_mainloop_new ())) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("pa_mainloop_new() failed.\n")); } mainloop_api = pa_mainloop_get_api (m); /* listen to signals */ r = pa_signal_init (mainloop_api); GNUNET_assert (r == 0); pa_signal_new (SIGINT, &exit_signal_callback, NULL); pa_signal_new (SIGTERM, &exit_signal_callback, NULL); /* connect to the main pulseaudio context */ if (!(context = pa_context_new (mainloop_api, "GNUNET VoIP"))) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("pa_context_new() failed.\n")); } pa_context_set_state_callback (context, &context_state_callback, NULL); if (pa_context_connect (context, NULL, 0, NULL) < 0) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("pa_context_connect() failed: %s\n"), pa_strerror (pa_context_errno (context))); } if (pa_mainloop_run (m, &i) < 0) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("pa_mainloop_run() failed.\n")); } }
struct ausrv *ausrv_create(struct tonegend *tonegend, char *server) { pa_glib_mainloop *mainloop = NULL; struct ausrv *ausrv; pa_mainloop_api *mainloop_api; if ((ausrv = malloc(sizeof(*ausrv))) == NULL) { LOG_ERROR("%s(): Can't allocate memory", __FUNCTION__); goto failed; } if ((mainloop = pa_glib_mainloop_new(NULL)) == NULL) { LOG_ERROR("%s(): pa_glib_mainloop_new() failed", __FUNCTION__); goto failed; } mainloop_api = pa_glib_mainloop_get_api(mainloop); if (pa_signal_init(mainloop_api) < 0) { LOG_ERROR("%s(): pa_signal_init() failed", __FUNCTION__); goto failed; } memset(ausrv, 0, sizeof(*ausrv)); ausrv->tonegend = tonegend; ausrv->server = strdup(server ? server : DEFAULT_SERVER); ausrv->mainloop = mainloop; connect_server(ausrv); return ausrv; failed: if (mainloop != NULL) pa_glib_mainloop_free(mainloop); if (ausrv != NULL) free(ausrv); return NULL; }
int PulseAudioDriver::setup(bool capture, bool playback, const QString& ) { PENTER; sample_spec.rate = frame_rate; sample_spec.channels = 2; sample_spec.format = PA_SAMPLE_FLOAT32NE; assert(pa_sample_spec_valid(&sample_spec)); if (channel_map_set && channel_map.channels != sample_spec.channels) { fprintf(stderr, "Channel map doesn't match file.\n"); return -1; } /* Set up a new main loop */ if (!(mainloop = pa_mainloop_new())) { fprintf(stderr, "pa_mainloop_new() failed.\n"); return -1; } mainloop_api = pa_mainloop_get_api(mainloop); int r = pa_signal_init(mainloop_api); assert(r == 0); /* Create a new connection context */ if (!(context = pa_context_new(mainloop_api, "Traverso"))) { fprintf(stderr, "pa_context_new() failed.\n"); return -1; } pa_context_set_state_callback(context, context_state_callback, this); /* Connect the context */ pa_context_connect(context, "", (pa_context_flags_t)0, NULL); int ret; /* Run the main loop */ // if (pa_mainloop_run(mainloop, &ret) < 0) { // fprintf(stderr, "pa_mainloop_run() failed.\n"); // return -1; // } AudioChannel* audiochannel; int port_flags; char buf[32]; // TODO use the found maxchannel count for the playback stream, instead of assuming 2 !! for (int chn = 0; chn < 2; chn++) { snprintf (buf, sizeof(buf) - 1, "playback_%d", chn+1); audiochannel = device->register_playback_channel(buf, "32 bit float audio", port_flags, frames_per_cycle, chn); audiochannel->set_latency( frames_per_cycle + capture_frame_latency ); playbackChannels.append(audiochannel); } // TODO use the found maxchannel count for the capture stream, instead of assuming 0 !! for (int chn = 0; chn < 2; chn++) { snprintf (buf, sizeof(buf) - 1, "capture_%d", chn+1); audiochannel = device->register_capture_channel(buf, "32 bit float audio", port_flags, frames_per_cycle, chn); audiochannel->set_latency( frames_per_cycle + capture_frame_latency ); captureChannels.append(audiochannel); } return 1; }
int main(int argc, char *argv[]) { pa_mainloop* m = NULL; int ret = 1, c; char *bn, *server = NULL; pa_time_event *time_event = NULL; const char *filename = NULL; static const struct option long_options[] = { {"record", 0, NULL, 'r'}, {"playback", 0, NULL, 'p'}, {"device", 1, NULL, 'd'}, {"server", 1, NULL, 's'}, {"client-name", 1, NULL, 'n'}, {"stream-name", 1, NULL, ARG_STREAM_NAME}, {"version", 0, NULL, ARG_VERSION}, {"help", 0, NULL, 'h'}, {"verbose", 0, NULL, 'v'}, {"volume", 1, NULL, ARG_VOLUME}, {"rate", 1, NULL, ARG_SAMPLERATE}, {"format", 1, NULL, ARG_SAMPLEFORMAT}, {"channels", 1, NULL, ARG_CHANNELS}, {"channel-map", 1, NULL, ARG_CHANNELMAP}, {"fix-format", 0, NULL, ARG_FIX_FORMAT}, {"fix-rate", 0, NULL, ARG_FIX_RATE}, {"fix-channels", 0, NULL, ARG_FIX_CHANNELS}, {"no-remap", 0, NULL, ARG_NO_REMAP}, {"no-remix", 0, NULL, ARG_NO_REMIX}, {"latency", 1, NULL, ARG_LATENCY}, {"process-time", 1, NULL, ARG_PROCESS_TIME}, {"property", 1, NULL, ARG_PROPERTY}, {"raw", 0, NULL, ARG_RAW}, {"file-format", 2, NULL, ARG_FILE_FORMAT}, {"list-file-formats", 0, NULL, ARG_LIST_FILE_FORMATS}, {"latency-msec", 1, NULL, ARG_LATENCY_MSEC}, {"process-time-msec", 1, NULL, ARG_PROCESS_TIME_MSEC}, {NULL, 0, NULL, 0} }; setlocale(LC_ALL, ""); bindtextdomain(GETTEXT_PACKAGE, PULSE_LOCALEDIR); bn = pa_path_get_filename(argv[0]); if (strstr(bn, "play")) { mode = PLAYBACK; raw = FALSE; } else if (strstr(bn, "record")) { mode = RECORD; raw = FALSE; } else if (strstr(bn, "cat")) { mode = PLAYBACK; raw = TRUE; } if (strstr(bn, "rec") || strstr(bn, "mon")) { mode = RECORD; raw = TRUE; } proplist = pa_proplist_new(); while ((c = getopt_long(argc, argv, "rpd:s:n:hv", long_options, NULL)) != -1) { switch (c) { case 'h' : help(bn); ret = 0; goto quit; case ARG_VERSION: printf(_("pacat %s\n" "Compiled with libpulse %s\n" "Linked with libpulse %s\n"), PACKAGE_VERSION, pa_get_headers_version(), pa_get_library_version()); ret = 0; goto quit; case 'r': mode = RECORD; break; case 'p': mode = PLAYBACK; break; case 'd': pa_xfree(device); device = pa_xstrdup(optarg); break; case 's': pa_xfree(server); server = pa_xstrdup(optarg); break; case 'n': { char *t; if (!(t = pa_locale_to_utf8(optarg)) || pa_proplist_sets(proplist, PA_PROP_APPLICATION_NAME, t) < 0) { pa_log(_("Invalid client name '%s'"), t ? t : optarg); pa_xfree(t); goto quit; } pa_xfree(t); break; } case ARG_STREAM_NAME: { char *t; if (!(t = pa_locale_to_utf8(optarg)) || pa_proplist_sets(proplist, PA_PROP_MEDIA_NAME, t) < 0) { pa_log(_("Invalid stream name '%s'"), t ? t : optarg); pa_xfree(t); goto quit; } pa_xfree(t); break; } case 'v': verbose = 1; break; case ARG_VOLUME: { int v = atoi(optarg); volume = v < 0 ? 0U : (pa_volume_t) v; volume_is_set = TRUE; break; } case ARG_CHANNELS: sample_spec.channels = (uint8_t) atoi(optarg); sample_spec_set = TRUE; break; case ARG_SAMPLEFORMAT: sample_spec.format = pa_parse_sample_format(optarg); sample_spec_set = TRUE; break; case ARG_SAMPLERATE: sample_spec.rate = (uint32_t) atoi(optarg); sample_spec_set = TRUE; break; case ARG_CHANNELMAP: if (!pa_channel_map_parse(&channel_map, optarg)) { pa_log(_("Invalid channel map '%s'"), optarg); goto quit; } channel_map_set = TRUE; break; case ARG_FIX_CHANNELS: flags |= PA_STREAM_FIX_CHANNELS; break; case ARG_FIX_RATE: flags |= PA_STREAM_FIX_RATE; break; case ARG_FIX_FORMAT: flags |= PA_STREAM_FIX_FORMAT; break; case ARG_NO_REMIX: flags |= PA_STREAM_NO_REMIX_CHANNELS; break; case ARG_NO_REMAP: flags |= PA_STREAM_NO_REMAP_CHANNELS; break; case ARG_LATENCY: if (((latency = (size_t) atoi(optarg))) <= 0) { pa_log(_("Invalid latency specification '%s'"), optarg); goto quit; } break; case ARG_PROCESS_TIME: if (((process_time = (size_t) atoi(optarg))) <= 0) { pa_log(_("Invalid process time specification '%s'"), optarg); goto quit; } break; case ARG_LATENCY_MSEC: if (((latency_msec = (int32_t) atoi(optarg))) <= 0) { pa_log(_("Invalid latency specification '%s'"), optarg); goto quit; } break; case ARG_PROCESS_TIME_MSEC: if (((process_time_msec = (int32_t) atoi(optarg))) <= 0) { pa_log(_("Invalid process time specification '%s'"), optarg); goto quit; } break; case ARG_PROPERTY: { char *t; if (!(t = pa_locale_to_utf8(optarg)) || pa_proplist_setp(proplist, t) < 0) { pa_xfree(t); pa_log(_("Invalid property '%s'"), optarg); goto quit; } pa_xfree(t); break; } case ARG_RAW: raw = TRUE; break; case ARG_FILE_FORMAT: raw = FALSE; if (optarg) { if ((file_format = pa_sndfile_format_from_string(optarg)) < 0) { pa_log(_("Unknown file format %s."), optarg); goto quit; } } raw = FALSE; break; case ARG_LIST_FILE_FORMATS: pa_sndfile_dump_formats(); ret = 0; goto quit; default: goto quit; } } if (!pa_sample_spec_valid(&sample_spec)) { pa_log(_("Invalid sample specification")); goto quit; } if (optind+1 == argc) { int fd; filename = argv[optind]; if ((fd = open(argv[optind], mode == PLAYBACK ? O_RDONLY : O_WRONLY|O_TRUNC|O_CREAT, 0666)) < 0) { pa_log(_("open(): %s"), strerror(errno)); goto quit; } if (dup2(fd, mode == PLAYBACK ? STDIN_FILENO : STDOUT_FILENO) < 0) { pa_log(_("dup2(): %s"), strerror(errno)); goto quit; } pa_close(fd); } else if (optind+1 <= argc) { pa_log(_("Too many arguments.")); goto quit; } if (!raw) { SF_INFO sfi; pa_zero(sfi); if (mode == RECORD) { /* This might patch up the sample spec */ if (pa_sndfile_write_sample_spec(&sfi, &sample_spec) < 0) { pa_log(_("Failed to generate sample specification for file.")); goto quit; } /* Transparently upgrade classic .wav to wavex for multichannel audio */ if (file_format <= 0) { if ((sample_spec.channels == 2 && (!channel_map_set || (channel_map.map[0] == PA_CHANNEL_POSITION_LEFT && channel_map.map[1] == PA_CHANNEL_POSITION_RIGHT))) || (sample_spec.channels == 1 && (!channel_map_set || (channel_map.map[0] == PA_CHANNEL_POSITION_MONO)))) file_format = SF_FORMAT_WAV; else file_format = SF_FORMAT_WAVEX; } sfi.format |= file_format; } if (!(sndfile = sf_open_fd(mode == RECORD ? STDOUT_FILENO : STDIN_FILENO, mode == RECORD ? SFM_WRITE : SFM_READ, &sfi, 0))) { pa_log(_("Failed to open audio file.")); goto quit; } if (mode == PLAYBACK) { if (sample_spec_set) pa_log(_("Warning: specified sample specification will be overwritten with specification from file.")); if (pa_sndfile_read_sample_spec(sndfile, &sample_spec) < 0) { pa_log(_("Failed to determine sample specification from file.")); goto quit; } sample_spec_set = TRUE; if (!channel_map_set) { /* Allow the user to overwrite the channel map on the command line */ if (pa_sndfile_read_channel_map(sndfile, &channel_map) < 0) { if (sample_spec.channels > 2) pa_log(_("Warning: Failed to determine channel map from file.")); } else channel_map_set = TRUE; } } } if (!channel_map_set) pa_channel_map_init_extend(&channel_map, sample_spec.channels, PA_CHANNEL_MAP_DEFAULT); if (!pa_channel_map_compatible(&channel_map, &sample_spec)) { pa_log(_("Channel map doesn't match sample specification")); goto quit; } if (!raw) { pa_proplist *sfp; if (mode == PLAYBACK) readf_function = pa_sndfile_readf_function(&sample_spec); else { if (pa_sndfile_write_channel_map(sndfile, &channel_map) < 0) pa_log(_("Warning: failed to write channel map to file.")); writef_function = pa_sndfile_writef_function(&sample_spec); } /* Fill in libsndfile prop list data */ sfp = pa_proplist_new(); pa_sndfile_init_proplist(sndfile, sfp); pa_proplist_update(proplist, PA_UPDATE_MERGE, sfp); pa_proplist_free(sfp); } if (verbose) { char tss[PA_SAMPLE_SPEC_SNPRINT_MAX], tcm[PA_CHANNEL_MAP_SNPRINT_MAX]; pa_log(_("Opening a %s stream with sample specification '%s' and channel map '%s'."), mode == RECORD ? _("recording") : _("playback"), pa_sample_spec_snprint(tss, sizeof(tss), &sample_spec), pa_channel_map_snprint(tcm, sizeof(tcm), &channel_map)); } /* Fill in client name if none was set */ if (!pa_proplist_contains(proplist, PA_PROP_APPLICATION_NAME)) { char *t; if ((t = pa_locale_to_utf8(bn))) { pa_proplist_sets(proplist, PA_PROP_APPLICATION_NAME, t); pa_xfree(t); } } /* Fill in media name if none was set */ if (!pa_proplist_contains(proplist, PA_PROP_MEDIA_NAME)) { const char *t; if ((t = filename) || (t = pa_proplist_gets(proplist, PA_PROP_APPLICATION_NAME))) pa_proplist_sets(proplist, PA_PROP_MEDIA_NAME, t); } /* Set up a new main loop */ if (!(m = pa_mainloop_new())) { pa_log(_("pa_mainloop_new() failed.")); goto quit; } mainloop_api = pa_mainloop_get_api(m); pa_assert_se(pa_signal_init(mainloop_api) == 0); pa_signal_new(SIGINT, exit_signal_callback, NULL); pa_signal_new(SIGTERM, exit_signal_callback, NULL); #ifdef SIGUSR1 pa_signal_new(SIGUSR1, sigusr1_signal_callback, NULL); #endif pa_disable_sigpipe(); if (raw) { if (!(stdio_event = mainloop_api->io_new(mainloop_api, mode == PLAYBACK ? STDIN_FILENO : STDOUT_FILENO, mode == PLAYBACK ? PA_IO_EVENT_INPUT : PA_IO_EVENT_OUTPUT, mode == PLAYBACK ? stdin_callback : stdout_callback, NULL))) { pa_log(_("io_new() failed.")); goto quit; } } /* Create a new connection context */ if (!(context = pa_context_new_with_proplist(mainloop_api, NULL, proplist))) { pa_log(_("pa_context_new() failed.")); goto quit; } pa_context_set_state_callback(context, context_state_callback, NULL); /* Connect the context */ if (pa_context_connect(context, server, 0, NULL) < 0) { pa_log(_("pa_context_connect() failed: %s"), pa_strerror(pa_context_errno(context))); goto quit; } if (verbose) { if (!(time_event = pa_context_rttime_new(context, pa_rtclock_now() + TIME_EVENT_USEC, time_event_callback, NULL))) { pa_log(_("pa_context_rttime_new() failed.")); goto quit; } } /* Run the main loop */ if (pa_mainloop_run(m, &ret) < 0) { pa_log(_("pa_mainloop_run() failed.")); goto quit; } quit: if (stream) pa_stream_unref(stream); if (context) pa_context_unref(context); if (stdio_event) { pa_assert(mainloop_api); mainloop_api->io_free(stdio_event); } if (time_event) { pa_assert(mainloop_api); mainloop_api->time_free(time_event); } if (m) { pa_signal_done(); pa_mainloop_free(m); } pa_xfree(buffer); pa_xfree(server); pa_xfree(device); if (sndfile) sf_close(sndfile); if (proplist) pa_proplist_free(proplist); return ret; }
void QPulseAudioThread::run() { const char * error = "FOO"; int ret = 1, r, c; const char *bn = "projectM"; pa_operation * operation ; sample_spec.format = PA_SAMPLE_FLOAT32LE; sample_spec.rate = 44100; sample_spec.channels = 2; pa_context_flags_t flags = ( pa_context_flags_t ) 0; verbose = 0; if ( !pa_sample_spec_valid ( &sample_spec ) ) { fprintf ( stderr, "Invalid sample specification\n" ); goto quit; } if ( channel_map_set && channel_map.channels != sample_spec.channels ) { fprintf ( stderr, "Channel map doesn't match sample specification\n" ); goto quit; } if ( verbose ) { char t[PA_SAMPLE_SPEC_SNPRINT_MAX]; pa_sample_spec_snprint ( t, sizeof ( t ), &sample_spec ); fprintf ( stderr, "Opening a %s stream with sample specification '%s'.\n", "recording" , t ); } if ( !client_name ) client_name = pa_xstrdup ( bn ); //printf("client name:%s", client_name); if ( !stream_name ) stream_name = pa_xstrdup ( client_name ); /* Set up a new main loop */ if ( ! ( mainloop = pa_threaded_mainloop_new() ) ) { fprintf ( stderr, "pa_mainloop_new() failed.\n" ); goto quit; } mainloop_api = pa_threaded_mainloop_get_api ( mainloop ); r = pa_signal_init ( mainloop_api ); assert ( r == 0 ); pa_signal_new ( SIGINT, exit_signal_callback, NULL ); pa_signal_new ( SIGTERM, exit_signal_callback, NULL ); #ifdef SIGUSR1 pa_signal_new ( SIGUSR1, sigusr1_signal_callback, NULL ); #endif #ifdef SIGPIPE signal ( SIGPIPE, SIG_IGN ); #endif /* if ( ! ( stdio_event = mainloop_api->io_new ( mainloop_api, STDOUT_FILENO, PA_IO_EVENT_OUTPUT, stdout_callback, s_qprojectM_MainWindowPtr ) ) ) { fprintf ( stderr, "io_new() failed.\n" ); goto quit; } /* /* Create a new connection context */ if ( ! ( context = pa_context_new ( mainloop_api, client_name ) ) ) { fprintf ( stderr, "pa_context_new() failed.\n" ); goto quit; } pa_context_set_state_callback ( context, context_state_callback, &s_sourceList ); pa_context_connect ( context, server, flags, NULL ); if ( verbose ) { struct timeval tv; pa_gettimeofday ( &tv ); pa_timeval_add ( &tv, TIME_EVENT_USEC ); if ( ! ( time_event = mainloop_api->time_new ( mainloop_api, &tv, time_event_callback, NULL ) ) ) { fprintf ( stderr, "time_new() failed.\n" ); goto quit; } } /* Run the main loop */ if ( pa_threaded_mainloop_start ( mainloop ) < 0 ) { fprintf ( stderr, "pa_mainloop_run() failed.\n" ); goto quit; } quit: emit(threadCleanedUp()); return ; }
int main(int argc, char **argv) { pa_glib_mainloop *mainloop; pa_mainloop_api *mainloop_api; pa_context *context; GdkWindow *root; int status = EXIT_SUCCESS; int i; gtk_init(&argc, &argv); mainloop = pa_glib_mainloop_new(NULL); if (!mainloop) { fprintf(stderr, "pa_glib_mainloop_new() failed\n"); status = EXIT_FAILURE; goto out; } mainloop_api = pa_glib_mainloop_get_api(mainloop); if (pa_signal_init(mainloop_api) < 0) { fprintf(stderr, "pa_signal_init() failed\n"); status = EXIT_FAILURE; goto mainloop_free; } pa_signal_new(SIGINT, exit_signal_callback, NULL); pa_signal_new(SIGTERM, exit_signal_callback, NULL); context = pa_context_new(mainloop_api, NULL); if (!context) { fprintf(stderr, "pa_context_new() failed\n"); status = EXIT_FAILURE; goto mainloop_free; } pa_context_set_state_callback(context, context_state_callback, NULL); if (pa_context_connect(context, NULL, 0, NULL) < 0) { fprintf(stderr, "pa_context_connect() failed: %s\n", pa_strerror(pa_context_errno(context))); status = EXIT_FAILURE; goto context_unref; } if (!notify_init(APP_NAME)) { fprintf(stderr, "Could not initialize libnotify\n"); status = EXIT_FAILURE; goto context_unref; } notification = notify_notification_new(APP_NAME, NULL, NULL); if (!notification) { fprintf(stderr, "notify_notification_new() failed\n"); status = EXIT_FAILURE; goto notify_uninit; } notify_notification_set_timeout(notification, NOTIFY_EXPIRES_DEFAULT); notify_notification_set_hint_string(notification, "synchronous", "volume"); root = gdk_get_default_root_window(); gdk_window_set_events(root, GDK_KEY_PRESS_MASK); gdk_window_add_filter(root, filter, context); for (i = 0; i < sizeof(keysyms) / sizeof(keysyms[0]); i++) { keycodes[i] = XKeysymToKeycode(GDK_WINDOW_XDISPLAY(root), keysyms[i]); if (!keycodes[i]) { fprintf(stderr, "%s is not mapped on this keyboard\n", XKeysymToString(keysyms[i])); continue; } XGrabKey(GDK_WINDOW_XDISPLAY(root), keycodes[i], AnyModifier, GDK_WINDOW_XID(root), False, GrabModeAsync, GrabModeAsync); } gtk_main(); g_object_unref(G_OBJECT(notification)); notify_uninit: notify_uninit(); context_unref: pa_context_unref(context); mainloop_free: pa_glib_mainloop_free(mainloop); out: return status; }
int main(int argc, char *argv[]) { pa_core *c = NULL; pa_strbuf *buf = NULL; pa_daemon_conf *conf = NULL; pa_mainloop *mainloop = NULL; char *s; char *configured_address; int r = 0, retval = 1, d = 0; pa_bool_t valid_pid_file = FALSE; pa_bool_t ltdl_init = FALSE; int passed_fd = -1; const char *e; #ifdef HAVE_FORK int daemon_pipe[2] = { -1, -1 }; int daemon_pipe2[2] = { -1, -1 }; #endif #ifdef OS_IS_WIN32 pa_time_event *win32_timer; struct timeval win32_tv; #endif int autospawn_fd = -1; pa_bool_t autospawn_locked = FALSE; #ifdef HAVE_DBUS pa_dbusobj_server_lookup *server_lookup = NULL; /* /org/pulseaudio/server_lookup */ pa_dbus_connection *lookup_service_bus = NULL; /* Always the user bus. */ pa_dbus_connection *server_bus = NULL; /* The bus where we reserve org.pulseaudio.Server, either the user or the system bus. */ pa_bool_t start_server; #endif pa_log_set_ident("pulseaudio"); pa_log_set_level(PA_LOG_NOTICE); pa_log_set_flags(PA_LOG_COLORS|PA_LOG_PRINT_FILE|PA_LOG_PRINT_LEVEL, PA_LOG_RESET); #if defined(__linux__) && defined(__OPTIMIZE__) /* Disable lazy relocations to make usage of external libraries more deterministic for our RT threads. We abuse __OPTIMIZE__ as a check whether we are a debug build or not. This all is admittedly a bit snake-oilish. */ if (!getenv("LD_BIND_NOW")) { char *rp; char *canonical_rp; /* We have to execute ourselves, because the libc caches the * value of $LD_BIND_NOW on initialization. */ pa_set_env("LD_BIND_NOW", "1"); if ((canonical_rp = pa_realpath(PA_BINARY))) { if ((rp = pa_readlink("/proc/self/exe"))) { if (pa_streq(rp, canonical_rp)) pa_assert_se(execv(rp, argv) == 0); else pa_log_warn("/proc/self/exe does not point to %s, cannot self execute. Are you playing games?", canonical_rp); pa_xfree(rp); } else pa_log_warn("Couldn't read /proc/self/exe, cannot self execute. Running in a chroot()?"); pa_xfree(canonical_rp); } else pa_log_warn("Couldn't canonicalize binary path, cannot self execute."); } #endif if ((e = getenv("PULSE_PASSED_FD"))) { passed_fd = atoi(e); if (passed_fd <= 2) passed_fd = -1; } /* We might be autospawned, in which case have no idea in which * context we have been started. Let's cleanup our execution * context as good as possible */ pa_reset_personality(); pa_drop_root(); pa_close_all(passed_fd, -1); pa_reset_sigs(-1); pa_unblock_sigs(-1); pa_reset_priority(); setlocale(LC_ALL, ""); pa_init_i18n(); conf = pa_daemon_conf_new(); if (pa_daemon_conf_load(conf, NULL) < 0) goto finish; if (pa_daemon_conf_env(conf) < 0) goto finish; if (pa_cmdline_parse(conf, argc, argv, &d) < 0) { pa_log(_("Failed to parse command line.")); goto finish; } pa_log_set_level(conf->log_level); pa_log_set_target(conf->auto_log_target ? PA_LOG_STDERR : conf->log_target); if (conf->log_meta) pa_log_set_flags(PA_LOG_PRINT_META, PA_LOG_SET); if (conf->log_time) pa_log_set_flags(PA_LOG_PRINT_TIME, PA_LOG_SET); pa_log_set_show_backtrace(conf->log_backtrace); #ifdef HAVE_DBUS /* conf->system_instance and conf->local_server_type control almost the * same thing; make them agree about what is requested. */ switch (conf->local_server_type) { case PA_SERVER_TYPE_UNSET: conf->local_server_type = conf->system_instance ? PA_SERVER_TYPE_SYSTEM : PA_SERVER_TYPE_USER; break; case PA_SERVER_TYPE_USER: case PA_SERVER_TYPE_NONE: conf->system_instance = FALSE; break; case PA_SERVER_TYPE_SYSTEM: conf->system_instance = TRUE; break; default: pa_assert_not_reached(); } start_server = conf->local_server_type == PA_SERVER_TYPE_USER || (getuid() == 0 && conf->local_server_type == PA_SERVER_TYPE_SYSTEM); if (!start_server && conf->local_server_type == PA_SERVER_TYPE_SYSTEM) { pa_log_notice(_("System mode refused for non-root user. Only starting the D-Bus server lookup service.")); conf->system_instance = FALSE; } #endif LTDL_SET_PRELOADED_SYMBOLS(); pa_ltdl_init(); ltdl_init = TRUE; if (conf->dl_search_path) lt_dlsetsearchpath(conf->dl_search_path); #ifdef OS_IS_WIN32 { WSADATA data; WSAStartup(MAKEWORD(2, 0), &data); } #endif pa_random_seed(); switch (conf->cmd) { case PA_CMD_DUMP_MODULES: pa_dump_modules(conf, argc-d, argv+d); retval = 0; goto finish; case PA_CMD_DUMP_CONF: { if (d < argc) { pa_log("Too many arguments.\n"); goto finish; } s = pa_daemon_conf_dump(conf); fputs(s, stdout); pa_xfree(s); retval = 0; goto finish; } case PA_CMD_DUMP_RESAMPLE_METHODS: { int i; if (d < argc) { pa_log("Too many arguments.\n"); goto finish; } for (i = 0; i < PA_RESAMPLER_MAX; i++) if (pa_resample_method_supported(i)) printf("%s\n", pa_resample_method_to_string(i)); retval = 0; goto finish; } case PA_CMD_HELP : pa_cmdline_help(argv[0]); retval = 0; goto finish; case PA_CMD_VERSION : if (d < argc) { pa_log("Too many arguments.\n"); goto finish; } printf(PACKAGE_NAME" "PACKAGE_VERSION"\n"); retval = 0; goto finish; case PA_CMD_CHECK: { pid_t pid; if (d < argc) { pa_log("Too many arguments.\n"); goto finish; } if (pa_pid_file_check_running(&pid, "pulseaudio") < 0) pa_log_info(_("Daemon not running")); else { pa_log_info(_("Daemon running as PID %u"), pid); retval = 0; } goto finish; } case PA_CMD_KILL: if (d < argc) { pa_log("Too many arguments.\n"); goto finish; } if (pa_pid_file_kill(SIGINT, NULL, "pulseaudio") < 0) pa_log(_("Failed to kill daemon: %s"), pa_cstrerror(errno)); else retval = 0; goto finish; case PA_CMD_CLEANUP_SHM: if (d < argc) { pa_log("Too many arguments.\n"); goto finish; } if (pa_shm_cleanup() >= 0) retval = 0; goto finish; default: pa_assert(conf->cmd == PA_CMD_DAEMON || conf->cmd == PA_CMD_START); } if (d < argc) { pa_log("Too many arguments.\n"); goto finish; } #ifdef HAVE_GETUID if (getuid() == 0 && !conf->system_instance) pa_log_warn(_("This program is not intended to be run as root (unless --system is specified).")); #ifndef HAVE_DBUS /* A similar, only a notice worthy check was done earlier, if D-Bus is enabled. */ else if (getuid() != 0 && conf->system_instance) { pa_log(_("Root privileges required.")); goto finish; } #endif #endif /* HAVE_GETUID */ if (conf->cmd == PA_CMD_START && conf->system_instance) { pa_log(_("--start not supported for system instances.")); goto finish; } if (conf->cmd == PA_CMD_START && (configured_address = check_configured_address())) { /* There is an server address in our config, but where did it come from? * By default a standard X11 login will load module-x11-publish which will * inject PULSE_SERVER X11 property. If the PA daemon crashes, we will end * up hitting this code path. So we have to check to see if our configured_address * is the same as the value that would go into this property so that we can * recover (i.e. autospawn) from a crash. */ char *ufn; pa_bool_t start_anyway = FALSE; if ((ufn = pa_runtime_path(PA_NATIVE_DEFAULT_UNIX_SOCKET))) { char *id; if ((id = pa_machine_id())) { pa_strlist *server_list; char formatted_ufn[256]; pa_snprintf(formatted_ufn, sizeof(formatted_ufn), "{%s}unix:%s", id, ufn); pa_xfree(id); if ((server_list = pa_strlist_parse(configured_address))) { char *u = NULL; /* We only need to check the first server */ server_list = pa_strlist_pop(server_list, &u); pa_strlist_free(server_list); start_anyway = (u && pa_streq(formatted_ufn, u)); pa_xfree(u); } } pa_xfree(ufn); } if (!start_anyway) { pa_log_notice(_("User-configured server at %s, refusing to start/autospawn."), configured_address); pa_xfree(configured_address); retval = 0; goto finish; } pa_log_notice(_("User-configured server at %s, which appears to be local. Probing deeper."), configured_address); pa_xfree(configured_address); } if (conf->system_instance && !conf->disallow_exit) pa_log_warn(_("Running in system mode, but --disallow-exit not set!")); if (conf->system_instance && !conf->disallow_module_loading) pa_log_warn(_("Running in system mode, but --disallow-module-loading not set!")); if (conf->system_instance && !conf->disable_shm) { pa_log_notice(_("Running in system mode, forcibly disabling SHM mode!")); conf->disable_shm = TRUE; } if (conf->system_instance && conf->exit_idle_time >= 0) { pa_log_notice(_("Running in system mode, forcibly disabling exit idle time!")); conf->exit_idle_time = -1; } if (conf->cmd == PA_CMD_START) { /* If we shall start PA only when it is not running yet, we * first take the autospawn lock to make things * synchronous. */ if ((autospawn_fd = pa_autospawn_lock_init()) < 0) { pa_log("Failed to initialize autospawn lock"); goto finish; } if ((pa_autospawn_lock_acquire(TRUE) < 0)) { pa_log("Failed to acquire autospawn lock"); goto finish; } autospawn_locked = TRUE; } if (conf->daemonize) { #ifdef HAVE_FORK pid_t child; #endif if (pa_stdio_acquire() < 0) { pa_log(_("Failed to acquire stdio.")); goto finish; } #ifdef HAVE_FORK if (pipe(daemon_pipe) < 0) { pa_log(_("pipe() failed: %s"), pa_cstrerror(errno)); goto finish; } if ((child = fork()) < 0) { pa_log(_("fork() failed: %s"), pa_cstrerror(errno)); pa_close_pipe(daemon_pipe); goto finish; } if (child != 0) { ssize_t n; /* Father */ pa_assert_se(pa_close(daemon_pipe[1]) == 0); daemon_pipe[1] = -1; if ((n = pa_loop_read(daemon_pipe[0], &retval, sizeof(retval), NULL)) != sizeof(retval)) { if (n < 0) pa_log(_("read() failed: %s"), pa_cstrerror(errno)); retval = 1; } if (retval) pa_log(_("Daemon startup failed.")); else pa_log_info(_("Daemon startup successful.")); goto finish; } if (autospawn_fd >= 0) { /* The lock file is unlocked from the parent, so we need * to close it in the child */ pa_autospawn_lock_release(); pa_autospawn_lock_done(TRUE); autospawn_locked = FALSE; autospawn_fd = -1; } pa_assert_se(pa_close(daemon_pipe[0]) == 0); daemon_pipe[0] = -1; #endif if (conf->auto_log_target) pa_log_set_target(PA_LOG_SYSLOG); #ifdef HAVE_SETSID if (setsid() < 0) { pa_log(_("setsid() failed: %s"), pa_cstrerror(errno)); goto finish; } #endif #ifdef HAVE_FORK /* We now are a session and process group leader. Let's fork * again and let the father die, so that we'll become a * process that can never acquire a TTY again, in a session and * process group without leader */ if (pipe(daemon_pipe2) < 0) { pa_log(_("pipe() failed: %s"), pa_cstrerror(errno)); goto finish; } if ((child = fork()) < 0) { pa_log(_("fork() failed: %s"), pa_cstrerror(errno)); pa_close_pipe(daemon_pipe2); goto finish; } if (child != 0) { ssize_t n; /* Father */ pa_assert_se(pa_close(daemon_pipe2[1]) == 0); daemon_pipe2[1] = -1; if ((n = pa_loop_read(daemon_pipe2[0], &retval, sizeof(retval), NULL)) != sizeof(retval)) { if (n < 0) pa_log(_("read() failed: %s"), pa_cstrerror(errno)); retval = 1; } /* We now have to take care of signalling the first fork with * the return value we've received from this fork... */ pa_assert(daemon_pipe[1] >= 0); pa_loop_write(daemon_pipe[1], &retval, sizeof(retval), NULL); pa_close(daemon_pipe[1]); daemon_pipe[1] = -1; goto finish; } pa_assert_se(pa_close(daemon_pipe2[0]) == 0); daemon_pipe2[0] = -1; /* We no longer need the (first) daemon_pipe as it's handled in our child above */ pa_close_pipe(daemon_pipe); #endif #ifdef SIGTTOU signal(SIGTTOU, SIG_IGN); #endif #ifdef SIGTTIN signal(SIGTTIN, SIG_IGN); #endif #ifdef SIGTSTP signal(SIGTSTP, SIG_IGN); #endif pa_nullify_stdfds(); } pa_set_env_and_record("PULSE_INTERNAL", "1"); pa_assert_se(chdir("/") == 0); umask(0022); #ifdef HAVE_SYS_RESOURCE_H set_all_rlimits(conf); #endif pa_rtclock_hrtimer_enable(); pa_raise_priority(conf->nice_level); if (conf->system_instance) if (change_user() < 0) goto finish; pa_set_env_and_record("PULSE_SYSTEM", conf->system_instance ? "1" : "0"); pa_log_info(_("This is PulseAudio %s"), PACKAGE_VERSION); pa_log_debug(_("Compilation host: %s"), CANONICAL_HOST); pa_log_debug(_("Compilation CFLAGS: %s"), PA_CFLAGS); s = pa_uname_string(); pa_log_debug(_("Running on host: %s"), s); pa_xfree(s); pa_log_debug(_("Found %u CPUs."), pa_ncpus()); pa_log_info(_("Page size is %lu bytes"), (unsigned long) PA_PAGE_SIZE); #ifdef HAVE_VALGRIND_MEMCHECK_H pa_log_debug(_("Compiled with Valgrind support: yes")); #else pa_log_debug(_("Compiled with Valgrind support: no")); #endif pa_log_debug(_("Running in valgrind mode: %s"), pa_yes_no(pa_in_valgrind())); pa_log_debug(_("Running in VM: %s"), pa_yes_no(pa_running_in_vm())); #ifdef __OPTIMIZE__ pa_log_debug(_("Optimized build: yes")); #else pa_log_debug(_("Optimized build: no")); #endif #ifdef NDEBUG pa_log_debug(_("NDEBUG defined, all asserts disabled.")); #elif defined(FASTPATH) pa_log_debug(_("FASTPATH defined, only fast path asserts disabled.")); #else pa_log_debug(_("All asserts enabled.")); #endif if (!(s = pa_machine_id())) { pa_log(_("Failed to get machine ID")); goto finish; } pa_log_info(_("Machine ID is %s."), s); pa_xfree(s); if ((s = pa_session_id())) { pa_log_info(_("Session ID is %s."), s); pa_xfree(s); } if (!(s = pa_get_runtime_dir())) goto finish; pa_log_info(_("Using runtime directory %s."), s); pa_xfree(s); if (!(s = pa_get_state_dir())) goto finish; pa_log_info(_("Using state directory %s."), s); pa_xfree(s); pa_log_info(_("Using modules directory %s."), conf->dl_search_path); pa_log_info(_("Running in system mode: %s"), pa_yes_no(pa_in_system_mode())); if (pa_in_system_mode()) pa_log_warn(_("OK, so you are running PA in system mode. Please note that you most likely shouldn't be doing that.\n" "If you do it nonetheless then it's your own fault if things don't work as expected.\n" "Please read http://pulseaudio.org/wiki/WhatIsWrongWithSystemMode for an explanation why system mode is usually a bad idea.")); if (conf->use_pid_file) { int z; if ((z = pa_pid_file_create("pulseaudio")) != 0) { if (conf->cmd == PA_CMD_START && z > 0) { /* If we are already running and with are run in * --start mode, then let's return this as success. */ retval = 0; goto finish; } pa_log(_("pa_pid_file_create() failed.")); goto finish; } valid_pid_file = TRUE; } pa_disable_sigpipe(); if (pa_rtclock_hrtimer()) pa_log_info(_("Fresh high-resolution timers available! Bon appetit!")); else pa_log_info(_("Dude, your kernel stinks! The chef's recommendation today is Linux with high-resolution timers enabled!")); if (conf->lock_memory) { #ifdef HAVE_SYS_MMAN_H if (mlockall(MCL_FUTURE) < 0) pa_log_warn("mlockall() failed: %s", pa_cstrerror(errno)); else pa_log_info("Successfully locked process into memory."); #else pa_log_warn("Memory locking requested but not supported on platform."); #endif } pa_memtrap_install(); pa_assert_se(mainloop = pa_mainloop_new()); if (!(c = pa_core_new(pa_mainloop_get_api(mainloop), !conf->disable_shm, conf->shm_size))) { pa_log(_("pa_core_new() failed.")); goto finish; } c->default_sample_spec = conf->default_sample_spec; c->alternate_sample_rate = conf->alternate_sample_rate; c->default_channel_map = conf->default_channel_map; c->default_n_fragments = conf->default_n_fragments; c->default_fragment_size_msec = conf->default_fragment_size_msec; c->deferred_volume_safety_margin_usec = conf->deferred_volume_safety_margin_usec; c->deferred_volume_extra_delay_usec = conf->deferred_volume_extra_delay_usec; c->exit_idle_time = conf->exit_idle_time; c->scache_idle_time = conf->scache_idle_time; c->resample_method = conf->resample_method; c->realtime_priority = conf->realtime_priority; c->realtime_scheduling = !!conf->realtime_scheduling; c->disable_remixing = !!conf->disable_remixing; c->disable_lfe_remixing = !!conf->disable_lfe_remixing; c->deferred_volume = !!conf->deferred_volume; c->running_as_daemon = !!conf->daemonize; c->disallow_exit = conf->disallow_exit; c->flat_volumes = conf->flat_volumes; #ifdef HAVE_DBUS c->server_type = conf->local_server_type; #endif c->cpu_info.cpu_type = PA_CPU_UNDEFINED; if (!getenv("PULSE_NO_SIMD")) { if (pa_cpu_init_x86(&(c->cpu_info.flags.x86))) c->cpu_info.cpu_type = PA_CPU_X86; if (pa_cpu_init_arm(&(c->cpu_info.flags.arm))) c->cpu_info.cpu_type = PA_CPU_ARM; pa_cpu_init_orc(c->cpu_info); } pa_assert_se(pa_signal_init(pa_mainloop_get_api(mainloop)) == 0); pa_signal_new(SIGINT, signal_callback, c); pa_signal_new(SIGTERM, signal_callback, c); #ifdef SIGUSR1 pa_signal_new(SIGUSR1, signal_callback, c); #endif #ifdef SIGUSR2 pa_signal_new(SIGUSR2, signal_callback, c); #endif #ifdef SIGHUP pa_signal_new(SIGHUP, signal_callback, c); #endif #ifdef OS_IS_WIN32 win32_timer = pa_mainloop_get_api(mainloop)->time_new(pa_mainloop_get_api(mainloop), pa_gettimeofday(&win32_tv), message_cb, NULL); #endif if (!conf->no_cpu_limit) pa_assert_se(pa_cpu_limit_init(pa_mainloop_get_api(mainloop)) == 0); buf = pa_strbuf_new(); #ifdef HAVE_DBUS pa_assert_se(dbus_threads_init_default()); if (start_server) { #endif if (conf->load_default_script_file) { FILE *f; if ((f = pa_daemon_conf_open_default_script_file(conf))) { r = pa_cli_command_execute_file_stream(c, f, buf, &conf->fail); fclose(f); } } if (r >= 0) r = pa_cli_command_execute(c, conf->script_commands, buf, &conf->fail); pa_log_error("%s", s = pa_strbuf_tostring_free(buf)); pa_xfree(s); if (r < 0 && conf->fail) { pa_log(_("Failed to initialize daemon.")); goto finish; } if (!c->modules || pa_idxset_size(c->modules) == 0) { pa_log(_("Daemon startup without any loaded modules, refusing to work.")); goto finish; } #ifdef HAVE_DBUS } else { /* When we just provide the D-Bus server lookup service, we don't want * any modules to be loaded. We haven't loaded any so far, so one might * think there's no way to contact the server, but receiving certain * signals could still cause modules to load. */ conf->disallow_module_loading = TRUE; } #endif /* We completed the initial module loading, so let's disable it * from now on, if requested */ c->disallow_module_loading = !!conf->disallow_module_loading; #ifdef HAVE_DBUS if (!conf->system_instance) { if ((server_lookup = pa_dbusobj_server_lookup_new(c))) { if (!(lookup_service_bus = register_dbus_name(c, DBUS_BUS_SESSION, "org.PulseAudio1"))) goto finish; } } if (start_server) server_bus = register_dbus_name(c, conf->system_instance ? DBUS_BUS_SYSTEM : DBUS_BUS_SESSION, "org.pulseaudio.Server"); #endif #ifdef HAVE_FORK if (daemon_pipe2[1] >= 0) { int ok = 0; pa_loop_write(daemon_pipe2[1], &ok, sizeof(ok), NULL); pa_close(daemon_pipe2[1]); daemon_pipe2[1] = -1; } #endif pa_log_info(_("Daemon startup complete.")); retval = 0; if (pa_mainloop_run(mainloop, &retval) < 0) goto finish; pa_log_info(_("Daemon shutdown initiated.")); finish: #ifdef HAVE_DBUS if (server_bus) pa_dbus_connection_unref(server_bus); if (lookup_service_bus) pa_dbus_connection_unref(lookup_service_bus); if (server_lookup) pa_dbusobj_server_lookup_free(server_lookup); #endif if (autospawn_fd >= 0) { if (autospawn_locked) pa_autospawn_lock_release(); pa_autospawn_lock_done(FALSE); } #ifdef OS_IS_WIN32 if (mainloop && win32_timer) pa_mainloop_get_api(mainloop)->time_free(win32_timer); #endif if (c) { /* Ensure all the modules/samples are unloaded when the core is still ref'ed, * as unlink callback hooks in modules may need the core to be ref'ed */ pa_module_unload_all(c); pa_scache_free_all(c); pa_core_unref(c); pa_log_info(_("Daemon terminated.")); } if (!conf->no_cpu_limit) pa_cpu_limit_done(); pa_signal_done(); #ifdef HAVE_FORK /* If we have daemon_pipe[1] still open, this means we've failed after * the first fork, but before the second. Therefore just write to it. */ if (daemon_pipe[1] >= 0) pa_loop_write(daemon_pipe[1], &retval, sizeof(retval), NULL); else if (daemon_pipe2[1] >= 0) pa_loop_write(daemon_pipe2[1], &retval, sizeof(retval), NULL); pa_close_pipe(daemon_pipe2); pa_close_pipe(daemon_pipe); #endif if (mainloop) pa_mainloop_free(mainloop); if (conf) pa_daemon_conf_free(conf); if (valid_pid_file) pa_pid_file_remove(); /* This has no real purpose except making things valgrind-clean */ pa_unset_env_recorded(); #ifdef OS_IS_WIN32 WSACleanup(); #endif if (ltdl_init) pa_ltdl_done(); #ifdef HAVE_DBUS dbus_shutdown(); #endif return retval; }
void quisk_start_sound_pulseaudio(struct sound_dev **pCapture, struct sound_dev **pPlayback) { int num_pa_devices = 0; int i; //sorted lists of local and remote devices struct sound_dev *LocalPulseDevices[PA_LIST_SIZE] = {NULL}; struct sound_dev *RemotePulseDevices[PA_LIST_SIZE] = {NULL}; sort_devices(pCapture, LocalPulseDevices, RemotePulseDevices); sort_devices(pPlayback, LocalPulseDevices, RemotePulseDevices); if (!RemotePulseDevices[0] && !LocalPulseDevices[0]) { if (quisk_sound_state.verbose_pulse) printf("No pulseaudio devices to open!\n"); return; //nothing to open. No need to start the mainloop. } // Create a mainloop API pa_ml = pa_threaded_mainloop_new(); pa_mlapi = pa_threaded_mainloop_get_api(pa_ml); assert(pa_signal_init(pa_mlapi) == 0); if (pa_threaded_mainloop_start(pa_ml) < 0) { printf("pa_mainloop_run() failed."); exit(1); } else printf("Pulseaudio threaded mainloop started\n"); pa_threaded_mainloop_lock(pa_ml); if (RemotePulseDevices[0]) { //we've got at least 1 remote device pa_IQ_ctx = pa_context_new(pa_mlapi, "Quisk-remote"); if (pa_context_connect(pa_IQ_ctx, quisk_sound_state.IQ_server, 0, NULL) < 0) printf("Failed to connect to remote Pulseaudio server\n"); pa_context_set_state_callback(pa_IQ_ctx, state_cb, RemotePulseDevices); //send a list of remote devices to open } if (LocalPulseDevices[0]) { //we've got at least 1 local device pa_ctx = pa_context_new(pa_mlapi, "Quisk-local"); if (pa_context_connect(pa_ctx, NULL, 0, NULL) < 0) printf("Failed to connect to local Pulseaudio server\n"); pa_context_set_state_callback(pa_ctx, state_cb, LocalPulseDevices); } pa_threaded_mainloop_unlock(pa_ml); for(i=0; LocalPulseDevices[i]; i++) { num_pa_devices++; } for(i=0; RemotePulseDevices[i]; i++) { num_pa_devices++; } if (quisk_sound_state.verbose_pulse) printf("Waiting for %d streams to start\n", num_pa_devices); while (streams_ready < num_pa_devices); // wait for all the devices to open if (quisk_sound_state.verbose_pulse) printf("All streams started\n"); }
int main(int argc, char *argv[]) { pa_core *c = NULL; pa_strbuf *buf = NULL; pa_daemon_conf *conf = NULL; pa_mainloop *mainloop = NULL; char *s; int r = 0, retval = 1, d = 0; pa_bool_t valid_pid_file = FALSE; pa_bool_t ltdl_init = FALSE; int passed_fd = -1; const char *e; #ifdef HAVE_FORK int daemon_pipe[2] = { -1, -1 }; #endif #ifdef OS_IS_WIN32 pa_time_event *win32_timer; struct timeval win32_tv; #endif int autospawn_fd = -1; pa_bool_t autospawn_locked = FALSE; #ifdef HAVE_DBUS pa_dbus_connection *dbus = NULL; #endif pa_log_set_ident("pulseaudio"); pa_log_set_level(PA_LOG_NOTICE); pa_log_set_flags(PA_LOG_COLORS|PA_LOG_PRINT_FILE|PA_LOG_PRINT_LEVEL, PA_LOG_RESET); #if defined(__linux__) && defined(__OPTIMIZE__) /* Disable lazy relocations to make usage of external libraries more deterministic for our RT threads. We abuse __OPTIMIZE__ as a check whether we are a debug build or not. This all is admittedly a bit snake-oilish. */ if (!getenv("LD_BIND_NOW")) { char *rp; char *canonical_rp; /* We have to execute ourselves, because the libc caches the * value of $LD_BIND_NOW on initialization. */ pa_set_env("LD_BIND_NOW", "1"); if ((canonical_rp = pa_realpath(PA_BINARY))) { if ((rp = pa_readlink("/proc/self/exe"))) { if (pa_streq(rp, canonical_rp)) pa_assert_se(execv(rp, argv) == 0); else pa_log_warn("/proc/self/exe does not point to %s, cannot self execute. Are you playing games?", canonical_rp); pa_xfree(rp); } else pa_log_warn("Couldn't read /proc/self/exe, cannot self execute. Running in a chroot()?"); pa_xfree(canonical_rp); } else pa_log_warn("Couldn't canonicalize binary path, cannot self execute."); } #endif if ((e = getenv("PULSE_PASSED_FD"))) { passed_fd = atoi(e); if (passed_fd <= 2) passed_fd = -1; } /* We might be autospawned, in which case have no idea in which * context we have been started. Let's cleanup our execution * context as good as possible */ pa_reset_personality(); pa_drop_root(); pa_close_all(passed_fd, -1); pa_reset_sigs(-1); pa_unblock_sigs(-1); pa_reset_priority(); setlocale(LC_ALL, ""); pa_init_i18n(); conf = pa_daemon_conf_new(); if (pa_daemon_conf_load(conf, NULL) < 0) goto finish; if (pa_daemon_conf_env(conf) < 0) goto finish; if (pa_cmdline_parse(conf, argc, argv, &d) < 0) { pa_log(_("Failed to parse command line.")); goto finish; } pa_log_set_level(conf->log_level); pa_log_set_target(conf->auto_log_target ? PA_LOG_STDERR : conf->log_target); if (conf->log_meta) pa_log_set_flags(PA_LOG_PRINT_META, PA_LOG_SET); if (conf->log_time) pa_log_set_flags(PA_LOG_PRINT_TIME, PA_LOG_SET); pa_log_set_show_backtrace(conf->log_backtrace); LTDL_SET_PRELOADED_SYMBOLS(); pa_ltdl_init(); ltdl_init = TRUE; if (conf->dl_search_path) lt_dlsetsearchpath(conf->dl_search_path); #ifdef OS_IS_WIN32 { WSADATA data; WSAStartup(MAKEWORD(2, 0), &data); } #endif pa_random_seed(); switch (conf->cmd) { case PA_CMD_DUMP_MODULES: pa_dump_modules(conf, argc-d, argv+d); retval = 0; goto finish; case PA_CMD_DUMP_CONF: { if (d < argc) { pa_log("Too many arguments.\n"); goto finish; } s = pa_daemon_conf_dump(conf); fputs(s, stdout); pa_xfree(s); retval = 0; goto finish; } case PA_CMD_DUMP_RESAMPLE_METHODS: { int i; if (d < argc) { pa_log("Too many arguments.\n"); goto finish; } for (i = 0; i < PA_RESAMPLER_MAX; i++) if (pa_resample_method_supported(i)) printf("%s\n", pa_resample_method_to_string(i)); retval = 0; goto finish; } case PA_CMD_HELP : pa_cmdline_help(argv[0]); retval = 0; goto finish; case PA_CMD_VERSION : if (d < argc) { pa_log("Too many arguments.\n"); goto finish; } printf(PACKAGE_NAME" "PACKAGE_VERSION"\n"); retval = 0; goto finish; case PA_CMD_CHECK: { pid_t pid; if (d < argc) { pa_log("Too many arguments.\n"); goto finish; } if (pa_pid_file_check_running(&pid, "pulseaudio") < 0) pa_log_info(_("Daemon not running")); else { pa_log_info(_("Daemon running as PID %u"), pid); retval = 0; } goto finish; } case PA_CMD_KILL: if (d < argc) { pa_log("Too many arguments.\n"); goto finish; } if (pa_pid_file_kill(SIGINT, NULL, "pulseaudio") < 0) pa_log(_("Failed to kill daemon: %s"), pa_cstrerror(errno)); else retval = 0; goto finish; case PA_CMD_CLEANUP_SHM: if (d < argc) { pa_log("Too many arguments.\n"); goto finish; } if (pa_shm_cleanup() >= 0) retval = 0; goto finish; default: pa_assert(conf->cmd == PA_CMD_DAEMON || conf->cmd == PA_CMD_START); } if (d < argc) { pa_log("Too many arguments.\n"); goto finish; } if (getuid() == 0 && !conf->system_instance) pa_log_warn(_("This program is not intended to be run as root (unless --system is specified).")); else if (getuid() != 0 && conf->system_instance) { pa_log(_("Root privileges required.")); goto finish; } if (conf->cmd == PA_CMD_START && conf->system_instance) { pa_log(_("--start not supported for system instances.")); goto finish; } if (conf->system_instance && !conf->disallow_exit) pa_log_warn(_("Running in system mode, but --disallow-exit not set!")); if (conf->system_instance && !conf->disallow_module_loading) pa_log_warn(_("Running in system mode, but --disallow-module-loading not set!")); if (conf->system_instance && !conf->disable_shm) { pa_log_notice(_("Running in system mode, forcibly disabling SHM mode!")); conf->disable_shm = TRUE; } if (conf->system_instance && conf->exit_idle_time >= 0) { pa_log_notice(_("Running in system mode, forcibly disabling exit idle time!")); conf->exit_idle_time = -1; } if (conf->cmd == PA_CMD_START) { /* If we shall start PA only when it is not running yet, we * first take the autospawn lock to make things * synchronous. */ if ((autospawn_fd = pa_autospawn_lock_init()) < 0) { pa_log("Failed to initialize autospawn lock"); goto finish; } if ((pa_autospawn_lock_acquire(TRUE) < 0)) { pa_log("Failed to acquire autospawn lock"); goto finish; } autospawn_locked = TRUE; } if (conf->daemonize) { pid_t child; int tty_fd; if (pa_stdio_acquire() < 0) { pa_log(_("Failed to acquire stdio.")); goto finish; } #ifdef HAVE_FORK if (pipe(daemon_pipe) < 0) { pa_log(_("pipe failed: %s"), pa_cstrerror(errno)); goto finish; } if ((child = fork()) < 0) { pa_log(_("fork() failed: %s"), pa_cstrerror(errno)); goto finish; } if (child != 0) { ssize_t n; /* Father */ pa_assert_se(pa_close(daemon_pipe[1]) == 0); daemon_pipe[1] = -1; if ((n = pa_loop_read(daemon_pipe[0], &retval, sizeof(retval), NULL)) != sizeof(retval)) { if (n < 0) pa_log(_("read() failed: %s"), pa_cstrerror(errno)); retval = 1; } if (retval) pa_log(_("Daemon startup failed.")); else pa_log_info(_("Daemon startup successful.")); goto finish; } if (autospawn_fd >= 0) { /* The lock file is unlocked from the parent, so we need * to close it in the child */ pa_autospawn_lock_release(); pa_autospawn_lock_done(TRUE); autospawn_locked = FALSE; autospawn_fd = -1; } pa_assert_se(pa_close(daemon_pipe[0]) == 0); daemon_pipe[0] = -1; #endif if (conf->auto_log_target) pa_log_set_target(PA_LOG_SYSLOG); #ifdef HAVE_SETSID setsid(); #endif #ifdef HAVE_SETPGID setpgid(0,0); #endif #ifndef OS_IS_WIN32 pa_close(0); pa_close(1); pa_close(2); pa_assert_se(open("/dev/null", O_RDONLY) == 0); pa_assert_se(open("/dev/null", O_WRONLY) == 1); pa_assert_se(open("/dev/null", O_WRONLY) == 2); #else FreeConsole(); #endif #ifdef SIGTTOU signal(SIGTTOU, SIG_IGN); #endif #ifdef SIGTTIN signal(SIGTTIN, SIG_IGN); #endif #ifdef SIGTSTP signal(SIGTSTP, SIG_IGN); #endif #ifdef TIOCNOTTY if ((tty_fd = open("/dev/tty", O_RDWR)) >= 0) { ioctl(tty_fd, TIOCNOTTY, (char*) 0); pa_assert_se(pa_close(tty_fd) == 0); } #endif } pa_set_env_and_record("PULSE_INTERNAL", "1"); pa_assert_se(chdir("/") == 0); umask(0022); #ifdef HAVE_SYS_RESOURCE_H set_all_rlimits(conf); #endif pa_rtclock_hrtimer_enable(); pa_raise_priority(conf->nice_level); if (conf->system_instance) if (change_user() < 0) goto finish; pa_set_env_and_record("PULSE_SYSTEM", conf->system_instance ? "1" : "0"); pa_log_info(_("This is PulseAudio %s"), PACKAGE_VERSION); pa_log_debug(_("Compilation host: %s"), CANONICAL_HOST); pa_log_debug(_("Compilation CFLAGS: %s"), PA_CFLAGS); s = pa_uname_string(); pa_log_debug(_("Running on host: %s"), s); pa_xfree(s); pa_log_debug(_("Found %u CPUs."), pa_ncpus()); pa_log_info(_("Page size is %lu bytes"), (unsigned long) PA_PAGE_SIZE); #ifdef HAVE_VALGRIND_MEMCHECK_H pa_log_debug(_("Compiled with Valgrind support: yes")); #else pa_log_debug(_("Compiled with Valgrind support: no")); #endif pa_log_debug(_("Running in valgrind mode: %s"), pa_yes_no(pa_in_valgrind())); pa_log_debug(_("Running in VM: %s"), pa_yes_no(pa_running_in_vm())); #ifdef __OPTIMIZE__ pa_log_debug(_("Optimized build: yes")); #else pa_log_debug(_("Optimized build: no")); #endif #ifdef NDEBUG pa_log_debug(_("NDEBUG defined, all asserts disabled.")); #elif defined(FASTPATH) pa_log_debug(_("FASTPATH defined, only fast path asserts disabled.")); #else pa_log_debug(_("All asserts enabled.")); #endif if (!(s = pa_machine_id())) { pa_log(_("Failed to get machine ID")); goto finish; } pa_log_info(_("Machine ID is %s."), s); pa_xfree(s); if ((s = pa_session_id())) { pa_log_info(_("Session ID is %s."), s); pa_xfree(s); } if (!(s = pa_get_runtime_dir())) goto finish; pa_log_info(_("Using runtime directory %s."), s); pa_xfree(s); if (!(s = pa_get_state_dir())) goto finish; pa_log_info(_("Using state directory %s."), s); pa_xfree(s); pa_log_info(_("Using modules directory %s."), conf->dl_search_path); pa_log_info(_("Running in system mode: %s"), pa_yes_no(pa_in_system_mode())); if (pa_in_system_mode()) pa_log_warn(_("OK, so you are running PA in system mode. Please note that you most likely shouldn't be doing that.\n" "If you do it nonetheless then it's your own fault if things don't work as expected.\n" "Please read http://pulseaudio.org/wiki/WhatIsWrongWithSystemMode for an explanation why system mode is usually a bad idea.")); if (conf->use_pid_file) { int z; if ((z = pa_pid_file_create("pulseaudio")) != 0) { if (conf->cmd == PA_CMD_START && z > 0) { /* If we are already running and with are run in * --start mode, then let's return this as success. */ retval = 0; goto finish; } pa_log(_("pa_pid_file_create() failed.")); goto finish; } valid_pid_file = TRUE; } pa_disable_sigpipe(); if (pa_rtclock_hrtimer()) pa_log_info(_("Fresh high-resolution timers available! Bon appetit!")); else pa_log_info(_("Dude, your kernel stinks! The chef's recommendation today is Linux with high-resolution timers enabled!")); if (conf->lock_memory) { #ifdef HAVE_SYS_MMAN_H if (mlockall(MCL_FUTURE) < 0) pa_log_warn("mlockall() failed: %s", pa_cstrerror(errno)); else pa_log_info("Sucessfully locked process into memory."); #else pa_log_warn("Memory locking requested but not supported on platform."); #endif } pa_memtrap_install(); if (!getenv("PULSE_NO_SIMD")) { pa_cpu_init_x86(); pa_cpu_init_arm(); } pa_assert_se(mainloop = pa_mainloop_new()); if (!(c = pa_core_new(pa_mainloop_get_api(mainloop), !conf->disable_shm, conf->shm_size))) { pa_log(_("pa_core_new() failed.")); goto finish; } c->default_sample_spec = conf->default_sample_spec; c->default_channel_map = conf->default_channel_map; c->default_n_fragments = conf->default_n_fragments; c->default_fragment_size_msec = conf->default_fragment_size_msec; c->exit_idle_time = conf->exit_idle_time; c->scache_idle_time = conf->scache_idle_time; c->resample_method = conf->resample_method; c->realtime_priority = conf->realtime_priority; c->realtime_scheduling = !!conf->realtime_scheduling; c->disable_remixing = !!conf->disable_remixing; c->disable_lfe_remixing = !!conf->disable_lfe_remixing; c->running_as_daemon = !!conf->daemonize; c->disallow_exit = conf->disallow_exit; c->flat_volumes = conf->flat_volumes; pa_assert_se(pa_signal_init(pa_mainloop_get_api(mainloop)) == 0); pa_signal_new(SIGINT, signal_callback, c); pa_signal_new(SIGTERM, signal_callback, c); #ifdef SIGUSR1 pa_signal_new(SIGUSR1, signal_callback, c); #endif #ifdef SIGUSR2 pa_signal_new(SIGUSR2, signal_callback, c); #endif #ifdef SIGHUP pa_signal_new(SIGHUP, signal_callback, c); #endif #ifdef OS_IS_WIN32 win32_timer = pa_mainloop_get_api(mainloop)->rtclock_time_new(pa_mainloop_get_api(mainloop), pa_gettimeofday(&win32_tv), message_cb, NULL); #endif if (!conf->no_cpu_limit) pa_assert_se(pa_cpu_limit_init(pa_mainloop_get_api(mainloop)) == 0); buf = pa_strbuf_new(); if (conf->load_default_script_file) { FILE *f; if ((f = pa_daemon_conf_open_default_script_file(conf))) { r = pa_cli_command_execute_file_stream(c, f, buf, &conf->fail); fclose(f); } } if (r >= 0) r = pa_cli_command_execute(c, conf->script_commands, buf, &conf->fail); pa_log_error("%s", s = pa_strbuf_tostring_free(buf)); pa_xfree(s); /* We completed the initial module loading, so let's disable it * from now on, if requested */ c->disallow_module_loading = !!conf->disallow_module_loading; if (r < 0 && conf->fail) { pa_log(_("Failed to initialize daemon.")); goto finish; } if (!c->modules || pa_idxset_size(c->modules) == 0) { pa_log(_("Daemon startup without any loaded modules, refusing to work.")); goto finish; } #ifdef HAVE_FORK if (daemon_pipe[1] >= 0) { int ok = 0; pa_loop_write(daemon_pipe[1], &ok, sizeof(ok), NULL); pa_close(daemon_pipe[1]); daemon_pipe[1] = -1; } #endif #ifdef HAVE_DBUS dbus = register_dbus(c); #endif pa_log_info(_("Daemon startup complete.")); retval = 0; if (pa_mainloop_run(mainloop, &retval) < 0) goto finish; pa_log_info(_("Daemon shutdown initiated.")); finish: #ifdef HAVE_DBUS if (dbus) pa_dbus_connection_unref(dbus); #endif if (autospawn_fd >= 0) { if (autospawn_locked) pa_autospawn_lock_release(); pa_autospawn_lock_done(FALSE); } #ifdef OS_IS_WIN32 if (win32_timer) pa_mainloop_get_api(mainloop)->time_free(win32_timer); #endif if (c) { pa_core_unref(c); pa_log_info(_("Daemon terminated.")); } if (!conf->no_cpu_limit) pa_cpu_limit_done(); pa_signal_done(); #ifdef HAVE_FORK if (daemon_pipe[1] >= 0) pa_loop_write(daemon_pipe[1], &retval, sizeof(retval), NULL); pa_close_pipe(daemon_pipe); #endif if (mainloop) pa_mainloop_free(mainloop); if (conf) pa_daemon_conf_free(conf); if (valid_pid_file) pa_pid_file_remove(); /* This has no real purpose except making things valgrind-clean */ pa_unset_env_recorded(); #ifdef OS_IS_WIN32 WSACleanup(); #endif if (ltdl_init) pa_ltdl_done(); #ifdef HAVE_DBUS dbus_shutdown(); #endif return retval; }