testProgs/testMP3Receiver.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 // Copyright (c) 1996-2012, Live Networks, Inc.  All rights reserved
00017 // A test program that receives a RTP/RTCP multicast MP3 stream,
00018 // and outputs the resulting MP3 file stream to 'stdout'
00019 // main program
00020 
00021 #include "liveMedia.hh"
00022 #include "GroupsockHelper.hh"
00023 
00024 #include "BasicUsageEnvironment.hh"
00025 
00026 // To receive a stream of 'ADUs' rather than raw MP3 frames, uncomment this:
00027 //#define STREAM_USING_ADUS 1
00028 // (For more information about ADUs and interleaving,
00029 //  see <http://www.live555.com/rtp-mp3/>)
00030 
00031 // To receive a "source-specific multicast" (SSM) stream, uncomment this:
00032 //#define USE_SSM 1
00033 
00034 void afterPlaying(void* clientData); // forward
00035 
00036 // A structure to hold the state of the current session.
00037 // It is used in the "afterPlaying()" function to clean up the session.
00038 struct sessionState_t {
00039   FramedSource* source;
00040   FileSink* sink;
00041   RTCPInstance* rtcpInstance;
00042 } sessionState;
00043 
00044 UsageEnvironment* env;
00045 
00046 int main(int argc, char** argv) {
00047   // Begin by setting up our usage environment:
00048   TaskScheduler* scheduler = BasicTaskScheduler::createNew();
00049   env = BasicUsageEnvironment::createNew(*scheduler);
00050 
00051   // Create the data sink for 'stdout':
00052   sessionState.sink = FileSink::createNew(*env, "stdout");
00053   // Note: The string "stdout" is handled as a special case.
00054   // A real file name could have been used instead.
00055 
00056   // Create 'groupsocks' for RTP and RTCP:
00057   char const* sessionAddressStr
00058 #ifdef USE_SSM
00059     = "232.255.42.42";
00060 #else
00061     = "239.255.42.42";
00062   // Note: If the session is unicast rather than multicast,
00063   // then replace this string with "0.0.0.0"
00064 #endif
00065   const unsigned short rtpPortNum = 6666;
00066   const unsigned short rtcpPortNum = rtpPortNum+1;
00067 #ifndef USE_SSM
00068   const unsigned char ttl = 1; // low, in case routers don't admin scope
00069 #endif
00070 
00071   struct in_addr sessionAddress;
00072   sessionAddress.s_addr = our_inet_addr(sessionAddressStr);
00073   const Port rtpPort(rtpPortNum);
00074   const Port rtcpPort(rtcpPortNum);
00075 
00076 #ifdef USE_SSM
00077   char* sourceAddressStr = "aaa.bbb.ccc.ddd";
00078                            // replace this with the real source address
00079   struct in_addr sourceFilterAddress;
00080   sourceFilterAddress.s_addr = our_inet_addr(sourceAddressStr);
00081 
00082   Groupsock rtpGroupsock(*env, sessionAddress, sourceFilterAddress, rtpPort);
00083   Groupsock rtcpGroupsock(*env, sessionAddress, sourceFilterAddress, rtcpPort);
00084   rtcpGroupsock.changeDestinationParameters(sourceFilterAddress,0,~0);
00085       // our RTCP "RR"s are sent back using unicast
00086 #else
00087   Groupsock rtpGroupsock(*env, sessionAddress, rtpPort, ttl);
00088   Groupsock rtcpGroupsock(*env, sessionAddress, rtcpPort, ttl);
00089 #endif
00090 
00091   RTPSource* rtpSource;
00092 #ifndef STREAM_USING_ADUS
00093   // Create the data source: a "MPEG Audio RTP source"
00094   rtpSource = MPEG1or2AudioRTPSource::createNew(*env, &rtpGroupsock);
00095 #else
00096   // Create the data source: a "MP3 *ADU* RTP source"
00097   unsigned char rtpPayloadFormat = 96; // a dynamic payload type
00098   rtpSource
00099     = MP3ADURTPSource::createNew(*env, &rtpGroupsock, rtpPayloadFormat);
00100 #endif
00101 
00102   // Create (and start) a 'RTCP instance' for the RTP source:
00103   const unsigned estimatedSessionBandwidth = 160; // in kbps; for RTCP b/w share
00104   const unsigned maxCNAMElen = 100;
00105   unsigned char CNAME[maxCNAMElen+1];
00106   gethostname((char*)CNAME, maxCNAMElen);
00107   CNAME[maxCNAMElen] = '\0'; // just in case
00108   sessionState.rtcpInstance
00109     = RTCPInstance::createNew(*env, &rtcpGroupsock,
00110                               estimatedSessionBandwidth, CNAME,
00111                               NULL /* we're a client */, rtpSource);
00112   // Note: This starts RTCP running automatically
00113 
00114   sessionState.source = rtpSource;
00115 #ifdef STREAM_USING_ADUS
00116   // Add a filter that deinterleaves the ADUs after depacketizing them:
00117   sessionState.source
00118     = MP3ADUdeinterleaver::createNew(*env, sessionState.source);
00119   if (sessionState.source == NULL) {
00120     *env << "Unable to create an ADU deinterleaving filter for the source\n";
00121     exit(1);
00122   }
00123 
00124   // Add another filter that converts these ADUs to MP3s:
00125   sessionState.source
00126     = MP3FromADUSource::createNew(*env, sessionState.source);
00127   if (sessionState.source == NULL) {
00128     *env << "Unable to create an ADU->MP3 filter for the source\n";
00129     exit(1);
00130   }
00131 #endif
00132 
00133   // Finally, start receiving the multicast stream:
00134   *env << "Beginning receiving multicast stream...\n";
00135   sessionState.sink->startPlaying(*sessionState.source, afterPlaying, NULL);
00136 
00137   env->taskScheduler().doEventLoop(); // does not return
00138 
00139   return 0; // only to prevent compiler warning
00140 }
00141 
00142 
00143 void afterPlaying(void* /*clientData*/) {
00144   *env << "...done receiving\n";
00145 
00146   // End by closing the media:
00147   Medium::close(sessionState.rtcpInstance); // Note: Sends a RTCP BYE
00148   Medium::close(sessionState.sink);
00149   Medium::close(sessionState.source);
00150 }

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