testProgs/playSIP.cpp

Go to the documentation of this file.
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-2013, Live Networks, Inc.  All rights reserved
00017 // A SIP client test program that opens a SIP URL argument,
00018 // and extracts the data from each incoming RTP stream.
00019 
00020 #include "playCommon.hh"
00021 #include "SIPClient.hh"
00022 
00023 static char* getLine(char* startOfLine) {
00024   // returns the start of the next line, or NULL if none
00025   for (char* ptr = startOfLine; *ptr != '\0'; ++ptr) {
00026     if (*ptr == '\r' || *ptr == '\n') {
00027       // We found the end of the line
00028       *ptr++ = '\0';
00029       if (*ptr == '\n') ++ptr;
00030       return ptr;
00031     }
00032   }
00033   
00034   return NULL;
00035 }
00036 
00037 SIPClient* ourSIPClient = NULL;
00038 Medium* createClient(UsageEnvironment& env, char const* /*url*/, int verbosityLevel, char const* applicationName) {
00039   // First, trim any directory prefixes from "applicationName":
00040   char const* suffix = &applicationName[strlen(applicationName)];
00041   while (suffix != applicationName) {
00042     if (*suffix == '/' || *suffix == '\\') {
00043       applicationName = ++suffix;
00044       break;
00045     }
00046     --suffix;
00047   }
00048 
00049   extern unsigned char desiredAudioRTPPayloadFormat;
00050   extern char* mimeSubtype;
00051   return ourSIPClient = SIPClient::createNew(env, desiredAudioRTPPayloadFormat, mimeSubtype, verbosityLevel, applicationName);
00052 }
00053 
00054 void getOptions(RTSPClient::responseHandler* afterFunc) { 
00055   ourSIPClient->envir().setResultMsg("NOT SUPPORTED IN CLIENT");
00056   afterFunc(NULL, -1, strDup(ourSIPClient->envir().getResultMsg()));
00057 }
00058 
00059 void getSDPDescription(RTSPClient::responseHandler* afterFunc) {
00060   extern char* proxyServerName;
00061   if (proxyServerName != NULL) {
00062     // Tell the SIP client about the proxy:
00063     NetAddressList addresses(proxyServerName);
00064     if (addresses.numAddresses() == 0) {
00065       ourSIPClient->envir() << "Failed to find network address for \"" << proxyServerName << "\"\n";
00066     } else {
00067       NetAddress address = *(addresses.firstAddress());
00068       unsigned proxyServerAddress // later, allow for IPv6 #####
00069         = *(unsigned*)(address.data());
00070       extern unsigned short proxyServerPortNum;
00071       if (proxyServerPortNum == 0) proxyServerPortNum = 5060; // default
00072 
00073       ourSIPClient->setProxyServer(proxyServerAddress, proxyServerPortNum);
00074     }
00075   }
00076 
00077   extern unsigned short desiredPortNum;
00078   unsigned short clientStartPortNum = desiredPortNum;
00079   if (clientStartPortNum == 0) clientStartPortNum = 8000; // default
00080   ourSIPClient->setClientStartPortNum(clientStartPortNum);
00081 
00082   extern char const* streamURL;
00083   char const* username = ourAuthenticator == NULL ? NULL : ourAuthenticator->username();
00084   char const* password = ourAuthenticator == NULL ? NULL : ourAuthenticator->password();
00085   char* result;
00086   if (username != NULL && password != NULL) {
00087     result = ourSIPClient->inviteWithPassword(streamURL, username, password);
00088   } else {
00089     result = ourSIPClient->invite(streamURL);
00090   }
00091 
00092   int resultCode = result == NULL ? -1 : 0;
00093   afterFunc(NULL, resultCode, strDup(result));
00094 }
00095 
00096 void setupSubsession(MediaSubsession* subsession, Boolean /*streamUsingTCP*/, RTSPClient::responseHandler* afterFunc) {
00097   subsession->setSessionId("mumble"); // anything that's non-NULL will work
00098 
00100   // Parse the "Transport:" header parameters:
00101   // We do not send audio, but we need port for RTCP
00102   char* serverAddressStr;
00103   portNumBits serverPortNum;
00104   unsigned char rtpChannelId, rtcpChannelId;
00105 
00106   rtpChannelId = rtcpChannelId = 0xff;
00107   serverPortNum = 0;
00108   serverAddressStr = NULL;
00109 
00110   char* sdp = strDup(ourSIPClient->getInviteSdpReply());
00111 
00112   char* lineStart;
00113   char* nextLineStart = sdp;
00114   while (1) {
00115     lineStart = nextLineStart;
00116     if (lineStart == NULL) {
00117       break;
00118     }
00119     nextLineStart = getLine(lineStart);
00120 
00121     char* toTagStr = strDupSize(lineStart);
00122 
00123     if (sscanf(lineStart, "m=audio %[^/\r\n]", toTagStr) == 1) {
00124       sscanf(toTagStr, "%hu", &serverPortNum);
00125     } else if (sscanf(lineStart, "c=IN IP4 %[^/\r\n]", toTagStr) == 1) {
00126       serverAddressStr = strDup(toTagStr);
00127     }
00128     delete[] toTagStr;
00129   }
00130 
00131   if(sdp != NULL) {
00132     delete[] sdp;
00133   }
00134 
00135   delete[] subsession->connectionEndpointName();
00136   subsession->connectionEndpointName() = serverAddressStr;
00137   subsession->serverPortNum = serverPortNum;
00138   subsession->rtpChannelId = rtpChannelId;
00139   subsession->rtcpChannelId = rtcpChannelId;
00140 
00141   // Set the RTP and RTCP sockets' destination address and port from the information in the SETUP response (if present):
00142   netAddressBits destAddress = subsession->connectionEndpointAddress();
00143   if (destAddress != 0) {
00144     subsession->setDestinations(destAddress);
00145   }
00147 
00148   afterFunc(NULL, 0, NULL);
00149 }
00150 
00151 void startPlayingSession(MediaSession* /*session*/, double /*start*/, double /*end*/, float /*scale*/, RTSPClient::responseHandler* afterFunc) {
00152   if (ourSIPClient->sendACK()) {
00153     //##### This isn't quite right, because we should really be allowing
00154     //##### for the possibility of this ACK getting lost, by retransmitting
00155     //##### it *each time* we get a 2xx response from the server.
00156     afterFunc(NULL, 0, NULL);
00157   } else {
00158     afterFunc(NULL, -1, strDup(ourSIPClient->envir().getResultMsg()));
00159   }
00160 }
00161 void startPlayingSession(MediaSession* /*session*/, const char* /*start*/, const char* /*end*/, float /*scale*/, RTSPClient::responseHandler* afterFunc) {
00162         startPlayingSession(NULL,(double)0,(double)0,0,afterFunc);
00163 }
00164 
00165 void tearDownSession(MediaSession* /*session*/, RTSPClient::responseHandler* afterFunc) {
00166   if (ourSIPClient == NULL || ourSIPClient->sendBYE()) {
00167     afterFunc(NULL, 0, NULL);
00168   } else {
00169     afterFunc(NULL, -1, strDup(ourSIPClient->envir().getResultMsg()));
00170   }
00171 }
00172 
00173 Boolean allowProxyServers = True;
00174 Boolean controlConnectionUsesTCP = False;
00175 Boolean supportCodecSelection = True;
00176 char const* clientProtocolName = "SIP";

Generated on Mon Apr 29 13:28:03 2013 for live by  doxygen 1.5.2