00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00028 #ifndef NEMESI_RTP_H
00029 #define NEMESI_RTP_H
00030
00031 #ifdef HAVE_CONFIG_H
00032 #include <config.h>
00033 #endif
00034 #include <stdio.h>
00035 #include <stdlib.h>
00036 #include <string.h>
00037
00038 #ifndef WIN32
00039 #include <unistd.h>
00040 #include <arpa/inet.h>
00041 #include <netinet/in.h>
00042 #include <sys/socket.h>
00043 #include <sys/time.h>
00044 #include <sys/types.h>
00045 #endif
00046
00047 #include <time.h>
00048 #include <pthread.h>
00049 #include <stdint.h>
00050
00051 #include "comm.h"
00052 #include "transport.h"
00053 #include "rtpptdefs.h"
00054
00055 #define RTP_VERSION 2
00056
00057
00058 #define RTP_AVP_UDP "RTP/AVP"
00059 #define RTP_AVP_TCP "RTP/AVP/TCP"
00060 #define RTP_AVP_SCTP "RTP/AVP/SCTP"
00061
00062 #define RTP_SEQ_MOD (1<<16)
00063 #define MIN_SEQUENTIAL 2
00064 #define MAX_DROPOUT 3000
00065 #define MAX_MISORDER 100
00066
00067 #define BANDWIDTH 16000
00068
00069 #define RTP_OK 0
00070
00071 #define RTP_ERROR 1
00072 #define RTP_ERRALLOC -1
00073
00074
00078 typedef struct {
00079 #ifdef WORDS_BIGENDIAN
00080 uint32_t ver:2;
00081 uint32_t pad:1;
00082 uint32_t ext:1;
00083 uint32_t cc:4;
00084 #else
00085 uint32_t cc:4;
00086 uint32_t ext:1;
00087 uint32_t pad:1;
00088 uint32_t ver:2;
00089 #endif
00090 #ifdef WORDS_BIGENDIAN
00091 uint32_t mark:1;
00092 uint32_t pt:7;
00093 #else
00094 uint32_t pt:7;
00095 uint32_t mark:1;
00096 #endif
00097 uint32_t seq:16;
00098 uint32_t time;
00099 uint32_t ssrc;
00100 uint8_t data[1];
00101 } rtp_pkt;
00102
00103 typedef struct {
00104 uint32_t len;
00105 uint8_t *data;
00106 } rtp_buff;
00107
00108
00109 typedef struct {
00110 long len;
00111 uint32_t timestamp;
00112 double time_sec;
00113 int fps;
00114 uint8_t pt;
00115 uint8_t *data;
00116 } rtp_frame;
00117
00118 #define RTP_PKT_CC(pkt) (pkt->cc)
00119 #define RTP_PKT_MARK(pkt) (pkt->mark)
00120 #define RTP_PKT_PT(pkt) (pkt->pt)
00121 #define RTP_PKT_SEQ(pkt) ntohs(pkt->seq)
00122 #define RTP_PKT_TS(pkt) ntohl(pkt->time)
00123 #define RTP_PKT_SSRC(pkt) ntohl(pkt->ssrc)
00124 #define RTP_PKT_DATA(pkt) (pkt->data + pkt->cc)
00125 #define RTP_PAYLOAD_SIZE(pkt, pkt_len) ((pkt) ? pkt_len - ((pkt->data)-(uint8_t *)pkt) - pkt->cc - ((*(((uint8_t *)pkt)+pkt_len-1)) * pkt->pad) : 0)
00126
00127 #define RTPPT_ISDYNAMIC(pt) (pt >= 96)
00128
00129 #define RTP_TRANSPORT_SPEC 10
00130 #define RTP_TRANSPORT_SOCKTYPE 11
00131 #define RTP_TRANSPORT_DELIVERY 20
00132 #define RTP_TRANSPORT_SRCADDR 30
00133 #define RTP_TRANSPORT_SRCADDRSTR 31
00134 #define RTP_TRANSPORT_DSTADDR 40
00135 #define RTP_TRANSPORT_DSTADDRSTR 41
00136 #define RTP_TRANSPORT_LAYERS 50
00137 #define RTP_TRANSPORT_MODE 60
00138 #define RTP_TRANSPORT_APPEND 70
00139 #define RTP_TRANSPORT_TTL 80
00140 #define RTP_TRANSPORT_MCSRTP 90
00141 #define RTP_TRANSPORT_MCSRTCP 91
00142 #define RTP_TRANSPORT_MCSPORTS 92
00143 #define RTP_TRANSPORT_CLIRTP 100
00144 #define RTP_TRANSPORT_CLIRTCP 101
00145 #define RTP_TRANSPORT_CLIPORTS 102
00146 #define RTP_TRANSPORT_SRVRTP 110
00147 #define RTP_TRANSPORT_SRVRTCP 111
00148 #define RTP_TRANSPORT_SRVPORTS 112
00149 #define RTP_TRANSPORT_ILVDRTP 120
00150 #define RTP_TRANSPORT_ILVDRTCP 121
00151 #define RTP_TRANSPORT_INTERLEAVED 122
00152 #define RTP_TRANSPORT_STREAMRTP 130
00153 #define RTP_TRANSPORT_STREAMRTCP 131
00154 #define RTP_TRANSPORT_STREAMS 132
00155 #define RTP_TRANSPORT_SSRC 140
00156
00157 #define RTP_TRANSPORT_NOTSET -1
00158 #define RTP_TRANSPORT_SET 0
00159 #define RTP_TRANSPORT_ERR 1
00160
00161 enum deliveries { unicast, multicast };
00162 enum modes { play, record };
00163
00164 typedef struct {
00165 char *spec;
00166 uint32_t ssrc;
00167 sock_type type;
00168 enum modes mode;
00169 int append;
00170 int layers;
00171 int ttl;
00172 enum deliveries delivery;
00173 nms_transport RTP;
00174 nms_transport RTCP;
00175 } rtp_transport;
00176
00177 struct rtp_ssrc_stats {
00178 uint16_t max_seq;
00179 uint32_t cycles;
00180 uint32_t base_seq;
00181 uint32_t bad_seq;
00182 uint32_t probation;
00183 uint32_t received;
00184 uint32_t expected_prior;
00185 uint32_t received_prior;
00186 uint32_t transit;
00187 double jitter;
00188 struct timeval lastrtp;
00189 struct timeval lastsr;
00190 uint32_t ntplastsr[2];
00191 uint32_t firstts;
00192 uint32_t lastts;
00193 struct timeval firsttv;
00194 };
00195
00196 struct rtp_ssrc_descr {
00197 char *end;
00198 char *cname;
00199 char *name;
00200 char *email;
00201 char *phone;
00202 char *loc;
00203 char *tool;
00204 char *note;
00205 char *priv;
00206 };
00207
00208 struct rtp_session_stats {
00209 struct timeval tp;
00210 struct timeval tn;
00211 uint32_t pmembers;
00212 uint32_t members;
00213 uint32_t senders;
00214 double rtcp_bw;
00215 uint8_t we_sent;
00216 double avg_rtcp_size;
00217 uint8_t initial;
00218 };
00219
00220 #define SSRC_KNOWN 0
00221 #define SSRC_NEW 1
00222 #define SSRC_RTPNEW 2
00223 #define SSRC_RTCPNEW 3
00224 #define SSRC_COLLISION 4
00225
00226 typedef struct rtp_ssrc_s {
00227 uint32_t ssrc;
00228 nms_sockaddr rtp_from;
00229 nms_sockaddr rtcp_from;
00230 nms_sockaddr rtcp_to;
00231 int no_rtcp;
00232 struct rtp_ssrc_stats ssrc_stats;
00233 struct rtp_ssrc_descr ssrc_sdes;
00234 struct playout_buff_t * po;
00235 struct rtp_session_s *rtp_sess;
00236 void *privs[128];
00237 struct rtp_ssrc_s *next;
00238 struct rtp_ssrc_s *next_active;
00239 int done_seek;
00240 void *park;
00241 } rtp_ssrc;
00242
00243 struct rtp_conflict {
00244 nms_sockaddr transaddr;
00245 time_t time;
00246 struct rtp_conflict *next;
00247 };
00248
00254 typedef int (*rtp_parser_init) (struct rtp_session_s * rtp_sess, unsigned pt);
00255 typedef int (*rtp_parser) (rtp_ssrc * stm_src, rtp_frame * fr,
00256 rtp_buff * conf);
00257 typedef int (*rtp_parser_uninit) (rtp_ssrc * stm_src, unsigned pt);
00258
00259 typedef struct rtp_session_s {
00260 void * owner;
00261 uint32_t local_ssrc;
00262 struct rtp_session_stats sess_stats;
00263 rtp_ssrc *ssrc_queue;
00264 rtp_ssrc *active_ssrc_queue;
00265 struct rtp_conflict *conf_queue;
00266 struct buffer_pool_t * bp;
00267 struct rtp_session_s *next;
00268 pthread_mutex_t syn;
00269 rtp_pt *ptdefs[128];
00270 rtp_fmts_list *announced_fmts;
00271
00272 rtp_parser_init parsers_inits[128];
00273 rtp_parser parsers[128];
00274 rtp_parser_uninit parsers_uninits[128];
00275 void *park;
00276 float fps;
00277 rtp_transport transport;
00278 } rtp_session;
00279
00280 typedef struct {
00281 int run;
00282
00283 rtp_session *rtp_sess_head;
00284
00285
00286 unsigned int prebuffer_size;
00287
00288 pthread_mutex_t syn;
00289 pthread_t rtp_tid;
00290 pthread_t rtcp_tid;
00291 } rtp_thread;
00292
00293 enum rtp_protos {
00294 RTP,
00295 RTCP
00296 };
00297
00301 #define RTP_FILL_OK 0
00302 #define RTP_BUFF_EMPTY 91
00303 #define RTP_PARSE_ERROR 92
00304 #define RTP_PKT_UNKNOWN 93
00305 #define RTP_IN_PRM_ERR 94
00306 #define RTP_SSRC_NOTVALID 97
00307 #define RTP_BUFFERING 99
00308
00309 #define RTP_PKT_DATA_LEN(pkt, len) (len > 0) ? len - ((uint8_t *)(pkt->data)-(uint8_t *)pkt) - pkt->cc - ((*(((uint8_t *)pkt)+len-1)) * pkt->pad) : 0
00310
00323 rtp_thread *rtp_init(void);
00324 int rtp_thread_create(rtp_thread *);
00335 int rtp_announce_pt(rtp_session * rtp_sess, unsigned pt,
00336 rtp_media_type media_type);
00337 int rtp_dynpt_reg(rtp_session * rtp_sess, unsigned pt, char *mime);
00338
00339 rtp_pt * rtp_get_pt_info(rtp_session * rtp_sess, unsigned pt);
00350 int rtp_recv(rtp_session *);
00361 rtp_ssrc *rtp_active_ssrc_queue(rtp_session * rtp_sess_head);
00362 rtp_ssrc *rtp_next_active_ssrc(rtp_ssrc * ssrc);
00363
00364 int rtp_ssrc_check(rtp_session *, uint32_t, rtp_ssrc **, nms_sockaddr *,
00365 enum rtp_protos);
00366 int rtp_ssrc_init(rtp_session *, rtp_ssrc **, uint32_t, nms_sockaddr *,
00367 enum rtp_protos);
00378 int rtp_fill_buffers(rtp_thread *);
00379 int rtp_fill_buffer(rtp_ssrc *, rtp_frame *, rtp_buff *);
00380
00381 double rtp_get_next_ts(rtp_ssrc *);
00382 int16_t rtp_get_next_pt(rtp_ssrc *);
00383 void rtp_update_fps(rtp_ssrc * stm_src, uint32_t, unsigned);
00384 float rtp_get_fps(rtp_ssrc *);
00385
00386 rtp_pkt *rtp_get_n_pkt(rtp_ssrc *, unsigned int *, unsigned);
00387 rtp_pkt *rtp_get_pkt(rtp_ssrc *, size_t *);
00388 int rtp_rm_pkt(rtp_ssrc *);
00389 void rtp_rm_all_pkts(rtp_ssrc *);
00399 int rtp_transport_set(rtp_session *, int, void *);
00400 int rtp_transport_get(rtp_session *, int, void *, uint32_t);
00401
00405 char *rtp_get_spec(rtp_session *);
00406 sock_type rtp_get_socktype(rtp_session *);
00407 enum deliveries rtp_get_delivery(rtp_session *);
00408 int rtp_get_srcaddrstr(rtp_session *, char *, uint32_t);
00409 nms_addr *rtp_get_srcaddr(rtp_session *);
00410 int rtp_get_dstaddrstr(rtp_session *, char *, uint32_t);
00411 nms_addr *rtp_get_dstaddr(rtp_session *);
00412 int rtp_get_layers(rtp_session *);
00413 enum modes rtp_get_mode(rtp_session *);
00414 int rtp_get_append(rtp_session *);
00415 int rtp_get_ttl(rtp_session *);
00416 int rtp_get_mcsports(rtp_session *, in_port_t[2]);
00417 in_port_t rtp_get_mcsrtpport(rtp_session *);
00418 in_port_t rtp_get_mcsrtcpport(rtp_session *);
00419 int rtp_get_srvports(rtp_session *, in_port_t[2]);
00420 in_port_t rtp_get_srvrtpport(rtp_session *);
00421 in_port_t rtp_get_srvrtcpport(rtp_session *);
00422 int rtp_get_cliports(rtp_session *, in_port_t[2]);
00423 in_port_t rtp_get_clirtpport(rtp_session *);
00424 in_port_t rtp_get_clirtcpport(rtp_session *);
00425 int rtp_get_interleaved(rtp_session *, uint8_t[2]);
00426 uint8_t rtp_get_ilvdrtp(rtp_session *);
00427 uint8_t rtp_get_ilvdrtcp(rtp_session *);
00428 int rtp_get_streams(rtp_session *, uint16_t[2]);
00429 uint16_t rtp_get_rtpstream(rtp_session *);
00430 uint16_t rtp_get_rtcpstream(rtp_session *);
00431 uint32_t rtp_get_ssrc(rtp_session *);
00432
00436 int rtp_set_delivery(rtp_session *, enum deliveries);
00437 int rtp_set_socktype(rtp_session *, sock_type);
00438 int rtp_set_srcaddrstr(rtp_session *, char *);
00439 int rtp_set_srcaddr(rtp_session *, nms_addr *);
00440 int rtp_set_dstaddrstr(rtp_session *, char *);
00441 int rtp_set_dstaddr(rtp_session *, nms_addr *);
00442 int rtp_set_layers(rtp_session *, int);
00443 int rtp_set_mode(rtp_session *, enum modes);
00444 int rtp_set_append(rtp_session *, int);
00445 int rtp_set_ttl(rtp_session *, int);
00446 int rtp_set_mcsports(rtp_session *, in_port_t[2]);
00447 int rtp_set_mcsrtpport(rtp_session *, in_port_t);
00448 int rtp_set_mcsrtcpport(rtp_session *, in_port_t);
00449 int rtp_set_srvports(rtp_session *, in_port_t[2]);
00450 int rtp_set_srvrtpport(rtp_session *, in_port_t);
00451 int rtp_set_srvrtcpport(rtp_session *, in_port_t);
00452 int rtp_set_cliports(rtp_session *, in_port_t[2]);
00453 int rtp_set_clirtpport(rtp_session *, in_port_t);
00454 int rtp_set_clirtcpport(rtp_session *, in_port_t);
00455 int rtp_set_interleaved(rtp_session *, uint8_t[2]);
00456 int rtp_set_ilvdrtp(rtp_session *, uint8_t);
00457 int rtp_set_ilvdrtcp(rtp_session *, uint8_t);
00458 int rtp_set_streams(rtp_session *, uint16_t[2]);
00459 int rtp_set_rtpstream(rtp_session *, uint16_t);
00460 int rtp_set_rtcpstream(rtp_session *, uint16_t);
00461 int rtp_set_ssrc(rtp_session *, uint32_t);
00471 rtp_session *rtp_session_init(nms_sockaddr *local, nms_sockaddr *peer);
00472 struct rtsp_ctrl_t;
00473 rtp_ssrc * rtp_session_get_ssrc(rtp_session *sess, struct rtsp_ctrl_t *ctl);
00482 #endif