File: MP3ADURTPSink.cpp

package info (click to toggle)
liblivemedia 2006.03.17-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,928 kB
  • ctags: 4,588
  • sloc: cpp: 35,064; ansic: 979; makefile: 78; sh: 73
file content (119 lines) | stat: -rw-r--r-- 4,283 bytes parent folder | download
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
/**********
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. (See <http://www.gnu.org/copyleft/lesser.html>.)

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.,
59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
**********/
// "liveMedia"
// Copyright (c) 1996-2005 Live Networks, Inc.  All rights reserved.
// RTP sink for 'ADUized' MP3 frames ("mpa-robust")
// Implementation

#include "MP3ADURTPSink.hh"

MP3ADURTPSink::MP3ADURTPSink(UsageEnvironment& env, Groupsock* RTPgs,
			     unsigned char RTPPayloadType)
  : AudioRTPSink(env, RTPgs, RTPPayloadType, 90000, "MPA-ROBUST") {
}

MP3ADURTPSink::~MP3ADURTPSink() {
}

MP3ADURTPSink*
MP3ADURTPSink::createNew(UsageEnvironment& env, Groupsock* RTPgs,
			 unsigned char RTPPayloadType) {
  return new MP3ADURTPSink(env, RTPgs, RTPPayloadType);
}

static void badDataSize(UsageEnvironment& env, unsigned numBytesInFrame) {
  env << "MP3ADURTPSink::doSpecialFrameHandling(): invalid size ("
      << numBytesInFrame << ") of non-fragmented input ADU!\n";
}

void MP3ADURTPSink::doSpecialFrameHandling(unsigned fragmentationOffset,
					   unsigned char* frameStart,
					   unsigned numBytesInFrame,
					   struct timeval frameTimestamp,
					   unsigned numRemainingBytes) {
  // If this is the first (or only) fragment of an ADU, then
  // check the "ADU descriptor" (that should be at the front) for validity:
  if (fragmentationOffset == 0) {
    unsigned aduDescriptorSize;

    if (numBytesInFrame < 1) {
      badDataSize(envir(), numBytesInFrame);
      return;
    }
    if (frameStart[0]&0x40) {
      // We have a 2-byte ADU descriptor
      aduDescriptorSize = 2;
      if (numBytesInFrame < 2) {
	badDataSize(envir(), numBytesInFrame);
	return;
      }
      fCurADUSize = ((frameStart[0]&~0xC0)<<8) | frameStart[1];
    } else {
      // We have a 1-byte ADU descriptor
      aduDescriptorSize = 1;
      fCurADUSize = frameStart[0]&~0x80;
    }

    if (frameStart[0]&0x80) {
      envir() << "Unexpected \"C\" bit seen on non-fragment input ADU!\n";
      return;
    }

    // Now, check whether the ADU size in the ADU descriptor is consistent
    // with the total data size of (all fragments of) the input frame:
    unsigned expectedADUSize =
      fragmentationOffset + numBytesInFrame + numRemainingBytes
      - aduDescriptorSize;
    if (fCurADUSize != expectedADUSize) {
      envir() << "MP3ADURTPSink::doSpecialFrameHandling(): Warning: Input ADU size "
	      << expectedADUSize << " (=" << fragmentationOffset
	      << "+" << numBytesInFrame << "+" << numRemainingBytes
	      << "-" << aduDescriptorSize
	      << ") did not match the value (" << fCurADUSize
	      << ") in the ADU descriptor!\n";
      fCurADUSize = expectedADUSize;
    }
  } else {
    // This is the second (or subsequent) fragment.
    // Insert a new ADU descriptor:
    unsigned char aduDescriptor[2];
    aduDescriptor[0] = 0xC0|(fCurADUSize>>8);
    aduDescriptor[1] = fCurADUSize&0xFF;
    setSpecialHeaderBytes(aduDescriptor, 2);
  }

  // Important: Also call our base class's doSpecialFrameHandling(),
  // to set the packet's timestamp:
  MultiFramedRTPSink::doSpecialFrameHandling(fragmentationOffset,
					     frameStart, numBytesInFrame,
					     frameTimestamp,
					     numRemainingBytes);
}

unsigned MP3ADURTPSink::specialHeaderSize() const {
  // Normally there's no special header.
  // (The "ADU descriptor" is already present in the data.)
  unsigned specialHeaderSize = 0;
  
  // However, if we're about to output the second (or subsequent) fragment
  // of a fragmented ADU, then we need to insert a new ADU descriptor at
  // the front of the packet:
  if (curFragmentationOffset() > 0) {
    specialHeaderSize = 2;
  }

  return specialHeaderSize;
}