rtsp-server: port to new thread API
[platform/upstream/gstreamer.git] / gst / rtsp-server / rtsp-media.h
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
20 #include <gst/gst.h>
21 #include <gst/rtsp/gstrtsprange.h>
22 #include <gst/rtsp/gstrtspurl.h>
23
24 #ifndef __GST_RTSP_MEDIA_H__
25 #define __GST_RTSP_MEDIA_H__
26
27 G_BEGIN_DECLS
28
29 /* types for the media */
30 #define GST_TYPE_RTSP_MEDIA              (gst_rtsp_media_get_type ())
31 #define GST_IS_RTSP_MEDIA(obj)           (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_RTSP_MEDIA))
32 #define GST_IS_RTSP_MEDIA_CLASS(klass)   (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_RTSP_MEDIA))
33 #define GST_RTSP_MEDIA_GET_CLASS(obj)    (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_RTSP_MEDIA, GstRTSPMediaClass))
34 #define GST_RTSP_MEDIA(obj)              (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_RTSP_MEDIA, GstRTSPMedia))
35 #define GST_RTSP_MEDIA_CLASS(klass)      (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_RTSP_MEDIA, GstRTSPMediaClass))
36 #define GST_RTSP_MEDIA_CAST(obj)         ((GstRTSPMedia*)(obj))
37 #define GST_RTSP_MEDIA_CLASS_CAST(klass) ((GstRTSPMediaClass*)(klass))
38
39 typedef struct _GstRTSPMediaStream GstRTSPMediaStream;
40 typedef struct _GstRTSPMedia GstRTSPMedia;
41 typedef struct _GstRTSPMediaClass GstRTSPMediaClass;
42 typedef struct _GstRTSPMediaTrans GstRTSPMediaTrans;
43
44 typedef gboolean (*GstRTSPSendFunc)      (GstBuffer *buffer, guint8 channel, gpointer user_data);
45 typedef void     (*GstRTSPKeepAliveFunc) (gpointer user_data);
46
47 /**
48  * GstRTSPMediaTrans:
49  * @idx: a stream index
50  * @send_rtp: callback for sending RTP messages
51  * @send_rtcp: callback for sending RTCP messages
52  * @send_rtp_list: callback for sending RTP messages
53  * @send_rtcp_list: callback for sending RTCP messages
54  * @user_data: user data passed in the callbacks
55  * @notify: free function for the user_data.
56  * @keep_alive: keep alive callback
57  * @ka_user_data: data passed to @keep_alive
58  * @ka_notify: called when @ka_user_data is freed
59  * @active: if we are actively sending
60  * @timeout: if we timed out
61  * @transport: a transport description
62  * @rtpsource: the receiver rtp source object
63  *
64  * A Transport description for stream @idx
65  */
66 struct _GstRTSPMediaTrans {
67   guint idx;
68
69   GstRTSPSendFunc      send_rtp;
70   GstRTSPSendFunc      send_rtcp;
71   gpointer             user_data;
72   GDestroyNotify       notify;
73
74   GstRTSPKeepAliveFunc keep_alive;
75   gpointer             ka_user_data;
76   GDestroyNotify       ka_notify;
77   gboolean             active;
78   gboolean             timeout;
79
80   GstRTSPTransport    *transport;
81
82   GObject             *rtpsource;
83 };
84
85 #include "rtsp-auth.h"
86
87 /**
88  * GstRTSPMediaStream:
89  * @srcpad: the srcpad of the stream
90  * @payloader: the payloader of the format
91  * @prepared: if the stream is prepared for streaming
92  * @recv_rtp_sink: sinkpad for RTP buffers
93  * @recv_rtcp_sink: sinkpad for RTCP buffers
94  * @send_rtp_src: srcpad for RTP buffers
95  * @send_rtcp_src: srcpad for RTCP buffers
96  * @udpsrc: the udp source elements for RTP/RTCP
97  * @udpsink: the udp sink elements for RTP/RTCP
98  * @appsrc: the app source elements for RTP/RTCP
99  * @appsink: the app sink elements for RTP/RTCP
100  * @server_port: the server ports for this stream
101  * @caps_sig: the signal id for detecting caps
102  * @caps: the caps of the stream
103  * @tranports: the current transports being streamed
104  *
105  * The definition of a media stream. The streams are identified by @id.
106  */
107 struct _GstRTSPMediaStream {
108   GstPad       *srcpad;
109   GstElement   *payloader;
110   gboolean      prepared;
111
112   /* pads on the rtpbin */
113   GstPad       *recv_rtcp_sink;
114   GstPad       *recv_rtp_sink;
115   GstPad       *send_rtp_sink;
116   GstPad       *send_rtp_src;
117   GstPad       *send_rtcp_src;
118
119   /* the RTPSession object */
120   GObject      *session;
121
122   /* sinks used for sending and receiving RTP and RTCP, they share
123    * sockets */
124   GstElement   *udpsrc[2];
125   GstElement   *udpsink[2];
126   /* for TCP transport */
127   GstElement   *appsrc[2];
128   GstElement   *appqueue[2];
129   GstElement   *appsink[2];
130
131   GstElement   *tee[2];
132   GstElement   *selector[2];
133
134   /* server ports for sending/receiving */
135   GstRTSPRange  server_port;
136
137   /* the caps of the stream */
138   gulong        caps_sig;
139   GstCaps      *caps;
140
141   /* transports we stream to */
142   GList        *transports;
143 };
144
145 /**
146  * GstRTSPMediaStatus:
147  * @GST_RTSP_MEDIA_STATUS_UNPREPARED: media pipeline not prerolled
148  * @GST_RTSP_MEDIA_STATUS_PREPARING: media pipeline is prerolling
149  * @GST_RTSP_MEDIA_STATUS_PREPARED: media pipeline is prerolled
150  * @GST_RTSP_MEDIA_STATUS_ERROR: media pipeline is in error
151  *
152  * The state of the media pipeline.
153  */
154 typedef enum {
155   GST_RTSP_MEDIA_STATUS_UNPREPARED = 0,
156   GST_RTSP_MEDIA_STATUS_PREPARING  = 1,
157   GST_RTSP_MEDIA_STATUS_PREPARED   = 2,
158   GST_RTSP_MEDIA_STATUS_ERROR      = 3
159 } GstRTSPMediaStatus;
160
161 /**
162  * GstRTSPMedia:
163  * @lock: for protecting the object
164  * @cond: for signaling the object
165  * @shared: if this media can be shared between clients
166  * @reusable: if this media can be reused after an unprepare
167  * @protocols: the allowed lower transport for this stream
168  * @reused: if this media has been reused
169  * @is_ipv6: if this media is using ipv6
170  * @element: the data providing element
171  * @streams: the different streams provided by @element
172  * @dynamic: list of dynamic elements managed by @element
173  * @status: the status of the media pipeline
174  * @active: the number of active connections
175  * @pipeline: the toplevel pipeline
176  * @fakesink: for making state changes async
177  * @source: the bus watch for pipeline messages.
178  * @id: the id of the watch
179  * @is_live: if the pipeline is live
180  * @seekable: if the pipeline can perform a seek
181  * @buffering: if the pipeline is buffering
182  * @target_state: the desired target state of the pipeline
183  * @rtpbin: the rtpbin
184  * @range: the range of the media being streamed
185  *
186  * A class that contains the GStreamer element along with a list of
187  * #GstRTSPMediaStream objects that can produce data.
188  *
189  * This object is usually created from a #GstRTSPMediaFactory.
190  */
191 struct _GstRTSPMedia {
192   GObject            parent;
193
194   GMutex             lock;
195   GCond              cond;
196
197   gboolean           shared;
198   gboolean           reusable;
199   GstRTSPLowerTrans  protocols;
200   gboolean           reused;
201   gboolean           is_ipv6;
202   gboolean           eos_shutdown;
203   guint              buffer_size;
204   GstRTSPAuth       *auth;
205   gchar             *multicast_group;
206
207   GstElement        *element;
208   GArray            *streams;
209   GList             *dynamic;
210   GstRTSPMediaStatus status;
211   gint               active;
212   gboolean           eos_pending;
213   gboolean           adding;
214
215   /* the pipeline for the media */
216   GstElement        *pipeline;
217   GstElement        *fakesink;
218   GSource           *source;
219   guint              id;
220
221   gboolean           is_live;
222   gboolean           seekable;
223   gboolean           buffering;
224   GstState           target_state;
225
226   /* RTP session manager */
227   GstElement        *rtpbin;
228
229   /* the range of media */
230   GstRTSPTimeRange   range;
231 };
232
233 /**
234  * GstRTSPMediaClass:
235  * @context: the main context for dispatching messages
236  * @loop: the mainloop for message.
237  * @thread: the thread dispatching messages.
238  * @handle_message: handle a message
239  * @unprepare: the default implementation sets the pipeline's state
240  *             to GST_STATE_NULL.
241  *
242  * The RTSP media class
243  */
244 struct _GstRTSPMediaClass {
245   GObjectClass  parent_class;
246
247   /* thread for the mainloop */
248   GMainContext *context;
249   GMainLoop    *loop;
250   GThread      *thread;
251
252   /* vmethods */
253   gboolean     (*handle_message)  (GstRTSPMedia *media, GstMessage *message);
254   gboolean     (*unprepare)       (GstRTSPMedia *media);
255
256   /* signals */
257   gboolean     (*prepared)        (GstRTSPMedia *media);
258   gboolean     (*unprepared)      (GstRTSPMedia *media);
259
260   gboolean     (*new_state)       (GstRTSPMedia *media, GstState state);
261 };
262
263 GType                 gst_rtsp_media_get_type         (void);
264
265 /* creating the media */
266 GstRTSPMedia *        gst_rtsp_media_new              (void);
267
268 void                  gst_rtsp_media_set_shared       (GstRTSPMedia *media, gboolean shared);
269 gboolean              gst_rtsp_media_is_shared        (GstRTSPMedia *media);
270
271 void                  gst_rtsp_media_set_reusable     (GstRTSPMedia *media, gboolean reusable);
272 gboolean              gst_rtsp_media_is_reusable      (GstRTSPMedia *media);
273
274 void                  gst_rtsp_media_set_protocols    (GstRTSPMedia *media, GstRTSPLowerTrans protocols);
275 GstRTSPLowerTrans     gst_rtsp_media_get_protocols    (GstRTSPMedia *media);
276
277 void                  gst_rtsp_media_set_eos_shutdown (GstRTSPMedia *media, gboolean eos_shutdown);
278 gboolean              gst_rtsp_media_is_eos_shutdown  (GstRTSPMedia *media);
279
280 void                  gst_rtsp_media_set_auth         (GstRTSPMedia *media, GstRTSPAuth *auth);
281 GstRTSPAuth *         gst_rtsp_media_get_auth         (GstRTSPMedia *media);
282
283 void                  gst_rtsp_media_set_buffer_size  (GstRTSPMedia *media, guint size);
284 guint                 gst_rtsp_media_get_buffer_size  (GstRTSPMedia *media);
285
286 void                  gst_rtsp_media_set_multicast_group (GstRTSPMedia *media, const gchar * mc);
287 gchar *               gst_rtsp_media_get_multicast_group (GstRTSPMedia *media);
288
289
290 /* prepare the media for playback */
291 gboolean              gst_rtsp_media_prepare          (GstRTSPMedia *media);
292 gboolean              gst_rtsp_media_is_prepared      (GstRTSPMedia *media);
293 gboolean              gst_rtsp_media_unprepare        (GstRTSPMedia *media);
294
295 /* dealing with the media */
296 guint                 gst_rtsp_media_n_streams        (GstRTSPMedia *media);
297 GstRTSPMediaStream *  gst_rtsp_media_get_stream       (GstRTSPMedia *media, guint idx);
298
299 gboolean              gst_rtsp_media_seek             (GstRTSPMedia *media, GstRTSPTimeRange *range);
300 gchar *               gst_rtsp_media_get_range_string (GstRTSPMedia *media, gboolean play);
301
302 GstFlowReturn         gst_rtsp_media_stream_rtp       (GstRTSPMediaStream *stream, GstBuffer *buffer);
303 GstFlowReturn         gst_rtsp_media_stream_rtcp      (GstRTSPMediaStream *stream, GstBuffer *buffer);
304
305 gboolean              gst_rtsp_media_set_state        (GstRTSPMedia *media, GstState state, GArray *transports);
306
307 void                  gst_rtsp_media_remove_elements  (GstRTSPMedia *media);
308
309 void                  gst_rtsp_media_trans_cleanup    (GstRTSPMediaTrans *trans);
310
311 G_END_DECLS
312
313 #endif /* __GST_RTSP_MEDIA_H__ */