RtspClientInteract.cxx
上传用户:sy_wanhua
上传日期:2013-07-25
资源大小:3048k
文件大小:13k
源码类别:

流媒体/Mpeg4/MP4

开发平台:

C/C++

  1. /* ====================================================================
  2.  * The Vovida Software License, Version 1.0 
  3.  * 
  4.  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
  5.  * 
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  * 
  10.  * 1. Redistributions of source code must retain the above copyright
  11.  *    notice, this list of conditions and the following disclaimer.
  12.  * 
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in
  15.  *    the documentation and/or other materials provided with the
  16.  *    distribution.
  17.  * 
  18.  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
  19.  *    and "Vovida Open Communication Application Library (VOCAL)" must
  20.  *    not be used to endorse or promote products derived from this
  21.  *    software without prior written permission. For written
  22.  *    permission, please contact vocal@vovida.org.
  23.  *
  24.  * 4. Products derived from this software may not be called "VOCAL", nor
  25.  *    may "VOCAL" appear in their name, without prior written
  26.  *    permission of Vovida Networks, Inc.
  27.  * 
  28.  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
  29.  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30.  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
  31.  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
  32.  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
  33.  * IN EXCESS OF 281421,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
  34.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  35.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  36.  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  37.  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  38.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  39.  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  40.  * DAMAGE.
  41.  * 
  42.  * ====================================================================
  43.  * 
  44.  * This software consists of voluntary contributions made by Vovida
  45.  * Networks, Inc. and many individuals on behalf of Vovida Networks,
  46.  * Inc.  For more information on Vovida Networks, Inc., please see
  47.  * <http://www.vovida.org/>.
  48.  *
  49.  */
  50. static const char* const RtspClientInteract_cxx_version =
  51.     "$Id: RtspClientInteract.cxx,v 1.30 2001/07/26 21:48:05 kle Exp $";
  52. #include <iostream.h>
  53. #include <stdio.h>
  54. #include <stdlib.h>
  55. #include <errno.h>
  56. #include <unistd.h>
  57. #include <list>
  58. #include <fcntl.h>
  59. #include "NetworkAddress.h"
  60. #include "Connection.hxx"
  61. #include "Tcp_ClientSocket.hxx"
  62. #include "VNetworkException.hxx"
  63. #include "cpLog.h"
  64. #include "RtspMsg.hxx"
  65. #include "RtspMsgParser.hxx"
  66. #include "RtspTransceiver.hxx"
  67. #include "RtpSession.hxx"
  68. int writeData( Connection conn, char* buffer, int len );
  69. int readData( Connection conn, char* buffer, size_t len);
  70. int main( int argc, char* argv[] )
  71. {
  72.     cpLogSetPriority(LOG_DEBUG);
  73.     if( argc < 3 )
  74.     {
  75.         printf("Usage: %s remotehost remoteportn", argv[0] );
  76.         return 0;
  77.     }
  78.     // build remote address
  79.     string rmtHost = argv[1];
  80.     int rmtPort = atoi( argv[2] );
  81.     NetworkAddress remoteAddress( rmtHost, rmtPort );
  82.     // create TCP connection
  83.     cout <<"Connecting to: ";
  84.     remoteAddress.print( cout );
  85.     cout <<endl;
  86.     TcpClientSocket cSock( remoteAddress, true, false );
  87.     try
  88.     {
  89.         cSock.connect();
  90.     }
  91.     catch (VException &e)
  92.     {
  93.         cerr <<e.getDescription().c_str()<<endl;
  94.         return 0;
  95.     }
  96.     char buffer[1024];
  97.     char choice[10];
  98.     int sessionId;
  99.     int cseqCounter = 0;
  100.     int cRand = 0;
  101.     bool quit = false;
  102.     Sptr< RtspMsg > savedMsg = 0;
  103.     Sptr< RtspMsg > response = 0;
  104.     while( !quit )
  105.     {
  106.         memset( buffer, 0, 1024);
  107.         cout<<"Enter command (d, a, s, w(setup with record), p, r, o(pause), t)" << endl;
  108.         cin >> choice;
  109.         cin >> sessionId;
  110.         cout<<endl;
  111.         switch( choice[0] )
  112.         {
  113.             case 'd':
  114.             {
  115.                 sprintf( buffer,"DESCRIBE rtsp://www.vovida.com/sounds/basic RTSP/1.0rnCSeq: %drnAccept: application/sdprnrn",
  116.                          cseqCounter++ );
  117.                 break;
  118.             }
  119.             case 'b':
  120.             {
  121.                 sprintf( buffer,"DESCRIBE rtsp://www.vovida.com/sounds/wav728.wav RTSP/1.0rnCSeq: %drnAccept: application/sdprnrn",
  122.                          cseqCounter++ );
  123.                 break;
  124.             }
  125.             case 'a':
  126.             {
  127.                 sprintf( buffer,"ANNOUNCE rtsp://www.vovida.com/sounds/basic RTSP/1.0rnCSeq: %drnContent-Length: 109rnContent-Type: application/sdprnrnv=0rno=- 12345 12345 IN IP4 0.0.0.0rns=Vovida RTSP Serverrnt=0 0rnm=audio 0 RTP/AVP 0rna=rtpmap:0 PCMU/8000rn",
  128.                          cseqCounter++ );
  129.                 break;
  130.             }
  131.             case 'm':
  132.             {
  133.                 sprintf( buffer,"ANNOUNCE rtsp://www.vovida.com/sounds/basic RTSP/1.0rnCSeq: %drnContent-Length: 21rnContent-Type: application/sdprnrnm=audio 0 RTP/AVP 0rn",
  134.                          cseqCounter++ );
  135.                 break;
  136.             }
  137.             case 'x':   // annouce with incremental filename
  138.             {
  139.                 cRand = cseqCounter;
  140.                 sprintf( buffer,"ANNOUNCE rtsp://www.vovida.com/sounds/basic%d.au RTSP/1.0rnCSeq: %drnContent-Length: 109rnContent-Type: application/sdprnrnv=0rno=- 12345 12345 IN IP4 0.0.0.0rns=Vovida RTSP Serverrnt=0 0rnm=audio 0 RTP/AVP 0rna=rtpmap:0 L16M/8000rn",
  141.                          getpid(), cseqCounter++ );
  142.                 break;
  143.             }
  144.             case 'y':  //performance play test (incremental client port)
  145.             {
  146.                 sprintf( buffer,"SETUP rtsp://www.vovida.com/sounds/basic%d.au RTSP/1.0rnCSeq: %drnTransport: rtp/avp;unicast;client_port=%d-%d;mode="record"rnrn",
  147.                          getpid(), cseqCounter++, cRand+9000, cRand+9000+1 );
  148.                 cRand++;
  149.                 break;
  150.             }
  151.             case 's':
  152.             {
  153.                 sprintf( buffer,"SETUP rtsp://www.vovida.com/sounds/basic RTSP/1.0rnCSeq: %drnTransport: rtp/avp;unicast;client_port=9000-9001rnrn",
  154.                          cseqCounter++ );
  155.                 break;
  156.             }
  157.             case 'q':  //setup for PCMU
  158.             {
  159.                 sprintf( buffer,"SETUP rtsp://www.vovida.com/sounds/LINEAR16-PCMU.raw RTSP/1.0rnCSeq: %drnTransport: rtp/avp;unicast;client_port=9000-9001rnrn",
  160.                          cseqCounter++ );
  161.                 break;
  162.             }
  163.             case 'w':  //setup(record)
  164.             {
  165.                 sprintf( buffer,"SETUP rtsp://www.vovida.com/sounds/basic RTSP/1.0rnCSeq: %drnTransport: rtp/avp;unicast;client_port=9000-9001;mode="record"rnrn",
  166.                          cseqCounter++ );
  167.                 break;
  168.             }
  169.             case 'n':  //setup(record)
  170.             {
  171.                 sprintf( buffer,"SETUP rtsp://www.vovida.com/sounds/basic2 RTSP/1.0rnCSeq: %drnTransport: rtp/avp;unicast;client_port=9000-9001;mode="record"rnrn",
  172.                          cseqCounter++ );
  173.                 break;
  174.             }
  175.             case 'g':  //setup(play)
  176.             {
  177.                 sprintf( buffer,"SETUP rtsp://www.vovida.com/sounds/twister-PCMU.au RTSP/1.0rnCSeq: %drnTransport: rtp/avp;unicast;client_port=9000-9001;mode="PLAY"rnrn",
  178.                          cseqCounter++ );
  179.                 break;
  180.             }
  181.             case 'h':  //setup(play)
  182.             {
  183.                 sprintf( buffer,"SETUP rtsp://www.vovida.com/sounds/sample-sound/au/gr_welcome.au RTSP/1.0rnCSeq: %drnTransport: rtp/avp;unicast;client_port=9000-9001;mode="PLAY"rnrn",
  184.                          cseqCounter++ );
  185.                 break;
  186.             }
  187.             case 'z':   //performance reocrd test
  188.             {
  189.                 sprintf( buffer,"SETUP rtsp://www.vovida.com/twister%d.au RTSP/1.0rnCSeq: %drnTransport: rtp/avp;unicast;client_port=%d-%d;mode="RECORD"rnrn",
  190.                          sessionId, cseqCounter++, cRand*2+9000, cRand*2+9000+1 );
  191.                 break;
  192.             }
  193.             case 'p':
  194.             {
  195.                 sprintf( buffer,"PLAY rtsp://www.vovida.com/sounds/foo RTSP/1.0rnCSeq: %drnSession: %drnrn",
  196.                          cseqCounter++, sessionId );
  197.                 break;
  198.             }
  199.             case '[':
  200.             {
  201.                 sprintf( buffer,"PLAY rtsp://www.vovida.com/sounds/foo RTSP/1.0rnCSeq: %drnSession: %drnRange: ntp=2.0rn",
  202.                          cseqCounter++, sessionId );
  203.                 break;
  204.             }
  205.             case 'r':
  206.             {
  207.                 sprintf( buffer,"RECORD rtsp://www.vovida.com/sounds/foo RTSP/1.0rnCSeq: %drnSession: %drnrn",
  208.                          cseqCounter++, sessionId );
  209.                 break;
  210.             }
  211.             case 'o':
  212.             {
  213.                 sprintf( buffer,"PAUSE rtsp://www.vovida.com/sounds/LINEAR16.raw RTSP/1.0rnCSeq: %drnSession: %drnrn",
  214.                          cseqCounter++, sessionId );
  215.                 break;
  216.             }
  217.             case 't':
  218.             {
  219.                 sprintf( buffer,"TEARDOWN rtsp://www.vovida.com/sounds/LINEAR16.raw RTSP/1.0rnCSeq: %drnSession: %drnrn",
  220.                          cseqCounter++, sessionId );
  221.                 break;
  222.             }
  223.             case '0':
  224.             {
  225.                 sprintf( buffer,"TEARDOWN rtsp://www.vovida.com/sounds/LINEAR16.raw RTSP/1.0rnCSeq: %drnSession: Globalrnrn",
  226.                          cseqCounter++ );
  227.                 break;
  228.             }
  229.             case '#':
  230.             {
  231.                 printf("Generating RTP traffic to %s:%dn", rmtHost.c_str(), sessionId);
  232.                 fflush(stdout);
  233.                 RtpSession stack( rmtHost.c_str(), sessionId, 0, 0, 0 );
  234.                 RtpPacket* packet = stack.createPacket();
  235.                 memset (packet->getPayloadLoc(), packet->getPayloadSize(), 0);
  236.                 packet->setPayloadUsage (packet->getPayloadSize());
  237.                 for( int i = 0; i < 1000; i++ )
  238.                 {
  239.                     usleep(18*1000);
  240.                     if( stack.transmit(packet) < 0 )
  241.                         cerr<<"x";
  242.                 }
  243.                 int sendPkts = stack.getRtpTran()->getUdpStack()->getPacketsTransmitted();
  244.                 printf("Udp send pkts: %dn", sendPkts);
  245.                 delete packet;
  246.                 continue;
  247.             }
  248.             case '1':
  249.             {
  250.                 printf("Sleeping 1 secn");
  251.                 sleep( 1 );
  252.                 continue;
  253.             }
  254.             case '5':
  255.             {
  256.                 printf("Sleeping 5 secn");
  257.                 sleep( 5 );
  258.                 continue;
  259.             }
  260.             case '!':
  261.             {
  262.                 printf("Donen");
  263.                 return 0;
  264.             }
  265.             default:
  266.             {
  267.                 printf("Invalid choicen");
  268.                 continue;
  269.             }
  270.         }
  271.             
  272.         if( !quit )
  273.         {
  274.             cerr<<"Sending rtsp message:n"<<buffer;
  275.             writeData( cSock.getConn(), buffer, strlen(buffer) );
  276.             cerr <<"Waiting for replyn";
  277.             if (cSock.getConn().isReadReady(2))
  278.             {
  279.                 int bytesRead = readData( cSock.getConn(), buffer, 1024 );
  280.                 int bytesParsed = 0;
  281.                 RtspMsgParser parser;
  282.                 // only try to get one msg from this buffer
  283.                 response = parser.preParse( buffer, bytesRead, bytesParsed, 
  284.                                             savedMsg );
  285.                 if( response->getCompletionFlag() == RTSP_MSG_COMPLETE )
  286.                 {
  287.                     savedMsg = 0;
  288.                 }
  289.                 else
  290.                 {
  291.                     savedMsg = response;
  292.                 }
  293.             }
  294.         }
  295.     }
  296.     cout <<"Closing connectionn";
  297.     cSock.close();
  298.     cout <<"Donen";
  299.     return 0;
  300. }
  301. int
  302. writeData( Connection conn, char* buffer, int len )
  303. {
  304.     cpLog( LOG_DEBUG_STACK, "Sending to tcp socket:n%s", buffer );
  305.     // write buffer to TCP connection
  306.     try
  307.     {
  308.         conn.writeData( buffer, len );
  309.     }
  310.     catch( VNetworkException& e )
  311.     {
  312.         cerr <<e.getDescription()<<endl;
  313.         return 0;
  314.     }
  315.     return 0;
  316. }
  317. int
  318. readData( Connection conn, char* buffer, size_t len )
  319. {
  320.     int bytesRead;
  321.     RtspTransceiver::readData( conn, (void*)buffer, len, bytesRead);
  322.     if (bytesRead > 0)
  323.     {
  324.         Data dRead(buffer, bytesRead);
  325.         // this is ready done in stack
  326.         //cout <<"Server responded: n";
  327.         //cout <<"---------------------n";
  328.         //cout <<"bytesRead= " << bytesRead << endl;
  329.         //cout <<dRead.getData()<<endl;
  330.         //cout <<"---------------------n";
  331.         return bytesRead;
  332.     }
  333.     else
  334.     {
  335.         cout << "Server no response n";
  336.         return 0;
  337.     }
  338. }
  339. /* Local Variables: */
  340. /* c-file-style: "stroustrup" */
  341. /* indent-tabs-mode: nil */
  342. /* c-file-offsets: ((access-label . -) (inclass . ++)) */
  343. /* c-basic-offset: 4 */
  344. /* End: */