rtsp: use child sources instead of using the sockets

Use the source of the pollable input/output streams instead of
accessing the sockets directly.
This commit is contained in:
Wim Taymans 2013-05-29 17:44:30 +02:00
parent 4ada677095
commit d09028b4c3

View file

@ -2730,8 +2730,8 @@ struct _GstRTSPWatch
GstRTSPBuilder builder;
GstRTSPMessage message;
GPollFD readfd;
GPollFD writefd;
GSource *readsrc;
GSource *writesrc;
/* queued message for transmission */
guint id;
@ -2767,38 +2767,19 @@ gst_rtsp_source_prepare (GSource * source, gint * timeout)
static gboolean
gst_rtsp_source_check (GSource * source)
{
GstRTSPWatch *watch = (GstRTSPWatch *) source;
if (watch->readfd.revents & READ_COND)
return TRUE;
if (watch->writefd.revents & WRITE_COND)
return TRUE;
return FALSE;
}
static gboolean
gst_rtsp_source_dispatch (GSource * source, GSourceFunc callback G_GNUC_UNUSED,
gpointer user_data G_GNUC_UNUSED)
gst_rtsp_source_dispatch_read (GPollableInputStream * stream,
GstRTSPWatch * watch)
{
GstRTSPWatch *watch = (GstRTSPWatch *) source;
GstRTSPResult res = GST_RTSP_ERROR;
gboolean keep_running = TRUE;
/* first read as much as we can */
if (watch->readfd.revents & READ_COND || watch->conn->initial_buffer != NULL) {
do {
if (watch->readfd.revents & READ_ERR)
goto read_error;
res = build_next (&watch->builder, &watch->message, watch->conn, FALSE);
if (res == GST_RTSP_EINTR)
break;
goto done;
else if (G_UNLIKELY (res == GST_RTSP_EEOF)) {
watch->readfd.events = 0;
watch->readfd.revents = 0;
g_source_remove_poll ((GSource *) watch, &watch->readfd);
/* When we are in tunnelled mode, the read socket can be closed and we
* should be prepared for a new POST method to reopen it */
if (watch->conn->tstate == TUNNEL_STATE_COMPLETE) {
@ -2839,15 +2820,12 @@ gst_rtsp_source_dispatch (GSource * source, GSourceFunc callback G_GNUC_UNUSED,
* GET connection */
if (watch->funcs.tunnel_complete) {
watch->funcs.tunnel_complete (watch, watch->user_data);
keep_running = !(watch->conn->read_socket == NULL &&
watch->conn->write_socket == NULL);
if (!keep_running)
goto done;
}
goto read_done;
}
}
}
} else
goto read_error;
if (!watch->conn->manual_http) {
/* if manual HTTP support is not enabled, then restore the message to
@ -2862,29 +2840,59 @@ gst_rtsp_source_dispatch (GSource * source, GSourceFunc callback G_GNUC_UNUSED,
} else if (watch->message.type == GST_RTSP_MESSAGE_HTTP_RESPONSE) {
watch->message.type = GST_RTSP_MESSAGE_RESPONSE;
if (watch->message.type_data.response.version != GST_RTSP_VERSION_1_0)
watch->message.type_data.response.version =
GST_RTSP_VERSION_INVALID;
watch->message.type_data.response.version = GST_RTSP_VERSION_INVALID;
res = GST_RTSP_EPARSE;
}
}
if (G_LIKELY (res == GST_RTSP_OK)) {
if (watch->funcs.message_received)
watch->funcs.message_received (watch, &watch->message,
watch->user_data);
} else {
if (G_LIKELY (res != GST_RTSP_OK))
goto read_error;
}
if (watch->funcs.message_received)
watch->funcs.message_received (watch, &watch->message, watch->user_data);
read_done:
gst_rtsp_message_unset (&watch->message);
build_reset (&watch->builder);
} while (FALSE);
done:
return TRUE;
/* ERRORS */
eof:
{
if (watch->funcs.closed)
watch->funcs.closed (watch, watch->user_data);
/* always stop when the readfd returns EOF in non-tunneled mode */
return FALSE;
}
read_error:
{
if (watch->funcs.error_full)
GST_RTSP_CHECK (watch->funcs.error_full (watch, res, &watch->message,
0, watch->user_data), error);
}
error:
{
if (watch->funcs.error)
watch->funcs.error (watch, res, watch->user_data);
return FALSE;
}
}
if (watch->writefd.revents & WRITE_COND) {
if (watch->writefd.revents & WRITE_ERR)
goto write_error;
static gboolean
gst_rtsp_source_dispatch (GSource * source, GSourceFunc callback G_GNUC_UNUSED,
gpointer user_data G_GNUC_UNUSED)
{
return TRUE;
}
static gboolean
gst_rtsp_source_dispatch_write (GPollableInputStream * stream,
GstRTSPWatch * watch)
{
GstRTSPResult res = GST_RTSP_ERROR;
g_mutex_lock (&watch->mutex);
do {
@ -2907,8 +2915,7 @@ gst_rtsp_source_dispatch (GSource * source, GSourceFunc callback G_GNUC_UNUSED,
}
res = write_bytes (watch->conn->output_stream, watch->write_data,
&watch->write_off, watch->write_size, FALSE,
watch->conn->cancellable);
&watch->write_off, watch->write_size, FALSE, watch->conn->cancellable);
g_mutex_unlock (&watch->mutex);
if (res == GST_RTSP_EINTR)
@ -2924,63 +2931,24 @@ gst_rtsp_source_dispatch (GSource * source, GSourceFunc callback G_GNUC_UNUSED,
g_free (watch->write_data);
watch->write_data = NULL;
} while (TRUE);
watch->writefd.events = WRITE_ERR;
g_mutex_unlock (&watch->mutex);
}
done:
write_blocked:
return keep_running;
return TRUE;
/* ERRORS */
eof:
{
if (watch->funcs.closed)
watch->funcs.closed (watch, watch->user_data);
/* always stop when the readfd returns EOF in non-tunneled mode */
return FALSE;
}
read_error:
{
watch->readfd.events = 0;
watch->readfd.revents = 0;
g_source_remove_poll ((GSource *) watch, &watch->readfd);
keep_running = (watch->writefd.events != 0);
if (keep_running) {
if (watch->funcs.error_full)
GST_RTSP_CHECK (watch->funcs.error_full (watch, res, &watch->message,
0, watch->user_data), error);
else
goto error;
} else
goto eof;
}
write_error:
{
watch->writefd.events = 0;
watch->writefd.revents = 0;
g_source_remove_poll ((GSource *) watch, &watch->writefd);
keep_running = (watch->readfd.events != 0);
if (keep_running) {
if (watch->funcs.error_full)
GST_RTSP_CHECK (watch->funcs.error_full (watch, res, NULL,
watch->write_id, watch->user_data), error);
else
goto error;
} else
goto eof;
}
error:
{
if (watch->funcs.error)
watch->funcs.error (watch, res, watch->user_data);
return keep_running;
return FALSE;
}
}
@ -3060,9 +3028,6 @@ gst_rtsp_watch_new (GstRTSPConnection * conn,
g_mutex_init (&result->mutex);
result->messages = g_queue_new ();
result->readfd.fd = -1;
result->writefd.fd = -1;
gst_rtsp_watch_reset (result);
result->funcs = *funcs;
@ -3082,23 +3047,30 @@ gst_rtsp_watch_new (GstRTSPConnection * conn,
void
gst_rtsp_watch_reset (GstRTSPWatch * watch)
{
if (watch->readfd.fd != -1)
g_source_remove_poll ((GSource *) watch, &watch->readfd);
if (watch->writefd.fd != -1)
g_source_remove_poll ((GSource *) watch, &watch->writefd);
if (watch->readsrc)
g_source_remove_child_source ((GSource *) watch, watch->readsrc);
if (watch->writesrc)
g_source_remove_child_source ((GSource *) watch, watch->writesrc);
watch->readfd.fd = g_socket_get_fd (watch->conn->read_socket);
watch->readfd.events = READ_COND;
watch->readfd.revents = 0;
watch->readsrc =
g_pollable_input_stream_create_source (G_POLLABLE_INPUT_STREAM
(watch->conn->input_stream), NULL);
g_source_set_callback (watch->readsrc,
(GSourceFunc) gst_rtsp_source_dispatch_read, watch, NULL);
watch->writesrc =
g_pollable_output_stream_create_source (G_POLLABLE_OUTPUT_STREAM
(watch->conn->output_stream), NULL);
g_source_set_callback (watch->writesrc,
(GSourceFunc) gst_rtsp_source_dispatch_write, watch, NULL);
watch->writefd.fd = g_socket_get_fd (watch->conn->write_socket);
watch->writefd.events = WRITE_ERR;
watch->writefd.revents = 0;
if (watch->readfd.fd != -1)
g_source_add_poll ((GSource *) watch, &watch->readfd);
if (watch->writefd.fd != -1)
g_source_add_poll ((GSource *) watch, &watch->writefd);
if (watch->readsrc) {
g_source_add_child_source ((GSource *) watch, watch->readsrc);
g_source_unref (watch->readsrc);
}
if (watch->writesrc) {
g_source_add_child_source ((GSource *) watch, watch->writesrc);
g_source_unref (watch->writesrc);
}
}
/**
@ -3266,10 +3238,7 @@ gst_rtsp_watch_write_data (GstRTSPWatch * watch, const guint8 * data,
/* make sure the main context will now also check for writability on the
* socket */
if (watch->writefd.events != WRITE_COND) {
watch->writefd.events = WRITE_COND;
context = ((GSource *) watch)->context;
}
if (id != NULL)
*id = rec->id;