+
+ pa_threaded_mainloop_wait(m);
+ }
+
+ h->m = m;
+ h->c = c;
+
+ pa_threaded_mainloop_unlock(m);
+
+ return 0;
+
+error:
+ pa_context_unref(c);
+ pa_threaded_mainloop_unlock(m);
+error_context_new:
+ pa_threaded_mainloop_free(m);
+
+ return -1;
+}
+
+static int _pa_stream_connect(wave_info_t *h)
+{
+ int ret = PA_OK;
+ pa_stream *s = NULL;
+ pa_proplist *proplist = NULL;
+ pa_stream_state_t state;
+
+ proplist = pa_proplist_new();
+ if (!proplist)
+ return -1;
+ pa_proplist_sets(proplist, PA_PROP_MEDIA_ROLE, h->stream_type);
+ pa_proplist_setf(proplist, PA_PROP_APPLICATION_PROCESS_ID_ORIGIN, "%d", h->client_pid);
+ if (h->stream_index != -1)
+ pa_proplist_setf(proplist, PA_PROP_MEDIA_PARENT_ID, "%d", h->stream_index);
+
+ pa_threaded_mainloop_lock(h->m);
+
+ s = pa_stream_new_with_proplist(h->c, "wav-player", &h->spec, NULL, proplist);
+ pa_proplist_free(proplist);
+ if (!s) {
+ debug_error("pa_stream_new failed. file(%s)", h->filename);
+ goto error;
+ }
+
+ pa_stream_set_state_callback(s, _pa_stream_state_callback, h->m);
+ pa_stream_set_write_callback(s, _pa_stream_write_callback, h);
+ pa_stream_set_moved_callback(s, _pa_stream_moved_callback, h);
+ pa_stream_set_underflow_callback(s, _pa_stream_underflow_callback, h);
+ pa_stream_set_buffer_attr_callback(s, _pa_stream_buffer_attr_callback, h);
+ pa_stream_set_started_callback(s, _pa_stream_started_callback, h);
+
+ ret = pa_stream_connect_playback(s, NULL, NULL,
+ PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_ADJUST_LATENCY | PA_STREAM_AUTO_TIMING_UPDATE | PA_STREAM_START_CORKED,
+ NULL, NULL);
+ if (ret < 0) {
+ debug_error("stream(%p) : failed to connect playback, ret(%d)", s, ret);
+ goto error;
+ }
+ for (;;) {
+ state = pa_stream_get_state(s);
+
+ if (state == PA_STREAM_READY)
+ break;
+
+ if (!PA_STREAM_IS_GOOD(state)) {
+ debug_error("stream(%p) : state(%d)", s, state);
+ goto error;