| 1 | /********** |
| 2 | This library is free software; you can redistribute it and/or modify it under |
| 3 | the terms of the GNU Lesser General Public License as published by the |
| 4 | Free Software Foundation; either version 3 of the License, or (at your |
| 5 | option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.) |
| 6 | |
| 7 | This library is distributed in the hope that it will be useful, but WITHOUT |
| 8 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 9 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for |
| 10 | more details. |
| 11 | |
| 12 | You should have received a copy of the GNU Lesser General Public License |
| 13 | along with this library; if not, write to the Free Software Foundation, Inc., |
| 14 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 15 | **********/ |
| 16 | // "liveMedia" |
| 17 | // Copyright (c) 1996-2020 Live Networks, Inc. All rights reserved. |
| 18 | // RTP sink for 'ADUized' MP3 frames ("mpa-robust") |
| 19 | // Implementation |
| 20 | |
| 21 | #include "MP3ADURTPSink.hh" |
| 22 | |
| 23 | MP3ADURTPSink::MP3ADURTPSink(UsageEnvironment& env, Groupsock* RTPgs, |
| 24 | unsigned char RTPPayloadType) |
| 25 | : AudioRTPSink(env, RTPgs, RTPPayloadType, 90000, "MPA-ROBUST" ) { |
| 26 | } |
| 27 | |
| 28 | MP3ADURTPSink::~MP3ADURTPSink() { |
| 29 | } |
| 30 | |
| 31 | MP3ADURTPSink* |
| 32 | MP3ADURTPSink::createNew(UsageEnvironment& env, Groupsock* RTPgs, |
| 33 | unsigned char RTPPayloadType) { |
| 34 | return new MP3ADURTPSink(env, RTPgs, RTPPayloadType); |
| 35 | } |
| 36 | |
| 37 | static void badDataSize(UsageEnvironment& env, unsigned numBytesInFrame) { |
| 38 | env << "MP3ADURTPSink::doSpecialFrameHandling(): invalid size (" |
| 39 | << numBytesInFrame << ") of non-fragmented input ADU!\n" ; |
| 40 | } |
| 41 | |
| 42 | void MP3ADURTPSink::doSpecialFrameHandling(unsigned fragmentationOffset, |
| 43 | unsigned char* frameStart, |
| 44 | unsigned numBytesInFrame, |
| 45 | struct timeval framePresentationTime, |
| 46 | unsigned numRemainingBytes) { |
| 47 | // If this is the first (or only) fragment of an ADU, then |
| 48 | // check the "ADU descriptor" (that should be at the front) for validity: |
| 49 | if (fragmentationOffset == 0) { |
| 50 | unsigned aduDescriptorSize; |
| 51 | |
| 52 | if (numBytesInFrame < 1) { |
| 53 | badDataSize(envir(), numBytesInFrame); |
| 54 | return; |
| 55 | } |
| 56 | if (frameStart[0]&0x40) { |
| 57 | // We have a 2-byte ADU descriptor |
| 58 | aduDescriptorSize = 2; |
| 59 | if (numBytesInFrame < 2) { |
| 60 | badDataSize(envir(), numBytesInFrame); |
| 61 | return; |
| 62 | } |
| 63 | fCurADUSize = ((frameStart[0]&~0xC0)<<8) | frameStart[1]; |
| 64 | } else { |
| 65 | // We have a 1-byte ADU descriptor |
| 66 | aduDescriptorSize = 1; |
| 67 | fCurADUSize = frameStart[0]&~0x80; |
| 68 | } |
| 69 | |
| 70 | if (frameStart[0]&0x80) { |
| 71 | envir() << "Unexpected \"C\" bit seen on non-fragment input ADU!\n" ; |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | // Now, check whether the ADU size in the ADU descriptor is consistent |
| 76 | // with the total data size of (all fragments of) the input frame: |
| 77 | unsigned expectedADUSize = |
| 78 | fragmentationOffset + numBytesInFrame + numRemainingBytes |
| 79 | - aduDescriptorSize; |
| 80 | if (fCurADUSize != expectedADUSize) { |
| 81 | envir() << "MP3ADURTPSink::doSpecialFrameHandling(): Warning: Input ADU size " |
| 82 | << expectedADUSize << " (=" << fragmentationOffset |
| 83 | << "+" << numBytesInFrame << "+" << numRemainingBytes |
| 84 | << "-" << aduDescriptorSize |
| 85 | << ") did not match the value (" << fCurADUSize |
| 86 | << ") in the ADU descriptor!\n" ; |
| 87 | fCurADUSize = expectedADUSize; |
| 88 | } |
| 89 | } else { |
| 90 | // This is the second (or subsequent) fragment. |
| 91 | // Insert a new ADU descriptor: |
| 92 | unsigned char aduDescriptor[2]; |
| 93 | aduDescriptor[0] = 0xC0|(fCurADUSize>>8); |
| 94 | aduDescriptor[1] = fCurADUSize&0xFF; |
| 95 | setSpecialHeaderBytes(aduDescriptor, 2); |
| 96 | } |
| 97 | |
| 98 | // Important: Also call our base class's doSpecialFrameHandling(), |
| 99 | // to set the packet's timestamp: |
| 100 | MultiFramedRTPSink::doSpecialFrameHandling(fragmentationOffset, |
| 101 | frameStart, numBytesInFrame, |
| 102 | framePresentationTime, |
| 103 | numRemainingBytes); |
| 104 | } |
| 105 | |
| 106 | unsigned MP3ADURTPSink::() const { |
| 107 | // Normally there's no special header. |
| 108 | // (The "ADU descriptor" is already present in the data.) |
| 109 | unsigned = 0; |
| 110 | |
| 111 | // However, if we're about to output the second (or subsequent) fragment |
| 112 | // of a fragmented ADU, then we need to insert a new ADU descriptor at |
| 113 | // the front of the packet: |
| 114 | if (curFragmentationOffset() > 0) { |
| 115 | specialHeaderSize = 2; |
| 116 | } |
| 117 | |
| 118 | return specialHeaderSize; |
| 119 | } |
| 120 | |