1 /*
2  *			GPAC - Multimedia Framework C SDK
3  *
4  *			Authors: Jean Le Feuvre
5  *			Copyright (c) Telecom ParisTech 2000-2019
6  *					All rights reserved
7  *
8  *  This file is part of GPAC / IETF RTP/RTSP/SDP sub-project
9  *
10  *  GPAC is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU Lesser General Public License as published by
12  *  the Free Software Foundation; either version 2, or (at your option)
13  *  any later version.
14  *
15  *  GPAC is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU Lesser General Public License for more details.
19  *
20  *  You should have received a copy of the GNU Lesser General Public
21  *  License along with this library; see the file COPYING.  If not, write to
22  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23  *
24  */
25 
26 
27 #ifndef	_GF_IETF_H_
28 #define _GF_IETF_H_
29 
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 /*!
36 \file <gpac/ietf.h>
37 \brief Tools for real-time streaming over IP using RTP/RTCP/RTSP/SDP .
38 */
39 
40 /*!
41 \addtogroup ietf_grp RTP Streaming
42 \ingroup media_grp
43 \brief  Tools for real-time streaming over IP using RTP/RTCP/RTSP/SDP.
44 
45 
46 This section documents the tools used for real-time streaming over IP using RTP/RTCP/RTSP/SDP.
47 
48 @{
49  */
50 
51 #include <gpac/list.h>
52 
53 #ifndef GPAC_DISABLE_STREAMING
54 
55 #include <gpac/bitstream.h>
56 #include <gpac/sync_layer.h>
57 #include <gpac/network.h>
58 
59 
60 /*! RTSP version supported by GPAC*/
61 #define GF_RTSP_VERSION		"RTSP/1.0"
62 
63 
64 /*! RTSP NOTIF CODES */
65 enum
66 {
67 	NC_RTSP_Continue		=		100,
68 	NC_RTSP_OK				=		200,
69 	NC_RTSP_Created			=		201,
70 	NC_RTSP_Low_on_Storage_Space	=	250,
71 
72 	NC_RTSP_Multiple_Choice	=	300,
73 	NC_RTSP_Moved_Permanently	=	301,
74 	NC_RTSP_Moved_Temporarily	=	302,
75 	NC_RTSP_See_Other	=	303,
76 	NC_RTSP_Use_Proxy	=	305,
77 
78 	NC_RTSP_Bad_Request	=	400,
79 	NC_RTSP_Unauthorized	=	401,
80 	NC_RTSP_Payment_Required	=	402,
81 	NC_RTSP_Forbidden	=	403,
82 	NC_RTSP_Not_Found	=	404,
83 	NC_RTSP_Method_Not_Allowed	=	405,
84 	NC_RTSP_Not_Acceptable	=	406,
85 	NC_RTSP_Proxy_Authentication_Required	=	407,
86 	NC_RTSP_Request_Timeout	=	408,
87 	NC_RTSP_Gone	=	410,
88 	NC_RTSP_Length_Required	=	411,
89 	NC_RTSP_Precondition_Failed	=	412,
90 	NC_RTSP_Request_Entity_Too_Large	=	413,
91 	NC_RTSP_Request_URI_Too_Long	=	414,
92 	NC_RTSP_Unsupported_Media_Type	=	415,
93 
94 	NC_RTSP_Invalid_parameter	=	451,
95 	NC_RTSP_Illegal_Conference_Identifier	=	452,
96 	NC_RTSP_Not_Enough_Bandwidth	=	453,
97 	NC_RTSP_Session_Not_Found	=	454,
98 	NC_RTSP_Method_Not_Valid_In_This_State	=	455,
99 	NC_RTSP_Header_Field_Not_Valid	=	456,
100 	NC_RTSP_Invalid_Range	=	457,
101 	NC_RTSP_Parameter_Is_ReadOnly	=	458,
102 	NC_RTSP_Aggregate_Operation_Not_Allowed	=	459,
103 	NC_RTSP_Only_Aggregate_Operation_Allowed	=	460,
104 	NC_RTSP_Unsupported_Transport	=	461,
105 	NC_RTSP_Destination_Unreachable	=	462,
106 
107 	NC_RTSP_Internal_Server_Error	=	500,
108 	NC_RTSP_Not_Implemented	=	501,
109 	NC_RTSP_Bad_Gateway	=	502,
110 	NC_RTSP_Service_Unavailable	=	503,
111 	NC_RTSP_Gateway_Timeout	=	504,
112 	NC_RTSP_RTSP_Version_Not_Supported	=	505,
113 
114 	NC_RTSP_Option_not_support	=	551,
115 };
116 
117 /*! Gives string descritpion of error code
118 \param ErrCode the RTSP error code
119 \return the description of the RTSP error code
120 */
121 const char *gf_rtsp_nc_to_string(u32 ErrCode);
122 
123 /*
124 		Common structures between commands and responses
125 */
126 
127 /*! RTSP Range information
128  	RTSP Session level only, although this is almost the same
129 	format as an SDP range, this is not used in the SDP lib as "a=range" is not part of SDP
130 	but part of RTSP
131 */
132 typedef struct {
133 	/* start and end range. If end is -1, the range is open (from start to unknown) */
134 	Double start, end;
135 	/* use SMPTE range (Start and End specify the number of frames) (currently not supported) */
136 	u32 UseSMPTE;
137 	/* framerate for SMPTE range */
138 	Double FPS;
139 } GF_RTSPRange;
140 
141 /*! parses a Range line and returns range header structure. This can be used for RTSP extension of SDP
142 Note: Only support for npt for now
143 \param range_buf the range string
144 \return a newly allocated RTSP range
145 */
146 GF_RTSPRange *gf_rtsp_range_parse(char *range_buf);
147 /*! creates a new RTSP range
148 \return a newly allocated RTSP range
149 */
150 GF_RTSPRange *gf_rtsp_range_new();
151 /*! destroys a RTSP range
152 \param range the target RTSP range
153 */
154 void gf_rtsp_range_del(GF_RTSPRange *range);
155 
156 /*
157 			Transport structure
158 		contains all network info for RTSP sessions (ports, uni/multi-cast, ...)
159 */
160 
161 /*! RTSP AVP Transport Profile */
162 #define GF_RTSP_PROFILE_RTP_AVP			"RTP/AVP"
163 /*! RTSP AVP + TCP Transport Profile */
164 #define GF_RTSP_PROFILE_RTP_AVP_TCP		"RTP/AVP/TCP"
165 /*! RTSP UDP Transport Profile */
166 #define GF_RTSP_PROFILE_UDP				"udp"
167 
168 /*! RTSP transport structure*/
169 typedef struct
170 {
171 	/* set to 1 if unicast */
172 	Bool IsUnicast;
173 	/* for multicast */
174 	char *destination;
175 	/* for redirections internal to servers */
176 	char *source;
177 	/*IsRecord is usually 0 (PLAY) . If set, Append specify that the stream should
178 	be concatenated to existing resources */
179 	Bool IsRecord, Append;
180 	/* in case transport is on TCP/RTSP, If only 1 ID is specified, it is stored in rtpID (this
181 	is not RTP interleaving) */
182 	Bool IsInterleaved;
183 	u32 rtpID, rtcpID;
184 	/* Multicast specific */
185 	u32 MulticastLayers;
186 	u8 TTL;
187 	/*RTP specific*/
188 
189 	/*port for multicast*/
190 	/*server port in unicast - RTP implies low is even , and last is low+1*/
191 	u16 port_first, port_last;
192 	/*client port in unicast - RTP implies low is even , and last is low+1*/
193 	u16 client_port_first, client_port_last;
194 	u32 SSRC;
195 
196 	/*Transport protocol. In this version we only support RTP/AVP, the following flag tells
197 	us if this is RTP/AVP/TCP or RTP/AVP (default)*/
198 	char *Profile;
199 
200 	Bool is_sender;
201 } GF_RTSPTransport;
202 
203 /*! clones a RTSP transport
204 \param transp the target RTSP transport
205 \return an allocated copy RTSP transport
206 */
207 GF_RTSPTransport *gf_rtsp_transport_clone(GF_RTSPTransport *transp);
208 /*! destroys a RTSP transport
209 \param transp the target RTSP transport
210 */
211 void gf_rtsp_transport_del(GF_RTSPTransport *transp);
212 
213 
214 /*
215 				RTSP Command
216 		the RTSP Response is sent by a client / received by a server
217 	text Allocation is done by the lib when parsing a command, and
218 	is automatically freed when calling reset / delete. Therefore you must
219 	set/allocate the fields yourself when writing a command (client)
220 
221 */
222 
223 /*ALL RTSP METHODS - all other methods will be ignored*/
224 /*! RTSP DESCRIBE method*/
225 #define GF_RTSP_DESCRIBE		"DESCRIBE"
226 /*! RTSP SETUP method*/
227 #define GF_RTSP_SETUP			"SETUP"
228 /*! RTSP PLAY method*/
229 #define GF_RTSP_PLAY			"PLAY"
230 /*! RTSP PAUSE method*/
231 #define GF_RTSP_PAUSE			"PAUSE"
232 /*! RTSP RECORD method*/
233 #define GF_RTSP_RECORD			"RECORD"
234 /*! RTSP TEARDOWN method*/
235 #define GF_RTSP_TEARDOWN		"TEARDOWN"
236 /*! RTSP GET_PARAMETER method*/
237 #define GF_RTSP_GET_PARAMETER	"GET_PARAMETER"
238 /*! RTSP SET_PARAMETER method*/
239 #define GF_RTSP_SET_PARAMETER	"SET_PARAMETER"
240 /*! RTSP OPTIONS method*/
241 #define GF_RTSP_OPTIONS			"OPTIONS"
242 /*! RTSP ANNOUCE method*/
243 #define GF_RTSP_ANNOUNCE		"ANNOUNCE"
244 /*! RTSP REDIRECT method*/
245 #define GF_RTSP_REDIRECT		"REDIRECT"
246 
247 /*! RTSP command structure*/
248 typedef struct
249 {
250 	char *Accept;
251 	char *Accept_Encoding;
252 	char *Accept_Language;
253 	char *Authorization;
254 	u32 Bandwidth;
255 	u32 Blocksize;
256 	char *Cache_Control;
257 	char *Conference;
258 	char *Connection;
259 	u32 Content_Length;
260 	u32 CSeq;
261 	char *From;
262 	char *Proxy_Authorization;
263 	char *Proxy_Require;
264 	GF_RTSPRange *Range;
265 	char *Referer;
266 	Double Scale;
267 	char *Session;
268 	Double Speed;
269 	/*nota : RTSP allows several configurations for a single channel (multicast and
270 	unicast , ...). Usually only 1*/
271 	GF_List *Transports;
272 	char *User_Agent;
273 
274 	/*type of the command, one of the described above*/
275 	char *method;
276 
277 	/*Header extensions*/
278 	GF_List *Xtensions;
279 
280 	/*body of the command, size is Content-Length (auto computed when sent). It is not
281 	terminated by a NULL char*/
282 	char *body;
283 
284 	/*
285 			Specify ControlString if your request targets
286 		a specific media stream in the service. If null, the service name only will be used
287 		for control (for ex, both A and V streams in a single file)
288 		If the request is GF_RTSP_OPTIONS, you must provide a control string containing the options
289 		you want to query
290 	*/
291 	char *ControlString;
292 
293 	/*user data: this is never touched by the lib, its intend is to help stacking
294 	RTSP commands in your app*/
295 	void *user_data;
296 
297 
298 	/*
299 		Server side Extensions
300 	*/
301 
302 	/*full URL of the command. Not used at client side, as the URL is ALWAYS relative
303 	to the server / service of the RTSP session
304 	On the server side however redirections are up to the server, so we cannot decide for it	*/
305 	char *service_name;
306 	/*RTSP status code of the command as parsed. One of the above RTSP StatusCode*/
307 	u32 StatusCode;
308 } GF_RTSPCommand;
309 
310 /*! creates an RTSP command
311 \return the newly allocated RTSP command
312 */
313 GF_RTSPCommand *gf_rtsp_command_new();
314 /*! destroys an RTSP command
315 \param com the target RTSP command
316 */
317 void gf_rtsp_command_del(GF_RTSPCommand *com);
318 /*! resets an RTSP command
319 \param com the target RTSP command
320 */
321 void gf_rtsp_command_reset(GF_RTSPCommand *com);
322 
323 
324 
325 /*
326 				RTSP Response
327 		the RTSP Response is received by a client / sent by a server
328 	text Allocation is done by the lib when parsing a response, and
329 	is automatically freed when calling reset / delete. Therefore you must
330 	allocate the fields yourself when writing a response (server)
331 
332 */
333 
334 /*! RTP-Info for RTP channels.
335 
336 	There may be several RTP-Infos in one response
337 	based on the server implementation (DSS/QTSS begaves this way)
338 */
339 typedef struct
340 {
341 	/*control string of the channel*/
342 	char *url;
343 	/*seq num for asociated rtp_time*/
344 	u32 seq;
345 	/*rtp TimeStamp corresponding to the Range start specified in the PLAY request*/
346 	u32 rtp_time;
347 	/*ssrc of sender if known, 0 otherwise*/
348 	u32 ssrc;
349 } GF_RTPInfo;
350 
351 
352 
353 /*! RTSP Response */
354 typedef struct
355 {
356 	/* response code*/
357 	u32 ResponseCode;
358 	/* comment from the server */
359 	char *ResponseInfo;
360 
361 	/*	Header Fields	*/
362 	char *Accept;
363 	char *Accept_Encoding;
364 	char *Accept_Language;
365 	char *Allow;
366 	char *Authorization;
367 	u32 Bandwidth;
368 	u32 Blocksize;
369 	char *Cache_Control;
370 	char *Conference;
371 	char *Connection;
372 	char *Content_Base;
373 	char *Content_Encoding;
374 	char *Content_Language;
375 	u32 Content_Length;
376 	char *Content_Location;
377 	char *Content_Type;
378 	u32 CSeq;
379 	char *Date;
380 	char *Expires;
381 	char *From;
382 	char *Host;
383 	char *If_Match;
384 	char *If_Modified_Since;
385 	char *Last_Modified;
386 	char *Location;
387 	char *Proxy_Authenticate;
388 	char *Proxy_Require;
389 	char *Public;
390 	GF_RTSPRange *Range;
391 	char *Referer;
392 	char *Require;
393 	char *Retry_After;
394 	GF_List *RTP_Infos;
395 	Double Scale;
396 	char *Server;
397 	char *Session;
398 	u32 SessionTimeOut;
399 	Double Speed;
400 	u32 StreamID; //only when sess->satip is true
401 	char *Timestamp;
402 	/*nota : RTSP allows several configurations for a single channel (multicast and
403 	unicast , ...). Usually only 1*/
404 	GF_List *Transports;
405 	char *Unsupported;
406 	char *User_Agent;
407 	char *Vary;
408 	char *Via;
409 	char *WWW_Authenticate;
410 
411 	/*Header extensions*/
412 	GF_List *Xtensions;
413 
414 	/*body of the response, size is Content-Length (auto computed when sent). It is not
415 	terminated by a NULL char when response is parsed but must be null-terminated when
416 	response is being sent*/
417 	char *body;
418 } GF_RTSPResponse;
419 
420 /*! creates an RTSP response
421 \return the newly allocated RTSP response
422 */
423 GF_RTSPResponse *gf_rtsp_response_new();
424 /*! deletes an RTSP response
425 \param rsp the target RTSP response
426 */
427 void gf_rtsp_response_del(GF_RTSPResponse *rsp);
428 /*! resets an RTSP response
429 \param rsp the target RTSP response
430 */
431 void gf_rtsp_response_reset(GF_RTSPResponse *rsp);
432 
433 
434 /*! RTSP session*/
435 typedef struct _tag_rtsp_session GF_RTSPSession;
436 
437 /*! creates a new RTSP session
438 \param sURL the target RTSP session URL
439 \param DefaultPort the target RTSP session port
440 \return a newly allocated RTSP session
441 */
442 GF_RTSPSession *gf_rtsp_session_new(char *sURL, u16 DefaultPort);
443 /*! destroys an RTSP session
444 \param sess the target RTSP session
445 */
446 void gf_rtsp_session_del(GF_RTSPSession *sess);
447 
448 /*! sets TCP buffer size of an RTSP session
449 \param sess the target RTSP session
450 \param BufferSize desired buffer size in bytes
451 \return error if any
452 */
453 GF_Err gf_rtsp_set_buffer_size(GF_RTSPSession *sess, u32 BufferSize);
454 
455 
456 /*! resets state machine, invalidate SessionID
457 NOTE: RFC2326 requires that the session is reseted when all RTP streams
458 are closed. As this lib doesn't maintain the number of valid streams
459 you MUST call reset when all your streams are shutdown (either requested through
460 TEARDOWN or signaled through RTCP BYE packets for RTP, or any other signaling means
461 for other protocols)
462 \param sess the target RTSP session
463 \param ResetConnection if set, this will destroy the associated TCP socket. This is useful in case of timeouts, because
464 some servers do not restart with the right CSeq.
465 */
466 void gf_rtsp_session_reset(GF_RTSPSession *sess, Bool ResetConnection);
467 
468 /*! checks if an RTSP session matches an RTSP URL
469 \param sess the target RTSP session
470 \param url the URL to test
471 \return GF_TRUE if the session matches the URL, GF_FALSE otherwise
472 */
473 Bool gf_rtsp_is_my_session(GF_RTSPSession *sess, char *url);
474 
475 /*! gets server name of an RTSP session
476 \param sess the target RTSP session
477 \return the server name
478 */
479 char *gf_rtsp_get_server_name(GF_RTSPSession *sess);
480 /*! gets server port of an RTSP session
481 \param sess the target RTSP session
482 \return the server port
483 */
484 u16 gf_rtsp_get_session_port(GF_RTSPSession *sess);
485 
486 /*! fetches an RTSP response from the server.  the GF_RTSPResponse will be reseted before fetch
487 \param sess the target RTSP session
488 \param rsp the response object to fill with the response. This will be reseted before TCP fetch
489 \return error if any
490 */
491 GF_Err gf_rtsp_get_response(GF_RTSPSession *sess, GF_RTSPResponse *rsp);
492 
493 
494 /*! RTSP States. The only non blocking mode is GF_RTSP_STATE_WAIT_FOR_CONTROL*/
495 enum
496 {
497 	/*! Initialized (connection might be off, but all structures are in place)
498 	This is the default state between # requests (aka, DESCRIBE and SETUP
499 	or SETUP and PLAY ...)*/
500 	GF_RTSP_STATE_INIT	=	0,
501 	/*! Waiting*/
502 	GF_RTSP_STATE_WAITING,
503 	/*! PLAY, PAUSE, RECORD. Aggregation is allowed for the same type, you can send several command
504 	in a row. However the session will return GF_SERVICE_ERROR if you do not have
505 	a valid SessionID in the command
506 	You cannot issue a SETUP / DESCRIBE while in this state*/
507 	GF_RTSP_STATE_WAIT_FOR_CONTROL,
508 
509 	/*! FATAL ERROR: session is invalidated by server. Call reset and restart from SETUP if needed*/
510 	GF_RTSP_STATE_INVALIDATED
511 };
512 
513 /*! gets the RTSP session state
514 \param sess the target RTSP session
515 \return the session state
516 */
517 u32 gf_rtsp_get_session_state(GF_RTSPSession *sess);
518 /*! forces a reset of the state to GF_RTSP_STATE_INIT
519 \param sess the target RTSP session
520 */
521 void gf_rtsp_reset_aggregation(GF_RTSPSession *sess);
522 
523 /*! sends an RTSP request to the server.
524 \param sess the target RTSP session
525 \param com the RTSP command to send
526 \return error if any
527 */
528 GF_Err gf_rtsp_send_command(GF_RTSPSession *sess, GF_RTSPCommand *com);
529 
530 
531 /*! callback function for interleaved RTSP/TCP transport
532 \param sess the target RTSP session
533 \param cbk_ptr opaque data
534 \param buffer RTP or RTCP packet
535 \param bufferSize packet size in bytes
536 \param IsRTCP set to GF_TRUE if the packet is an RTCP packet, GF_FALSE otherwise
537 \return error if any
538 */
539 typedef GF_Err (*gf_rtsp_interleave_callback)(GF_RTSPSession *sess, void *cbk_ptr, u8 *buffer, u32 bufferSize, Bool IsRTCP);
540 
541 /*! assigns the callback function for interleaved RTSP/TCP transport
542 \param sess the target RTSP session
543 \param SignalData the callback function on each interleaved packet
544 \return error if any
545 */
546 GF_Err gf_rtsp_set_interleave_callback(GF_RTSPSession *sess, gf_rtsp_interleave_callback SignalData);
547 
548 /*! reads RTSP session (response fetch and interleaved RTSP/TCP transport)
549 \param sess the target RTSP session
550 \return error if any
551 */
552 GF_Err gf_rtsp_session_read(GF_RTSPSession *sess);
553 
554 /*! registers a new interleaved RTP channel over an RTSP connection
555 \param sess the target RTSP session
556 \param the_ch opaque data passed to \ref gf_rtsp_interleave_callback
557 \param LowInterID ID of the RTP interleave channel
558 \param HighInterID ID of the RCTP interleave channel
559 \return error if any
560 */
561 GF_Err gf_rtsp_register_interleave(GF_RTSPSession *sess, void *the_ch, u8 LowInterID, u8 HighInterID);
562 /*! unregisters a new interleaved RTP channel over an RTSP connection
563 \param sess the target RTSP session
564 \param LowInterID ID of the RTP interleave channel
565 \return the numbers of registered interleaved channels remaining
566 */
567 u32 gf_rtsp_unregister_interleave(GF_RTSPSession *sess, u8 LowInterID);
568 
569 
570 /*! creates a new RTSP session from an existing socket in listen state. If no pending connection
571 	is detected, return NULL
572 \param rtsp_listener the listening server socket
573 \return the newly allocated RTSP session if any, NULL otherwise
574 */
575 GF_RTSPSession *gf_rtsp_session_new_server(GF_Socket *rtsp_listener);
576 
577 /*! fetches an RTSP request
578 \param sess the target RTSP session
579 \param com the RTSP command to fill with the command. This will be reseted before fetch
580 \return error if any
581 */
582 GF_Err gf_rtsp_get_command(GF_RTSPSession *sess, GF_RTSPCommand *com);
583 
584 /*! generates a session ID for the given session
585 \param sess the target RTSP session
586 \return an allocated string containing a session ID
587 */
588 char *gf_rtsp_generate_session_id(GF_RTSPSession *sess);
589 
590 /*! sends an RTSP response
591 \param sess the target RTSP session
592 \param rsp the response to send
593 \return error if any
594 */
595 GF_Err gf_rtsp_send_response(GF_RTSPSession *sess, GF_RTSPResponse *rsp);
596 
597 /*! gets the IP address of the local host running the session
598 \param sess the target RTSP session
599 \param buffer buffer to store the local host name
600 \return error if any
601 */
602 GF_Err gf_rtsp_get_session_ip(GF_RTSPSession *sess, char buffer[GF_MAX_IP_NAME_LEN]);
603 
604 /*! gets the IP address of the connected peer
605 \param sess the target RTSP session
606 \param buffer buffer to store the connected peer name
607 \return error if any
608 */
609 GF_Err gf_rtsp_get_remote_address(GF_RTSPSession *sess, char *buffer);
610 
611 /*! writes a packet on an interleaved channel over RTSP
612 \param sess the target RTSP session
613 \param idx ID (RTP or RTCP) of the interleaved channel
614 \param pck packet data (RTP or RTCP packet) to write
615 \param pck_size packet size in bytes
616 \return error if any
617 */
618 GF_Err gf_rtsp_session_write_interleaved(GF_RTSPSession *sess, u32 idx, u8 *pck, u32 pck_size);
619 
620 /*
621 		RTP LIB EXPORTS
622 */
623 
624 /*! RTP header */
625 typedef struct tagRTP_HEADER {
626 	/*version, must be 2*/
627 	u8 Version;
628 	/*padding bits in the payload*/
629 	u8 Padding;
630 	/*header extension is defined*/
631 	u8 Extension;
632 	/*number of CSRC (<=15)*/
633 	u8 CSRCCount;
634 	/*Marker Bit*/
635 	u8 Marker;
636 	/*payload type on 7 bits*/
637 	u8 PayloadType;
638 	/*packet seq number*/
639 	u16 SequenceNumber;
640 	/*packet time stamp*/
641 	u32 TimeStamp;
642 	/*sync source identifier*/
643 	u32 SSRC;
644 	/*in our basic client, CSRC should always be NULL*/
645 	u32 CSRC[16];
646 
647 	/*internal to out lib*/
648 	u64 recomputed_ntp_ts;
649 } GF_RTPHeader;
650 
651 
652 /*! RTPMap information*/
653 typedef struct
654 {
655 	/*dynamic payload type of this map*/
656 	u32 PayloadType;
657 	/*registered payload name of this map*/
658 	char *payload_name;
659 	/*RTP clock rate (TS resolution) of this map*/
660 	u32 ClockRate;
661 	/*optional parameters for audio, specifying number of channels. Unused for other media types.*/
662 	u32 AudioChannels;
663 } GF_RTPMap;
664 
665 
666 /*! RTP channel*/
667 typedef struct __tag_rtp_channel GF_RTPChannel;
668 
669 /*! creates a new RTP channel
670 \return a newly allocated RTP channel
671 */
672 GF_RTPChannel *gf_rtp_new();
673 /*! destroys an RTP channel
674 \param ch the target RTP channel
675 */
676 void gf_rtp_del(GF_RTPChannel *ch);
677 
678 /*! setup transport for an RTP channel
679 A server channelis configured through the transport structure, with the same info as a
680 client channel, the client_port_* info designing the REMOTE client and port_* designing
681 the server channel
682 \param ch the target RTP channel
683 \param trans_info the transport info for this channel
684 \param remote_address the remote / destination address of the channel
685 \return error if any
686 */
687 GF_Err gf_rtp_setup_transport(GF_RTPChannel *ch, GF_RTSPTransport *trans_info, const char *remote_address);
688 
689 /*! setup of rtp/rtcp transport ports
690 This only applies in unicast, non interleaved cases.
691 For multicast port setup MUST be done through the above gf_rtp_setup_transport function
692 This will take care of port reuse
693 \param ch the target RTP channel
694 \param first_port RTP port number of the RTP channel
695 \return error if any
696 */
697 GF_Err gf_rtp_set_ports(GF_RTPChannel *ch, u16 first_port);
698 
699 /*! init of RTP payload information. Only ONE payload per sync source is supported in this
700 version of the library (a sender cannot switch payload types on a single media)
701 \param ch the target RTP channel
702 \param PayloadType identifier of RTP payload
703 \param ClockRate clock rate in (1/Hz) of RTP channel
704 \return error if any
705 */
706 GF_Err gf_rtp_setup_payload(GF_RTPChannel *ch, u32 PayloadType, u32 ClockRate);
707 
708 /*! enables sending of NAT keep-alive packets for NAT traversal
709 \param ch the target RTP channel
710 \param nat_timeout specifies the inactivity period in ms after which NAT keepalive packets are sent. If 0, disables NAT keep-alive packets
711 */
712 void gf_rtp_enable_nat_keepalive(GF_RTPChannel *ch, u32 nat_timeout);
713 
714 
715 /*! initializes the RTP channel.
716 \param ch the target RTP channel
717 \param UDPBufferSize UDP stack buffer size if configurable by OS/ISP - ignored otherwise
718 NOTE: on WinCE devices, this is not configurable on an app bases but for the whole OS
719 you must update the device registry with:
720 \code
721 	[HKEY_LOCAL_MACHINE\Comm\Afd]
722 	DgramBuffer=dword:N
723 \endcode
724 
725 	where N is the number of UDP datagrams a socket should be able to buffer. For multimedia
726 app you should set N as large as possible. The device MUST be reseted for the param to take effect
727 
728 \param IsSource if true, the channel is a sender (media data, sender report, Receiver report processing)
729 if source, you must specify the Path MTU size. The RTP lib won't send any packet bigger than this size
730 your application shall perform payload size splitting if needed
731 \param PathMTU desired path MTU (max packet size) in bytes
732 \param ReorederingSize max number of packets to queue for reordering. 0 means no reordering
733 \param MaxReorderDelay max time to wait in ms before releasing first packet in reoderer when only one packet is present.
734 If 0 and reordering size is specified, defaults to 200 ms (usually enough).
735 
736 \param local_interface_ip local interface address to use for multicast. If NULL, default address is used
737 \return error if any
738 */
739 GF_Err gf_rtp_initialize(GF_RTPChannel *ch, u32 UDPBufferSize, Bool IsSource, u32 PathMTU, u32 ReorederingSize, u32 MaxReorderDelay, char *local_interface_ip);
740 
741 /*! stops the RTP channel. This destrpys RTP and RTCP sockets as well as packet reorderer
742 \param ch the target RTP channel
743 \return error if any
744 */
745 GF_Err gf_rtp_stop(GF_RTPChannel *ch);
746 
747 /*! inits the RTP info after a PLAY or PAUSE, rtp_time is the rtp TimeStamp of the RTP packet
748 with seq_num sequence number. This info is needed to compute the CurrentTime of the RTP channel
749 ssrc may not be known if sender hasn't indicated it (use 0 then)
750 \param ch the target RTP channel
751 \param seq_num the seq num of the next packet to be received
752 \param rtp_time the time in RTP timescale of the next packet to be received
753 \param ssrc the SSRC identifier of the next packet to be received
754 \return error if any
755 */
756 GF_Err gf_rtp_set_info_rtp(GF_RTPChannel *ch, u32 seq_num, u32 rtp_time, u32 ssrc);
757 
758 /*! retrieves current RTP time in sec. If rtp_time was unknown (not on demand media) the time is absolute.
759 Otherwise this is the time in ms elapsed since the last PLAY range start value
760 Not supported yet if played without RTSP (aka RTCP time not supported)
761 \param ch the target RTP channel
762 \return NTP clock in seconds
763 */
764 Double gf_rtp_get_current_time(GF_RTPChannel *ch);
765 
766 /*! resets both sockets and packet reorderer
767 \param ch the target RTP channel
768 */
769 void gf_rtp_reset_buffers(GF_RTPChannel *ch);
770 
771 /*! resets sender SSRC
772 \param ch the target RTP channel
773 */
774 void gf_rtp_reset_ssrc(GF_RTPChannel *ch);
775 
776 /*! reads any RTP data on UDP only (not valid for TCP). Performs re-ordering if configured for it
777 \param ch the target RTP channel
778 \param buffer the buffer where to store the RTP packet
779 \param buffer_size the size of the buffer
780 \return amount of data read in bytes
781 */
782 u32 gf_rtp_read_rtp(GF_RTPChannel *ch, u8 *buffer, u32 buffer_size);
783 
784 /*! flushes any pending data in packet reorderer, but does not flush packet reorderer if reorderer timeout is not exceeded
785 \param ch the target RTP channel
786 \param buffer the buffer where to store the data
787 \param buffer_size the size of the buffer
788 \return amount of data read in bytes
789 */
790 u32 gf_rtp_flush_rtp(GF_RTPChannel *ch, u8 *buffer, u32 buffer_size);
791 
792 /*! reads any RTCP data on UDP only (not valid for TCP). Performs re-ordering if configured for it
793 \param ch the target RTP channel
794 \param buffer the buffer where to store the RTCP packet
795 \param buffer_size the size of the buffer
796 \return amount of data read in bytes
797 */
798 u32 gf_rtp_read_rtcp(GF_RTPChannel *ch, u8 *buffer, u32 buffer_size);
799 
800 /*! flushes any pending data in packet reorderer, and flushes packet reorderer if reorderer timeout is not exceeded. Typically called several times until returning 0.
801 \param ch the target RTP channel
802 \param buffer the buffer where to store the data
803 \param buffer_size the size of the buffer
804 \return amount of data read in bytes
805 */
806 u32 gf_rtp_read_flush(GF_RTPChannel *ch, u8 *buffer, u32 buffer_size);
807 
808 /*! decodes an RTP packet and gets the beginning of the RTP payload
809 \param ch the target RTP channel
810 \param pck the RTP packet buffer
811 \param pck_size the size of the RTP packet
812 \param rtp_hdr filled with decoded RTP header information
813 \param PayloadStart set to the offset in bytes of the start of the payload in the RTP packet
814 \return error if any
815 */
816 GF_Err gf_rtp_decode_rtp(GF_RTPChannel *ch, u8 *pck, u32 pck_size, GF_RTPHeader *rtp_hdr, u32 *PayloadStart);
817 
818 /*! decodes an RTCP packet and update timing info, send ReceiverReport too
819 \param ch the target RTP channel
820 \param pck the RTP packet buffer
821 \param pck_size the size of the RTP packet
822 \param has_sr set to GF_TRUE if the RTCP packet contained an SenderReport
823 \return error if any
824 */
825 GF_Err gf_rtp_decode_rtcp(GF_RTPChannel *ch, u8 *pck, u32 pck_size, Bool *has_sr);
826 
827 /*! computes and send Receiver report.
828 If the channel is a TCP channel, you must specify
829 the callback function. NOTE: many RTP implementation do NOT process RTCP info received on TCP...
830 the lib will decide whether the report shall be sent or not, therefore you should call
831 this function at regular times
832 \param ch the target RTP channel
833 \return error if any
834 */
835 GF_Err gf_rtp_send_rtcp_report(GF_RTPChannel *ch);
836 
837 /*! sends a BYE info (leaving the session)
838 \param ch the target RTP channel
839 \return error if any
840 */
841 GF_Err gf_rtp_send_bye(GF_RTPChannel *ch);
842 
843 
844 /*! sends an RTP packet. In fast_send mode,
845 \param ch the target RTP channel
846 \param rtp_hdr the RTP header of the packet
847 \param pck the RTP payload buffer
848 \param pck_size the RTP payload size
849 \param fast_send if set, the payload buffer contains 12 bytes available BEFORE its indicated start where the RTP header is written in place
850 \return error if any
851 */
852 GF_Err gf_rtp_send_packet(GF_RTPChannel *ch, GF_RTPHeader *rtp_hdr, u8 *pck, u32 pck_size, Bool fast_send);
853 
854 
855 /*! callback used for writing rtp over TCP
856 \param cbk1 opaque user data
857 \param cbk2 opaque user data
858 \param is_rtcp indicates the data is an RTCP packet
859 \param pck the RTP/RTCP buffer
860 \param pck_size the RTP/RTCP size
861 \return error if any
862 */
863 typedef GF_Err (*gf_rtp_tcp_callback)(void *cbk1, void *cbk2, Bool is_rtcp, u8 *pck, u32 pck_size);
864 
865 /*! sets RTP interleaved callback on the RTP channel
866 \param ch the target RTP channel
867 \param tcp_callback the callback function
868 \param cbk1 user data for the callback function
869 \param cbk2 user data for the callback function
870 \return error if any
871 */
872 GF_Err gf_rtp_set_interleave_callbacks(GF_RTPChannel *ch, gf_rtp_tcp_callback tcp_callback, void *cbk1, void *cbk2);
873 
874 /*! checks if an RTP channel is unicast
875 \param ch the target RTP channel
876 \return GF_TRUE if unicast, GF_FALSE otherwise
877 */
878 u32 gf_rtp_is_unicast(GF_RTPChannel *ch);
879 /*! checks if an RTP channel is interleaved
880 \param ch the target RTP channel
881 \return GF_TRUE if interleaved, GF_FALSE otherwise
882 */
883 u32 gf_rtp_is_interleaved(GF_RTPChannel *ch);
884 /*! gets clockrate/timescale of an RTP channel
885 \param ch the target RTP channel
886 \return the RTP clock rate
887 */
888 u32 gf_rtp_get_clockrate(GF_RTPChannel *ch);
889 /*! gets the low interleave ID of an RTP channel
890 \param ch the target RTP channel
891 \return the low (RTP) interleave ID of the channel, or 0 if not interleaved
892 */
893 u8 gf_rtp_get_low_interleave_id(GF_RTPChannel *ch);
894 /*! gets the high interleave ID of an RTP channel
895 \param ch the target RTP channel
896 \return the high (RTCP) interleave ID of the channel, or 0 if not interleaved
897 */
898 u8 gf_rtp_get_hight_interleave_id(GF_RTPChannel *ch);
899 /*! gets the transport associated with an RTP channel
900 \param ch the target RTP channel
901 \return the RTSP transport information
902 */
903 const GF_RTSPTransport *gf_rtp_get_transport(GF_RTPChannel *ch);
904 
905 /*! gets loss rate of the RTP channel
906 \param ch the target RTP channel
907 \return the loss rate of the channel, between 0 and 100 %
908 */
909 Float gf_rtp_get_loss(GF_RTPChannel *ch);
910 /*! gets number of TCP bytes send for an interleaved channel
911 \param ch the target RTP channel
912 \return the number of bytes sent
913 */
914 u32 gf_rtp_get_tcp_bytes_sent(GF_RTPChannel *ch);
915 /*! gets ports of an non-interleaved RTP channel
916 \param ch the target RTP channel
917 \param rtp_port the RTP port number
918 \param rtcp_port the RCTP port number
919 */
920 void gf_rtp_get_ports(GF_RTPChannel *ch, u16 *rtp_port, u16 *rtcp_port);
921 
922 
923 
924 /****************************************************************************
925 
926 					SDP LIBRARY EXPORTS
927 
928 		  Note: SDP is mainly a text protocol with
929 	well defined containers. The following structures are used to write / read
930 	SDP informations, and the library also provides consistency checking
931 
932   When reading SDP, all text items/structures are allocated by the lib, and you
933   must call gf_sdp_info_reset(GF_SDPInfo *sdp) or gf_sdp_info_del(GF_SDPInfo *sdp) to release the memory
934 
935   When writing the SDP from a GF_SDPInfo, the output buffer is allocated by the library,
936   and you must release it yourself
937 
938   Some quick constructors are available for GF_SDPConnection and GF_SDPMedia in order to set up
939   some specific parameters to their default value
940 ****************************************************************************/
941 
942 /*! Extension Attribute
943 
944 	All attributes x-ZZZZ are considered as extensions attributes. If no "x-" is found
945 	the attributes in the RTSP response is SKIPPED. The "x-" radical is removed in the structure
946 	when parsing commands / responses
947 */
948 typedef struct
949 {
950 	char *Name;
951 	char *Value;
952 } GF_X_Attribute;
953 
954 
955 /*! SDP bandwidth info*/
956 typedef struct
957 {
958 	/*"CT", "AS" are defined. Private extensions must be "X-*" ( * "are recommended to be short")*/
959 	char *name;
960 	/*in kBitsPerSec*/
961 	u32 value;
962 } GF_SDPBandwidth;
963 
964 /*! SDP maximum time offset
965 We do not support more than this offset / zone adjustment
966 if more are needed, RFC recommends to use several entries rather than a big offset*/
967 #define GF_SDP_MAX_TIMEOFFSET	10
968 
969 /*! SDP Timing information*/
970 typedef struct
971 {
972 	/*NPT time in sec*/
973 	u32 StartTime;
974 	/*if 0, session is unbound. NPT time in sec*/
975 	u32 StopTime;
976 	/*if 0 session is not repeated. Expressed in sec.
977 	Session is signaled repeated every repeatInterval*/
978 	u32 RepeatInterval;
979 	/*active duration of the session in sec*/
980 	u32 ActiveDuration;
981 
982 	/*time offsets to use with repeat. Specify a non-regular repeat time from the Start time*/
983 	u32 OffsetFromStart[GF_SDP_MAX_TIMEOFFSET];
984 	/*Number of offsets*/
985 	u32 NbRepeatOffsets;
986 
987 	/*EX of repeat:
988 	a session happens 3 times a week, on mon 1PM, thu 3PM and fri 10AM
989 	1- StartTime should be NPT for the session on the very first monday, StopTime
990 	the end of this session
991 	2- the repeatInterval should be 1 week, ActiveDuration the length of the session
992 	3- 3 offsets: 0 (for monday) (3*24+2)*3600 for thu and (4*24-3) for fri
993 	*/
994 
995 
996 	/*timezone adjustments, to cope with #timezones, daylight saving countries and co ...
997 	Ex: adjTime = [2882844526 2898848070] adjOffset=[-1h 0]
998 	[0]: at 2882844526 the time base by which the session's repeat times are calculated
999 	is shifted back by 1 hour
1000 	[1]: at time 2898848070 the session's original time base is restored
1001 	*/
1002 
1003 	/*Adjustment time at which the corresponding time offset is to be applied to the
1004 	session time line (time used to compute the "repeat session").
1005 	All Expressed in NPT*/
1006 	u32 AdjustmentTime[GF_SDP_MAX_TIMEOFFSET];
1007 	/* Offset with the session time line, ALWAYS ABSOLUTE OFFSET TO the specified StartTime*/
1008 	s32 AdjustmentOffset[GF_SDP_MAX_TIMEOFFSET];
1009 	/*Number of offsets.*/
1010 	u32 NbZoneOffsets;
1011 } GF_SDPTiming;
1012 
1013 /*! SDP Connection information*/
1014 typedef struct
1015 {
1016 	/*only "IN" currently defined*/
1017 	char *net_type;
1018 	/*"IP4","IP6"*/
1019 	char *add_type;
1020 	/*hex IPv6 address or doted IPv4 address*/
1021 	char *host;
1022 	/*TTL - MUST BE PRESENT if IP is multicast - -1 otherwise*/
1023 	s32 TTL;
1024 	/*multiple address counts - ONLY in media descriptions if needed. This
1025 	is used for content scaling, when # quality of the same media are multicasted on
1026 	# IP addresses*/
1027 	u32 add_count;
1028 } GF_SDPConnection;
1029 
1030 /*! SDP FormatTypePayload
1031 
1032 	description of dynamic payload types. This is opaque at the SDP level.
1033 	Each attributes is assumed to be formatted as <param_name=param_val; ...>
1034 	If not the case the attribute will have an empty value string and only the
1035 	parameter name.
1036 */
1037 typedef struct
1038 {
1039 	/*payload type of the format described*/
1040 	u32 PayloadType;
1041 	/*list of GF_X_Attribute elements. The Value field may be NULL*/
1042 	GF_List *Attributes;
1043 } GF_SDP_FMTP;
1044 
1045 /*! SDP Media information*/
1046 typedef struct
1047 {
1048 	/*m=
1049 	0: application - 1:video - 2: audio - 3: text - 4:data - 5: control*/
1050 	u32 Type;
1051 	/*Port Number - For transports based on UDP, the value should be in the range 1024
1052 	to 65535 inclusive. For RTP compliance it should be an even number*/
1053 	u32 PortNumber;
1054 	/*number of ports described. If >= 2, the next media(s) in the SDP will be configured
1055 	to use the next tuple (for RTP). If 0 or 1, ignored
1056 	Note: this is used for scalable media: PortNumber indicates the port of the base
1057 	media and NumPorts the ports||total number of the upper layers*/
1058 	u32 NumPorts;
1059 	/*currently ony "RTP/AVP" and "udp" defined*/
1060 	char *Profile;
1061 
1062 	/*list of GF_SDPConnection's. A media can have several connection in case of scalable content*/
1063 	GF_List *Connections;
1064 
1065 	/*RTPMaps contains a list SDPRTPMaps*/
1066 	GF_List *RTPMaps;
1067 
1068 	/*FMTP contains a list of FMTP structures*/
1069 	GF_List *FMTP;
1070 
1071 	/*for RTP this is PayloadType, but can be opaque (string) depending on the app.
1072 	Formated as XX WW QQ FF
1073 	When reading the SDP, the payloads defined in RTPMap are removed from this list
1074 	When writing the SDP for RTP, you should only specify static payload types here,
1075 	as dynamic ones are stored in RTPMaps and automatically written*/
1076 	char *fmt_list;
1077 
1078 	/*all attributes not defined in RFC 2327 for the media*/
1079 	GF_List *Attributes;
1080 
1081 	/*Other SDP attributes for media desc*/
1082 
1083 	/*k=
1084 	method is 'clear' (key follows), 'base64' (key in base64), 'uri' (key is the URI)
1085 	or 'prompt' (key not included)*/
1086 	char *k_method, *k_key;
1087 
1088 	GF_List *Bandwidths;
1089 
1090 	/*0 if not present*/
1091 	u32 PacketTime;
1092 	/*0: none - 1: recv, 2: send, 3 both*/
1093 	u32 SendReceive;
1094 	char *orientation, *sdplang, *lang;
1095 	/*for video only, 0.0 if not present*/
1096 	Double FrameRate;
1097 	/*between 0 and 10, -1 if not present*/
1098 	s32 Quality;
1099 } GF_SDPMedia;
1100 
1101 /*! SDP information*/
1102 typedef struct
1103 {
1104 	/*v=*/
1105 	u32 Version;
1106 	/*o=*/
1107 	char *o_username, *o_session_id, *o_version, *o_address;
1108 	/*"IN" for Net, "IP4" or "IP6" for address are currently valid*/
1109 	char *o_net_type, *o_add_type;
1110 
1111 	/*s=*/
1112 	char *s_session_name;
1113 	/*i=*/
1114 	char *i_description;
1115 	/*u=*/
1116 	char *u_uri;
1117 	/*e=*/
1118 	char *e_email;
1119 	/*p=*/
1120 	char *p_phone;
1121 	/*c= either 1 or 0 GF_SDPConnection */
1122 	GF_SDPConnection *c_connection;
1123 	/*b=*/
1124 	GF_List *b_bandwidth;
1125 	/*All time info (t, r, z)*/
1126 	GF_List *Timing;
1127 	/*k=
1128 	method is 'clear' (key follows), 'base64' (key in base64), 'uri' (key is the URI)
1129 	or 'prompt' (key not included)*/
1130 	char *k_method, *k_key;
1131 	/*all possible attributes (a=), session level*/
1132 	char *a_cat, *a_keywds, *a_tool;
1133 	/*0: none, 1: recv, 2: send, 3 both*/
1134 	u32 a_SendReceive;
1135 	/*should be `broadcast', `meeting', `moderated', `test' or `H332'*/
1136 	char *a_type;
1137 	char *a_charset;
1138 	char *a_sdplang, *a_lang;
1139 
1140 	/*all attributes not defined in RFC 2327 for the presentation*/
1141 	GF_List *Attributes;
1142 
1143 	/*list of media in the SDP*/
1144 	GF_List *media_desc;
1145 } GF_SDPInfo;
1146 
1147 
1148 /*
1149 */
1150 /*! creates a new SDP info
1151 \return the newly allocated SDP
1152 */
1153 GF_SDPInfo *gf_sdp_info_new();
1154 /*! destrucs an SDP info
1155   Memory Consideration: the destructors free all non-NULL string. You should therefore
1156   be carefull while (de-)assigning the strings. The function gf_sdp_info_parse() performs a complete
1157   reset of the GF_SDPInfo
1158 
1159 \param sdp the target SDP to destroy
1160 */
1161 void gf_sdp_info_del(GF_SDPInfo *sdp);
1162 /*! resets all structures and destroys substructures too
1163 \param sdp the target SDP to destroy
1164 */
1165 void gf_sdp_info_reset(GF_SDPInfo *sdp);
1166 /*! parses a memory SDP buffer
1167 \param sdp the target SDP to destroy
1168 \param sdp_text the encoded SDP buffer
1169 \param text_size sizeo if the encoded SDP buffer
1170 \return error if any
1171 */
1172 GF_Err gf_sdp_info_parse(GF_SDPInfo *sdp, char *sdp_text, u32 text_size);
1173 
1174 
1175 /*! creates a new SDP media
1176 \return the newly allocated SDP media
1177 */
1178 GF_SDPMedia *gf_sdp_media_new();
1179 /*! destroys an SDP media
1180 \param media the target SDP media
1181 */
1182 void gf_sdp_media_del(GF_SDPMedia *media);
1183 
1184 /*! creates a new SDP connection
1185 \return the newly allocated SDP connection
1186 */
1187 GF_SDPConnection *gf_sdp_conn_new();
1188 /*! destroys an SDP connection
1189 \param conn the target SDP connection
1190 */
1191 void gf_sdp_conn_del(GF_SDPConnection *conn);
1192 
1193 /*! creates a new SDP payload
1194 \return the newly allocated SDP payload
1195 */
1196 GF_SDP_FMTP *gf_sdp_fmtp_new();
1197 /*! destroys an SDP payload
1198 \param fmtp the target SDP payload
1199 */
1200 void gf_sdp_fmtp_del(GF_SDP_FMTP *fmtp);
1201 
1202 
1203 
1204 /*
1205 	RTP packetizer
1206 */
1207 
1208 /*! Mapping between RTP and GPAC / MPEG-4 Systems SyncLayer */
1209 typedef struct
1210 {
1211 	/*1 - required options*/
1212 
1213 	/*mode, or "" if no mode ("generic" should be used instead)*/
1214 	char mode[30];
1215 
1216 	/*config of the stream if carried in SDP*/
1217 	u8 *config;
1218 	u32 configSize;
1219 	/* Stream Type*/
1220 	u8 StreamType;
1221 	/* stream profile and level indication - for AVC/H264, 0xPPCCLL, with PP:profile, CC:compatibility, LL:level*/
1222 	u32 PL_ID;
1223 
1224 	/*rvc config of the stream if carried in SDP*/
1225 	u16 rvc_predef;
1226 	u8 *rvc_config;
1227 	u32 rvc_config_size;
1228 
1229 	/*2 - optional options*/
1230 
1231 	/*size of AUs if constant*/
1232 	u32 ConstantSize;
1233 	/*duration of AUs if constant, in RTP timescale*/
1234 	u32 ConstantDuration;
1235 
1236 	/* CodecID */
1237 	u32 CodecID;
1238 	/*audio max displacement when interleaving (eg, de-interleaving window buffer max length) in RTP timescale*/
1239 	u32 maxDisplacement;
1240 	/*de-interleaveBufferSize if not recomputable from maxDisplacement*/
1241 	u32 deinterleaveBufferSize;
1242 
1243 	/*The number of bits on which the AU-size field is encoded in the AU-header*/
1244 	u32 SizeLength;
1245 	/*The number of bits on which the AU-Index is encoded in the first AU-header*/
1246 	u32 IndexLength;
1247 	/*The number of bits on which the AU-Index-delta field is encoded in any non-first AU-header*/
1248 	u32 IndexDeltaLength;
1249 
1250 	/*The number of bits on which the DTS-delta field is encoded in the AU-header*/
1251 	u32 DTSDeltaLength;
1252 	/*The number of bits on which the CTS-delta field is encoded in the AU-header*/
1253 	u32 CTSDeltaLength;
1254 	/*random access point flag present*/
1255 	Bool RandomAccessIndication;
1256 
1257 	/*The number of bits on which the Stream-state field is encoded in the AU-header (systems only)*/
1258 	u32 StreamStateIndication;
1259 	/*The number of bits that is used to encode the auxiliary-data-size field
1260 	(no normative usage of this section)*/
1261 	u32 AuxiliaryDataSizeLength;
1262 
1263 	/*ISMACryp stuff*/
1264 	u8 IV_length, IV_delta_length;
1265 	u8 KI_length;
1266 
1267 	/*internal stuff*/
1268 	/*len of first AU header in an RTP payload*/
1269 	u32 auh_first_min_len;
1270 	/*len of non-first AU header in an RTP payload*/
1271 	u32 auh_min_len;
1272 } GP_RTPSLMap;
1273 
1274 
1275 /*! packetizer config flags - some flags are dynamically re-assigned when detecting multiSL / B-Frames / ...*/
1276 enum
1277 {
1278 	/*forces MPEG-4 generic transport if MPEG-4 systems mapping is available*/
1279 	GP_RTP_PCK_FORCE_MPEG4 =	(1),
1280 	/*Enables AUs concatenation in an RTP packet (if payload supports it) - this forces GP_RTP_PCK_SIGNAL_SIZE for MPEG-4*/
1281 	GP_RTP_PCK_USE_MULTI	=	(1<<1),
1282 	/*if set, audio interleaving is used if payload supports it (forces GP_RTP_PCK_USE_MULTI flag)
1283 		THIS IS CURRENTLY NOT IMPLEMENTED*/
1284 	GP_RTP_PCK_USE_INTERLEAVING =	(1<<2),
1285 	/*uses static RTP payloadID if any defined*/
1286 	GP_RTP_PCK_USE_STATIC_ID =	(1<<3),
1287 
1288 	/*MPEG-4 generic transport option*/
1289 	/*if flag set, RAP flag is signaled in RTP payload*/
1290 	GP_RTP_PCK_SIGNAL_RAP	=	(1<<4),
1291 	/*if flag set, AU indexes are signaled in RTP payload - only usable for AU interleaving (eg audio)*/
1292 	GP_RTP_PCK_SIGNAL_AU_IDX	=	(1<<5),
1293 	/*if flag set, AU size is signaled in RTP payload*/
1294 	GP_RTP_PCK_SIGNAL_SIZE	=	(1<<6),
1295 	/*if flag set, CTS is signaled in RTP payload - DTS is automatically set if needed*/
1296 	GP_RTP_PCK_SIGNAL_TS	=	(1<<7),
1297 
1298 	/*setup payload for carouseling of systems streams*/
1299 	GP_RTP_PCK_SYSTEMS_CAROUSEL = (1<<8),
1300 
1301 	/*use LATM payload for AAC-LC*/
1302 	GP_RTP_PCK_USE_LATM_AAC	=	(1<<9),
1303 
1304 	/*ISMACryp options*/
1305 	/*signals that input data is selectively encrypted (eg not all input frames are encrypted)
1306 	- this is usually automatically set by hinter*/
1307 	GP_RTP_PCK_SELECTIVE_ENCRYPTION =	(1<<10),
1308 	/*signals that each sample will have its own key indicator - ignored in non-multi modes
1309 	if not set and key indicator changes, a new RTP packet will be forced*/
1310 	GP_RTP_PCK_KEY_IDX_PER_AU =	(1<<11),
1311 
1312 	/*is zip compression used in DIMS unit ?*/
1313 	GP_RTP_DIMS_COMPRESSED =	(1<<12),
1314 };
1315 
1316 
1317 
1318 /*
1319 		Generic packetization tools - used by track hinters and future live tools
1320 */
1321 
1322 /*! Supported payload types*/
1323 enum
1324 {
1325 	/*assigned payload types*/
1326 	/*cf RFC 3551*/
1327 	GF_RTP_PAYT_PCMU = 0,
1328 	/*cf RFC 3551*/
1329 	GF_RTP_PAYT_GSM,
1330 	/*cf RFC 3551*/
1331 	GF_RTP_PAYT_G723,
1332 	/*cf RFC 3551*/
1333 	GF_RTP_PAYT_DVI4_8K,
1334 	/*cf RFC 3551*/
1335 	GF_RTP_PAYT_DVI4_16K,
1336 	/*cf RFC 3551*/
1337 	GF_RTP_PAYT_LPC,
1338 	/*cf RFC 3551*/
1339 	GF_RTP_PAYT_PCMA,
1340 	/*cf RFC 3551*/
1341 	GF_RTP_PAYT_G722,
1342 	/*cf RFC 3551*/
1343 	GF_RTP_PAYT_L16_STEREO,
1344 	/*cf RFC 3551*/
1345 	GF_RTP_PAYT_L16_MONO,
1346 	/*cf RFC 3551*/
1347 	GF_RTP_PAYT_QCELP_BASIC,
1348 	/*cf RFC 3389*/
1349 	GF_RTP_PAYT_CN,
1350 	/*use generic MPEG-1/2 audio transport - RFC 2250*/
1351 	GF_RTP_PAYT_MPEG12_AUDIO,
1352 	/*cf RFC 3551*/
1353 	GF_RTP_PAYT_G728,
1354 	GF_RTP_PAYT_DVI4_11K,
1355 	GF_RTP_PAYT_DVI4_22K,
1356 	/*cf RFC 3551*/
1357 	GF_RTP_PAYT_G729,
1358 	/*cf RFC 2029*/
1359 	GF_RTP_PAYT_CelB = 25,
1360 	/*cf RFC 2435*/
1361 	GF_RTP_PAYT_JPEG = 26,
1362 	/*cf RFC 3551*/
1363 	GF_RTP_PAYT_nv = 28,
1364 	/*cf RFC 4587*/
1365 	GF_RTP_PAYT_H261 = 31,
1366 	/* generic MPEG-1/2 video transport - RFC 2250*/
1367 	GF_RTP_PAYT_MPEG12_VIDEO = 32,
1368 	/*MPEG-2 TS - RFC 2250*/
1369 	GF_RTP_PAYT_MP2T = 33,
1370 	/*use H263 transport - RFC 2429*/
1371 	GF_RTP_PAYT_H263 = 34,
1372 
1373 	GF_RTP_PAYT_LAST_STATIC_DEFINED = 35,
1374 
1375 	/*not defined*/
1376 	GF_RTP_PAYT_UNKNOWN = 128,
1377 
1378 	/*internal types for all dynamic payloads*/
1379 
1380 	/*use generic MPEG-4 transport - RFC 3016 and RFC 3640*/
1381 	GF_RTP_PAYT_MPEG4,
1382 	/*use AMR transport - RFC 3267*/
1383 	GF_RTP_PAYT_AMR,
1384 	/*use AMR-WB transport - RFC 3267*/
1385 	GF_RTP_PAYT_AMR_WB,
1386 	/*use QCELP transport - RFC 2658*/
1387 	GF_RTP_PAYT_QCELP,
1388 	/*use EVRC/SMV transport - RFC 3558*/
1389 	GF_RTP_PAYT_EVRC_SMV,
1390 	/*use 3GPP Text transport - no RFC yet, only draft*/
1391 	GF_RTP_PAYT_3GPP_TEXT,
1392 	/*use H264 transport - no RFC yet, only draft*/
1393 	GF_RTP_PAYT_H264_AVC,
1394 	/*use LATM for AAC-LC*/
1395 	GF_RTP_PAYT_LATM,
1396 	/*use AC3 audio format*/
1397 	GF_RTP_PAYT_AC3,
1398 	/*use H264-SVC transport*/
1399 	GF_RTP_PAYT_H264_SVC,
1400 	/*use HEVC/H265 transport - no RFC yet, only draft*/
1401 	GF_RTP_PAYT_HEVC,
1402 	GF_RTP_PAYT_LHVC,
1403 #if GPAC_ENABLE_3GPP_DIMS_RTP
1404 	/*use 3GPP DIMS format*/
1405 	GF_RTP_PAYT_3GPP_DIMS,
1406 #endif
1407 };
1408 
1409 
1410 
1411 /*
1412 	RTP packetizer
1413 */
1414 
1415 
1416 /*! RTP builder (packetizer)*/
1417 typedef struct __tag_rtp_packetizer GP_RTPPacketizer;
1418 
1419 /*! creates a new builder
1420 \param rtp_payt rtp payload format, one of the above GF_RTP_PAYT_*
1421 \param slc user-given SL config to use. If none specified, default RFC config is used
1422 \param flags packetizer flags, one of the above GP_RTP_PCK_*
1423 \param cbk_obj callback object passed back in functions
1424 \param OnNewPacket callback function starting new RTP packet
1425 		header: rtp header for new packet - note that RTP header flags are not used until PacketDone is called
1426 \param OnPacketDone callback function closing current RTP packet
1427 		header: final rtp header for packet
1428 \param OnDataReference optional, to call each time data from input buffer is added to current RTP packet.
1429 		If not set, data must be added through OnData
1430 		payload_size: size of reference data
1431 		offset_from_orig: start offset in input buffer
1432 \param OnData to call each time data is added to current RTP packet (either extra data from payload or
1433 		data from input when not using referencing)
1434 		is_head: signal the data added MUST be inserted at the beginning of the payload. Otherwise data is concatenated as received
1435 \return a new RTP packetizer object
1436 */
1437 GP_RTPPacketizer *gf_rtp_builder_new(u32 rtp_payt,
1438                                      GF_SLConfig *slc,
1439                                      u32 flags,
1440                                      void *cbk_obj,
1441                                      void (*OnNewPacket)(void *cbk, GF_RTPHeader *header),
1442                                      void (*OnPacketDone)(void *cbk, GF_RTPHeader *header),
1443                                      void (*OnDataReference)(void *cbk, u32 payload_size, u32 offset_from_orig),
1444                                      void (*OnData)(void *cbk, u8 *data, u32 data_size, Bool is_head)
1445                                     );
1446 
1447 /*! destroys an RTP packetizer
1448 \param builder the target RTP packetizer
1449 */
1450 void gf_rtp_builder_del(GP_RTPPacketizer *builder);
1451 
1452 /*! inits the RTP packetizer
1453 \param builder the target RTP packetizer
1454 \param PayloadType the payload type to use
1455 \param MaxPayloadSize maximum payload size of RTP packets (eg MTU minus IP/UDP/RTP headers)
1456 \param max_ptime maximum packet duration IN RTP TIMESCALE
1457 \param StreamType MPEG-4 system stream type - MUST always be provided for payloads format specifying
1458 		audio or video streams
1459 \param codecid ID of the media codec
1460 \param PL_ID profile and level identifier for the stream
1461 
1462 			*** all other params are for RFC 3640 ***
1463 
1464 \param avgSize average size of an AU. This is not always known (real-time encoding).
1465 In this case you should specify a rough compute indicating how many packets could be
1466 stored per RTP packet. for ex AAC stereo at 44100 k / 64kbps , one AU ~= 380 bytes
1467 so 3 AUs for 1500 MTU is ok - BE CAREFULL: MultiSL adds some SL info on top of the 12
1468 byte RTP header so you should specify a smaller size
1469 The packetizer will ALWAYS make sure there's no pb storing the packets so specifying
1470 more will result in a slight overhead in the SL mapping but the gain to singleSL
1471 will still be worth it.
1472 	-Nota: at init, the packetizer can decide to switch to SingleSL if the average size
1473 specified is too close to the PathMTU
1474 
1475 \param maxSize max size of an AU. If unknown (real-time) set to 0
1476 \param avgTS average CTS progression (1000/FPS for video)
1477 \param maxDTS maximum DTS offset in case of bidirectional coding.
1478 \param IV_length size (in bytes) of IV when ISMACrypted
1479 \param KI_length size (in bytes) of key indicator when ISMACrypted
1480 \param pref_mode MPEG-4 generic only, specifies the payload mode - can be NULL (mode generic)
1481 */
1482 void gf_rtp_builder_init(GP_RTPPacketizer *builder, u8 PayloadType, u32 MaxPayloadSize, u32 max_ptime,
1483                          u32 StreamType, u32 codecid, u32 PL_ID,
1484                          u32 avgSize, u32 maxSize,
1485                          u32 avgTS, u32 maxDTS,
1486                          u32 IV_length, u32 KI_length,
1487                          char *pref_mode);
1488 
1489 /*! sets frame crypto info (ISMA E&A) for frame starting in next RTP packet
1490 \param builder the target RTP packetizer
1491 \param IV initialization vector
1492 \param key_indicator key indicator
1493 \param is_encrypted encrypted flag
1494 */
1495 void gf_rtp_builder_set_cryp_info(GP_RTPPacketizer *builder, u64 IV, char *key_indicator, Bool is_encrypted);
1496 /*! packetizes input buffer
1497 \param builder the target RTP packetizer
1498 \param data input buffer
1499 \param data_size input buffer size
1500 \param IsAUEnd set to one if this buffer is the last of the AU
1501 \param FullAUSize complete access unit size if known, 0 otherwise
1502 \param duration sample duration in rtp timescale (mostly needed for 3GPP text streams)
1503 \param descIndex sample description index (mostly needed for 3GPP text streams)
1504 \return error if any
1505 */
1506 GF_Err gf_rtp_builder_process(GP_RTPPacketizer *builder, u8 *data, u32 data_size, u8 IsAUEnd, u32 FullAUSize, u32 duration, u8 descIndex);
1507 
1508 /*! formats the "fmtp: " attribute for the MPEG-4 generic packetizer. sdpline shall be at least 2000 char
1509 \param builder the target RTP packetizer
1510 \param payload_name name of the payload to use (profile of RFC 3640)
1511 \param sdp_line SDP line buffer to fill
1512 \param dsi decoder config of stream if any, or NULL
1513 \param dsi_size size of the decoder config
1514 \return error if any
1515 */
1516 GF_Err gf_rtp_builder_format_sdp(GP_RTPPacketizer *builder, char *payload_name, char *sdp_line, char *dsi, u32 dsi_size);
1517 /*! formats SDP payload name and media name
1518 \param builder the target RTP packetizer
1519 \param payload_name the buffer to fill with the payload name
1520 \param media_name the buffer to fill with the payload name
1521 \return GF_TRUE if success, GF_FALSE otherwise
1522 */
1523 Bool gf_rtp_builder_get_payload_name(GP_RTPPacketizer *builder, char payload_name[20], char media_name[20]);
1524 
1525 
1526 /*! rtp payload flags*/
1527 enum
1528 {
1529 	/*AU end was detected (eg next packet is AU start)*/
1530 	GF_RTP_NEW_AU = (1),
1531 	/*AMR config*/
1532 	GF_RTP_AMR_ALIGN = (1<<1),
1533 	/*for RFC3016, signals bitstream inspection for RAP discovery*/
1534 	GF_RTP_M4V_CHECK_RAP = (1<<2),
1535 
1536 	/*AWFULL hack at rtp level to cope with ffmpeg h264 crashes when jumping in stream without IDR*/
1537 	GF_RTP_AVC_WAIT_RAP = (1<<4),
1538 	/*ISMACryp stuff*/
1539 	GF_RTP_HAS_ISMACRYP = (1<<5),
1540 	GF_RTP_ISMA_SEL_ENC = (1<<6),
1541 	GF_RTP_ISMA_HAS_KEY_IDX = (1<<7),
1542 
1543 	GF_RTP_AVC_USE_ANNEX_B = (1<<8)
1544 };
1545 
1546 /*! Static RTP map definition, for static payload types*/
1547 typedef struct rtp_static_payt {
1548 	u32 fmt;
1549 	u32 clock_rate;
1550 	u32 stream_type;
1551 	u32 codec_id;
1552 	const char *mime;
1553 } GF_RTPStaticMap;
1554 
1555 
1556 /*! RTP depacketizer callback
1557 \param udta opaque user data
1558 \param payload depacketized payload data
1559 \param size payload size
1560 \param hdr MPEG-4 Sync Layer structure for the payload (with AU start/end flag, timestamps, etc)
1561 \param e error if any
1562 */
1563 typedef void (*gf_rtp_packet_cbk)(void *udta, u8 *payload, u32 size, GF_SLHeader *hdr, GF_Err e);
1564 
1565 
1566 
1567 /*! RTP parser (depacketizer)*/
1568 typedef struct __tag_rtp_depacketizer GF_RTPDepacketizer;
1569 
1570 /*! creates a new depacketizer
1571 \param media the SDP media structure describing the RTP stream - can be NULL for static payload types
1572 \param hdr_payt the static RTP payload type when no SDP is used
1573 \param sl_packet_cbk callback function of the depacketizer to retrieves payload
1574 \param udta opaque data for the callback function
1575 \return a newly allocated RTP depacketizer, or NULL if not supported*/
1576 GF_RTPDepacketizer *gf_rtp_depacketizer_new(GF_SDPMedia *media, u32 hdr_payt, gf_rtp_packet_cbk sl_packet_cbk, void *udta);
1577 /*! destroys an RTP depacketizer
1578 \param rtpd the target RTP depacketizer
1579 */
1580 void gf_rtp_depacketizer_del(GF_RTPDepacketizer *rtpd);
1581 /*! resets an RTP depacketizer, assuming next packet should be an AU start
1582 \param rtpd the target RTP depacketizer
1583 \param full_reset if set, completely reset the SL header
1584 */
1585 void gf_rtp_depacketizer_reset(GF_RTPDepacketizer *rtpd, Bool full_reset);
1586 /*! process an RTP depacketizer
1587 \param rtpd the target RTP depacketizer
1588 \param hdr the RTP packet header
1589 \param payload the RTP packet payload
1590 \param size the RTP packet payload size
1591 */
1592 void gf_rtp_depacketizer_process(GF_RTPDepacketizer *rtpd, GF_RTPHeader *hdr, u8 *payload, u32 size);
1593 
1594 #endif /*GPAC_DISABLE_STREAMING*/
1595 
1596 /*! @} */
1597 
1598 #ifdef __cplusplus
1599 }
1600 #endif
1601 
1602 #endif		/*_GF_IETF_H_*/
1603 
1604