Logo Search packages:      
Sourcecode: pulseaudio version File versions

pa_simple* pa_simple_new ( const char *  server,
const char *  name,
pa_stream_direction_t  dir,
const char *  dev,
const char *  stream_name,
const pa_sample_spec ss,
const pa_channel_map map,
const pa_buffer_attr attr,
int *  error 
)

Create a new connection to the server

Parameters:
server  Server name, or NULL for default
name  A descriptive name for this client (application name, ...)
dir  Open this stream for recording or playback?
dev  Sink (resp. source) name, or NULL for default
stream_name  A descriptive name for this client (application name, song title, ...)
ss  The sample type to use
map  The channel map to use, or NULL for default
attr  Buffering attributes, or NULL for default
error  A pointer where the error code is stored when the routine returns NULL. It is OK to pass NULL here.

Definition at line 137 of file simple.c.

References pa_sample_spec::channels, pa_channel_map::channels, pa_simple::context, pa_simple::direction, pa_simple::mainloop, PA_CONTEXT_READY, PA_ERR_INTERNAL, PA_ERR_INVALID, PA_STREAM_AUTO_TIMING_UPDATE, PA_STREAM_INTERPOLATE_TIMING, PA_STREAM_PLAYBACK, PA_STREAM_READY, PA_STREAM_RECORD, pa_xnew, pa_simple::read_data, pa_simple::read_index, pa_simple::read_length, and pa_simple::stream.

                     {

    pa_simple *p;
    int error = PA_ERR_INTERNAL, r;

    CHECK_VALIDITY_RETURN_ANY(rerror, !server || *server, PA_ERR_INVALID, NULL);
    CHECK_VALIDITY_RETURN_ANY(rerror, dir == PA_STREAM_PLAYBACK || dir == PA_STREAM_RECORD, PA_ERR_INVALID, NULL);
    CHECK_VALIDITY_RETURN_ANY(rerror, !dev || *dev, PA_ERR_INVALID, NULL);
    CHECK_VALIDITY_RETURN_ANY(rerror, ss && pa_sample_spec_valid(ss), PA_ERR_INVALID, NULL);
    CHECK_VALIDITY_RETURN_ANY(rerror, !map || (pa_channel_map_valid(map) && map->channels == ss->channels), PA_ERR_INVALID, NULL)

    p = pa_xnew(pa_simple, 1);
    p->context = NULL;
    p->stream = NULL;
    p->direction = dir;
    p->read_data = NULL;
    p->read_index = p->read_length = 0;

    if (!(p->mainloop = pa_threaded_mainloop_new()))
        goto fail;

    if (!(p->context = pa_context_new(pa_threaded_mainloop_get_api(p->mainloop), name)))
        goto fail;

    pa_context_set_state_callback(p->context, context_state_cb, p);

    if (pa_context_connect(p->context, server, 0, NULL) < 0) {
        error = pa_context_errno(p->context);
        goto fail;
    }

    pa_threaded_mainloop_lock(p->mainloop);

    if (pa_threaded_mainloop_start(p->mainloop) < 0)
        goto unlock_and_fail;

    /* Wait until the context is ready */
    pa_threaded_mainloop_wait(p->mainloop);

    if (pa_context_get_state(p->context) != PA_CONTEXT_READY) {
        error = pa_context_errno(p->context);
        goto unlock_and_fail;
    }

    if (!(p->stream = pa_stream_new(p->context, stream_name, ss, map))) {
        error = pa_context_errno(p->context);
        goto unlock_and_fail;
    }

    pa_stream_set_state_callback(p->stream, stream_state_cb, p);
    pa_stream_set_read_callback(p->stream, stream_request_cb, p);
    pa_stream_set_write_callback(p->stream, stream_request_cb, p);
    pa_stream_set_latency_update_callback(p->stream, stream_latency_update_cb, p);

    if (dir == PA_STREAM_PLAYBACK)
        r = pa_stream_connect_playback(p->stream, dev, attr, PA_STREAM_INTERPOLATE_TIMING|PA_STREAM_AUTO_TIMING_UPDATE, NULL, NULL);
    else
        r = pa_stream_connect_record(p->stream, dev, attr, PA_STREAM_INTERPOLATE_TIMING|PA_STREAM_AUTO_TIMING_UPDATE);

    if (r < 0) {
        error = pa_context_errno(p->context);
        goto unlock_and_fail;
    }

    /* Wait until the stream is ready */
    pa_threaded_mainloop_wait(p->mainloop);

    /* Wait until the stream is ready */
    if (pa_stream_get_state(p->stream) != PA_STREAM_READY) {
        error = pa_context_errno(p->context);
        goto unlock_and_fail;
    }

    pa_threaded_mainloop_unlock(p->mainloop);

    return p;

unlock_and_fail:
    pa_threaded_mainloop_unlock(p->mainloop);

fail:
    if (rerror)
        *rerror = error;
    pa_simple_free(p);
    return NULL;
}


Generated by  Doxygen 1.6.0   Back to index