testProgs/testH264VideoStreamer.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 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00015 **********/
00016 // Copyright (c) 1996-2014, Live Networks, Inc.  All rights reserved
00017 // A test program that reads a H.264 Elementary Stream video file
00018 // and streams it using RTP
00019 // main program
00020 //
00021 // NOTE: For this application to work, the H.264 Elementary Stream video file *must* contain SPS and PPS NAL units,
00022 // ideally at or near the start of the file.  These SPS and PPS NAL units are used to specify 'configuration' information
00023 // that is set in the output stream's SDP description (by the RTSP server that is built in to this application).
00024 // Note also that - unlike some other "*Streamer" demo applications - the resulting stream can be received only using a
00025 // RTSP client (such as "openRTSP")
00026 
00027 #include <liveMedia.hh>
00028 #include <BasicUsageEnvironment.hh>
00029 #include <GroupsockHelper.hh>
00030 
00031 UsageEnvironment* env;
00032 char const* inputFileName = "test.264";
00033 H264VideoStreamFramer* videoSource;
00034 RTPSink* videoSink;
00035 
00036 void play(); // forward
00037 
00038 int main(int argc, char** argv) {
00039   // Begin by setting up our usage environment:
00040   TaskScheduler* scheduler = BasicTaskScheduler::createNew();
00041   env = BasicUsageEnvironment::createNew(*scheduler);
00042 
00043   // Create 'groupsocks' for RTP and RTCP:
00044   struct in_addr destinationAddress;
00045   destinationAddress.s_addr = chooseRandomIPv4SSMAddress(*env);
00046   // Note: This is a multicast address.  If you wish instead to stream
00047   // using unicast, then you should use the "testOnDemandRTSPServer"
00048   // test program - not this test program - as a model.
00049 
00050   const unsigned short rtpPortNum = 18888;
00051   const unsigned short rtcpPortNum = rtpPortNum+1;
00052   const unsigned char ttl = 255;
00053 
00054   const Port rtpPort(rtpPortNum);
00055   const Port rtcpPort(rtcpPortNum);
00056 
00057   Groupsock rtpGroupsock(*env, destinationAddress, rtpPort, ttl);
00058   rtpGroupsock.multicastSendOnly(); // we're a SSM source
00059   Groupsock rtcpGroupsock(*env, destinationAddress, rtcpPort, ttl);
00060   rtcpGroupsock.multicastSendOnly(); // we're a SSM source
00061 
00062   // Create a 'H264 Video RTP' sink from the RTP 'groupsock':
00063   OutPacketBuffer::maxSize = 100000;
00064   videoSink = H264VideoRTPSink::createNew(*env, &rtpGroupsock, 96);
00065 
00066   // Create (and start) a 'RTCP instance' for this RTP sink:
00067   const unsigned estimatedSessionBandwidth = 500; // in kbps; for RTCP b/w share
00068   const unsigned maxCNAMElen = 100;
00069   unsigned char CNAME[maxCNAMElen+1];
00070   gethostname((char*)CNAME, maxCNAMElen);
00071   CNAME[maxCNAMElen] = '\0'; // just in case
00072   RTCPInstance* rtcp
00073   = RTCPInstance::createNew(*env, &rtcpGroupsock,
00074                             estimatedSessionBandwidth, CNAME,
00075                             videoSink, NULL /* we're a server */,
00076                             True /* we're a SSM source */);
00077   // Note: This starts RTCP running automatically
00078 
00079   RTSPServer* rtspServer = RTSPServer::createNew(*env, 8554);
00080   if (rtspServer == NULL) {
00081     *env << "Failed to create RTSP server: " << env->getResultMsg() << "\n";
00082     exit(1);
00083   }
00084   ServerMediaSession* sms
00085     = ServerMediaSession::createNew(*env, "testStream", inputFileName,
00086                    "Session streamed by \"testH264VideoStreamer\"",
00087                                            True /*SSM*/);
00088   sms->addSubsession(PassiveServerMediaSubsession::createNew(*videoSink, rtcp));
00089   rtspServer->addServerMediaSession(sms);
00090 
00091   char* url = rtspServer->rtspURL(sms);
00092   *env << "Play this stream using the URL \"" << url << "\"\n";
00093   delete[] url;
00094 
00095   // Start the streaming:
00096   *env << "Beginning streaming...\n";
00097   play();
00098 
00099   env->taskScheduler().doEventLoop(); // does not return
00100 
00101   return 0; // only to prevent compiler warning
00102 }
00103 
00104 void afterPlaying(void* /*clientData*/) {
00105   *env << "...done reading from file\n";
00106   videoSink->stopPlaying();
00107   Medium::close(videoSource);
00108   // Note that this also closes the input file that this source read from.
00109 
00110   // Start playing once again:
00111   play();
00112 }
00113 
00114 void play() {
00115   // Open the input file as a 'byte-stream file source':
00116   ByteStreamFileSource* fileSource
00117     = ByteStreamFileSource::createNew(*env, inputFileName);
00118   if (fileSource == NULL) {
00119     *env << "Unable to open file \"" << inputFileName
00120          << "\" as a byte-stream file source\n";
00121     exit(1);
00122   }
00123 
00124   FramedSource* videoES = fileSource;
00125 
00126   // Create a framer for the Video Elementary Stream:
00127   videoSource = H264VideoStreamFramer::createNew(*env, videoES);
00128 
00129   // Finally, start playing:
00130   *env << "Beginning to read from file...\n";
00131   videoSink->startPlaying(*videoSource, afterPlaying, videoSink);
00132 }

Generated on Tue Mar 25 14:35:36 2014 for live by  doxygen 1.5.2