00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "liveMedia.hh"
00022 #include "BasicUsageEnvironment.hh"
00023 #include "GroupsockHelper.hh"
00024
00025
00026
00027
00028
00029
00030
00031 #ifdef USE_SSM
00032 Boolean const isSSM = True;
00033 #else
00034 Boolean const isSSM = False;
00035 #endif
00036
00037
00038
00039
00040
00041
00042
00043 Boolean iFramesOnly = False;
00044
00045 UsageEnvironment* env;
00046 char const* inputFileName = "test.mpg";
00047 #ifdef SOURCE_IS_PROGRAM_STREAM
00048 MPEG1or2Demux* mpegDemux;
00049 #endif
00050 MediaSource* videoSource;
00051 RTPSink* videoSink;
00052
00053 void play();
00054
00055 int main(int argc, char** argv) {
00056
00057 TaskScheduler* scheduler = BasicTaskScheduler::createNew();
00058 env = BasicUsageEnvironment::createNew(*scheduler);
00059
00060
00061 char const* destinationAddressStr
00062 #ifdef USE_SSM
00063 = "232.255.42.42";
00064 #else
00065 = "239.255.42.42";
00066
00067
00068
00069
00070 #endif
00071 const unsigned short rtpPortNum = 8888;
00072 const unsigned short rtcpPortNum = rtpPortNum+1;
00073 const unsigned char ttl = 7;
00074
00075 struct in_addr destinationAddress;
00076 destinationAddress.s_addr = our_inet_addr(destinationAddressStr);
00077 const Port rtpPort(rtpPortNum);
00078 const Port rtcpPort(rtcpPortNum);
00079
00080 Groupsock rtpGroupsock(*env, destinationAddress, rtpPort, ttl);
00081 Groupsock rtcpGroupsock(*env, destinationAddress, rtcpPort, ttl);
00082 #ifdef USE_SSM
00083 rtpGroupsock.multicastSendOnly();
00084 rtcpGroupsock.multicastSendOnly();
00085 #endif
00086
00087
00088 videoSink = MPEG1or2VideoRTPSink::createNew(*env, &rtpGroupsock);
00089
00090
00091 const unsigned estimatedSessionBandwidth = 4500;
00092 const unsigned maxCNAMElen = 100;
00093 unsigned char CNAME[maxCNAMElen+1];
00094 gethostname((char*)CNAME, maxCNAMElen);
00095 CNAME[maxCNAMElen] = '\0';
00096 #ifdef IMPLEMENT_RTSP_SERVER
00097 RTCPInstance* rtcp =
00098 #endif
00099 RTCPInstance::createNew(*env, &rtcpGroupsock,
00100 estimatedSessionBandwidth, CNAME,
00101 videoSink, NULL , isSSM);
00102
00103
00104 #ifdef IMPLEMENT_RTSP_SERVER
00105 RTSPServer* rtspServer = RTSPServer::createNew(*env);
00106
00107
00108
00109 if (rtspServer == NULL) {
00110 *env << "Failed to create RTSP server: " << env->getResultMsg() << "\n";
00111 exit(1);
00112 }
00113 ServerMediaSession* sms
00114 = ServerMediaSession::createNew(*env, "testStream", inputFileName,
00115 "Session streamed by \"testMPEG1or2VideoStreamer\"",
00116 isSSM);
00117 sms->addSubsession(PassiveServerMediaSubsession::createNew(*videoSink, rtcp));
00118 rtspServer->addServerMediaSession(sms);
00119
00120 char* url = rtspServer->rtspURL(sms);
00121 *env << "Play this stream using the URL \"" << url << "\"\n";
00122 delete[] url;
00123 #endif
00124
00125
00126 *env << "Beginning streaming...\n";
00127 play();
00128
00129 env->taskScheduler().doEventLoop();
00130
00131 return 0;
00132 }
00133
00134 void afterPlaying(void* ) {
00135 *env << "...done reading from file\n";
00136
00137 videoSink->stopPlaying();
00138 Medium::close(videoSource);
00139 #ifdef SOURCE_IS_PROGRAM_STREAM
00140 Medium::close(mpegDemux);
00141 #endif
00142
00143
00144 play();
00145 }
00146
00147 void play() {
00148
00149 ByteStreamFileSource* fileSource
00150 = ByteStreamFileSource::createNew(*env, inputFileName);
00151 if (fileSource == NULL) {
00152 *env << "Unable to open file \"" << inputFileName
00153 << "\" as a byte-stream file source\n";
00154 exit(1);
00155 }
00156
00157 FramedSource* videoES;
00158 #ifdef SOURCE_IS_PROGRAM_STREAM
00159
00160 mpegDemux = MPEG1or2Demux::createNew(*env, fileSource);
00161 videoES = mpegDemux->newVideoStream();
00162 #else
00163
00164 videoES = fileSource;
00165 #endif
00166
00167
00168 videoSource
00169 = MPEG1or2VideoStreamFramer::createNew(*env, videoES, iFramesOnly);
00170
00171
00172 *env << "Beginning to read from file...\n";
00173 videoSink->startPlaying(*videoSource, afterPlaying, videoSink);
00174 }