RTPApplication.cc

Go to the documentation of this file.
00001 /***************************************************************************
00002                        RTPApplication.cpp  -  description
00003                              -------------------
00004     (C) 2007 Ahmed Ayadi  <ahmed.ayadi@sophia.inria.fr>
00005     (C) 2001 Matthias Oppitz <Matthias.Oppitz@gmx.de>
00006  ***************************************************************************/
00007 
00008 /***************************************************************************
00009  *                                                                         *
00010  *   This program is free software; you can redistribute it and/or modify  *
00011  *   it under the terms of the GNU General Public License as published by  *
00012  *   the Free Software Foundation; either version 2 of the License, or     *
00013  *   (at your option) any later version.                                   *
00014  *                                                                         *
00015  ***************************************************************************/
00016 
00017 
00018 #include "IPAddress.h"
00019 #include "RTPApplication.h"
00020 #include "RTPInterfacePacket.h"
00021 
00022 Define_Module(RTPApplication)
00023 
00024 
00025 void RTPApplication::initialize()
00026 {
00027 
00028     // read all omnet parameters
00029 
00030     // the common name (CNAME) of this host
00031     _commonName = par("commonName");
00032 
00033     // which rtp profile is to be used (usually RTPAVProfile)
00034     _profileName = par("profileName");
00035 
00036     // bandwidth in bytes per second for this session
00037     _bandwidth = par("bandwidth");
00038 
00039     // the ip address to connect to (unicast or multicast)
00040     _destinationAddress = IPAddress(par("destinationAddress").stringValue());
00041 
00042     // port number which is to be used; to ports are actually used: one
00043     // for rtp and one for rtcp
00044     _port = (int)par("portNumber").longValue();
00045 
00046     // fileName of file to be transmitted
00047     // NULL or "" means this system acts only as a receiver
00048     _fileName = par("fileName");
00049 
00050     // payload type of file to transmit
00051     _payloadType = par("payloadType");
00052 
00053     _sessionEnterDelay = par("sessionEnterDelay");
00054     _transmissionStartDelay = par("transmissionStartDelay");
00055     _transmissionStopDelay = par("transmissionStopDelay");
00056     _sessionLeaveDelay = par("sessionLeaveDelay");
00057 
00058     ev<< "commonName" <<  _commonName <<endl;
00059     ev<< "profileName" <<  _profileName <<endl;
00060     ev<< "bandwidth" <<  _bandwidth <<endl;
00061     ev<< "destinationAddress" <<  _destinationAddress <<endl;
00062     ev<< "portNumber" <<  _port <<endl;
00063     ev<< "fileName" <<  _fileName <<endl;
00064     ev<< "payloadType" <<  _payloadType <<endl;
00065 }
00066 
00067 
00068 void RTPApplication::activity()
00069 {
00070 
00071 
00072     bool sessionEntered = false;
00073     bool transmissionStarted = false;
00074     bool transmissionFinished = false;
00075     bool sessionLeft = false;
00076 
00077     cMessage *msg1 = new cMessage("enterSession");
00078     scheduleAt(simTime() + _sessionEnterDelay, msg1);
00079 
00080     uint32 ssrc = 0;
00081 
00082     while (!sessionLeft) {
00083 
00084         cMessage *msgIn = receive();
00085         if (msgIn->isSelfMessage()) {
00086             if (!opp_strcmp(msgIn->getName(), "enterSession")) {
00087                 ev << "enterSession"<<endl;
00088                 // create an RTPInterfacePacket to enter the session
00089                 RTPInterfacePacket *rifpOut1 = new RTPInterfacePacket("enterSession()");
00090                 rifpOut1->enterSession(opp_strdup(_commonName), opp_strdup(_profileName), _bandwidth, _destinationAddress, _port);
00091                 // and send it to the rtp layer
00092                 send(rifpOut1, "rtpOut");
00093             }
00094             else if (!opp_strcmp(msgIn->getName(), "startTransmission")) {
00095                 ev << "startTransmission"<<endl;
00096                 RTPSenderControlMessage *rscm = new RTPSenderControlMessage();
00097                 rscm->setCommand("PLAY");
00098                 RTPInterfacePacket *rifpOut = new RTPInterfacePacket("senderModuleControl(PLAY)");
00099                 rifpOut->senderModuleControl(ssrc, rscm);
00100                 send(rifpOut, "rtpOut");
00101                 transmissionStarted = true;
00102 
00103                 cMessage *msg4 = new cMessage("stopTransmission");
00104                 scheduleAt(simTime() + _transmissionStopDelay, msg4);
00105             }
00106             else if (!opp_strcmp(msgIn->getName(), "stopTransmission")) {
00107                 ev << "stopTransmission"<<endl;
00108                 RTPSenderControlMessage *rscm = new RTPSenderControlMessage();
00109                 rscm->setCommand("STOP");
00110                 RTPInterfacePacket *rifpOut = new RTPInterfacePacket("senderModuleControl(STOP)");
00111                 rifpOut->senderModuleControl(ssrc, rscm);
00112                 send(rifpOut, "rtpOut");
00113             }
00114             else if (!opp_strcmp(msgIn->getName(), "leaveSession")) {
00115                 ev<< "leaveSession"<<endl;
00116                 RTPInterfacePacket *rifpOut = new RTPInterfacePacket("leaveSession()");
00117                 rifpOut->leaveSession();
00118                 send(rifpOut, "rtpOut");
00119             }
00120         }
00121         else {
00122             RTPInterfacePacket *rifpIn = check_and_cast<RTPInterfacePacket *>(msgIn);
00123 
00124             if (rifpIn->getType() == RTPInterfacePacket::RTP_IFP_SESSION_ENTERED) {
00125                 ev << "Session Entered"<<endl;
00126                 ssrc = rifpIn->getSSRC();
00127                 sessionEntered = true;
00128                 if (opp_strcmp(_fileName, "")) {
00129                     RTPInterfacePacket *rifpOut = new RTPInterfacePacket("createSenderModule()");
00130                     rifpOut->createSenderModule(ssrc, _payloadType, opp_strdup(_fileName));
00131                     ev << "CreateSenderModule"<<endl;
00132                     send(rifpOut, "rtpOut");
00133                 }
00134                 else {
00135                     cMessage *msg2 = new cMessage("leaveSession");
00136                     ev << "Receiver Module : leaveSession"<<endl;
00137                     scheduleAt(simTime() + _sessionLeaveDelay, msg2);
00138                 }
00139             }
00140             else if (rifpIn->getType() == RTPInterfacePacket::RTP_IFP_SENDER_MODULE_CREATED) {
00141                 cMessage *msg3 = new cMessage("startTransmission");
00142                 ev << "Sender Module Created"<<endl;
00143                 scheduleAt(simTime() + _transmissionStartDelay, msg3);
00144             }
00145             else if (rifpIn->getType() == RTPInterfacePacket::RTP_IFP_SENDER_STATUS) {
00146                 RTPSenderStatusMessage *rsim = (RTPSenderStatusMessage *)(rifpIn->decapsulate());
00147                 if (!opp_strcmp(rsim->getStatus(), "PLAYING")) {
00148                     ev << "PLAYING"<<endl;
00149                 }
00150                 else if (!opp_strcmp(rsim->getStatus(), "FINISHED")) {
00151                     transmissionFinished = true;
00152                     ev << "FINISHED"<<endl;
00153                     cMessage *msg5 = new cMessage("leaveSession");
00154                     scheduleAt(simTime() + _sessionLeaveDelay, msg5);
00155                 }
00156                 else if (!opp_strcmp(rsim->getStatus(), "STOPPED")) {
00157                     transmissionFinished = true;
00158                     ev << "FINISHED"<<endl;
00159                     cMessage *msg6 = new cMessage("leaveSession");
00160                     scheduleAt(simTime() + _sessionLeaveDelay, msg6);
00161                 }
00162                 else {
00163                     delete rifpIn;
00164                 }
00165                 cancelAndDelete(rsim);
00166             }
00167             else if (rifpIn->getType() == RTPInterfacePacket::RTP_IFP_SESSION_LEFT) {
00168                 sessionLeft = true;
00169             }
00170         }
00171         delete msgIn;
00172 
00173     }
00174 }