9cee47700ac3bafbcba26ada7458a77a1a7cbde6
[platform/upstream/gstreamer.git] / gst / rtsp-server / rtsp-session-media.c
1 /* GStreamer
2  * Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
3  *
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.
8  *
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.
13  *
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.
18  */
19 /**
20  * SECTION:rtsp-session-media
21  * @short_description: Media managed in a session
22  * @see_also: #GstRTSPMedia, #GstRTSPSession
23  *
24  * The #GstRTSPSessionMedia object manages a #GstRTSPMedia with a given path.
25  *
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.
29  *
30  * Use gst_rtsp_session_media_set_state() to control the media state and
31  * transports.
32  *
33  * Last reviewed on 2013-07-16 (1.0.0)
34  */
35
36 #include <string.h>
37
38 #include "rtsp-session.h"
39
40 #define GST_RTSP_SESSION_MEDIA_GET_PRIVATE(obj)  \
41     (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_SESSION_MEDIA, GstRTSPSessionMediaPrivate))
42
43 struct _GstRTSPSessionMediaPrivate
44 {
45   GMutex lock;
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 */
51
52   GPtrArray *transports;        /* protected by lock */
53 };
54
55 enum
56 {
57   PROP_0,
58   PROP_LAST
59 };
60
61 GST_DEBUG_CATEGORY_STATIC (rtsp_session_media_debug);
62 #define GST_CAT_DEFAULT rtsp_session_media_debug
63
64 static void gst_rtsp_session_media_finalize (GObject * obj);
65
66 G_DEFINE_TYPE (GstRTSPSessionMedia, gst_rtsp_session_media, G_TYPE_OBJECT);
67
68 static void
69 gst_rtsp_session_media_class_init (GstRTSPSessionMediaClass * klass)
70 {
71   GObjectClass *gobject_class;
72
73   g_type_class_add_private (klass, sizeof (GstRTSPSessionMediaPrivate));
74
75   gobject_class = G_OBJECT_CLASS (klass);
76
77   gobject_class->finalize = gst_rtsp_session_media_finalize;
78
79   GST_DEBUG_CATEGORY_INIT (rtsp_session_media_debug, "rtspsessionmedia", 0,
80       "GstRTSPSessionMedia");
81 }
82
83 static void
84 gst_rtsp_session_media_init (GstRTSPSessionMedia * media)
85 {
86   GstRTSPSessionMediaPrivate *priv = GST_RTSP_SESSION_MEDIA_GET_PRIVATE (media);
87
88   media->priv = priv;
89
90   g_mutex_init (&priv->lock);
91   priv->state = GST_RTSP_STATE_INIT;
92 }
93
94 static void
95 gst_rtsp_session_media_finalize (GObject * obj)
96 {
97   GstRTSPSessionMedia *media;
98   GstRTSPSessionMediaPrivate *priv;
99
100   media = GST_RTSP_SESSION_MEDIA (obj);
101   priv = media->priv;
102
103   GST_INFO ("free session media %p", media);
104
105   gst_rtsp_session_media_set_state (media, GST_STATE_NULL);
106
107   g_ptr_array_unref (priv->transports);
108
109   g_free (priv->path);
110   g_object_unref (priv->media);
111   g_mutex_clear (&priv->lock);
112
113   G_OBJECT_CLASS (gst_rtsp_session_media_parent_class)->finalize (obj);
114 }
115
116 static void
117 free_session_media (gpointer data)
118 {
119   if (data)
120     g_object_unref (data);
121 }
122
123 /**
124  * gst_rtsp_session_media_new:
125  * @path: the path
126  * @media: the #GstRTSPMedia
127  *
128  * Create a new #GstRTSPSessionMedia that manages the streams
129  * in @media for @path. @media should be prepared.
130  *
131  * Ownership is taken of @media.
132  *
133  * Returns: a new #GstRTSPSessionMedia.
134  */
135 GstRTSPSessionMedia *
136 gst_rtsp_session_media_new (const gchar * path, GstRTSPMedia * media)
137 {
138   GstRTSPSessionMediaPrivate *priv;
139   GstRTSPSessionMedia *result;
140   guint n_streams;
141   GstRTSPMediaStatus status;
142
143   g_return_val_if_fail (path != NULL, NULL);
144   g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
145   status = gst_rtsp_media_get_status (media);
146   g_return_val_if_fail (status == GST_RTSP_MEDIA_STATUS_PREPARED || status ==
147       GST_RTSP_MEDIA_STATUS_SUSPENDED, NULL);
148
149   result = g_object_new (GST_TYPE_RTSP_SESSION_MEDIA, NULL);
150   priv = result->priv;
151
152   priv->path = g_strdup (path);
153   priv->path_len = strlen (path);
154   priv->media = media;
155
156   /* prealloc the streams now, filled with NULL */
157   n_streams = gst_rtsp_media_n_streams (media);
158   priv->transports = g_ptr_array_new_full (n_streams, free_session_media);
159   g_ptr_array_set_size (priv->transports, n_streams);
160
161   return result;
162 }
163
164 /**
165  * gst_rtsp_session_media_matches:
166  * @media: a #GstRTSPSessionMedia
167  * @path: a path
168  * @matched: (out): the amount of matched characters of @path
169  *
170  * Check if the path of @media matches @path. It @path matches, the amount of
171  * matched characters is returned in @matched.
172  *
173  * Returns: %TRUE when @path matches the path of @media.
174  */
175 gboolean
176 gst_rtsp_session_media_matches (GstRTSPSessionMedia * media,
177     const gchar * path, gint * matched)
178 {
179   GstRTSPSessionMediaPrivate *priv;
180   gint len;
181
182   g_return_val_if_fail (GST_IS_RTSP_SESSION_MEDIA (media), FALSE);
183   g_return_val_if_fail (path != NULL, FALSE);
184   g_return_val_if_fail (matched != NULL, FALSE);
185
186   priv = media->priv;
187   len = strlen (path);
188
189   /* path needs to be smaller than the media path */
190   if (len < priv->path_len)
191     return FALSE;
192
193   /* if media path is larger, it there should be a / following the path */
194   if (len > priv->path_len && path[priv->path_len] != '/')
195     return FALSE;
196
197   *matched = priv->path_len;
198
199   return strncmp (path, priv->path, priv->path_len) == 0;
200 }
201
202 /**
203  * gst_rtsp_session_media_get_media:
204  * @media: a #GstRTSPSessionMedia
205  *
206  * Get the #GstRTSPMedia that was used when constructing @media
207  *
208  * Returns: (transfer none): the #GstRTSPMedia of @media. Remains valid as long
209  *         as @media is valid.
210  */
211 GstRTSPMedia *
212 gst_rtsp_session_media_get_media (GstRTSPSessionMedia * media)
213 {
214   g_return_val_if_fail (GST_IS_RTSP_SESSION_MEDIA (media), NULL);
215
216   return media->priv->media;
217 }
218
219 /**
220  * gst_rtsp_session_media_get_base_time:
221  * @media: a #GstRTSPSessionMedia
222  *
223  * Get the base_time of the #GstRTSPMedia in @media
224  *
225  * Returns: the base_time of the media.
226  */
227 GstClockTime
228 gst_rtsp_session_media_get_base_time (GstRTSPSessionMedia * media)
229 {
230   g_return_val_if_fail (GST_IS_RTSP_SESSION_MEDIA (media), GST_CLOCK_TIME_NONE);
231
232   return gst_rtsp_media_get_base_time (media->priv->media);
233 }
234
235 /**
236  * gst_rtsp_session_media_get_rtpinfo:
237  * @media: a #GstRTSPSessionMedia
238  *
239  * Retrieve the RTP-Info header string for all streams in @media
240  * with configured transports.
241  *
242  * Returns: (transfer full): The RTP-Info as a string, g_free()
243  *   after usage.
244  */
245 gchar *
246 gst_rtsp_session_media_get_rtpinfo (GstRTSPSessionMedia * media)
247 {
248   GstRTSPSessionMediaPrivate *priv;
249   GString *rtpinfo = NULL;
250   GstRTSPStreamTransport *transport;
251   GstRTSPStream *stream;
252   guint i, n_streams;
253   GstClockTime earliest = GST_CLOCK_TIME_NONE;
254
255   g_return_val_if_fail (GST_IS_RTSP_SESSION_MEDIA (media), NULL);
256
257   priv = media->priv;
258   g_mutex_lock (&priv->lock);
259
260   if (gst_rtsp_media_get_status (priv->media) != GST_RTSP_MEDIA_STATUS_PREPARED)
261     goto not_prepared;
262
263   n_streams = priv->transports->len;
264
265   /* first step, take lowest running-time from all streams */
266   GST_LOG_OBJECT (media, "determining start time among %d transports",
267       n_streams);
268
269   for (i = 0; i < n_streams; i++) {
270     GstClockTime running_time;
271
272     transport = g_ptr_array_index (priv->transports, i);
273     if (transport == NULL) {
274       GST_DEBUG_OBJECT (media, "ignoring unconfigured transport %d", i);
275       continue;
276     }
277
278     stream = gst_rtsp_stream_transport_get_stream (transport);
279     if (!gst_rtsp_stream_get_rtpinfo (stream, NULL, NULL, NULL, &running_time))
280       continue;
281
282     GST_LOG_OBJECT (media, "running time of %d stream: %" GST_TIME_FORMAT, i,
283         GST_TIME_ARGS (running_time));
284
285     if (!GST_CLOCK_TIME_IS_VALID (earliest)) {
286       earliest = running_time;
287     } else {
288       earliest = MIN (earliest, running_time);
289     }
290   }
291
292   GST_LOG_OBJECT (media, "media start time: %" GST_TIME_FORMAT,
293       GST_TIME_ARGS (earliest));
294
295   /* next step, scale all rtptime of all streams to lowest running-time */
296   GST_LOG_OBJECT (media, "collecting RTP info for %d transports", n_streams);
297
298   for (i = 0; i < n_streams; i++) {
299     gchar *stream_rtpinfo;
300
301     transport = g_ptr_array_index (priv->transports, i);
302     if (transport == NULL) {
303       GST_DEBUG_OBJECT (media, "ignoring unconfigured transport %d", i);
304       continue;
305     }
306
307     stream_rtpinfo =
308         gst_rtsp_stream_transport_get_rtpinfo (transport, earliest);
309     if (stream_rtpinfo == NULL)
310       goto stream_rtpinfo_missing;
311
312     if (rtpinfo == NULL)
313       rtpinfo = g_string_new ("");
314     else
315       g_string_append (rtpinfo, ", ");
316
317     g_string_append (rtpinfo, stream_rtpinfo);
318     g_free (stream_rtpinfo);
319   }
320
321   if (rtpinfo == NULL) {
322     GST_INFO_OBJECT (media, "no transports configured, RTP info is empty");
323     rtpinfo = g_string_new ("");
324   }
325
326   g_mutex_unlock (&priv->lock);
327
328   return g_string_free (rtpinfo, FALSE);
329
330   /* ERRORS */
331 not_prepared:
332   {
333     g_mutex_unlock (&priv->lock);
334     GST_ERROR_OBJECT (media, "media was not prepared");
335     return NULL;
336   }
337 stream_rtpinfo_missing:
338   {
339     g_mutex_unlock (&priv->lock);
340     if (rtpinfo)
341       g_string_free (rtpinfo, TRUE);
342     GST_ERROR_OBJECT (media, "could not get stream %d rtpinfo", i);
343     return NULL;
344   }
345 }
346
347 /**
348  * gst_rtsp_session_media_set_transport:
349  * @media: a #GstRTSPSessionMedia
350  * @stream: a #GstRTSPStream
351  * @tr: a #GstRTSPTransport
352  *
353  * Configure the transport for @stream to @tr in @media.
354  *
355  * Returns: (transfer none): the new or updated #GstRTSPStreamTransport for @stream.
356  */
357 GstRTSPStreamTransport *
358 gst_rtsp_session_media_set_transport (GstRTSPSessionMedia * media,
359     GstRTSPStream * stream, GstRTSPTransport * tr)
360 {
361   GstRTSPSessionMediaPrivate *priv;
362   GstRTSPStreamTransport *result;
363   guint idx;
364
365   g_return_val_if_fail (GST_IS_RTSP_SESSION_MEDIA (media), NULL);
366   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
367   g_return_val_if_fail (tr != NULL, NULL);
368   priv = media->priv;
369   idx = gst_rtsp_stream_get_index (stream);
370   g_return_val_if_fail (idx < priv->transports->len, NULL);
371
372   g_mutex_lock (&priv->lock);
373   result = g_ptr_array_index (priv->transports, idx);
374   if (result == NULL) {
375     result = gst_rtsp_stream_transport_new (stream, tr);
376     g_ptr_array_index (priv->transports, idx) = result;
377     g_mutex_unlock (&priv->lock);
378   } else {
379     gst_rtsp_stream_transport_set_transport (result, tr);
380     g_mutex_unlock (&priv->lock);
381   }
382
383   return result;
384 }
385
386 /**
387  * gst_rtsp_session_media_get_transport:
388  * @media: a #GstRTSPSessionMedia
389  * @idx: the stream index
390  *
391  * Get a previously created #GstRTSPStreamTransport for the stream at @idx.
392  *
393  * Returns: (transfer none): a #GstRTSPStreamTransport that is valid until the
394  * session of @media is unreffed.
395  */
396 GstRTSPStreamTransport *
397 gst_rtsp_session_media_get_transport (GstRTSPSessionMedia * media, guint idx)
398 {
399   GstRTSPSessionMediaPrivate *priv;
400   GstRTSPStreamTransport *result;
401
402   g_return_val_if_fail (GST_IS_RTSP_SESSION_MEDIA (media), NULL);
403   priv = media->priv;
404   g_return_val_if_fail (idx < priv->transports->len, NULL);
405
406   g_mutex_lock (&priv->lock);
407   result = g_ptr_array_index (priv->transports, idx);
408   g_mutex_unlock (&priv->lock);
409
410   return result;
411 }
412
413 /**
414  * gst_rtsp_session_media_alloc_channels:
415  * @media: a #GstRTSPSessionMedia
416  * @range: a #GstRTSPRange
417  *
418  * Fill @range with the next available min and max channels for
419  * interleaved transport.
420  *
421  * Returns: %TRUE on success.
422  */
423 gboolean
424 gst_rtsp_session_media_alloc_channels (GstRTSPSessionMedia * media,
425     GstRTSPRange * range)
426 {
427   GstRTSPSessionMediaPrivate *priv;
428
429   g_return_val_if_fail (GST_IS_RTSP_SESSION_MEDIA (media), FALSE);
430
431   priv = media->priv;
432
433   g_mutex_lock (&priv->lock);
434   range->min = priv->counter++;
435   range->max = priv->counter++;
436   g_mutex_unlock (&priv->lock);
437
438   return TRUE;
439 }
440
441 /**
442  * gst_rtsp_session_media_set_state:
443  * @media: a #GstRTSPSessionMedia
444  * @state: the new state
445  *
446  * Tell the media object @media to change to @state.
447  *
448  * Returns: %TRUE on success.
449  */
450 gboolean
451 gst_rtsp_session_media_set_state (GstRTSPSessionMedia * media, GstState state)
452 {
453   GstRTSPSessionMediaPrivate *priv;
454   gboolean ret;
455
456   g_return_val_if_fail (GST_IS_RTSP_SESSION_MEDIA (media), FALSE);
457
458   priv = media->priv;
459
460   g_mutex_lock (&priv->lock);
461   ret = gst_rtsp_media_set_state (priv->media, state, priv->transports);
462   g_mutex_unlock (&priv->lock);
463
464   return ret;
465 }
466
467 /**
468  * gst_rtsp_session_media_set_rtsp_state:
469  * @media: a #GstRTSPSessionMedia
470  * @state: a #GstRTSPState
471  *
472  * Set the RTSP state of @media to @state.
473  */
474 void
475 gst_rtsp_session_media_set_rtsp_state (GstRTSPSessionMedia * media,
476     GstRTSPState state)
477 {
478   GstRTSPSessionMediaPrivate *priv;
479
480   g_return_if_fail (GST_IS_RTSP_SESSION_MEDIA (media));
481
482   priv = media->priv;
483
484   g_mutex_lock (&priv->lock);
485   priv->state = state;
486   g_mutex_unlock (&priv->lock);
487 }
488
489 /**
490  * gst_rtsp_session_media_get_rtsp_state:
491  * @media: a #GstRTSPSessionMedia
492  *
493  * Get the current RTSP state of @media.
494  *
495  * Returns: the current RTSP state of @media.
496  */
497 GstRTSPState
498 gst_rtsp_session_media_get_rtsp_state (GstRTSPSessionMedia * media)
499 {
500   GstRTSPSessionMediaPrivate *priv;
501   GstRTSPState ret;
502
503   g_return_val_if_fail (GST_IS_RTSP_SESSION_MEDIA (media),
504       GST_RTSP_STATE_INVALID);
505
506   priv = media->priv;
507
508   g_mutex_lock (&priv->lock);
509   ret = priv->state;
510   g_mutex_unlock (&priv->lock);
511
512   return ret;
513 }