├── README.md ├── md5.h ├── .gitignore ├── stream_queue.h ├── rtp_enc.h ├── comm.h ├── rtsp_demo.h ├── stream_queue.c ├── utils.h ├── md5.c ├── rtp_enc.c ├── rtsp_msg.h ├── utils.c ├── queue.h ├── LICENSE └── rtsp_msg.c /README.md: -------------------------------------------------------------------------------- 1 | # librtsp_demo 2 | tiny RTSP(RFC2326) streaming server for H.264 or H.265 video 3 | -------------------------------------------------------------------------------- /md5.h: -------------------------------------------------------------------------------- 1 | #ifndef __MD5_H__ 2 | #define __MD5_H__ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #ifdef __cplusplus 10 | extern "C" 11 | { 12 | #endif 13 | 14 | void md5(uint8_t *initial_msg, size_t initial_len, uint8_t *out_md5_str); 15 | 16 | #ifdef __cplusplus 17 | } 18 | #endif 19 | #endif 20 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Object files 5 | *.o 6 | *.ko 7 | *.obj 8 | *.elf 9 | 10 | # Linker output 11 | *.ilk 12 | *.map 13 | *.exp 14 | 15 | # Precompiled Headers 16 | *.gch 17 | *.pch 18 | 19 | # Libraries 20 | *.lib 21 | *.a 22 | *.la 23 | *.lo 24 | 25 | # Shared objects (inc. Windows DLLs) 26 | *.dll 27 | *.so 28 | *.so.* 29 | *.dylib 30 | 31 | # Executables 32 | *.exe 33 | *.out 34 | *.app 35 | *.i*86 36 | *.x86_64 37 | *.hex 38 | 39 | # Debug files 40 | *.dSYM/ 41 | *.su 42 | *.idb 43 | *.pdb 44 | 45 | # Kernel Module Compile Results 46 | *.mod* 47 | *.cmd 48 | .tmp_versions/ 49 | modules.order 50 | Module.symvers 51 | Mkfile.old 52 | dkms.conf 53 | -------------------------------------------------------------------------------- /stream_queue.h: -------------------------------------------------------------------------------- 1 | #ifndef __STREAM_QUEUE_H__ 2 | #define __STREAM_QUEUE_H__ 3 | 4 | #ifdef __cplusplus 5 | extern "C" 6 | { 7 | #endif 8 | 9 | struct stream_queue 10 | { 11 | int pktsiz; 12 | int nbpkts; 13 | int head; 14 | int tail; 15 | int *pktlen; 16 | char *buf; 17 | }; 18 | 19 | struct stream_queue *streamq_alloc(int pktsiz, int nbpkts); 20 | int streamq_query(struct stream_queue *q, int index, char **ppacket, int **ppktlen); 21 | int streamq_inused(struct stream_queue *q, int index); 22 | int streamq_next(struct stream_queue *q, int index); 23 | int streamq_head(struct stream_queue *q); 24 | int streamq_tail(struct stream_queue *q); 25 | int streamq_push(struct stream_queue *q); 26 | int streamq_pop(struct stream_queue *q); 27 | void streamq_free(struct stream_queue *q); 28 | 29 | #ifdef __cplusplus 30 | } 31 | #endif 32 | #endif 33 | -------------------------------------------------------------------------------- /rtp_enc.h: -------------------------------------------------------------------------------- 1 | #ifndef __RTP_ENC_H__ 2 | #define __RTP_ENC_H__ 3 | 4 | #include 5 | 6 | #ifdef __cplusplus 7 | extern "C" 8 | { 9 | #endif 10 | 11 | typedef struct __rtp_enc 12 | { 13 | uint8_t pt; 14 | uint16_t seq; 15 | uint32_t ssrc; 16 | uint32_t sample_rate; 17 | } rtp_enc; 18 | 19 | int rtp_enc_h264(rtp_enc *e, const uint8_t *frame, int len, uint64_t ts, uint8_t *packets[], int pktsizs[]); 20 | int rtp_enc_h265(rtp_enc *e, const uint8_t *frame, int len, uint64_t ts, uint8_t *packets[], int pktsizs[]); 21 | int rtp_enc_aac(rtp_enc *e, const uint8_t *frame, int len, uint64_t ts, uint8_t *packets[], int pktsizs[]); 22 | int rtp_enc_g711(rtp_enc *e, const uint8_t *frame, int len, uint64_t ts, uint8_t *packets[], int pktsizs[]); 23 | int rtp_enc_g726(rtp_enc *e, const uint8_t *frame, int len, uint64_t ts, uint8_t *packets[], int pktsizs[]); 24 | 25 | #ifdef __cplusplus 26 | } 27 | #endif 28 | #endif 29 | -------------------------------------------------------------------------------- /comm.h: -------------------------------------------------------------------------------- 1 | #ifndef __COMM_H__ 2 | #define __COMM_H__ 3 | 4 | #include 5 | 6 | #define __LINUX__ 1 7 | 8 | #define dbg(fmt, ...) \ 9 | do \ 10 | { \ 11 | printf("[DEBUG %s:%d] " fmt, __FUNCTION__, __LINE__, ##__VA_ARGS__); \ 12 | } while (0) 13 | #define info(fmt, ...) \ 14 | do \ 15 | { \ 16 | printf("[INFO %s:%d] " fmt, __FUNCTION__, __LINE__, ##__VA_ARGS__); \ 17 | } while (0) 18 | #define warn(fmt, ...) \ 19 | do \ 20 | { \ 21 | printf("[WARN %s:%d] " fmt, __FUNCTION__, __LINE__, ##__VA_ARGS__); \ 22 | } while (0) 23 | #define err(fmt, ...) \ 24 | do \ 25 | { \ 26 | printf("[ERROR %s:%d] " fmt, __FUNCTION__, __LINE__, ##__VA_ARGS__); \ 27 | } while (0) 28 | 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /rtsp_demo.h: -------------------------------------------------------------------------------- 1 | #ifndef __RTSP_DEMO_H__ 2 | #define __RTSP_DEMO_H__ 3 | /* 4 | * a simple RTSP server demo 5 | * RTP over UDP/TCP H264/G711a 6 | * */ 7 | 8 | #include 9 | 10 | #ifdef __cplusplus 11 | extern "C" 12 | { 13 | #endif 14 | 15 | enum rtsp_codec_id 16 | { 17 | RTSP_CODEC_ID_NONE = 0, 18 | RTSP_CODEC_ID_VIDEO_H264 = 0x0001, /*codec_data is SPS + PPS frames*/ 19 | RTSP_CODEC_ID_VIDEO_H265, /*codec_data is VPS + SPS + PPS frames*/ 20 | RTSP_CODEC_ID_VIDEO_MPEG4, /*now not support*/ 21 | RTSP_CODEC_ID_AUDIO_G711A = 0x4001, /*codec_data is NULL*/ 22 | RTSP_CODEC_ID_AUDIO_G711U, /*codec_data is NULL*/ 23 | RTSP_CODEC_ID_AUDIO_G726, /*codec_data is bitrate (int)*/ 24 | RTSP_CODEC_ID_AUDIO_AAC, /*codec_data is audio specific config (2bytes). frame type is ADTS*/ 25 | }; 26 | 27 | typedef void *rtsp_demo_handle; 28 | typedef void *rtsp_session_handle; 29 | 30 | rtsp_demo_handle rtsp_new_demo(int port); 31 | 32 | int rtsp_do_event(rtsp_demo_handle demo); 33 | 34 | // if the username and password are empty, authentication is disabled 35 | rtsp_session_handle rtsp_new_session(rtsp_demo_handle demo, const char *path, const char *username, const char *password); 36 | 37 | int rtsp_set_video(rtsp_session_handle session, int codec_id, const uint8_t *codec_data, int data_len); 38 | int rtsp_set_audio(rtsp_session_handle session, int codec_id, const uint8_t *codec_data, int data_len); 39 | 40 | int rtsp_tx_video(rtsp_session_handle session, const uint8_t *frame, int len, uint64_t ts); 41 | int rtsp_tx_audio(rtsp_session_handle session, const uint8_t *frame, int len, uint64_t ts); 42 | 43 | void rtsp_del_session(rtsp_session_handle session); 44 | void rtsp_del_demo(rtsp_demo_handle demo); 45 | 46 | uint64_t rtsp_get_reltime(void); 47 | uint64_t rtsp_get_ntptime(void); 48 | 49 | int rtsp_sync_video_ts(rtsp_session_handle session, uint64_t ts, uint64_t ntptime); 50 | int rtsp_sync_audio_ts(rtsp_session_handle session, uint64_t ts, uint64_t ntptime); 51 | 52 | #ifdef __cplusplus 53 | } 54 | #endif 55 | #endif 56 | -------------------------------------------------------------------------------- /stream_queue.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "comm.h" 4 | #include "stream_queue.h" 5 | 6 | struct stream_queue *streamq_alloc(int pktsiz, int nbpkts) 7 | { 8 | struct stream_queue *q; 9 | 10 | if (pktsiz <= 0 || nbpkts <= 0) 11 | return NULL; 12 | 13 | q = (struct stream_queue *)calloc(1, sizeof(struct stream_queue) + pktsiz * nbpkts + sizeof(int) * nbpkts); 14 | if (!q) 15 | { 16 | err("alloc memory failed for stream_queue\n"); 17 | return NULL; 18 | } 19 | 20 | q->pktsiz = pktsiz; 21 | q->nbpkts = nbpkts; 22 | q->pktlen = (int *)(((char *)q) + sizeof(struct stream_queue)); 23 | q->buf = (char *)(((char *)q) + sizeof(struct stream_queue) + sizeof(int) * nbpkts); 24 | 25 | return q; 26 | } 27 | 28 | int streamq_query(struct stream_queue *q, int index, char **ppacket, int **ppktlen) 29 | { 30 | if (!q || index >= q->nbpkts) 31 | return -1; 32 | if (ppacket) 33 | *ppacket = q->buf + index * q->pktsiz; 34 | if (ppktlen) 35 | *ppktlen = &q->pktlen[index]; 36 | return 0; 37 | } 38 | 39 | int streamq_inused(struct stream_queue *q, int index) 40 | { 41 | if (!q) 42 | return -1; 43 | if ((q->head <= index && index < q->tail) || (q->head > q->tail && (index >= q->head || index < q->tail))) 44 | return 1; 45 | return 0; 46 | } 47 | 48 | int streamq_next(struct stream_queue *q, int index) 49 | { 50 | if (!q) 51 | return -1; 52 | 53 | index = (index + 1) % q->nbpkts; 54 | return index; 55 | } 56 | 57 | int streamq_head(struct stream_queue *q) 58 | { 59 | if (!q) 60 | return -1; 61 | return q->head; 62 | } 63 | 64 | int streamq_tail(struct stream_queue *q) 65 | { 66 | if (!q) 67 | return -1; 68 | return q->tail; 69 | } 70 | 71 | int streamq_push(struct stream_queue *q) 72 | { 73 | if (!q) 74 | return -1; 75 | if ((q->tail + 1) % q->nbpkts == q->head) 76 | return -1; 77 | q->tail = (q->tail + 1) % q->nbpkts; 78 | return q->tail; 79 | } 80 | 81 | int streamq_pop(struct stream_queue *q) 82 | { 83 | if (!q) 84 | return -1; 85 | if (q->head == q->tail) 86 | return -1; 87 | q->head = (q->head + 1) % q->nbpkts; 88 | return q->head; 89 | } 90 | 91 | void streamq_free(struct stream_queue *q) 92 | { 93 | if (q) 94 | { 95 | free(q); 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /utils.h: -------------------------------------------------------------------------------- 1 | #ifndef __UTILS_H__ 2 | #define __UTILS_H__ 3 | 4 | #include 5 | 6 | #ifdef __cplusplus 7 | extern "C" 8 | { 9 | #endif 10 | 11 | struct codec_data_h264 12 | { 13 | uint8_t sps[64]; //no nal leader code 001 14 | uint8_t pps[64]; 15 | uint32_t sps_len; 16 | uint32_t pps_len; 17 | }; 18 | 19 | struct codec_data_h265 20 | { 21 | uint8_t vps[64]; 22 | uint8_t sps[64]; 23 | uint8_t pps[64]; 24 | uint32_t vps_len; 25 | uint32_t sps_len; 26 | uint32_t pps_len; 27 | }; 28 | 29 | struct codec_data_g726 30 | { 31 | uint32_t bit_rate; 32 | }; 33 | 34 | struct codec_data_aac 35 | { 36 | uint8_t audio_specific_config[64]; 37 | uint32_t audio_specific_config_len; 38 | uint32_t sample_rate; 39 | uint32_t channels; 40 | }; 41 | 42 | const uint8_t *rtsp_find_h264_h265_nalu(const uint8_t *buff, int len, int *size); 43 | 44 | int rtsp_codec_data_parse_from_user_h264(const uint8_t *codec_data, int data_len, struct codec_data_h264 *pst_codec_data); 45 | int rtsp_codec_data_parse_from_user_h265(const uint8_t *codec_data, int data_len, struct codec_data_h265 *pst_codec_data); 46 | int rtsp_codec_data_parse_from_user_g726(const uint8_t *codec_data, int data_len, struct codec_data_g726 *pst_codec_data); 47 | int rtsp_codec_data_parse_from_user_aac(const uint8_t *codec_data, int data_len, struct codec_data_aac *pst_codec_data); 48 | 49 | int rtsp_codec_data_parse_from_frame_h264(const uint8_t *frame, int len, struct codec_data_h264 *pst_codec_data); 50 | int rtsp_codec_data_parse_from_frame_h265(const uint8_t *frame, int len, struct codec_data_h265 *pst_codec_data); 51 | int rtsp_codec_data_parse_from_frame_aac(const uint8_t *frame, int len, struct codec_data_aac *pst_codec_data); 52 | 53 | int rtsp_build_sdp_media_attr_h264(int pt, int sample_rate, const struct codec_data_h264 *pst_codec_data, char *sdpbuf, int maxlen); 54 | int rtsp_build_sdp_media_attr_h265(int pt, int sample_rate, const struct codec_data_h265 *pst_codec_data, char *sdpbuf, int maxlen); 55 | int rtsp_build_sdp_media_attr_g711a(int pt, int sample_rate, char *sdpbuf, int maxlen); 56 | int rtsp_build_sdp_media_attr_g711u(int pt, int sample_rate, char *sdpbuf, int maxlen); 57 | int rtsp_build_sdp_media_attr_g726(int pt, int sample_rate, const struct codec_data_g726 *pst_codec_data, char *sdpbuf, int maxlen); 58 | int rtsp_build_sdp_media_attr_aac(int pt, int sample_rate, const struct codec_data_aac *pst_codec_data, char *sdpbuf, int maxlen); 59 | 60 | #ifdef __cplusplus 61 | } 62 | #endif 63 | #endif 64 | -------------------------------------------------------------------------------- /md5.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Simple MD5 implementation fork from https://gist.github.com/creationix/4710780 3 | * 4 | * Compile with: gcc -o md5 -O3 -lm md5.c 5 | */ 6 | 7 | #include "md5.h" 8 | 9 | // leftrotate function definition 10 | #define LEFTROTATE(x, c) (((x) << (c)) | ((x) >> (32 - (c)))) 11 | 12 | void md5(uint8_t *initial_msg, size_t initial_len, uint8_t *out_md5_str) 13 | { 14 | // These vars will contain the hash 15 | uint32_t h0, h1, h2, h3; 16 | 17 | // Message (to prepare) 18 | uint8_t *msg = NULL; 19 | 20 | // Note: All variables are unsigned 32 bit and wrap modulo 2^32 when calculating 21 | 22 | // r specifies the per-round shift amounts 23 | 24 | uint32_t r[] = {7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 25 | 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 26 | 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 27 | 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21}; 28 | 29 | // Use binary integer part of the sines of integers (in radians) as constants// Initialize variables: 30 | uint32_t k[] = { 31 | 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 32 | 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501, 33 | 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, 34 | 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, 35 | 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa, 36 | 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8, 37 | 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, 38 | 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a, 39 | 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, 40 | 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70, 41 | 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05, 42 | 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665, 43 | 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, 44 | 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1, 45 | 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, 46 | 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391}; 47 | 48 | h0 = 0x67452301; 49 | h1 = 0xefcdab89; 50 | h2 = 0x98badcfe; 51 | h3 = 0x10325476; 52 | 53 | // Pre-processing: adding a single 1 bit 54 | //append "1" bit to message 55 | /* Notice: the input bytes are considered as bits strings, 56 | where the first bit is the most significant bit of the byte.[37] */ 57 | 58 | // Pre-processing: padding with zeros 59 | //append "0" bit until message length in bit ≡ 448 (mod 512) 60 | //append length mod (2 pow 64) to message 61 | 62 | int new_len = ((((initial_len + 8) / 64) + 1) * 64) - 8; 63 | 64 | msg = calloc(new_len + 64, 1); // also appends "0" bits 65 | // (we alloc also 64 extra bytes...) 66 | memcpy(msg, initial_msg, initial_len); 67 | msg[initial_len] = 128; // write the "1" bit 68 | 69 | uint32_t bits_len = 8 * initial_len; // note, we append the len 70 | memcpy(msg + new_len, &bits_len, 4); // in bits at the end of the buffer 71 | 72 | // Process the message in successive 512-bit chunks: 73 | //for each 512-bit chunk of message: 74 | int offset; 75 | for (offset = 0; offset < new_len; offset += (512 / 8)) 76 | { 77 | 78 | // break chunk into sixteen 32-bit words w[j], 0 ≤ j ≤ 15 79 | uint32_t *w = (uint32_t *)(msg + offset); 80 | 81 | // Initialize hash value for this chunk: 82 | uint32_t a = h0; 83 | uint32_t b = h1; 84 | uint32_t c = h2; 85 | uint32_t d = h3; 86 | 87 | // Main loop: 88 | uint32_t i; 89 | for (i = 0; i < 64; i++) 90 | { 91 | uint32_t f, g; 92 | 93 | if (i < 16) 94 | { 95 | f = (b & c) | ((~b) & d); 96 | g = i; 97 | } 98 | else if (i < 32) 99 | { 100 | f = (d & b) | ((~d) & c); 101 | g = (5 * i + 1) % 16; 102 | } 103 | else if (i < 48) 104 | { 105 | f = b ^ c ^ d; 106 | g = (3 * i + 5) % 16; 107 | } 108 | else 109 | { 110 | f = c ^ (b | (~d)); 111 | g = (7 * i) % 16; 112 | } 113 | 114 | uint32_t temp = d; 115 | d = c; 116 | c = b; 117 | b = b + LEFTROTATE((a + f + k[i] + w[g]), r[i]); 118 | a = temp; 119 | } 120 | 121 | // Add this chunk's hash to result so far: 122 | 123 | h0 += a; 124 | h1 += b; 125 | h2 += c; 126 | h3 += d; 127 | } 128 | 129 | // cleanup 130 | free(msg); 131 | 132 | uint8_t *p0, *p1, *p2, *p3; 133 | 134 | p0 = (uint8_t *)&h0; 135 | p1 = (uint8_t *)&h1; 136 | p2 = (uint8_t *)&h2; 137 | p3 = (uint8_t *)&h3; 138 | 139 | sprintf(out_md5_str, 140 | "%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x", 141 | p0[0], p0[1], p0[2], p0[3], 142 | p1[0], p1[1], p1[2], p1[3], 143 | p2[0], p2[1], p2[2], p2[3], 144 | p3[0], p3[1], p3[2], p3[3]); 145 | } -------------------------------------------------------------------------------- /rtp_enc.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "comm.h" 6 | #include "rtp_enc.h" 7 | 8 | struct rtphdr 9 | { 10 | #ifdef __BIG_ENDIAN__ 11 | uint16_t v : 2; 12 | uint16_t p : 1; 13 | uint16_t x : 1; 14 | uint16_t cc : 4; 15 | uint16_t m : 1; 16 | uint16_t pt : 7; 17 | #else 18 | uint16_t cc : 4; 19 | uint16_t x : 1; 20 | uint16_t p : 1; 21 | uint16_t v : 2; 22 | uint16_t pt : 7; 23 | uint16_t m : 1; 24 | #endif 25 | uint16_t seq; 26 | uint32_t ts; 27 | uint32_t ssrc; 28 | }; 29 | 30 | #define RTPHDR_SIZE (12) 31 | 32 | int rtp_enc_h264(rtp_enc *e, const uint8_t *frame, int len, uint64_t ts, uint8_t *packets[], int pktsizs[]) 33 | { 34 | int count = 0; 35 | uint8_t nalhdr; 36 | uint32_t rtp_ts; 37 | 38 | if (!e || !frame || len <= 0 || !packets || !pktsizs) 39 | return -1; 40 | 41 | //drop 0001 42 | if (frame[0] == 0 && frame[1] == 0 && frame[2] == 1) 43 | { 44 | frame += 3; 45 | len -= 3; 46 | } 47 | if (frame[0] == 0 && frame[1] == 0 && frame[2] == 0 && frame[3] == 1) 48 | { 49 | frame += 4; 50 | len -= 4; 51 | } 52 | 53 | nalhdr = frame[0]; 54 | rtp_ts = (uint32_t)(ts * e->sample_rate / 1000000); 55 | 56 | while (len > 0 && packets[count] && pktsizs[count] > RTPHDR_SIZE) 57 | { 58 | struct rtphdr *hdr = (struct rtphdr *)packets[count]; 59 | int pktsiz = pktsizs[count]; 60 | hdr->v = 2; 61 | hdr->p = 0; 62 | hdr->x = 0; 63 | hdr->cc = 0; 64 | hdr->m = 0; 65 | hdr->pt = e->pt; 66 | hdr->seq = htons(e->seq++); 67 | hdr->ts = htonl(rtp_ts); 68 | hdr->ssrc = htonl(e->ssrc); 69 | 70 | if (count == 0 && len <= pktsiz - RTPHDR_SIZE) 71 | { 72 | hdr->m = 1; 73 | memcpy(packets[count] + RTPHDR_SIZE, frame, len); 74 | pktsizs[count] = RTPHDR_SIZE + len; 75 | frame += len; 76 | len -= len; 77 | } 78 | else 79 | { 80 | int mark = 0; 81 | if (count == 0) 82 | { 83 | frame++; //drop nalu header 84 | len--; 85 | } 86 | else if (len <= pktsiz - RTPHDR_SIZE - 2) 87 | { 88 | mark = 1; 89 | } 90 | hdr->m = mark; 91 | 92 | packets[count][RTPHDR_SIZE + 0] = (nalhdr & 0xe0) | 28; //FU-A 93 | packets[count][RTPHDR_SIZE + 1] = (nalhdr & 0x1f); //FU-A 94 | if (count == 0) 95 | { 96 | packets[count][RTPHDR_SIZE + 1] |= 0x80; //S 97 | } 98 | 99 | if (mark) 100 | { 101 | packets[count][RTPHDR_SIZE + 1] |= 0x40; //E 102 | memcpy(packets[count] + RTPHDR_SIZE + 2, frame, len); 103 | pktsizs[count] = RTPHDR_SIZE + 2 + len; 104 | frame += len; 105 | len -= len; 106 | } 107 | else 108 | { 109 | memcpy(packets[count] + RTPHDR_SIZE + 2, frame, pktsiz - RTPHDR_SIZE - 2); 110 | pktsizs[count] = pktsiz; 111 | frame += pktsiz - RTPHDR_SIZE - 2; 112 | len -= pktsiz - RTPHDR_SIZE - 2; 113 | } 114 | } 115 | count++; 116 | } 117 | return count; 118 | } 119 | 120 | int rtp_enc_h265(rtp_enc *e, const uint8_t *frame, int len, uint64_t ts, uint8_t *packets[], int pktsizs[]) 121 | { 122 | int count = 0; 123 | uint8_t nalhdr[2]; 124 | uint32_t rtp_ts; 125 | 126 | if (!e || !frame || len <= 0 || !packets || !pktsizs) 127 | return -1; 128 | 129 | //drop 0001 130 | if (frame[0] == 0 && frame[1] == 0 && frame[2] == 1) 131 | { 132 | frame += 3; 133 | len -= 3; 134 | } 135 | if (frame[0] == 0 && frame[1] == 0 && frame[2] == 0 && frame[3] == 1) 136 | { 137 | frame += 4; 138 | len -= 4; 139 | } 140 | 141 | nalhdr[0] = frame[0]; 142 | nalhdr[1] = frame[1]; 143 | rtp_ts = (uint32_t)(ts * e->sample_rate / 1000000); 144 | 145 | while (len > 0 && packets[count] && pktsizs[count] > RTPHDR_SIZE) 146 | { 147 | struct rtphdr *hdr = (struct rtphdr *)packets[count]; 148 | int pktsiz = pktsizs[count]; 149 | hdr->v = 2; 150 | hdr->p = 0; 151 | hdr->x = 0; 152 | hdr->cc = 0; 153 | hdr->m = 0; 154 | hdr->pt = e->pt; 155 | hdr->seq = htons(e->seq++); 156 | hdr->ts = htonl(rtp_ts); 157 | hdr->ssrc = htonl(e->ssrc); 158 | 159 | if (count == 0 && len <= pktsiz - RTPHDR_SIZE) 160 | { 161 | hdr->m = 1; 162 | memcpy(packets[count] + RTPHDR_SIZE, frame, len); 163 | pktsizs[count] = RTPHDR_SIZE + len; 164 | frame += len; 165 | len -= len; 166 | } 167 | else 168 | { 169 | int mark = 0; 170 | if (count == 0) 171 | { 172 | frame += 2; //drop nalu header 173 | len -= 2; 174 | } 175 | else if (len <= pktsiz - RTPHDR_SIZE - 3) 176 | { 177 | mark = 1; 178 | } 179 | hdr->m = mark; 180 | 181 | packets[count][RTPHDR_SIZE + 0] = (nalhdr[0] & 0x81) | (49 << 1); //FU-A 182 | packets[count][RTPHDR_SIZE + 1] = (nalhdr[1]); 183 | packets[count][RTPHDR_SIZE + 2] = ((nalhdr[0] >> 1) & 0x3f); //FU-A 184 | if (count == 0) 185 | { 186 | packets[count][RTPHDR_SIZE + 2] |= 0x80; //S 187 | } 188 | 189 | if (mark) 190 | { 191 | packets[count][RTPHDR_SIZE + 2] |= 0x40; //E 192 | memcpy(packets[count] + RTPHDR_SIZE + 3, frame, len); 193 | pktsizs[count] = RTPHDR_SIZE + 3 + len; 194 | frame += len; 195 | len -= len; 196 | } 197 | else 198 | { 199 | memcpy(packets[count] + RTPHDR_SIZE + 3, frame, pktsiz - RTPHDR_SIZE - 3); 200 | pktsizs[count] = pktsiz; 201 | frame += pktsiz - RTPHDR_SIZE - 3; 202 | len -= pktsiz - RTPHDR_SIZE - 3; 203 | } 204 | } 205 | count++; 206 | } 207 | return count; 208 | } 209 | 210 | int rtp_enc_aac(rtp_enc *e, const uint8_t *frame, int len, uint64_t ts, uint8_t *packets[], int pktsizs[]) 211 | { 212 | int count = 0; 213 | uint32_t rtp_ts; 214 | uint32_t au_len; 215 | 216 | if (!e || !frame || len <= 0 || !packets || !pktsizs) 217 | return -1; 218 | 219 | //drop fff 220 | if (frame[0] == 0xff && (frame[1] & 0xf0) == 0xf0) 221 | { 222 | frame += 7; 223 | len -= 7; 224 | } 225 | 226 | rtp_ts = (uint32_t)(ts * e->sample_rate / 1000000); 227 | au_len = len; 228 | 229 | while (len > 0 && packets[count] && pktsizs[count] > RTPHDR_SIZE + 4) 230 | { 231 | struct rtphdr *hdr = (struct rtphdr *)packets[count]; 232 | int pktsiz = pktsizs[count]; 233 | hdr->v = 2; 234 | hdr->p = 0; 235 | hdr->x = 0; 236 | hdr->cc = 0; 237 | hdr->m = 0; 238 | hdr->pt = e->pt; 239 | hdr->seq = htons(e->seq++); 240 | hdr->ts = htonl(rtp_ts); 241 | hdr->ssrc = htonl(e->ssrc); 242 | 243 | packets[count][RTPHDR_SIZE + 0] = 0x00; 244 | packets[count][RTPHDR_SIZE + 1] = 0x10; 245 | packets[count][RTPHDR_SIZE + 2] = au_len >> 5; 246 | packets[count][RTPHDR_SIZE + 3] = (au_len & 0x1f) << 3; 247 | 248 | if (len <= pktsiz - RTPHDR_SIZE - 4) 249 | { 250 | hdr->m = 1; 251 | memcpy(packets[count] + RTPHDR_SIZE + 4, frame, len); 252 | pktsizs[count] = RTPHDR_SIZE + 4 + len; 253 | frame += len; 254 | len -= len; 255 | } 256 | else 257 | { 258 | memcpy(packets[count] + RTPHDR_SIZE + 4, frame, pktsiz - RTPHDR_SIZE - 4); 259 | pktsizs[count] = pktsiz; 260 | frame += pktsiz - RTPHDR_SIZE - 4; 261 | len -= pktsiz - RTPHDR_SIZE - 4; 262 | } 263 | count++; 264 | } 265 | 266 | return count; 267 | } 268 | 269 | int rtp_enc_g711(rtp_enc *e, const uint8_t *frame, int len, uint64_t ts, uint8_t *packets[], int pktsizs[]) 270 | { 271 | int count = 0; 272 | uint32_t rtp_ts; 273 | 274 | if (!e || !frame || len <= 0 || !packets || !pktsizs) 275 | return -1; 276 | 277 | rtp_ts = (uint32_t)(ts * e->sample_rate / 1000000); 278 | while (len > 0 && packets[count] && pktsizs[count] > RTPHDR_SIZE) 279 | { 280 | struct rtphdr *hdr = (struct rtphdr *)packets[count]; 281 | int pktsiz = pktsizs[count]; 282 | hdr->v = 2; 283 | hdr->p = 0; 284 | hdr->x = 0; 285 | hdr->cc = 0; 286 | hdr->m = (e->seq == 0); 287 | hdr->pt = e->pt; 288 | hdr->seq = htons(e->seq++); 289 | hdr->ts = htonl(rtp_ts); 290 | hdr->ssrc = htonl(e->ssrc); 291 | 292 | if (len <= pktsiz - RTPHDR_SIZE) 293 | { 294 | memcpy(packets[count] + RTPHDR_SIZE, frame, len); 295 | pktsizs[count] = RTPHDR_SIZE + len; 296 | frame += len; 297 | len -= len; 298 | } 299 | else 300 | { 301 | memcpy(packets[count] + RTPHDR_SIZE, frame, pktsiz - RTPHDR_SIZE); 302 | pktsizs[count] = pktsiz; 303 | frame += pktsiz - RTPHDR_SIZE; 304 | len -= pktsiz - RTPHDR_SIZE; 305 | } 306 | count++; 307 | } 308 | 309 | return count; 310 | } 311 | 312 | int rtp_enc_g726(rtp_enc *e, const uint8_t *frame, int len, uint64_t ts, uint8_t *packets[], int pktsizs[]) 313 | { 314 | return rtp_enc_g711(e, frame, len, ts, packets, pktsizs); 315 | } 316 | -------------------------------------------------------------------------------- /rtsp_msg.h: -------------------------------------------------------------------------------- 1 | #ifndef __RTSP_MSG_H__ 2 | #define __RTSP_MSG_H__ 3 | 4 | #include 5 | 6 | #ifdef __cplusplus 7 | extern "C" 8 | { 9 | #endif 10 | 11 | //RTSP/1.0 Message Parse/Build 12 | 13 | typedef enum __rtsp_msg_type_e 14 | { 15 | RTSP_MSG_TYPE_REQUEST = 0, 16 | RTSP_MSG_TYPE_RESPONSE, 17 | RTSP_MSG_TYPE_INTERLEAVED, 18 | RTSP_MSG_TYPE_BUTT, 19 | } rtsp_msg_type_e; 20 | 21 | typedef enum __rtsp_msg_method_e 22 | { 23 | RTSP_MSG_METHOD_OPTIONS = 0, 24 | RTSP_MSG_METHOD_DESCRIBE, 25 | RTSP_MSG_METHOD_SETUP, 26 | RTSP_MSG_METHOD_PLAY, 27 | RTSP_MSG_METHOD_RECORD, 28 | RTSP_MSG_METHOD_PAUSE, 29 | RTSP_MSG_METHOD_TEARDOWN, 30 | RTSP_MSG_METHOD_ANNOUNCE, 31 | RTSP_MSG_METHOD_SET_PARAMETER, 32 | RTSP_MSG_METHOD_GET_PARAMETER, 33 | RTSP_MSG_METHOD_REDIRECT, 34 | RTSP_MSG_METHOD_BUTT, 35 | } rtsp_msg_method_e; 36 | 37 | typedef enum __rtsp_msg_uri_scheme_e 38 | { 39 | RTSP_MSG_URI_SCHEME_RTSP = 0, 40 | RTSP_MSG_URI_SCHEME_RTSPU, 41 | RTSP_MSG_URI_SCHEME_BUTT, 42 | } rtsp_msg_uri_scheme_e; 43 | 44 | typedef struct __rtsp_msg_uri_s 45 | { 46 | rtsp_msg_uri_scheme_e scheme; 47 | uint16_t port; 48 | char ipaddr[32]; 49 | char abspath[64]; 50 | } rtsp_msg_uri_s; 51 | 52 | typedef enum __rtsp_msg_version_e 53 | { 54 | RTSP_MSG_VERSION_1_0 = 0, 55 | RTSP_MSG_VERSION_BUTT, 56 | } rtsp_msg_version_e; 57 | 58 | typedef struct __rtsp_msg_request_line_s 59 | { 60 | rtsp_msg_method_e method; 61 | rtsp_msg_uri_s uri; 62 | rtsp_msg_version_e version; 63 | } rtsp_msg_request_line_s; 64 | 65 | typedef struct __rtsp_msg_response_line_s 66 | { 67 | rtsp_msg_version_e version; 68 | uint32_t status_code; 69 | } rtsp_msg_response_line_s; 70 | 71 | typedef struct __rtsp_msg_interleaved_line_s 72 | { 73 | uint8_t channel; 74 | uint16_t length; 75 | uint8_t reserved; 76 | } rtsp_msg_interleaved_line_s; 77 | 78 | //CSeq g req. all 79 | typedef struct __rtsp_msg_cseq_s 80 | { 81 | uint32_t cseq; 82 | } rtsp_msg_cseq_s; 83 | 84 | //Date g opt. all 85 | typedef struct __rtsp_msg_date_s 86 | { 87 | char http_date[32]; 88 | } rtsp_msg_date_s; 89 | 90 | //Session Rr req. all but SETUP,OPTIONS 91 | typedef struct __rtsp_msg_session_s 92 | { 93 | uint32_t session; 94 | } rtsp_msg_session_s; 95 | 96 | typedef enum __rtsp_msg_transport_type_e 97 | { 98 | RTSP_MSG_TRANSPORT_TYPE_RTP_AVP = 0, //RTPoverUDP 99 | RTSP_MSG_TRANSPORT_TYPE_RTP_AVP_TCP, //RTPoverTCP 100 | RTSP_MSG_TRANSPORT_TYPE_BUTT, 101 | } rtsp_msg_transport_type_e; 102 | 103 | //Transport Rr req. SETUP 104 | typedef struct __rtsp_msg_transport_s 105 | { 106 | rtsp_msg_transport_type_e type; 107 | uint32_t flags; 108 | #define RTSP_MSG_TRANSPORT_FLAG_SSRC (1 << 0) 109 | #define RTSP_MSG_TRANSPORT_FLAG_UNICAST (1 << 1) 110 | #define RTSP_MSG_TRANSPORT_FLAG_MULTICAST (1 << 2) 111 | #define RTSP_MSG_TRANSPORT_FLAG_CLIENT_PORT (1 << 3) 112 | #define RTSP_MSG_TRANSPORT_FLAG_SERVER_PORT (1 << 4) 113 | #define RTSP_MSG_TRANSPORT_FLAG_INTERLEAVED (1 << 5) 114 | uint32_t ssrc; 115 | uint16_t client_port; //rtcp is rtp + 1 116 | uint16_t server_port; 117 | uint8_t interleaved; 118 | } rtsp_msg_transport_s; 119 | 120 | typedef enum __rtsp_msg_time_type_e 121 | { 122 | RTSP_MSG_TIME_TYPE_SMPTE = 0, 123 | RTSP_MSG_TIME_TYPE_NPT, 124 | RTSP_MSG_TIME_TYPE_UTC, 125 | RTSP_MSG_TIME_TYPE_BUTT, 126 | } rtsp_msg_time_type_e; 127 | 128 | typedef struct __rtsp_msg_time_smpte_s 129 | { 130 | //10:07:33:05.01 131 | uint32_t seconds; //10*3600 + 07*60 + 33 132 | uint32_t subframes; //05*100 + 01 133 | } rtsp_msg_time_smpte_s; 134 | 135 | typedef struct __rtsp_msg_time_npt_s 136 | { 137 | //123.45 138 | uint32_t secords; //123 139 | uint32_t usecords; //45 140 | } rtsp_msg_time_npt_s; 141 | 142 | typedef struct __rtsp_msg_time_utc_s 143 | { 144 | //19961108T142730.25Z 145 | uint32_t secords; //1996/11/08 14:27:30 - 1900/1/1 0:0:0 146 | uint32_t usecords; //25 147 | } rtsp_msg_time_utc_s; 148 | 149 | //Range Rr opt. PLAY,PAUSE,RECORD 150 | typedef struct __rtsp_msg_range_s 151 | { 152 | rtsp_msg_time_type_e type; 153 | union __start_u 154 | { 155 | rtsp_msg_time_smpte_s smpte; 156 | rtsp_msg_time_npt_s npt; 157 | rtsp_msg_time_utc_s utc; 158 | } start; 159 | union __end_u 160 | { 161 | rtsp_msg_time_smpte_s smpte; 162 | rtsp_msg_time_npt_s npt; 163 | rtsp_msg_time_utc_s utc; 164 | } end; 165 | } rtsp_msg_range_s; 166 | 167 | typedef enum __rtsp_msg_content_type_e 168 | { 169 | RTSP_MSG_CONTENT_TYPE_SDP = 0, 170 | RTSP_MSG_CONTENT_TYPE_RTSL, 171 | RTSP_MSG_CONTENT_TYPE_MHEG, 172 | RTSP_MSG_CONTENT_TYPE_BUTT, 173 | } rtsp_msg_content_type_e; 174 | 175 | //Accept R opt. entity 176 | typedef struct __rtsp_msg_accept_s 177 | { 178 | uint32_t accept; 179 | #define RTSP_MSG_ACCEPT_SDP (1 << RTSP_MSG_CONTENT_TYPE_SDP) 180 | #define RTSP_MSG_ACCEPT_RTSL (1 << RTSP_MSG_CONTENT_TYPE_RTSL) 181 | #define RTSP_MSG_ACCEPT_MHEG (1 << RTSP_MSG_CONTENT_TYPE_MHEG) 182 | } rtsp_msg_accept_s; 183 | 184 | //WWW-Authenticate R opt. all 185 | typedef struct __rtsp_msg_www_authenticate_s 186 | { 187 | char realm[64]; 188 | char nonce[64]; 189 | } rtsp_msg_www_authenticate_s; 190 | 191 | //Authorization R opt. all 192 | typedef struct __rtsp_msg_authorization_s 193 | { 194 | char username[64]; 195 | char uri[64]; 196 | char response[64]; 197 | } rtsp_msg_authorization_s; 198 | 199 | //User-Agent R opt. all 200 | typedef struct __rtsp_msg_user_agent_s 201 | { 202 | char user_agent[64]; 203 | } rtsp_msg_user_agent_s; 204 | 205 | //Public r opt. all 206 | typedef struct __rtsp_msg_public_s 207 | { 208 | uint32_t public_; 209 | #define RTSP_MSG_PUBLIC_OPTIONS (1 << RTSP_MSG_METHOD_OPTIONS) 210 | #define RTSP_MSG_PUBLIC_DESCRIBE (1 << RTSP_MSG_METHOD_DESCRIBE) 211 | #define RTSP_MSG_PUBLIC_SETUP (1 << RTSP_MSG_METHOD_SETUP) 212 | #define RTSP_MSG_PUBLIC_PLAY (1 << RTSP_MSG_METHOD_PLAY) 213 | #define RTSP_MSG_PUBLIC_RECORD (1 << RTSP_MSG_METHOD_RECORD) 214 | #define RTSP_MSG_PUBLIC_PAUSE (1 << RTSP_MSG_METHOD_PAUSE) 215 | #define RTSP_MSG_PUBLIC_TEARDOWN (1 << RTSP_MSG_METHOD_TEARDOWN) 216 | #define RTSP_MSG_PUBLIC_ANNOUNCE (1 << RTSP_MSG_METHOD_ANNOUNCE) 217 | #define RTSP_MSG_PUBLIC_SET_PARAMETER (1 << RTSP_MSG_METHOD_SET_PARAMETER) 218 | #define RTSP_MSG_PUBLIC_GET_PARAMETER (1 << RTSP_MSG_METHOD_GET_PARAMETER) 219 | #define RTSP_MSG_PUBLIC_REDIRECT (1 << RTSP_MSG_METHOD_REDIRECT) 220 | } rtsp_msg_public_s; 221 | 222 | typedef struct __rtsp_msg_rtp_subinfo_s 223 | { 224 | rtsp_msg_uri_s url; 225 | uint32_t isseq; 226 | union __param_u 227 | { 228 | uint32_t rtptime; 229 | uint32_t seq; 230 | } param; 231 | } rtsp_msg_rtp_subinfo_s; 232 | 233 | //RTP-Info r req. PLAY 234 | typedef struct __rtsp_msg_rtp_info_s 235 | { 236 | uint32_t ninfos; 237 | rtsp_msg_rtp_subinfo_s **info_array; 238 | } rtsp_msg_rtp_info_s; 239 | 240 | //Server r opt. all 241 | typedef struct __rtsp_msg_server_s 242 | { 243 | char server[64]; 244 | } rtsp_msg_server_s; 245 | 246 | //Content-Length e req. SET_PARAMETER,ANNOUNCE 247 | //Content-Length e req. entity 248 | typedef struct __rtsp_msg_content_length_s 249 | { 250 | uint32_t length; 251 | } rtsp_msg_content_length_s; 252 | 253 | //Content-Type e req. SET_PARAMETER,ANNOUNCE 254 | //Content-Type r req. entity 255 | typedef struct __rtsp_msg_content_type_s 256 | { 257 | rtsp_msg_content_type_e type; 258 | } rtsp_msg_content_type_s; 259 | 260 | typedef struct __rtsp_msg_hdr_s 261 | { 262 | union __start_line_u 263 | { 264 | rtsp_msg_request_line_s reqline; 265 | rtsp_msg_response_line_s resline; 266 | rtsp_msg_interleaved_line_s interline; 267 | } startline; 268 | 269 | //general-headers 270 | rtsp_msg_cseq_s *cseq; 271 | rtsp_msg_date_s *date; 272 | rtsp_msg_session_s *session; 273 | rtsp_msg_transport_s *transport; 274 | rtsp_msg_range_s *range; 275 | 276 | //request-headers 277 | rtsp_msg_accept_s *accept; 278 | rtsp_msg_www_authenticate_s *www_authenticate; 279 | rtsp_msg_user_agent_s *user_agent; 280 | 281 | //response-headers 282 | rtsp_msg_public_s *public_; 283 | rtsp_msg_rtp_info_s *rtp_info; 284 | rtsp_msg_authorization_s *authorization; 285 | rtsp_msg_server_s *server; 286 | 287 | //entity-headers 288 | rtsp_msg_content_length_s *content_length; 289 | rtsp_msg_content_type_s *content_type; 290 | } rtsp_msg_hdr_s; 291 | 292 | typedef struct __rtsp_msg_body_s 293 | { 294 | void *body; 295 | } rtsp_msg_body_s; 296 | 297 | typedef struct __rtsp_msg_s 298 | { 299 | rtsp_msg_type_e type; 300 | rtsp_msg_hdr_s hdrs; 301 | rtsp_msg_body_s body; 302 | } rtsp_msg_s; 303 | 304 | //bases 305 | void *rtsp_mem_alloc(int size); 306 | void rtsp_mem_free(void *ptr); 307 | void *rtsp_mem_dup(const void *ptr, int size); 308 | char *rtsp_str_dup(const char *str); 309 | 310 | int rtsp_msg_init(rtsp_msg_s *msg); 311 | void rtsp_msg_free(rtsp_msg_s *msg); 312 | 313 | //return data's bytes which is parsed. when success 314 | //return 0. when data is not enough 315 | //return -1. when data is invalid 316 | int rtsp_msg_parse_from_array(rtsp_msg_s *msg, const void *data, int size); 317 | 318 | //return data's bytes which is used. when success 319 | //return -1. when failed 320 | int rtsp_msg_build_to_array(const rtsp_msg_s *msg, void *data, int size); 321 | 322 | //utils XXX 323 | int rtsp_msg_set_request(rtsp_msg_s *msg, rtsp_msg_method_e mt, const char *ipaddr, const char *abspath); 324 | int rtsp_msg_set_response(rtsp_msg_s *msg, int status_code); 325 | int rtsp_msg_get_cseq(const rtsp_msg_s *msg, uint32_t *cseq); 326 | int rtsp_msg_set_cseq(rtsp_msg_s *msg, uint32_t cseq); 327 | int rtsp_msg_get_session(const rtsp_msg_s *msg, uint32_t *session); 328 | int rtsp_msg_set_session(rtsp_msg_s *msg, uint32_t session); 329 | int rtsp_msg_get_date(const rtsp_msg_s *msg, char *date, int len); 330 | int rtsp_msg_set_date(rtsp_msg_s *msg, const char *date); 331 | int rtsp_msg_set_transport_udp(rtsp_msg_s *msg, uint32_t ssrc, int client_port, int server_port); 332 | int rtsp_msg_set_transport_tcp(rtsp_msg_s *msg, uint32_t ssrc, int interleaved); 333 | int rtsp_msg_get_accept(const rtsp_msg_s *msg, uint32_t *accept); 334 | int rtsp_msg_set_accept(rtsp_msg_s *msg, uint32_t accept); 335 | int rtsp_msg_get_user_agent(const rtsp_msg_s *msg, char *user_agent, int len); 336 | int rtsp_msg_set_user_agent(rtsp_msg_s *msg, const char *user_agent); 337 | int rtsp_msg_get_public(const rtsp_msg_s *msg, uint32_t *public_); 338 | int rtsp_msg_set_public(rtsp_msg_s *msg, uint32_t public_); 339 | int rtsp_msg_get_server(const rtsp_msg_s *msg, char *server, int len); 340 | int rtsp_msg_set_server(rtsp_msg_s *msg, const char *server); 341 | int rtsp_msg_get_content_type(const rtsp_msg_s *msg, int *type); 342 | int rtsp_msg_set_content_type(rtsp_msg_s *msg, int type); 343 | int rtsp_msg_get_content_length(const rtsp_msg_s *msg, int *length); 344 | int rtsp_msg_set_content_length(rtsp_msg_s *msg, int length); 345 | int rtsp_msg_set_www_authenticate(rtsp_msg_s *msg, char *nonce, char *realm); 346 | const char *rtsp_req_msg_method_int2str(int intval); 347 | int rtsp_req_msg_method_str2int(const char *str); 348 | 349 | uint32_t rtsp_msg_gen_session_id(void); 350 | 351 | #ifdef __cplusplus 352 | } 353 | #endif 354 | #endif 355 | -------------------------------------------------------------------------------- /utils.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "comm.h" 3 | #include "utils.h" 4 | 5 | /***************************************************************************** 6 | * b64_encode: Stolen from VLC's http.c. 7 | * Simplified by Michael. 8 | * Fixed edge cases and made it work from data (vs. strings) by Ryan. 9 | *****************************************************************************/ 10 | static char *base64_encode(char *out, int out_size, const uint8_t *in, int in_size) 11 | { 12 | static const char b64[] = 13 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 14 | char *ret, *dst; 15 | unsigned i_bits = 0; 16 | int i_shift = 0; 17 | int bytes_remaining = in_size; 18 | 19 | #define __UINT_MAX (~0lu) 20 | #define __BASE64_SIZE(x) (((x) + 2) / 3 * 4 + 1) 21 | #define __RB32(x) \ 22 | (((uint32_t)((const uint8_t *)(x))[0] << 24) | \ 23 | (((const uint8_t *)(x))[1] << 16) | \ 24 | (((const uint8_t *)(x))[2] << 8) | \ 25 | ((const uint8_t *)(x))[3]) 26 | if (in_size >= __UINT_MAX / 4 || 27 | out_size < __BASE64_SIZE(in_size)) 28 | return NULL; 29 | ret = dst = out; 30 | while (bytes_remaining > 3) 31 | { 32 | i_bits = __RB32(in); 33 | in += 3; 34 | bytes_remaining -= 3; 35 | *dst++ = b64[i_bits >> 26]; 36 | *dst++ = b64[(i_bits >> 20) & 0x3F]; 37 | *dst++ = b64[(i_bits >> 14) & 0x3F]; 38 | *dst++ = b64[(i_bits >> 8) & 0x3F]; 39 | } 40 | i_bits = 0; 41 | while (bytes_remaining) 42 | { 43 | i_bits = (i_bits << 8) + *in++; 44 | bytes_remaining--; 45 | i_shift += 8; 46 | } 47 | while (i_shift > 0) 48 | { 49 | *dst++ = b64[(i_bits << 6 >> i_shift) & 0x3f]; 50 | i_shift -= 6; 51 | } 52 | while ((dst - ret) & 3) 53 | *dst++ = '='; 54 | *dst = '\0'; 55 | 56 | return ret; 57 | } 58 | 59 | const uint8_t *rtsp_find_h264_h265_nalu(const uint8_t *buff, int len, int *size) 60 | { 61 | const uint8_t *s = NULL; 62 | while (len >= 3) 63 | { 64 | if (buff[0] == 0 && buff[1] == 0 && buff[2] == 1) 65 | { 66 | if (!s) 67 | { 68 | if (len < 4) 69 | return NULL; 70 | s = buff; 71 | } 72 | else 73 | { 74 | *size = (buff - s); 75 | return s; 76 | } 77 | buff += 3; 78 | len -= 3; 79 | continue; 80 | } 81 | if (len >= 4 && buff[0] == 0 && buff[1] == 0 && buff[2] == 0 && buff[3] == 1) 82 | { 83 | if (!s) 84 | { 85 | if (len < 5) 86 | return NULL; 87 | s = buff; 88 | } 89 | else 90 | { 91 | *size = (buff - s); 92 | return s; 93 | } 94 | buff += 4; 95 | len -= 4; 96 | continue; 97 | } 98 | buff++; 99 | len--; 100 | } 101 | if (!s) 102 | return NULL; 103 | *size = (buff - s + len); 104 | return s; 105 | } 106 | 107 | const uint8_t *rtsp_find_aac_adts(const uint8_t *buff, int len, int *size) 108 | { 109 | const uint8_t *s = buff; 110 | while (len > 2) 111 | { 112 | if (s[0] == 0xff && (s[1] & 0xf0) == 0xf0) 113 | { 114 | break; 115 | } 116 | buff++; 117 | len--; 118 | } 119 | 120 | if (len <= 2) 121 | return NULL; 122 | 123 | //aac_frame_length 124 | *size = 0; 125 | *size |= (s[3] & 3) << 11; 126 | *size |= (s[4] << 3); 127 | *size |= (s[5] >> 5); 128 | 129 | if (*size > len) 130 | return NULL; 131 | 132 | return s; 133 | } 134 | 135 | int rtsp_codec_data_parse_from_user_h264(const uint8_t *codec_data, int data_len, struct codec_data_h264 *pst_codec_data) 136 | { 137 | const uint8_t *s = codec_data; 138 | const uint8_t *frame = NULL; 139 | int len = data_len; 140 | int size = 0; 141 | int ret = 0; 142 | 143 | while (len > 3) 144 | { 145 | uint8_t type = 0; 146 | if (pst_codec_data->sps_len > 0 && pst_codec_data->pps_len > 0) 147 | { 148 | break; 149 | } 150 | 151 | frame = rtsp_find_h264_h265_nalu(s, len, &size); 152 | if (!frame) 153 | { 154 | break; 155 | } 156 | 157 | len = len - (frame - s + size); 158 | s = frame + size; 159 | 160 | if (frame[2] == 0) 161 | { 162 | frame += 4; //drop 0001 163 | size -= 4; 164 | } 165 | else 166 | { 167 | frame += 3; //drop 001 168 | size -= 3; 169 | } 170 | 171 | type = frame[0] & 0x1f; 172 | if (type == 7) 173 | { 174 | dbg("sps %d\n", size); 175 | if (size > (int)sizeof(pst_codec_data->sps)) 176 | size = sizeof(pst_codec_data->sps); 177 | memcpy(pst_codec_data->sps, frame, size); 178 | pst_codec_data->sps_len = size; 179 | ret++; 180 | } 181 | if (type == 8) 182 | { 183 | dbg("pps %d\n", size); 184 | if (size > (int)sizeof(pst_codec_data->pps)) 185 | size = sizeof(pst_codec_data->pps); 186 | memcpy(pst_codec_data->pps, frame, size); 187 | pst_codec_data->pps_len = size; 188 | ret++; 189 | } 190 | } 191 | 192 | return (ret >= 2 ? 1 : 0); 193 | } 194 | 195 | int rtsp_codec_data_parse_from_user_h265(const uint8_t *codec_data, int data_len, struct codec_data_h265 *pst_codec_data) 196 | { 197 | const uint8_t *s = codec_data; 198 | const uint8_t *frame = NULL; 199 | int len = data_len; 200 | int size = 0; 201 | int ret = 0; 202 | 203 | while (len > 3) 204 | { 205 | uint8_t type = 0; 206 | if (pst_codec_data->vps_len > 0 && pst_codec_data->sps_len > 0 && pst_codec_data->pps_len > 0) 207 | { 208 | break; 209 | } 210 | 211 | frame = rtsp_find_h264_h265_nalu(s, len, &size); 212 | if (!frame) 213 | { 214 | break; 215 | } 216 | 217 | len = len - (frame - s + size); 218 | s = frame + size; 219 | 220 | if (frame[2] == 0) 221 | { 222 | frame += 4; //drop 0001 223 | size -= 4; 224 | } 225 | else 226 | { 227 | frame += 3; //drop 001 228 | size -= 3; 229 | } 230 | 231 | type = (frame[0] >> 1) & 0x3f; 232 | if (type == 32) 233 | { 234 | dbg("vps %d\n", size); 235 | if (size > (int)sizeof(pst_codec_data->vps)) 236 | size = sizeof(pst_codec_data->vps); 237 | memcpy(pst_codec_data->vps, frame, size); 238 | pst_codec_data->vps_len = size; 239 | ret++; 240 | } 241 | if (type == 33) 242 | { 243 | dbg("sps %d\n", size); 244 | if (size > (int)sizeof(pst_codec_data->sps)) 245 | size = sizeof(pst_codec_data->sps); 246 | memcpy(pst_codec_data->sps, frame, size); 247 | pst_codec_data->sps_len = size; 248 | ret++; 249 | } 250 | if (type == 34) 251 | { 252 | dbg("pps %d\n", size); 253 | if (size > (int)sizeof(pst_codec_data->pps)) 254 | size = sizeof(pst_codec_data->pps); 255 | memcpy(pst_codec_data->pps, frame, size); 256 | pst_codec_data->pps_len = size; 257 | ret++; 258 | } 259 | } 260 | 261 | return (ret >= 3 ? 1 : 0); 262 | } 263 | 264 | int rtsp_codec_data_parse_from_user_g726(const uint8_t *codec_data, int data_len, struct codec_data_g726 *pst_codec_data) 265 | { 266 | int bit_rate; 267 | 268 | if (data_len != sizeof(bit_rate)) 269 | { 270 | err("bit rate invalid\n"); 271 | return -1; 272 | } 273 | 274 | bit_rate = *((int *)codec_data); 275 | 276 | switch (bit_rate) 277 | { 278 | case 16000: 279 | case 24000: 280 | case 32000: 281 | case 40000: 282 | break; 283 | default: 284 | err("bit rate invalid\n"); 285 | return -1; 286 | } 287 | 288 | pst_codec_data->bit_rate = bit_rate; 289 | return 1; 290 | } 291 | 292 | int rtsp_codec_data_parse_from_user_aac(const uint8_t *codec_data, int data_len, struct codec_data_aac *pst_codec_data) 293 | { 294 | int sample_rate_index, channels; 295 | const uint32_t sample_rate_tbl[16] = {96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350, 0, 0, 0}; 296 | if (data_len != 2) 297 | { 298 | err("audio specific config invalid\n"); 299 | return -1; 300 | } 301 | 302 | sample_rate_index = ((codec_data[0] & 0x7) << 1) | (codec_data[1] >> 7); 303 | channels = (codec_data[1] >> 3) & 0x0f; 304 | 305 | if (sample_rate_index > 12 && channels > 7) 306 | { 307 | err("audio specific config invalid\n"); 308 | return -1; 309 | } 310 | 311 | memcpy(pst_codec_data->audio_specific_config, codec_data, data_len); 312 | pst_codec_data->audio_specific_config_len = data_len; 313 | pst_codec_data->sample_rate = sample_rate_tbl[sample_rate_index]; 314 | pst_codec_data->channels = (channels == 7) ? 8 : channels; 315 | dbg("config=%02X%02X sample_rate=%d channels=%d\n", 316 | pst_codec_data->audio_specific_config[0], pst_codec_data->audio_specific_config[1], 317 | sample_rate_tbl[sample_rate_index], channels); 318 | 319 | return 1; 320 | } 321 | 322 | int rtsp_codec_data_parse_from_frame_h264(const uint8_t *frame, int len, struct codec_data_h264 *pst_codec_data) 323 | { 324 | return rtsp_codec_data_parse_from_user_h264(frame, len, pst_codec_data); 325 | } 326 | 327 | int rtsp_codec_data_parse_from_frame_h265(const uint8_t *frame, int len, struct codec_data_h265 *pst_codec_data) 328 | { 329 | return rtsp_codec_data_parse_from_user_h265(frame, len, pst_codec_data); 330 | } 331 | 332 | int rtsp_codec_data_parse_from_frame_aac(const uint8_t *frame, int len, struct codec_data_aac *pst_codec_data) 333 | { 334 | int profile, sample_rate_index, channels; 335 | const uint32_t sample_rate_tbl[16] = {96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350, 0, 0, 0}; 336 | int size = 0; 337 | 338 | if (pst_codec_data->audio_specific_config_len > 0) 339 | return 0; 340 | 341 | frame = rtsp_find_aac_adts(frame, len, &size); 342 | if (!frame) 343 | { 344 | err("find adts header failed\n"); 345 | return -1; 346 | } 347 | 348 | profile = frame[2] >> 6; 349 | sample_rate_index = (frame[2] >> 2) & 0x0f; 350 | channels = ((frame[2] & 0x1) << 1) | (frame[3] >> 6); 351 | 352 | if (sample_rate_index > 12 && channels > 7) 353 | { 354 | err("audio specific config invalid\n"); 355 | return -1; 356 | } 357 | 358 | pst_codec_data->audio_specific_config[0] = ((profile + 1) << 3) | ((sample_rate_index >> 1) & 0x7); 359 | pst_codec_data->audio_specific_config[1] = ((sample_rate_index & 0x1) << 7) | (channels << 3); 360 | pst_codec_data->audio_specific_config_len = 2; 361 | pst_codec_data->sample_rate = sample_rate_tbl[sample_rate_index]; 362 | pst_codec_data->channels = (channels == 7) ? 8 : channels; 363 | dbg("config=%02X%02X sample_rate=%d channels=%d\n", 364 | pst_codec_data->audio_specific_config[0], pst_codec_data->audio_specific_config[1], 365 | sample_rate_tbl[sample_rate_index], channels); 366 | 367 | return 1; 368 | } 369 | 370 | int rtsp_build_sdp_media_attr_h264(int pt, int sample_rate, const struct codec_data_h264 *pst_codec_data, char *sdpbuf, int maxlen) 371 | { 372 | char *p = sdpbuf; 373 | // dbg("\n"); 374 | 375 | p += sprintf(p, "m=video 0 RTP/AVP %d\r\n", pt); 376 | p += sprintf(p, "c=IN IP4 0.0.0.0\r\n"); 377 | p += sprintf(p, "a=rtpmap:%d H264/%d\r\n", pt, sample_rate); 378 | if (pst_codec_data->sps_len > 0 && pst_codec_data->pps_len > 0) 379 | { 380 | const uint8_t *sps = pst_codec_data->sps; 381 | const uint8_t *pps = pst_codec_data->pps; 382 | int sps_len = pst_codec_data->sps_len; 383 | int pps_len = pst_codec_data->pps_len; 384 | p += sprintf(p, "a=fmtp:%d packetization-mode=1;sprop-parameter-sets=", pt); 385 | base64_encode(p, (maxlen - (p - sdpbuf)), sps, sps_len); 386 | p += strlen(p); 387 | p += sprintf(p, ","); 388 | base64_encode(p, (maxlen - (p - sdpbuf)), pps, pps_len); 389 | p += strlen(p); 390 | p += sprintf(p, "\r\n"); 391 | } 392 | else 393 | { 394 | p += sprintf(p, "a=fmtp:%d packetization-mode=1\r\n", pt); 395 | } 396 | 397 | return (p - sdpbuf); 398 | } 399 | 400 | int rtsp_build_sdp_media_attr_h265(int pt, int sample_rate, const struct codec_data_h265 *pst_codec_data, char *sdpbuf, int maxlen) 401 | { 402 | char *p = sdpbuf; 403 | // dbg("\n"); 404 | 405 | p += sprintf(p, "m=video 0 RTP/AVP %d\r\n", pt); 406 | p += sprintf(p, "c=IN IP4 0.0.0.0\r\n"); 407 | p += sprintf(p, "a=rtpmap:%d H265/%d\r\n", pt, sample_rate); 408 | if (pst_codec_data->vps_len > 0 && pst_codec_data->sps_len > 0 && pst_codec_data->pps_len > 0) 409 | { 410 | const uint8_t *vps = pst_codec_data->vps; 411 | const uint8_t *sps = pst_codec_data->sps; 412 | const uint8_t *pps = pst_codec_data->pps; 413 | int vps_len = pst_codec_data->vps_len; 414 | int sps_len = pst_codec_data->sps_len; 415 | int pps_len = pst_codec_data->pps_len; 416 | 417 | p += sprintf(p, "a=fmtp:%d", pt); 418 | p += sprintf(p, " sprop-vps="); 419 | base64_encode(p, (maxlen - (p - sdpbuf)), vps, vps_len); 420 | p += strlen(p); 421 | p += sprintf(p, ";sprop-sps="); 422 | base64_encode(p, (maxlen - (p - sdpbuf)), sps, sps_len); 423 | p += strlen(p); 424 | p += sprintf(p, ";sprop-pps="); 425 | base64_encode(p, (maxlen - (p - sdpbuf)), pps, pps_len); 426 | p += strlen(p); 427 | p += sprintf(p, "\r\n"); 428 | } 429 | 430 | return (p - sdpbuf); 431 | } 432 | 433 | int rtsp_build_sdp_media_attr_g711a(int pt, int sample_rate, char *sdpbuf, int maxlen) 434 | { 435 | char *p = sdpbuf; 436 | // dbg("\n"); 437 | 438 | p += sprintf(p, "m=audio 0 RTP/AVP %d\r\n", pt); 439 | p += sprintf(p, "c=IN IP4 0.0.0.0\r\n"); 440 | p += sprintf(p, "a=rtpmap:%d PCMA/%d/1\r\n", pt, sample_rate); 441 | 442 | return (p - sdpbuf); 443 | } 444 | 445 | int rtsp_build_sdp_media_attr_g711u(int pt, int sample_rate, char *sdpbuf, int maxlen) 446 | { 447 | char *p = sdpbuf; 448 | // dbg("\n"); 449 | 450 | p += sprintf(p, "m=audio 0 RTP/AVP %d\r\n", pt); 451 | p += sprintf(p, "c=IN IP4 0.0.0.0\r\n"); 452 | p += sprintf(p, "a=rtpmap:%d PCMU/%d/1\r\n", pt, sample_rate); 453 | 454 | return (p - sdpbuf); 455 | } 456 | 457 | int rtsp_build_sdp_media_attr_g726(int pt, int sample_rate, const struct codec_data_g726 *pst_codec_data, char *sdpbuf, int maxlen) 458 | { 459 | char *p = sdpbuf; 460 | // dbg("\n"); 461 | 462 | p += sprintf(p, "m=audio 0 RTP/AVP %d\r\n", pt); 463 | p += sprintf(p, "c=IN IP4 0.0.0.0\r\n"); 464 | p += sprintf(p, "a=rtpmap:%d G726-%d/%d/1\r\n", pt, 465 | pst_codec_data->bit_rate ? pst_codec_data->bit_rate / 1000 : 32, 466 | sample_rate); 467 | 468 | return (p - sdpbuf); 469 | } 470 | 471 | int rtsp_build_sdp_media_attr_aac(int pt, int sample_rate, const struct codec_data_aac *pst_codec_data, char *sdpbuf, int maxlen) 472 | { 473 | char *p = sdpbuf; 474 | // dbg("\n"); 475 | 476 | p += sprintf(p, "m=audio 0 RTP/AVP %d\r\n", pt); 477 | p += sprintf(p, "c=IN IP4 0.0.0.0\r\n"); 478 | p += sprintf(p, "a=rtpmap:%d MPEG4-GENERIC/%d/%d\r\n", pt, 479 | pst_codec_data->sample_rate ? pst_codec_data->sample_rate : 44100, 480 | pst_codec_data->channels ? pst_codec_data->channels : 2); 481 | 482 | if (pst_codec_data->audio_specific_config_len == 2) 483 | { 484 | p += sprintf(p, "a=fmtp:%d profile-level-id=1;mode=AAC-hbr;sizelength=13;indexlength=3;indexdeltalength=3;config=%02X%02X\r\n", 485 | pt, pst_codec_data->audio_specific_config[0], pst_codec_data->audio_specific_config[1]); 486 | } 487 | else 488 | { 489 | p += sprintf(p, "a=fmtp:%d profile-level-id=1;mode=AAC-hbr;sizelength=13;indexlength=3;indexdeltalength=3\r\n", pt); 490 | } 491 | return (p - sdpbuf); 492 | } 493 | -------------------------------------------------------------------------------- /queue.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 1991, 1993 3 | * The Regents of the University of California. All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 3. Neither the name of the University nor the names of its contributors 14 | * may be used to endorse or promote products derived from this software 15 | * without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 | * SUCH DAMAGE. 28 | * 29 | * @(#)queue.h 8.5 (Berkeley) 8/20/94 30 | */ 31 | 32 | #ifndef _SYS_QUEUE_H_ 33 | #define _SYS_QUEUE_H_ 34 | 35 | /* 36 | * This file defines five types of data structures: singly-linked lists, 37 | * lists, simple queues, tail queues, and circular queues. 38 | * 39 | * A singly-linked list is headed by a single forward pointer. The 40 | * elements are singly linked for minimum space and pointer manipulation 41 | * overhead at the expense of O(n) removal for arbitrary elements. New 42 | * elements can be added to the list after an existing element or at the 43 | * head of the list. Elements being removed from the head of the list 44 | * should use the explicit macro for this purpose for optimum 45 | * efficiency. A singly-linked list may only be traversed in the forward 46 | * direction. Singly-linked lists are ideal for applications with large 47 | * datasets and few or no removals or for implementing a LIFO queue. 48 | * 49 | * A list is headed by a single forward pointer (or an array of forward 50 | * pointers for a hash table header). The elements are doubly linked 51 | * so that an arbitrary element can be removed without a need to 52 | * traverse the list. New elements can be added to the list before 53 | * or after an existing element or at the head of the list. A list 54 | * may only be traversed in the forward direction. 55 | * 56 | * A simple queue is headed by a pair of pointers, one the head of the 57 | * list and the other to the tail of the list. The elements are singly 58 | * linked to save space, so elements can only be removed from the 59 | * head of the list. New elements can be added to the list after 60 | * an existing element, at the head of the list, or at the end of the 61 | * list. A simple queue may only be traversed in the forward direction. 62 | * 63 | * A tail queue is headed by a pair of pointers, one to the head of the 64 | * list and the other to the tail of the list. The elements are doubly 65 | * linked so that an arbitrary element can be removed without a need to 66 | * traverse the list. New elements can be added to the list before or 67 | * after an existing element, at the head of the list, or at the end of 68 | * the list. A tail queue may be traversed in either direction. 69 | * 70 | * A circle queue is headed by a pair of pointers, one to the head of the 71 | * list and the other to the tail of the list. The elements are doubly 72 | * linked so that an arbitrary element can be removed without a need to 73 | * traverse the list. New elements can be added to the list before or after 74 | * an existing element, at the head of the list, or at the end of the list. 75 | * A circle queue may be traversed in either direction, but has a more 76 | * complex end of list detection. 77 | * 78 | * For details on the use of these macros, see the queue(3) manual page. 79 | */ 80 | 81 | /* 82 | * List definitions. 83 | */ 84 | #define LIST_HEAD(name, type) \ 85 | struct name \ 86 | { \ 87 | struct type *lh_first; /* first element */ \ 88 | } 89 | 90 | #define LIST_HEAD_INITIALIZER(head) \ 91 | { \ 92 | NULL \ 93 | } 94 | 95 | #define LIST_ENTRY(type) \ 96 | struct \ 97 | { \ 98 | struct type *le_next; /* next element */ \ 99 | struct type **le_prev; /* address of previous next element */ \ 100 | } 101 | 102 | /* 103 | * List functions. 104 | */ 105 | #define LIST_INIT(head) \ 106 | do \ 107 | { \ 108 | (head)->lh_first = NULL; \ 109 | } while (/*CONSTCOND*/ 0) 110 | 111 | #define LIST_INSERT_AFTER(listelm, elm, field) \ 112 | do \ 113 | { \ 114 | if (((elm)->field.le_next = (listelm)->field.le_next) != NULL) \ 115 | (listelm)->field.le_next->field.le_prev = \ 116 | &(elm)->field.le_next; \ 117 | (listelm)->field.le_next = (elm); \ 118 | (elm)->field.le_prev = &(listelm)->field.le_next; \ 119 | } while (/*CONSTCOND*/ 0) 120 | 121 | #define LIST_INSERT_BEFORE(listelm, elm, field) \ 122 | do \ 123 | { \ 124 | (elm)->field.le_prev = (listelm)->field.le_prev; \ 125 | (elm)->field.le_next = (listelm); \ 126 | *(listelm)->field.le_prev = (elm); \ 127 | (listelm)->field.le_prev = &(elm)->field.le_next; \ 128 | } while (/*CONSTCOND*/ 0) 129 | 130 | #define LIST_INSERT_HEAD(head, elm, field) \ 131 | do \ 132 | { \ 133 | if (((elm)->field.le_next = (head)->lh_first) != NULL) \ 134 | (head)->lh_first->field.le_prev = &(elm)->field.le_next; \ 135 | (head)->lh_first = (elm); \ 136 | (elm)->field.le_prev = &(head)->lh_first; \ 137 | } while (/*CONSTCOND*/ 0) 138 | 139 | #define LIST_REMOVE(elm, field) \ 140 | do \ 141 | { \ 142 | if ((elm)->field.le_next != NULL) \ 143 | (elm)->field.le_next->field.le_prev = \ 144 | (elm)->field.le_prev; \ 145 | *(elm)->field.le_prev = (elm)->field.le_next; \ 146 | } while (/*CONSTCOND*/ 0) 147 | 148 | #define LIST_FOREACH(var, head, field) \ 149 | for ((var) = ((head)->lh_first); \ 150 | (var); \ 151 | (var) = ((var)->field.le_next)) 152 | 153 | /* 154 | * List access methods. 155 | */ 156 | #define LIST_EMPTY(head) ((head)->lh_first == NULL) 157 | #define LIST_FIRST(head) ((head)->lh_first) 158 | #define LIST_NEXT(elm, field) ((elm)->field.le_next) 159 | 160 | /* 161 | * Singly-linked List definitions. 162 | */ 163 | #define SLIST_HEAD(name, type) \ 164 | struct name \ 165 | { \ 166 | struct type *slh_first; /* first element */ \ 167 | } 168 | 169 | #define SLIST_HEAD_INITIALIZER(head) \ 170 | { \ 171 | NULL \ 172 | } 173 | 174 | #define SLIST_ENTRY(type) \ 175 | struct \ 176 | { \ 177 | struct type *sle_next; /* next element */ \ 178 | } 179 | 180 | /* 181 | * Singly-linked List functions. 182 | */ 183 | #define SLIST_INIT(head) \ 184 | do \ 185 | { \ 186 | (head)->slh_first = NULL; \ 187 | } while (/*CONSTCOND*/ 0) 188 | 189 | #define SLIST_INSERT_AFTER(slistelm, elm, field) \ 190 | do \ 191 | { \ 192 | (elm)->field.sle_next = (slistelm)->field.sle_next; \ 193 | (slistelm)->field.sle_next = (elm); \ 194 | } while (/*CONSTCOND*/ 0) 195 | 196 | #define SLIST_INSERT_HEAD(head, elm, field) \ 197 | do \ 198 | { \ 199 | (elm)->field.sle_next = (head)->slh_first; \ 200 | (head)->slh_first = (elm); \ 201 | } while (/*CONSTCOND*/ 0) 202 | 203 | #define SLIST_REMOVE_HEAD(head, field) \ 204 | do \ 205 | { \ 206 | (head)->slh_first = (head)->slh_first->field.sle_next; \ 207 | } while (/*CONSTCOND*/ 0) 208 | 209 | #define SLIST_REMOVE(head, elm, type, field) \ 210 | do \ 211 | { \ 212 | if ((head)->slh_first == (elm)) \ 213 | { \ 214 | SLIST_REMOVE_HEAD((head), field); \ 215 | } \ 216 | else \ 217 | { \ 218 | struct type *curelm = (head)->slh_first; \ 219 | while (curelm->field.sle_next != (elm)) \ 220 | curelm = curelm->field.sle_next; \ 221 | curelm->field.sle_next = \ 222 | curelm->field.sle_next->field.sle_next; \ 223 | } \ 224 | } while (/*CONSTCOND*/ 0) 225 | 226 | #define SLIST_FOREACH(var, head, field) \ 227 | for ((var) = (head)->slh_first; (var); (var) = (var)->field.sle_next) 228 | 229 | /* 230 | * Singly-linked List access methods. 231 | */ 232 | #define SLIST_EMPTY(head) ((head)->slh_first == NULL) 233 | #define SLIST_FIRST(head) ((head)->slh_first) 234 | #define SLIST_NEXT(elm, field) ((elm)->field.sle_next) 235 | 236 | /* 237 | * Singly-linked Tail queue declarations. 238 | */ 239 | #define STAILQ_HEAD(name, type) \ 240 | struct name \ 241 | { \ 242 | struct type *stqh_first; /* first element */ \ 243 | struct type **stqh_last; /* addr of last next element */ \ 244 | } 245 | 246 | #define STAILQ_HEAD_INITIALIZER(head) \ 247 | { \ 248 | NULL, &(head).stqh_first \ 249 | } 250 | 251 | #define STAILQ_ENTRY(type) \ 252 | struct \ 253 | { \ 254 | struct type *stqe_next; /* next element */ \ 255 | } 256 | 257 | /* 258 | * Singly-linked Tail queue functions. 259 | */ 260 | #define STAILQ_INIT(head) \ 261 | do \ 262 | { \ 263 | (head)->stqh_first = NULL; \ 264 | (head)->stqh_last = &(head)->stqh_first; \ 265 | } while (/*CONSTCOND*/ 0) 266 | 267 | #define STAILQ_INSERT_HEAD(head, elm, field) \ 268 | do \ 269 | { \ 270 | if (((elm)->field.stqe_next = (head)->stqh_first) == NULL) \ 271 | (head)->stqh_last = &(elm)->field.stqe_next; \ 272 | (head)->stqh_first = (elm); \ 273 | } while (/*CONSTCOND*/ 0) 274 | 275 | #define STAILQ_INSERT_TAIL(head, elm, field) \ 276 | do \ 277 | { \ 278 | (elm)->field.stqe_next = NULL; \ 279 | *(head)->stqh_last = (elm); \ 280 | (head)->stqh_last = &(elm)->field.stqe_next; \ 281 | } while (/*CONSTCOND*/ 0) 282 | 283 | #define STAILQ_INSERT_AFTER(head, listelm, elm, field) \ 284 | do \ 285 | { \ 286 | if (((elm)->field.stqe_next = (listelm)->field.stqe_next) == NULL) \ 287 | (head)->stqh_last = &(elm)->field.stqe_next; \ 288 | (listelm)->field.stqe_next = (elm); \ 289 | } while (/*CONSTCOND*/ 0) 290 | 291 | #define STAILQ_REMOVE_HEAD(head, field) \ 292 | do \ 293 | { \ 294 | if (((head)->stqh_first = (head)->stqh_first->field.stqe_next) == NULL) \ 295 | (head)->stqh_last = &(head)->stqh_first; \ 296 | } while (/*CONSTCOND*/ 0) 297 | 298 | #define STAILQ_REMOVE(head, elm, type, field) \ 299 | do \ 300 | { \ 301 | if ((head)->stqh_first == (elm)) \ 302 | { \ 303 | STAILQ_REMOVE_HEAD((head), field); \ 304 | } \ 305 | else \ 306 | { \ 307 | struct type *curelm = (head)->stqh_first; \ 308 | while (curelm->field.stqe_next != (elm)) \ 309 | curelm = curelm->field.stqe_next; \ 310 | if ((curelm->field.stqe_next = \ 311 | curelm->field.stqe_next->field.stqe_next) == NULL) \ 312 | (head)->stqh_last = &(curelm)->field.stqe_next; \ 313 | } \ 314 | } while (/*CONSTCOND*/ 0) 315 | 316 | #define STAILQ_FOREACH(var, head, field) \ 317 | for ((var) = ((head)->stqh_first); \ 318 | (var); \ 319 | (var) = ((var)->field.stqe_next)) 320 | 321 | #define STAILQ_CONCAT(head1, head2) \ 322 | do \ 323 | { \ 324 | if (!STAILQ_EMPTY((head2))) \ 325 | { \ 326 | *(head1)->stqh_last = (head2)->stqh_first; \ 327 | (head1)->stqh_last = (head2)->stqh_last; \ 328 | STAILQ_INIT((head2)); \ 329 | } \ 330 | } while (/*CONSTCOND*/ 0) 331 | 332 | /* 333 | * Singly-linked Tail queue access methods. 334 | */ 335 | #define STAILQ_EMPTY(head) ((head)->stqh_first == NULL) 336 | #define STAILQ_FIRST(head) ((head)->stqh_first) 337 | #define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next) 338 | 339 | /* 340 | * Simple queue definitions. 341 | */ 342 | #define SIMPLEQ_HEAD(name, type) \ 343 | struct name \ 344 | { \ 345 | struct type *sqh_first; /* first element */ \ 346 | struct type **sqh_last; /* addr of last next element */ \ 347 | } 348 | 349 | #define SIMPLEQ_HEAD_INITIALIZER(head) \ 350 | { \ 351 | NULL, &(head).sqh_first \ 352 | } 353 | 354 | #define SIMPLEQ_ENTRY(type) \ 355 | struct \ 356 | { \ 357 | struct type *sqe_next; /* next element */ \ 358 | } 359 | 360 | /* 361 | * Simple queue functions. 362 | */ 363 | #define SIMPLEQ_INIT(head) \ 364 | do \ 365 | { \ 366 | (head)->sqh_first = NULL; \ 367 | (head)->sqh_last = &(head)->sqh_first; \ 368 | } while (/*CONSTCOND*/ 0) 369 | 370 | #define SIMPLEQ_INSERT_HEAD(head, elm, field) \ 371 | do \ 372 | { \ 373 | if (((elm)->field.sqe_next = (head)->sqh_first) == NULL) \ 374 | (head)->sqh_last = &(elm)->field.sqe_next; \ 375 | (head)->sqh_first = (elm); \ 376 | } while (/*CONSTCOND*/ 0) 377 | 378 | #define SIMPLEQ_INSERT_TAIL(head, elm, field) \ 379 | do \ 380 | { \ 381 | (elm)->field.sqe_next = NULL; \ 382 | *(head)->sqh_last = (elm); \ 383 | (head)->sqh_last = &(elm)->field.sqe_next; \ 384 | } while (/*CONSTCOND*/ 0) 385 | 386 | #define SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) \ 387 | do \ 388 | { \ 389 | if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL) \ 390 | (head)->sqh_last = &(elm)->field.sqe_next; \ 391 | (listelm)->field.sqe_next = (elm); \ 392 | } while (/*CONSTCOND*/ 0) 393 | 394 | #define SIMPLEQ_REMOVE_HEAD(head, field) \ 395 | do \ 396 | { \ 397 | if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL) \ 398 | (head)->sqh_last = &(head)->sqh_first; \ 399 | } while (/*CONSTCOND*/ 0) 400 | 401 | #define SIMPLEQ_REMOVE(head, elm, type, field) \ 402 | do \ 403 | { \ 404 | if ((head)->sqh_first == (elm)) \ 405 | { \ 406 | SIMPLEQ_REMOVE_HEAD((head), field); \ 407 | } \ 408 | else \ 409 | { \ 410 | struct type *curelm = (head)->sqh_first; \ 411 | while (curelm->field.sqe_next != (elm)) \ 412 | curelm = curelm->field.sqe_next; \ 413 | if ((curelm->field.sqe_next = \ 414 | curelm->field.sqe_next->field.sqe_next) == NULL) \ 415 | (head)->sqh_last = &(curelm)->field.sqe_next; \ 416 | } \ 417 | } while (/*CONSTCOND*/ 0) 418 | 419 | #define SIMPLEQ_FOREACH(var, head, field) \ 420 | for ((var) = ((head)->sqh_first); \ 421 | (var); \ 422 | (var) = ((var)->field.sqe_next)) 423 | 424 | /* 425 | * Simple queue access methods. 426 | */ 427 | #define SIMPLEQ_EMPTY(head) ((head)->sqh_first == NULL) 428 | #define SIMPLEQ_FIRST(head) ((head)->sqh_first) 429 | #define SIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next) 430 | 431 | /* 432 | * Tail queue definitions. 433 | */ 434 | #define _TAILQ_HEAD(name, type, qual) \ 435 | struct name \ 436 | { \ 437 | qual type *tqh_first; /* first element */ \ 438 | qual type *qual *tqh_last; /* addr of last next element */ \ 439 | } 440 | #define TAILQ_HEAD(name, type) _TAILQ_HEAD(name, struct type, ) 441 | 442 | #define TAILQ_HEAD_INITIALIZER(head) \ 443 | { \ 444 | NULL, &(head).tqh_first \ 445 | } 446 | 447 | #define _TAILQ_ENTRY(type, qual) \ 448 | struct \ 449 | { \ 450 | qual type *tqe_next; /* next element */ \ 451 | qual type *qual *tqe_prev; /* address of previous next element */ \ 452 | } 453 | #define TAILQ_ENTRY(type) _TAILQ_ENTRY(struct type, ) 454 | 455 | /* 456 | * Tail queue functions. 457 | */ 458 | #define TAILQ_INIT(head) \ 459 | do \ 460 | { \ 461 | (head)->tqh_first = NULL; \ 462 | (head)->tqh_last = &(head)->tqh_first; \ 463 | } while (/*CONSTCOND*/ 0) 464 | 465 | #define TAILQ_INSERT_HEAD(head, elm, field) \ 466 | do \ 467 | { \ 468 | if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \ 469 | (head)->tqh_first->field.tqe_prev = \ 470 | &(elm)->field.tqe_next; \ 471 | else \ 472 | (head)->tqh_last = &(elm)->field.tqe_next; \ 473 | (head)->tqh_first = (elm); \ 474 | (elm)->field.tqe_prev = &(head)->tqh_first; \ 475 | } while (/*CONSTCOND*/ 0) 476 | 477 | #define TAILQ_INSERT_TAIL(head, elm, field) \ 478 | do \ 479 | { \ 480 | (elm)->field.tqe_next = NULL; \ 481 | (elm)->field.tqe_prev = (head)->tqh_last; \ 482 | *(head)->tqh_last = (elm); \ 483 | (head)->tqh_last = &(elm)->field.tqe_next; \ 484 | } while (/*CONSTCOND*/ 0) 485 | 486 | #define TAILQ_INSERT_AFTER(head, listelm, elm, field) \ 487 | do \ 488 | { \ 489 | if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL) \ 490 | (elm)->field.tqe_next->field.tqe_prev = \ 491 | &(elm)->field.tqe_next; \ 492 | else \ 493 | (head)->tqh_last = &(elm)->field.tqe_next; \ 494 | (listelm)->field.tqe_next = (elm); \ 495 | (elm)->field.tqe_prev = &(listelm)->field.tqe_next; \ 496 | } while (/*CONSTCOND*/ 0) 497 | 498 | #define TAILQ_INSERT_BEFORE(listelm, elm, field) \ 499 | do \ 500 | { \ 501 | (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \ 502 | (elm)->field.tqe_next = (listelm); \ 503 | *(listelm)->field.tqe_prev = (elm); \ 504 | (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \ 505 | } while (/*CONSTCOND*/ 0) 506 | 507 | #define TAILQ_REMOVE(head, elm, field) \ 508 | do \ 509 | { \ 510 | if (((elm)->field.tqe_next) != NULL) \ 511 | (elm)->field.tqe_next->field.tqe_prev = \ 512 | (elm)->field.tqe_prev; \ 513 | else \ 514 | (head)->tqh_last = (elm)->field.tqe_prev; \ 515 | *(elm)->field.tqe_prev = (elm)->field.tqe_next; \ 516 | } while (/*CONSTCOND*/ 0) 517 | 518 | #define TAILQ_FOREACH(var, head, field) \ 519 | for ((var) = ((head)->tqh_first); \ 520 | (var); \ 521 | (var) = ((var)->field.tqe_next)) 522 | 523 | #define TAILQ_FOREACH_REVERSE(var, head, headname, field) \ 524 | for ((var) = (*(((struct headname *)((head)->tqh_last))->tqh_last)); \ 525 | (var); \ 526 | (var) = (*(((struct headname *)((var)->field.tqe_prev))->tqh_last))) 527 | 528 | #define TAILQ_CONCAT(head1, head2, field) \ 529 | do \ 530 | { \ 531 | if (!TAILQ_EMPTY(head2)) \ 532 | { \ 533 | *(head1)->tqh_last = (head2)->tqh_first; \ 534 | (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \ 535 | (head1)->tqh_last = (head2)->tqh_last; \ 536 | TAILQ_INIT((head2)); \ 537 | } \ 538 | } while (/*CONSTCOND*/ 0) 539 | 540 | /* 541 | * Tail queue access methods. 542 | */ 543 | #define TAILQ_EMPTY(head) ((head)->tqh_first == NULL) 544 | #define TAILQ_FIRST(head) ((head)->tqh_first) 545 | #define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next) 546 | 547 | #define TAILQ_LAST(head, headname) \ 548 | (*(((struct headname *)((head)->tqh_last))->tqh_last)) 549 | #define TAILQ_PREV(elm, headname, field) \ 550 | (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last)) 551 | 552 | /* 553 | * Circular queue definitions. 554 | */ 555 | #define CIRCLEQ_HEAD(name, type) \ 556 | struct name \ 557 | { \ 558 | struct type *cqh_first; /* first element */ \ 559 | struct type *cqh_last; /* last element */ \ 560 | } 561 | 562 | #define CIRCLEQ_HEAD_INITIALIZER(head) \ 563 | { \ 564 | (void *)&head, (void *)&head \ 565 | } 566 | 567 | #define CIRCLEQ_ENTRY(type) \ 568 | struct \ 569 | { \ 570 | struct type *cqe_next; /* next element */ \ 571 | struct type *cqe_prev; /* previous element */ \ 572 | } 573 | 574 | /* 575 | * Circular queue functions. 576 | */ 577 | #define CIRCLEQ_INIT(head) \ 578 | do \ 579 | { \ 580 | (head)->cqh_first = (void *)(head); \ 581 | (head)->cqh_last = (void *)(head); \ 582 | } while (/*CONSTCOND*/ 0) 583 | 584 | #define CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) \ 585 | do \ 586 | { \ 587 | (elm)->field.cqe_next = (listelm)->field.cqe_next; \ 588 | (elm)->field.cqe_prev = (listelm); \ 589 | if ((listelm)->field.cqe_next == (void *)(head)) \ 590 | (head)->cqh_last = (elm); \ 591 | else \ 592 | (listelm)->field.cqe_next->field.cqe_prev = (elm); \ 593 | (listelm)->field.cqe_next = (elm); \ 594 | } while (/*CONSTCOND*/ 0) 595 | 596 | #define CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) \ 597 | do \ 598 | { \ 599 | (elm)->field.cqe_next = (listelm); \ 600 | (elm)->field.cqe_prev = (listelm)->field.cqe_prev; \ 601 | if ((listelm)->field.cqe_prev == (void *)(head)) \ 602 | (head)->cqh_first = (elm); \ 603 | else \ 604 | (listelm)->field.cqe_prev->field.cqe_next = (elm); \ 605 | (listelm)->field.cqe_prev = (elm); \ 606 | } while (/*CONSTCOND*/ 0) 607 | 608 | #define CIRCLEQ_INSERT_HEAD(head, elm, field) \ 609 | do \ 610 | { \ 611 | (elm)->field.cqe_next = (head)->cqh_first; \ 612 | (elm)->field.cqe_prev = (void *)(head); \ 613 | if ((head)->cqh_last == (void *)(head)) \ 614 | (head)->cqh_last = (elm); \ 615 | else \ 616 | (head)->cqh_first->field.cqe_prev = (elm); \ 617 | (head)->cqh_first = (elm); \ 618 | } while (/*CONSTCOND*/ 0) 619 | 620 | #define CIRCLEQ_INSERT_TAIL(head, elm, field) \ 621 | do \ 622 | { \ 623 | (elm)->field.cqe_next = (void *)(head); \ 624 | (elm)->field.cqe_prev = (head)->cqh_last; \ 625 | if ((head)->cqh_first == (void *)(head)) \ 626 | (head)->cqh_first = (elm); \ 627 | else \ 628 | (head)->cqh_last->field.cqe_next = (elm); \ 629 | (head)->cqh_last = (elm); \ 630 | } while (/*CONSTCOND*/ 0) 631 | 632 | #define CIRCLEQ_REMOVE(head, elm, field) \ 633 | do \ 634 | { \ 635 | if ((elm)->field.cqe_next == (void *)(head)) \ 636 | (head)->cqh_last = (elm)->field.cqe_prev; \ 637 | else \ 638 | (elm)->field.cqe_next->field.cqe_prev = \ 639 | (elm)->field.cqe_prev; \ 640 | if ((elm)->field.cqe_prev == (void *)(head)) \ 641 | (head)->cqh_first = (elm)->field.cqe_next; \ 642 | else \ 643 | (elm)->field.cqe_prev->field.cqe_next = \ 644 | (elm)->field.cqe_next; \ 645 | } while (/*CONSTCOND*/ 0) 646 | 647 | #define CIRCLEQ_FOREACH(var, head, field) \ 648 | for ((var) = ((head)->cqh_first); \ 649 | (var) != (const void *)(head); \ 650 | (var) = ((var)->field.cqe_next)) 651 | 652 | #define CIRCLEQ_FOREACH_REVERSE(var, head, field) \ 653 | for ((var) = ((head)->cqh_last); \ 654 | (var) != (const void *)(head); \ 655 | (var) = ((var)->field.cqe_prev)) 656 | 657 | /* 658 | * Circular queue access methods. 659 | */ 660 | #define CIRCLEQ_EMPTY(head) ((head)->cqh_first == (void *)(head)) 661 | #define CIRCLEQ_FIRST(head) ((head)->cqh_first) 662 | #define CIRCLEQ_LAST(head) ((head)->cqh_last) 663 | #define CIRCLEQ_NEXT(elm, field) ((elm)->field.cqe_next) 664 | #define CIRCLEQ_PREV(elm, field) ((elm)->field.cqe_prev) 665 | 666 | #define CIRCLEQ_LOOP_NEXT(head, elm, field) \ 667 | (((elm)->field.cqe_next == (void *)(head)) \ 668 | ? ((head)->cqh_first) \ 669 | : (elm->field.cqe_next)) 670 | #define CIRCLEQ_LOOP_PREV(head, elm, field) \ 671 | (((elm)->field.cqe_prev == (void *)(head)) \ 672 | ? ((head)->cqh_last) \ 673 | : (elm->field.cqe_prev)) 674 | 675 | #endif /* sys/queue.h */ 676 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /rtsp_msg.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "comm.h" 8 | #include "rtsp_msg.h" 9 | 10 | void *rtsp_mem_alloc(int size) 11 | { 12 | if (size > 0) 13 | return calloc(1, size); 14 | return NULL; 15 | } 16 | 17 | void rtsp_mem_free(void *ptr) 18 | { 19 | if (ptr) 20 | free(ptr); 21 | } 22 | 23 | void *rtsp_mem_dup(const void *ptr, int size) 24 | { 25 | void *ptr1 = calloc(1, size); 26 | if (ptr1 && ptr) 27 | memcpy(ptr1, ptr, size); 28 | return ptr1; 29 | } 30 | 31 | char *rtsp_str_dup(const char *str) 32 | { 33 | int len = strlen(str); 34 | char *str1 = (char *)calloc(1, len + 1); 35 | if (str1 && str) 36 | memcpy(str1, str, len); 37 | return str1; 38 | } 39 | 40 | #define ARRAY_SIZE(_arr) (sizeof(_arr) / sizeof(_arr[0])) 41 | 42 | typedef struct __rtsp_msg_int2str_tbl_s 43 | { 44 | int intval; 45 | int strsiz; 46 | const char *strval; 47 | } rtsp_msg_int2str_tbl_s; 48 | 49 | static const char *rtsp_msg_int2str(const rtsp_msg_int2str_tbl_s *tbl, int num, int intval) 50 | { 51 | int i; 52 | for (i = 0; i < num; i++) 53 | { 54 | if (intval == tbl[i].intval) 55 | return tbl[i].strval; 56 | } 57 | return tbl[num - 1].strval; 58 | } 59 | 60 | static int rtsp_msg_str2int(const rtsp_msg_int2str_tbl_s *tbl, int num, const char *str) 61 | { 62 | int i; 63 | for (i = 0; i < num; i++) 64 | { 65 | if (strncmp(tbl[i].strval, str, tbl[i].strsiz) == 0) 66 | return tbl[i].intval; 67 | } 68 | return tbl[num - 1].intval; 69 | } 70 | 71 | static const rtsp_msg_int2str_tbl_s rtsp_msg_method_tbl[] = { 72 | { 73 | RTSP_MSG_METHOD_OPTIONS, 74 | 7, 75 | "OPTIONS", 76 | }, 77 | { 78 | RTSP_MSG_METHOD_DESCRIBE, 79 | 8, 80 | "DESCRIBE", 81 | }, 82 | { 83 | RTSP_MSG_METHOD_SETUP, 84 | 5, 85 | "SETUP", 86 | }, 87 | { 88 | RTSP_MSG_METHOD_PLAY, 89 | 4, 90 | "PLAY", 91 | }, 92 | { 93 | RTSP_MSG_METHOD_RECORD, 94 | 6, 95 | "RECORD", 96 | }, 97 | { 98 | RTSP_MSG_METHOD_PAUSE, 99 | 5, 100 | "PAUSE", 101 | }, 102 | { 103 | RTSP_MSG_METHOD_TEARDOWN, 104 | 8, 105 | "TEARDOWN", 106 | }, 107 | { 108 | RTSP_MSG_METHOD_ANNOUNCE, 109 | 8, 110 | "ANNOUNCE", 111 | }, 112 | { 113 | RTSP_MSG_METHOD_SET_PARAMETER, 114 | 13, 115 | "SET_PARAMETER", 116 | }, 117 | { 118 | RTSP_MSG_METHOD_GET_PARAMETER, 119 | 13, 120 | "GET_PARAMETER", 121 | }, 122 | { 123 | RTSP_MSG_METHOD_REDIRECT, 124 | 8, 125 | "REDIRECT", 126 | }, 127 | { 128 | RTSP_MSG_METHOD_BUTT, 129 | 0, 130 | "", 131 | }, 132 | }; 133 | 134 | const char *rtsp_req_msg_method_int2str(int intval) 135 | { 136 | int i; 137 | int num; 138 | 139 | num = ARRAY_SIZE(rtsp_msg_method_tbl); 140 | for (i = 0; i < num; i++) 141 | { 142 | if (intval == rtsp_msg_method_tbl[i].intval) 143 | return rtsp_msg_method_tbl[i].strval; 144 | } 145 | return rtsp_msg_method_tbl[num - 1].strval; 146 | } 147 | 148 | int rtsp_req_msg_method_str2int(const char *str) 149 | { 150 | int i; 151 | int num; 152 | 153 | num = ARRAY_SIZE(rtsp_msg_method_tbl); 154 | for (i = 0; i < num; i++) 155 | { 156 | if (strncmp(rtsp_msg_method_tbl[i].strval, str, rtsp_msg_method_tbl[i].strsiz) == 0) 157 | return rtsp_msg_method_tbl[i].intval; 158 | } 159 | return rtsp_msg_method_tbl[num - 1].intval; 160 | } 161 | 162 | static const rtsp_msg_int2str_tbl_s rtsp_msg_uri_scheme_tbl[] = { 163 | {RTSP_MSG_URI_SCHEME_RTSPU, 6, "rtspu:"}, 164 | {RTSP_MSG_URI_SCHEME_RTSP, 5, "rtsp:"}, 165 | {RTSP_MSG_URI_SCHEME_BUTT, 0, ""}, 166 | }; 167 | 168 | static const rtsp_msg_int2str_tbl_s rtsp_msg_version_tbl[] = { 169 | {RTSP_MSG_VERSION_1_0, 8, "RTSP/1.0"}, 170 | {RTSP_MSG_VERSION_BUTT, 0, ""}, 171 | }; 172 | 173 | static const rtsp_msg_int2str_tbl_s rtsp_msg_status_code_tbl[] = { 174 | {100, 0, "Continue"}, 175 | {200, 0, "OK"}, 176 | {201, 0, "Created"}, 177 | {250, 0, "Low on Storage Space"}, 178 | {300, 0, "Multiple Choices"}, 179 | {301, 0, "Moved Permanently"}, 180 | {302, 0, "Moved Temporarily"}, 181 | {303, 0, "See Other"}, 182 | {305, 0, "Use Proxy"}, 183 | {400, 0, "Bad Request"}, 184 | {401, 0, "Unauthorized"}, 185 | {402, 0, "Payment Required"}, 186 | {403, 0, "Forbidden"}, 187 | {404, 0, "Not Found"}, 188 | {405, 0, "Method Not Allowed"}, 189 | {406, 0, "Not Acceptable"}, 190 | {407, 0, "Proxy Authentication Required"}, 191 | {408, 0, "Request Timeout"}, 192 | {410, 0, "Gone"}, 193 | {411, 0, "Length Required"}, 194 | {412, 0, "Precondition Failed"}, 195 | {413, 0, "Request Entity Too Large"}, 196 | {414, 0, "Request-URI Too Long"}, 197 | {415, 0, "Unsupported Media Type"}, 198 | {451, 0, "Invalid parameter"}, 199 | {452, 0, "Illegal Conference Identifier"}, 200 | {453, 0, "Not Enough Bandwidth"}, 201 | {454, 0, "Session Not Found"}, 202 | {455, 0, "Method Not Valid In This State"}, 203 | {456, 0, "Header Field Not Valid"}, 204 | {457, 0, "Invalid Range"}, 205 | {458, 0, "Parameter Is Read-Only"}, 206 | {459, 0, "Aggregate Operation Not Allowed"}, 207 | {460, 0, "Only Aggregate Operation Allowed"}, 208 | {461, 0, "Unsupported Transport"}, 209 | {462, 0, "Destination Unreachable"}, 210 | {500, 0, "Internal Server Error"}, 211 | {501, 0, "Not Implemented"}, 212 | {502, 0, "Bad Gateway"}, 213 | {503, 0, "Service Unavailable"}, 214 | {504, 0, "Gateway Timeout"}, 215 | {505, 0, "RTSP Version Not Supported"}, 216 | {551, 0, "Option not support"}, 217 | }; 218 | 219 | static const rtsp_msg_int2str_tbl_s rtsp_msg_transport_type_tbl[] = { 220 | {RTSP_MSG_TRANSPORT_TYPE_RTP_AVP_TCP, 11, "RTP/AVP/TCP"}, 221 | {RTSP_MSG_TRANSPORT_TYPE_RTP_AVP, 7, "RTP/AVP"}, 222 | {RTSP_MSG_TRANSPORT_TYPE_BUTT, 0, ""}, 223 | }; 224 | 225 | static const rtsp_msg_int2str_tbl_s rtsp_msg_content_type_tbl[] = { 226 | {RTSP_MSG_CONTENT_TYPE_SDP, 15, "application/sdp"}, 227 | {RTSP_MSG_CONTENT_TYPE_RTSL, 16, "application/rtsl"}, 228 | {RTSP_MSG_CONTENT_TYPE_MHEG, 16, "application/mheg"}, 229 | {RTSP_MSG_CONTENT_TYPE_BUTT, 0, ""}, 230 | }; 231 | 232 | static int rtsp_msg_parse_uri(const char *line, rtsp_msg_uri_s *uri) 233 | { 234 | const char *p = line, *q; 235 | unsigned int tmp; 236 | 237 | uri->scheme = (rtsp_msg_uri_scheme_e)rtsp_msg_str2int(rtsp_msg_uri_scheme_tbl, 238 | ARRAY_SIZE(rtsp_msg_uri_scheme_tbl), line); 239 | if (uri->scheme == RTSP_MSG_URI_SCHEME_BUTT) 240 | { 241 | err("parse scheme failed. line: %s\n", line); 242 | return -1; 243 | } 244 | uri->port = 0; //default 245 | uri->ipaddr[0] = 0; 246 | uri->abspath[0] = 0; 247 | 248 | while (islower(*p) || *p == ':') 249 | p++; 250 | if (*p != '/' || *(p + 1) != '/') 251 | { 252 | err("parse ip failed. line: %s\n", line); 253 | return -1; 254 | } 255 | p += 2; 256 | 257 | q = p; 258 | while (isgraph(*q) && *q != ':' && *q != '/') 259 | q++; 260 | if (*q == ':') 261 | { 262 | if (sscanf(q + 1, "%u", &tmp) != 1) 263 | { 264 | err("parse uri port failed. line: %s\n", line); 265 | return -1; 266 | } 267 | uri->port = tmp; 268 | } 269 | tmp = q - p; 270 | if (tmp > sizeof(uri->ipaddr) - 1) 271 | tmp = sizeof(uri->ipaddr) - 1; 272 | memcpy(uri->ipaddr, p, tmp); 273 | uri->ipaddr[tmp] = 0; 274 | 275 | while (isgraph(*q) && *q != '/') 276 | q++; 277 | if (*q != '/') 278 | return (q - line); 279 | 280 | p = q; 281 | while (isgraph(*q)) 282 | q++; 283 | tmp = q - p; 284 | if (tmp > sizeof(uri->abspath) - 1) 285 | tmp = sizeof(uri->abspath) - 1; 286 | memcpy(uri->abspath, p, tmp); 287 | uri->abspath[tmp] = 0; 288 | 289 | return (q - line); 290 | } 291 | 292 | static int rtsp_msg_build_uri(const rtsp_msg_uri_s *uri, char *line, int size) 293 | { 294 | if (uri->port) 295 | snprintf(line, size, "%s//%s:%u%s", 296 | rtsp_msg_int2str(rtsp_msg_uri_scheme_tbl, 297 | ARRAY_SIZE(rtsp_msg_uri_scheme_tbl), uri->scheme), 298 | uri->ipaddr, uri->port, uri->abspath); 299 | else 300 | snprintf(line, size, "%s//%s%s", 301 | rtsp_msg_int2str(rtsp_msg_uri_scheme_tbl, 302 | ARRAY_SIZE(rtsp_msg_uri_scheme_tbl), uri->scheme), 303 | uri->ipaddr, uri->abspath); 304 | return strlen(line); 305 | } 306 | 307 | //return 0. if success 308 | static int rtsp_msg_parse_startline(rtsp_msg_s *msg, const char *line) 309 | { 310 | const char *p = line; 311 | int ret; 312 | ret = rtsp_msg_str2int(rtsp_msg_method_tbl, 313 | ARRAY_SIZE(rtsp_msg_method_tbl), p); 314 | if (ret != RTSP_MSG_METHOD_BUTT) 315 | { 316 | msg->type = RTSP_MSG_TYPE_REQUEST; 317 | msg->hdrs.startline.reqline.method = (rtsp_msg_method_e)ret; 318 | while (isgraph(*p)) 319 | p++; 320 | p++; //next field 321 | ret = rtsp_msg_parse_uri(p, &msg->hdrs.startline.reqline.uri); 322 | if (ret <= 0) 323 | return -1; 324 | while (isgraph(*p)) 325 | p++; 326 | p++; //next field 327 | ret = rtsp_msg_str2int(rtsp_msg_version_tbl, 328 | ARRAY_SIZE(rtsp_msg_version_tbl), p); 329 | if (ret == RTSP_MSG_VERSION_BUTT) 330 | { 331 | err("parse version failed. line: %s\n", line); 332 | return -1; 333 | } 334 | return 0; 335 | } 336 | 337 | ret = rtsp_msg_str2int(rtsp_msg_version_tbl, 338 | ARRAY_SIZE(rtsp_msg_version_tbl), p); 339 | if (ret != RTSP_MSG_VERSION_BUTT) 340 | { 341 | msg->type = RTSP_MSG_TYPE_RESPONSE; 342 | msg->hdrs.startline.resline.version = (rtsp_msg_version_e)ret; 343 | while (isgraph(*p)) 344 | p++; 345 | p++; //next field 346 | if (sscanf(p, "%d", &ret) != 1) 347 | { 348 | err("parse status-code failed. line: %s\n", line); 349 | return -1; 350 | } 351 | msg->hdrs.startline.resline.status_code = ret; 352 | return 0; 353 | } 354 | 355 | if (*p != '$') 356 | { 357 | err("parse startline failed: %s\n", line); 358 | return -1; 359 | } 360 | 361 | msg->type = RTSP_MSG_TYPE_INTERLEAVED; 362 | msg->hdrs.startline.interline.channel = *((uint8_t *)(p + 1)); 363 | msg->hdrs.startline.interline.length = *((uint16_t *)(p + 2)); //XXX 364 | msg->hdrs.startline.interline.reserved = 0; 365 | return 0; 366 | } 367 | 368 | static int rtsp_msg_build_startline(const rtsp_msg_s *msg, char *line, int size) 369 | { 370 | char *p = line; 371 | int ret; 372 | 373 | if (msg->type == RTSP_MSG_TYPE_REQUEST) 374 | { 375 | snprintf(line, size, "%s ", 376 | rtsp_msg_int2str(rtsp_msg_method_tbl, 377 | ARRAY_SIZE(rtsp_msg_method_tbl), 378 | msg->hdrs.startline.reqline.method)); 379 | ret = strlen(p); 380 | p += ret; 381 | size -= ret; 382 | if (size <= 1) 383 | return (p - line); 384 | 385 | ret = rtsp_msg_build_uri(&msg->hdrs.startline.reqline.uri, 386 | p, size); 387 | p += ret; 388 | size -= ret; 389 | if (size <= 1) 390 | return (p - line); 391 | 392 | snprintf(p, size, " %s\r\n", 393 | rtsp_msg_int2str(rtsp_msg_version_tbl, 394 | ARRAY_SIZE(rtsp_msg_version_tbl), 395 | msg->hdrs.startline.reqline.version)); 396 | p += strlen(p); 397 | return (p - line); 398 | } 399 | 400 | if (msg->type == RTSP_MSG_TYPE_RESPONSE) 401 | { 402 | snprintf(p, size, "%s %u %s\r\n", 403 | rtsp_msg_int2str(rtsp_msg_version_tbl, 404 | ARRAY_SIZE(rtsp_msg_version_tbl), 405 | msg->hdrs.startline.resline.version), 406 | msg->hdrs.startline.resline.status_code, 407 | rtsp_msg_int2str(rtsp_msg_status_code_tbl, 408 | ARRAY_SIZE(rtsp_msg_status_code_tbl), 409 | msg->hdrs.startline.resline.status_code)); 410 | return strlen(p); 411 | } 412 | 413 | return 0; 414 | } 415 | 416 | //Transport 417 | static int rtsp_msg_parse_transport(rtsp_msg_s *msg, const char *line) 418 | { 419 | rtsp_msg_hdr_s *hdrs = &msg->hdrs; 420 | const char *p; 421 | unsigned int tmp; 422 | 423 | if (hdrs->transport) 424 | { 425 | rtsp_mem_free(hdrs->transport); 426 | hdrs->transport = NULL; 427 | } 428 | 429 | hdrs->transport = (rtsp_msg_transport_s *)rtsp_mem_alloc(sizeof(rtsp_msg_transport_s)); 430 | if (!hdrs->transport) 431 | { 432 | err("rtsp_mem_alloc for %s failed\n", "rtsp_msg_transport_s"); 433 | return -1; 434 | } 435 | 436 | p = strstr(line, "RTP/AVP"); 437 | if (!p) 438 | { 439 | err("parse transport failed. line: %s\n", line); 440 | rtsp_mem_free(hdrs->transport); 441 | hdrs->transport = NULL; 442 | return -1; 443 | } 444 | hdrs->transport->type = (rtsp_msg_transport_type_e)rtsp_msg_str2int( 445 | rtsp_msg_transport_type_tbl, 446 | ARRAY_SIZE(rtsp_msg_transport_type_tbl), p); 447 | 448 | if ((p = strstr(line, "ssrc="))) 449 | { 450 | if (sscanf(p, "ssrc=%X", &tmp) == 1) 451 | { 452 | hdrs->transport->flags |= RTSP_MSG_TRANSPORT_FLAG_SSRC; 453 | hdrs->transport->ssrc = tmp; 454 | } 455 | } 456 | 457 | if ((p = strstr(line, "unicast"))) 458 | { 459 | hdrs->transport->flags |= RTSP_MSG_TRANSPORT_FLAG_UNICAST; 460 | } 461 | if ((p = strstr(line, "multicast"))) 462 | { 463 | hdrs->transport->flags |= RTSP_MSG_TRANSPORT_FLAG_MULTICAST; 464 | } 465 | 466 | if ((p = strstr(line, "client_port="))) 467 | { 468 | if (sscanf(p, "client_port=%u-%*u", &tmp) == 1) 469 | { 470 | hdrs->transport->flags |= RTSP_MSG_TRANSPORT_FLAG_CLIENT_PORT; 471 | hdrs->transport->client_port = tmp; 472 | } 473 | } 474 | 475 | if ((p = strstr(line, "server_port="))) 476 | { 477 | if (sscanf(p, "server_port=%u-%*u", &tmp) == 1) 478 | { 479 | hdrs->transport->flags |= RTSP_MSG_TRANSPORT_FLAG_SERVER_PORT; 480 | hdrs->transport->server_port = tmp; 481 | } 482 | } 483 | 484 | if ((p = strstr(line, "interleaved="))) 485 | { 486 | if (sscanf(p, "interleaved=%u-%*u", &tmp) == 1) 487 | { 488 | hdrs->transport->flags |= RTSP_MSG_TRANSPORT_FLAG_INTERLEAVED; 489 | hdrs->transport->interleaved = tmp; 490 | } 491 | } 492 | return 0; 493 | } 494 | 495 | static int rtsp_msg_build_transport(const rtsp_msg_s *msg, char *line, int size) 496 | { 497 | const rtsp_msg_hdr_s *hdrs = &msg->hdrs; 498 | if (hdrs->transport) 499 | { 500 | char *p = line; 501 | int len; 502 | snprintf(p, size, "Transport: %s", rtsp_msg_int2str(rtsp_msg_transport_type_tbl, ARRAY_SIZE(rtsp_msg_transport_type_tbl), hdrs->transport->type)); 503 | #define TRANSPORT_BUILD_STEP() \ 504 | len = strlen(p); \ 505 | p += len; \ 506 | size -= len; \ 507 | if (size <= 1) \ 508 | { \ 509 | return (p - line); \ 510 | } 511 | TRANSPORT_BUILD_STEP(); 512 | 513 | if (hdrs->transport->flags & RTSP_MSG_TRANSPORT_FLAG_SSRC) 514 | { 515 | snprintf(p, size, ";ssrc=%08X", hdrs->transport->ssrc); 516 | TRANSPORT_BUILD_STEP(); 517 | } 518 | 519 | if (hdrs->transport->flags & RTSP_MSG_TRANSPORT_FLAG_MULTICAST) 520 | { 521 | snprintf(p, size, ";multicast"); 522 | TRANSPORT_BUILD_STEP(); 523 | } 524 | else if (hdrs->transport->flags & RTSP_MSG_TRANSPORT_FLAG_UNICAST) 525 | { 526 | snprintf(p, size, ";unicast"); 527 | TRANSPORT_BUILD_STEP(); 528 | } 529 | 530 | if (hdrs->transport->flags & RTSP_MSG_TRANSPORT_FLAG_CLIENT_PORT) 531 | { 532 | snprintf(p, size, ";client_port=%u-%u", 533 | hdrs->transport->client_port, 534 | hdrs->transport->client_port + 1); 535 | TRANSPORT_BUILD_STEP(); 536 | } 537 | 538 | if (hdrs->transport->flags & RTSP_MSG_TRANSPORT_FLAG_SERVER_PORT) 539 | { 540 | snprintf(p, size, ";server_port=%u-%u", 541 | hdrs->transport->server_port, 542 | hdrs->transport->server_port + 1); 543 | TRANSPORT_BUILD_STEP(); 544 | } 545 | 546 | if (hdrs->transport->flags & RTSP_MSG_TRANSPORT_FLAG_INTERLEAVED) 547 | { 548 | snprintf(p, size, ";interleaved=%u-%u", 549 | hdrs->transport->interleaved, 550 | hdrs->transport->interleaved + 1); 551 | TRANSPORT_BUILD_STEP(); 552 | } 553 | 554 | snprintf(p, size, "\r\n"); 555 | TRANSPORT_BUILD_STEP(); 556 | return (p - line); 557 | } 558 | return 0; 559 | } 560 | 561 | //Range 562 | static int rtsp_msg_parse_range(rtsp_msg_s *msg, const char *line) 563 | { 564 | return 0; //TODO 565 | } 566 | 567 | static int rtsp_msg_build_range(const rtsp_msg_s *msg, char *line, int size) 568 | { 569 | return 0; //TODO 570 | } 571 | 572 | //Authorization 573 | static int rtsp_msg_parse_authorization(rtsp_msg_s *msg, const char *line) 574 | { 575 | rtsp_msg_hdr_s *hdrs = &msg->hdrs; 576 | const char *p; 577 | 578 | if (hdrs->authorization) 579 | { 580 | rtsp_mem_free(hdrs->authorization); 581 | hdrs->authorization = NULL; 582 | } 583 | hdrs->authorization = (rtsp_msg_authorization_s *)rtsp_mem_alloc(sizeof(rtsp_msg_authorization_s)); 584 | if (!hdrs->authorization) 585 | { 586 | err("rtsp_mem_alloc for authorization failed\n"); 587 | return -1; 588 | } 589 | 590 | if ((p = strstr(line, "username="))) 591 | { 592 | sscanf(p, "username=\"%[^\"]\"", hdrs->authorization->username); 593 | } 594 | 595 | if ((p = strstr(line, "uri="))) 596 | { 597 | sscanf(p, "uri=\"%[^\"]\"", hdrs->authorization->uri); 598 | } 599 | 600 | if ((p = strstr(line, "response="))) 601 | { 602 | sscanf(p, "response=\"%[^\"]\"", hdrs->authorization->response); 603 | } 604 | return 0; 605 | } 606 | 607 | static int rtsp_msg_build_www_authenticate(const rtsp_msg_s *msg, char *line, int size) 608 | { 609 | const rtsp_msg_hdr_s *hdrs = &msg->hdrs; 610 | if (hdrs->www_authenticate) 611 | { 612 | char *p = line; 613 | sprintf(p, "WWW-Authenticate: Digest realm=\"%s\", nonce=\"%s\", algorithm=\"MD5\", stale=\"FALSE\"\r\n", hdrs->www_authenticate->realm, hdrs->www_authenticate->nonce); 614 | return strlen(p); 615 | } 616 | 617 | return 0; 618 | } 619 | 620 | //RTP-Info 621 | static int rtsp_msg_parse_rtp_info(rtsp_msg_s *msg, const char *line) 622 | { 623 | return 0; //TODO 624 | } 625 | 626 | static int rtsp_msg_build_rtp_info(const rtsp_msg_s *msg, char *line, int size) 627 | { 628 | return 0; //TODO 629 | } 630 | 631 | //link CSeq/Session int 632 | #define DEFINE_PARSE_BUILD_LINK_CSEQ(_name, _type, _param, _fmt) \ 633 | static int rtsp_msg_parse_##_name(rtsp_msg_s *msg, const char *line) \ 634 | { \ 635 | rtsp_msg_hdr_s *hdrs = &msg->hdrs; \ 636 | if (hdrs->_name) \ 637 | { \ 638 | rtsp_mem_free(hdrs->_name); \ 639 | hdrs->_name = NULL; \ 640 | } \ 641 | hdrs->_name = (_type *)rtsp_mem_alloc(sizeof(_type)); \ 642 | if (!hdrs->_name) \ 643 | { \ 644 | err("rtsp_mem_alloc for %s failed\n", #_type); \ 645 | return -1; \ 646 | } \ 647 | if (sscanf(line, _fmt, &hdrs->_name->_param) != 1) \ 648 | { \ 649 | rtsp_mem_free(hdrs->_name); \ 650 | hdrs->_name = NULL; \ 651 | err("parse %s failed. line: %s\n", #_name, line); \ 652 | return -1; \ 653 | } \ 654 | return 0; \ 655 | } \ 656 | static int rtsp_msg_build_##_name(const rtsp_msg_s *msg, char *line, int size) \ 657 | { \ 658 | if (msg->hdrs._name) \ 659 | { \ 660 | snprintf(line, size, _fmt "\r\n", msg->hdrs._name->_param); \ 661 | return strlen(line); \ 662 | } \ 663 | return 0; \ 664 | } 665 | 666 | DEFINE_PARSE_BUILD_LINK_CSEQ(cseq, rtsp_msg_cseq_s, cseq, "CSeq: %u") 667 | DEFINE_PARSE_BUILD_LINK_CSEQ(session, rtsp_msg_session_s, session, "Session: %08X") 668 | DEFINE_PARSE_BUILD_LINK_CSEQ(content_length, rtsp_msg_content_length_s, length, "Content-Length: %u") 669 | 670 | //link Server/User-Agent char[] 671 | #define DEFINE_PARSE_BUILD_LINK_SERVER(_name, _type, _param, _fmt) \ 672 | static int rtsp_msg_parse_##_name(rtsp_msg_s *msg, const char *line) \ 673 | { \ 674 | rtsp_msg_hdr_s *hdrs = &msg->hdrs; \ 675 | const char *p = line; \ 676 | unsigned int tmp = 0; \ 677 | if (hdrs->_name) \ 678 | { \ 679 | rtsp_mem_free(hdrs->_name); \ 680 | hdrs->_name = NULL; \ 681 | } \ 682 | hdrs->_name = (_type *)rtsp_mem_alloc(sizeof(_type)); \ 683 | if (!hdrs->_name) \ 684 | { \ 685 | err("rtsp_mem_alloc for %s failed\n", #_type); \ 686 | return -1; \ 687 | } \ 688 | while (isgraph(*p) && *p != ':') \ 689 | p++; \ 690 | if (*p != ':') \ 691 | { \ 692 | rtsp_mem_free(hdrs->_name); \ 693 | hdrs->_name = NULL; \ 694 | err("parse %s failed. line: %s\n", #_name, line); \ 695 | return -1; \ 696 | } \ 697 | p++; \ 698 | while (*p == ' ') \ 699 | p++; \ 700 | while (isprint(*p) && tmp < sizeof(hdrs->_name->_param) - 1) \ 701 | { \ 702 | hdrs->_name->_param[tmp++] = *p++; \ 703 | } \ 704 | hdrs->_name->_param[tmp] = 0; \ 705 | return 0; \ 706 | } \ 707 | static int rtsp_msg_build_##_name(const rtsp_msg_s *msg, char *line, int size) \ 708 | { \ 709 | if (msg->hdrs._name) \ 710 | { \ 711 | snprintf(line, size, _fmt "\r\n", msg->hdrs._name->_param); \ 712 | return strlen(line); \ 713 | } \ 714 | return 0; \ 715 | } 716 | 717 | DEFINE_PARSE_BUILD_LINK_SERVER(server, rtsp_msg_server_s, server, "Server: %s") 718 | DEFINE_PARSE_BUILD_LINK_SERVER(user_agent, rtsp_msg_user_agent_s, user_agent, "User-Agent: %s") 719 | DEFINE_PARSE_BUILD_LINK_SERVER(date, rtsp_msg_date_s, http_date, "Date: %s") 720 | 721 | //link Content-Type 722 | #define DEFINE_PARSE_BUILD_LINK_CONTENT_TYPE(_name, _type, _param, _fmt, _tbl) \ 723 | static int rtsp_msg_parse_##_name(rtsp_msg_s *msg, const char *line) \ 724 | { \ 725 | rtsp_msg_hdr_s *hdrs = &msg->hdrs; \ 726 | const char *p = line; \ 727 | int num = ARRAY_SIZE(_tbl); \ 728 | int i = 0; \ 729 | if (hdrs->_name) \ 730 | { \ 731 | rtsp_mem_free(hdrs->_name); \ 732 | hdrs->_name = NULL; \ 733 | } \ 734 | hdrs->_name = (_type *)rtsp_mem_alloc(sizeof(_type)); \ 735 | if (!hdrs->_name) \ 736 | { \ 737 | err("rtsp_mem_alloc for %s failed\n", #_type); \ 738 | return -1; \ 739 | } \ 740 | while (isgraph(*p) && *p != ':') \ 741 | p++; \ 742 | if (*p != ':') \ 743 | { \ 744 | rtsp_mem_free(hdrs->_name); \ 745 | hdrs->_name = NULL; \ 746 | err("parse %s failed. line: %s\n", #_name, line); \ 747 | return -1; \ 748 | } \ 749 | p++; \ 750 | while (*p == ' ') \ 751 | p++; \ 752 | for (i = 0; i < num; i++) \ 753 | { \ 754 | if (_tbl[i].strsiz && strstr(p, _tbl[i].strval)) \ 755 | { \ 756 | *((int *)&hdrs->_name->_param) = _tbl[i].intval; \ 757 | return 0; \ 758 | } \ 759 | } \ 760 | rtsp_mem_free(hdrs->_name); \ 761 | hdrs->_name = NULL; \ 762 | return -1; \ 763 | } \ 764 | static int rtsp_msg_build_##_name(const rtsp_msg_s *msg, char *line, int size) \ 765 | { \ 766 | if (msg->hdrs._name) \ 767 | { \ 768 | int i, num = ARRAY_SIZE(_tbl); \ 769 | for (i = 0; i < num; i++) \ 770 | { \ 771 | if ((int)msg->hdrs._name->_param == _tbl[i].intval) \ 772 | { \ 773 | snprintf(line, size, _fmt "\r\n", _tbl[i].strval); \ 774 | return strlen(line); \ 775 | } \ 776 | } \ 777 | return 0; \ 778 | } \ 779 | return 0; \ 780 | } 781 | 782 | DEFINE_PARSE_BUILD_LINK_CONTENT_TYPE(content_type, rtsp_msg_content_type_s, type, "Content-Type: %s", rtsp_msg_content_type_tbl) 783 | 784 | //link Public/Accept 785 | #define DEFINE_PARSE_BUILD_LINK_PUBLIC(_name, _type, _param, _fmt, _tbl) \ 786 | static int rtsp_msg_parse_##_name(rtsp_msg_s *msg, const char *line) \ 787 | { \ 788 | rtsp_msg_hdr_s *hdrs = &msg->hdrs; \ 789 | const char *p = line; \ 790 | int num = ARRAY_SIZE(_tbl); \ 791 | int i = 0; \ 792 | if (hdrs->_name) \ 793 | { \ 794 | rtsp_mem_free(hdrs->_name); \ 795 | hdrs->_name = NULL; \ 796 | } \ 797 | hdrs->_name = (_type *)rtsp_mem_alloc(sizeof(_type)); \ 798 | if (!hdrs->_name) \ 799 | { \ 800 | err("rtsp_mem_alloc for %s failed\n", #_type); \ 801 | return -1; \ 802 | } \ 803 | while (isgraph(*p) && *p != ':') \ 804 | p++; \ 805 | if (*p != ':') \ 806 | { \ 807 | rtsp_mem_free(hdrs->_name); \ 808 | hdrs->_name = NULL; \ 809 | err("parse %s failed. line: %s\n", #_name, line); \ 810 | return -1; \ 811 | } \ 812 | p++; \ 813 | while (*p == ' ') \ 814 | p++; \ 815 | for (i = 0; i < num; i++) \ 816 | { \ 817 | if (_tbl[i].strsiz && strstr(p, _tbl[i].strval)) \ 818 | hdrs->_name->_param |= 1 << _tbl[i].intval; \ 819 | } \ 820 | return 0; \ 821 | } \ 822 | static int rtsp_msg_build_##_name(const rtsp_msg_s *msg, char *line, int size) \ 823 | { \ 824 | if (msg->hdrs._name) \ 825 | { \ 826 | char *p = line; \ 827 | int len, i, flag = 0; \ 828 | int num = ARRAY_SIZE(_tbl); \ 829 | snprintf(p, size, _fmt, ""); \ 830 | len = strlen(p); \ 831 | p += len; \ 832 | size -= len; \ 833 | if (size <= 1) \ 834 | { \ 835 | return (p - line); \ 836 | } \ 837 | for (i = 0; i < num; i++) \ 838 | { \ 839 | if (msg->hdrs._name->_param & (1 << _tbl[i].intval)) \ 840 | { \ 841 | if (flag) \ 842 | { \ 843 | snprintf(p, size, ", %s", _tbl[i].strval); \ 844 | } \ 845 | else \ 846 | { \ 847 | snprintf(p, size, "%s", _tbl[i].strval); \ 848 | flag = 1; \ 849 | } \ 850 | len = strlen(p); \ 851 | p += len; \ 852 | size -= len; \ 853 | if (size <= 1) \ 854 | { \ 855 | return (p - line); \ 856 | } \ 857 | } \ 858 | } \ 859 | snprintf(p, size, "\r\n"); \ 860 | len = strlen(p); \ 861 | p += len; \ 862 | return (p - line); \ 863 | } \ 864 | return 0; \ 865 | } 866 | 867 | DEFINE_PARSE_BUILD_LINK_PUBLIC(public_, rtsp_msg_public_s, public_, "Public: %s", rtsp_msg_method_tbl) 868 | DEFINE_PARSE_BUILD_LINK_PUBLIC(accept, rtsp_msg_accept_s, accept, "Accept: %s", rtsp_msg_content_type_tbl) 869 | 870 | typedef int (*rtsp_msg_line_parser)(rtsp_msg_s *msg, const char *line); 871 | typedef struct __rtsp_msg_str2parser_tbl_s 872 | { 873 | int strsiz; 874 | const char *strval; 875 | rtsp_msg_line_parser parser; 876 | } rtsp_msg_str2parser_tbl_s; 877 | 878 | static const rtsp_msg_str2parser_tbl_s rtsp_msg_hdr_line_parse_tbl[] = { 879 | {6, "CSeq: ", rtsp_msg_parse_cseq}, 880 | {6, "Date: ", rtsp_msg_parse_date}, 881 | {9, "Session: ", rtsp_msg_parse_session}, 882 | {11, "Transport: ", rtsp_msg_parse_transport}, 883 | {7, "Range: ", rtsp_msg_parse_range}, 884 | {8, "Accept: ", rtsp_msg_parse_accept}, 885 | {15, "Authorization: ", rtsp_msg_parse_authorization}, 886 | {12, "User-Agent: ", rtsp_msg_parse_user_agent}, 887 | {8, "Public: ", rtsp_msg_parse_public_}, 888 | {10, "RTP-Info: ", rtsp_msg_parse_rtp_info}, 889 | {8, "Server: ", rtsp_msg_parse_server}, 890 | {14, "Content-Type: ", rtsp_msg_parse_content_type}, 891 | {16, "Content-Length: ", rtsp_msg_parse_content_length}, 892 | }; 893 | 894 | static rtsp_msg_line_parser rtsp_msg_str2parser(const char *line) 895 | { 896 | const rtsp_msg_str2parser_tbl_s *tbl = rtsp_msg_hdr_line_parse_tbl; 897 | int num = ARRAY_SIZE(rtsp_msg_hdr_line_parse_tbl); 898 | int i; 899 | 900 | for (i = 0; i < num; i++) 901 | { 902 | if (strncmp(tbl[i].strval, line, tbl[i].strsiz) == 0) 903 | return tbl[i].parser; 904 | } 905 | return NULL; 906 | } 907 | 908 | //@start: data 909 | //@line: store current line (has \0. but no \r\n) 910 | //@maxlen: line max size 911 | //@return: non-NULL is next line pointer. NULL has no next line 912 | static const char *rtsp_msg_hdr_next_line(const char *start, char *line, int maxlen) 913 | { 914 | const char *p = start; 915 | 916 | while (*p && *p != '\r' && *p != '\n') 917 | p++; 918 | if (*p != '\r' || *(p + 1) != '\n') 919 | return NULL; 920 | 921 | if (line && maxlen > 0) 922 | { 923 | maxlen--; 924 | if (maxlen > p - start) 925 | maxlen = p - start; 926 | memcpy(line, start, maxlen); 927 | line[maxlen] = '\0'; 928 | } 929 | 930 | return (p + 2); 931 | } 932 | 933 | int rtsp_msg_init(rtsp_msg_s *msg) 934 | { 935 | if (msg) 936 | memset(msg, 0, sizeof(rtsp_msg_s)); 937 | return 0; 938 | } 939 | 940 | //free all msg elements. not free msg 941 | void rtsp_msg_free(rtsp_msg_s *msg) 942 | { 943 | if (msg->hdrs.cseq) 944 | rtsp_mem_free(msg->hdrs.cseq); 945 | if (msg->hdrs.date) 946 | rtsp_mem_free(msg->hdrs.date); 947 | if (msg->hdrs.session) 948 | rtsp_mem_free(msg->hdrs.session); 949 | if (msg->hdrs.transport) 950 | rtsp_mem_free(msg->hdrs.transport); 951 | if (msg->hdrs.range) 952 | rtsp_mem_free(msg->hdrs.range); 953 | 954 | if (msg->hdrs.accept) 955 | rtsp_mem_free(msg->hdrs.accept); 956 | if (msg->hdrs.www_authenticate) 957 | rtsp_mem_free(msg->hdrs.www_authenticate); 958 | if (msg->hdrs.user_agent) 959 | rtsp_mem_free(msg->hdrs.user_agent); 960 | 961 | if (msg->hdrs.public_) 962 | rtsp_mem_free(msg->hdrs.public_); 963 | //TODO free rtp-info 964 | if (msg->hdrs.server) 965 | rtsp_mem_free(msg->hdrs.server); 966 | 967 | if (msg->hdrs.authorization) 968 | rtsp_mem_free(msg->hdrs.authorization); 969 | if (msg->hdrs.content_type) 970 | rtsp_mem_free(msg->hdrs.content_type); 971 | if (msg->hdrs.content_length) 972 | rtsp_mem_free(msg->hdrs.content_length); 973 | 974 | if (msg->body.body) 975 | rtsp_mem_free(msg->body.body); 976 | 977 | memset(msg, 0, sizeof(rtsp_msg_s)); 978 | } 979 | 980 | uint32_t rtsp_msg_gen_session_id(void) 981 | { 982 | static uint32_t session_id = 0x12345678; 983 | return session_id++; //FIXME 984 | } 985 | 986 | //return frame real size. when frame is completed 987 | //return 0. when frame size is not enough 988 | //return -1. when frame is invalid 989 | int rtsp_msg_frame_size(const void *data, int size) 990 | { 991 | const char *frame = (const char *)data; 992 | const char *p; 993 | int hdrlen = 0, content_len = 0; 994 | 995 | //check first 996 | p = strstr(frame, "\r\n"); 997 | if (!p || size < p - frame + 2) 998 | { 999 | if (size > 256) 1000 | return -1; //first line is too large 1001 | return 0; 1002 | } 1003 | 1004 | //check headers 1005 | p = strstr(frame, "\r\n\r\n"); 1006 | if (!p || size < p - frame + 4) 1007 | { 1008 | if (size > 1024) 1009 | return -1; //headers is too large 1010 | return 0; 1011 | } 1012 | hdrlen = p - frame + 4; 1013 | 1014 | //get content-length 1015 | p = frame; 1016 | while ((p = rtsp_msg_hdr_next_line(p, NULL, 0))) 1017 | { 1018 | if (strncmp(p, "\r\n", 2) == 0) 1019 | break; //header end 1020 | if (strncmp(p, "Content-Length", 14) == 0) 1021 | { 1022 | if (sscanf(p, "Content-Length: %d", &content_len) != 1) 1023 | { 1024 | err("parse Content-Length failed. line: %s", p); 1025 | return -1; 1026 | } 1027 | } 1028 | } 1029 | 1030 | if (size < hdrlen + content_len) 1031 | return 0; 1032 | return (hdrlen + content_len); 1033 | } 1034 | 1035 | //return data's bytes which is parsed. when success 1036 | //return 0. when data is not enough 1037 | //return -1. when data is invalid 1038 | int rtsp_msg_parse_from_array(rtsp_msg_s *msg, const void *data, int size) 1039 | { 1040 | const char *frame = (const char *)data; 1041 | const char *p = frame; 1042 | char line[256]; 1043 | int ret; 1044 | 1045 | memset(msg, 0, sizeof(rtsp_msg_s)); 1046 | 1047 | //interleaved frame 1048 | if (frame[0] == '$') 1049 | { 1050 | uint16_t interlen = *((uint16_t *)(p + 2)); 1051 | interlen = ntohs(interlen); 1052 | if (size < interlen + 4) 1053 | return 0; 1054 | msg->type = RTSP_MSG_TYPE_INTERLEAVED; 1055 | msg->hdrs.startline.interline.channel = *((uint8_t *)(p + 1)); 1056 | msg->hdrs.startline.interline.length = interlen; 1057 | msg->body.body = rtsp_mem_dup((const char *)data + 4, interlen); 1058 | return (interlen + 4); 1059 | } 1060 | 1061 | dbg("\n%s", frame); 1062 | 1063 | ret = rtsp_msg_frame_size(data, size); 1064 | if (ret <= 0) 1065 | return ret; 1066 | size = ret; 1067 | 1068 | p = rtsp_msg_hdr_next_line(p, line, sizeof(line)); 1069 | if (!p) 1070 | { 1071 | return -1; 1072 | } 1073 | 1074 | ret = rtsp_msg_parse_startline(msg, line); 1075 | if (ret < 0) 1076 | return -1; 1077 | 1078 | while ((p = rtsp_msg_hdr_next_line(p, line, sizeof(line)))) 1079 | { 1080 | rtsp_msg_line_parser parser; 1081 | 1082 | if (strlen(line) == 0) 1083 | break; 1084 | parser = rtsp_msg_str2parser(line); 1085 | if (!parser) 1086 | { 1087 | warn("unknown line: %s\n", line); 1088 | continue; 1089 | } 1090 | 1091 | ret = (*parser)(msg, line); 1092 | if (ret < 0) 1093 | { 1094 | err("parse failed. line: %s\n", line); 1095 | break; 1096 | } 1097 | } 1098 | if (!p || strlen(line)) 1099 | { 1100 | //dbg("p = %p len = %lu\n", p, strlen(line)); 1101 | rtsp_msg_free(msg); 1102 | return -1; 1103 | } 1104 | 1105 | if (msg->hdrs.content_length) 1106 | { 1107 | msg->body.body = rtsp_mem_dup(p, msg->hdrs.content_length->length); 1108 | if (!msg->body.body) 1109 | { 1110 | err("set body failed\n"); 1111 | rtsp_msg_free(msg); 1112 | return -1; 1113 | } 1114 | } 1115 | 1116 | //debug 1117 | ret = p - frame; 1118 | if (msg->hdrs.content_length) 1119 | ret += msg->hdrs.content_length->length; 1120 | if (ret != size) 1121 | { 1122 | warn("frame size is %d. but real used %d\n", size, ret); 1123 | } 1124 | 1125 | return size; 1126 | } 1127 | 1128 | //return data's bytes which is used. when success 1129 | //return -1. when failed 1130 | int rtsp_msg_build_to_array(const rtsp_msg_s *msg, void *data, int size) 1131 | { 1132 | char *frame = (char *)data; 1133 | char *p = frame; 1134 | int len; 1135 | 1136 | //interleaved frame 1137 | if (msg->type == RTSP_MSG_TYPE_INTERLEAVED) 1138 | { 1139 | uint8_t hdr[4]; 1140 | uint16_t interlen = msg->hdrs.startline.interline.length; 1141 | hdr[0] = '$'; 1142 | hdr[1] = msg->hdrs.startline.interline.channel; 1143 | *((uint16_t *)(&hdr[2])) = htons(interlen); 1144 | if (size > 4 + interlen) 1145 | size = interlen + 4; 1146 | memcpy(data, hdr, 4); 1147 | if (msg->body.body) 1148 | memcpy((char *)data + 4, msg->body.body, size - 4); 1149 | return size; 1150 | } 1151 | 1152 | #define MSG_BUILD_STEP() \ 1153 | do \ 1154 | { \ 1155 | if (len < 0) \ 1156 | return -1; \ 1157 | p += len; \ 1158 | size -= len; \ 1159 | if (size <= 1) \ 1160 | return (p - frame); \ 1161 | } while (0) 1162 | 1163 | len = rtsp_msg_build_startline(msg, p, size); 1164 | ; 1165 | MSG_BUILD_STEP(); 1166 | 1167 | #define MSG_BUILD_LINE(_name) \ 1168 | do \ 1169 | { \ 1170 | if (msg->hdrs._name) \ 1171 | { \ 1172 | len = rtsp_msg_build_##_name(msg, p, size); \ 1173 | MSG_BUILD_STEP(); \ 1174 | } \ 1175 | } while (0) 1176 | 1177 | MSG_BUILD_LINE(cseq); 1178 | MSG_BUILD_LINE(date); 1179 | MSG_BUILD_LINE(session); 1180 | MSG_BUILD_LINE(transport); 1181 | MSG_BUILD_LINE(range); 1182 | 1183 | MSG_BUILD_LINE(accept); 1184 | MSG_BUILD_LINE(www_authenticate); 1185 | MSG_BUILD_LINE(user_agent); 1186 | 1187 | MSG_BUILD_LINE(public_); 1188 | MSG_BUILD_LINE(rtp_info); 1189 | MSG_BUILD_LINE(server); 1190 | 1191 | MSG_BUILD_LINE(content_type); 1192 | MSG_BUILD_LINE(content_length); 1193 | 1194 | snprintf(p, size, "\r\n"); 1195 | len = strlen(p); 1196 | MSG_BUILD_STEP(); 1197 | 1198 | if (msg->hdrs.content_length) 1199 | { 1200 | len = msg->hdrs.content_length->length; 1201 | if (len > size) 1202 | len = size; 1203 | memcpy(p, msg->body.body, len); 1204 | p += len; 1205 | //size -= len; 1206 | } 1207 | 1208 | dbg("\n%s", frame); 1209 | return (p - frame); 1210 | } 1211 | 1212 | int rtsp_msg_set_request(rtsp_msg_s *msg, rtsp_msg_method_e mt, const char *ipaddr, const char *abspath) 1213 | { 1214 | msg->type = RTSP_MSG_TYPE_REQUEST; 1215 | msg->hdrs.startline.reqline.method = mt; 1216 | msg->hdrs.startline.reqline.uri.scheme = RTSP_MSG_URI_SCHEME_RTSP; 1217 | strncpy(msg->hdrs.startline.reqline.uri.ipaddr, ipaddr, 1218 | sizeof(msg->hdrs.startline.reqline.uri.ipaddr) - 1); 1219 | strncpy(msg->hdrs.startline.reqline.uri.abspath, abspath, 1220 | sizeof(msg->hdrs.startline.reqline.uri.abspath) - 1); 1221 | msg->hdrs.startline.reqline.version = RTSP_MSG_VERSION_1_0; 1222 | return 0; 1223 | } 1224 | 1225 | int rtsp_msg_set_response(rtsp_msg_s *msg, int status_code) 1226 | { 1227 | msg->type = RTSP_MSG_TYPE_RESPONSE; 1228 | msg->hdrs.startline.resline.version = RTSP_MSG_VERSION_1_0; 1229 | msg->hdrs.startline.resline.status_code = status_code; 1230 | return 0; 1231 | } 1232 | 1233 | int rtsp_msg_get_cseq(const rtsp_msg_s *msg, uint32_t *cseq) 1234 | { 1235 | if (!msg->hdrs.cseq) 1236 | return -1; 1237 | if (cseq) 1238 | *cseq = msg->hdrs.cseq->cseq; 1239 | return 0; 1240 | } 1241 | 1242 | int rtsp_msg_set_cseq(rtsp_msg_s *msg, uint32_t cseq) 1243 | { 1244 | if (!msg->hdrs.cseq) 1245 | msg->hdrs.cseq = (rtsp_msg_cseq_s *)rtsp_mem_alloc(sizeof(rtsp_msg_cseq_s)); 1246 | if (!msg->hdrs.cseq) 1247 | return -1; 1248 | msg->hdrs.cseq->cseq = cseq; 1249 | return 0; 1250 | } 1251 | 1252 | int rtsp_msg_get_session(const rtsp_msg_s *msg, uint32_t *session) 1253 | { 1254 | if (!msg->hdrs.session) 1255 | return -1; 1256 | if (session) 1257 | *session = msg->hdrs.session->session; 1258 | return 0; 1259 | } 1260 | 1261 | int rtsp_msg_set_session(rtsp_msg_s *msg, uint32_t session) 1262 | { 1263 | if (!msg->hdrs.session) 1264 | msg->hdrs.session = (rtsp_msg_session_s *)rtsp_mem_alloc(sizeof(rtsp_msg_session_s)); 1265 | if (!msg->hdrs.session) 1266 | return -1; 1267 | msg->hdrs.session->session = session; 1268 | return 0; 1269 | } 1270 | 1271 | int rtsp_msg_get_date(const rtsp_msg_s *msg, char *date, int len) 1272 | { 1273 | if (!msg->hdrs.date) 1274 | return -1; 1275 | if (date) 1276 | strncpy(date, msg->hdrs.date->http_date, len - 1); 1277 | return 0; 1278 | } 1279 | 1280 | int rtsp_msg_set_date(rtsp_msg_s *msg, const char *date) 1281 | { 1282 | if (!msg->hdrs.date) 1283 | msg->hdrs.date = (rtsp_msg_date_s *)rtsp_mem_alloc(sizeof(rtsp_msg_date_s)); 1284 | if (!msg->hdrs.date) 1285 | return -1; 1286 | if (date) 1287 | { 1288 | strncpy(msg->hdrs.date->http_date, date, sizeof(msg->hdrs.date->http_date) - 1); 1289 | } 1290 | else 1291 | { 1292 | time_t t = time(NULL); 1293 | char *p; 1294 | strncpy(msg->hdrs.date->http_date, ctime(&t), 1295 | sizeof(msg->hdrs.date->http_date) - 1); 1296 | p = msg->hdrs.date->http_date; 1297 | while (isprint(*p)) 1298 | p++; 1299 | *p = 0; 1300 | } 1301 | return 0; 1302 | } 1303 | 1304 | int rtsp_msg_set_transport_udp(rtsp_msg_s *msg, uint32_t ssrc, int client_port, int server_port) 1305 | { 1306 | if (!msg->hdrs.transport) 1307 | msg->hdrs.transport = (rtsp_msg_transport_s *)rtsp_mem_alloc(sizeof(rtsp_msg_transport_s)); 1308 | if (!msg->hdrs.transport) 1309 | return -1; 1310 | msg->hdrs.transport->type = RTSP_MSG_TRANSPORT_TYPE_RTP_AVP; 1311 | msg->hdrs.transport->flags |= RTSP_MSG_TRANSPORT_FLAG_SSRC | RTSP_MSG_TRANSPORT_FLAG_UNICAST; 1312 | msg->hdrs.transport->ssrc = ssrc; 1313 | if (client_port >= 0) 1314 | { 1315 | msg->hdrs.transport->flags |= RTSP_MSG_TRANSPORT_FLAG_CLIENT_PORT; 1316 | msg->hdrs.transport->client_port = client_port; 1317 | } 1318 | if (server_port >= 0) 1319 | { 1320 | msg->hdrs.transport->flags |= RTSP_MSG_TRANSPORT_FLAG_SERVER_PORT; 1321 | msg->hdrs.transport->server_port = server_port; 1322 | } 1323 | return 0; 1324 | } 1325 | 1326 | int rtsp_msg_set_transport_tcp(rtsp_msg_s *msg, uint32_t ssrc, int interleaved) 1327 | { 1328 | if (!msg->hdrs.transport) 1329 | msg->hdrs.transport = (rtsp_msg_transport_s *)rtsp_mem_alloc(sizeof(rtsp_msg_transport_s)); 1330 | if (!msg->hdrs.transport) 1331 | return -1; 1332 | msg->hdrs.transport->type = RTSP_MSG_TRANSPORT_TYPE_RTP_AVP_TCP; 1333 | msg->hdrs.transport->flags |= RTSP_MSG_TRANSPORT_FLAG_SSRC; 1334 | msg->hdrs.transport->ssrc = ssrc; 1335 | if (interleaved >= 0) 1336 | { 1337 | msg->hdrs.transport->flags |= RTSP_MSG_TRANSPORT_FLAG_INTERLEAVED; 1338 | msg->hdrs.transport->interleaved = interleaved; 1339 | } 1340 | return 0; 1341 | } 1342 | 1343 | int rtsp_msg_get_accept(const rtsp_msg_s *msg, uint32_t *accept) 1344 | { 1345 | if (!msg->hdrs.accept) 1346 | return -1; 1347 | if (accept) 1348 | *accept = msg->hdrs.accept->accept; 1349 | return 0; 1350 | } 1351 | 1352 | int rtsp_msg_set_accept(rtsp_msg_s *msg, uint32_t accept) 1353 | { 1354 | if (!msg->hdrs.accept) 1355 | msg->hdrs.accept = (rtsp_msg_accept_s *)rtsp_mem_alloc(sizeof(rtsp_msg_accept_s)); 1356 | if (!msg->hdrs.accept) 1357 | return -1; 1358 | msg->hdrs.accept->accept = accept; 1359 | return 0; 1360 | } 1361 | 1362 | int rtsp_msg_get_user_agent(const rtsp_msg_s *msg, char *user_agent, int len) 1363 | { 1364 | if (!msg->hdrs.user_agent) 1365 | return -1; 1366 | if (user_agent) 1367 | strncpy(user_agent, msg->hdrs.user_agent->user_agent, len - 1); 1368 | return 0; 1369 | } 1370 | 1371 | int rtsp_msg_set_user_agent(rtsp_msg_s *msg, const char *user_agent) 1372 | { 1373 | if (!msg->hdrs.user_agent) 1374 | msg->hdrs.user_agent = (rtsp_msg_user_agent_s *)rtsp_mem_alloc(sizeof(rtsp_msg_user_agent_s)); 1375 | if (!msg->hdrs.user_agent) 1376 | return -1; 1377 | if (user_agent) 1378 | { 1379 | strncpy(msg->hdrs.user_agent->user_agent, user_agent, sizeof(msg->hdrs.user_agent->user_agent) - 1); 1380 | } 1381 | else 1382 | { 1383 | strncpy(msg->hdrs.user_agent->user_agent, "rtsp_msg_user_agent", 1384 | sizeof(msg->hdrs.user_agent->user_agent) - 1); 1385 | } 1386 | return 0; 1387 | } 1388 | 1389 | int rtsp_msg_get_public(const rtsp_msg_s *msg, uint32_t *public_) 1390 | { 1391 | if (!msg->hdrs.public_) 1392 | return -1; 1393 | if (public_) 1394 | *public_ = msg->hdrs.public_->public_; 1395 | return 0; 1396 | } 1397 | 1398 | int rtsp_msg_set_public(rtsp_msg_s *msg, uint32_t public_) 1399 | { 1400 | if (!msg->hdrs.public_) 1401 | msg->hdrs.public_ = (rtsp_msg_public_s *)rtsp_mem_alloc(sizeof(rtsp_msg_public_s)); 1402 | if (!msg->hdrs.public_) 1403 | return -1; 1404 | msg->hdrs.public_->public_ = public_; 1405 | return 0; 1406 | } 1407 | 1408 | int rtsp_msg_get_server(const rtsp_msg_s *msg, char *server, int len) 1409 | { 1410 | if (!msg->hdrs.server) 1411 | return -1; 1412 | if (server) 1413 | strncpy(server, msg->hdrs.server->server, len - 1); 1414 | return 0; 1415 | } 1416 | 1417 | int rtsp_msg_set_server(rtsp_msg_s *msg, const char *server) 1418 | { 1419 | if (!msg->hdrs.server) 1420 | msg->hdrs.server = (rtsp_msg_server_s *)rtsp_mem_alloc(sizeof(rtsp_msg_server_s)); 1421 | if (!msg->hdrs.server) 1422 | return -1; 1423 | if (server) 1424 | { 1425 | strncpy(msg->hdrs.server->server, server, sizeof(msg->hdrs.server->server) - 1); 1426 | } 1427 | else 1428 | { 1429 | strncpy(msg->hdrs.server->server, "rtsp_msg_server", 1430 | sizeof(msg->hdrs.server->server) - 1); 1431 | } 1432 | return 0; 1433 | } 1434 | 1435 | int rtsp_msg_get_content_type(const rtsp_msg_s *msg, int *type) 1436 | { 1437 | if (!msg->hdrs.content_type) 1438 | return -1; 1439 | if (type) 1440 | *type = msg->hdrs.content_type->type; 1441 | return 0; 1442 | } 1443 | 1444 | int rtsp_msg_set_content_type(rtsp_msg_s *msg, int type) 1445 | { 1446 | if (!msg->hdrs.content_type) 1447 | msg->hdrs.content_type = (rtsp_msg_content_type_s *)rtsp_mem_alloc(sizeof(rtsp_msg_content_type_s)); 1448 | if (!msg->hdrs.content_type) 1449 | return -1; 1450 | msg->hdrs.content_type->type = (rtsp_msg_content_type_e)type; 1451 | return 0; 1452 | } 1453 | 1454 | int rtsp_msg_get_content_length(const rtsp_msg_s *msg, int *length) 1455 | { 1456 | if (!msg->hdrs.content_length) 1457 | return -1; 1458 | if (length) 1459 | *length = msg->hdrs.content_length->length; 1460 | return 0; 1461 | } 1462 | 1463 | int rtsp_msg_set_content_length(rtsp_msg_s *msg, int length) 1464 | { 1465 | if (!msg->hdrs.content_length) 1466 | msg->hdrs.content_length = (rtsp_msg_content_length_s *)rtsp_mem_alloc(sizeof(rtsp_msg_content_length_s)); 1467 | if (!msg->hdrs.content_length) 1468 | return -1; 1469 | msg->hdrs.content_length->length = length; 1470 | return 0; 1471 | } 1472 | 1473 | int rtsp_msg_set_www_authenticate(rtsp_msg_s *msg, char *nonce, char *realm) 1474 | { 1475 | if (!msg->hdrs.www_authenticate) 1476 | msg->hdrs.www_authenticate = (rtsp_msg_www_authenticate_s *)rtsp_mem_alloc(sizeof(rtsp_msg_www_authenticate_s)); 1477 | if (!msg->hdrs.www_authenticate) 1478 | return -1; 1479 | if (nonce) 1480 | { 1481 | strncpy(msg->hdrs.www_authenticate->nonce, nonce, sizeof(msg->hdrs.www_authenticate->nonce) - 1); 1482 | strncpy(msg->hdrs.www_authenticate->realm, realm, sizeof(msg->hdrs.www_authenticate->realm) - 1); 1483 | } 1484 | 1485 | return 0; 1486 | } 1487 | 1488 | #if 0 1489 | #include 1490 | int main(int argc, char *argv[]) 1491 | { 1492 | const char *file = "rtsp.log"; 1493 | int fd; 1494 | char srcbuf[1024]; 1495 | char dstbuf[1024]; 1496 | int srclen, dstlen; 1497 | int ret; 1498 | rtsp_msg_s msg; 1499 | 1500 | rtsp_msg_init(&msg); 1501 | 1502 | if (argc > 1) 1503 | file = argv[1]; 1504 | 1505 | fd = open(file, O_RDONLY); 1506 | if (fd < 0) { 1507 | perror("open failed"); 1508 | return -1; 1509 | } 1510 | 1511 | srclen = 0; 1512 | do { 1513 | ret = rtsp_msg_parse_from_array(&msg, srcbuf, srclen); 1514 | if (ret < 0) { 1515 | printf(">>>>>>>>>>1\n"); 1516 | break; 1517 | } 1518 | if (ret == 0) { 1519 | ret = read(fd, srcbuf + srclen, sizeof(srcbuf) - srclen); 1520 | if (ret <= 0) { 1521 | printf(">>>>>>>>>>>2\n"); 1522 | break; 1523 | } 1524 | srclen += ret; 1525 | continue; 1526 | } 1527 | 1528 | printf("ret = %d\n", ret); 1529 | memmove(srcbuf, srcbuf + ret, srclen - ret); 1530 | srclen -= ret; 1531 | 1532 | ret = rtsp_msg_build_to_array(&msg, dstbuf, sizeof(dstbuf)); 1533 | if (ret <= 0) { 1534 | printf(">>>>>>>>>>3\n"); 1535 | break; 1536 | } 1537 | printf("ret = %d\n", ret); 1538 | fwrite(dstbuf, ret, 1, stderr); 1539 | rtsp_msg_free(&msg); 1540 | } while (srclen || ret > 0); 1541 | srcbuf[srclen] = 0; 1542 | printf("srclen = %d\n%s", srclen, srcbuf); 1543 | 1544 | close(fd); 1545 | return 0; 1546 | } 1547 | #endif 1548 | --------------------------------------------------------------------------------