examples/MultimediaProtocols/RTPExample/src/ExampleApp.cpp

00001 /*
00002 Copyright (c) 2005-2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008 � list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010 � this list of conditions and the following disclaimer in the documentation
00011 � and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013 � may be used to endorse or promote products derived from this software
00014 � without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description:� 
00028 */
00029 
00030 
00031 
00032 #include "RTPFileStreamer.h"
00033 #include "ExampleApp.h"
00034 
00035 
00036 //CActiveConsole
00037 CActiveConsole::CActiveConsole(MActiveConsoleNotify& aNotify):CActive(EPriorityStandard), iNotify(aNotify)
00038         {
00039         CActiveScheduler::Add(this);
00040         }
00041 
00042 CActiveConsole::~CActiveConsole()
00043         {
00044         delete iConsole;
00045         Cancel();
00046         }
00047 
00048 CActiveConsole* CActiveConsole::NewL(MActiveConsoleNotify& aNotify,const TDesC& aTitle,const TSize& aSize)
00049         {
00050         CActiveConsole* console = new (ELeave) CActiveConsole(aNotify);
00051         CleanupStack::PushL(console);
00052         console->ConstructL(aTitle,aSize);
00053         CleanupStack::Pop();
00054         return console;
00055         }
00056 
00057 void CActiveConsole::ConstructL(const TDesC& aTitle,const TSize& aSize)
00058         {
00059         iConsole = Console::NewL(aTitle,aSize);
00060         }
00061 
00062 void CActiveConsole::DoCancel()
00063         {
00064         iConsole->ReadCancel();
00065         }
00066 
00067 void CActiveConsole::RequestKey()
00068         {
00069         DrawCursor();
00070         iConsole->Read(iStatus);
00071         SetActive();
00072         }
00073 
00074 void CActiveConsole::DrawCursor()
00075         {
00076         iConsole->Printf(_L(">>"));
00077         }
00078 
00079 void CActiveConsole::RunL()
00080         {
00081         TChar ch = iConsole->KeyCode();
00082         iNotify.KeyPressed(ch);
00083         }
00084 
00085 //CExampleApp
00086 CExampleApp::CExampleApp()
00087         {
00088         }
00089         
00090 CExampleApp::~CExampleApp()
00091         {
00092         delete iActiveConsole;
00093         delete iMonitorConsole;
00094         iSockServ.Close();
00095         }
00096         
00097 CExampleApp* CExampleApp::NewL()
00098         {
00099         CExampleApp* thisapp = new (ELeave) CExampleApp();
00100         CleanupStack::PushL(thisapp);
00101         thisapp->ConstructL();
00102         CleanupStack::Pop();
00103         return thisapp;
00104         }
00105 
00106 static TSize gMainConsole(KConsFullScreen,KConsFullScreen);
00107 static TSize gTinyConsole(40,10);
00108 
00109 void CExampleApp::ConstructL()
00110         {
00111         iActiveConsole = CActiveConsole::NewL(*this,_L("Status"),gMainConsole);
00112         iMonitorConsole = CActiveConsole::NewL(*this,_L("RtpExample"),gTinyConsole);
00113         User::LeaveIfError(iSockServ.Connect());
00114         }
00115         
00116 void CExampleApp::StartL()
00117         {
00118         _LIT(KTextWelcome, " ### RTP Example ### \n");
00119         iMonitorConsole->Console().Printf(KTextWelcome);
00120         
00121         _LIT(KTextStartApp, "\n\n Starting the RTP application");
00122         iMonitorConsole->Console().Printf ( KTextStartApp );
00123         
00124         _LIT(KTextPressAKey, "\n\n Press any key to step through the example");
00125         iMonitorConsole->Console().Printf ( KTextPressAKey );
00126         iMonitorConsole->Console().Getch ();    
00127 
00128         _LIT(KDAddr,"\n\n Local address: ");
00129         iMonitorConsole->Console().Printf(KDAddr);
00130         iMonitorConsole->Console().Printf(KDestAddr);
00131         iMonitorConsole->Console().Getch ();
00132         _LIT(KLPort,"\n\n Address family: ");
00133         iMonitorConsole->Console().Printf(KLPort);
00134         iMonitorConsole->Console().Printf(KLocalPort);
00135         iMonitorConsole->Console().Getch ();
00136         _LIT(KDPort,"\n\n RTP port number: ");
00137         iMonitorConsole->Console().Printf(KDPort);
00138         iMonitorConsole->Console().Printf(KDestPort);
00139         iMonitorConsole->Console().Getch ();
00140         _LIT(KSFile,"\n\n Source file name: ");
00141         iMonitorConsole->Console().Printf(KSFile);
00142         iMonitorConsole->Console().Printf(KsFileName);
00143         iMonitorConsole->Console().Getch ();
00144         _LIT(KTFile,"\n\n Target file name: ");
00145         iMonitorConsole->Console().Printf(KTFile);
00146         iMonitorConsole->Console().Printf(KdFileName);
00147         iMonitorConsole->Console().Getch ();
00148         _LIT(KBufSize,"\n\n Buffer size: ");
00149         iMonitorConsole->Console().Printf(KBufSize);
00150         iMonitorConsole->Console().Printf(KSize);
00151         iMonitorConsole->Console().Getch ();
00152 
00153         TInt dport;
00154         TInt lport;
00155         TInt psize;
00156         TInetAddr daddr;
00157         TLex parser;
00158         parser.Assign(KDestPort);
00159         User::LeaveIfError(parser.Val(dport));
00160         parser.Assign(KSize);
00161         User::LeaveIfError(parser.Val(psize));
00162         parser.Assign(KLocalPort);
00163         User::LeaveIfError(parser.Val(lport));
00164 
00165         RHostResolver resolver;
00166         User::LeaveIfError(resolver.Open(iSockServ,KAfInet,KProtocolInetUdp));
00167         CleanupClosePushL(resolver);
00168         TNameEntry entry;
00169         TRequestStatus status;
00170         resolver.GetByName(KDestAddr,entry,status);
00171         User::WaitForRequest(status);
00172         User::LeaveIfError(status.Int());
00173         CleanupStack::PopAndDestroy(1);//close resolver
00174 
00175         TSockAddr remote = entry().iAddr;
00176         remote.SetPort(dport);
00177 
00178         TInt connId=KErrNotFound;
00179         iMonitorConsole->Console().Printf(_L("\n\n Sending RTP packet from %S file to %S file."),&KsFileName,&KdFileName);              
00180         iMonitorConsole->Console().Getch();
00181         iStreamer = CRtpFileStreamer::NewL(iSockServ,KsFileName,KdFileName,psize,remote,lport,connId);
00182         iStreamer->SetObserver(*this);
00183         iStreamer->StartL();
00184         CActiveScheduler::Start();
00185         }
00186         
00187 void CExampleApp::Stop()
00188         {
00189         if (iStreamer)
00190                 {
00191                 delete iStreamer;
00192                 }
00193         CActiveScheduler::Stop();
00194         }
00195         
00196 void CExampleApp::DrawMonitor()
00197         {
00198         iMonitorConsole->Console().Printf(_L("\n Sent: %d\n Recv: %d"),iSent,iRecv);
00199         }
00200         
00201 void CExampleApp::NotifyPacketSent()
00202         {
00203         iSent++;
00204         DrawMonitor();
00205         iActiveConsole->Console().Printf(_L("s"));
00206         }
00207         
00208 void CExampleApp::NotifyPacketReceived()
00209         {
00210         iRecv++;
00211         DrawMonitor();
00212         iActiveConsole->Console().Printf(_L("r"));
00213         }
00214         
00215 void CExampleApp::NotifyComplete()
00216         {
00217         iMonitorConsole->Console().Printf(_L("\n\n Successfully sent %d RTP packet(s)"),iRecv);
00218         iMonitorConsole->Console().Getch();
00219         
00220         _LIT(KExit,"\n\n Press any key to exit the application ");
00221         iMonitorConsole->Console().Printf(KExit); 
00222         iMonitorConsole->Console().Getch();
00223         
00224         if (iStreamer)
00225                 {
00226                 delete iStreamer;
00227                 }
00228         CActiveScheduler::Stop();
00229         }
00230         
00231 void CExampleApp::NotifyError()
00232         {
00233         iMonitorConsole->Console().Printf(_L("Error\n"));
00234         iMonitorConsole->Console().Getch();
00235         iActiveConsole->Console().Printf(_L("E"));
00236         }
00237         
00238 void CExampleApp::KeyPressed(TChar /*aKey*/)
00239         {
00240         }
00241 
00242 void MainL()
00243         {
00244         CExampleApp* app = CExampleApp::NewL();
00245         CleanupStack::PushL(app);
00246         app->StartL();
00247         CleanupStack::PopAndDestroy(app);
00248         }
00249         
00250 TInt E32Main()
00251         {
00252         __UHEAP_MARK;
00253         CTrapCleanup* cleanupStack=CTrapCleanup::New();
00254         CActiveScheduler* activescheduler=new CActiveScheduler;
00255         CActiveScheduler::Install(activescheduler);
00256 
00257         TRAPD(err, MainL());
00258         _LIT(KTxtEPOC32EX,"EXAMPLES");
00259         __ASSERT_ALWAYS(!err,User::Panic(KTxtEPOC32EX,err));
00260         
00261         delete activescheduler;
00262         delete cleanupStack;
00263         __UHEAP_MARKEND;
00264         return KErrNone;
00265         }

Generated by  doxygen 1.6.2