soa_static.c 30.3 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
1 2 3
/*
 * This file is part of the Sofia-SIP package
 *
4
 * Copyright (C) 2006 Nokia Corporation.
Pekka Pessi's avatar
Pekka Pessi committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

/**@CFILE soa_static.c
 *
 * @brief Static implementation of Sofia SDP Offer/Answer Engine
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 *
 * @date Created: Tue Aug 16 17:06:06 EEST 2005
32 33 34 35 36 37 38 39 40 41 42 43 44 45
 *
 * @par Use-cases
 *  1. no existing session
 *    a) generating offer (upgrade with user-SDP)
 *    b) generating answer (upgrade with remote-SDP, rejects with user-SDP)
 *  2. session exists
 *    a) generating offer: 
 *       upgrades with user-SDP
 *    b) generating answer: 
 *       upgrades with remote-SDP, rejects with user-SDP
 *    c) processing answer: 
 *       rejects with user-SDP, no upgrades
 *
 * Upgrading session with user SDP:
Pekka Pessi's avatar
Pekka Pessi committed
46 47 48 49 50 51 52 53 54 55
 */

#include "config.h"

#include <stddef.h>
#include <stdlib.h>
#include <string.h>

#include <assert.h>

Pekka Pessi's avatar
Pekka Pessi committed
56 57 58 59
struct soa_static_complete;

#define SU_MSG_ARG_T struct soa_static_completed

60 61 62 63 64
#include <sofia-sip/su_wait.h>
#include <sofia-sip/su_tag_class.h>
#include <sofia-sip/su_tag_class.h>
#include <sofia-sip/su_tagarg.h>
#include <sofia-sip/su_strlst.h>
65 66
#include <sofia-sip/string0.h>
#include <sofia-sip/bnf.h>
Pekka Pessi's avatar
Pekka Pessi committed
67

68 69 70
#include "sofia-sip/soa.h"
#include <sofia-sip/sdp.h>
#include "sofia-sip/soa_session.h"
Pekka Pessi's avatar
Pekka Pessi committed
71 72 73 74

#define NONE ((void *)-1)
#define XXX assert(!"implemented")

75 76 77
#if !HAVE_STRCASESTR
char *strcasestr(const char *haystack, const char *needle);
#endif
78

Pekka Pessi's avatar
Pekka Pessi committed
79
typedef struct soa_static_session
Pekka Pessi's avatar
Pekka Pessi committed
80
{
Pekka Pessi's avatar
Pekka Pessi committed
81
  soa_session_t sss_session[1];
82
  char *sss_audio_aux;
Pekka Pessi's avatar
Pekka Pessi committed
83 84
}
soa_static_session_t;
Pekka Pessi's avatar
Pekka Pessi committed
85 86 87

static int soa_static_init(char const *, soa_session_t *, soa_session_t *);
static void soa_static_deinit(soa_session_t *);
88 89
static int soa_static_set_params(soa_session_t *ss, tagi_t const *tags);
static int soa_static_get_params(soa_session_t const *ss, tagi_t *tags);
90 91 92
static tagi_t *soa_static_get_paramlist(soa_session_t const *ss,
					tag_type_t tag, tag_value_t value, 
					...);
93 94
static int soa_static_set_capability_sdp(soa_session_t *ss, 
				       sdp_session_t *sdp,
95
				       char const *, isize_t);
96 97 98
static int soa_static_set_remote_sdp(soa_session_t *ss, 
				   int new_version,
				   sdp_session_t *sdp,
99
				   char const *, isize_t);
100 101
static int soa_static_set_user_sdp(soa_session_t *ss, 
				   sdp_session_t *sdp,
102
				   char const *, isize_t);
103 104 105 106 107
static int soa_static_generate_offer(soa_session_t *ss, soa_callback_f *);
static int soa_static_generate_answer(soa_session_t *ss, soa_callback_f *);
static int soa_static_process_answer(soa_session_t *ss, soa_callback_f *);
static int soa_static_process_reject(soa_session_t *ss, soa_callback_f *);

108 109
static int soa_static_activate(soa_session_t *ss, char const *option);
static int soa_static_deactivate(soa_session_t *ss, char const *option);
Pekka Pessi's avatar
Pekka Pessi committed
110
static void soa_static_terminate(soa_session_t *ss, char const *option);
Pekka Pessi's avatar
Pekka Pessi committed
111

Pekka Pessi's avatar
Pekka Pessi committed
112 113
struct soa_session_actions const soa_default_actions = 
  {
114
    (sizeof soa_default_actions),
Pekka Pessi's avatar
Pekka Pessi committed
115
    sizeof (struct soa_static_session),
Pekka Pessi's avatar
Pekka Pessi committed
116
    "static",
Pekka Pessi's avatar
Pekka Pessi committed
117 118 119 120 121 122
    soa_static_init,
    soa_static_deinit,
    soa_static_set_params,
    soa_static_get_params,
    soa_static_get_paramlist,
    soa_base_media_features,
123 124
    soa_base_sip_require,
    soa_base_sip_supported,
Pekka Pessi's avatar
Pekka Pessi committed
125
    soa_base_remote_sip_features,
126 127
    soa_static_set_capability_sdp,
    soa_static_set_remote_sdp,
128
    soa_static_set_user_sdp,
Pekka Pessi's avatar
Pekka Pessi committed
129 130 131
    soa_static_generate_offer,
    soa_static_generate_answer,
    soa_static_process_answer,
132
    soa_static_process_reject,
Pekka Pessi's avatar
Pekka Pessi committed
133
    soa_static_activate,
134
    soa_static_deactivate,
Pekka Pessi's avatar
Pekka Pessi committed
135 136 137
    soa_static_terminate
  };

Pekka Pessi's avatar
Pekka Pessi committed
138
/* Initialize session */
Pekka Pessi's avatar
Pekka Pessi committed
139 140
static int soa_static_init(char const *name,
			   soa_session_t *ss,
Pekka Pessi's avatar
Pekka Pessi committed
141 142
			   soa_session_t *parent)
{
Pekka Pessi's avatar
Pekka Pessi committed
143
  return soa_base_init(name, ss, parent);
Pekka Pessi's avatar
Pekka Pessi committed
144 145 146 147
}

static void soa_static_deinit(soa_session_t *ss)
{
Pekka Pessi's avatar
Pekka Pessi committed
148
  soa_base_deinit(ss);
Pekka Pessi's avatar
Pekka Pessi committed
149 150
}

151
static int soa_static_set_params(soa_session_t *ss, tagi_t const *tags)
Pekka Pessi's avatar
Pekka Pessi committed
152
{
153 154
  soa_static_session_t *sss = (soa_static_session_t *)ss;
  char const *audio_aux = sss->sss_audio_aux;
155
  int n, m;
156 157 158 159 160

  n = tl_gets(tags,
	      SOATAG_AUDIO_AUX_REF(audio_aux),
	      TAG_END());

161
  if (n > 0 && str0casecmp(audio_aux, sss->sss_audio_aux)) {
162 163 164 165 166 167 168 169 170 171 172 173 174
    char *s = su_strdup(ss->ss_home, audio_aux), *tbf = sss->sss_audio_aux;
    if (s == NULL && audio_aux != NULL)
      return -1;
    sss->sss_audio_aux = s;
    if (tbf)
      su_free(ss->ss_home, tbf);
  }

  m = soa_base_set_params(ss, tags);
  if (m < 0)
    return m;

  return n + m;
Pekka Pessi's avatar
Pekka Pessi committed
175 176
}

177
static int soa_static_get_params(soa_session_t const *ss, tagi_t *tags)
Pekka Pessi's avatar
Pekka Pessi committed
178
{
179 180
  soa_static_session_t *sss = (soa_static_session_t *)ss;

181
  int n, m;
182 183 184 185 186 187 188 189 190

  n = tl_tgets(tags,
	       SOATAG_AUDIO_AUX(sss->sss_audio_aux),
	       TAG_END());
  m = soa_base_get_params(ss, tags);
  if (m < 0)
    return m;

  return n + m;
Pekka Pessi's avatar
Pekka Pessi committed
191 192
}

193 194 195
static tagi_t *soa_static_get_paramlist(soa_session_t const *ss,
					tag_type_t tag, tag_value_t value, 
					...)
Pekka Pessi's avatar
Pekka Pessi committed
196
{
197 198
  soa_static_session_t *sss = (soa_static_session_t *)ss;

199 200 201 202 203
  ta_list ta;
  tagi_t *tl;

  ta_start(ta, tag, value);

204 205 206 207
  tl = soa_base_get_paramlist(ss,
			      TAG_IF(sss->sss_audio_aux,
				     SOATAG_AUDIO_AUX(sss->sss_audio_aux)),
			      TAG_NEXT(ta_args(ta)));
208 209 210 211

  ta_end(ta);

  return tl;
Pekka Pessi's avatar
Pekka Pessi committed
212 213
}

214 215 216
static int soa_static_set_capability_sdp(soa_session_t *ss, 
					 sdp_session_t *sdp,
					 char const *sdp_str, 
217
					 isize_t sdp_len)
218 219 220 221 222 223 224 225 226
{
  return soa_base_set_capability_sdp(ss, sdp, sdp_str, sdp_len);
}


static int soa_static_set_remote_sdp(soa_session_t *ss, 
				     int new_version,
				     sdp_session_t *sdp,
				     char const *sdp_str, 
227
				     isize_t sdp_len)
228 229 230 231 232
{
  return soa_base_set_remote_sdp(ss, new_version, sdp, sdp_str, sdp_len);
}


233 234 235
static int soa_static_set_user_sdp(soa_session_t *ss, 
				   sdp_session_t *sdp,
				   char const *sdp_str, 
236
				   isize_t sdp_len)
237
{
238
  return soa_base_set_user_sdp(ss, sdp, sdp_str, sdp_len);
239 240
}

241 242 243
/** Generate a rejected m= line */
sdp_media_t *soa_sdp_make_rejected_media(su_home_t *home, 
					 sdp_media_t const *m,
244 245
					 sdp_session_t *sdp,
					 int include_all_codecs)
Pekka Pessi's avatar
Pekka Pessi committed
246
{
247 248 249 250 251 252 253
  sdp_media_t rejected[1] = {{ sizeof (rejected) }};

  rejected->m_type = m->m_type;
  rejected->m_type_name = m->m_type_name;
  rejected->m_port = 0;
  rejected->m_proto = m->m_proto;
  rejected->m_proto_name = m->m_proto_name;
254 255 256

  if (include_all_codecs) {
    rejected->m_rtpmaps = m->m_rtpmaps;
257
  }
258

259
  rejected->m_rejected = 1;
260

261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
  return sdp_media_dup(home, rejected, sdp);
}

/** Expand a @a truncated SDP.
 */
sdp_session_t *soa_sdp_expand_media(su_home_t *home,
				    sdp_session_t const *truncated,
				    sdp_session_t const *complete)
{
  sdp_session_t *expanded;
  sdp_media_t **m0;
  sdp_media_t * const *m1;

  expanded = sdp_session_dup(home, truncated);

  if (expanded) {
    for (m0 = &expanded->sdp_media, m1 = &complete->sdp_media;
	 *m1;
	 m1 = &(*m1)->m_next) {
      if (!*m0) {
281
	*m0 = soa_sdp_make_rejected_media(home, *m1, expanded, 0);
282 283 284 285 286 287 288 289 290 291
	if (!*m0)
	  return NULL;
      }
      m0 = &(*m0)->m_next;
    }
  }

  return expanded;
}

292
/** Check if @a session should be upgraded with @a remote */ 
293
int soa_sdp_upgrade_is_needed(sdp_session_t const *session,
294
			      sdp_session_t const *remote)
295
{
296
  sdp_media_t const *rm, *lm;
297

298
  if (!remote)
299 300 301 302
    return 0;
  if (!session)
    return 1;

303 304 305 306 307 308
  for (rm = remote->sdp_media, lm = session->sdp_media; 
       rm && lm ; rm = rm->m_next, lm = lm->m_next) {
    if (rm->m_rejected)
      continue;
    if (lm->m_rejected)
      break;
309 310
  }

311
  return rm != NULL;
312 313
}

314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
/** Check if codec is in auxiliary list */
int soa_sdp_is_auxiliary_codec(sdp_rtpmap_t const *rm, char const *auxiliary)
{
  char const *codec;
  size_t clen, alen;
  char const *match;

  if (!rm || !rm->rm_encoding || !auxiliary)
    return 0;

  codec = rm->rm_encoding;

  clen = strlen(codec), alen = strlen(auxiliary);

  if (clen > alen)
    return 0;

  for (match = auxiliary;
       (match = strcasestr(match, codec));
       match = match + 1) {
    if (IS_ALPHANUM(match[clen]) || match[clen] == '-')
      continue;
    if (match != auxiliary &&
	(IS_ALPHANUM(match[-1]) || match[-1] == '-'))
      continue;
    return 1;
  }

  return 0;
}

345 346

/** Find first matching media in table. */
347 348 349 350
sdp_media_t *soa_sdp_matching(soa_session_t *ss, 
			      sdp_media_t *mm[],
			      sdp_media_t const *with,
			      int *return_common_codecs)
351
{
352
  int i, j = -1;
Pekka Pessi's avatar
Pekka Pessi committed
353
  sdp_media_t *m;
354
  sdp_rtpmap_t const *rm;
355 356 357 358 359 360 361 362
  soa_static_session_t *sss = (soa_static_session_t *)ss;
  char const *auxiliary;

  auxiliary = with->m_type == sdp_media_audio ? sss->sss_audio_aux : NULL;

  /* Looking for a single codec */
  if (with->m_rtpmaps && with->m_rtpmaps->rm_next == NULL)
    auxiliary = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
363

364
  for (i = 0; mm[i]; i++) {
365 366 367 368 369 370 371 372 373 374 375
    if (!sdp_media_match_with(mm[i], with))
      continue;
    
    if (!sdp_media_uses_rtp(with))
      break;

    if (!return_common_codecs)
      break;

    /* Check also rtpmaps  */
    for (rm = mm[i]->m_rtpmaps; rm; rm = rm->rm_next) {
376 377 378 379
      /* Ignore auxiliary codecs */
      if (auxiliary && soa_sdp_is_auxiliary_codec(rm, auxiliary))
	continue;

380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
      if (sdp_rtpmap_find_matching(with->m_rtpmaps, rm))
	break;
    }
    if (rm)
      break;
    if (j == -1)
      j = i;
  }

  if (return_common_codecs)
    *return_common_codecs = mm[i] != NULL;

  if (mm[i] == NULL && j != -1)
    i = j;			/* return m= line without common codecs */

  m = mm[i];

  for (; mm[i]; i++)
    mm[i] = mm[i + 1];

  return m;
}

/** Set payload types in @a l_m according to the values in @a r_m.
 * 
 * @retval number of common codecs
 */
int soa_sdp_set_rtpmap_pt(sdp_media_t *l_m, 
			  sdp_media_t const *r_m)
{
  sdp_rtpmap_t *lrm, **next_lrm;
  sdp_rtpmap_t const *rrm;

  int local_codecs = 0, common_codecs = 0;

  unsigned char dynamic_pt[128];
  unsigned pt;

  for (next_lrm = &l_m->m_rtpmaps; (lrm = *next_lrm); ) {
    if (lrm->rm_any) {
      /* Remove codecs known only by pt number */
      *next_lrm = lrm->rm_next;
      continue;
    }
    else {
      next_lrm = &lrm->rm_next;
    }

    local_codecs++;

    rrm = sdp_rtpmap_find_matching(r_m->m_rtpmaps, lrm);

    /* XXX - do fmtp comparison */

    if (rrm) {
      /* Use same payload type as remote */
      if (lrm->rm_pt != rrm->rm_pt) {
	lrm->rm_predef = 0;
	lrm->rm_pt = rrm->rm_pt;
      }
      common_codecs++;
    }
    else {
      /* Determine payload type later */
      lrm->rm_any = 1;
    }
  }
  
  if (local_codecs == common_codecs)
    return common_codecs;

  /* Select unique dynamic payload type for each payload */

  memset(dynamic_pt, 0, sizeof dynamic_pt);

  for (lrm = l_m->m_rtpmaps; lrm; lrm = lrm->rm_next) {
    if (!lrm->rm_any)
      dynamic_pt[lrm->rm_pt] = 1;
  }

  for (rrm = r_m->m_rtpmaps; rrm; rrm = rrm->rm_next) {
    dynamic_pt[rrm->rm_pt] = 1;
  }

  for (next_lrm = &l_m->m_rtpmaps; (lrm = *next_lrm); ) {
    if (!lrm->rm_any) {
      next_lrm = &lrm->rm_next;
      continue;
    }
    
    lrm->rm_any = 0;

    pt = lrm->rm_pt;

    if (dynamic_pt[pt]) {
      for (pt = 96; pt < 128; pt++)
        if (!dynamic_pt[pt])
          break;
      
      if (pt == 128) {
        for (pt = 0; pt < 128; pt++)
          if (!sdp_rtpmap_well_known[pt] && !dynamic_pt[pt])
            break;
      }

      if (pt == 128)  {
        for (pt = 0; pt < 128; pt++)
          if (!dynamic_pt[pt])
            break;
      }

      if (pt == 128) {
        /* Too many payload types */
        *next_lrm = lrm->rm_next;
        continue;
      }

      lrm->rm_pt = pt;
      lrm->rm_predef = 0;
    }

    dynamic_pt[pt] = 1;
  
    next_lrm = &lrm->rm_next;
  }

  return common_codecs;
}


510
/** Sort rtpmaps in @a inout_list according to the values in @a rrm.
511 512 513 514
 *
 * @return Number of common codecs
 */
int soa_sdp_sort_rtpmap(sdp_rtpmap_t **inout_list, 
515 516
			sdp_rtpmap_t const *rrm,
			char const *auxiliary)
517 518
{
  sdp_rtpmap_t *sorted = NULL, **next = &sorted, **left;
519
  sdp_rtpmap_t *aux = NULL, **next_aux = &aux;
520 521 522 523 524 525 526

  int common_codecs = 0;

  assert(inout_list);
  if (!inout_list)
    return 0;

527 528 529 530
  /* If remote has only single codec, ignore list of auxiliary codecs */
  if (rrm && !rrm->rm_next)
    auxiliary = NULL;

531 532 533 534 535
  /* Insertion sort from *inout_list to sorted */
  for (; rrm && *inout_list; rrm = rrm->rm_next) {
    for (left = inout_list; *left; left = &(*left)->rm_next) {
      if (sdp_rtpmap_match(rrm, (*left)))
	break;
536
    }
537 538
    if (!*left)
      continue;
539 540 541 542 543 544 545 546

    if (auxiliary && soa_sdp_is_auxiliary_codec(rrm, auxiliary)) {
      *next_aux = *left, next_aux = &(*next_aux)->rm_next;
    }
    else {
      common_codecs++;
      *next = *left; next = &(*next)->rm_next;
    }
547
    *left = (*left)->rm_next;
548
  }
549

550 551 552 553
  /* Append common auxiliary codecs */
  if (aux)
    *next = aux, next = next_aux;

554 555 556 557 558 559
  /* Append leftover codecs */
  *next = *inout_list;

  *inout_list = sorted;

  return common_codecs;
560 561
}

562

563
/** Select rtpmaps in @a inout_list according to the values in @a rrm.
564 565 566 567
 *
 * @return Number of common codecs
 */
int soa_sdp_select_rtpmap(sdp_rtpmap_t **inout_list, 
568 569 570
			  sdp_rtpmap_t const *rrm,
			  char const *auxiliary,
			  int select_single)
571 572
{
  sdp_rtpmap_t **left;
573
  sdp_rtpmap_t *aux = NULL, **next_aux = &aux;
574 575 576 577 578 579 580 581

  int common_codecs = 0;

  assert(inout_list);
  if (!inout_list)
    return 0;

  for (left = inout_list; *left; ) {
582 583 584 585 586 587
    if (auxiliary && soa_sdp_is_auxiliary_codec(*left, auxiliary))
      /* Insert into list of auxiliary codecs */
      *next_aux = *left, *left = (*left)->rm_next, 
	next_aux = &(*next_aux)->rm_next;
    else if (!(select_single && common_codecs > 0)
	     && sdp_rtpmap_find_matching(rrm, (*left)))
588 589 590 591 592 593 594
      /* Select */
      left = &(*left)->rm_next, common_codecs++;
    else
      /* Remove */
      *left = (*left)->rm_next;
  }

595 596
  *left = aux, *next_aux = NULL;

597 598 599 600 601 602 603 604
  return common_codecs;
}

/** Sort and select rtpmaps within session */ 
int soa_sdp_upgrade_rtpmaps(soa_session_t *ss,
			    sdp_session_t *session,
			    sdp_session_t const *remote)
{
605
  soa_static_session_t *sss = (soa_static_session_t *)ss;
606 607 608 609 610 611 612 613 614 615 616
  sdp_media_t *sm;
  sdp_media_t const *rm;

  for (sm = session->sdp_media, rm = remote->sdp_media; 
       sm && rm; 
       sm = sm->m_next, rm = rm->m_next) {
    if (sm->m_rejected)
      continue;
    if (sdp_media_uses_rtp(sm)) {
      int common_codecs = soa_sdp_set_rtpmap_pt(sm, rm);

617 618 619
      char const *auxiliary =
	rm->m_type == sdp_media_audio ? sss->sss_audio_aux : NULL;

620 621 622
      if (ss->ss_rtp_sort == SOA_RTP_SORT_REMOTE || 
	  (ss->ss_rtp_sort == SOA_RTP_SORT_DEFAULT &&
	   rm->m_mode == sdp_recvonly)) {
623
	soa_sdp_sort_rtpmap(&sm->m_rtpmaps, rm->m_rtpmaps, auxiliary);
624 625 626 627 628
      }

      if (common_codecs == 0)
	;
      else if (ss->ss_rtp_select == SOA_RTP_SELECT_SINGLE) {
629
	soa_sdp_select_rtpmap(&sm->m_rtpmaps, rm->m_rtpmaps, auxiliary, 1);
630 631
      }
      else if (ss->ss_rtp_select == SOA_RTP_SELECT_COMMON) {
632
	soa_sdp_select_rtpmap(&sm->m_rtpmaps, rm->m_rtpmaps, auxiliary, 0);
633 634 635 636 637 638 639 640
      }
    }
  }

  return 0;
}


641
/** Upgrade m= lines within session */ 
642 643
int soa_sdp_upgrade(soa_session_t *ss,
		    su_home_t *home,
644 645 646 647
		    sdp_session_t *session,
		    sdp_session_t const *caps,
		    sdp_session_t const *upgrader)
{
648 649
  soa_static_session_t *sss = (soa_static_session_t *)ss;

650
  int Ns, Nc, Nu, size, i, j;
651 652
  sdp_media_t *m, **mm, *cm;
  sdp_media_t **s_media, **o_media, **c_media;
653 654 655 656 657 658 659 660 661 662 663 664 665 666
  sdp_media_t const **u_media;

  Ns = sdp_media_count(session, sdp_media_any, 0, 0, 0);
  Nc = sdp_media_count(caps, sdp_media_any, 0, 0, 0);
  Nu = sdp_media_count(upgrader, sdp_media_any, 0, 0, 0);

  if (caps == upgrader)
    size = Ns + Nc + 1;
  else if (Ns < Nu)
    size = Nu + 1;
  else
    size = Ns + 1;

  s_media = su_zalloc(home, size * (sizeof *s_media));
667
  o_media = su_zalloc(home, (Ns + 1) * (sizeof *o_media));
668 669 670
  c_media = su_zalloc(home, (Nc + 1) * (sizeof *c_media));
  u_media = su_zalloc(home, (Nu + 1) * (sizeof *u_media));

671 672 673
  cm = sdp_media_dup_all(home, caps->sdp_media, session); 

  if (!s_media || !c_media || !u_media || !cm)
674 675 676
    return -1;

  for (i = 0, m = session->sdp_media; m && i < Ns; m = m->m_next)
677
    o_media[i++] = m;
678
  assert(i == Ns);
679
  for (i = 0, m = cm; m && i < Nc; m = m->m_next)
680 681 682 683 684 685 686 687 688
    c_media[i++] = m;
  assert(i == Nc);
  for (i = 0, m = upgrader->sdp_media; m && i < Nu; m = m->m_next)
    u_media[i++] = m;
  assert(i == Nu);

  if (caps != upgrader) {
    /* Update session according to remote */
    for (i = 0; i < Nu; i++) {
689 690 691 692 693 694 695 696 697
      int common_codecs = 0;

      m = soa_sdp_matching(ss, c_media, u_media[i], &common_codecs);

      if (!m || u_media[i]->m_rejected) {
	m = soa_sdp_make_rejected_media(home, u_media[i], session, 0);
      }
      else if (sdp_media_uses_rtp(m)) {
	/* Process rtpmaps */
698 699 700
	char const *auxiliary =
	  m->m_type == sdp_media_audio ? sss->sss_audio_aux : NULL;

701 702 703 704 705 706 707
	if (!common_codecs && !ss->ss_rtp_mismatch)
	  m = soa_sdp_make_rejected_media(home, m, session, 1);
	soa_sdp_set_rtpmap_pt(m, u_media[i]);

	if (ss->ss_rtp_sort == SOA_RTP_SORT_REMOTE || 
	    (ss->ss_rtp_sort == SOA_RTP_SORT_DEFAULT &&
	     u_media[i]->m_mode == sdp_recvonly)) {
708
	  soa_sdp_sort_rtpmap(&m->m_rtpmaps, u_media[i]->m_rtpmaps, auxiliary);
709 710 711 712 713
	}

	if (common_codecs &&
	    (ss->ss_rtp_select == SOA_RTP_SELECT_SINGLE ||
	     ss->ss_rtp_select == SOA_RTP_SELECT_COMMON)) {
714 715
	  soa_sdp_select_rtpmap(&m->m_rtpmaps, u_media[i]->m_rtpmaps, auxiliary,
				ss->ss_rtp_select == SOA_RTP_SELECT_SINGLE);
716 717 718
	}
      }

719 720 721 722 723 724
      s_media[i] = m;
    }
  }
  else {
    /* Update session according to local */
    for (i = 0; i < Ns; i++) {
725
      m = soa_sdp_matching(ss, c_media, o_media[i], NULL);
726
      if (!m)
727
	m = soa_sdp_make_rejected_media(home, o_media[i], session, 0);
728 729 730 731 732 733 734 735 736 737
      s_media[i] = m;
    }
    /* Here we just append new media at the end */
    for (j = 0; c_media[j]; j++)
      s_media[i++] = c_media[j];
    assert(i <= size);
  }

  mm = &session->sdp_media;
  for (i = 0; s_media[i]; i++) {
738
    m = s_media[i]; *mm = m; mm = &m->m_next;
739
  }
740 741
  *mm = NULL;

742 743 744
  return 0;
}

745 746 747
/** Check if @a session contains media that are rejected by @a remote. */ 
int soa_sdp_reject_is_needed(sdp_session_t const *session,
			     sdp_session_t const *remote)
748 749 750 751 752 753 754 755 756 757
{
  sdp_media_t const *sm, *rm;

  if (!remote)
    return 1;
  if (!session)
    return 0;

  for (sm = session->sdp_media, rm = remote->sdp_media; 
       sm && rm; sm = sm->m_next, rm = rm->m_next) {
758 759 760 761 762
    if (rm->m_rejected) {
      if (!sm->m_rejected)
	return 1;
    }
    else {
763
      /* Mode bits do not match */
764 765
      if (((rm->m_mode & sdp_recvonly) ? sdp_sendonly : 0)
	  != (sdp_mode_t)(sm->m_mode & sdp_sendonly))
766 767
	return 1;
    }
768 769 770 771 772 773 774 775
  }

  if (sm)
    return 1;

  return 0;
}

776
/** If m= line is rejected by, remote mark m= line rejected within session */ 
777 778 779 780 781 782 783
int soa_sdp_reject(su_home_t *home,
		   sdp_session_t *session,
		   sdp_session_t const *remote)
{
  sdp_media_t *sm;
  sdp_media_t const *rm;

784
  if (!session || !session->sdp_media || !remote)
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
    return 0;

  rm = remote->sdp_media;

  for (sm = session->sdp_media; sm; sm = sm->m_next) {
    if (!rm || rm->m_rejected) {
      sm->m_rejected = 1;
      sm->m_mode = 0;
      sm->m_port = 0;
      sm->m_number_of_ports = 1;
      if (sm->m_format)
	sm->m_format->l_next = NULL;
      if (sm->m_rtpmaps)
	sm->m_rtpmaps->rm_next = NULL;
      sm->m_information = NULL;
      if (sm->m_connections)
	sm->m_connections->c_next = NULL;
      sm->m_bandwidths = NULL;
      sm->m_key = NULL;
      sm->m_attributes = NULL;
      sm->m_user = NULL;
    }
807

808 809 810 811 812 813 814
    if (rm)
      rm = rm->m_next;
  }

  return 0;
}

815 816 817 818 819 820 821
/** Check if @a session mode should be changed. */ 
int soa_sdp_mode_set_is_needed(sdp_session_t const *session,
			       sdp_session_t const *remote,
			       char const *hold)
{
  sdp_media_t const *sm, *rm, *rm_next;
  int hold_all;
822
  sdp_mode_t recv_mode;
823

Pekka Pessi's avatar
Pekka Pessi committed
824 825 826
  SU_DEBUG_7(("soa_sdp_mode_set_is_needed(%p, %p, \"%s\"): called\n",
	      session, remote, hold ? hold : ""));

827 828 829 830 831 832 833 834 835 836 837 838 839 840
  if (!session )
    return 0;

  hold_all = str0cmp(hold, "*") == 0;

  rm = remote ? remote->sdp_media : NULL, rm_next = NULL;

  for (sm = session->sdp_media; sm; sm = sm->m_next, rm = rm_next) {
    rm_next = rm ? rm->m_next : NULL;

    if (sm->m_rejected)
      continue;

    if (rm) {
841
      /* Mode bits do not match */
842 843
      if (((rm->m_mode & sdp_recvonly) ? sdp_sendonly : 0)
	  != (sdp_mode_t)(sm->m_mode & sdp_sendonly))
844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866
	return 1;
    }

    recv_mode = sm->m_mode & sdp_recvonly;
    if (recv_mode && hold &&
	(hold_all || strcasestr(hold, sm->m_type_name)))
      return 1;
  }

  return 0;
}


/** Update mode within session */ 
int soa_sdp_mode_set(sdp_session_t *session,
		     sdp_session_t const *remote,
		     char const *hold)
{
  sdp_media_t *sm;
  sdp_media_t const *rm, *rm_next;
  int hold_all;
  sdp_mode_t send_mode, recv_mode;

Pekka Pessi's avatar
Pekka Pessi committed
867 868 869
  SU_DEBUG_7(("soa_sdp_mode_set(%p, %p, \"%s\"): called\n",
	      session, remote, hold ? hold : ""));

870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
  if (!session || !session->sdp_media)
    return 0;

  rm = remote ? remote->sdp_media : NULL, rm_next = NULL;

  hold_all = str0cmp(hold, "*") == 0;

  for (sm = session->sdp_media; sm; sm = sm->m_next, rm = rm_next) {
    rm_next = rm ? rm->m_next : NULL;

    if (sm->m_rejected)
      continue;

    send_mode = sdp_sendonly;
    if (rm)
      send_mode = (rm->m_mode & sdp_recvonly) ? sdp_sendonly : 0;

    recv_mode = sm->m_mode & sdp_recvonly;
    if (recv_mode && hold && (hold_all || strcasestr(hold, sm->m_type_name)))
      recv_mode = 0;

    sm->m_mode = recv_mode | send_mode;
  }

  return 0;
}

897 898 899 900 901 902 903 904 905 906 907 908 909 910 911
enum offer_answer_action {
  generate_offer,
  generate_answer,
  process_answer
};

/**
 * Updates the modified copy of local SDP based
 * on application provided local SDP and remote SDP.
 */
static int offer_answer_step(soa_session_t *ss,
			     enum offer_answer_action action,
			     char const *by)
{
  char c_address[64];
912
  sdp_session_t *local = ss->ss_local->ssd_sdp;
913
  sdp_session_t local0[1];
Pekka Pessi's avatar
Pekka Pessi committed
914

915 916
  sdp_session_t *user = ss->ss_user->ssd_sdp;
  unsigned user_version = ss->ss_user_version;
Pekka Pessi's avatar
Pekka Pessi committed
917

918 919 920 921 922 923
  sdp_session_t *remote = ss->ss_remote->ssd_sdp;
  unsigned remote_version = ss->ss_remote_version;

  sdp_origin_t o[1] = {{ sizeof(o) }};
  sdp_connection_t *c, c0[1] = {{ sizeof(c0) }};
  sdp_time_t t[1] = {{ sizeof(t) }};
924

925 926 927 928 929
  char const *phrase = "Internal Media Error";

  su_home_t tmphome[SU_HOME_AUTO_SIZE(8192)];

  su_home_auto(tmphome, sizeof tmphome);
930

Pekka Pessi's avatar
Pekka Pessi committed
931 932
  SU_DEBUG_7(("soa_static_offer_answer_action(%p, %s): called\n", ss, by));

933 934 935
  if (user == NULL)
    return soa_set_status(ss, 500, "No session set by user");

936 937 938 939 940
  if (action == generate_offer)
    remote = NULL;

  /* Pre-negotiation Step: Expand truncated remote SDP */
  if (local && remote) switch (action) {
941 942 943 944
  case generate_answer:
  case process_answer:
    if (sdp_media_count(remote, sdp_media_any, "*", 0, 0) < 
	sdp_media_count(local, sdp_media_any, "*", 0, 0)) {
Pekka Pessi's avatar
Pekka Pessi committed
945
      SU_DEBUG_5(("%s: remote %s is truncated: expanding\n",
946 947
		  by, action == generate_answer ? "offer" : "answer"));
      remote = soa_sdp_expand_media(tmphome, remote, local);
948
    }
949 950 951
  default:
    break;
  }
952
  
953
  /* Step A: Create local SDP session (based on user-supplied SDP) */
954
  if (local == NULL) switch (action) {
955 956
  case generate_offer:
  case generate_answer:
Pekka Pessi's avatar
Pekka Pessi committed
957 958
    SU_DEBUG_7(("soa_static(%p, %s): generating local description\n", ss, by));

959 960 961 962 963 964 965
    local = local0;
    *local = *user, local->sdp_media = NULL;

    if (local->sdp_origin) {
      o->o_username = local->sdp_origin->o_username;
      /* o->o_address = local->sdp_origin->o_address; */
    }
966 967
    if (!o->o_address)
      o->o_address = c0; 
968
    local->sdp_origin = o;
969 970

    if (soa_init_sdp_origin(ss, o, c_address) < 0) {
971 972
      phrase = "Cannot Get IP Address for Media";
      goto internal_error;
973
    }
Pekka Pessi's avatar
Pekka Pessi committed
974

975
    break;
976

977 978
  case process_answer:
  default:
979
    goto internal_error;
980
  }
981

982 983 984 985
  /* Step B: upgrade local SDP (add m= lines to it)  */
  switch (action) {
  case generate_offer:
    /* Upgrade local SDP based on user SDP */
986
    if (local != local0 && ss->ss_local_user_version == user_version)
987
      break;
988 989
    if (local != local0)
      *local0 = *local, local = local0;
990 991
    SU_DEBUG_7(("soa_static(%p, %s): %s\n", ss, by, 
		"upgrade with local description"));
992
    soa_sdp_upgrade(ss, tmphome, local, user, user);
993 994 995
    break;
  case generate_answer:
    /* Upgrade local SDP based on remote SDP */
996 997
    if (ss->ss_local_user_version == user_version &&
	ss->ss_local_remote_version == remote_version)
998
      break;
999
    if (1) {
1000 1001
      if (local != local0)
	*local0 = *local, local = local0;
1002 1003
      SU_DEBUG_7(("soa_static(%p, %s): %s\n", ss, by,
		  "upgrade with remote description"));
1004
      soa_sdp_upgrade(ss, tmphome, local, user, remote);
1005 1006
    }
    break;
1007
  case process_answer:
1008 1009
  default:
    break;
1010
  }
1011

1012

1013 1014 1015 1016 1017 1018 1019 1020 1021
  /* Step C: reject media */
  switch (action) {
  case generate_offer:
    /* Local media is marked as rejected already in upgrade phase */
    break;
  case generate_answer:
  case process_answer:
    if (ss->ss_local_remote_version == remote_version)
      break;
1022 1023
    if (soa_sdp_reject_is_needed(local, remote)) {
      if (local != local0) {
1024
	*local0 = *local, local = local0;
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
#define DUP_LOCAL(local)					 \
	do {							 \
	  if (!local->sdp_media) break;				 \
	  local->sdp_media =					 \
	    sdp_media_dup_all(tmphome, local->sdp_media, local); \
	  if (!local->sdp_media)				 \
	    goto internal_error;				 \
	} while (0)
	DUP_LOCAL(local);
      }
Pekka Pessi's avatar
Pekka Pessi committed
1035
      SU_DEBUG_7(("soa_static(%p, %s): marking rejected media\n", ss, by));
1036 1037
      soa_sdp_reject(tmphome, local, remote);
    }
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
    break;
  default:
    break;
  }

  /* Step D: Set media mode bits */
  switch (action) {
  case generate_offer:
  case generate_answer:
  case process_answer:
    if (soa_sdp_mode_set_is_needed(local, remote, ss->ss_hold)) {
      if (local != local0) {
1050
	*local0 = *local, local = local0;
1051 1052 1053 1054 1055 1056
	DUP_LOCAL(local);
      }

      soa_sdp_mode_set(local, remote, ss->ss_hold);
    }
    break;
1057 1058
  default:
    break;
1059
  }
1060

1061
  /* Step E: Upgrade codecs by answer. */
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077
  switch (action) {
  case process_answer:
    /* Upgrade local SDP based on remote SDP */
    if (ss->ss_local_remote_version == remote_version)
      break;
    if (1 /* We don't have good test for codecs */) {
      SU_DEBUG_7(("soa_static(%p, %s): %s\n", ss, by,
		  "upgrade codecs with remote description"));
      if (local != local0) {
	*local0 = *local, local = local0; 
	DUP_LOCAL(local);
      }
      soa_sdp_upgrade_rtpmaps(ss, local, remote);
    }
    break;
  case generate_offer:
1078
  case generate_answer:
1079 1080 1081 1082
  default:
    break;
  }

1083 1084
  soa_description_free(ss, ss->ss_previous);

1085 1086
  if (ss->ss_local->ssd_sdp != local &&
      sdp_session_cmp(ss->ss_local->ssd_sdp, local)) {
1087
    /* We have modfied local session: update origin-line */
1088 1089 1090 1091
    if (local->sdp_origin != o)
      *o = *local->sdp_origin, local->sdp_origin = o;
    o->o_version++;

1092
    /* Do sanity checks for the created SDP */
1093 1094 1095 1096 1097 1098 1099 1100 1101
    if (!local->sdp_subject)	/* s= is mandatory */
      local->sdp_subject = "-";
    if (!local->sdp_time)	/* t= is mandatory */
      local->sdp_time = t;

    /* Every m= line (even rejected one) must have a c= line 
     * or there must be a c= line at session level
     */
    c = local->sdp_origin->o_address;
1102

1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
    if (local->sdp_connection == NULL) {
      sdp_media_t *m;

      for (m = local->sdp_media; m; m = m->m_next)
	if (m->m_connections == NULL)
	  break;
      if (m)
	local->sdp_connection = c;
    }

    if (action == generate_offer) {
1114
      /* Keep a copy of previous session state */
1115 1116 1117 1118 1119 1120
      *ss->ss_previous = *ss->ss_local;
      memset(ss->ss_local, 0, (sizeof *ss->ss_local));
      ss->ss_previous_user_version = ss->ss_local_user_version;
      ss->ss_previous_remote_version = ss->ss_local_remote_version;
    }

Pekka Pessi's avatar
Pekka Pessi committed
1121 1122
    SU_DEBUG_7(("soa_static(%p, %s): storing local description\n", ss, by));

1123 1124 1125 1126
    /* Update the unparsed and pretty-printed descriptions  */
    if (soa_description_set(ss, ss->ss_local, local, NULL, 0) < 0) {
      goto internal_error;
    }
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
  }

  /* Update version numbers */
  switch (action) {
  case generate_offer:
    ss->ss_local_user_version = user_version;
    break;
  case generate_answer:
    ss->ss_local_user_version = user_version;
    ss->ss_local_remote_version = remote_version;
    break;
  case process_answer:
    ss->ss_local_remote_version = remote_version;
  default:
    break;
1142
  }
1143 1144 1145

  su_home_deinit(tmphome);
  return 0;