| 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 | // Transcoder for ADUized MP3 frames | 
|---|
| 19 | // Implementation | 
|---|
| 20 |  | 
|---|
| 21 | #include "MP3ADUTranscoder.hh" | 
|---|
| 22 | #include "MP3Internals.hh" | 
|---|
| 23 | #include <string.h> | 
|---|
| 24 |  | 
|---|
| 25 | MP3ADUTranscoder::MP3ADUTranscoder(UsageEnvironment& env, | 
|---|
| 26 | unsigned outBitrate /* in kbps */, | 
|---|
| 27 | FramedSource* inputSource) | 
|---|
| 28 | : FramedFilter(env, inputSource), | 
|---|
| 29 | fOutBitrate(outBitrate), | 
|---|
| 30 | fAvailableBytesForBackpointer(0), | 
|---|
| 31 | fOrigADU(new unsigned char[MAX_MP3_FRAME_SIZE]) { | 
|---|
| 32 | } | 
|---|
| 33 |  | 
|---|
| 34 | MP3ADUTranscoder::~MP3ADUTranscoder() { | 
|---|
| 35 | delete[] fOrigADU; | 
|---|
| 36 | } | 
|---|
| 37 |  | 
|---|
| 38 | MP3ADUTranscoder* MP3ADUTranscoder::createNew(UsageEnvironment& env, | 
|---|
| 39 | unsigned outBitrate /* in kbps */, | 
|---|
| 40 | FramedSource* inputSource) { | 
|---|
| 41 | // The source must be an MP3 ADU source: | 
|---|
| 42 | if (strcmp(inputSource->MIMEtype(), "audio/MPA-ROBUST") != 0) { | 
|---|
| 43 | env.setResultMsg(inputSource->name(), " is not an MP3 ADU source"); | 
|---|
| 44 | return NULL; | 
|---|
| 45 | } | 
|---|
| 46 |  | 
|---|
| 47 | return new MP3ADUTranscoder(env, outBitrate, inputSource); | 
|---|
| 48 | } | 
|---|
| 49 |  | 
|---|
| 50 | void MP3ADUTranscoder::getAttributes() const { | 
|---|
| 51 | // Begin by getting the attributes from our input source: | 
|---|
| 52 | fInputSource->getAttributes(); | 
|---|
| 53 |  | 
|---|
| 54 | // Then modify them by appending the corrected bandwidth | 
|---|
| 55 | char buffer[30]; | 
|---|
| 56 | sprintf(buffer, " bandwidth %d", outBitrate()); | 
|---|
| 57 | envir().appendToResultMsg(buffer); | 
|---|
| 58 | } | 
|---|
| 59 |  | 
|---|
| 60 | void MP3ADUTranscoder::doGetNextFrame() { | 
|---|
| 61 | fInputSource->getNextFrame(fOrigADU, MAX_MP3_FRAME_SIZE, | 
|---|
| 62 | afterGettingFrame, this, handleClosure, this); | 
|---|
| 63 | } | 
|---|
| 64 |  | 
|---|
| 65 | void MP3ADUTranscoder::afterGettingFrame(void* clientData, | 
|---|
| 66 | unsigned numBytesRead, | 
|---|
| 67 | unsigned numTruncatedBytes, | 
|---|
| 68 | struct timeval presentationTime, | 
|---|
| 69 | unsigned durationInMicroseconds) { | 
|---|
| 70 | MP3ADUTranscoder* transcoder = (MP3ADUTranscoder*)clientData; | 
|---|
| 71 | transcoder->afterGettingFrame1(numBytesRead, numTruncatedBytes, | 
|---|
| 72 | presentationTime, durationInMicroseconds); | 
|---|
| 73 | } | 
|---|
| 74 |  | 
|---|
| 75 | void MP3ADUTranscoder::afterGettingFrame1(unsigned numBytesRead, | 
|---|
| 76 | unsigned numTruncatedBytes, | 
|---|
| 77 | struct timeval presentationTime, | 
|---|
| 78 | unsigned durationInMicroseconds) { | 
|---|
| 79 | fNumTruncatedBytes = numTruncatedBytes; // but can we handle this being >0? ##### | 
|---|
| 80 | fPresentationTime = presentationTime; | 
|---|
| 81 | fDurationInMicroseconds = durationInMicroseconds; | 
|---|
| 82 | fFrameSize = TranscodeMP3ADU(fOrigADU, numBytesRead, fOutBitrate, | 
|---|
| 83 | fTo, fMaxSize, fAvailableBytesForBackpointer); | 
|---|
| 84 | if (fFrameSize == 0) { // internal error - bad ADU data? | 
|---|
| 85 | handleClosure(); | 
|---|
| 86 | return; | 
|---|
| 87 | } | 
|---|
| 88 |  | 
|---|
| 89 | // Call our own 'after getting' function.  Because we're not a 'leaf' | 
|---|
| 90 | // source, we can call this directly, without risking infinite recursion. | 
|---|
| 91 | afterGetting(this); | 
|---|
| 92 | } | 
|---|
| 93 |  | 
|---|