rtsp: massive refactoring
[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., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19 #include <string.h>
20
21 #include "rtsp-session.h"
22
23 #undef DEBUG
24
25 #define DEFAULT_TIMEOUT 60
26
27 enum
28 {
29   PROP_0,
30   PROP_LAST
31 };
32
33 GST_DEBUG_CATEGORY_STATIC (rtsp_session_media_debug);
34 #define GST_CAT_DEFAULT rtsp_session_media_debug
35
36 static void gst_rtsp_session_media_finalize (GObject * obj);
37
38 G_DEFINE_TYPE (GstRTSPSessionMedia, gst_rtsp_session_media, G_TYPE_OBJECT);
39
40 static void
41 gst_rtsp_session_media_class_init (GstRTSPSessionMediaClass * klass)
42 {
43   GObjectClass *gobject_class;
44
45   gobject_class = G_OBJECT_CLASS (klass);
46
47   gobject_class->finalize = gst_rtsp_session_media_finalize;
48
49   GST_DEBUG_CATEGORY_INIT (rtsp_session_media_debug, "rtspsessionmedia", 0,
50       "GstRTSPSessionMedia");
51 }
52
53 static void
54 gst_rtsp_session_media_init (GstRTSPSessionMedia * media)
55 {
56   media->state = GST_RTSP_STATE_INIT;
57 }
58
59 static void
60 gst_rtsp_session_media_finalize (GObject * obj)
61 {
62   GstRTSPSessionMedia *media;
63
64   media = GST_RTSP_SESSION_MEDIA (obj);
65
66   GST_INFO ("free session media %p", media);
67
68   gst_rtsp_session_media_set_state (media, GST_STATE_NULL);
69
70   g_ptr_array_unref (media->transports);
71
72   gst_rtsp_url_free (media->url);
73   g_object_unref (media->media);
74
75   G_OBJECT_CLASS (gst_rtsp_session_media_parent_class)->finalize (obj);
76 }
77
78 static void
79 free_session_media (gpointer data)
80 {
81   if (data)
82     g_object_unref (data);
83 }
84
85 /**
86  * gst_rtsp_session_media_new:
87  * @url: the #GstRTSPUrl
88  * @media: the #GstRTSPMedia
89  *
90  * Create a new #GstRTPSessionMedia that manages the streams
91  * in @media for @url. @media should be prepared.
92  *
93  * Ownership is taken of @media.
94  *
95  * Returns: a new #GstRTSPSessionMedia.
96  */
97 GstRTSPSessionMedia *
98 gst_rtsp_session_media_new (const GstRTSPUrl * url, GstRTSPMedia * media)
99 {
100   GstRTSPSessionMedia *result;
101   guint n_streams;
102
103   g_return_val_if_fail (url != NULL, NULL);
104   g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
105   g_return_val_if_fail (media->status == GST_RTSP_MEDIA_STATUS_PREPARED, NULL);
106
107   result = g_object_new (GST_TYPE_RTSP_SESSION_MEDIA, NULL);
108   result->url = gst_rtsp_url_copy ((GstRTSPUrl *) url);
109   result->media = media;
110
111   /* prealloc the streams now, filled with NULL */
112   n_streams = gst_rtsp_media_n_streams (media);
113   result->transports = g_ptr_array_new_full (n_streams, free_session_media);
114   g_ptr_array_set_size (result->transports, n_streams);
115
116   return result;
117 }
118
119 /**
120  * gst_rtsp_session_media_get_transport:
121  * @media: a #GstRTSPSessionMedia
122  * @idx: the stream index
123  *
124  * Get a previously created or create a new #GstRTSPStreamTransport at @idx.
125  *
126  * Returns: a #GstRTSPStreamTransport that is valid until the session of @media
127  * is unreffed.
128  */
129 GstRTSPStreamTransport *
130 gst_rtsp_session_media_get_transport (GstRTSPSessionMedia * media, guint idx)
131 {
132   GstRTSPStreamTransport *result;
133
134   g_return_val_if_fail (GST_IS_RTSP_SESSION_MEDIA (media), NULL);
135   g_return_val_if_fail (media->media != NULL, NULL);
136
137   if (idx >= media->transports->len)
138     return NULL;
139
140   result = g_ptr_array_index (media->transports, idx);
141   if (result == NULL) {
142     GstRTSPStream *stream;
143
144     stream = gst_rtsp_media_get_stream (media->media, idx);
145     if (stream == NULL)
146       goto no_media;
147
148     result = gst_rtsp_stream_transport_new (stream);
149
150     g_ptr_array_index (media->transports, idx) = result;
151   }
152   return result;
153
154   /* ERRORS */
155 no_media:
156   {
157     return NULL;
158   }
159 }
160
161 /**
162  * gst_rtsp_session_media_alloc_channels:
163  * @media: a #GstRTSPSessionMedia
164  * @range: a #GstRTSPRange
165  *
166  * Fill @range with the next available min and max channels for
167  * interleaved transport.
168  *
169  * Returns: %TRUE on success.
170  */
171 gboolean
172 gst_rtsp_session_media_alloc_channels (GstRTSPSessionMedia * media,
173     GstRTSPRange * range)
174 {
175   g_return_val_if_fail (GST_IS_RTSP_SESSION_MEDIA (media), FALSE);
176
177   range->min = media->counter++;
178   range->max = media->counter++;
179
180   return TRUE;
181 }
182
183 /**
184  * gst_rtsp_session_media_set_state:
185  * @media: a #GstRTSPSessionMedia
186  * @state: the new state
187  *
188  * Tell the media object @media to change to @state.
189  *
190  * Returns: %TRUE on success.
191  */
192 gboolean
193 gst_rtsp_session_media_set_state (GstRTSPSessionMedia * media, GstState state)
194 {
195   gboolean ret;
196
197   g_return_val_if_fail (GST_IS_RTSP_SESSION_MEDIA (media), FALSE);
198
199   ret = gst_rtsp_media_set_state (media->media, state, media->transports);
200
201   return ret;
202 }