testProgs/testReplicator.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-2013, Live Networks, Inc.  All rights reserved
00017 // A demo application that receives a UDP multicast stream, replicates it (using the "FrameReplicator" class),
00018 // and retransmits one replica stream to another (multicast or unicast) address & port,
00019 // and writes the other replica stream to a file.
00020 //
00021 // main program
00022 
00023 #include <liveMedia.hh>
00024 #include "BasicUsageEnvironment.hh"
00025 #include "GroupsockHelper.hh"
00026 
00027 UsageEnvironment* env;
00028 
00029 // To receive a "source-specific multicast" (SSM) stream, uncomment this:
00030 //#define USE_SSM 1
00031 
00032 void startReplicaUDPSink(StreamReplicator* replicator, char const* outputAddressStr, portNumBits outputPortNum); // forward
00033 void startReplicaFileSink(StreamReplicator* replicator, char const* outputFileName); // forward
00034 
00035 int main(int argc, char** argv) {
00036   // Begin by setting up our usage environment:
00037   TaskScheduler* scheduler = BasicTaskScheduler::createNew();
00038   env = BasicUsageEnvironment::createNew(*scheduler);
00039 
00040   // Create a 'groupsock' for the input multicast group,port:
00041   char const* inputAddressStr
00042 #ifdef USE_SSM
00043     = "232.255.42.42";
00044 #else
00045     = "239.255.42.42";
00046 #endif
00047   struct in_addr inputAddress;
00048   inputAddress.s_addr = our_inet_addr(inputAddressStr);
00049 
00050   Port const inputPort(8888);
00051   unsigned char const inputTTL = 0; // we're only reading from this mcast group
00052 
00053 #ifdef USE_SSM
00054   char* sourceAddressStr = "aaa.bbb.ccc.ddd";
00055                            // replace this with the real source address
00056   struct in_addr sourceFilterAddress;
00057   sourceFilterAddress.s_addr = our_inet_addr(sourceAddressStr);
00058 
00059   Groupsock inputGroupsock(*env, inputAddress, sourceFilterAddress, inputPort);
00060 #else
00061   Groupsock inputGroupsock(*env, inputAddress, inputPort, inputTTL);
00062 #endif
00063 
00064   // Then create a liveMedia 'source' object, encapsulating this groupsock:
00065   FramedSource* source = BasicUDPSource::createNew(*env, &inputGroupsock);
00066 
00067   // And feed this into a 'stream replicator':
00068   StreamReplicator* replicator = StreamReplicator::createNew(*env, source);
00069 
00070   // Then create a network (UDP) 'sink' object to receive a replica of the input stream, and start it.
00071   // If you wish, you can duplicate this line - with different network addresses and ports - to create multiple output UDP streams:
00072   startReplicaUDPSink(replicator, "239.255.43.43", 4444);
00073 
00074   // Then create a file 'sink' object to receive a replica of the input stream, and start it.
00075   // If you wish, you can duplicate this line - with a different file name - to create multiple output files:
00076   startReplicaFileSink(replicator, "test.out");
00077 
00078   // Finally, enter the 'event loop' (which is where most of the 'real work' in a LIVE555-based application gets done):
00079   env->taskScheduler().doEventLoop(); // does not return
00080 
00081   return 0; // only to prevent compiler warning
00082 }
00083 
00084 void startReplicaUDPSink(StreamReplicator* replicator, char const* outputAddressStr, portNumBits outputPortNum) {
00085   // Begin by creating an input stream from our replicator:
00086   FramedSource* source = replicator->createStreamReplica();
00087 
00088   // Create a 'groupsock' for the destination address and port:
00089   struct in_addr outputAddress;
00090   outputAddress.s_addr = our_inet_addr(outputAddressStr);
00091 
00092   Port const outputPort(outputPortNum);
00093   unsigned char const outputTTL = 255;
00094 
00095   Groupsock* outputGroupsock = new Groupsock(*env, outputAddress, outputPort, outputTTL);
00096 
00097   // Then create a liveMedia 'sink' object, encapsulating this groupsock:
00098   unsigned const maxPacketSize = 65536; // allow for large UDP packets
00099   MediaSink* sink = BasicUDPSink::createNew(*env, outputGroupsock, maxPacketSize);
00100 
00101   // Now, start playing, feeding the sink object from the source:
00102   sink->startPlaying(*source, NULL, NULL);
00103 }
00104 
00105 void startReplicaFileSink(StreamReplicator* replicator, char const* outputFileName) {
00106   // Begin by creating an input stream from our replicator:
00107   FramedSource* source = replicator->createStreamReplica();
00108 
00109   // Then create a 'file sink' object to receive thie replica stream:
00110   MediaSink* sink = FileSink::createNew(*env, outputFileName);
00111 
00112   // Now, start playing, feeding the sink object from the source:
00113   sink->startPlaying(*source, NULL, NULL);
00114 }

Generated on Mon Apr 29 13:28:04 2013 for live by  doxygen 1.5.2