rtsp-client: Use fixed backlog size
[platform/upstream/gstreamer.git] / docs / README
1 README 
2 ------
3
4 (Last updated on Mon 15 jul 2013, version 0.11.90.1)
5
6 This HOWTO describes the basic usage of the GStreamer RTSP libraries and how you
7 can build simple server applications with it.
8
9 * General
10
11  The server relies heavily on the RTSP infrastructure of GStreamer. This includes
12  all of the media acquisition, decoding, encoding, payloading and UDP/TCP
13  streaming. We use the rtpbin element for all the session management. Most of
14  the RTSP message parsing and construction in the server is done using the RTSP
15  library that comes with gst-plugins-base. 
16
17  The result is that the server is rather small (a few 11000 lines of code) and easy
18  to understand and extend. In its current state of development, things change
19  fast, API and ABI are unstable. We encourage people to use it for their various
20  use cases and participate by suggesting changes/features.
21
22  Most of the server is built as a library containing a bunch of GObject objects
23  that provide reasonable default functionality but has a fair amount of hooks
24  to override the default behaviour.
25
26  The server currently integrates with the glib mainloop nicely.  It's currently
27  not meant to be used in high-load scenarios and because no security audit has
28  been done, you should probably not put it on a public IP address.
29
30 * Initialisation
31
32  You need to initialize GStreamer before using any of the RTSP server functions.
33
34    #include <gst/gst.h>
35
36    int
37    main (int argc, char *argv[])
38    {
39      gst_init (&argc, &argv);
40
41      ...
42    }
43  
44  The server itself currently does not have any specific initialisation function
45  but that might change in the future.
46
47
48 * Creating the server
49
50  The first thing you want to do is create a new GstRTSPServer object. This object
51  will handle all the new client connections to your server once it is added to a
52  GMainLoop. You can create a new server object like this:
53
54    #include <gst/rtsp-server/rtsp-server.h>
55
56    GstRTSPServer *server;
57
58    server = gst_rtsp_server_new ();
59
60  The server will by default listen on port 8554 for new connections. This can be
61  changed by calling gst_rtsp_server_set_service() or with the 'service' GObject
62  property. This makes it possible to run multiple server instances listening on
63  multiple ports on one machine.
64
65  We can make the server start listening on its default port by attaching it to a
66  mainloop. The following example shows how this is done and will start a server
67  on the default 8554 port. For any request we make, we will get a NOT_FOUND
68  error code because we need to configure more things before the server becomes
69  useful.
70
71    #include <gst/gst.h>
72    #include <gst/rtsp-server/rtsp-server.h>
73
74    int
75    main (int argc, char *argv[])
76    {
77      GstRTSPServer *server;
78      GMainLoop *loop;
79
80      gst_init (&argc, &argv);
81
82      server = gst_rtsp_server_new ();
83
84      /* make a mainloop for the default context */
85      loop = g_main_loop_new (NULL, FALSE);
86
87      /* attach the server to the default maincontext */
88      gst_rtsp_server_attach (server, NULL);
89
90      /* start serving */
91      g_main_loop_run (loop);
92    }
93
94  The server manages four other objects: GstRTSPSessionPool,
95  GstRTSPMountPoints, GstRTSPAuth and GstRTSPThreadPool.
96
97  The GstRTSPSessionPool is an object that keeps track of all the active sessions
98  in the server. A session will usually be kept for each client that performed a
99  SETUP request for a certain media stream. It contains the configuration that
100  the client negotiated with the server to receive the particular stream, ie. the
101  transport used and port pairs for UDP along with the state of the streaming.
102  The default implementation of the session pool is usually sufficient but
103  alternative implementation can be used by the server.
104
105  The GstRTSPMountPoints object is more interesting and needs more configuration
106  before the server object is useful. This object manages the mapping from a
107  request URL to a specific stream and its configuration. We explain in the next
108  topic how to configure this object.
109
110  GstRTSPAuth is an object that authenticates users and authorizes actions
111  performed by users. By default, a server does not have a GstRTSPAuth object and
112  thus does not try to perform any authentication or authorization.
113
114  GstRTSPThreadPool manages the threads used for client connections and media
115  pipelines. The server has a default implementation of a threadpool that should
116  be sufficient in most cases.
117
118
119 * Making url mount points
120
121  Next we need to define what media is attached to a particular URL. What we want
122  to achieve is that when the user asks our server for a specific URL, say /test,
123  that we create (or reuse) a GStreamer pipeline that produces one or more RTP
124  streams. 
125  
126  The object that can create such pipeline is called a GstRTSPMediaFactory object. 
127  The default implementation of GstRTSPMediaFactory allows you to easily create
128  GStreamer pipelines using the gst-launch syntax. It is possible to create a
129  GstRTSPMediaFactory subclass that uses different methods for constructing
130  pipelines.
131
132  The default GstRTSPMediaFactory can be configured with a gst-launch line that
133  produces a toplevel bin (use '(' and ')' around the pipeline description to
134  force a toplevel GstBin instead of the default GstPipeline toplevel element).
135  The pipeline description should contain elements named payN, one for each
136  stream (ex. pay0, pay1, ...). Also, for increased compatibility each stream
137  should have a different payload type which can be configured on the payloader.
138
139  The following code snippet illustrates how to create a media factory that
140  creates an RTP feed of an H264 encoded test video signal.
141
142    GstRTSPMediaFactory *factory;
143
144    factory = gst_rtsp_media_factory_new ();
145
146    gst_rtsp_media_factory_set_launch (factory, 
147                 "( videotestsrc ! x264enc ! rtph264pay pt=96 name=pay0 )");
148
149  Now that we have the media factory, we can attach it to a specific url. To do
150  this we get the default GstRTSPMountPoints from our server and add the url to
151  factory mount points to it like this:
152
153    GstRTSPMountPoints *mounts;
154
155    ...create server..create factory..
156
157    /* get the default mount points from the server */
158    mounts = gst_rtsp_server_get_mount_points (server);
159
160    /* attach the video test signal to the "/test" URL */
161    gst_rtsp_mount_points_add_factory (mounts, "/test", factory);
162    g_object_unref (mounts);
163
164  When starting the server now and directing an RTP client to the URL (like with 
165  vlc, mplayer or gstreamer):
166
167    rtsp://localhost:8554/test 
168
169  a test signal will be streamed to the client. The full example code can be
170  found in the examples/test-readme.c file.
171
172  Note that by default the factory will create a new pipeline for each client. If
173  you want to share a pipeline between clients, use
174  gst_rtsp_media_factory_set_shared().
175
176
177 * more on GstRTSPMediaFactory
178
179  The GstRTSPMediaFactory is responsible for creating and caching GstRTSPMedia
180  objects. 
181
182  A freshly created GstRTSPMedia object from the factory initially only contains a
183  GstElement containing the elements to produce the RTP streams for the media and
184  a GPtrArray of GstRTSPStream objects describing the payloader and its source
185  pad. The media is unprepared in this state.
186
187  Usually the url will determine what kind of pipeline should be created. You can
188  for example use query parameters to configure certain parts of the pipeline or
189  select encoders and payloaders based on some url pattern.
190
191  When dealing with a live stream from, for example, a webcam, it can be
192  interesting to share the pipeline with multiple clients. This must be done when
193  only one instance of the video capture element can be used at a time. In this
194  case, the shared property of GstRTSPMedia must be used to instruct the default
195  GstRTSPMediaFactory implementation to cache the media.
196
197  When all objects created from a factory can be shared, you can set the shared
198  property directly on the factory.
199
200 * more on GstRTSPMedia
201
202  After creating the GstRTSPMedia object from the factory, it can be prepared
203  with gst_rtsp_media_prepare(). This method will put those objects in a
204  GstPipeline and will construct and link the streaming elements and the
205  rtpbin session manager object.
206
207  The _prepare() method will then preroll the pipeline in order to figure out the
208  caps on the payloaders. After the GstRTSPMedia prerolled it will be in the
209  prepared state and can be used for creating SDP files or for streaming to
210  clients.
211
212  The prepare method will also create 2 UDP ports for each stream that can be
213  used for sending and receiving RTP/RTCP from clients. These port numbers will
214  have to be negotiated with the client in the SETUP requests.
215
216  When preparing a GstRTSPMedia, an appsink and asppsrc is also constructed
217  for streaming the stream over TCP when requested.
218
219  Media is prepared by the server when DESCRIBE or SETUP requests are received
220  from the client.
221
222
223 * the GstRTSPClient object
224
225  When a server detects a new client connection on its port, it will accept the
226  connection, check if the connection is allowed and then call the vmethod
227  create_client. The default implementation of this function will create
228  a new GstRTCPClient object, will configure the session pool, mount points,
229  auth and thread pool objects in it.
230
231  The server will then attach the new client to a server mainloop to let it
232  handle further communication with the client. In RTSP it is usual to keep
233  the connection open between multiple RTSP requests. The client watch will
234  be dispatched by the server mainloop when a new GstRTSPMessage is received,
235  which will then be handled and a response will be sent.
236
237  The GstRTSPClient object remains alive for as long as a client has a TCP
238  connection open with the server. Since is possible for a client to open and close
239  the TCP connection between requests, we cannot store the state related
240  to the configured RTSP session in the GstRTSPClient object. This server state
241  is instead stored in the GstRTSPSession object, identified with the session
242  id.
243
244
245 * GstRTSPSession
246
247  This object contains state about a specific RTSP session identified with a
248  session id. This state contains the configured streams and their associated
249  transports.
250  
251  When a GstRTSPClient performs a SETUP request, the server will allocate a new
252  GstRTSPSession with a unique session id from the GstRTSPSessionPool. The pool
253  maintains a list of all existing sessions and makes sure that no session id is
254  used multiple times. The session id is sent to the client so that the client
255  can refer to its previously configured state by sending the session id in
256  further requests.
257
258  A client will then use the session id to configure one or more
259  GstRTSPSessionMedia objects, identified by their url. This SessionMedia object
260  contains the configuration of a GstRTSPMedia and its configured
261  GstRTSPStreamTransport.
262
263
264 * GstRTSPSessionMedia and GstRTSPStreamTransport
265
266  A GstRTSPSessionMedia is identified by a URL and is referenced by a
267  GstRTSPSession. It is created as soon as a client performs a SETUP operation on
268  a particular URL. It will contain a link to the GstRTSPMedia object associated
269  with the URL along with the state of the media and the configured transports
270  for each of the streams in the media.
271
272  Each SETUP request performed by the client will configure a
273  GstRTSPStreamTransport object linked to by the GstRTSPSessionMedia structure.
274  It will contain the transport information needed to send this stream to the
275  client. The GstRTSPStreamTransport also contains a link to the GstRTSPStream
276  object that generates the actual data to be streamed to the client.
277
278  Note how GstRTSPMedia and GstRTSPStream (the providers of the data to
279  stream) are decoupled from GstRTSPSessionMedia and GstRTSPStreamTransport (the
280  configuration of how to send this stream to a client) in order to be able to
281  send the data of one GstRTSPMedia to multiple clients.
282
283
284 * media control
285
286  After a client has configured the transports for a GstRTSPMedia and its
287  GstRTSPStreams, the client can play/pause/stop the stream.
288
289  The GstRTSPMedia object was prepared in the DESCRIBE call (or during SETUP when
290  the client skipped the DESCRIBE request). As seen earlier, this configures a
291  couple of udpsink and udpsrc elements to respectively send and receive the
292  media to clients.
293
294  When a client performs a PLAY request, its configured destination UDP ports are
295  added to the GstRTSPStream target destinations, at which point data will
296  be sent to the client. The corresponding GstRTSPMedia object will be set to the
297  PLAYING state if it was not allready in order to send the data to the
298  destination.
299
300  The server needs to prepare an RTP-Info header field in the PLAY response,
301  which consists of the sequence number and the RTP timestamp of the next RTP
302  packet. In order to achive this, the server queries the payloaders for this
303  information when it prerolled the pipeline.
304
305  When a client performs a PAUSE request, the destination UDP ports are removed
306  from the GstRTSPStream object and the GstRTSPMedia object is set to PAUSED
307  if no other destinations are configured anymore.
308
309
310 * seeking
311
312  A seek is performed when a client sends a Range header in the PLAY request.
313  This only works when not dealing with shared (live) streams.
314
315  The server performs a GStreamer flushing seek on the media, waits for the
316  pipeline to preroll again and then responds to the client after collecting the
317  new RTP sequence number and timestamp from the payloaders.
318
319
320 * session management
321
322  The server has to react to clients that suddenly disappear because of network
323  problems or otherwise. It needs to make sure that it can reasonable free the
324  resources that are used by the various objects in use for streaming when the
325  client appears to be gone.
326
327  Each of the GstRTSPSession objects managed by a GstRTSPSessionPool has
328  therefore a last_access field that contains the timestamp of when activity from
329  a client was last recorded.
330  
331  Various ways exist to detect activity from a client:
332
333   - RTSP keepalive requests. When a client is receiving RTP data, the RTSP TCP
334     connection is largely unused. It is the client's responsability to
335     periodically send keep-alive requests over the TCP channel.
336
337     Whenever a keep-alive request is received by the server (any request that
338     contains a session id, usually an OPTION or GET_PARAMETER request) the
339     last_access of the session is updated.
340
341   - Since it is not required for a client to keep the RTSP TCP connection open
342     while streaming, gst-rtsp-server also detects activity from clients by
343     looking at the RTCP messages it receives.
344
345     When an RTCP message is received from a client, the server looks in its list
346     of active ports if this message originates from a known host/port pair that
347     is currently active in a GstRTSPSession. If this is the case, the session is
348     kept alive.
349
350     Since the server does not know anything about the port number that will be
351     used by the client to send RTCP, this method does not always work. Later
352     RTSP RFCs will include support for negotiating this port number with the
353     server. Most clients however use the same port number for sending and
354     receiving RTCP exactly for this reason.
355
356  If there was no activity in a particular session for a long time (by default 60
357  seconds), the application should remove the session from the pool. For this,
358  the application should periodically (say every 2 seconds) check if no sessions
359  expired and call gst_rtsp_session_pool_cleanup() to remove them.
360  
361  When a session is removed from the sessionpool and its last reference is
362  unreffef, all related objects and media are destroyed as if a TEARDOWN happened
363  from the client.
364
365
366 * TEARDOWN
367
368  A TEARDOWN request will first locate the GstRTSPSessionMedia of the URL. It
369  will then remove all transports from the streams, making sure that streaming
370  stops to the clients. It will then remove the GstRTSPSessionMedia and
371  GstRTSPStreamTransport objects. Finally the GstRTSPSession is released back
372  into the pool.
373
374  When there are no more references to the GstRTSPMedia, the media pipeline is
375  shut down (with _unprepare) and destroyed. This will then also destroy the
376  GstRTSPStream objects.
377
378
379 * Security
380
381  The security of the server and the policy is implemented in a GstRTSPAuth
382  object. The object is reponsible for:
383
384   - authenticate the user of the server.
385
386   - check if the current user is authorized to perform an operation.
387
388  For critical operations, the server will call gst_rtsp_auth_check() with
389  a string describing the operation which should be validated. The installed
390  GstRTSPAuth object is then responsible for checking if the operation is
391  allowed.
392
393  Implementations of GstRTSPAuth objects can use the following infrastructure
394  bits of the rtsp server to implement these checks:
395
396   - GstRTSPToken: a generic structure describing roles and permissions granted
397     to a user.
398
399   - GstRTSPPermissions: a generic list of roles and matching permissions. These
400     can be attached to media and facties currently.
401
402  An Auth implementation will usually authenticate a user, using method such as
403  Basic authentication or client certificates or perhaps simply use the IP address.
404  The result of the authentication of the user will be a GstRTSPToken that is made
405  current in the context of the ongoing request.
406
407  The auth module can then implement the various checks in the server by looking
408  at the current token and, if needed, compare it to the required GstRTSPPermissions
409  of the current object.
410
411  The security is deliberately kept generic with a default implementation of the
412  GstRTSPAuth object providing a usable and simple implementaion. To make more
413  complicated security modules, the auth object should be subclassed and new
414  implementations for the checks needs to be made.
415
416
417 Objects
418 -------
419
420 GstRTSPServer
421  - Toplevel object listening for connections and creating new
422    GstRTSPClient objects
423
424 GstRTSPClient
425  - Handle RTSP Requests from connected clients. All other objects
426    are called by this object.
427
428 GstRTSPContext
429  - Helper structure contaning the current state of the request
430    handled by the client.
431
432
433 GstRTSPMountPoints
434  - Maps a url to a GstRTSPMediaFactory implementation. The default
435    implementation uses a simple hashtable to map a url to a factory.
436
437 GstRTSPMediaFactory
438  - Creates and caches GstRTSPMedia objects. The default implementation
439    can create GstRTSPMedia objects based on gst-launch syntax.
440
441 GstRTSPMediaFactoryURI
442  - Specialized GstRTSPMediaFactory that can stream the content of any
443    URI.
444
445 GstRTSPMedia
446  - The object that contains the media pipeline and various GstRTSPStream
447    objects that produce RTP packets
448
449 GstRTSPStream
450  - Manages the elements to stream a stream of a GstRTSPMedia to one or
451    more GstRTSPStreamTransports.
452
453
454 GstRTSPSessionPool
455  - Creates and manages GstRTSPSession objects identified by an id.
456
457 GstRTSPSession
458  - An object containing the various GstRTSPSessionMedia objects managed
459    by this session.
460
461 GstRTSPSessionMedia
462  - The state of a GstRTSPMedia and the configuration of a GstRTSPStream
463    objects. The configuration for the GstRTSPStream is stored in
464    GstRTSPStreamTransport objects.
465
466 GstRTSPStreamTransport
467  - Configuration of how a GstRTSPStream is send to a particular client. It
468    contains the transport that was negotiated with the client in the SETUP
469    request.
470
471
472 GstRTSPSDP
473  - helper functions for creating SDP messages from gstRTSPMedia
474
475 GstRTSPAddressPool
476  - a pool of multicast and unicast addresses used in streaming
477
478 GstRTSPThreadPool
479  - a pool of threads used for various server tasks such as handling clients and
480    managin media pipelines.
481
482
483 GstRTSPAuth
484  - Hooks for checking authorizations, all client activity will call this
485    object with the GstRTSPContext structure. By default it supports
486    basic authentication.
487
488 GstRTSPToken
489  - Credentials of a user. This contrains the roles that the user is allowed
490    to assume and other permissions or capabilities of the user.
491
492 GstRTSPPermissions
493  - A list of permissions for each role. The permissions are usually attached
494    to objects to describe what roles have what permissions.
495
496 GstRTSPParams
497  - object to handle get and set parameter requests.
498