2 * Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 #include "rtsp-session.h"
23 #define GST_RTSP_SESSION_MEDIA_GET_PRIVATE(obj) \
24 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_SESSION_MEDIA, GstRTSPSessionMediaPrivate))
26 struct _GstRTSPSessionMediaPrivate
29 gchar *path; /* unmutable */
30 gint path_len; /* unmutable */
31 GstRTSPMedia *media; /* unmutable */
32 GstRTSPState state; /* protected by lock */
33 guint counter; /* protected by lock */
35 GPtrArray *transports; /* protected by lock */
44 GST_DEBUG_CATEGORY_STATIC (rtsp_session_media_debug);
45 #define GST_CAT_DEFAULT rtsp_session_media_debug
47 static void gst_rtsp_session_media_finalize (GObject * obj);
49 G_DEFINE_TYPE (GstRTSPSessionMedia, gst_rtsp_session_media, G_TYPE_OBJECT);
52 gst_rtsp_session_media_class_init (GstRTSPSessionMediaClass * klass)
54 GObjectClass *gobject_class;
56 g_type_class_add_private (klass, sizeof (GstRTSPSessionMediaPrivate));
58 gobject_class = G_OBJECT_CLASS (klass);
60 gobject_class->finalize = gst_rtsp_session_media_finalize;
62 GST_DEBUG_CATEGORY_INIT (rtsp_session_media_debug, "rtspsessionmedia", 0,
63 "GstRTSPSessionMedia");
67 gst_rtsp_session_media_init (GstRTSPSessionMedia * media)
69 GstRTSPSessionMediaPrivate *priv = GST_RTSP_SESSION_MEDIA_GET_PRIVATE (media);
73 g_mutex_init (&priv->lock);
74 priv->state = GST_RTSP_STATE_INIT;
78 gst_rtsp_session_media_finalize (GObject * obj)
80 GstRTSPSessionMedia *media;
81 GstRTSPSessionMediaPrivate *priv;
83 media = GST_RTSP_SESSION_MEDIA (obj);
86 GST_INFO ("free session media %p", media);
88 gst_rtsp_session_media_set_state (media, GST_STATE_NULL);
90 g_ptr_array_unref (priv->transports);
93 g_object_unref (priv->media);
94 g_mutex_clear (&priv->lock);
96 G_OBJECT_CLASS (gst_rtsp_session_media_parent_class)->finalize (obj);
100 free_session_media (gpointer data)
103 g_object_unref (data);
107 * gst_rtsp_session_media_new:
109 * @media: the #GstRTSPMedia
111 * Create a new #GstRTPSessionMedia that manages the streams
112 * in @media for @path. @media should be prepared.
114 * Ownership is taken of @media.
116 * Returns: a new #GstRTSPSessionMedia.
118 GstRTSPSessionMedia *
119 gst_rtsp_session_media_new (const gchar * path, GstRTSPMedia * media)
121 GstRTSPSessionMediaPrivate *priv;
122 GstRTSPSessionMedia *result;
125 g_return_val_if_fail (path != NULL, NULL);
126 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
127 g_return_val_if_fail (gst_rtsp_media_get_status (media) ==
128 GST_RTSP_MEDIA_STATUS_PREPARED, NULL);
130 result = g_object_new (GST_TYPE_RTSP_SESSION_MEDIA, NULL);
133 priv->path = g_strdup (path);
134 priv->path_len = strlen (path);
137 /* prealloc the streams now, filled with NULL */
138 n_streams = gst_rtsp_media_n_streams (media);
139 priv->transports = g_ptr_array_new_full (n_streams, free_session_media);
140 g_ptr_array_set_size (priv->transports, n_streams);
146 * gst_rtsp_session_media_matches:
147 * @media: a #GstRTSPSessionMedia
149 * @matched: the amount of matched characters of @path
151 * Check if the path of @media matches @path. It @path matches, the amount of
152 * matched characters is returned in @matched.
154 * Returns: %TRUE when @path matches the path of @media.
157 gst_rtsp_session_media_matches (GstRTSPSessionMedia * media,
158 const gchar * path, gint * matched)
160 GstRTSPSessionMediaPrivate *priv;
163 g_return_val_if_fail (GST_IS_RTSP_SESSION_MEDIA (media), FALSE);
164 g_return_val_if_fail (path != NULL, FALSE);
165 g_return_val_if_fail (matched != NULL, FALSE);
170 /* path needs to be smaller than the media path */
171 if (len < priv->path_len)
174 /* if media path is larger, it there should be a / following the path */
175 if (len > priv->path_len && path[priv->path_len] != '/')
178 *matched = priv->path_len;
180 return strncmp (path, priv->path, priv->path_len) == 0;
184 * gst_rtsp_session_media_get_media:
185 * @media: a #GstRTSPSessionMedia
187 * Get the #GstRTSPMedia that was used when constructing @media
189 * Returns: (transfer none): the #GstRTSPMedia of @media. Remains valid as long
190 * as @media is valid.
193 gst_rtsp_session_media_get_media (GstRTSPSessionMedia * media)
195 g_return_val_if_fail (GST_IS_RTSP_SESSION_MEDIA (media), NULL);
197 return media->priv->media;
201 * gst_rtsp_session_media_get_base_time:
202 * @media: a #GstRTSPSessionMedia
204 * Get the base_time of the #GstRTSPMedia in @media
206 * Returns: the base_time of the media.
209 gst_rtsp_session_media_get_base_time (GstRTSPSessionMedia * media)
211 g_return_val_if_fail (GST_IS_RTSP_SESSION_MEDIA (media), GST_CLOCK_TIME_NONE);
213 return gst_rtsp_media_get_base_time (media->priv->media);
217 * gst_rtsp_session_media_set_transport:
218 * @media: a #GstRTSPSessionMedia
219 * @stream: a #GstRTSPStream
220 * @tr: a #GstRTSPTransport
222 * Configure the transport for @stream to @tr in @media.
224 * Returns: (transfer none): the new or updated #GstRTSPStreamTransport for @stream.
226 GstRTSPStreamTransport *
227 gst_rtsp_session_media_set_transport (GstRTSPSessionMedia * media,
228 GstRTSPStream * stream, GstRTSPTransport * tr)
230 GstRTSPSessionMediaPrivate *priv;
231 GstRTSPStreamTransport *result;
234 g_return_val_if_fail (GST_IS_RTSP_SESSION_MEDIA (media), NULL);
235 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
236 g_return_val_if_fail (tr != NULL, NULL);
238 idx = gst_rtsp_stream_get_index (stream);
239 g_return_val_if_fail (idx < priv->transports->len, NULL);
241 g_mutex_lock (&priv->lock);
242 result = g_ptr_array_index (priv->transports, idx);
243 if (result == NULL) {
244 result = gst_rtsp_stream_transport_new (stream, tr);
245 g_ptr_array_index (priv->transports, idx) = result;
246 g_mutex_unlock (&priv->lock);
248 gst_rtsp_stream_transport_set_transport (result, tr);
249 g_mutex_unlock (&priv->lock);
256 * gst_rtsp_session_media_get_transport:
257 * @media: a #GstRTSPSessionMedia
258 * @idx: the stream index
260 * Get a previously created #GstRTSPStreamTransport for the stream at @idx.
262 * Returns: (transfer none): a #GstRTSPStreamTransport that is valid until the
263 * session of @media is unreffed.
265 GstRTSPStreamTransport *
266 gst_rtsp_session_media_get_transport (GstRTSPSessionMedia * media, guint idx)
268 GstRTSPSessionMediaPrivate *priv;
269 GstRTSPStreamTransport *result;
271 g_return_val_if_fail (GST_IS_RTSP_SESSION_MEDIA (media), NULL);
273 g_return_val_if_fail (idx < priv->transports->len, NULL);
275 g_mutex_lock (&priv->lock);
276 result = g_ptr_array_index (priv->transports, idx);
277 g_mutex_unlock (&priv->lock);
283 * gst_rtsp_session_media_alloc_channels:
284 * @media: a #GstRTSPSessionMedia
285 * @range: a #GstRTSPRange
287 * Fill @range with the next available min and max channels for
288 * interleaved transport.
290 * Returns: %TRUE on success.
293 gst_rtsp_session_media_alloc_channels (GstRTSPSessionMedia * media,
294 GstRTSPRange * range)
296 GstRTSPSessionMediaPrivate *priv;
298 g_return_val_if_fail (GST_IS_RTSP_SESSION_MEDIA (media), FALSE);
302 g_mutex_lock (&priv->lock);
303 range->min = priv->counter++;
304 range->max = priv->counter++;
305 g_mutex_unlock (&priv->lock);
311 * gst_rtsp_session_media_set_state:
312 * @media: a #GstRTSPSessionMedia
313 * @state: the new state
315 * Tell the media object @media to change to @state.
317 * Returns: %TRUE on success.
320 gst_rtsp_session_media_set_state (GstRTSPSessionMedia * media, GstState state)
322 GstRTSPSessionMediaPrivate *priv;
325 g_return_val_if_fail (GST_IS_RTSP_SESSION_MEDIA (media), FALSE);
329 g_mutex_lock (&priv->lock);
330 ret = gst_rtsp_media_set_state (priv->media, state, priv->transports);
331 g_mutex_unlock (&priv->lock);
337 * gst_rtsp_session_media_set_rtsp_state:
338 * @media: a #GstRTSPSessionMedia
339 * @state: a #GstRTSPState
341 * Set the RTSP state of @media to @state.
344 gst_rtsp_session_media_set_rtsp_state (GstRTSPSessionMedia * media,
347 GstRTSPSessionMediaPrivate *priv;
349 g_return_if_fail (GST_IS_RTSP_SESSION_MEDIA (media));
353 g_mutex_lock (&priv->lock);
355 g_mutex_unlock (&priv->lock);
359 * gst_rtsp_session_media_get_rtsp_state:
360 * @media: a #GstRTSPSessionMedia
362 * Get the current RTSP state of @media.
364 * Returns: the current RTSP state of @media.
367 gst_rtsp_session_media_get_rtsp_state (GstRTSPSessionMedia * media)
369 GstRTSPSessionMediaPrivate *priv;
372 g_return_val_if_fail (GST_IS_RTSP_SESSION_MEDIA (media),
373 GST_RTSP_STATE_INVALID);
377 g_mutex_lock (&priv->lock);
379 g_mutex_unlock (&priv->lock);