testProgs/testMPEG2TransportReceiver.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-2014, Live Networks, Inc.  All rights reserved
00017 // A test program that receives a RTP/RTCP multicast MPEG-2 Transport Stream,
00018 // and outputs the resulting Transport Stream data to 'stdout'
00019 // main program
00020 
00021 #include "liveMedia.hh"
00022 #include "GroupsockHelper.hh"
00023 
00024 #include "BasicUsageEnvironment.hh"
00025 
00026 // To receive a "source-specific multicast" (SSM) stream, uncomment this:
00027 //#define USE_SSM 1
00028 
00029 void afterPlaying(void* clientData); // forward
00030 
00031 // A structure to hold the state of the current session.
00032 // It is used in the "afterPlaying()" function to clean up the session.
00033 struct sessionState_t {
00034   RTPSource* source;
00035   MediaSink* sink;
00036   RTCPInstance* rtcpInstance;
00037 } sessionState;
00038 
00039 UsageEnvironment* env;
00040 
00041 int main(int argc, char** argv) {
00042   // Begin by setting up our usage environment:
00043   TaskScheduler* scheduler = BasicTaskScheduler::createNew();
00044   env = BasicUsageEnvironment::createNew(*scheduler);
00045 
00046   // Create the data sink for 'stdout':
00047   sessionState.sink = FileSink::createNew(*env, "stdout");
00048   // Note: The string "stdout" is handled as a special case.
00049   // A real file name could have been used instead.
00050 
00051   // Create 'groupsocks' for RTP and RTCP:
00052   char const* sessionAddressStr
00053 #ifdef USE_SSM
00054     = "232.255.42.42";
00055 #else
00056     = "239.255.42.42";
00057   // Note: If the session is unicast rather than multicast,
00058   // then replace this string with "0.0.0.0"
00059 #endif
00060   const unsigned short rtpPortNum = 1234;
00061   const unsigned short rtcpPortNum = rtpPortNum+1;
00062 #ifndef USE_SSM
00063   const unsigned char ttl = 1; // low, in case routers don't admin scope
00064 #endif
00065 
00066   struct in_addr sessionAddress;
00067   sessionAddress.s_addr = our_inet_addr(sessionAddressStr);
00068   const Port rtpPort(rtpPortNum);
00069   const Port rtcpPort(rtcpPortNum);
00070 
00071 #ifdef USE_SSM
00072   char* sourceAddressStr = "aaa.bbb.ccc.ddd";
00073                            // replace this with the real source address
00074   struct in_addr sourceFilterAddress;
00075   sourceFilterAddress.s_addr = our_inet_addr(sourceAddressStr);
00076 
00077   Groupsock rtpGroupsock(*env, sessionAddress, sourceFilterAddress, rtpPort);
00078   Groupsock rtcpGroupsock(*env, sessionAddress, sourceFilterAddress, rtcpPort);
00079   rtcpGroupsock.changeDestinationParameters(sourceFilterAddress,0,~0);
00080       // our RTCP "RR"s are sent back using unicast
00081 #else
00082   Groupsock rtpGroupsock(*env, sessionAddress, rtpPort, ttl);
00083   Groupsock rtcpGroupsock(*env, sessionAddress, rtcpPort, ttl);
00084 #endif
00085 
00086   // Create the data source: a "MPEG-2 TransportStream RTP source" (which uses a 'simple' RTP payload format):
00087   sessionState.source = SimpleRTPSource::createNew(*env, &rtpGroupsock, 33, 90000, "video/MP2T", 0, False /*no 'M' bit*/);
00088 
00089   // Create (and start) a 'RTCP instance' for the RTP source:
00090   const unsigned estimatedSessionBandwidth = 160; // in kbps; for RTCP b/w share
00091   const unsigned maxCNAMElen = 100;
00092   unsigned char CNAME[maxCNAMElen+1];
00093   gethostname((char*)CNAME, maxCNAMElen);
00094   CNAME[maxCNAMElen] = '\0'; // just in case
00095   sessionState.rtcpInstance
00096     = RTCPInstance::createNew(*env, &rtcpGroupsock,
00097                               estimatedSessionBandwidth, CNAME,
00098                               NULL /* we're a client */, sessionState.source);
00099   // Note: This starts RTCP running automatically
00100 
00101   // Finally, start receiving the multicast stream:
00102   *env << "Beginning receiving multicast stream...\n";
00103   sessionState.sink->startPlaying(*sessionState.source, afterPlaying, NULL);
00104 
00105   env->taskScheduler().doEventLoop(); // does not return
00106 
00107   return 0; // only to prevent compiler warning
00108 }
00109 
00110 
00111 void afterPlaying(void* /*clientData*/) {
00112   *env << "...done receiving\n";
00113 
00114   // End by closing the media:
00115   Medium::close(sessionState.rtcpInstance); // Note: Sends a RTCP BYE
00116   Medium::close(sessionState.sink);
00117   Medium::close(sessionState.source);
00118 }

Generated on Wed Apr 23 16:12:01 2014 for live by  doxygen 1.5.2