ameba micropython sdk first commit

This commit is contained in:
xidameng 2020-07-31 22:16:12 +08:00
commit 8508ee6139
5619 changed files with 1874619 additions and 0 deletions

View file

@ -0,0 +1,159 @@
#ifndef _RTP_API_H_
#define _RTP_API_H_
#include <stdint.h>
#include "dlist.h"
#include "osdep_service.h"
#include "avcodec.h"
#include <lwip/def.h> //for host network byte order convertion
/* from error_base.h */
#define EIO 5 /* I/O error */
#define EAGAIN 11 /* Try again */
#define ENOMEM 12 /* Out of memory */
#define EINVAL 22 /* Invalid argument */
#define RTP_BIG_ENDIAN 0
#define RTP_HDR_SZ 12
#define RTP_SERVER_PORT_BASE 55608
#define RTP_PORT_BASE 50020
#define RTP_CLIENT_PORT_BASE 51020
/*
* RTP data header from RFC1889
*/
/*
*
*
* The RTP header has the following format:
*
* 0 1 2 3
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* |V=2|P|X| CC |M| PT | sequence number |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | timestamp |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | synchronization source (SSRC) identifier |
* +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
* | contributing source (CSRC) identifiers |
* | .... |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*
* RTP data header
*/
typedef struct {
#if RTP_BIG_ENDIAN
uint16_t version:2; /* protocol version */
uint16_t p:1; /* padding flag */
uint16_t x:1; /* header extension flag */
uint16_t cc:4; /* CSRC count */
uint16_t m:1; /* marker bit */
uint16_t pt:7; /* payload type */
#else /*RTP_LITTLE_ENDIAN*/
uint16_t cc:4; /* CSRC count */
uint16_t x:1; /* header extension flag */
uint16_t p:1; /* padding flag */
uint16_t version:2; /* protocol version */
uint16_t pt:7; /* payload type */
uint16_t m:1; /* marker bit */
#endif
uint16_t seq; /* sequence number */
uint32_t ts; /* timestamp */
uint32_t ssrc; /* synchronization source */
uint32_t *csrc; /* optional CSRC list, skip if cc is set to 0 here*/
} rtp_hdr_t;
/*sturcture to hold connect info*/
struct connect_context
{
int socket_id;
uint8_t *server_ip;
uint16_t server_port;
uint8_t *remote_ip;
uint16_t remote_port;
#ifdef ENABLE_SIP_MMFV2
uint16_t server_port_audio;
uint16_t server_port_video;
uint16_t remote_port_audio;
uint16_t remote_port_video;
#endif
};
struct rtp_statistics
{
uint32_t rtp_tick;
uint32_t rtp_tick_inc;
uint32_t base_timestamp;
/*for flow control*/
uint32_t delay_threshold; //in ms
uint32_t timer1; //time before send
uint32_t timer2; //time after sent
uint32_t delta_timer;
uint8_t do_start_check; //indicate if start check need to be done
uint32_t sent_packet;
uint32_t drop_packet;
};
struct rtp_periodic_report_s
{
uint32_t period;
uint32_t timer1;
uint32_t timer2;
uint32_t bytes;
uint32_t send_frame;
uint32_t drop_frame;
uint32_t last_timestamp;
};
enum rtp_object_state
{
RTP_OBJECT_IDLE = 0,
RTP_OBJECT_READY,
RTP_OBJECT_INUSE,
RTP_OBJECT_USED,
};
#ifdef ENABLE_SIP_MMFV2
struct ua_stream_context;
#endif
struct stream_context;
struct rtp_object;
struct rtp_object
{
struct list_head rtp_list;
_mutex list_lock;
rtp_hdr_t *rtphdr;
void *extra; //pointer to type specific structure
int index; //respective internal buffer index
uint8_t *data; // respective internal buffer data addr
int len; //one complete frame data length
uint32_t timestamp; //timestamp
uint32_t fs:1; //fragment start
uint32_t fe:1; //fragment end
uint32_t fk:1; //fragment keep indicator so that cannot avoid sending by flow control
uint32_t fd:29; //fragment data size (max size of 2^29-1)
enum rtp_object_state state;
struct connect_context connect_ctx;
#ifdef ENABLE_SIP_MMFV2
int (*sip_rtp_object_handler)(struct ua_stream_context *stream_ctx, struct rtp_object *payload);
#else
int (*rtp_object_handler)(struct stream_context *stream_ctx, struct rtp_object *payload);
#endif
};
void rtp_object_init(struct rtp_object *payload);
void rtp_object_deinit(struct rtp_object *payload);
void rtp_object_set_fs(struct rtp_object *payload, int flag);
void rtp_object_set_fe(struct rtp_object *payload, int flag);
void rtp_object_set_fk(struct rtp_object *payload, int flag);
void rtp_object_set_fd(struct rtp_object *payload, int size);
void rtp_load_o_handler_by_codec_id(struct rtp_object *payload, int id);
void rtp_fill_header(rtp_hdr_t *rtphdr, int version, int padding, int extension, int cc, int marker, int pt, uint16_t seq, uint32_t ts, uint32_t ssrc);
int rtp_parse_header(uint8_t *src, rtp_hdr_t *rtphdr, int is_nbo);
void rtp_dump_header(rtp_hdr_t *rtphdr, int is_nbo);
void rtp_report(struct stream_context *stream_ctx);
#endif

View file

@ -0,0 +1,191 @@
#ifndef _RTSP_API_H_
#define _RTSP_API_H_
#include "dlist.h"
#include "basic_types.h"
#include "osdep_service.h"
#if defined(CONFIG_PLATFORM_8195A)
#include "mmf_dbg.h"
#endif
#if defined(CONFIG_PLATFORM_8195BHP)
#include "mmf2_dbg.h"
#endif
#include "rtsp/rtp_api.h"
#include "avcodec.h"
#include "rtsp/sdp.h"
//Trigger for softphone feature, when open it, run sip example and close rtsp example
//#define ENABLE_SIP_MMFV2
#define ENABLE_PROXY_SEVER 0
#if ENABLE_PROXY_SEVER
#define PROXY_SERVER_IP "52.198.104.214"
#define PROXY_SERVER_PORT 5000
#endif
/* clock usage */
#define RTSP_DEPEND_CLK_HZ configTICK_RATE_HZ
#define RTSP_SERVICE_PRIORITY 3
#define RTSP_MAX_STREAM_NUM 2
#define RTSP_REQUEST_BUF_SIZE 512
#define RTSP_RESPONSE_BUF_SIZE MAX_SDP_SIZE //max size for response buffer
#define DEF_RTSP_PORT 554
#define DEF_HTTP_PORT 5008
#define RTSP_SELECT_SOCK 8
/*rtsp request type list*/
#define REQUEST_OPTIONS 1
#define REQUEST_DESCRIBE 2
#define REQUEST_SETUP 3
#define REQUEST_TEARDOWN 4
#define REQUEST_PLAY 5
#define REQUEST_PAUSE 6
#define REQUEST_GET_PARAM 7
#define RTSP_INTERLEAVED_FRAME 8
/*rtsp cast mode list*/
#define UNICAST_UDP_MODE 1
#define UNICAST_TCP_MODE 2
#define MULTICAST_MODE 3
#define BOUNDARY "amebaimagetest"
/*RTSP KEEPALIVE TIMEOUT ENABLE*/
#define KEEPALIVE_TIMEOUT_ENABLE
enum _rtsp_state {
RTSP_INIT = 0,
RTSP_READY = 1,
RTSP_PLAYING = 2,
};
typedef enum _rtsp_state rtsp_state;
struct rtsp_session
{
uint32_t id;
uint8_t version;
uint32_t start_time;
uint32_t end_time;
uint8_t *user;
uint8_t *name;
};
struct rtsp_transport
{
uint8_t isRtp; //transport protocol
uint8_t isTcp; //lower transport protocol
uint8_t castMode; //unicast UDP(1) or unicast TCP(2) or multicast(3)
int port_low;
int port_high;
int clientport_low;
int clientport_high;
int serverport_low;
int serverport_high;
uint8_t ttl; //multicast time to live
//to be added if necessary
int interleaved_low; // for RTSP over TCP
int interleaved_high; // for RTSP over TCP
};
struct __internal_payload{
int codec_id;
struct rtp_object payload;
};
struct stream_context
{
struct rtsp_context *parent;
int stream_id; //sync with stream_flow id
struct list_head input_queue;
_mutex input_lock;
struct list_head output_queue;
_mutex output_lock;
struct codec_info *codec;
uint8_t media_type;
uint8_t framerate;
uint32_t bitrate;
uint32_t samplerate;
uint32_t channel;
uint32_t old_depend_clock_tick;
uint32_t rtp_timestamp;
uint8_t use_rtp_tick_inc;
uint8_t index; // index in rtsp_context
uint8_t setup_done;
struct rtp_statistics statistics;
struct rtp_periodic_report_s periodic_report;
struct __internal_payload rtpobj;
};
struct rtsp_context
{
int id;
uint8_t allow_stream;
rtsp_state state;
uint8_t request_type;
uint8_t request_incomplete;
uint32_t CSeq;
char *response;
struct connect_context connect_ctx;
int proxy_socket;
int proxy_port;
uint8_t is_connected_to_proxy;
struct rtsp_transport transport[RTSP_MAX_STREAM_NUM];
struct rtsp_session session;
u16 rtpseq[RTSP_MAX_STREAM_NUM];
uint8_t is_rtsp_start;
_sema start_rtsp_sema;
uint8_t is_rtp_start;
_sema start_rtp_sema;
void (* rtp_service_handle) (struct rtsp_context* rtsp_ctx);
_sema start_proxy_connect_sema;
_sema rtp_input_sema;
_sema rtp_output_sema;
#ifdef SUPPORT_RTCP
uint8_t is_rtcp_start;
_sema start_rtcp_sema;
void (* rtcp_service_handle) (struct rtsp_context* rtsp_ctx);
#endif
#ifdef SUPPORT_HTTP
//to be added
#endif
uint8_t nb_streams_setup;
int8_t setup_stream_index;
uint8_t nb_streams;
struct stream_context *stream_ctx;
uint32_t pre_filter_packet;
int client_socket;
_mutex socket_lock;
// callback
int (*cb_start)(void*); // play
int (*cb_stop)(void*); // teardown
int (*cb_pause)(void*); // pause
int (*cb_custom)(void*); // setparam
};
uint32_t rtsp_get_timestamp(struct stream_context *stream_ctx, uint32_t current_clock_tick);
int rtsp_get_number(int number_base, uint32_t *number_bitmap, _mutex *bitmap_lock);
void rtsp_put_number(int number, int number_base, uint32_t *number_bitmap, _mutex *bitmap_lock);
struct rtsp_context *rtsp_context_create(uint8_t nb_streams);
void rtsp_context_free(struct rtsp_context *rtsp_ctx);
int rtsp_is_stream_enabled(struct rtsp_context *rtsp_ctx);
int rtsp_is_service_enabled(struct rtsp_context *rtsp_ctx);
int rtsp_open(struct rtsp_context *rtsp_ctx);
void rtsp_close(struct rtsp_context *rtsp_ctx);
void rtsp_start(struct rtsp_context *rtsp_ctx);
void rtsp_stop(struct rtsp_context *rtsp_ctx);
void rtp_object_in_stream_queue(struct rtp_object *payload, struct stream_context *stream_ctx);
struct rtp_object *rtp_object_out_stream_queue(struct stream_context *stream_ctx);
void set_profile_lv_string(char * plid);
void set_sps_string(char * sps);
void set_pps_string(char * pps);
int rtsp_parse_stream_media_type(struct codec_info *codec);
void rtsp_stream_context_init(struct rtsp_context *rtsp_ctx, struct stream_context *stream_ctx);
void set_prefilter_packet(struct rtsp_context *rtsp_ctx,uint32_t num);
void time_sync_disable(void);
void time_sync_enable(void);
#endif

View file

@ -0,0 +1,26 @@
#ifndef _SDP_H_
#define _SDP_H_
#include "avcodec.h"
#define CRLF "\r\n"
#define MAX_SDP_SIZE (512+256)
#define SDP_LINE_LEN (128+256)
#define SDP_BWTYPE_CT 0
#define SDP_BWTYPE_AS 1
void sdp_strcat(char *buf1, int size, char *buf2);
void sdp_fill_o_field(char *sdp_buf, int size, uint8_t *username, uint32_t session_id, uint8_t session_version, uint8_t* nettype, uint8_t* addrtype, uint8_t* unicast_addr);
void sdp_fill_s_field(char *sdp_buf, int size, uint8_t * session_name);
void sdp_fill_i_field(char *sdp_buf, int size, uint8_t * session_info);
void sdp_fill_u_field(char * sdp_buf, int size, uint8_t *uri);
void sdp_fill_c_field(char *sdp_buf, int size, uint8_t *nettype, uint8_t *addrtype, uint8_t *connection_addr, uint8_t ttl);
void sdp_fill_b_field(char *sdp_buf, int size, int bwtype, int bw);
void sdp_fill_t_field(char *sdp_buf, int size, uint32_t start_time, uint32_t end_time);
void sdp_fill_m_field(char *sdp_buf, int size, int media_type, uint16_t port, int fmt);
void sdp_fill_a_string(char *sdp_buf, int size, char *string);
void sdp_fill_a_rtpmap(char *sdp_buf, int size, struct codec_info *codec);
#endif