1
/* $Id: vid_stream_info.c 4257 2012-09-17 03:11:44Z ming $ */
3
* Copyright (C) 2011 Teluu Inc. (http://www.teluu.com)
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.
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.
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
19
#include <pjmedia/vid_stream.h>
20
#include <pjmedia/sdp_neg.h>
21
#include <pjmedia/stream_common.h>
25
#if defined(PJMEDIA_HAS_VIDEO) && (PJMEDIA_HAS_VIDEO != 0)
27
static const pj_str_t ID_VIDEO = { "video", 5};
28
static const pj_str_t ID_IN = { "IN", 2 };
29
static const pj_str_t ID_IP4 = { "IP4", 3};
30
static const pj_str_t ID_IP6 = { "IP6", 3};
31
static const pj_str_t ID_RTP_AVP = { "RTP/AVP", 7 };
32
static const pj_str_t ID_RTP_SAVP = { "RTP/SAVP", 8 };
33
//static const pj_str_t ID_SDP_NAME = { "pjmedia", 7 };
34
static const pj_str_t ID_RTPMAP = { "rtpmap", 6 };
36
static const pj_str_t STR_INACTIVE = { "inactive", 8 };
37
static const pj_str_t STR_SENDRECV = { "sendrecv", 8 };
38
static const pj_str_t STR_SENDONLY = { "sendonly", 8 };
39
static const pj_str_t STR_RECVONLY = { "recvonly", 8 };
43
* Internal function for collecting codec info and param from the SDP media.
45
static pj_status_t get_video_codec_info_param(pjmedia_vid_stream_info *si,
47
pjmedia_vid_codec_mgr *mgr,
48
const pjmedia_sdp_media *local_m,
49
const pjmedia_sdp_media *rem_m)
52
const pjmedia_vid_codec_info *p_info;
55
pt = pj_strtoul(&local_m->desc.fmt[0]);
57
/* Get payload type for receiving direction */
60
/* Get codec info and payload type for transmitting direction. */
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)
67
si->codec_info = *p_info;
69
/* Get payload type for transmitting direction.
70
* For static payload type, pt's are symetric.
74
const pjmedia_sdp_attr *attr;
75
pjmedia_sdp_rtpmap *rtpmap;
76
pjmedia_codec_id codec_id;
80
/* Determine payload type for outgoing channel, by finding
81
* dynamic payload type in remote SDP that matches the answer.
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) ==
90
/* Found matched codec. */
91
si->tx_pt = pj_strtoul(&rem_m->desc.fmt[i]);
96
if (si->tx_pt == 0xFFFF)
97
return PJMEDIA_EMISSINGRTPMAP;
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]);
103
return PJMEDIA_EMISSINGRTPMAP;
105
status = pjmedia_sdp_attr_to_rtpmap(pool, attr, &rtpmap);
106
if (status != PJ_SUCCESS)
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);
114
status = pjmedia_vid_codec_mgr_find_codecs_by_id(mgr, &codec_id_st,
116
if (status != PJ_SUCCESS)
119
si->codec_info = *p_info;
123
/* Request for codec with the correct packing for streaming */
124
si->codec_info.packings = PJMEDIA_VID_PACKING_PACKETS;
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,
132
/* Adjust encoding bitrate, if higher than remote preference. The remote
133
* bitrate preference is read from SDP "b=TIAS" line in media level.
135
if ((si->dir & PJMEDIA_DIR_ENCODING) && rem_m->bandw_count) {
136
unsigned i, bandw = 0;
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))
143
bandw = rem_m->bandw[i]->value;
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;
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);
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);
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 #:
172
* Thanks Alain Totouom
174
if (status != PJ_SUCCESS && si->dir != PJMEDIA_DIR_NONE)
183
* Create stream info from SDP media line.
185
PJ_DEF(pj_status_t) pjmedia_vid_stream_info_from_sdp(
186
pjmedia_vid_stream_info *si,
188
pjmedia_endpt *endpt,
189
const pjmedia_sdp_session *local,
190
const pjmedia_sdp_session *remote,
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;
202
PJ_UNUSED_ARG(endpt);
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);
209
/* Keep SDP shortcuts */
210
local_m = local->media[stream_idx];
211
rem_m = remote->media[stream_idx];
213
local_conn = local_m->conn ? local_m->conn : local->conn;
214
if (local_conn == NULL)
215
return PJMEDIA_SDP_EMISSINGCONN;
217
rem_conn = rem_m->conn ? rem_m->conn : remote->conn;
218
if (rem_conn == NULL)
219
return PJMEDIA_SDP_EMISSINGCONN;
221
/* Media type must be video */
222
if (pj_stricmp(&local_m->desc.media, &ID_VIDEO) != 0)
223
return PJMEDIA_EINVALIMEDIATYPE;
228
pj_bzero(si, sizeof(*si));
231
si->type = PJMEDIA_TYPE_VIDEO;
233
/* Transport protocol */
235
/* At this point, transport type must be compatible,
236
* the transport instance will do more validation later.
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;
243
if (pj_stricmp(&local_m->desc.transport, &ID_RTP_AVP) == 0) {
245
si->proto = PJMEDIA_TP_PROTO_RTP_AVP;
247
} else if (pj_stricmp(&local_m->desc.transport, &ID_RTP_SAVP) == 0) {
249
si->proto = PJMEDIA_TP_PROTO_RTP_SAVP;
253
si->proto = PJMEDIA_TP_PROTO_UNKNOWN;
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();
268
if (rem_af==pj_AF_UNSPEC()) {
269
/* Unsupported address family */
273
/* Set remote address: */
274
status = pj_sockaddr_init(rem_af, &si->rem_addr, &rem_conn->addr,
276
if (status != PJ_SUCCESS) {
277
/* Invalid IP address. */
278
return PJMEDIA_EINVALIDIP;
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();
291
if (local_af==pj_AF_UNSPEC()) {
292
/* Unsupported address family */
296
/* Set remote address: */
297
status = pj_sockaddr_init(local_af, &local_addr, &local_conn->addr,
299
if (status != PJ_SUCCESS) {
300
/* Invalid IP address. */
301
return PJMEDIA_EINVALIDIP;
304
/* Local and remote address family must match */
305
if (local_af != rem_af)
308
/* Media direction: */
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)
315
/* Inactive stream. */
317
si->dir = PJMEDIA_DIR_NONE;
319
} else if (pjmedia_sdp_media_find_attr(local_m, &STR_SENDONLY, NULL)!=NULL) {
321
/* Send only stream. */
323
si->dir = PJMEDIA_DIR_ENCODING;
325
} else if (pjmedia_sdp_media_find_attr(local_m, &STR_RECVONLY, NULL)!=NULL) {
327
/* Recv only stream. */
329
si->dir = PJMEDIA_DIR_DECODING;
333
/* Send and receive stream. */
335
si->dir = PJMEDIA_DIR_ENCODING_DECODING;
339
/* No need to do anything else if stream is rejected */
340
if (local_m->desc.port == 0) {
344
/* If "rtcp" attribute is present in the SDP, set the RTCP address
345
* from that attribute. Otherwise, calculate from RTP address.
347
attr = pjmedia_sdp_attr_find2(rem_m->attr_count, rem_m->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);
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));
366
if (!pj_sockaddr_has_addr(&si->rem_rtcp)) {
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);
374
/* Get codec info and param */
375
status = get_video_codec_info_param(si, pool, NULL, local_m, rem_m);
377
/* Leave SSRC to random. */
378
si->ssrc = pj_rand();
380
/* Set default jitter buffer parameter. */
381
si->jb_init = si->jb_max = si->jb_min_pre = si->jb_max_pre = -1;
386
#endif /* PJMEDIA_HAS_VIDEO */