liveMedia/MP3AudioFileServerMediaSubsession.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 // A 'ServerMediaSubsession' object that creates new, unicast, "RTPSink"s
00019 // on demand, from a MP3 audio file.
00020 // (Actually, any MPEG-1 or MPEG-2 audio file should work.)
00021 // Implementation
00022 
00023 #include "MP3AudioFileServerMediaSubsession.hh"
00024 #include "MPEG1or2AudioRTPSink.hh"
00025 #include "MP3ADURTPSink.hh"
00026 #include "MP3FileSource.hh"
00027 #include "MP3ADU.hh"
00028 
00029 MP3AudioFileServerMediaSubsession* MP3AudioFileServerMediaSubsession
00030 ::createNew(UsageEnvironment& env, char const* fileName, Boolean reuseFirstSource,
00031             Boolean generateADUs, Interleaving* interleaving) {
00032   return new MP3AudioFileServerMediaSubsession(env, fileName, reuseFirstSource,
00033                                                generateADUs, interleaving);
00034 }
00035 
00036 MP3AudioFileServerMediaSubsession
00037 ::MP3AudioFileServerMediaSubsession(UsageEnvironment& env,
00038                                     char const* fileName, Boolean reuseFirstSource,
00039                                     Boolean generateADUs,
00040                                     Interleaving* interleaving)
00041   : FileServerMediaSubsession(env, fileName, reuseFirstSource),
00042     fGenerateADUs(generateADUs), fInterleaving(interleaving), fFileDuration(0.0) {
00043 }
00044 
00045 MP3AudioFileServerMediaSubsession
00046 ::~MP3AudioFileServerMediaSubsession() {
00047   delete fInterleaving;
00048 }
00049 
00050 FramedSource* MP3AudioFileServerMediaSubsession
00051 ::createNewStreamSourceCommon(FramedSource* baseMP3Source, unsigned mp3NumBytes, unsigned& estBitrate) {
00052   FramedSource* streamSource;
00053   do {
00054     streamSource = baseMP3Source; // by default
00055     if (streamSource == NULL) break;
00056 
00057     // Use the MP3 file size, plus the duration, to estimate the stream's bitrate:
00058     if (mp3NumBytes > 0 && fFileDuration > 0.0) {
00059       estBitrate = (unsigned)(mp3NumBytes/(125*fFileDuration) + 0.5); // kbps, rounded
00060     } else {
00061       estBitrate = 128; // kbps, estimate
00062     }
00063 
00064     if (fGenerateADUs) {
00065       // Add a filter that converts the source MP3s to ADUs:
00066       streamSource = ADUFromMP3Source::createNew(envir(), streamSource);
00067       if (streamSource == NULL) break;
00068 
00069       if (fInterleaving != NULL) {
00070         // Add another filter that interleaves the ADUs before packetizing:
00071         streamSource = MP3ADUinterleaver::createNew(envir(), *fInterleaving,
00072                                                     streamSource);
00073         if (streamSource == NULL) break;
00074       }
00075     } else if (fFileDuration > 0.0) {
00076       // Because this is a seekable file, insert a pair of filters: one that
00077       // converts the input MP3 stream to ADUs; another that converts these
00078       // ADUs back to MP3.  This allows us to seek within the input stream without
00079       // tripping over the MP3 'bit reservoir':
00080       streamSource = ADUFromMP3Source::createNew(envir(), streamSource);
00081       if (streamSource == NULL) break;
00082 
00083       streamSource = MP3FromADUSource::createNew(envir(), streamSource);
00084       if (streamSource == NULL) break;
00085     }
00086   } while (0);
00087 
00088   return streamSource;
00089 }
00090 
00091 void MP3AudioFileServerMediaSubsession::getBaseStreams(FramedSource* frontStream,
00092                                                        FramedSource*& sourceMP3Stream, ADUFromMP3Source*& aduStream/*if any*/) {
00093   if (fGenerateADUs) {
00094     // There's an ADU stream.
00095     if (fInterleaving != NULL) {
00096       // There's an interleaving filter in front of the ADU stream.  So go back one, to reach the ADU stream:
00097       aduStream = (ADUFromMP3Source*)(((FramedFilter*)frontStream)->inputSource());
00098     } else {
00099       aduStream = (ADUFromMP3Source*)frontStream;
00100     }
00101 
00102     // Then, go back one more, to reach the MP3 source:
00103     sourceMP3Stream = (MP3FileSource*)(aduStream->inputSource());
00104   } else if (fFileDuration > 0.0) {
00105     // There are a pair of filters - MP3->ADU and ADU->MP3 - in front of the
00106     // original MP3 source.  So, go back one, to reach the ADU source:
00107     aduStream = (ADUFromMP3Source*)(((FramedFilter*)frontStream)->inputSource());
00108 
00109     // Then, go back one more, to reach the MP3 source:
00110     sourceMP3Stream = (MP3FileSource*)(aduStream->inputSource());
00111   } else {
00112     // There's no filter in front of the source MP3 stream (and there's no ADU stream):
00113     aduStream = NULL;
00114     sourceMP3Stream = frontStream;
00115   }
00116 }
00117 
00118 
00119 void MP3AudioFileServerMediaSubsession
00120 ::seekStreamSource(FramedSource* inputSource, double& seekNPT, double streamDuration, u_int64_t& /*numBytes*/) {
00121   FramedSource* sourceMP3Stream;
00122   ADUFromMP3Source* aduStream;
00123   getBaseStreams(inputSource, sourceMP3Stream, aduStream);
00124 
00125   if (aduStream != NULL) aduStream->resetInput(); // because we're about to seek within its source
00126   ((MP3FileSource*)sourceMP3Stream)->seekWithinFile(seekNPT, streamDuration);
00127 }
00128 
00129 void MP3AudioFileServerMediaSubsession
00130 ::setStreamSourceScale(FramedSource* inputSource, float scale) {
00131 
00132   FramedSource* sourceMP3Stream;
00133   ADUFromMP3Source* aduStream;
00134   getBaseStreams(inputSource, sourceMP3Stream, aduStream);
00135 
00136   if (aduStream == NULL) return; // because, in this case, the stream's not scalable
00137 
00138   int iScale = (int)scale;
00139   aduStream->setScaleFactor(iScale);
00140   ((MP3FileSource*)sourceMP3Stream)->setPresentationTimeScale(iScale);
00141 }
00142 
00143 FramedSource* MP3AudioFileServerMediaSubsession
00144 ::createNewStreamSource(unsigned /*clientSessionId*/, unsigned& estBitrate) {
00145   MP3FileSource* mp3Source = MP3FileSource::createNew(envir(), fFileName);
00146   if (mp3Source == NULL) return NULL;
00147   fFileDuration = mp3Source->filePlayTime();
00148 
00149   return createNewStreamSourceCommon(mp3Source, mp3Source->fileSize(), estBitrate);
00150 }
00151 
00152 RTPSink* MP3AudioFileServerMediaSubsession
00153 ::createNewRTPSink(Groupsock* rtpGroupsock,
00154                    unsigned char rtpPayloadTypeIfDynamic,
00155                    FramedSource* /*inputSource*/) {
00156   if (fGenerateADUs) {
00157     return MP3ADURTPSink::createNew(envir(), rtpGroupsock,
00158                                     rtpPayloadTypeIfDynamic);
00159   } else {
00160     return MPEG1or2AudioRTPSink::createNew(envir(), rtpGroupsock);
00161   }
00162 }
00163 
00164 void MP3AudioFileServerMediaSubsession::testScaleFactor(float& scale) {
00165   if (fFileDuration <= 0.0) {
00166     // The file is non-seekable, so is probably a live input source.
00167     // We don't support scale factors other than 1
00168     scale = 1;
00169   } else {
00170     // We support any integral scale >= 1
00171     int iScale = (int)(scale + 0.5); // round
00172     if (iScale < 1) iScale = 1;
00173     scale = (float)iScale;
00174   }
00175 }
00176 
00177 float MP3AudioFileServerMediaSubsession::duration() const {
00178   return fFileDuration;
00179 }

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