]> sjero.net Git - linphone/blob - coreapi/offeranswer.c
Merge branch 'master' of git.linphone.org:linphone-private
[linphone] / coreapi / offeranswer.c
1 /*
2 linphone
3 Copyright (C) 2010  Simon MORLAT (simon.morlat@free.fr)
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18 */
19
20 #include "sal.h"
21 #include "offeranswer.h"
22
23
24 static PayloadType * find_payload_type_best_match(const MSList *l, const PayloadType *refpt){
25         PayloadType *pt;
26         char value[10];
27         const MSList *elem;
28         PayloadType *candidate=NULL;
29
30         for (elem=l;elem!=NULL;elem=elem->next){
31                 pt=(PayloadType*)elem->data;
32                 if (strcasecmp(pt->mime_type,refpt->mime_type)==0 && pt->clock_rate==refpt->clock_rate){
33                         candidate=pt;
34                         /*good candidate, check fmtp for H264 */
35                         if (strcasecmp(pt->mime_type,"H264")==0){
36                                 if (pt->recv_fmtp!=NULL && refpt->recv_fmtp!=NULL){
37                                         int mode1=0,mode2=0;
38                                         if (fmtp_get_value(pt->recv_fmtp,"packetization-mode",value,sizeof(value))){
39                                                 mode1=atoi(value);
40                                         }
41                                         if (fmtp_get_value(refpt->recv_fmtp,"packetization-mode",value,sizeof(value))){
42                                                 mode2=atoi(value);
43                                         }
44                                         if (mode1==mode2)
45                                             break; /*exact match */
46                                 }
47                         }else break;
48                 }
49         }
50         return candidate;
51 }
52
53 static MSList *match_payloads(const MSList *local, const MSList *remote){
54         const MSList *e2;
55         MSList *res=NULL;
56         PayloadType *matched;
57         for(e2=remote;e2!=NULL;e2=e2->next){
58                 PayloadType *p2=(PayloadType*)e2->data;
59                 matched=find_payload_type_best_match(local,p2);
60                 if (matched){
61                         matched=payload_type_clone(matched);
62                         if (p2->send_fmtp)
63                                 payload_type_set_send_fmtp(matched,p2->send_fmtp);
64                         res=ms_list_append(res,matched);
65                         payload_type_set_number(matched,payload_type_get_number(p2));
66                 }else{
67                         ms_message("No match for %s/%i",p2->mime_type,p2->clock_rate);
68                 }
69         }
70         return res;
71 }
72
73 static bool_t only_telephone_event(const MSList *l){
74         PayloadType *p=(PayloadType*)l->data;
75         if (strcasecmp(p->mime_type,"telephone-event")!=0){
76                 return FALSE;
77         }
78         return TRUE;
79 }
80
81 static void initiate_outgoing(const SalStreamDescription *local_offer,
82                                         const SalStreamDescription *remote_answer,
83                                         SalStreamDescription *result){
84         if (remote_answer->port!=0)
85                 result->payloads=match_payloads(local_offer->payloads,remote_answer->payloads);
86         result->proto=local_offer->proto;
87         result->type=local_offer->type;
88         if (result->payloads && !only_telephone_event(result->payloads)){
89                 strcpy(result->addr,remote_answer->addr);
90                 result->port=remote_answer->port;
91                 result->bandwidth=remote_answer->bandwidth;
92                 result->ptime=remote_answer->ptime;
93         }else{
94                 result->port=0;
95         }
96 }
97
98
99 static void initiate_incoming(const SalStreamDescription *local_cap,
100                                         const SalStreamDescription *remote_offer,
101                                         SalStreamDescription *result){
102         result->payloads=match_payloads(local_cap->payloads,remote_offer->payloads);
103         result->proto=local_cap->proto;
104         result->type=local_cap->type;
105         if (result->payloads && !only_telephone_event(result->payloads)){
106                 strcpy(result->addr,local_cap->addr);
107                 result->port=local_cap->port;
108                 result->bandwidth=local_cap->bandwidth;
109                 result->ptime=local_cap->ptime;         
110         }else{
111                 result->port=0;
112         }
113 }
114
115 /**
116  * Returns a media description to run the streams with, based on a local offer
117  * and the returned response (remote).
118 **/
119 int offer_answer_initiate_outgoing(const SalMediaDescription *local_offer,
120                                                                         const SalMediaDescription *remote_answer,
121                                                         SalMediaDescription *result){
122     int i,j;
123         const SalStreamDescription *ls,*rs;
124     for(i=0,j=0;i<local_offer->nstreams;++i){
125                 ms_message("Processing for stream %i",i);
126                 ls=&local_offer->streams[i];
127                 rs=sal_media_description_find_stream(remote_answer,ls->proto,ls->type);
128         if (rs) {
129                         initiate_outgoing(ls,rs,&result->streams[j]);
130                         ++j;
131                 }
132                 else ms_warning("No matching stream for %i",i);
133     }
134         result->nstreams=j;
135         strcpy(result->addr,remote_answer->addr);
136         return 0;
137 }
138
139 /**
140  * Returns a media description to run the streams with, based on the local capabilities and
141  * and the received offer.
142  * The returned media description is an answer and should be sent to the offerer.
143 **/
144 int offer_answer_initiate_incoming(const SalMediaDescription *local_capabilities,
145                                                 const SalMediaDescription *remote_offer,
146                                         SalMediaDescription *result){
147     int i,j;
148         const SalStreamDescription *ls,*rs;
149                                                         
150     for(i=0,j=0;i<remote_offer->nstreams;++i){
151                 rs=&remote_offer->streams[i];
152                 ms_message("Processing for stream %i",i);
153                 ls=sal_media_description_find_stream(local_capabilities,rs->proto,rs->type);
154                 if (ls){
155                 initiate_incoming(ls,rs,&result->streams[j]);
156                         ++j;
157                 }
158     }
159         result->nstreams=j;
160         strcpy(result->username, local_capabilities->username);
161         strcpy(result->addr,local_capabilities->addr);
162         return 0;
163 }
164