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.
20 * SECTION:rtsp-session-media
21 * @short_description: Media managed in a session
22 * @see_also: #GstRTSPMedia, #GstRTSPSession
24 * The #GstRTSPSessionMedia object manages a #GstRTSPMedia with a given path.
26 * With gst_rtsp_session_media_get_transport() and
27 * gst_rtsp_session_media_set_transport() the transports of a #GstRTSPStream of
28 * the managed #GstRTSPMedia can be retrieved and configured.
30 * Use gst_rtsp_session_media_set_state() to control the media state and
33 * Last reviewed on 2013-07-16 (1.0.0)
38 #include "rtsp-session.h"
40 #define GST_RTSP_SESSION_MEDIA_GET_PRIVATE(obj) \
41 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_SESSION_MEDIA, GstRTSPSessionMediaPrivate))
43 struct _GstRTSPSessionMediaPrivate
46 gchar *path; /* unmutable */
47 gint path_len; /* unmutable */
48 GstRTSPMedia *media; /* unmutable */
49 GstRTSPState state; /* protected by lock */
50 guint counter; /* protected by lock */
52 GPtrArray *transports; /* protected by lock */
61 GST_DEBUG_CATEGORY_STATIC (rtsp_session_media_debug);
62 #define GST_CAT_DEFAULT rtsp_session_media_debug
64 static void gst_rtsp_session_media_finalize (GObject * obj);
66 G_DEFINE_TYPE (GstRTSPSessionMedia, gst_rtsp_session_media, G_TYPE_OBJECT);
69 gst_rtsp_session_media_class_init (GstRTSPSessionMediaClass * klass)
71 GObjectClass *gobject_class;
73 g_type_class_add_private (klass, sizeof (GstRTSPSessionMediaPrivate));
75 gobject_class = G_OBJECT_CLASS (klass);
77 gobject_class->finalize = gst_rtsp_session_media_finalize;
79 GST_DEBUG_CATEGORY_INIT (rtsp_session_media_debug, "rtspsessionmedia", 0,
80 "GstRTSPSessionMedia");
84 gst_rtsp_session_media_init (GstRTSPSessionMedia * media)
86 GstRTSPSessionMediaPrivate *priv = GST_RTSP_SESSION_MEDIA_GET_PRIVATE (media);
90 g_mutex_init (&priv->lock);
91 priv->state = GST_RTSP_STATE_INIT;
95 gst_rtsp_session_media_finalize (GObject * obj)
97 GstRTSPSessionMedia *media;
98 GstRTSPSessionMediaPrivate *priv;
100 media = GST_RTSP_SESSION_MEDIA (obj);
103 GST_INFO ("free session media %p", media);
105 gst_rtsp_session_media_set_state (media, GST_STATE_NULL);
107 g_ptr_array_unref (priv->transports);
110 g_object_unref (priv->media);
111 g_mutex_clear (&priv->lock);
113 G_OBJECT_CLASS (gst_rtsp_session_media_parent_class)->finalize (obj);
117 free_session_media (gpointer data)
120 g_object_unref (data);
124 * gst_rtsp_session_media_new:
126 * @media: the #GstRTSPMedia
128 * Create a new #GstRTSPSessionMedia that manages the streams
129 * in @media for @path. @media should be prepared.
131 * Ownership is taken of @media.
133 * Returns: a new #GstRTSPSessionMedia.
135 GstRTSPSessionMedia *
136 gst_rtsp_session_media_new (const gchar * path, GstRTSPMedia * media)
138 GstRTSPSessionMediaPrivate *priv;
139 GstRTSPSessionMedia *result;
142 g_return_val_if_fail (path != NULL, NULL);
143 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
144 g_return_val_if_fail (gst_rtsp_media_get_status (media) ==
145 GST_RTSP_MEDIA_STATUS_PREPARED, NULL);
147 result = g_object_new (GST_TYPE_RTSP_SESSION_MEDIA, NULL);
150 priv->path = g_strdup (path);
151 priv->path_len = strlen (path);
154 /* prealloc the streams now, filled with NULL */
155 n_streams = gst_rtsp_media_n_streams (media);
156 priv->transports = g_ptr_array_new_full (n_streams, free_session_media);
157 g_ptr_array_set_size (priv->transports, n_streams);
163 * gst_rtsp_session_media_matches:
164 * @media: a #GstRTSPSessionMedia
166 * @matched: (out): the amount of matched characters of @path
168 * Check if the path of @media matches @path. It @path matches, the amount of
169 * matched characters is returned in @matched.
171 * Returns: %TRUE when @path matches the path of @media.
174 gst_rtsp_session_media_matches (GstRTSPSessionMedia * media,
175 const gchar * path, gint * matched)
177 GstRTSPSessionMediaPrivate *priv;
180 g_return_val_if_fail (GST_IS_RTSP_SESSION_MEDIA (media), FALSE);
181 g_return_val_if_fail (path != NULL, FALSE);
182 g_return_val_if_fail (matched != NULL, FALSE);
187 /* path needs to be smaller than the media path */
188 if (len < priv->path_len)
191 /* if media path is larger, it there should be a / following the path */
192 if (len > priv->path_len && path[priv->path_len] != '/')
195 *matched = priv->path_len;
197 return strncmp (path, priv->path, priv->path_len) == 0;
201 * gst_rtsp_session_media_get_media:
202 * @media: a #GstRTSPSessionMedia
204 * Get the #GstRTSPMedia that was used when constructing @media
206 * Returns: (transfer none): the #GstRTSPMedia of @media. Remains valid as long
207 * as @media is valid.
210 gst_rtsp_session_media_get_media (GstRTSPSessionMedia * media)
212 g_return_val_if_fail (GST_IS_RTSP_SESSION_MEDIA (media), NULL);
214 return media->priv->media;
218 * gst_rtsp_session_media_get_base_time:
219 * @media: a #GstRTSPSessionMedia
221 * Get the base_time of the #GstRTSPMedia in @media
223 * Returns: the base_time of the media.
226 gst_rtsp_session_media_get_base_time (GstRTSPSessionMedia * media)
228 g_return_val_if_fail (GST_IS_RTSP_SESSION_MEDIA (media), GST_CLOCK_TIME_NONE);
230 return gst_rtsp_media_get_base_time (media->priv->media);
234 * gst_rtsp_session_media_set_transport:
235 * @media: a #GstRTSPSessionMedia
236 * @stream: a #GstRTSPStream
237 * @tr: a #GstRTSPTransport
239 * Configure the transport for @stream to @tr in @media.
241 * Returns: (transfer none): the new or updated #GstRTSPStreamTransport for @stream.
243 GstRTSPStreamTransport *
244 gst_rtsp_session_media_set_transport (GstRTSPSessionMedia * media,
245 GstRTSPStream * stream, GstRTSPTransport * tr)
247 GstRTSPSessionMediaPrivate *priv;
248 GstRTSPStreamTransport *result;
251 g_return_val_if_fail (GST_IS_RTSP_SESSION_MEDIA (media), NULL);
252 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
253 g_return_val_if_fail (tr != NULL, NULL);
255 idx = gst_rtsp_stream_get_index (stream);
256 g_return_val_if_fail (idx < priv->transports->len, NULL);
258 g_mutex_lock (&priv->lock);
259 result = g_ptr_array_index (priv->transports, idx);
260 if (result == NULL) {
261 result = gst_rtsp_stream_transport_new (stream, tr);
262 g_ptr_array_index (priv->transports, idx) = result;
263 g_mutex_unlock (&priv->lock);
265 gst_rtsp_stream_transport_set_transport (result, tr);
266 g_mutex_unlock (&priv->lock);
273 * gst_rtsp_session_media_get_transport:
274 * @media: a #GstRTSPSessionMedia
275 * @idx: the stream index
277 * Get a previously created #GstRTSPStreamTransport for the stream at @idx.
279 * Returns: (transfer none): a #GstRTSPStreamTransport that is valid until the
280 * session of @media is unreffed.
282 GstRTSPStreamTransport *
283 gst_rtsp_session_media_get_transport (GstRTSPSessionMedia * media, guint idx)
285 GstRTSPSessionMediaPrivate *priv;
286 GstRTSPStreamTransport *result;
288 g_return_val_if_fail (GST_IS_RTSP_SESSION_MEDIA (media), NULL);
290 g_return_val_if_fail (idx < priv->transports->len, NULL);
292 g_mutex_lock (&priv->lock);
293 result = g_ptr_array_index (priv->transports, idx);
294 g_mutex_unlock (&priv->lock);
300 * gst_rtsp_session_media_alloc_channels:
301 * @media: a #GstRTSPSessionMedia
302 * @range: a #GstRTSPRange
304 * Fill @range with the next available min and max channels for
305 * interleaved transport.
307 * Returns: %TRUE on success.
310 gst_rtsp_session_media_alloc_channels (GstRTSPSessionMedia * media,
311 GstRTSPRange * range)
313 GstRTSPSessionMediaPrivate *priv;
315 g_return_val_if_fail (GST_IS_RTSP_SESSION_MEDIA (media), FALSE);
319 g_mutex_lock (&priv->lock);
320 range->min = priv->counter++;
321 range->max = priv->counter++;
322 g_mutex_unlock (&priv->lock);
328 * gst_rtsp_session_media_set_state:
329 * @media: a #GstRTSPSessionMedia
330 * @state: the new state
332 * Tell the media object @media to change to @state.
334 * Returns: %TRUE on success.
337 gst_rtsp_session_media_set_state (GstRTSPSessionMedia * media, GstState state)
339 GstRTSPSessionMediaPrivate *priv;
342 g_return_val_if_fail (GST_IS_RTSP_SESSION_MEDIA (media), FALSE);
346 g_mutex_lock (&priv->lock);
347 ret = gst_rtsp_media_set_state (priv->media, state, priv->transports);
348 g_mutex_unlock (&priv->lock);
354 * gst_rtsp_session_media_set_rtsp_state:
355 * @media: a #GstRTSPSessionMedia
356 * @state: a #GstRTSPState
358 * Set the RTSP state of @media to @state.
361 gst_rtsp_session_media_set_rtsp_state (GstRTSPSessionMedia * media,
364 GstRTSPSessionMediaPrivate *priv;
366 g_return_if_fail (GST_IS_RTSP_SESSION_MEDIA (media));
370 g_mutex_lock (&priv->lock);
372 g_mutex_unlock (&priv->lock);
376 * gst_rtsp_session_media_get_rtsp_state:
377 * @media: a #GstRTSPSessionMedia
379 * Get the current RTSP state of @media.
381 * Returns: the current RTSP state of @media.
384 gst_rtsp_session_media_get_rtsp_state (GstRTSPSessionMedia * media)
386 GstRTSPSessionMediaPrivate *priv;
389 g_return_val_if_fail (GST_IS_RTSP_SESSION_MEDIA (media),
390 GST_RTSP_STATE_INVALID);
394 g_mutex_lock (&priv->lock);
396 g_mutex_unlock (&priv->lock);