liveMedia/MP3ADURTPSink.cpp

Go to the documentation of this file.
00001 /**********
00002 This library is free software; you can redistribute it and/or modify it under
00003 the terms of the GNU Lesser General Public License as published by the
00004 Free Software Foundation; either version 2.1 of the License, or (at your
00005 option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)
00006 
00007 This library is distributed in the hope that it will be useful, but WITHOUT
00008 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00009 FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
00010 more details.
00011 
00012 You should have received a copy of the GNU Lesser General Public License
00013 along with this library; if not, write to the Free Software Foundation, Inc.,
00014 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
00015 **********/
00016 // "liveMedia"
00017 // Copyright (c) 1996-2012 Live Networks, Inc.  All rights reserved.
00018 // RTP sink for 'ADUized' MP3 frames ("mpa-robust")
00019 // Implementation
00020 
00021 #include "MP3ADURTPSink.hh"
00022 
00023 MP3ADURTPSink::MP3ADURTPSink(UsageEnvironment& env, Groupsock* RTPgs,
00024                              unsigned char RTPPayloadType)
00025   : AudioRTPSink(env, RTPgs, RTPPayloadType, 90000, "MPA-ROBUST") {
00026 }
00027 
00028 MP3ADURTPSink::~MP3ADURTPSink() {
00029 }
00030 
00031 MP3ADURTPSink*
00032 MP3ADURTPSink::createNew(UsageEnvironment& env, Groupsock* RTPgs,
00033                          unsigned char RTPPayloadType) {
00034   return new MP3ADURTPSink(env, RTPgs, RTPPayloadType);
00035 }
00036 
00037 static void badDataSize(UsageEnvironment& env, unsigned numBytesInFrame) {
00038   env << "MP3ADURTPSink::doSpecialFrameHandling(): invalid size ("
00039       << numBytesInFrame << ") of non-fragmented input ADU!\n";
00040 }
00041 
00042 void MP3ADURTPSink::doSpecialFrameHandling(unsigned fragmentationOffset,
00043                                            unsigned char* frameStart,
00044                                            unsigned numBytesInFrame,
00045                                            struct timeval framePresentationTime,
00046                                            unsigned numRemainingBytes) {
00047   // If this is the first (or only) fragment of an ADU, then
00048   // check the "ADU descriptor" (that should be at the front) for validity:
00049   if (fragmentationOffset == 0) {
00050     unsigned aduDescriptorSize;
00051 
00052     if (numBytesInFrame < 1) {
00053       badDataSize(envir(), numBytesInFrame);
00054       return;
00055     }
00056     if (frameStart[0]&0x40) {
00057       // We have a 2-byte ADU descriptor
00058       aduDescriptorSize = 2;
00059       if (numBytesInFrame < 2) {
00060         badDataSize(envir(), numBytesInFrame);
00061         return;
00062       }
00063       fCurADUSize = ((frameStart[0]&~0xC0)<<8) | frameStart[1];
00064     } else {
00065       // We have a 1-byte ADU descriptor
00066       aduDescriptorSize = 1;
00067       fCurADUSize = frameStart[0]&~0x80;
00068     }
00069 
00070     if (frameStart[0]&0x80) {
00071       envir() << "Unexpected \"C\" bit seen on non-fragment input ADU!\n";
00072       return;
00073     }
00074 
00075     // Now, check whether the ADU size in the ADU descriptor is consistent
00076     // with the total data size of (all fragments of) the input frame:
00077     unsigned expectedADUSize =
00078       fragmentationOffset + numBytesInFrame + numRemainingBytes
00079       - aduDescriptorSize;
00080     if (fCurADUSize != expectedADUSize) {
00081       envir() << "MP3ADURTPSink::doSpecialFrameHandling(): Warning: Input ADU size "
00082               << expectedADUSize << " (=" << fragmentationOffset
00083               << "+" << numBytesInFrame << "+" << numRemainingBytes
00084               << "-" << aduDescriptorSize
00085               << ") did not match the value (" << fCurADUSize
00086               << ") in the ADU descriptor!\n";
00087       fCurADUSize = expectedADUSize;
00088     }
00089   } else {
00090     // This is the second (or subsequent) fragment.
00091     // Insert a new ADU descriptor:
00092     unsigned char aduDescriptor[2];
00093     aduDescriptor[0] = 0xC0|(fCurADUSize>>8);
00094     aduDescriptor[1] = fCurADUSize&0xFF;
00095     setSpecialHeaderBytes(aduDescriptor, 2);
00096   }
00097 
00098   // Important: Also call our base class's doSpecialFrameHandling(),
00099   // to set the packet's timestamp:
00100   MultiFramedRTPSink::doSpecialFrameHandling(fragmentationOffset,
00101                                              frameStart, numBytesInFrame,
00102                                              framePresentationTime,
00103                                              numRemainingBytes);
00104 }
00105 
00106 unsigned MP3ADURTPSink::specialHeaderSize() const {
00107   // Normally there's no special header.
00108   // (The "ADU descriptor" is already present in the data.)
00109   unsigned specialHeaderSize = 0;
00110 
00111   // However, if we're about to output the second (or subsequent) fragment
00112   // of a fragmented ADU, then we need to insert a new ADU descriptor at
00113   // the front of the packet:
00114   if (curFragmentationOffset() > 0) {
00115     specialHeaderSize = 2;
00116   }
00117 
00118   return specialHeaderSize;
00119 }

Generated on Thu May 17 07:11:46 2012 for live by  doxygen 1.5.2