rtcpcompoundpacketbuilder.cpp 22.2 KB
1 2 3 4 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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 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 257 258 259 260 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 323 324 325 326 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 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 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813
/*

  This file is a part of JRTPLIB
  Copyright (c) 1999-2017 Jori Liesenborgs

  Contact: jori.liesenborgs@gmail.com

  This library was developed at the Expertise Centre for Digital Media
  (http://www.edm.uhasselt.be), a research center of the Hasselt University
  (http://www.uhasselt.be). The library is based upon work done for 
  my thesis at the School for Knowledge Technology (Belgium/The Netherlands).

  Permission is hereby granted, free of charge, to any person obtaining a
  copy of this software and associated documentation files (the "Software"),
  to deal in the Software without restriction, including without limitation
  the rights to use, copy, modify, merge, publish, distribute, sublicense,
  and/or sell copies of the Software, and to permit persons to whom the
  Software is furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included
  in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  IN THE SOFTWARE.

*/

#include "rtcpcompoundpacketbuilder.h"
#include "rtcpsrpacket.h"
#include "rtcprrpacket.h"
#include "rtcpsdespacket.h"
#include "rtcpbyepacket.h"
#include "rtcpapppacket.h"
#ifdef RTP_SUPPORT_RTCPUNKNOWN
	#include "rtcpunknownpacket.h"
#endif // RTP_SUPPORT_RTCPUNKNOWN 
#ifdef RTP_SUPPORT_NETINET_IN
	#include <netinet/in.h>
#endif // RTP_SUPPORT_NETINET_IN
#include <string.h>

#include "rtpdebug.h"

namespace jrtplib
{

RTCPCompoundPacketBuilder::RTCPCompoundPacketBuilder(RTPMemoryManager *mgr) : RTCPCompoundPacket(mgr), report(mgr), sdes(mgr)
{
	byesize = 0;
	appsize = 0;
#ifdef RTP_SUPPORT_RTCPUNKNOWN
	unknownsize = 0;
#endif // RTP_SUPPORT_RTCPUNKNOWN
	maximumpacketsize = 0;
	buffer = 0;
	external = false;
	arebuilding = false;
}

RTCPCompoundPacketBuilder::~RTCPCompoundPacketBuilder()
{
	if (external)
		compoundpacket = 0; // make sure RTCPCompoundPacket doesn't delete the external buffer
	ClearBuildBuffers();
}

void RTCPCompoundPacketBuilder::ClearBuildBuffers()
{
	report.Clear();
	sdes.Clear();

	std::list<Buffer>::const_iterator it;
	for (it = byepackets.begin() ; it != byepackets.end() ; it++)
	{
		if ((*it).packetdata)
			RTPDeleteByteArray((*it).packetdata,GetMemoryManager());
	}
	for (it = apppackets.begin() ; it != apppackets.end() ; it++)
	{
		if ((*it).packetdata)
			RTPDeleteByteArray((*it).packetdata,GetMemoryManager());
	}
#ifdef RTP_SUPPORT_RTCPUNKNOWN
	for (it = unknownpackets.begin() ; it != unknownpackets.end() ; it++)
	{
		if ((*it).packetdata)
			RTPDeleteByteArray((*it).packetdata,GetMemoryManager());
	}
#endif // RTP_SUPPORT_RTCPUNKNOWN 

	byepackets.clear();
	apppackets.clear();
#ifdef RTP_SUPPORT_RTCPUNKNOWN
	unknownpackets.clear();
#endif // RTP_SUPPORT_RTCPUNKNOWN 
	byesize = 0;
	appsize = 0;
#ifdef RTP_SUPPORT_RTCPUNKNOWN
	unknownsize = 0;
#endif // RTP_SUPPORT_RTCPUNKNOWN 
}

int RTCPCompoundPacketBuilder::InitBuild(size_t maxpacketsize)
{
	if (arebuilding)
		return ERR_RTP_RTCPCOMPPACKBUILDER_ALREADYBUILDING;
	if (compoundpacket)
		return ERR_RTP_RTCPCOMPPACKBUILDER_ALREADYBUILT;

	if (maxpacketsize < RTP_MINPACKETSIZE)
		return ERR_RTP_RTCPCOMPPACKBUILDER_MAXPACKETSIZETOOSMALL;
	
	maximumpacketsize = maxpacketsize;
	buffer = 0;
	external = false;
	byesize = 0;
	appsize = 0;
#ifdef RTP_SUPPORT_RTCPUNKNOWN
	unknownsize = 0;
#endif // RTP_SUPPORT_RTCPUNKNOWN 
	
	arebuilding = true;
	return 0;
}

int RTCPCompoundPacketBuilder::InitBuild(void *externalbuffer,size_t buffersize)
{
	if (arebuilding)
		return ERR_RTP_RTCPCOMPPACKBUILDER_ALREADYBUILDING;
	if (compoundpacket)
		return ERR_RTP_RTCPCOMPPACKBUILDER_ALREADYBUILT;

	if (buffersize < RTP_MINPACKETSIZE)
		return ERR_RTP_RTCPCOMPPACKBUILDER_BUFFERSIZETOOSMALL;

	maximumpacketsize = buffersize;
	buffer = (uint8_t *)externalbuffer;
	external = true;
	byesize = 0;
	appsize = 0;
#ifdef RTP_SUPPORT_RTCPUNKNOWN
	unknownsize = 0;
#endif // RTP_SUPPORT_RTCPUNKNOWN 

	arebuilding = true;
	return 0;
}

int RTCPCompoundPacketBuilder::StartSenderReport(uint32_t senderssrc,const RTPNTPTime &ntptimestamp,uint32_t rtptimestamp,
                                                 uint32_t packetcount,uint32_t octetcount)
{
	if (!arebuilding)
		return ERR_RTP_RTCPCOMPPACKBUILDER_NOTBUILDING;

	if (report.headerlength != 0)
		return ERR_RTP_RTCPCOMPPACKBUILDER_ALREADYGOTREPORT;

#ifndef RTP_SUPPORT_RTCPUNKNOWN
	size_t totalsize = byesize+appsize+sdes.NeededBytes();
#else
	size_t totalsize = byesize+appsize+unknownsize+sdes.NeededBytes();
#endif // RTP_SUPPORT_RTCPUNKNOWN 
	size_t sizeleft = maximumpacketsize-totalsize;
	size_t neededsize = sizeof(RTCPCommonHeader)+sizeof(uint32_t)+sizeof(RTCPSenderReport);
	
	if (neededsize > sizeleft)
		return ERR_RTP_RTCPCOMPPACKBUILDER_NOTENOUGHBYTESLEFT;
	
	// fill in some things

	report.headerlength = sizeof(uint32_t)+sizeof(RTCPSenderReport);
	report.isSR = true;	
	
	uint32_t *ssrc = (uint32_t *)report.headerdata;
	*ssrc = htonl(senderssrc);

	RTCPSenderReport *sr = (RTCPSenderReport *)(report.headerdata + sizeof(uint32_t));
	sr->ntptime_msw = htonl(ntptimestamp.GetMSW());
	sr->ntptime_lsw = htonl(ntptimestamp.GetLSW());
	sr->rtptimestamp = htonl(rtptimestamp);
	sr->packetcount = htonl(packetcount);
	sr->octetcount = htonl(octetcount);

	return 0;
}

int RTCPCompoundPacketBuilder::StartReceiverReport(uint32_t senderssrc)
{
	if (!arebuilding)
		return ERR_RTP_RTCPCOMPPACKBUILDER_NOTBUILDING;
	if (report.headerlength != 0)
		return ERR_RTP_RTCPCOMPPACKBUILDER_ALREADYGOTREPORT;

#ifndef RTP_SUPPORT_RTCPUNKNOWN
	size_t totalsize = byesize+appsize+sdes.NeededBytes();
#else
	size_t totalsize = byesize+appsize+unknownsize+sdes.NeededBytes();
#endif // RTP_SUPPORT_RTCPUNKNOWN 
	size_t sizeleft = maximumpacketsize-totalsize;
	size_t neededsize = sizeof(RTCPCommonHeader)+sizeof(uint32_t);
	
	if (neededsize > sizeleft)
		return ERR_RTP_RTCPCOMPPACKBUILDER_NOTENOUGHBYTESLEFT;
	
	// fill in some things

	report.headerlength = sizeof(uint32_t);
	report.isSR = false;
	
	uint32_t *ssrc = (uint32_t *)report.headerdata;
	*ssrc = htonl(senderssrc);

	return 0;
}

int RTCPCompoundPacketBuilder::AddReportBlock(uint32_t ssrc,uint8_t fractionlost,int32_t packetslost,uint32_t exthighestseq,
	                                      uint32_t jitter,uint32_t lsr,uint32_t dlsr)
{
	if (!arebuilding)
		return ERR_RTP_RTCPCOMPPACKBUILDER_NOTBUILDING;
	if (report.headerlength == 0)
		return ERR_RTP_RTCPCOMPPACKBUILDER_REPORTNOTSTARTED;

#ifndef RTP_SUPPORT_RTCPUNKNOWN
	size_t totalothersize = byesize+appsize+sdes.NeededBytes();
#else
	size_t totalothersize = byesize+appsize+unknownsize+sdes.NeededBytes();
#endif // RTP_SUPPORT_RTCPUNKNOWN 
	size_t reportsizewithextrablock = report.NeededBytesWithExtraReportBlock();
	
	if ((totalothersize+reportsizewithextrablock) > maximumpacketsize)
		return ERR_RTP_RTCPCOMPPACKBUILDER_NOTENOUGHBYTESLEFT;

	uint8_t *buf = RTPNew(GetMemoryManager(),RTPMEM_TYPE_CLASS_RTCPRECEIVERREPORT) uint8_t[sizeof(RTCPReceiverReport)];
	if (buf == 0)
		return ERR_RTP_OUTOFMEM;
	
	RTCPReceiverReport *rr = (RTCPReceiverReport *)buf;
	uint32_t *packlost = (uint32_t *)&packetslost;
	uint32_t packlost2 = (*packlost);
		
	rr->ssrc = htonl(ssrc);
	rr->fractionlost = fractionlost;
	rr->packetslost[2] = (uint8_t)(packlost2&0xFF);
	rr->packetslost[1] = (uint8_t)((packlost2>>8)&0xFF);
	rr->packetslost[0] = (uint8_t)((packlost2>>16)&0xFF);
	rr->exthighseqnr = htonl(exthighestseq);
	rr->jitter = htonl(jitter);
	rr->lsr = htonl(lsr);
	rr->dlsr = htonl(dlsr);

	report.reportblocks.push_back(Buffer(buf,sizeof(RTCPReceiverReport)));
	return 0;
}

int RTCPCompoundPacketBuilder::AddSDESSource(uint32_t ssrc)
{
	if (!arebuilding)
		return ERR_RTP_RTCPCOMPPACKBUILDER_NOTBUILDING;

#ifndef RTP_SUPPORT_RTCPUNKNOWN
	size_t totalotherbytes = byesize+appsize+report.NeededBytes();
#else
	size_t totalotherbytes = byesize+appsize+unknownsize+report.NeededBytes();
#endif // RTP_SUPPORT_RTCPUNKNOWN 
	size_t sdessizewithextrasource = sdes.NeededBytesWithExtraSource();

	if ((totalotherbytes + sdessizewithextrasource) > maximumpacketsize)
		return ERR_RTP_RTCPCOMPPACKBUILDER_NOTENOUGHBYTESLEFT;

	int status;

	if ((status = sdes.AddSSRC(ssrc)) < 0)
		return status;
	return 0;
}

int RTCPCompoundPacketBuilder::AddSDESNormalItem(RTCPSDESPacket::ItemType t,const void *itemdata,uint8_t itemlength)
{
	if (!arebuilding)
		return ERR_RTP_RTCPCOMPPACKBUILDER_NOTBUILDING;
	if (sdes.sdessources.empty())
		return ERR_RTP_RTCPCOMPPACKBUILDER_NOCURRENTSOURCE;

	uint8_t itemid;
	
	switch(t)
	{
	case RTCPSDESPacket::CNAME:
		itemid = RTCP_SDES_ID_CNAME;
		break;
	case RTCPSDESPacket::NAME:
		itemid = RTCP_SDES_ID_NAME;
		break;
	case RTCPSDESPacket::EMAIL:
		itemid = RTCP_SDES_ID_EMAIL;
		break;
	case RTCPSDESPacket::PHONE:
		itemid = RTCP_SDES_ID_PHONE;
		break;
	case RTCPSDESPacket::LOC:
		itemid = RTCP_SDES_ID_LOCATION;
		break;
	case RTCPSDESPacket::TOOL:
		itemid = RTCP_SDES_ID_TOOL;
		break;
	case RTCPSDESPacket::NOTE:
		itemid = RTCP_SDES_ID_NOTE;
		break;
	default:
		return ERR_RTP_RTCPCOMPPACKBUILDER_INVALIDITEMTYPE;
	}

#ifndef RTP_SUPPORT_RTCPUNKNOWN
	size_t totalotherbytes = byesize+appsize+report.NeededBytes();
#else
	size_t totalotherbytes = byesize+appsize+unknownsize+report.NeededBytes();
#endif // RTP_SUPPORT_RTCPUNKNOWN 
	size_t sdessizewithextraitem = sdes.NeededBytesWithExtraItem(itemlength);

	if ((sdessizewithextraitem+totalotherbytes) > maximumpacketsize)
		return ERR_RTP_RTCPCOMPPACKBUILDER_NOTENOUGHBYTESLEFT;

	uint8_t *buf;
	size_t len;

	buf = RTPNew(GetMemoryManager(),RTPMEM_TYPE_BUFFER_RTCPSDESBLOCK) uint8_t[sizeof(RTCPSDESHeader)+(size_t)itemlength];
	if (buf == 0)
		return ERR_RTP_OUTOFMEM;
	len = sizeof(RTCPSDESHeader)+(size_t)itemlength;

	RTCPSDESHeader *sdeshdr = (RTCPSDESHeader *)(buf);

	sdeshdr->sdesid = itemid;
	sdeshdr->length = itemlength;
	if (itemlength != 0)
		memcpy((buf + sizeof(RTCPSDESHeader)),itemdata,(size_t)itemlength);

	sdes.AddItem(buf,len);
	return 0;
}

#ifdef RTP_SUPPORT_SDESPRIV
int RTCPCompoundPacketBuilder::AddSDESPrivateItem(const void *prefixdata,uint8_t prefixlength,const void *valuedata,
                                                  uint8_t valuelength)
{
	if (!arebuilding)
		return ERR_RTP_RTCPCOMPPACKBUILDER_NOTBUILDING;
	if (sdes.sdessources.empty())
		return ERR_RTP_RTCPCOMPPACKBUILDER_NOCURRENTSOURCE;

	size_t itemlength = ((size_t)prefixlength)+1+((size_t)valuelength);
	if (itemlength > 255)
		return ERR_RTP_RTCPCOMPPACKBUILDER_TOTALITEMLENGTHTOOBIG;
	
#ifndef RTP_SUPPORT_RTCPUNKNOWN
	size_t totalotherbytes = byesize+appsize+report.NeededBytes();
#else
	size_t totalotherbytes = byesize+appsize+unknownsize+report.NeededBytes();
#endif // RTP_SUPPORT_RTCPUNKNOWN 
	size_t sdessizewithextraitem = sdes.NeededBytesWithExtraItem(itemlength);

	if ((sdessizewithextraitem+totalotherbytes) > maximumpacketsize)
		return ERR_RTP_RTCPCOMPPACKBUILDER_NOTENOUGHBYTESLEFT;

	uint8_t *buf;
	size_t len;

	buf = RTPNew(GetMemoryManager(),RTPMEM_TYPE_BUFFER_RTCPSDESBLOCK) uint8_t[sizeof(RTCPSDESHeader)+itemlength];
	if (buf == 0)
		return ERR_RTP_OUTOFMEM;
	len = sizeof(RTCPSDESHeader)+(size_t)itemlength;

	RTCPSDESHeader *sdeshdr = (RTCPSDESHeader *)(buf);

	sdeshdr->sdesid = RTCP_SDES_ID_PRIVATE;
	sdeshdr->length = itemlength;
	
	buf[sizeof(RTCPSDESHeader)] = prefixlength;
	if (prefixlength != 0)
		memcpy((buf+sizeof(RTCPSDESHeader)+1),prefixdata,(size_t)prefixlength);
	if (valuelength != 0)
		memcpy((buf+sizeof(RTCPSDESHeader)+1+(size_t)prefixlength),valuedata,(size_t)valuelength);

	sdes.AddItem(buf,len);
	return 0;
}
#endif // RTP_SUPPORT_SDESPRIV

int RTCPCompoundPacketBuilder::AddBYEPacket(uint32_t *ssrcs,uint8_t numssrcs,const void *reasondata,uint8_t reasonlength)
{
	if (!arebuilding)
		return ERR_RTP_RTCPCOMPPACKBUILDER_NOTBUILDING;

	if (numssrcs > 31)
		return ERR_RTP_RTCPCOMPPACKBUILDER_TOOMANYSSRCS;
	
	size_t packsize = sizeof(RTCPCommonHeader)+sizeof(uint32_t)*((size_t)numssrcs);
	size_t zerobytes = 0;
	
	if (reasonlength > 0)
	{
		packsize += 1; // 1 byte for the length;
		packsize += (size_t)reasonlength;

		size_t r = (packsize&0x03);
		if (r != 0)
		{
			zerobytes = 4-r;
			packsize += zerobytes;
		}
	}

#ifndef RTP_SUPPORT_RTCPUNKNOWN
	size_t totalotherbytes = appsize+byesize+sdes.NeededBytes()+report.NeededBytes();
#else
	size_t totalotherbytes = appsize+unknownsize+byesize+sdes.NeededBytes()+report.NeededBytes();
#endif // RTP_SUPPORT_RTCPUNKNOWN 

	if ((totalotherbytes + packsize) > maximumpacketsize)
		return ERR_RTP_RTCPCOMPPACKBUILDER_NOTENOUGHBYTESLEFT;

	uint8_t *buf;
	size_t numwords;
	
	buf = RTPNew(GetMemoryManager(),RTPMEM_TYPE_BUFFER_RTCPBYEPACKET) uint8_t[packsize];
	if (buf == 0)
		return ERR_RTP_OUTOFMEM;

	RTCPCommonHeader *hdr = (RTCPCommonHeader *)buf;

	hdr->version = 2;
	hdr->padding = 0;
	hdr->count = numssrcs;
	
	numwords = packsize/sizeof(uint32_t);
	hdr->length = htons((uint16_t)(numwords-1));
	hdr->packettype = RTP_RTCPTYPE_BYE;
	
	uint32_t *sources = (uint32_t *)(buf+sizeof(RTCPCommonHeader));
	uint8_t srcindex;
	
	for (srcindex = 0 ; srcindex < numssrcs ; srcindex++)
		sources[srcindex] = htonl(ssrcs[srcindex]);

	if (reasonlength != 0)
	{
		size_t offset = sizeof(RTCPCommonHeader)+((size_t)numssrcs)*sizeof(uint32_t);

		buf[offset] = reasonlength;
		memcpy((buf+offset+1),reasondata,(size_t)reasonlength);
		for (size_t i = 0 ; i < zerobytes ; i++)
			buf[packsize-1-i] = 0;
	}

	byepackets.push_back(Buffer(buf,packsize));
	byesize += packsize;
	
	return 0;
}

int RTCPCompoundPacketBuilder::AddAPPPacket(uint8_t subtype,uint32_t ssrc,const uint8_t name[4],const void *appdata,size_t appdatalen)
{
	if (!arebuilding)
		return ERR_RTP_RTCPCOMPPACKBUILDER_NOTBUILDING;
	if (subtype > 31)
		return ERR_RTP_RTCPCOMPPACKBUILDER_ILLEGALSUBTYPE;
	if ((appdatalen%4) != 0)
		return ERR_RTP_RTCPCOMPPACKBUILDER_ILLEGALAPPDATALENGTH;

	size_t appdatawords = appdatalen/4;

	if ((appdatawords+2) > 65535)
		return ERR_RTP_RTCPCOMPPACKBUILDER_APPDATALENTOOBIG;
	
	size_t packsize = sizeof(RTCPCommonHeader)+sizeof(uint32_t)*2+appdatalen;
#ifndef RTP_SUPPORT_RTCPUNKNOWN
	size_t totalotherbytes = appsize+byesize+sdes.NeededBytes()+report.NeededBytes();
#else
	size_t totalotherbytes = appsize+unknownsize+byesize+sdes.NeededBytes()+report.NeededBytes();
#endif // RTP_SUPPORT_RTCPUNKNOWN 

	if ((totalotherbytes + packsize) > maximumpacketsize)
		return ERR_RTP_RTCPCOMPPACKBUILDER_NOTENOUGHBYTESLEFT;

	uint8_t *buf;
	
	buf = RTPNew(GetMemoryManager(),RTPMEM_TYPE_BUFFER_RTCPAPPPACKET) uint8_t[packsize];
	if (buf == 0)
		return ERR_RTP_OUTOFMEM;

	RTCPCommonHeader *hdr = (RTCPCommonHeader *)buf;

	hdr->version = 2;
	hdr->padding = 0;
	hdr->count = subtype;
	
	hdr->length = htons((uint16_t)(appdatawords+2));
	hdr->packettype = RTP_RTCPTYPE_APP;
	
	uint32_t *source = (uint32_t *)(buf+sizeof(RTCPCommonHeader));
	*source = htonl(ssrc);

	buf[sizeof(RTCPCommonHeader)+sizeof(uint32_t)+0] = name[0];
	buf[sizeof(RTCPCommonHeader)+sizeof(uint32_t)+1] = name[1];
	buf[sizeof(RTCPCommonHeader)+sizeof(uint32_t)+2] = name[2];
	buf[sizeof(RTCPCommonHeader)+sizeof(uint32_t)+3] = name[3];

	if (appdatalen > 0)
		memcpy((buf+sizeof(RTCPCommonHeader)+sizeof(uint32_t)*2),appdata,appdatalen);

	apppackets.push_back(Buffer(buf,packsize));
	appsize += packsize;
	
	return 0;
}

#ifdef RTP_SUPPORT_RTCPUNKNOWN

int RTCPCompoundPacketBuilder::AddUnknownPacket(uint8_t payload_type, uint8_t subtype, uint32_t ssrc, const void *data, size_t len)
{
	if (!arebuilding)
		return ERR_RTP_RTCPCOMPPACKBUILDER_NOTBUILDING;

	size_t datawords = len/4;

	if ((datawords+2) > 65535)
		return ERR_RTP_RTCPCOMPPACKBUILDER_APPDATALENTOOBIG;
	
	size_t packsize = sizeof(RTCPCommonHeader)+sizeof(uint32_t)+len;
	size_t totalotherbytes = appsize+unknownsize+byesize+sdes.NeededBytes()+report.NeededBytes();

	if ((totalotherbytes + packsize) > maximumpacketsize)
		return ERR_RTP_RTCPCOMPPACKBUILDER_NOTENOUGHBYTESLEFT;

	uint8_t *buf = RTPNew(GetMemoryManager(),RTPMEM_TYPE_CLASS_RTCPUNKNOWNPACKET) uint8_t[packsize];
	if (buf == 0)
		return ERR_RTP_OUTOFMEM;

	RTCPCommonHeader *hdr = (RTCPCommonHeader *)buf;

	hdr->version = 2;
	hdr->padding = 0;
	hdr->count = subtype;
	hdr->length = htons((uint16_t)(datawords+1));
	hdr->packettype = payload_type;
	
	uint32_t *source = (uint32_t *)(buf+sizeof(RTCPCommonHeader));
	*source = htonl(ssrc);

	if (len > 0)
		memcpy((buf+sizeof(RTCPCommonHeader)+sizeof(uint32_t)),data,len);

	unknownpackets.push_back(Buffer(buf,packsize));
	unknownsize += packsize;
	
	return 0;
}

#endif // RTP_SUPPORT_RTCPUNKNOWN 

int RTCPCompoundPacketBuilder::EndBuild()
{
	if (!arebuilding)
		return ERR_RTP_RTCPCOMPPACKBUILDER_NOTBUILDING;
	if (report.headerlength == 0)
		return ERR_RTP_RTCPCOMPPACKBUILDER_NOREPORTPRESENT;
	
	uint8_t *buf;
	size_t len;
	
#ifndef RTP_SUPPORT_RTCPUNKNOWN
	len = appsize+byesize+report.NeededBytes()+sdes.NeededBytes();
#else
	len = appsize+unknownsize+byesize+report.NeededBytes()+sdes.NeededBytes();
#endif // RTP_SUPPORT_RTCPUNKNOWN 
	
	if (!external)
	{
		buf = RTPNew(GetMemoryManager(),RTPMEM_TYPE_BUFFER_RTCPCOMPOUNDPACKET) uint8_t[len];
		if (buf == 0)
			return ERR_RTP_OUTOFMEM;
	}
	else
		buf = buffer;
	
	uint8_t *curbuf = buf;
	RTCPPacket *p;

	// first, we'll add all report info
	
	{
		bool firstpacket = true;
		bool done = false;
		std::list<Buffer>::const_iterator it = report.reportblocks.begin();
		do
		{
			RTCPCommonHeader *hdr = (RTCPCommonHeader *)curbuf;
			size_t offset;
			
			hdr->version = 2;
			hdr->padding = 0;

			if (firstpacket && report.isSR)
			{
				hdr->packettype = RTP_RTCPTYPE_SR;
				memcpy((curbuf+sizeof(RTCPCommonHeader)),report.headerdata,report.headerlength);
				offset = sizeof(RTCPCommonHeader)+report.headerlength;
			}
			else
			{
				hdr->packettype = RTP_RTCPTYPE_RR;
				memcpy((curbuf+sizeof(RTCPCommonHeader)),report.headerdata,sizeof(uint32_t));
				offset = sizeof(RTCPCommonHeader)+sizeof(uint32_t);
			}
			firstpacket = false;
			
			uint8_t count = 0;

			while (it != report.reportblocks.end() && count < 31)
			{
				memcpy(curbuf+offset,(*it).packetdata,(*it).packetlength);
				offset += (*it).packetlength;
				count++;
				it++;
			}

			size_t numwords = offset/sizeof(uint32_t);

			hdr->length = htons((uint16_t)(numwords-1));
			hdr->count = count;

			// add entry in parent's list
			if (hdr->packettype == RTP_RTCPTYPE_SR)
				p = RTPNew(GetMemoryManager(),RTPMEM_TYPE_CLASS_RTCPSRPACKET) RTCPSRPacket(curbuf,offset);
			else
				p = RTPNew(GetMemoryManager(),RTPMEM_TYPE_CLASS_RTCPRRPACKET) RTCPRRPacket(curbuf,offset);
			if (p == 0)
			{
				if (!external)
					RTPDeleteByteArray(buf,GetMemoryManager());
				ClearPacketList();
				return ERR_RTP_OUTOFMEM;
			}
			rtcppacklist.push_back(p);

			curbuf += offset;
			if (it == report.reportblocks.end())
				done = true;
		} while (!done);
	}
		
	// then, we'll add the sdes info

	if (!sdes.sdessources.empty())
	{
		bool done = false;
		std::list<SDESSource *>::const_iterator sourceit = sdes.sdessources.begin();
		
		do
		{
			RTCPCommonHeader *hdr = (RTCPCommonHeader *)curbuf;
			size_t offset = sizeof(RTCPCommonHeader);
			
			hdr->version = 2;
			hdr->padding = 0;
			hdr->packettype = RTP_RTCPTYPE_SDES;

			uint8_t sourcecount = 0;
			
			while (sourceit != sdes.sdessources.end() && sourcecount < 31)
			{
				uint32_t *ssrc = (uint32_t *)(curbuf+offset);
				*ssrc = htonl((*sourceit)->ssrc);
				offset += sizeof(uint32_t);
				
				std::list<Buffer>::const_iterator itemit,itemend;

				itemit = (*sourceit)->items.begin();
				itemend = (*sourceit)->items.end();
				while (itemit != itemend)
				{
					memcpy(curbuf+offset,(*itemit).packetdata,(*itemit).packetlength);
					offset += (*itemit).packetlength;
					itemit++;
				}

				curbuf[offset] = 0; // end of item list;
				offset++;

				size_t r = offset&0x03;
				if (r != 0) // align to 32 bit boundary
				{
					size_t num = 4-r;
					size_t i;

					for (i = 0 ; i < num ; i++)
						curbuf[offset+i] = 0;
					offset += num;
				}
				
				sourceit++;
				sourcecount++;
			}

			size_t numwords = offset/4;
			
			hdr->count = sourcecount;
			hdr->length = htons((uint16_t)(numwords-1));

			p = RTPNew(GetMemoryManager(),RTPMEM_TYPE_CLASS_RTCPSDESPACKET) RTCPSDESPacket(curbuf,offset);
			if (p == 0)
			{
				if (!external)
					RTPDeleteByteArray(buf,GetMemoryManager());
				ClearPacketList();
				return ERR_RTP_OUTOFMEM;
			}
			rtcppacklist.push_back(p);
			
			curbuf += offset;
			if (sourceit == sdes.sdessources.end())
				done = true;
		} while (!done);
	}
	
	// adding the app data
	
	{
		std::list<Buffer>::const_iterator it;

		for (it = apppackets.begin() ; it != apppackets.end() ; it++)
		{
			memcpy(curbuf,(*it).packetdata,(*it).packetlength);
			
			p = RTPNew(GetMemoryManager(),RTPMEM_TYPE_CLASS_RTCPAPPPACKET) RTCPAPPPacket(curbuf,(*it).packetlength);
			if (p == 0)
			{
				if (!external)
					RTPDeleteByteArray(buf,GetMemoryManager());
				ClearPacketList();
				return ERR_RTP_OUTOFMEM;
			}
			rtcppacklist.push_back(p);
	
			curbuf += (*it).packetlength;
		}
	}

#ifdef RTP_SUPPORT_RTCPUNKNOWN

	// adding the unknown data
	
	{
		std::list<Buffer>::const_iterator it;

		for (it = unknownpackets.begin() ; it != unknownpackets.end() ; it++)
		{
			memcpy(curbuf,(*it).packetdata,(*it).packetlength);
			
			p = RTPNew(GetMemoryManager(),RTPMEM_TYPE_CLASS_RTCPUNKNOWNPACKET) RTCPUnknownPacket(curbuf,(*it).packetlength);
			if (p == 0)
			{
				if (!external)
					RTPDeleteByteArray(buf,GetMemoryManager());
				ClearPacketList();
				return ERR_RTP_OUTOFMEM;
			}
			rtcppacklist.push_back(p);
	
			curbuf += (*it).packetlength;
		}
	}

#endif // RTP_SUPPORT_RTCPUNKNOWN 

	// adding bye packets
	
	{
		std::list<Buffer>::const_iterator it;

		for (it = byepackets.begin() ; it != byepackets.end() ; it++)
		{
			memcpy(curbuf,(*it).packetdata,(*it).packetlength);
			
			p = RTPNew(GetMemoryManager(),RTPMEM_TYPE_CLASS_RTCPBYEPACKET) RTCPBYEPacket(curbuf,(*it).packetlength);
			if (p == 0)
			{
				if (!external)
					RTPDeleteByteArray(buf,GetMemoryManager());
				ClearPacketList();
				return ERR_RTP_OUTOFMEM;
			}
			rtcppacklist.push_back(p);
	
			curbuf += (*it).packetlength;
		}
	}
	
	compoundpacket = buf;
	compoundpacketlength = len;
	arebuilding = false;
	ClearBuildBuffers();
	return 0;
}

} // end namespace