⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rtspclient.cpp

📁 c++实现的流媒体库,和mplayer结合就可以实现多媒体的网络播放,属于开源项目,很值得研究.
💻 CPP
📖 第 1 页 / 共 5 页
字号:
    delete[] cmd;    return True;  } while (0);  delete[] cmd;  return False;}Boolean RTSPClient::pauseMediaSession(MediaSession& session) {  char* cmd = NULL;  do {    // First, make sure that we have a RTSP session in progress    if (fLastSessionId == NULL) {      envir().setResultMsg(NoSessionErr);      break;    }    // Send the PAUSE command:    // First, construct an authenticator string:    char* authenticatorStr      = createAuthenticatorString(&fCurrentAuthenticator, "PAUSE", fBaseURL);    char const* sessURL = sessionURL(session);    char* const cmdFmt =      "PAUSE %s RTSP/1.0\r\n"      "CSeq: %d\r\n"      "Session: %s\r\n"      "%s"      "%s"      "\r\n";    unsigned cmdSize = strlen(cmdFmt)      + strlen(sessURL)      + 20 /* max int len */      + strlen(fLastSessionId)      + strlen(authenticatorStr)      + fUserAgentHeaderStrSize;    cmd = new char[cmdSize];    sprintf(cmd, cmdFmt,	    sessURL,	    ++fCSeq,	    fLastSessionId,	    authenticatorStr,	    fUserAgentHeaderStr);    delete[] authenticatorStr;    if (!sendRequest(cmd, "PAUSE")) break;    if (fTCPStreamIdCount == 0) { // When TCP streaming, don't look for a response      // Get the response from the server:      unsigned bytesRead; unsigned responseCode;      char* firstLine; char* nextLineStart;      if (!getResponse("PAUSE", bytesRead, responseCode, firstLine, nextLineStart)) break;    }    delete[] cmd;    return True;  } while (0);  delete[] cmd;  return False;}Boolean RTSPClient::pauseMediaSubsession(MediaSubsession& subsession) {  char* cmd = NULL;  do {    // First, make sure that we have a RTSP session in progress    if (subsession.sessionId == NULL) {      envir().setResultMsg(NoSessionErr);      break;    }        // Send the PAUSE command:        // First, construct an authenticator string:    char* authenticatorStr      = createAuthenticatorString(&fCurrentAuthenticator, "PAUSE", fBaseURL);        char* const cmdFmt =      "PAUSE %s%s%s RTSP/1.0\r\n"      "CSeq: %d\r\n"      "Session: %s\r\n"      "%s"      "%s"      "\r\n";        char const *prefix, *separator, *suffix;    constructSubsessionURL(subsession, prefix, separator, suffix);    if (fServerIsKasenna) separator = suffix = "";    unsigned cmdSize = strlen(cmdFmt)      + strlen(prefix) + strlen(separator) + strlen(suffix)      + 20 /* max int len */      + strlen(subsession.sessionId)      + strlen(authenticatorStr)      + fUserAgentHeaderStrSize;    cmd = new char[cmdSize];    sprintf(cmd, cmdFmt,	    prefix, separator, suffix,	    ++fCSeq,	    subsession.sessionId,	    authenticatorStr,	    fUserAgentHeaderStr);    delete[] authenticatorStr;        if (!sendRequest(cmd, "PAUSE")) break;    if (fTCPStreamIdCount == 0) { // When TCP streaming, don't look for a response      // Get the response from the server:      unsigned bytesRead; unsigned responseCode;      char* firstLine; char* nextLineStart;      if (!getResponse("PAUSE", bytesRead, responseCode, firstLine, nextLineStart)) break;    }          delete[] cmd;    return True;  } while (0);    delete[] cmd;  return False;}Boolean RTSPClient::recordMediaSubsession(MediaSubsession& subsession) {  char* cmd = NULL;  do {    // First, make sure that we have a RTSP session in progress    if (subsession.sessionId == NULL) {      envir().setResultMsg(NoSessionErr);      break;    }    // Send the RECORD command:    // First, construct an authenticator string:    char* authenticatorStr      = createAuthenticatorString(&fCurrentAuthenticator,				  "RECORD", fBaseURL);    char* const cmdFmt =      "RECORD %s%s%s RTSP/1.0\r\n"      "CSeq: %d\r\n"      "Session: %s\r\n"      "Range: npt=0-\r\n"      "%s"      "%s"      "\r\n";    char const *prefix, *separator, *suffix;    constructSubsessionURL(subsession, prefix, separator, suffix);    unsigned cmdSize = strlen(cmdFmt)      + strlen(prefix) + strlen(separator) + strlen(suffix)      + 20 /* max int len */      + strlen(subsession.sessionId)      + strlen(authenticatorStr)      + fUserAgentHeaderStrSize;    cmd = new char[cmdSize];    sprintf(cmd, cmdFmt,	    prefix, separator, suffix,	    ++fCSeq,	    subsession.sessionId,	    authenticatorStr,	    fUserAgentHeaderStr);    delete[] authenticatorStr;    if (!sendRequest(cmd, "RECORD")) break;    // Get the response from the server:    unsigned bytesRead; unsigned responseCode;    char* firstLine; char* nextLineStart;    if (!getResponse("RECORD", bytesRead, responseCode, firstLine, nextLineStart)) break;    delete[] cmd;    return True;  } while (0);  delete[] cmd;  return False;}Boolean RTSPClient::setMediaSessionParameter(MediaSession& /*session*/,					     char const* parameterName,					     char const* parameterValue) {  char* cmd = NULL;  do {    // First, make sure that we have a RTSP session in progress    if (fLastSessionId == NULL) {      envir().setResultMsg(NoSessionErr);      break;    }    // Send the SET_PARAMETER command:    // First, construct an authenticator string:    char* authenticatorStr      = createAuthenticatorString(&fCurrentAuthenticator,				  "SET_PARAMETER", fBaseURL);    char* const cmdFmt =      "SET_PARAMETER %s RTSP/1.0\r\n"      "CSeq: %d\r\n"      "Session: %s\r\n"      "%s"      "%s"      "Content-length: %d\r\n\r\n"      "%s: %s\r\n";    unsigned parameterNameLen = strlen(parameterName);     unsigned parameterValueLen = strlen(parameterValue);     unsigned cmdSize = strlen(cmdFmt)      + strlen(fBaseURL)      + 20 /* max int len */      + strlen(fLastSessionId)      + strlen(authenticatorStr)      + fUserAgentHeaderStrSize      + parameterNameLen + parameterValueLen;    cmd = new char[cmdSize];    sprintf(cmd, cmdFmt,	    fBaseURL,	    ++fCSeq,	    fLastSessionId,	    authenticatorStr,	    fUserAgentHeaderStr,	    parameterNameLen + parameterValueLen + 2, // the "+ 2" is for the \r\n after the parameter "name: value"	    parameterName, parameterValue);    delete[] authenticatorStr;    if (!sendRequest(cmd, "SET_PARAMETER")) break;    // Get the response from the server:    unsigned bytesRead; unsigned responseCode;    char* firstLine; char* nextLineStart;    if (!getResponse("SET_PARAMETER", bytesRead, responseCode, firstLine, nextLineStart)) break;    delete[] cmd;    return True;  } while (0);  delete[] cmd;  return False;}Boolean RTSPClient::getMediaSessionParameter(MediaSession& /*session*/,					     char const* parameterName,					     char*& parameterValue) {  parameterValue = NULL; // default result  Boolean const haveParameterName = parameterName != NULL && parameterName[0] != '\0';  char* cmd = NULL;  do {    // First, make sure that we have a RTSP session in progress    if (fLastSessionId == NULL) {      envir().setResultMsg(NoSessionErr);      break;    }    // Send the GET_PARAMETER command:    // First, construct an authenticator string:    char* authenticatorStr      = createAuthenticatorString(&fCurrentAuthenticator,				  "GET_PARAMETER", fBaseURL);    if (haveParameterName) {      char* const cmdFmt =	"GET_PARAMETER %s RTSP/1.0\r\n"	"CSeq: %d\r\n"	"Session: %s\r\n"	"%s"	"%s"	"Content-type: text/parameters\r\n"	"Content-length: %d\r\n\r\n"	"%s\r\n";            unsigned parameterNameLen = strlen(parameterName);       unsigned cmdSize = strlen(cmdFmt)	+ strlen(fBaseURL)	+ 20 /* max int len */	+ strlen(fLastSessionId)	+ strlen(authenticatorStr)	+ fUserAgentHeaderStrSize	+ parameterNameLen;      cmd = new char[cmdSize];      sprintf(cmd, cmdFmt,	      fBaseURL,	      ++fCSeq,	      fLastSessionId,	      authenticatorStr,	      fUserAgentHeaderStr,	      parameterNameLen + 2, // the "+ 2" is for the \r\n after the parameter name	      parameterName);    } else {      char* const cmdFmt =	"GET_PARAMETER %s RTSP/1.0\r\n"	"CSeq: %d\r\n"	"Session: %s\r\n"	"%s"	"%s"	"\r\n";            unsigned cmdSize = strlen(cmdFmt)	+ strlen(fBaseURL)	+ 20 /* max int len */	+ strlen(fLastSessionId)	+ strlen(authenticatorStr)	+ fUserAgentHeaderStrSize;      cmd = new char[cmdSize];      sprintf(cmd, cmdFmt,	      fBaseURL,	      ++fCSeq,	      fLastSessionId,	      authenticatorStr,	      fUserAgentHeaderStr);    }    delete[] authenticatorStr;    if (!sendRequest(cmd, "GET_PARAMETER")) break;    // Get the response from the server:    // This section was copied/modified from the RTSPClient::describeURL func    unsigned bytesRead; unsigned responseCode;    char* firstLine; char* nextLineStart;    if (!getResponse("GET_PARAMETER", bytesRead, responseCode, firstLine,             nextLineStart, False /*don't check for response code 200*/)) break;    // Inspect the first line to check whether it's a result code that    // we can handle.    if (responseCode != 200) {      envir().setResultMsg("cannot handle GET_PARAMETER response: ", firstLine);      break;    }    // Skip every subsequent header line, until we see a blank line    // The remaining data is assumed to be the parameter data that we want.    char* serverType = new char[fResponseBufferSize]; // ensures enough space    int contentLength = -1;    char* lineStart;    while (1) {      lineStart = nextLineStart;      if (lineStart == NULL) break;      nextLineStart = getLine(lineStart);      if (lineStart[0] == '\0') break; // this is a blank line      if (sscanf(lineStart, "Content-Length: %d", &contentLength) == 1	  || sscanf(lineStart, "Content-length: %d", &contentLength) == 1) {	if (contentLength < 0) {	  envir().setResultMsg("Bad \"Content-length:\" header: \"",			       lineStart, "\"");	  break;	}      }    }     delete[] serverType;    // We're now at the end of the response header lines    if (lineStart == NULL) {      envir().setResultMsg("no content following header lines: ",                             fResponseBuffer);      break;    }    // Use the remaining data as the parameter data, but first, check    // the "Content-length:" header (if any) that we saw.  We may need to    // read more data, or we may have extraneous data in the buffer.    char* bodyStart = nextLineStart;    if (contentLength >= 0) {      // We saw a "Content-length:" header      unsigned numBodyBytes = &firstLine[bytesRead] - bodyStart;      if (contentLength > (int)numBodyBytes) {	// We need to read more data.  First, make sure we have enough	// space for it:	unsigned numExtraBytesNeeded = contentLength - numBodyBytes;	unsigned remainingBufferSize	  = fResponseBufferSize - (bytesRead + (firstLine - fResponseBuffer));	if (numExtraBytesNeeded > remainingBufferSize) {	  char tmpBuf[200];	  sprintf(tmpBuf, "Read buffer size (%d) is too small for \"Content-length:\" %d (need a buffer size of >= %d bytes\n",		  fResponseBufferSize, contentLength,		  fResponseBufferSize + numExtraBytesNeeded - remainingBufferSize);	  envir().setResultMsg(tmpBuf);	  break;	}	// Keep reading more data until we have enough:	if (fVerbosityLevel >= 1) {	  envir() << "Need to read " << numExtraBytesNeeded		  << " extra bytes\n";	}	while (numExtraBytesNeeded > 0) {	  struct sockaddr_in fromAddress;	  char* ptr = &firstLine[bytesRead];	  int bytesRead2 = readSocket(envir(), fInputSocketNum, (unsigned char*)ptr,				      numExtraBytesNeeded, fromAddress);	  if (bytesRead2 < 0) break;	  ptr[bytesRead2] = '\0';	  if (fVerbosityLevel >= 1) {	    envir() << "Read " << bytesRead2 << " extra bytes: "		    << ptr << "\n";	  }	  bytesRead += bytesRead2;	  numExtraBytesNeeded -= bytesRead2;	}	if (numExtraBytesNeeded > 0) break; // one of the reads failed      }    }    if (haveParameterName	&& !parseGetParameterHeader(bodyStart, parameterName, parameterValue)) break;    delete[] cmd;    return True;  } while (0);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -