gstreamer/gst/rtsp-server/rtsp-session-media.c
Wim Taymans de7c72dec2 rtsp: massive refactoring
Make GObjects from the remaining simple structures.
Remove GstRTSPSessionStream, it's not needed.
Rename GstRTSPMediaStream -> GstRTSPStream: It is shorter
Rename GstRTSPMediaTrans -> GstRTSPStreamTransport: It describes how
  a GstRTSPStream should be transported to a client.
Rename GstRTSPMediaFactory::get_element -> create_element because that
  more accurately describes what it does.
Make nice methods instead of poking in the structures.
Move some methods inside the relevant object source code.
Use GPtrArray to store objects instead of plain arrays, it is more
  natural and allows us to more easily clean up.
Move the allocation of udp ports to the Stream object. The Stream object
  contains the elements needed to stream the media to a client.
Improve the prepare and unprepare methods. Unprepare should now undo
  everything prepare did. Improve also async unprepare when doing EOS on
  shutdown. Make sure we always unprepare correctly.
2012-10-25 21:29:58 +02:00

202 lines
5 KiB
C

/* GStreamer
* Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include <string.h>
#include "rtsp-session.h"
#undef DEBUG
#define DEFAULT_TIMEOUT 60
enum
{
PROP_0,
PROP_LAST
};
GST_DEBUG_CATEGORY_STATIC (rtsp_session_media_debug);
#define GST_CAT_DEFAULT rtsp_session_media_debug
static void gst_rtsp_session_media_finalize (GObject * obj);
G_DEFINE_TYPE (GstRTSPSessionMedia, gst_rtsp_session_media, G_TYPE_OBJECT);
static void
gst_rtsp_session_media_class_init (GstRTSPSessionMediaClass * klass)
{
GObjectClass *gobject_class;
gobject_class = G_OBJECT_CLASS (klass);
gobject_class->finalize = gst_rtsp_session_media_finalize;
GST_DEBUG_CATEGORY_INIT (rtsp_session_media_debug, "rtspsessionmedia", 0,
"GstRTSPSessionMedia");
}
static void
gst_rtsp_session_media_init (GstRTSPSessionMedia * media)
{
media->state = GST_RTSP_STATE_INIT;
}
static void
gst_rtsp_session_media_finalize (GObject * obj)
{
GstRTSPSessionMedia *media;
media = GST_RTSP_SESSION_MEDIA (obj);
GST_INFO ("free session media %p", media);
gst_rtsp_session_media_set_state (media, GST_STATE_NULL);
g_ptr_array_unref (media->transports);
gst_rtsp_url_free (media->url);
g_object_unref (media->media);
G_OBJECT_CLASS (gst_rtsp_session_media_parent_class)->finalize (obj);
}
static void
free_session_media (gpointer data)
{
if (data)
g_object_unref (data);
}
/**
* gst_rtsp_session_media_new:
* @url: the #GstRTSPUrl
* @media: the #GstRTSPMedia
*
* Create a new #GstRTPSessionMedia that manages the streams
* in @media for @url. @media should be prepared.
*
* Ownership is taken of @media.
*
* Returns: a new #GstRTSPSessionMedia.
*/
GstRTSPSessionMedia *
gst_rtsp_session_media_new (const GstRTSPUrl * url, GstRTSPMedia * media)
{
GstRTSPSessionMedia *result;
guint n_streams;
g_return_val_if_fail (url != NULL, NULL);
g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
g_return_val_if_fail (media->status == GST_RTSP_MEDIA_STATUS_PREPARED, NULL);
result = g_object_new (GST_TYPE_RTSP_SESSION_MEDIA, NULL);
result->url = gst_rtsp_url_copy ((GstRTSPUrl *) url);
result->media = media;
/* prealloc the streams now, filled with NULL */
n_streams = gst_rtsp_media_n_streams (media);
result->transports = g_ptr_array_new_full (n_streams, free_session_media);
g_ptr_array_set_size (result->transports, n_streams);
return result;
}
/**
* gst_rtsp_session_media_get_transport:
* @media: a #GstRTSPSessionMedia
* @idx: the stream index
*
* Get a previously created or create a new #GstRTSPStreamTransport at @idx.
*
* Returns: a #GstRTSPStreamTransport that is valid until the session of @media
* is unreffed.
*/
GstRTSPStreamTransport *
gst_rtsp_session_media_get_transport (GstRTSPSessionMedia * media, guint idx)
{
GstRTSPStreamTransport *result;
g_return_val_if_fail (GST_IS_RTSP_SESSION_MEDIA (media), NULL);
g_return_val_if_fail (media->media != NULL, NULL);
if (idx >= media->transports->len)
return NULL;
result = g_ptr_array_index (media->transports, idx);
if (result == NULL) {
GstRTSPStream *stream;
stream = gst_rtsp_media_get_stream (media->media, idx);
if (stream == NULL)
goto no_media;
result = gst_rtsp_stream_transport_new (stream);
g_ptr_array_index (media->transports, idx) = result;
}
return result;
/* ERRORS */
no_media:
{
return NULL;
}
}
/**
* gst_rtsp_session_media_alloc_channels:
* @media: a #GstRTSPSessionMedia
* @range: a #GstRTSPRange
*
* Fill @range with the next available min and max channels for
* interleaved transport.
*
* Returns: %TRUE on success.
*/
gboolean
gst_rtsp_session_media_alloc_channels (GstRTSPSessionMedia * media,
GstRTSPRange * range)
{
g_return_val_if_fail (GST_IS_RTSP_SESSION_MEDIA (media), FALSE);
range->min = media->counter++;
range->max = media->counter++;
return TRUE;
}
/**
* gst_rtsp_session_media_set_state:
* @media: a #GstRTSPSessionMedia
* @state: the new state
*
* Tell the media object @media to change to @state.
*
* Returns: %TRUE on success.
*/
gboolean
gst_rtsp_session_media_set_state (GstRTSPSessionMedia * media, GstState state)
{
gboolean ret;
g_return_val_if_fail (GST_IS_RTSP_SESSION_MEDIA (media), FALSE);
ret = gst_rtsp_media_set_state (media->media, state, media->transports);
return ret;
}