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 splits a MPEG-1 or 2 Program Stream file into 00018 // video and audio output files. 00019 // main program 00020 00021 #include "liveMedia.hh" 00022 #include "BasicUsageEnvironment.hh" 00023 #include <stdlib.h> 00024 00025 char const* inputFileName = "in.mpg"; 00026 char const* outputFileName_video = "out_video.mpg"; 00027 char const* outputFileName_audio = "out_audio.mpg"; 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 MPEG1or2Demux* baseDemultiplexor; 00035 MediaSource* videoSource; 00036 MediaSource* audioSource; 00037 FileSink* videoSink; 00038 FileSink* audioSink; 00039 } sessionState; 00040 00041 UsageEnvironment* env; 00042 00043 int main(int argc, char** argv) { 00044 // Begin by setting up our usage environment: 00045 TaskScheduler* scheduler = BasicTaskScheduler::createNew(); 00046 env = BasicUsageEnvironment::createNew(*scheduler); 00047 00048 // Open the input file as a 'byte-stream file source': 00049 ByteStreamFileSource* inputSource 00050 = ByteStreamFileSource::createNew(*env, inputFileName); 00051 if (inputSource == NULL) { 00052 *env << "Unable to open file \"" << inputFileName 00053 << "\" as a byte-stream file source\n"; 00054 exit(1); 00055 } 00056 00057 // Create a MPEG demultiplexor that reads from that source. 00058 sessionState.baseDemultiplexor = MPEG1or2Demux::createNew(*env, inputSource); 00059 00060 // Create, from this, our own sources (video and audio): 00061 sessionState.videoSource = sessionState.baseDemultiplexor->newVideoStream(); 00062 sessionState.audioSource = sessionState.baseDemultiplexor->newAudioStream(); 00063 00064 // Create the data sinks (output files): 00065 sessionState.videoSink = FileSink::createNew(*env, outputFileName_video); 00066 sessionState.audioSink = FileSink::createNew(*env, outputFileName_audio); 00067 00068 // Finally, start playing each sink. 00069 *env << "Beginning to read...\n"; 00070 sessionState.videoSink->startPlaying(*sessionState.videoSource, 00071 afterPlaying, sessionState.videoSink); 00072 sessionState.audioSink->startPlaying(*sessionState.audioSource, 00073 afterPlaying, sessionState.audioSink); 00074 00075 env->taskScheduler().doEventLoop(); // does not return 00076 00077 return 0; // only to prevent compiler warning 00078 } 00079 00080 void afterPlaying(void* clientData) { 00081 Medium* finishedSink = (Medium*)clientData; 00082 00083 if (finishedSink == sessionState.videoSink) { 00084 *env << "No more video\n"; 00085 Medium::close(sessionState.videoSink); 00086 Medium::close(sessionState.videoSource); 00087 sessionState.videoSink = NULL; 00088 } else if (finishedSink == sessionState.audioSink) { 00089 *env << "No more audio\n"; 00090 Medium::close(sessionState.audioSink); 00091 Medium::close(sessionState.audioSource); 00092 sessionState.audioSink = NULL; 00093 } 00094 00095 if (sessionState.videoSink == NULL && sessionState.audioSink == NULL) { 00096 *env << "...finished reading\n"; 00097 00098 Medium::close(sessionState.baseDemultiplexor); 00099 00100 exit(0); 00101 } 00102 }
1.5.2