Initial release including wifi display based on gst-rtsp-server-1.4.1
[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: (transfer full): 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: (transfer full): 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) (nullable): The RTP-Info as a string or
243  * %NULL when no RTP-Info could be generated, g_free() 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       GST_DEBUG_OBJECT (media, "ignoring unknown RTPInfo %d", i);
311       continue;
312     }
313
314     if (rtpinfo == NULL)
315       rtpinfo = g_string_new ("");
316     else
317       g_string_append (rtpinfo, ", ");
318
319     g_string_append (rtpinfo, stream_rtpinfo);
320     g_free (stream_rtpinfo);
321   }
322   g_mutex_unlock (&priv->lock);
323
324   if (rtpinfo == NULL) {
325     GST_WARNING_OBJECT (media, "RTP info is empty");
326     return NULL;
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 }
338
339 /**
340  * gst_rtsp_session_media_set_transport:
341  * @media: a #GstRTSPSessionMedia
342  * @stream: a #GstRTSPStream
343  * @tr: (transfer full): a #GstRTSPTransport
344  *
345  * Configure the transport for @stream to @tr in @media.
346  *
347  * Returns: (transfer none): the new or updated #GstRTSPStreamTransport for @stream.
348  */
349 GstRTSPStreamTransport *
350 gst_rtsp_session_media_set_transport (GstRTSPSessionMedia * media,
351     GstRTSPStream * stream, GstRTSPTransport * tr)
352 {
353   GstRTSPSessionMediaPrivate *priv;
354   GstRTSPStreamTransport *result;
355   guint idx;
356
357   g_return_val_if_fail (GST_IS_RTSP_SESSION_MEDIA (media), NULL);
358   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
359   g_return_val_if_fail (tr != NULL, NULL);
360   priv = media->priv;
361   idx = gst_rtsp_stream_get_index (stream);
362   g_return_val_if_fail (idx < priv->transports->len, NULL);
363
364   g_mutex_lock (&priv->lock);
365   result = g_ptr_array_index (priv->transports, idx);
366   if (result == NULL) {
367     result = gst_rtsp_stream_transport_new (stream, tr);
368     g_ptr_array_index (priv->transports, idx) = result;
369     g_mutex_unlock (&priv->lock);
370   } else {
371     gst_rtsp_stream_transport_set_transport (result, tr);
372     g_mutex_unlock (&priv->lock);
373   }
374
375   return result;
376 }
377
378 /**
379  * gst_rtsp_session_media_get_transport:
380  * @media: a #GstRTSPSessionMedia
381  * @idx: the stream index
382  *
383  * Get a previously created #GstRTSPStreamTransport for the stream at @idx.
384  *
385  * Returns: (transfer none): a #GstRTSPStreamTransport that is valid until the
386  * session of @media is unreffed.
387  */
388 GstRTSPStreamTransport *
389 gst_rtsp_session_media_get_transport (GstRTSPSessionMedia * media, guint idx)
390 {
391   GstRTSPSessionMediaPrivate *priv;
392   GstRTSPStreamTransport *result;
393
394   g_return_val_if_fail (GST_IS_RTSP_SESSION_MEDIA (media), NULL);
395   priv = media->priv;
396   g_return_val_if_fail (idx < priv->transports->len, NULL);
397
398   g_mutex_lock (&priv->lock);
399   result = g_ptr_array_index (priv->transports, idx);
400   g_mutex_unlock (&priv->lock);
401
402   return result;
403 }
404
405 /**
406  * gst_rtsp_session_media_alloc_channels:
407  * @media: a #GstRTSPSessionMedia
408  * @range: (out): a #GstRTSPRange
409  *
410  * Fill @range with the next available min and max channels for
411  * interleaved transport.
412  *
413  * Returns: %TRUE on success.
414  */
415 gboolean
416 gst_rtsp_session_media_alloc_channels (GstRTSPSessionMedia * media,
417     GstRTSPRange * range)
418 {
419   GstRTSPSessionMediaPrivate *priv;
420
421   g_return_val_if_fail (GST_IS_RTSP_SESSION_MEDIA (media), FALSE);
422
423   priv = media->priv;
424
425   g_mutex_lock (&priv->lock);
426   range->min = priv->counter++;
427   range->max = priv->counter++;
428   g_mutex_unlock (&priv->lock);
429
430   return TRUE;
431 }
432
433 /**
434  * gst_rtsp_session_media_set_state:
435  * @media: a #GstRTSPSessionMedia
436  * @state: the new state
437  *
438  * Tell the media object @media to change to @state.
439  *
440  * Returns: %TRUE on success.
441  */
442 gboolean
443 gst_rtsp_session_media_set_state (GstRTSPSessionMedia * media, GstState state)
444 {
445   GstRTSPSessionMediaPrivate *priv;
446   gboolean ret;
447
448   g_return_val_if_fail (GST_IS_RTSP_SESSION_MEDIA (media), FALSE);
449
450   priv = media->priv;
451
452   g_mutex_lock (&priv->lock);
453   ret = gst_rtsp_media_set_state (priv->media, state, priv->transports);
454   g_mutex_unlock (&priv->lock);
455
456   return ret;
457 }
458
459 /**
460  * gst_rtsp_session_media_set_rtsp_state:
461  * @media: a #GstRTSPSessionMedia
462  * @state: a #GstRTSPState
463  *
464  * Set the RTSP state of @media to @state.
465  */
466 void
467 gst_rtsp_session_media_set_rtsp_state (GstRTSPSessionMedia * media,
468     GstRTSPState state)
469 {
470   GstRTSPSessionMediaPrivate *priv;
471
472   g_return_if_fail (GST_IS_RTSP_SESSION_MEDIA (media));
473
474   priv = media->priv;
475
476   g_mutex_lock (&priv->lock);
477   priv->state = state;
478   g_mutex_unlock (&priv->lock);
479 }
480
481 /**
482  * gst_rtsp_session_media_get_rtsp_state:
483  * @media: a #GstRTSPSessionMedia
484  *
485  * Get the current RTSP state of @media.
486  *
487  * Returns: the current RTSP state of @media.
488  */
489 GstRTSPState
490 gst_rtsp_session_media_get_rtsp_state (GstRTSPSessionMedia * media)
491 {
492   GstRTSPSessionMediaPrivate *priv;
493   GstRTSPState ret;
494
495   g_return_val_if_fail (GST_IS_RTSP_SESSION_MEDIA (media),
496       GST_RTSP_STATE_INVALID);
497
498   priv = media->priv;
499
500   g_mutex_lock (&priv->lock);
501   ret = priv->state;
502   g_mutex_unlock (&priv->lock);
503
504   return ret;
505 }