media: avoid doing _get_state() for state changes
[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  * @user_data: user data passed in the callbacks
53  * @notify: free function for the user_data.
54  * @keep_alive: keep alive callback
55  * @ka_user_data: data passed to @keep_alive
56  * @ka_notify: called when @ka_user_data is freed
57  * @active: if we are actively sending
58  * @timeout: if we timed out
59  * @transport: a transport description
60  * @rtpsource: the receiver rtp source object
61  *
62  * A Transport description for stream @idx
63  */
64 struct _GstRTSPMediaTrans {
65   guint idx;
66
67   GstRTSPSendFunc      send_rtp;
68   GstRTSPSendFunc      send_rtcp;
69   gpointer             user_data;
70   GDestroyNotify       notify;
71
72   GstRTSPKeepAliveFunc keep_alive;
73   gpointer             ka_user_data;
74   GDestroyNotify       ka_notify;
75   gboolean             active;
76   gboolean             timeout;
77
78   GstRTSPTransport    *transport;
79
80   GObject             *rtpsource;
81 };
82
83 /**
84  * GstRTSPMediaStream:
85  *
86  * @srcpad: the srcpad of the stream
87  * @payloader: the payloader of the format
88  * @prepared: if the stream is prepared for streaming
89  * @server_port: the server udp ports
90  * @recv_rtp_sink: sinkpad for RTP buffers
91  * @recv_rtcp_sink: sinkpad for RTCP buffers
92  * @recv_rtp_src: srcpad for RTP buffers
93  * @recv_rtcp_src: srcpad for RTCP buffers
94  * @udpsrc: the udp source elements for RTP/RTCP
95  * @udpsink: the udp sink elements for RTP/RTCP
96  * @appsrc: the app source elements for RTP/RTCP
97  * @appsink: the app sink elements for RTP/RTCP
98  * @server_port: the server ports for this stream
99  * @caps_sig: the signal id for detecting caps
100  * @caps: the caps of the stream
101  * @tranports: the current transports being streamed
102  *
103  * The definition of a media stream. The streams are identified by @id.
104  */
105 struct _GstRTSPMediaStream {
106   GstPad       *srcpad;
107   GstElement   *payloader;
108   gboolean      prepared;
109
110   /* pads on the rtpbin */
111   GstPad       *recv_rtcp_sink;
112   GstPad       *recv_rtp_sink;
113   GstPad       *send_rtp_sink;
114   GstPad       *send_rtp_src;
115   GstPad       *send_rtcp_src;
116
117   /* the RTPSession object */
118   GObject      *session;
119
120   /* sinks used for sending and receiving RTP and RTCP, they share
121    * sockets */
122   GstElement   *udpsrc[2];
123   GstElement   *udpsink[2];
124   /* for TCP transport */
125   GstElement   *appsrc[2];
126   GstElement   *appsink[2];
127
128   GstElement   *tee[2];
129   GstElement   *selector[2];
130
131   /* server ports for sending/receiving */
132   GstRTSPRange  server_port;
133
134   /* the caps of the stream */
135   gulong        caps_sig;
136   GstCaps      *caps;
137
138   /* transports we stream to */
139   GList        *transports;
140 };
141
142 /**
143  * GstRTSPMediaStatus:
144  * @GST_RTSP_MEDIA_STATUS_UNPREPARED: media pipeline not prerolled
145  * @GST_RTSP_MEDIA_STATUS_PREPARING: media pipeline is prerolling
146  * @GST_RTSP_MEDIA_STATUS_PREPARED: media pipeline is prerolled
147  * @GST_RTSP_MEDIA_STATUS_ERROR: media pipeline is in error
148  *
149  * The state of the media pipeline.
150  */
151 typedef enum {
152   GST_RTSP_MEDIA_STATUS_UNPREPARED = 0,
153   GST_RTSP_MEDIA_STATUS_PREPARING  = 1,
154   GST_RTSP_MEDIA_STATUS_PREPARED   = 2,
155   GST_RTSP_MEDIA_STATUS_ERROR      = 3
156 } GstRTSPMediaStatus;
157
158 /**
159  * GstRTSPMedia:
160  * @shared: if this media can be shared between clients
161  * @reusable: if this media can be reused after an unprepare
162  * @element: the data providing element
163  * @streams: the different streams provided by @element
164  * @status: the status of the media pipeline
165  * @pipeline: the toplevel pipeline
166  * @source: the bus watch for pipeline messages.
167  * @id: the id of the watch
168  * @is_live: if the pipeline is live
169  * @buffering: if the pipeline is buffering
170  * @target_state: the desired target state of the pipeline
171  * @rtpbin: the rtpbin
172  * @range: the range of the media being streamed
173  *
174  * A class that contains the GStreamer element along with a list of
175  * #GstRTSPediaStream objects that can produce data.
176  *
177  * This object is usually created from a #GstRTSPMediaFactory.
178  */
179 struct _GstRTSPMedia {
180   GObject            parent;
181
182   GMutex            *lock;
183   GCond             *cond;
184
185   gboolean           shared;
186   gboolean           reusable;
187   gboolean           reused;
188
189   GstElement        *element;
190   GArray            *streams;
191   GList             *dynamic;
192   GstRTSPMediaStatus status;
193   gint               active;
194
195   /* the pipeline for the media */
196   GstElement        *pipeline;
197   GstElement        *fakesink;
198   GSource           *source;
199   guint              id;
200
201   gboolean           is_live;
202   gboolean           buffering;
203   GstState           target_state;
204
205   /* RTP session manager */
206   GstElement        *rtpbin;
207
208   /* the range of media */
209   GstRTSPTimeRange   range;
210 };
211
212 /**
213  * GstRTSPMediaClass:
214  * @context: the main context for dispatching messages
215  * @loop: the mainloop for message.
216  * @thread: the thread dispatching messages.
217  * @handle_message: handle a message
218  * @unprepare: the default implementation sets the pipeline's state
219  *             to GST_STATE_NULL.
220  *
221  * The RTSP media class
222  */
223 struct _GstRTSPMediaClass {
224   GObjectClass  parent_class;
225
226   /* thread for the mainloop */
227   GMainContext *context;
228   GMainLoop    *loop;
229   GThread      *thread;
230
231   /* vmethods */
232   gboolean     (*handle_message)  (GstRTSPMedia *media, GstMessage *message);
233   gboolean     (*unprepare)       (GstRTSPMedia *media);
234
235   /* signals */
236   gboolean     (*unprepared)      (GstRTSPMedia *media);
237 };
238
239 GType                 gst_rtsp_media_get_type         (void);
240
241 /* creating the media */
242 GstRTSPMedia *        gst_rtsp_media_new              (void);
243
244 void                  gst_rtsp_media_set_shared       (GstRTSPMedia *media, gboolean shared);
245 gboolean              gst_rtsp_media_is_shared        (GstRTSPMedia *media);
246
247 void                  gst_rtsp_media_set_reusable     (GstRTSPMedia *media, gboolean reusable);
248 gboolean              gst_rtsp_media_is_reusable      (GstRTSPMedia *media);
249
250 /* prepare the media for playback */
251 gboolean              gst_rtsp_media_prepare          (GstRTSPMedia *media);
252 gboolean              gst_rtsp_media_is_prepared      (GstRTSPMedia *media);
253 gboolean              gst_rtsp_media_unprepare        (GstRTSPMedia *media);
254
255 /* dealing with the media */
256 guint                 gst_rtsp_media_n_streams        (GstRTSPMedia *media);
257 GstRTSPMediaStream *  gst_rtsp_media_get_stream       (GstRTSPMedia *media, guint idx);
258
259 gboolean              gst_rtsp_media_seek             (GstRTSPMedia *media, GstRTSPTimeRange *range);
260
261 GstFlowReturn         gst_rtsp_media_stream_rtp       (GstRTSPMediaStream *stream, GstBuffer *buffer);
262 GstFlowReturn         gst_rtsp_media_stream_rtcp      (GstRTSPMediaStream *stream, GstBuffer *buffer);
263
264 gboolean              gst_rtsp_media_set_state        (GstRTSPMedia *media, GstState state, GArray *trans);
265
266 void                  gst_rtsp_media_remove_elements  (GstRTSPMedia *media);
267
268 G_END_DECLS
269
270 #endif /* __GST_RTSP_MEDIA_H__ */