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; GstRTSPBuilder builder;
GstRTSPMessage message; GstRTSPMessage message;
GPollFD readfd; GSource *readsrc;
GPollFD writefd; GSource *writesrc;
/* queued message for transmission */ /* queued message for transmission */
guint id; guint id;
@ -2767,38 +2767,19 @@ gst_rtsp_source_prepare (GSource * source, gint * timeout)
static gboolean static gboolean
gst_rtsp_source_check (GSource * source) 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; return FALSE;
} }
static gboolean static gboolean
gst_rtsp_source_dispatch (GSource * source, GSourceFunc callback G_GNUC_UNUSED, gst_rtsp_source_dispatch_read (GPollableInputStream * stream,
gpointer user_data G_GNUC_UNUSED) GstRTSPWatch * watch)
{ {
GstRTSPWatch *watch = (GstRTSPWatch *) source;
GstRTSPResult res = GST_RTSP_ERROR; 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); res = build_next (&watch->builder, &watch->message, watch->conn, FALSE);
if (res == GST_RTSP_EINTR) if (res == GST_RTSP_EINTR)
break; goto done;
else if (G_UNLIKELY (res == GST_RTSP_EEOF)) { 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 /* 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 */ * should be prepared for a new POST method to reopen it */
if (watch->conn->tstate == TUNNEL_STATE_COMPLETE) { if (watch->conn->tstate == TUNNEL_STATE_COMPLETE) {
@ -2839,15 +2820,12 @@ gst_rtsp_source_dispatch (GSource * source, GSourceFunc callback G_GNUC_UNUSED,
* GET connection */ * GET connection */
if (watch->funcs.tunnel_complete) { if (watch->funcs.tunnel_complete) {
watch->funcs.tunnel_complete (watch, watch->user_data); 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; goto read_done;
} }
} }
} } else
goto read_error;
if (!watch->conn->manual_http) { if (!watch->conn->manual_http) {
/* if manual HTTP support is not enabled, then restore the message to /* 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) { } else if (watch->message.type == GST_RTSP_MESSAGE_HTTP_RESPONSE) {
watch->message.type = GST_RTSP_MESSAGE_RESPONSE; watch->message.type = GST_RTSP_MESSAGE_RESPONSE;
if (watch->message.type_data.response.version != GST_RTSP_VERSION_1_0) if (watch->message.type_data.response.version != GST_RTSP_VERSION_1_0)
watch->message.type_data.response.version = watch->message.type_data.response.version = GST_RTSP_VERSION_INVALID;
GST_RTSP_VERSION_INVALID;
res = GST_RTSP_EPARSE; res = GST_RTSP_EPARSE;
} }
} }
if (G_LIKELY (res != GST_RTSP_OK))
if (G_LIKELY (res == GST_RTSP_OK)) {
if (watch->funcs.message_received)
watch->funcs.message_received (watch, &watch->message,
watch->user_data);
} else {
goto read_error; goto read_error;
}
read_done: if (watch->funcs.message_received)
watch->funcs.message_received (watch, &watch->message, watch->user_data);
read_done:
gst_rtsp_message_unset (&watch->message); gst_rtsp_message_unset (&watch->message);
build_reset (&watch->builder); build_reset (&watch->builder);
} while (FALSE);
}
if (watch->writefd.revents & WRITE_COND) { done:
if (watch->writefd.revents & WRITE_ERR) return TRUE;
goto write_error;
/* 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;
}
}
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); g_mutex_lock (&watch->mutex);
do { 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, res = write_bytes (watch->conn->output_stream, watch->write_data,
&watch->write_off, watch->write_size, FALSE, &watch->write_off, watch->write_size, FALSE, watch->conn->cancellable);
watch->conn->cancellable);
g_mutex_unlock (&watch->mutex); g_mutex_unlock (&watch->mutex);
if (res == GST_RTSP_EINTR) 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); g_free (watch->write_data);
watch->write_data = NULL; watch->write_data = NULL;
} while (TRUE); } while (TRUE);
watch->writefd.events = WRITE_ERR;
g_mutex_unlock (&watch->mutex); g_mutex_unlock (&watch->mutex);
}
done:
write_blocked: write_blocked:
return keep_running; return TRUE;
/* ERRORS */ /* 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: 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) if (watch->funcs.error_full)
GST_RTSP_CHECK (watch->funcs.error_full (watch, res, NULL, GST_RTSP_CHECK (watch->funcs.error_full (watch, res, NULL,
watch->write_id, watch->user_data), error); watch->write_id, watch->user_data), error);
else
goto error;
} else
goto eof;
} }
error: error:
{ {
if (watch->funcs.error) if (watch->funcs.error)
watch->funcs.error (watch, res, watch->user_data); 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); g_mutex_init (&result->mutex);
result->messages = g_queue_new (); result->messages = g_queue_new ();
result->readfd.fd = -1;
result->writefd.fd = -1;
gst_rtsp_watch_reset (result); gst_rtsp_watch_reset (result);
result->funcs = *funcs; result->funcs = *funcs;
@ -3082,23 +3047,30 @@ gst_rtsp_watch_new (GstRTSPConnection * conn,
void void
gst_rtsp_watch_reset (GstRTSPWatch * watch) gst_rtsp_watch_reset (GstRTSPWatch * watch)
{ {
if (watch->readfd.fd != -1) if (watch->readsrc)
g_source_remove_poll ((GSource *) watch, &watch->readfd); g_source_remove_child_source ((GSource *) watch, watch->readsrc);
if (watch->writefd.fd != -1) if (watch->writesrc)
g_source_remove_poll ((GSource *) watch, &watch->writefd); g_source_remove_child_source ((GSource *) watch, watch->writesrc);
watch->readfd.fd = g_socket_get_fd (watch->conn->read_socket); watch->readsrc =
watch->readfd.events = READ_COND; g_pollable_input_stream_create_source (G_POLLABLE_INPUT_STREAM
watch->readfd.revents = 0; (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); if (watch->readsrc) {
watch->writefd.events = WRITE_ERR; g_source_add_child_source ((GSource *) watch, watch->readsrc);
watch->writefd.revents = 0; g_source_unref (watch->readsrc);
}
if (watch->readfd.fd != -1) if (watch->writesrc) {
g_source_add_poll ((GSource *) watch, &watch->readfd); g_source_add_child_source ((GSource *) watch, watch->writesrc);
if (watch->writefd.fd != -1) g_source_unref (watch->writesrc);
g_source_add_poll ((GSource *) watch, &watch->writefd); }
} }
/** /**
@ -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 /* make sure the main context will now also check for writability on the
* socket */ * socket */
if (watch->writefd.events != WRITE_COND) {
watch->writefd.events = WRITE_COND;
context = ((GSource *) watch)->context; context = ((GSource *) watch)->context;
}
if (id != NULL) if (id != NULL)
*id = rec->id; *id = rec->id;