/***************************************************************************************
 *
 *  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
 *
 *  By downloading, copying, installing or using the software you agree to this license.
 *  If you do not agree to this license, do not download, install, 
 *  copy or use the software.
 *
 *  Copyright (C) 2014-2024, Happytimesoft Corporation, all rights reserved.
 *
 *  Redistribution and use in binary forms, with or without modification, are permitted.
 *
 *  Unless required by applicable law or agreed to in writing, software distributed 
 *  under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 *  CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
 *  language governing permissions and limitations under the License.
 *
****************************************************************************************/

#ifndef	_RTSP_PARSE_H
#define	_RTSP_PARSE_H

#include "sys_inc.h"

/*************************************************************************/
typedef enum rtsp_req_mt
{
	RTSP_MT_NULL = 0,
	RTSP_MT_DESCRIBE,   
	RTSP_MT_ANNOUNCE,   
	RTSP_MT_OPTIONS,    
	RTSP_MT_PAUSE,  
	RTSP_MT_PLAY,   
	RTSP_MT_RECORD, 
	RTSP_MT_REDIRECT,   
	RTSP_MT_SETUP,  
	RTSP_MT_SET_PARAMETER,
	RTSP_MT_GET_PARAMETER,
	RTSP_MT_TEARDOWN    
} RTSP_RMT;

typedef struct rtsp_req_message_type_value
{
	RTSP_RMT	msg_type;
	char		msg_str[32];
	int			msg_len;
} RREQMTV;

typedef enum rtsp_context_type
{
	RTSP_CTX_NULL = 0,
	RTSP_CTX_RTSP,
	RTSP_CTX_SDP,
	RTSP_CTX_TXT,
	RTSP_CTX_HTM,
} RTSPCTXT;

typedef struct hrtsp_msg_content
{
	uint32	    msg_type;		// message type : 0 represents a request, 1 represents a response
	uint32	    msg_sub_type;	// message sub type
	HDRV        first_line;

	PPSN_CTX    rtsp_ctx;
	PPSN_CTX    sdp_ctx;

	int         rtsp_len;
	int         sdp_len;

	RTSPCTXT    ctx_type;
	int         ctx_len;

	char *      msg_buf;
	int         buf_offset;

	uint32	    remote_ip;
	uint16	    remote_port;
	uint16	    local_port;
}HRTSP_MSG;

#ifdef __cplusplus
extern "C" {
#endif

/*************************************************************************/
BOOL        rtsp_is_rtsp_msg(char * msg_buf);
void        rtsp_headl_parse(char * pline, int llen, HRTSP_MSG * p_msg);
int         rtsp_line_parse(char * p_buf, int max_len, char sep_char, PPSN_CTX * p_ctx);
int         rtsp_ctx_parse(HRTSP_MSG * p_msg);
int         rtsp_msg_parse(char * msg_buf, int msg_buf_len, HRTSP_MSG * msg);
int         rtsp_msg_parse_part1(char * p_buf, int buf_len, HRTSP_MSG * msg);
int         rtsp_msg_parse_part2(char * p_buf, int buf_len, HRTSP_MSG * msg);

/*************************************************************************/
HDRV      * rtsp_find_headline(HRTSP_MSG * msg, const char * head);
HDRV      * rtsp_find_headline_next(HRTSP_MSG * msg, const char * head, HDRV * hrv);
HDRV      * rtsp_find_sdp_headline(HRTSP_MSG * msg, const char * head);
BOOL        rtsp_msg_with_sdp(HRTSP_MSG * msg);
BOOL        rtsp_match_msg_session(HRTSP_MSG * rx_msg, HRTSP_MSG * tx_msg);
BOOL        rtsp_match_msg_cseq(HRTSP_MSG * rx_msg, HRTSP_MSG * tx_msg);
BOOL        rtsp_get_headline_uri(HRTSP_MSG * rx_msg, char * p_uri, int size);
BOOL        rtsp_get_headline_string(HRTSP_MSG * rx_msg, const char * head, char * p_value, int size);
BOOL        rtsp_get_msg_session(HRTSP_MSG * rx_msg, char *session_buf, int len);
BOOL        rtsp_get_msg_cseq(HRTSP_MSG * rx_msg, char *cseq_buf, int len);
BOOL        rtsp_get_user_agent_info(HRTSP_MSG * rx_msg, char * agent_buf, int max_len);
BOOL 	    rtsp_get_session_info(HRTSP_MSG * rx_msg, char * session_buf, int max_len, int * timeout);
BOOL        rtsp_get_rtp_info(HRTSP_MSG * rx_msg, char * v_ctl, uint32 * p_v_ts, char * a_ctl, uint32 * p_a_ts);
BOOL        rtsp_get_tcp_transport_info(HRTSP_MSG * rx_msg, uint16 *interleaved);
BOOL        rtsp_get_udp_transport_info(HRTSP_MSG * rx_msg, uint16 *client_port, uint16 *server_port);
BOOL        rtsp_get_mc_transport_info(HRTSP_MSG * rx_msg, char *destination, uint16 *port);
BOOL        rtsp_get_cbase_info(HRTSP_MSG * rx_msg, char * cbase_buf, int max_len);
BOOL 	    rtsp_get_digest_info(HRTSP_MSG * rx_msg, HD_AUTH_INFO * auth_info);
BOOL        rtsp_get_auth_digest_info(HRTSP_MSG * rx_msg, HD_AUTH_INFO * p_auth);
BOOL        rtsp_is_line_exist(HRTSP_MSG * rx_msg, const char * head, const char * value);
void        rtsp_add_tx_msg_line(HRTSP_MSG * tx_msg, const char * msg_hdr, const char * msg_fmt, ...);
void        rtsp_add_tx_msg_sdp_line(HRTSP_MSG * tx_msg, const char * msg_hdr, const char * msg_fmt, ...);
void 	    rtsp_add_tx_msg_fline(HRTSP_MSG * tx_msg, const char * msg_hdr, const char * msg_fmt, ...);
void        rtsp_copy_msg_line(HRTSP_MSG * src_msg, HRTSP_MSG * dst_msg, const char * msg_hdr);
void        rtsp_free_msg(HRTSP_MSG * msg);
void        rtsp_free_msg_content(HRTSP_MSG * msg);

/*************************************************************************/
BOOL 	    rtsp_get_remote_media_ip(HRTSP_MSG * rx_msg, uint32 * media_ip);
HDRV      * rtsp_find_mdesc_point(HRTSP_MSG * rx_msg, HDRV * pStartHdr, const char * cap_name, int * next_offset, const char * attr=NULL);
BOOL 	    rtsp_get_remote_cap(HRTSP_MSG * rx_msg, const char * cap_name, int * cap_count, uint8 * cap_array, uint16 * rtp_port, const char * attr=NULL);
BOOL 	    rtsp_get_remote_cap_desc(HRTSP_MSG * rx_msg, const char * cap_name, char cap_desc[][MAX_AVDESCLEN], const char * attr=NULL);
BOOL 	    rtsp_find_sdp_control(HRTSP_MSG * rx_msg, char * ctl_buf, const char * tname, int max_len, const char *attr = NULL);
BOOL        rtsp_get_sdp_range(HRTSP_MSG * rx_msg, int * p_start, int * p_end);
BOOL        rtsp_is_support_mcast(HRTSP_MSG * rx_msg);
BOOL 		rtsp_get_scale_info(HRTSP_MSG * rx_msg, int * p_scale);
BOOL 		rtsp_get_rate_control(HRTSP_MSG * rx_msg, int * p_ratectrl);
BOOL 		rtsp_get_immediate(HRTSP_MSG * rx_msg, int * p_imme);
BOOL		rtsp_get_frame_info(HRTSP_MSG * rx_msg, int * p_frame, int * p_interval);

/*************************************************************************/
BOOL        rtsp_msg_buf_init(int num);
void	    rtsp_msg_buf_deinit();
HRTSP_MSG * rtsp_get_msg_buf();
void        rtsp_msg_ctx_init(HRTSP_MSG * msg);
void        rtsp_free_msg_buf(HRTSP_MSG * msg);
uint32 	    rtsp_idle_msg_buf_num();
BOOL        rtsp_parse_buf_init(int nums);
void        rtsp_parse_buf_deinit();


#ifdef __cplusplus
}
#endif

#endif // _RTSP_PARSE_H


