static void pulse_set_volume(int l, int r) { if (! connected) return; pa_threaded_mainloop_lock (mainloop); CHECK_DEAD_GOTO (fail, 1); /* sanitize output volumes. */ l = CLAMP(l, 0, 100); r = CLAMP(r, 0, 100); if (!volume_valid || volume.channels != 1) { volume.values[0] = ((pa_volume_t) l * PA_VOLUME_NORM + 50) / 100; volume.values[1] = ((pa_volume_t) r * PA_VOLUME_NORM + 50) / 100; volume.channels = 2; } else { volume.values[0] = ((pa_volume_t) MAX (l, r) * PA_VOLUME_NORM + 50) / 100; volume.channels = 1; } volume_valid = 1; if (connected && !volume_time_event) { struct timeval tv; pa_mainloop_api *api = pa_threaded_mainloop_get_api(mainloop); volume_time_event = api->time_new(api, pa_timeval_add(pa_gettimeofday(&tv), 100000), volume_time_cb, NULL); } fail: if (connected) pa_threaded_mainloop_unlock(mainloop); }
static void message_cb(pa_mainloop_api*a, pa_time_event*e, const struct timeval *tv, void *userdata) { MSG msg; struct timeval tvnext; while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { if (msg.message == WM_QUIT) raise(SIGTERM); else { TranslateMessage(&msg); DispatchMessage(&msg); } } pa_timeval_add(pa_gettimeofday(&tvnext), 100000); a->time_restart(e, &tvnext); }
static void thread_func(void *userdata) { struct userdata *u = userdata; pa_assert(u); pa_log_debug("Thread starting up"); pa_thread_mq_install(&u->thread_mq); pa_rtpoll_install(u->rtpoll); pa_rtclock_get(&u->timestamp); for (;;) { int ret; /* Render some data and drop it immediately */ if (u->sink->thread_info.state == PA_SINK_RUNNING) { struct timeval now; pa_rtclock_get(&now); if (pa_timeval_cmp(&u->timestamp, &now) <= 0) { pa_sink_skip(u->sink, u->block_size); pa_timeval_add(&u->timestamp, pa_bytes_to_usec(u->block_size, &u->sink->sample_spec)); } pa_rtpoll_set_timer_absolute(u->rtpoll, &u->timestamp); } else pa_rtpoll_set_timer_disabled(u->rtpoll); /* Hmm, nothing to do. Let's sleep */ if ((ret = pa_rtpoll_run(u->rtpoll, 1)) < 0) goto fail; if (ret == 0) goto finish; } fail: /* If this was no regular exit from the loop we have to continue * processing messages until we received PA_MESSAGE_SHUTDOWN */ pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL); pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN); finish: pa_log_debug("Thread shutting down"); }
void QPulseAudioThread::time_event_callback ( pa_mainloop_api*m, pa_time_event *e, const struct timeval *tv, void *userdata ) { struct timeval next; if ( stream && pa_stream_get_state ( stream ) == PA_STREAM_READY ) { pa_operation *o; if ( ! ( o = pa_stream_update_timing_info ( stream, stream_update_timing_callback, NULL ) ) ) fprintf ( stderr, "pa_stream_update_timing_info() failed: %s\n", pa_strerror ( pa_context_errno ( context ) ) ); else pa_operation_unref ( o ); } pa_gettimeofday ( &next ); pa_timeval_add ( &next, TIME_EVENT_USEC ); m->time_restart ( e, &next ); }
static struct timeval* wallclock_from_rtclock(struct timeval *tv) { struct timeval wc_now, rt_now; pa_assert(tv); pa_gettimeofday(&wc_now); pa_rtclock_get(&rt_now); /* pa_timeval_sub() saturates on underflow! */ if (pa_timeval_cmp(&rt_now, tv) < 0) pa_timeval_add(&wc_now, pa_timeval_diff(tv, &rt_now)); else pa_timeval_sub(&wc_now, pa_timeval_diff(&rt_now, tv)); *tv = wc_now; return tv; }
static void work(void *p) { pa_log_notice("CPU%i: Created thread.", PA_PTR_TO_UINT(p)); pa_make_realtime(12); #ifdef HAVE_PTHREAD_SETAFFINITY_NP { #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) cpuset_t mask; #else cpu_set_t mask; #endif CPU_ZERO(&mask); CPU_SET((size_t) PA_PTR_TO_UINT(p), &mask); pa_assert_se(pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) == 0); } #endif for (;;) { struct timeval now, end; uint64_t usec; pa_log_notice("CPU%i: Sleeping for 1s", PA_PTR_TO_UINT(p)); pa_msleep(1000); usec = (uint64_t) ((((double) rand())*(double)(msec_upper-msec_lower)*PA_USEC_PER_MSEC)/RAND_MAX) + (uint64_t) ((uint64_t) msec_lower*PA_USEC_PER_MSEC); pa_log_notice("CPU%i: Freezing for %ims", PA_PTR_TO_UINT(p), (int) (usec/PA_USEC_PER_MSEC)); pa_rtclock_get(&end); pa_timeval_add(&end, usec); do { pa_rtclock_get(&now); } while (pa_timeval_cmp(&now, &end) < 0); } }
static void restart_time_event (GstPulseMixerCtrl * c) { struct timeval tv; pa_mainloop_api *api; g_assert (c); if (c->time_event) return; /* Updating the volume too often will cause a lot of traffic * when accessing a networked server. Therefore we make sure * to update the volume only once every 50ms */ api = pa_threaded_mainloop_get_api (c->mainloop); c->time_event = api->time_new (api, pa_timeval_add (pa_gettimeofday (&tv), UPDATE_DELAY), gst_pulsemixer_ctrl_timeout_event, c); }
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 ; }