src/payloadtype.c

00001 /*
00002   The oRTP library is an RTP (Realtime Transport Protocol - rfc3550) stack.
00003   Copyright (C) 2001  Simon MORLAT simon.morlat@linphone.org
00004 
00005   This library is free software; you can redistribute it and/or
00006   modify it under the terms of the GNU Lesser General Public
00007   License as published by the Free Software Foundation; either
00008   version 2.1 of the License, or (at your option) any later version.
00009 
00010   This library is distributed in the hope that it will be useful,
00011   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013   Lesser General Public License for more details.
00014 
00015   You should have received a copy of the GNU Lesser General Public
00016   License along with this library; if not, write to the Free Software
00017   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 */
00019 
00020 #include "ortp/ortp.h"
00021 #include "ortp/payloadtype.h"
00022 #include "utils.h"
00023 
00024 char *payload_type_get_rtpmap(PayloadType *pt)
00025 {
00026         int len=(int)strlen(pt->mime_type)+15;
00027         char *rtpmap=(char *) ortp_malloc(len);
00028         snprintf(rtpmap,len,"%s/%i/1",pt->mime_type,pt->clock_rate);
00029         return rtpmap;
00030 }
00031 
00032 PayloadType *payload_type_new()
00033 {
00034         PayloadType *newpayload=(PayloadType *)ortp_new0(PayloadType,1);
00035         newpayload->flags|=PAYLOAD_TYPE_ALLOCATED;
00036         return newpayload;
00037 }
00038 
00039 
00040 PayloadType *payload_type_clone(PayloadType *payload)
00041 {
00042         PayloadType *newpayload=(PayloadType *)ortp_new0(PayloadType,1);
00043         memcpy(newpayload,payload,sizeof(PayloadType));
00044         newpayload->mime_type=ortp_strdup(payload->mime_type);
00045         if (payload->recv_fmtp!=NULL) {
00046                 newpayload->recv_fmtp=ortp_strdup(payload->recv_fmtp);
00047         }
00048         if (payload->send_fmtp!=NULL){
00049                 newpayload->send_fmtp=ortp_strdup(payload->send_fmtp);
00050         }
00051         newpayload->flags|=PAYLOAD_TYPE_ALLOCATED;
00052         return newpayload;
00053 }
00054 
00055 static bool_t canWrite(PayloadType *pt){
00056         if (!(pt->flags & PAYLOAD_TYPE_ALLOCATED)) {
00057                 ortp_error("Cannot change parameters of statically defined payload types: make your"
00058                         " own copy using payload_type_clone() first.");
00059                 return FALSE;
00060         }
00061         return TRUE;
00062 }
00063 
00069 void payload_type_set_recv_fmtp(PayloadType *pt, const char *fmtp){
00070         if (canWrite(pt)){
00071                 if (pt->recv_fmtp!=NULL) ortp_free(pt->recv_fmtp);
00072                 if (fmtp!=NULL) pt->recv_fmtp=ortp_strdup(fmtp);
00073                 else pt->recv_fmtp=NULL;
00074         }
00075 }
00076 
00082 void payload_type_set_send_fmtp(PayloadType *pt, const char *fmtp){
00083         if (canWrite(pt)){
00084                 if (pt->send_fmtp!=NULL) ortp_free(pt->send_fmtp);
00085                 if (fmtp!=NULL) pt->send_fmtp=ortp_strdup(fmtp);
00086                 else pt->send_fmtp=NULL;
00087         }
00088 }
00089 
00093 void payload_type_destroy(PayloadType *pt)
00094 {
00095         if (pt->mime_type) ortp_free(pt->mime_type);
00096         if (pt->recv_fmtp) ortp_free(pt->recv_fmtp);
00097         if (pt->send_fmtp) ortp_free(pt->send_fmtp);
00098         ortp_free(pt);
00099 }
00100 
00112 bool_t fmtp_get_value(const char *fmtp, const char *param_name, char *result, size_t result_len){
00113         const char *pos=strstr(fmtp,param_name);
00114         if (pos){
00115                 const char *equal=strchr(pos,'=');
00116                 if (equal){
00117                         int copied;
00118                         const char *end=strchr(equal+1,';');
00119                         if (end==NULL) end=fmtp+strlen(fmtp); /*assuming this is the last param */
00120                         copied=MIN(result_len-1,end-(equal+1));
00121                         strncpy(result,equal+1,copied);
00122                         result[copied]='\0';
00123                         return TRUE;
00124                 }
00125         }
00126         return FALSE;
00127 }
00128 
00129 
00130 int rtp_profile_get_payload_number_from_mime(RtpProfile *profile,const char *mime)
00131 {
00132         PayloadType *pt;
00133         int i;
00134         for (i=0;i<RTP_PROFILE_MAX_PAYLOADS;i++)
00135         {
00136                 pt=rtp_profile_get_payload(profile,i);
00137                 if (pt!=NULL)
00138                 {
00139                         if (strcasecmp(pt->mime_type,mime)==0){
00140                                 return i;
00141                         }
00142                 }
00143         }
00144         return -1;
00145 }
00146 
00147 int rtp_profile_find_payload_number(RtpProfile*profile,const char *mime,int rate)
00148 {
00149         int i;
00150         PayloadType *pt;
00151         for (i=0;i<RTP_PROFILE_MAX_PAYLOADS;i++)
00152         {
00153                 pt=rtp_profile_get_payload(profile,i);
00154                 if (pt!=NULL)
00155                 {
00156                         if (strcasecmp(pt->mime_type,mime)==0 && pt->clock_rate==rate){
00157                         
00158                                 return i;
00159                         }
00160                 }
00161         }
00162         return -1;
00163 }
00164 
00165 int rtp_profile_get_payload_number_from_rtpmap(RtpProfile *profile,const char *rtpmap)
00166 {
00167         int clock_rate,ret;
00168         char *p,*mime,*tmp,*c;
00169         
00170         /* parse the rtpmap */
00171         tmp=ortp_strdup(rtpmap);
00172         p=strchr(tmp,'/');
00173         if (p!=NULL){
00174                 mime=tmp;
00175                 *p='\0';
00176                 c=p+1;
00177                 p=strchr(c,'/');
00178                 if (p!=NULL) *p='\0';
00179                 clock_rate=atoi(c);
00180         }else return -1;
00181         
00182         //printf("Searching for payload %s at freq %i",mime,clock_rate);
00183         ret=rtp_profile_find_payload_number(profile,mime,clock_rate);
00184         ortp_free(tmp);
00185         return ret;
00186 }
00187 
00188 PayloadType * rtp_profile_find_payload(RtpProfile *prof,const char *mime,int rate)
00189 {
00190         int i;
00191         i=rtp_profile_find_payload_number(prof,mime,rate);
00192         if (i>=0) return rtp_profile_get_payload(prof,i);
00193         return NULL;
00194 }
00195 
00196 
00197 PayloadType * rtp_profile_get_payload_from_mime(RtpProfile *profile,const char *mime)
00198 {
00199         int pt;
00200         pt=rtp_profile_get_payload_number_from_mime(profile,mime);
00201         if (pt==-1) return NULL;
00202         else return rtp_profile_get_payload(profile,pt);
00203 }
00204 
00205 
00206 PayloadType * rtp_profile_get_payload_from_rtpmap(RtpProfile *profile, const char *rtpmap)
00207 {
00208         int pt;
00209         pt=rtp_profile_get_payload_number_from_rtpmap(profile,rtpmap);
00210         if (pt==-1) return NULL;
00211         else return rtp_profile_get_payload(profile,pt);
00212 }
00213 
00214 int rtp_profile_move_payload(RtpProfile *prof,int oldpos,int newpos){
00215         prof->payload[newpos]=prof->payload[oldpos];
00216         prof->payload[oldpos]=NULL;
00217         return 0;
00218 }
00219 
00220 RtpProfile * rtp_profile_new(const char *name)
00221 {
00222         RtpProfile *prof=(RtpProfile*)ortp_new0(RtpProfile,1);
00223         rtp_profile_set_name(prof,name);
00224         return prof;
00225 }
00226 
00234 void rtp_profile_set_payload(RtpProfile *prof, int idx, PayloadType *pt){
00235         if (idx<0 || idx>=RTP_PROFILE_MAX_PAYLOADS) {
00236                 ortp_error("Bad index %i",idx);
00237                 return;
00238         }
00239         prof->payload[idx]=pt;
00240 }
00241 
00247 void rtp_profile_clear_all(RtpProfile *obj){
00248         int i;
00249         for (i=0;i<RTP_PROFILE_MAX_PAYLOADS;i++){
00250                 obj->payload[i]=0;
00251         }
00252 }
00253 
00254 
00261 void rtp_profile_set_name(RtpProfile *obj, const char *name){
00262         if (obj->name!=NULL) ortp_free(obj->name);
00263         obj->name=ortp_strdup(name);
00264 }
00265 
00266 /* ! payload are not cloned*/
00267 RtpProfile * rtp_profile_clone(RtpProfile *prof)
00268 {
00269         int i;
00270         PayloadType *pt;
00271         RtpProfile *newprof=rtp_profile_new(prof->name);
00272         for (i=0;i<RTP_PROFILE_MAX_PAYLOADS;i++){
00273                 pt=rtp_profile_get_payload(prof,i);
00274                 if (pt!=NULL){
00275                         rtp_profile_set_payload(newprof,i,pt);
00276                 }
00277         }
00278         return newprof;
00279 }
00280 
00281 
00282 /*clone a profile and its payloads */
00283 RtpProfile * rtp_profile_clone_full(RtpProfile *prof)
00284 {
00285         int i;
00286         PayloadType *pt;
00287         RtpProfile *newprof=rtp_profile_new(prof->name);
00288         for (i=0;i<RTP_PROFILE_MAX_PAYLOADS;i++){
00289                 pt=rtp_profile_get_payload(prof,i);
00290                 if (pt!=NULL){
00291                         rtp_profile_set_payload(newprof,i,payload_type_clone(pt));
00292                 }
00293         }
00294         return newprof;
00295 }
00296 
00297 void rtp_profile_destroy(RtpProfile *prof)
00298 {
00299         int i;
00300         PayloadType *payload;
00301         if (prof->name) {
00302                 ortp_free(prof->name);
00303                 prof->name = NULL;
00304         }
00305         for (i=0;i<RTP_PROFILE_MAX_PAYLOADS;i++)
00306         {
00307                 payload=rtp_profile_get_payload(prof,i);
00308                 if (payload!=NULL && (payload->flags & PAYLOAD_TYPE_ALLOCATED))
00309                         payload_type_destroy(payload);
00310         }
00311         ortp_free(prof);
00312 }

Generated on Fri Jun 22 17:31:19 2007 for oRTP by  doxygen 1.5.1