blob: 11e1dd227133f390c0345adbb658e3d52c6b0000 [file] [log] [blame]
Alexandre Lision8af73cb2013-12-10 14:11:20 -05001/* $Id$ */
2/*
3 * Copyright (C) 2011 Teluu Inc. (http://www.teluu.com)
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (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#include <pjmedia/vid_stream.h>
20#include <pjmedia/sdp_neg.h>
21#include <pjmedia/stream_common.h>
22#include <pj/ctype.h>
23#include <pj/rand.h>
24
25#if defined(PJMEDIA_HAS_VIDEO) && (PJMEDIA_HAS_VIDEO != 0)
26
27static const pj_str_t ID_VIDEO = { "video", 5};
28static const pj_str_t ID_IN = { "IN", 2 };
29static const pj_str_t ID_IP4 = { "IP4", 3};
30static const pj_str_t ID_IP6 = { "IP6", 3};
31static const pj_str_t ID_RTP_AVP = { "RTP/AVP", 7 };
32static const pj_str_t ID_RTP_SAVP = { "RTP/SAVP", 8 };
33//static const pj_str_t ID_SDP_NAME = { "pjmedia", 7 };
34static const pj_str_t ID_RTPMAP = { "rtpmap", 6 };
35
36static const pj_str_t STR_INACTIVE = { "inactive", 8 };
37static const pj_str_t STR_SENDRECV = { "sendrecv", 8 };
38static const pj_str_t STR_SENDONLY = { "sendonly", 8 };
39static const pj_str_t STR_RECVONLY = { "recvonly", 8 };
40
41
42/*
43 * Internal function for collecting codec info and param from the SDP media.
44 */
45static pj_status_t get_video_codec_info_param(pjmedia_vid_stream_info *si,
46 pj_pool_t *pool,
47 pjmedia_vid_codec_mgr *mgr,
48 const pjmedia_sdp_media *local_m,
49 const pjmedia_sdp_media *rem_m)
50{
51 unsigned pt = 0;
52 const pjmedia_vid_codec_info *p_info;
53 pj_status_t status;
54
55 pt = pj_strtoul(&local_m->desc.fmt[0]);
56
57 /* Get payload type for receiving direction */
58 si->rx_pt = pt;
59
60 /* Get codec info and payload type for transmitting direction. */
61 if (pt < 96) {
62 /* For static payload types, get the codec info from codec manager. */
63 status = pjmedia_vid_codec_mgr_get_codec_info(mgr, pt, &p_info);
64 if (status != PJ_SUCCESS)
65 return status;
66
67 si->codec_info = *p_info;
68
69 /* Get payload type for transmitting direction.
70 * For static payload type, pt's are symetric.
71 */
72 si->tx_pt = pt;
73 } else {
74 const pjmedia_sdp_attr *attr;
75 pjmedia_sdp_rtpmap *rtpmap;
76 pjmedia_codec_id codec_id;
77 pj_str_t codec_id_st;
78 unsigned i;
79
80 /* Determine payload type for outgoing channel, by finding
81 * dynamic payload type in remote SDP that matches the answer.
82 */
83 si->tx_pt = 0xFFFF;
84 for (i=0; i<rem_m->desc.fmt_count; ++i) {
85 if (pjmedia_sdp_neg_fmt_match(NULL,
86 (pjmedia_sdp_media*)local_m, 0,
87 (pjmedia_sdp_media*)rem_m, i, 0) ==
88 PJ_SUCCESS)
89 {
90 /* Found matched codec. */
91 si->tx_pt = pj_strtoul(&rem_m->desc.fmt[i]);
92 break;
93 }
94 }
95
96 if (si->tx_pt == 0xFFFF)
97 return PJMEDIA_EMISSINGRTPMAP;
98
99 /* For dynamic payload types, get codec name from the rtpmap */
100 attr = pjmedia_sdp_media_find_attr(local_m, &ID_RTPMAP,
101 &local_m->desc.fmt[0]);
102 if (attr == NULL)
103 return PJMEDIA_EMISSINGRTPMAP;
104
105 status = pjmedia_sdp_attr_to_rtpmap(pool, attr, &rtpmap);
106 if (status != PJ_SUCCESS)
107 return status;
108
109 /* Then get the codec info from the codec manager */
110 pj_ansi_snprintf(codec_id, sizeof(codec_id), "%.*s/",
111 (int)rtpmap->enc_name.slen, rtpmap->enc_name.ptr);
112 codec_id_st = pj_str(codec_id);
113 i = 1;
114 status = pjmedia_vid_codec_mgr_find_codecs_by_id(mgr, &codec_id_st,
115 &i, &p_info, NULL);
116 if (status != PJ_SUCCESS)
117 return status;
118
119 si->codec_info = *p_info;
120 }
121
122
123 /* Request for codec with the correct packing for streaming */
124 si->codec_info.packings = PJMEDIA_VID_PACKING_PACKETS;
125
126 /* Now that we have codec info, get the codec param. */
127 si->codec_param = PJ_POOL_ALLOC_T(pool, pjmedia_vid_codec_param);
128 status = pjmedia_vid_codec_mgr_get_default_param(mgr,
129 &si->codec_info,
130 si->codec_param);
131
132 /* Adjust encoding bitrate, if higher than remote preference. The remote
133 * bitrate preference is read from SDP "b=TIAS" line in media level.
134 */
135 if ((si->dir & PJMEDIA_DIR_ENCODING) && rem_m->bandw_count) {
136 unsigned i, bandw = 0;
137
138 for (i = 0; i < rem_m->bandw_count; ++i) {
139 const pj_str_t STR_BANDW_MODIFIER_TIAS = { "TIAS", 4 };
140 if (!pj_stricmp(&rem_m->bandw[i]->modifier,
141 &STR_BANDW_MODIFIER_TIAS))
142 {
143 bandw = rem_m->bandw[i]->value;
144 break;
145 }
146 }
147
148 if (bandw) {
149 pjmedia_video_format_detail *enc_vfd;
150 enc_vfd = pjmedia_format_get_video_format_detail(
151 &si->codec_param->enc_fmt, PJ_TRUE);
152 if (!enc_vfd->avg_bps || enc_vfd->avg_bps > bandw)
153 enc_vfd->avg_bps = bandw * 3 / 4;
154 if (!enc_vfd->max_bps || enc_vfd->max_bps > bandw)
155 enc_vfd->max_bps = bandw;
156 }
157 }
158
159 /* Get remote fmtp for our encoder. */
160 pjmedia_stream_info_parse_fmtp(pool, rem_m, si->tx_pt,
161 &si->codec_param->enc_fmtp);
162
163 /* Get local fmtp for our decoder. */
164 pjmedia_stream_info_parse_fmtp(pool, local_m, si->rx_pt,
165 &si->codec_param->dec_fmtp);
166
167 /* When direction is NONE (it means SDP negotiation has failed) we don't
168 * need to return a failure here, as returning failure will cause
169 * the whole SDP to be rejected. See ticket #:
170 * http://
171 *
172 * Thanks Alain Totouom
173 */
174 if (status != PJ_SUCCESS && si->dir != PJMEDIA_DIR_NONE)
175 return status;
176
177 return PJ_SUCCESS;
178}
179
180
181
182/*
183 * Create stream info from SDP media line.
184 */
185PJ_DEF(pj_status_t) pjmedia_vid_stream_info_from_sdp(
186 pjmedia_vid_stream_info *si,
187 pj_pool_t *pool,
188 pjmedia_endpt *endpt,
189 const pjmedia_sdp_session *local,
190 const pjmedia_sdp_session *remote,
191 unsigned stream_idx)
192{
193 const pjmedia_sdp_attr *attr;
194 const pjmedia_sdp_media *local_m;
195 const pjmedia_sdp_media *rem_m;
196 const pjmedia_sdp_conn *local_conn;
197 const pjmedia_sdp_conn *rem_conn;
198 int rem_af, local_af;
199 pj_sockaddr local_addr;
200 pj_status_t status;
201
202 PJ_UNUSED_ARG(endpt);
203
204 /* Validate arguments: */
205 PJ_ASSERT_RETURN(pool && si && local && remote, PJ_EINVAL);
206 PJ_ASSERT_RETURN(stream_idx < local->media_count, PJ_EINVAL);
207 PJ_ASSERT_RETURN(stream_idx < remote->media_count, PJ_EINVAL);
208
209 /* Keep SDP shortcuts */
210 local_m = local->media[stream_idx];
211 rem_m = remote->media[stream_idx];
212
213 local_conn = local_m->conn ? local_m->conn : local->conn;
214 if (local_conn == NULL)
215 return PJMEDIA_SDP_EMISSINGCONN;
216
217 rem_conn = rem_m->conn ? rem_m->conn : remote->conn;
218 if (rem_conn == NULL)
219 return PJMEDIA_SDP_EMISSINGCONN;
220
221 /* Media type must be video */
222 if (pj_stricmp(&local_m->desc.media, &ID_VIDEO) != 0)
223 return PJMEDIA_EINVALIMEDIATYPE;
224
225
226 /* Reset: */
227
228 pj_bzero(si, sizeof(*si));
229
230 /* Media type: */
231 si->type = PJMEDIA_TYPE_VIDEO;
232
233 /* Transport protocol */
234
235 /* At this point, transport type must be compatible,
236 * the transport instance will do more validation later.
237 */
238 status = pjmedia_sdp_transport_cmp(&rem_m->desc.transport,
239 &local_m->desc.transport);
240 if (status != PJ_SUCCESS)
241 return PJMEDIA_SDPNEG_EINVANSTP;
242
243 if (pj_stricmp(&local_m->desc.transport, &ID_RTP_AVP) == 0) {
244
245 si->proto = PJMEDIA_TP_PROTO_RTP_AVP;
246
247 } else if (pj_stricmp(&local_m->desc.transport, &ID_RTP_SAVP) == 0) {
248
249 si->proto = PJMEDIA_TP_PROTO_RTP_SAVP;
250
251 } else {
252
253 si->proto = PJMEDIA_TP_PROTO_UNKNOWN;
254 return PJ_SUCCESS;
255 }
256
257
258 /* Check address family in remote SDP */
259 rem_af = pj_AF_UNSPEC();
260 if (pj_stricmp(&rem_conn->net_type, &ID_IN)==0) {
261 if (pj_stricmp(&rem_conn->addr_type, &ID_IP4)==0) {
262 rem_af = pj_AF_INET();
263 } else if (pj_stricmp(&rem_conn->addr_type, &ID_IP6)==0) {
264 rem_af = pj_AF_INET6();
265 }
266 }
267
268 if (rem_af==pj_AF_UNSPEC()) {
269 /* Unsupported address family */
270 return PJ_EAFNOTSUP;
271 }
272
273 /* Set remote address: */
274 status = pj_sockaddr_init(rem_af, &si->rem_addr, &rem_conn->addr,
275 rem_m->desc.port);
276 if (status != PJ_SUCCESS) {
277 /* Invalid IP address. */
278 return PJMEDIA_EINVALIDIP;
279 }
280
281 /* Check address family of local info */
282 local_af = pj_AF_UNSPEC();
283 if (pj_stricmp(&local_conn->net_type, &ID_IN)==0) {
284 if (pj_stricmp(&local_conn->addr_type, &ID_IP4)==0) {
285 local_af = pj_AF_INET();
286 } else if (pj_stricmp(&local_conn->addr_type, &ID_IP6)==0) {
287 local_af = pj_AF_INET6();
288 }
289 }
290
291 if (local_af==pj_AF_UNSPEC()) {
292 /* Unsupported address family */
293 return PJ_SUCCESS;
294 }
295
296 /* Set remote address: */
297 status = pj_sockaddr_init(local_af, &local_addr, &local_conn->addr,
298 local_m->desc.port);
299 if (status != PJ_SUCCESS) {
300 /* Invalid IP address. */
301 return PJMEDIA_EINVALIDIP;
302 }
303
304 /* Local and remote address family must match */
305 if (local_af != rem_af)
306 return PJ_EAFNOTSUP;
307
308 /* Media direction: */
309
310 if (local_m->desc.port == 0 ||
311 pj_sockaddr_has_addr(&local_addr)==PJ_FALSE ||
312 pj_sockaddr_has_addr(&si->rem_addr)==PJ_FALSE ||
313 pjmedia_sdp_media_find_attr(local_m, &STR_INACTIVE, NULL)!=NULL)
314 {
315 /* Inactive stream. */
316
317 si->dir = PJMEDIA_DIR_NONE;
318
319 } else if (pjmedia_sdp_media_find_attr(local_m, &STR_SENDONLY, NULL)!=NULL) {
320
321 /* Send only stream. */
322
323 si->dir = PJMEDIA_DIR_ENCODING;
324
325 } else if (pjmedia_sdp_media_find_attr(local_m, &STR_RECVONLY, NULL)!=NULL) {
326
327 /* Recv only stream. */
328
329 si->dir = PJMEDIA_DIR_DECODING;
330
331 } else {
332
333 /* Send and receive stream. */
334
335 si->dir = PJMEDIA_DIR_ENCODING_DECODING;
336
337 }
338
339 /* No need to do anything else if stream is rejected */
340 if (local_m->desc.port == 0) {
341 return PJ_SUCCESS;
342 }
343
344 /* If "rtcp" attribute is present in the SDP, set the RTCP address
345 * from that attribute. Otherwise, calculate from RTP address.
346 */
347 attr = pjmedia_sdp_attr_find2(rem_m->attr_count, rem_m->attr,
348 "rtcp", NULL);
349 if (attr) {
350 pjmedia_sdp_rtcp_attr rtcp;
351 status = pjmedia_sdp_attr_get_rtcp(attr, &rtcp);
352 if (status == PJ_SUCCESS) {
353 if (rtcp.addr.slen) {
354 status = pj_sockaddr_init(rem_af, &si->rem_rtcp, &rtcp.addr,
355 (pj_uint16_t)rtcp.port);
356 } else {
357 pj_sockaddr_init(rem_af, &si->rem_rtcp, NULL,
358 (pj_uint16_t)rtcp.port);
359 pj_memcpy(pj_sockaddr_get_addr(&si->rem_rtcp),
360 pj_sockaddr_get_addr(&si->rem_addr),
361 pj_sockaddr_get_addr_len(&si->rem_addr));
362 }
363 }
364 }
365
366 if (!pj_sockaddr_has_addr(&si->rem_rtcp)) {
367 int rtcp_port;
368
369 pj_memcpy(&si->rem_rtcp, &si->rem_addr, sizeof(pj_sockaddr));
370 rtcp_port = pj_sockaddr_get_port(&si->rem_addr) + 1;
371 pj_sockaddr_set_port(&si->rem_rtcp, (pj_uint16_t)rtcp_port);
372 }
373
374 /* Get codec info and param */
375 status = get_video_codec_info_param(si, pool, NULL, local_m, rem_m);
376
377 /* Leave SSRC to random. */
378 si->ssrc = pj_rand();
379
380 /* Set default jitter buffer parameter. */
381 si->jb_init = si->jb_max = si->jb_min_pre = si->jb_max_pre = -1;
382
383 return status;
384}
385
386#endif /* PJMEDIA_HAS_VIDEO */