sdp_print.c 19 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
1 2 3 4 5 6 7
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2005 Nokia Corporation.
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
8
 * This library is free software; you can redistribute it and/or
Pekka Pessi's avatar
Pekka Pessi committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * 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
 *
 */

25
/**@ingroup sdp_printer
Pekka Pessi's avatar
Pekka Pessi committed
26 27 28 29 30 31 32 33 34 35
 *
 * @CFILE sdp_print.c  Simple SDP printer interface.
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 *
 * @date Created: Fri Feb 18 10:25:08 2000 ppessi
 */

#include "config.h"

36
#include <sofia-sip/su_alloc.h>
37
#include <sofia-sip/su_string.h>
38

39
#include "sofia-sip/sdp.h"
40

Pekka Pessi's avatar
Pekka Pessi committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include <assert.h>

/* ========================================================================= */
/* Printing API */
/* */

#define SDP_BLOCK   (512)

#define CRLF "\015\012"

56
typedef unsigned longlong ull;
57

58 59 60 61 62 63 64 65
/** @typedef struct sdp_printer_s sdp_printer_t
 *
 * SDP printer handle.
 *
 * @sa #sdp_session_t, sdp_print(), sdp_printing_error(),
 * sdp_message(), sdp_message_size(), sdp_printer_free()
 */

Pekka Pessi's avatar
Pekka Pessi committed
66 67 68 69
struct sdp_printer_s {
  int        pr_size;
  su_home_t *pr_home;
  char      *pr_buffer;
70 71
  size_t     pr_bsiz;
  size_t     pr_used;
Pekka Pessi's avatar
Pekka Pessi committed
72
  /* various flags */
73 74
  unsigned   pr_ok : 1;
  unsigned   pr_strict : 1;
75 76 77 78 79
  unsigned   pr_owns_buffer:1;
  unsigned   pr_may_realloc:1;
  unsigned   pr_all_rtpmaps:1;
  unsigned   pr_mode_manual:1;
  unsigned   pr_mode_always:1;
Pekka Pessi's avatar
Pekka Pessi committed
80 81 82 83 84
};

static struct sdp_printer_s printer_memory_error = {
  sizeof(printer_memory_error),
  NULL,
85
  "memory exhausted",
Pekka Pessi's avatar
Pekka Pessi committed
86 87 88 89 90 91 92 93
  sizeof(printer_memory_error.pr_buffer),
  sizeof(printer_memory_error.pr_buffer)
};

static void print_session(sdp_printer_t *p, sdp_session_t const *session);

/** Print a SDP description.
 *
94
 * Encode the contents of the SDP session structure #sdp_session_t
95
 * to the @a msgbuf. The @a msgbuf has size @a msgsize
Pekka Pessi's avatar
Pekka Pessi committed
96 97 98 99 100
 * bytes. If @a msgbuf is @c NULL, the sdp_print() function allocates the
 * required buffer from the @a home heap.
 *
 * @param home     Memory home (may be NULL).
 * @param session  SDP session description structure to be encoded.
101
 * @param msgbuf   Buffer to which encoding is stored (may be NULL).
Pekka Pessi's avatar
Pekka Pessi committed
102 103 104 105 106
 * @param msgsize  Size of @a msgbuf.
 * @param flags    Flags specifying the encoding options.
 *
 * The @a flags specify encoding options as follows:
 *
107
 * @li #sdp_f_strict - Printer should only emit messages conforming strictly
Pekka Pessi's avatar
Pekka Pessi committed
108 109
 * to the * specification.
 *
110
 * @li #sdp_f_realloc - If this flag is specified, and @a msgbuf is too
Pekka Pessi's avatar
Pekka Pessi committed
111 112 113
 * small for the resulting SDP message, @c sdp_print() may allocate a new
 * buffer for it from the heap.
 *
114 115
 * @li #sdp_f_print_prefix - The buffer provided by caller already contains
 * valid data. The result will concatenated to the string in the buffer.
Pekka Pessi's avatar
Pekka Pessi committed
116
 *
117 118 119
 * @li #sdp_f_mode_always - Always add mode attributes to media
 *
 * @li #sdp_f_mode_manual - Do not generate mode attributes
120
 *
121
 * @return
122 123 124 125 126
 * Always return a handle to an #sdp_printer_t object.
 *
 * @sa #sdp_printer_t, #sdp_session_t, sdp_printing_error(),
 * sdp_message(), sdp_message_size(), sdp_printer_free(),
 * sdp_parse().
Pekka Pessi's avatar
Pekka Pessi committed
127
 */
128 129 130 131
sdp_printer_t *sdp_print(su_home_t *home,
			 sdp_session_t const *session,
			 char msgbuf[],
			 isize_t msgsize,
Pekka Pessi's avatar
Pekka Pessi committed
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
			 int flags)
{
  sdp_printer_t *p = su_salloc(home, sizeof(*p));

  if (p) {
    p->pr_size = sizeof(p);
    p->pr_home = home;
    p->pr_used   = 0;
    if (msgbuf) {
      p->pr_may_realloc = (flags & sdp_f_realloc) != 0;
      p->pr_buffer = msgbuf;
      p->pr_bsiz   = msgsize;
      if (flags & sdp_f_print_prefix)
	p->pr_used = strlen(msgbuf);
    }
    else {
      p->pr_owns_buffer = 1;
      p->pr_buffer = su_alloc(home, SDP_BLOCK);
      p->pr_bsiz   = SDP_BLOCK;
    }
    p->pr_strict = (flags & sdp_f_strict) != 0;
    p->pr_all_rtpmaps = (flags & sdp_f_all_rtpmaps) != 0;
154 155
    p->pr_mode_manual = (flags & sdp_f_mode_manual) != 0;
    p->pr_mode_always = (flags & sdp_f_mode_always) != 0;
Pekka Pessi's avatar
Pekka Pessi committed
156 157 158 159 160 161 162 163 164 165 166 167

    print_session(p, session);

    return p;
  }
  else {
    return &printer_memory_error;
  }
}

/** @brief Get encoding error.
 *
168
 * Return a message describing the encoding error.
Pekka Pessi's avatar
Pekka Pessi committed
169
 *
170
 * @param p Pointer to an #sdp_printer_t object.
Pekka Pessi's avatar
Pekka Pessi committed
171 172
 *
 * @return
173 174 175
 * Return a pointer to C string describing printing errors, or NULL if no
 * error was encountered.
 */
Pekka Pessi's avatar
Pekka Pessi committed
176 177 178 179 180 181
char const *sdp_printing_error(sdp_printer_t *p)
{
  if (p)
    if (!p->pr_ok)
      return p->pr_buffer;
    else
182
      return NULL;
Pekka Pessi's avatar
Pekka Pessi committed
183 184 185 186 187 188
  else
    return "null sdp_printer_t*";
}

/** @brief Get encoded SDP message.
 *
189
 * Return a pointer to a C string containing the SDP message.
190
 *
191
 * @param p Pointer to an #sdp_printer_t object.
Pekka Pessi's avatar
Pekka Pessi committed
192
 *
193
 * @return
194 195
 * Return a pointer to a C string containing the encoded SDP message, or
 * NULL upon an error.
Pekka Pessi's avatar
Pekka Pessi committed
196 197 198 199 200 201 202 203 204 205 206
 */
char const *sdp_message(sdp_printer_t *p)
{
  if (p && p->pr_ok)
    return p->pr_buffer;
  else
    return NULL;
}

/** @brief Get size of encoded SDP message.
 *
207
 * Return the size of the encoded SDP message.
Pekka Pessi's avatar
Pekka Pessi committed
208
 *
209
 * @param p Pointer to an #sdp_printer_t object.
Pekka Pessi's avatar
Pekka Pessi committed
210
 *
211
 * @return
212
 * Number of bytes in SDP message excluding final NUL or 0 upon an error.
Pekka Pessi's avatar
Pekka Pessi committed
213
 */
214
isize_t sdp_message_size(sdp_printer_t *p)
Pekka Pessi's avatar
Pekka Pessi committed
215 216 217 218 219 220 221 222 223
{
  if (p && p->pr_ok)
    return p->pr_used;
  else
    return 0;
}

/** Free a SDP printer.
 *
224 225
 * Free the printer object @a p and the message buffer possibly associated
 * with it.
Pekka Pessi's avatar
Pekka Pessi committed
226
 *
227
 * @param p Pointer to an #sdp_printer_t object.
Pekka Pessi's avatar
Pekka Pessi committed
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
 */
void sdp_printer_free(sdp_printer_t *p)
{
  if (p && p != &printer_memory_error) {
    if (p->pr_owns_buffer && p->pr_buffer)
      su_free(p->pr_home, p->pr_buffer), p->pr_buffer = NULL;
    su_free(p->pr_home, p);
  }
}

/* ========================================================================= */
static void print_version(sdp_printer_t *p, sdp_version_t const *v);
static void print_origin(sdp_printer_t *p, sdp_origin_t const *o);
static void print_subject(sdp_printer_t *p, sdp_text_t *s);
static void print_information(sdp_printer_t *p, sdp_text_t *i);
static void print_uri(sdp_printer_t *p, sdp_text_t  *u);
static void print_emails(sdp_printer_t *p, sdp_list_t const *e);
static void print_phones(sdp_printer_t *p, sdp_list_t const *ph);
static void print_connection(sdp_printer_t *p, sdp_connection_t const *c);
static void print_connection_list(sdp_printer_t *p, sdp_connection_t const *c);
static void print_connection2(sdp_printer_t *p, sdp_connection_t const *c);
static void print_bandwidths(sdp_printer_t *p, sdp_bandwidth_t const *b);
static void print_time(sdp_printer_t *p, sdp_time_t const *t);
static void print_repeat(sdp_printer_t *p, sdp_repeat_t const *r);
static void print_zone(sdp_printer_t *p, sdp_zone_t const *z);
static void print_typed_time(sdp_printer_t *p, unsigned long t);
static void print_key(sdp_printer_t *p, sdp_key_t const *k);
static void print_attributes(sdp_printer_t *p, sdp_attribute_t const *a);
static void print_charset(sdp_printer_t *p, sdp_text_t *charset);
257
static void print_media(sdp_printer_t *p, sdp_session_t const *,
Pekka Pessi's avatar
Pekka Pessi committed
258 259
			sdp_media_t const *m);

260
static void print_text_list(sdp_printer_t*,
Pekka Pessi's avatar
Pekka Pessi committed
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
			    const char *, sdp_list_t const *l);

static void sdp_printf(sdp_printer_t *p, const char *fmt, ...);
static void printing_error(sdp_printer_t *p, const char *fmt, ...);

static void print_session(sdp_printer_t *p, sdp_session_t const *sdp)
{
  p->pr_ok = 1;

  if (!sdp)
    printing_error(p, "NULL session description");
  if (p->pr_ok && sdp->sdp_version)
    print_version(p, sdp->sdp_version);
  if (p->pr_ok && sdp->sdp_origin)
    print_origin(p, sdp->sdp_origin);
  if (p->pr_ok && sdp->sdp_subject)
    print_subject(p, sdp->sdp_subject);
  if (p->pr_ok && sdp->sdp_information)
    print_information(p, sdp->sdp_information);
  if (p->pr_ok && sdp->sdp_uri)
    print_uri(p, sdp->sdp_uri);
  if (p->pr_ok && sdp->sdp_emails)
    print_emails(p, sdp->sdp_emails);
  if (p->pr_ok && sdp->sdp_phones)
    print_phones(p, sdp->sdp_phones);
  if (p->pr_ok && sdp->sdp_connection)
    print_connection(p, sdp->sdp_connection);
  if (p->pr_ok && sdp->sdp_bandwidths)
    print_bandwidths(p, sdp->sdp_bandwidths);
  if (p->pr_ok)
    print_time(p, sdp->sdp_time);
  if (p->pr_ok && sdp->sdp_time) {
    if (p->pr_ok && sdp->sdp_time->t_repeat)
      print_repeat(p, sdp->sdp_time->t_repeat);
    if (p->pr_ok && sdp->sdp_time->t_zone)
      print_zone(p, sdp->sdp_time->t_zone);
  }
  if (p->pr_ok && sdp->sdp_key)
    print_key(p, sdp->sdp_key);
  if (p->pr_ok && sdp->sdp_charset)
    print_charset(p, sdp->sdp_charset);
  if (p->pr_ok && sdp->sdp_attributes)
    print_attributes(p, sdp->sdp_attributes);
  if (p->pr_ok && sdp->sdp_media)
    print_media(p, sdp, sdp->sdp_media);
}

static void print_version(sdp_printer_t *p, sdp_version_t const *v)
{
  sdp_printf(p, "v=%lu" CRLF, *v);
}

static void print_origin(sdp_printer_t *p, sdp_origin_t const *o)
{
  if (!o->o_address ||
      !o->o_address->c_address ||
      o->o_address->c_ttl != 0 ||
      o->o_address->c_groups > 1) {
    printing_error(p, "o= address malformed");
    return;
  }

323
  sdp_printf(p, "o=%s "LLU" "LLU" ",
Pekka Pessi's avatar
Pekka Pessi committed
324
	     o->o_username,
325 326
	     (ull)o->o_id,
	     (ull)o->o_version);
Pekka Pessi's avatar
Pekka Pessi committed
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 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

  print_connection2(p, o->o_address);
}

static void print_subject(sdp_printer_t *p, sdp_text_t *subject)
{
  sdp_printf(p, "s=%s" CRLF, subject);
}

static void print_information(sdp_printer_t *p, sdp_text_t *information)
{
  sdp_printf(p, "i=%s" CRLF, information);
}

static void print_uri(sdp_printer_t *p, sdp_text_t *uri)
{
  sdp_printf(p, "u=%s" CRLF, uri);
}

static void print_emails(sdp_printer_t *p, sdp_list_t const *emails)
{
  print_text_list(p, "e=%s" CRLF, emails);
}

static void print_phones(sdp_printer_t *p, sdp_list_t const *phones)
{
  print_text_list(p, "p=%s" CRLF, phones);
}

static void print_connection(sdp_printer_t *p, sdp_connection_t const *c)
{
  sdp_printf(p, "c=");
  print_connection2(p, c);
}

static void print_connection_list(sdp_printer_t *p, sdp_connection_t const *c)
{
  for (; c ; c = c->c_next) {
    sdp_printf(p, "c=");
    print_connection2(p, c);
  }
}

static void print_connection2(sdp_printer_t *p, sdp_connection_t const *c)
{
  const char *nettype;
  const char *addrtype;

  switch (c->c_nettype) {
  case sdp_net_x:
    nettype = NULL;
    break;
  case sdp_net_in:
    nettype = "IN ";
    break;
  default:
    printing_error(p, "unknown nettype %u", c->c_nettype);
    return;
  }

  switch (c->c_addrtype) {
  case sdp_addr_x:
    addrtype = NULL;
    break;
  case sdp_addr_ip4:
    nettype = "IN ";
    addrtype = "IP4 ";
    break;
  case sdp_addr_ip6:
    nettype = "IN ";
    addrtype = "IP6 ";
    break;
  default:
    printing_error(p, "unknown address type %u", c->c_addrtype);
    return;
  }

  if (c->c_address == NULL) {
    printing_error(p, "missing address");
    return;
  }

409 410 411 412 413 414
  if (nettype && addrtype)
    sdp_printf(p, "%s%s%s", nettype, addrtype, c->c_address);
  else if (nettype)
    sdp_printf(p, "%s%s%s", nettype, c->c_address);
  else
    sdp_printf(p, "%s", c->c_address);
415

Pekka Pessi's avatar
Pekka Pessi committed
416 417 418 419 420 421 422 423 424 425 426 427
  if (c->c_mcast || c->c_ttl) {
    sdp_printf(p, "/%u", c->c_ttl);
    if (c->c_groups > 1)
      sdp_printf(p, "/%u", c->c_groups);
  }
  sdp_printf(p, CRLF);
}

static void print_bandwidths(sdp_printer_t *p, sdp_bandwidth_t const *b)
{
  for (; b ; b = b->b_next) {
    char const *name;
428

Pekka Pessi's avatar
Pekka Pessi committed
429 430 431 432 433
    switch (b->b_modifier) {
    case sdp_bw_ct: name = "CT"; break;
    case sdp_bw_as: name = "AS"; break;
    default:        name = b->b_modifier_name; break;
    }
434

Pekka Pessi's avatar
Pekka Pessi committed
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
    sdp_printf(p, "b=%s:%lu" CRLF, name, b->b_value);
  }
}

static void print_time(sdp_printer_t *p, sdp_time_t const *t)
{
  if (t || p->pr_strict)
    sdp_printf(p, "t=%lu %lu" CRLF, t ? t->t_start : 0L, t ? t->t_stop : 0L);
}

static void print_repeat(sdp_printer_t *p, sdp_repeat_t const *r)
{
  int i;

  sdp_printf(p, "r=");
  print_typed_time(p, r->r_interval);
  sdp_printf(p, " ");
  print_typed_time(p, r->r_duration);
  for (i = 0; i < r->r_number_of_offsets; i++) {
    sdp_printf(p, " ");
    print_typed_time(p, r->r_offsets[i]);
  }
  sdp_printf(p, CRLF);
}

static void print_zone(sdp_printer_t *p, sdp_zone_t const *z)
{
  int i;
  sdp_printf(p, "z=");

  for (i = 0; i < z->z_number_of_adjustments; i++) {
    int negative = z->z_adjustments[i].z_offset < 0L;
467
    sdp_printf(p, "%s%lu %s",
Pekka Pessi's avatar
Pekka Pessi committed
468
	       i > 0 ? " " : "",
469
	       z->z_adjustments[i].z_at,
Pekka Pessi's avatar
Pekka Pessi committed
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 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
	       negative ? "-" : "");
    if (negative)
      print_typed_time(p, -z->z_adjustments[i].z_offset);
    else
      print_typed_time(p, z->z_adjustments[i].z_offset);
  }

  sdp_printf(p, CRLF);
}

static void  print_typed_time(sdp_printer_t *p, unsigned long t)
{
  if (t % 60 || t == 0) {
    sdp_printf(p, "%lu", t);
  }
  else {
    t /= 60;

    if (t % 60) {
      sdp_printf(p, "%lum", t); /* minutes */
    }
    else {
      t /= 60;

      if (t % 24) {
	sdp_printf(p, "%luh", t); /* hours */
      }
      else {
	t /= 24;

	sdp_printf(p, "%lud", t);	/* days */
      }
    }
  }
}

static void print_key(sdp_printer_t *p, sdp_key_t const *k)
{
  const char *method;
  int have_material = k->k_material != NULL;

  switch (k->k_method) {
  case sdp_key_x:
    method = k->k_method_name; break;
  case sdp_key_clear:
    method = "clear"; break;
  case sdp_key_base64:
    method = "base64"; break;
  case sdp_key_uri:
    method = "uri"; break;
  case sdp_key_prompt:
    method = "prompt"; break;
  default:
    printing_error(p, "unknown key method (%d)", k->k_method);
    return;
  }

527
  sdp_printf(p, "k=%s%s%s" CRLF, method,
Pekka Pessi's avatar
Pekka Pessi committed
528 529 530 531 532 533 534 535 536 537 538 539 540
	     have_material ? ":" : "",
	     have_material ? k->k_material : "");
}

static void print_attributes(sdp_printer_t *p, sdp_attribute_t const *a)
{
  for (;a; a = a->a_next) {
    char const *name = a->a_name;
    char const *value = a->a_value;
    sdp_printf(p, "a=%s%s%s" CRLF, name, value ? ":" : "", value ? value : "");
  }
}

541
static void
542 543 544 545 546 547
print_attributes_without_mode(sdp_printer_t *p, sdp_attribute_t const *a)
{
  for (;a; a = a->a_next) {
    char const *name = a->a_name;
    char const *value = a->a_value;

548 549 550 551
    if (su_casematch(name, "inactive") ||
	su_casematch(name, "sendonly") ||
	su_casematch(name, "recvonly") ||
	su_casematch(name, "sendrecv"))
552 553 554 555 556 557
      continue;

    sdp_printf(p, "a=%s%s%s" CRLF, name, value ? ":" : "", value ? value : "");
  }
}

Pekka Pessi's avatar
Pekka Pessi committed
558 559 560 561 562
static void print_charset(sdp_printer_t *p, sdp_text_t *charset)
{
  sdp_printf(p, "a=charset%s%s" CRLF, charset ? ":" : "", charset ? charset : "");
}

563
static void print_media(sdp_printer_t *p,
Pekka Pessi's avatar
Pekka Pessi committed
564 565 566 567 568 569
			sdp_session_t const *sdp,
			sdp_media_t const *m)
{
  char const *media, *proto;
  sdp_rtpmap_t *rm;

570 571 572 573 574
  sdp_mode_t session_mode = sdp_sendrecv;

  if (!p->pr_mode_manual)
    session_mode = sdp_attribute_mode(sdp->sdp_attributes, sdp_sendrecv);

Pekka Pessi's avatar
Pekka Pessi committed
575 576 577 578 579 580 581 582 583 584 585
  for (;m ; m = m->m_next) {
    switch (m->m_type) {
    case sdp_media_audio:       media = "audio"; break;
    case sdp_media_video:       media = "video"; break;
    case sdp_media_application: media = "application"; break;
    case sdp_media_data:        media = "data"; break;
    case sdp_media_control:     media = "control"; break;
    case sdp_media_message:     media = "message"; break;
    case sdp_media_image  :     media = "image"; break;
    default:                    media = m->m_type_name;
    }
586

Pekka Pessi's avatar
Pekka Pessi committed
587 588
    switch (m->m_proto) {
    case sdp_proto_tcp:   proto = "tcp"; break;
589 590 591
    case sdp_proto_udp:   proto = "udp"; break;
    case sdp_proto_rtp:   proto = "RTP/AVP"; break;
    case sdp_proto_srtp:  proto = "RTP/SAVP"; break;
592
    case sdp_proto_udptl: proto = "udptl"; break;
Pekka Pessi's avatar
Pekka Pessi committed
593 594 595
    case sdp_proto_tls:   proto = "tls"; break;
    default:              proto = m->m_proto_name; break;
    }
596

Pekka Pessi's avatar
Pekka Pessi committed
597 598 599
    if (m->m_number_of_ports <= 1)
      sdp_printf(p, "m=%s %u %s", media, m->m_port, proto);
    else
600
      sdp_printf(p, "m=%s %u/%u %s",
Pekka Pessi's avatar
Pekka Pessi committed
601
		 media, m->m_port, m->m_number_of_ports, proto);
602

Pekka Pessi's avatar
Pekka Pessi committed
603 604 605 606 607 608 609 610 611 612 613 614 615 616
    if (m->m_rtpmaps) {
      for (rm = m->m_rtpmaps; rm; rm = rm->rm_next) {
	if (rm->rm_any)
	  sdp_printf(p, " *");
	else
	  sdp_printf(p, " %u", (unsigned)rm->rm_pt);
      }
    }
    else if (m->m_format) {
      sdp_list_t *l = m->m_format;
      for (; l; l = l->l_next)
	sdp_printf(p, " %s", l->l_text);
    }
    else {
617 618
      sdp_printf(p, " 19");      /* SDP syntax requires at least one format.
				    19 is used by nobody, right?. */
Pekka Pessi's avatar
Pekka Pessi committed
619
    }
620 621


Pekka Pessi's avatar
Pekka Pessi committed
622 623 624 625 626 627 628
    sdp_printf(p, CRLF);

    if (m->m_information)
      print_information(p, m->m_information);
    if (m->m_connections)
#ifdef nomore
    if (m->m_connections != sdp->sdp_connection)
629
#endif
Pekka Pessi's avatar
Pekka Pessi committed
630 631 632 633 634 635 636 637 638 639
      print_connection_list(p, m->m_connections);
    if (m->m_bandwidths)
      print_bandwidths(p, m->m_bandwidths);
    if (m->m_key)
      print_key(p, m->m_key);

    for (rm = m->m_rtpmaps; rm; rm = rm->rm_next) {
      if (!rm->rm_predef || p->pr_all_rtpmaps)
	sdp_printf(p, "a=rtpmap:%u %s/%lu%s%s" CRLF,
		   rm->rm_pt, rm->rm_encoding, rm->rm_rate,
640
		   rm->rm_params ? "/" : "",
Pekka Pessi's avatar
Pekka Pessi committed
641 642
		   rm->rm_params ? rm->rm_params : "");
      if (rm->rm_fmtp)
643
	sdp_printf(p, "a=fmtp:%u %s" CRLF,
Pekka Pessi's avatar
Pekka Pessi committed
644 645 646
		   rm->rm_pt, rm->rm_fmtp);
    }

647
    if (!p->pr_mode_manual && !m->m_rejected &&
648
	(m->m_mode != (unsigned int)session_mode || p->pr_mode_always)) {
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
      switch (m->m_mode) {
      case sdp_inactive:
	sdp_printf(p, "a=inactive" CRLF);
	break;
      case sdp_sendonly:
	sdp_printf(p, "a=sendonly" CRLF);
	break;
      case sdp_recvonly:
	sdp_printf(p, "a=recvonly" CRLF);
	break;
      case sdp_sendrecv:
	sdp_printf(p, "a=sendrecv" CRLF);
	break;
      default:
	break;
      }
    }

    if (p->pr_mode_manual)
Pekka Pessi's avatar
Pekka Pessi committed
668
      print_attributes(p, m->m_attributes);
669 670
    else
      print_attributes_without_mode(p, m->m_attributes);
Pekka Pessi's avatar
Pekka Pessi committed
671 672 673
  }
}

674
static void print_text_list(sdp_printer_t *p,
Pekka Pessi's avatar
Pekka Pessi committed
675 676 677 678 679 680 681 682 683 684
			    const char *fmt, sdp_list_t const *l)
{
  for (;l; l = l->l_next) {
    sdp_printf(p, fmt, l->l_text);
  }
}

static void printing_error(sdp_printer_t *p, const char *fmt, ...)
{
  va_list ap;
685

Pekka Pessi's avatar
Pekka Pessi committed
686 687
  if (p->pr_ok) {
    int n;
688
    va_start(ap, fmt);
Pekka Pessi's avatar
Pekka Pessi committed
689
    n = vsnprintf(p->pr_buffer, p->pr_bsiz, fmt, ap);
690
    va_end(ap);
Pekka Pessi's avatar
Pekka Pessi committed
691 692
  }

693
  p->pr_ok = 0;
Pekka Pessi's avatar
Pekka Pessi committed
694 695 696 697 698 699 700 701
}

static void sdp_printf(sdp_printer_t *p, const char *fmt, ...)
{
  va_list ap;

  while (p->pr_ok) {
    int n;
702 703

    va_start(ap, fmt);
Pekka Pessi's avatar
Pekka Pessi committed
704 705
    n = vsnprintf(p->pr_buffer + p->pr_used, p->pr_bsiz - p->pr_used, fmt, ap);
    va_end(ap);
706

707
    if (n > -1 && (size_t)n < p->pr_bsiz - p->pr_used) {
Pekka Pessi's avatar
Pekka Pessi committed
708 709 710 711 712 713 714 715 716 717 718 719
      p->pr_used += n;
      break;
    }
    else {
      if (p->pr_owns_buffer) {
	p->pr_buffer = su_realloc(p->pr_home, p->pr_buffer, 2 * p->pr_bsiz);
	if (p->pr_buffer) {
	  p->pr_bsiz = 2 * p->pr_bsiz;
	  continue;
	}
	p->pr_owns_buffer = 0;
      }
720
      else if (p->pr_may_realloc) {
Pekka Pessi's avatar
Pekka Pessi committed
721
	char *buffer;
722
	size_t size;
Pekka Pessi's avatar
Pekka Pessi committed
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
	if (p->pr_bsiz < SDP_BLOCK)
	  size = SDP_BLOCK;
	else
	  size = 2 * p->pr_bsiz;
	buffer = su_alloc(p->pr_home, size);
	if (buffer) {
	  p->pr_owns_buffer = 1;
	  p->pr_buffer = memcpy(buffer, p->pr_buffer, p->pr_bsiz);
	  p->pr_bsiz = size;
	  continue;
	}
      }
      p->pr_ok = 0;
      p->pr_buffer = "Memory exhausted";
    }
  }
}