00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00027 #include <stdio.h>
00028 #include <stdlib.h>
00029 #include <string.h>
00030
00031 #include "utils.h"
00032 #include "comm.h"
00033 #include "rtp.h"
00034 #include "parsers/rtpparsers.h"
00035 #include "rtpptdefs.h"
00036
00037
00038
00039 static rtp_audio pcmu =
00040 { "PCMU", AU, 8000, 0, RTP_PT_ATTRS_INITIALIZER, NULL, 1 };
00041
00042 #if 0
00043
00044
00045 static rtp_audio res_au =
00046 { "res", AU, RTP_PT_ATTRS_INITIALIZER, NULL, 0 };
00047
00048
00049 #endif
00050
00051 static rtp_audio gsm =
00052 { "GSM", AU, 8000, 0, RTP_PT_ATTRS_INITIALIZER, NULL, 1 };
00053 static rtp_audio g723 =
00054 { "G723", AU, 8000, 0, RTP_PT_ATTRS_INITIALIZER, NULL, 1 };
00055 static rtp_audio dvi4_8000 =
00056 { "DVI4", AU, 8000, 0, RTP_PT_ATTRS_INITIALIZER, NULL, 1 };
00057 static rtp_audio dvi4_16000 =
00058 { "DVI4", AU, 16000, 0, RTP_PT_ATTRS_INITIALIZER, NULL, 1 };
00059 static rtp_audio lpc =
00060 { "LPC", AU, 8000, 0, RTP_PT_ATTRS_INITIALIZER, NULL, 1 };
00061 static rtp_audio pcma =
00062 { "PCMA", AU, 8000, 0, RTP_PT_ATTRS_INITIALIZER, NULL, 1 };
00063 static rtp_audio g722 =
00064 { "G722", AU, 8000, 0, RTP_PT_ATTRS_INITIALIZER, NULL, 1 };
00065 static rtp_audio l16_2 =
00066 { "L16", AU, 44100, 0, RTP_PT_ATTRS_INITIALIZER, NULL, 2 };
00067 static rtp_audio l16_1 =
00068 { "L16", AU, 44100, 0, RTP_PT_ATTRS_INITIALIZER, NULL, 1 };
00069 static rtp_audio qcelp =
00070 { "QCELP", AU, 8000, 0, RTP_PT_ATTRS_INITIALIZER, NULL, 1 };
00071 static rtp_audio cn =
00072 { "CN", AU, 8000, 0, RTP_PT_ATTRS_INITIALIZER, NULL, 1 };
00073 static rtp_audio mpa =
00074 { "MPA", AU, 90000, 0, RTP_PT_ATTRS_INITIALIZER, NULL, 0 };
00075 static rtp_audio g728 =
00076 { "G728", AU, 8000, 0, RTP_PT_ATTRS_INITIALIZER, NULL, 1 };
00077 static rtp_audio dvi4_11025 =
00078 { "DVI4", AU, 11025, 0, RTP_PT_ATTRS_INITIALIZER, NULL, 1 };
00079 static rtp_audio dvi4_22050 =
00080 { "DVI4", AU, 22050, 0, RTP_PT_ATTRS_INITIALIZER, NULL, 1 };
00081 static rtp_audio g729 =
00082 { "G729", AU, 8000, 0, RTP_PT_ATTRS_INITIALIZER, NULL, 1 };
00083
00084
00085 #if 0
00086
00087
00088 static rtp_audio unas_a =
00089 { "unas", AU, RTP_PT_ATTRS_INITIALIZER, NULL, 0 };
00090
00091
00092 static rtp_video unas_v =
00093 { "unas", VI, RTP_PT_ATTRS_INITIALIZER, NULL };
00094
00095
00096 #endif
00097 static rtp_video celb =
00098 { "CelB", VI, 90000, 0, RTP_PT_ATTRS_INITIALIZER, NULL };
00099 static rtp_video jpeg =
00100 { "JPEG", VI, 90000, 0, RTP_PT_ATTRS_INITIALIZER, NULL };
00101
00102
00103 static rtp_video nv =
00104 { "nv", VI, 90000, 0, RTP_PT_ATTRS_INITIALIZER, NULL };
00105
00106
00107
00108 static rtp_video h261 =
00109 { "H261", VI, 90000, 0, RTP_PT_ATTRS_INITIALIZER, NULL };
00110 static rtp_video mpv =
00111 { "MPV", VI, 90000, 0, RTP_PT_ATTRS_INITIALIZER, NULL };
00112 static rtp_audio_video mp2t =
00113 { "MP2T", AV, 90000, 0, RTP_PT_ATTRS_INITIALIZER, NULL, 0 };
00114 static rtp_video h263 =
00115 { "H263", VI, 90000, 0, RTP_PT_ATTRS_INITIALIZER, NULL };
00116
00117 #if 0
00118
00119
00120 static rtp_pt unas =
00121 { "unas", NA, RTP_PT_ATTRS_INITIALIZER, NULL };
00122 static rtp_pt res =
00123 { "res", NA, RTP_PT_ATTRS_INITIALIZER, NULL };
00124
00125
00126 static rtp_pt dyn =
00127 { "dyn", NA, RTP_PT_ATTRS_INITIALIZER, NULL };
00128 #endif
00129
00130 #if 0 // we don't use them yet.
00131
00132 static rtp_audio g726_40 =
00133 { "G726-40", AU, 800RTP_PT_ATTRS_INITIALIZER, NULL, 1 };
00134 static rtp_audio g726_32 =
00135 { "G726-32", AU, 800RTP_PT_ATTRS_INITIALIZER, NULL, 1 };
00136 static rtp_audio g726_24 =
00137 { "G726-24", AU, 800RTP_PT_ATTRS_INITIALIZER, NULL, 1 };
00138 static rtp_audio g726_16 =
00139 { "G726-16", AU, 800RTP_PT_ATTRS_INITIALIZER, NULL, 1 };
00140 static rtp_audio g729d =
00141 { "G729D", AU, 800RTP_PT_ATTRS_INITIALIZER, NULL, 1 };
00142 static rtp_audio g729e =
00143 { "G729E", AU, 800RTP_PT_ATTRS_INITIALIZER, NULL, 1 };
00144 static rtp_audio gsm_efr =
00145 { "GSM-EFR", AU, 800RTP_PT_ATTRS_INITIALIZER, NULL, 1 };
00146 static rtp_audio l8 =
00147 { "L8", AU, RTP_PT_ATTRS_INITIALIZER, NULL, 0 };
00148 static rtp_audio red =
00149 { "RED", AU, RTP_PT_ATTRS_INITIALIZER, NULL, 0 };
00150 static rtp_audio vdvi =
00151 { "VDVI", AU, RTP_PT_ATTRS_INITIALIZER, NULL, 1 };
00152 static rtp_video h263_1998 =
00153 { "H263-1998", VI, 9000RTP_PT_ATTRS_INITIALIZER, NULL };
00154 #endif
00155
00156 static rtp_pt *rtp_pt_defs[128] = {
00157 RTP_PT(&pcmu), NULL ,
00158 NULL , RTP_PT(&gsm),
00159 RTP_PT(&g723), RTP_PT(&dvi4_8000),
00160 RTP_PT(&dvi4_16000), RTP_PT(&lpc),
00161 RTP_PT(&pcma), RTP_PT(&g722),
00162 RTP_PT(&l16_2), RTP_PT(&l16_1),
00163 RTP_PT(&qcelp), RTP_PT(&cn),
00164 RTP_PT(&mpa),
00165 RTP_PT(&g728),
00166 RTP_PT(&dvi4_11025), RTP_PT(&dvi4_22050), RTP_PT(&g729),
00167 NULL ,
00168 NULL , NULL , NULL ,
00169 NULL ,
00170 NULL , RTP_PT(&celb),
00171 RTP_PT(&jpeg), NULL ,
00172 RTP_PT(&nv), NULL ,
00173 NULL , RTP_PT(&h261),
00174 RTP_PT(&mpv), RTP_PT(&mp2t),
00175 RTP_PT(&h263), NULL ,
00176 NULL , NULL ,
00177 NULL , NULL ,
00178 NULL , NULL ,
00179 NULL , NULL ,
00180 NULL , NULL ,
00181 NULL , NULL ,
00182 NULL , NULL ,
00183 NULL , NULL ,
00184 NULL , NULL ,
00185 NULL , NULL ,
00186 NULL , NULL ,
00187 NULL , NULL ,
00188 NULL , NULL ,
00189 NULL , NULL ,
00190 NULL , NULL ,
00191 NULL , NULL ,
00192 NULL , NULL ,
00193 NULL , NULL ,
00194 NULL , NULL ,
00195 NULL , NULL ,
00196 NULL , NULL ,
00197 NULL , NULL ,
00198 NULL , NULL ,
00199 NULL , NULL ,
00200 NULL , NULL ,
00201 NULL , NULL ,
00202 NULL , NULL ,
00203 NULL , NULL ,
00204 NULL , NULL ,
00205 NULL , NULL ,
00206 NULL , NULL ,
00207 NULL , NULL ,
00208 NULL , NULL ,
00209 NULL , NULL ,
00210 NULL , NULL ,
00211 NULL , NULL ,
00212 NULL , NULL ,
00213 NULL , NULL ,
00214 NULL , NULL ,
00215 NULL , NULL ,
00216 NULL , NULL ,
00217 NULL , NULL ,
00218 NULL , NULL ,
00219 NULL , NULL ,
00220 NULL , NULL ,
00221 NULL , NULL ,
00222 };
00223
00227 void rtpptdefs_new(rtp_pt * new_defs[])
00228 {
00229 memcpy(new_defs, rtp_pt_defs, sizeof(rtp_pt_defs));
00230 }
00231
00237 rtp_pt *rtp_pt_new(rtp_media_type mtype)
00238 {
00239 rtp_pt *new;
00240 size_t struct_size;
00241
00242 switch (mtype) {
00243 case AU:
00244 struct_size = sizeof(rtp_audio);
00245 break;
00246 case VI:
00247 struct_size = sizeof(rtp_video);
00248 break;
00249 case AV:
00250 struct_size = sizeof(rtp_audio_video);
00251 break;
00252 case NA:
00253 struct_size = sizeof(rtp_pt);
00254 break;
00255 default:
00256 return NULL;
00257 break;
00258 }
00259
00260 if (!(new = calloc(1, struct_size)))
00261 return NULL;
00262
00263 new->type = mtype;
00264 return new;
00265 }
00266
00276 int rtp_dynpt_set(rtp_pt * defs[], rtp_pt * pt, uint8_t value)
00277 {
00278 if (value < 96) {
00279 nms_printf(NMSML_ERR,
00280 "You tried to set a non-dynamic payload type (%u)\n",
00281 value);
00282 return RTP_ERROR;
00283 }
00284
00285 free(defs[value]);
00286 defs[value] = pt;
00287
00288 return RTP_OK;
00289 }
00290
00301 int rtp_dynpt_encname(rtp_pt * defs[], uint8_t value, char *enc_name)
00302 {
00303 if (value < 96) {
00304 nms_printf(NMSML_ERR,
00305 "You tried to set encoding name for non-dynamic payload type (%u)\n",
00306 value);
00307 return RTP_ERROR;
00308 }
00309
00310 if (!defs[value]) {
00311 nms_printf(NMSML_ERR,
00312 "No dynamic payload type initialized for pt %u\n",
00313 value);
00314 return RTP_ERROR;
00315 }
00316
00317 strncpy(defs[value]->name, enc_name, sizeof(defs[value]->name));
00318 defs[value]->name[sizeof(defs[value]->name)-1] = '\0';
00319
00320 return RTP_OK;
00321 }
00322
00323 #if 0
00324
00325 void rtp_pt_attrs_init(rtp_pt_attrs * attrs)
00326 {
00327 attrs->data = NULL;
00328 attrs->size = attrs->allocated = 0;
00329 }
00330 #endif
00331
00343 int rtp_pt_attr_add(rtp_pt * defs[], uint8_t value, char *attr)
00344 {
00345 rtp_pt_attrs *attrs;
00346
00347 if (value > 127)
00348 return RTP_ERROR;
00349
00350 attrs = &defs[value]->attrs;
00351
00352 if (attrs->allocated == attrs->size) {
00353 int minsize = min(attrs->allocated + 1, RTP_ATTRS_ARRAY_DEF_SIZE);
00354 if (! (attrs->data = realloc(attrs->data, minsize * sizeof(char*))))
00355 return -1;
00356 attrs->allocated = minsize;
00357 }
00358 if (!(attrs->data[attrs->size] = strdup(attr)))
00359 return -1;
00360
00361 attrs->size++;
00362
00363 return RTP_OK;
00364 }
00365
00376 rtp_pt * rtp_get_pt_info(rtp_session * rtp_sess, unsigned pt)
00377 {
00378 return rtp_sess->ptdefs[pt];
00379 }
00380
00392 int rtp_dynpt_reg(rtp_session * rtp_sess, unsigned pt, char *mime)
00393 {
00394 if (rtp_dynpt_encname(rtp_sess->ptdefs, pt, mime))
00395 return RTP_ERROR;
00396
00397 if (rtp_parser_reg(rtp_sess, pt, mime))
00398 return RTP_ERROR;
00399
00400 nms_printf(NMSML_DBG2, "registered %s mime for %d payload type\n",
00401 mime, pt);
00402
00403 return RTP_OK;
00404 }
00405
00417 int rtp_announce_pt(rtp_session * rtp_sess, unsigned pt,
00418 rtp_media_type media_type)
00419 {
00420 rtp_pt *rtppt;
00421 rtp_fmts_list *fmt, **prev_fmt;
00422
00423 if (pt > 127) {
00424 nms_printf(NMSML_ERR, "rtp payload type not valid (%u)\n", pt);
00425 return RTP_ERROR;
00426 } else if (pt >= 96) {
00427 if (!(rtppt = rtp_pt_new(media_type)))
00428 return RTP_ERROR;
00429 rtp_dynpt_set(rtp_sess->ptdefs, rtppt, pt);
00430 }
00431
00432 if (!(fmt = malloc(sizeof(rtp_fmts_list)))) {
00433 nms_printf(NMSML_FATAL,
00434 "Could not alloc memory for rtp_fmts_list\n");
00435 return RTP_ERRALLOC;
00436 }
00437 fmt->pt = pt;
00438 fmt->rtppt = rtp_sess->ptdefs[pt];
00439 fmt->next = NULL;
00440 for (prev_fmt = &rtp_sess->announced_fmts; *prev_fmt;
00441 prev_fmt = &(*prev_fmt)->next);
00442 *prev_fmt = fmt;
00443 prev_fmt = &fmt->next;
00444
00445 return RTP_OK;
00446 }