5、RTSP服务运做git
基础基本搞明白了,那么RTSP,RTP等这些协议又是如何利用这些基础机制运做的呢?首先来看RTSP.RTSP首先需创建TCP侦听socket。可见于此函数:session
[cpp] view plaincopyapp
DynamicRTSPServer* DynamicRTSPServer::createNew(UsageEnvironment& env, Port ourPort, dom
UserAuthenticationDatabase* authDatabase, socket
unsigned reclamationTestSeconds) { 函数
int ourSocket = setUpOurSocket(env, ourPort); //创建TCP socket this
if (ourSocket == -1) url
return NULL; spa
return new DynamicRTSPServer(env, ourSocket, ourPort, authDatabase, .net
reclamationTestSeconds);
}
要帧听客户端的链接,就须要利用任务调度机制了,因此需添加一个socket handler。可见于此函数:
[cpp] view plaincopy
RTSPServer::RTSPServer(UsageEnvironment& env,
int ourSocket,
Port ourPort,
UserAuthenticationDatabase* authDatabase,
unsigned reclamationTestSeconds) :
Medium(env),
fRTSPServerSocket(ourSocket),
fRTSPServerPort(ourPort),
fHTTPServerSocket(-1),
fHTTPServerPort(0),
fClientSessionsForHTTPTunneling(NULL),
fAuthDB(authDatabase),
fReclamationTestSeconds(reclamationTestSeconds),
fServerMediaSessions(HashTable::create(STRING_HASH_KEYS))
{
#ifdef USE_SIGNALS
// Ignore the SIGPIPE signal, so that clients on the same host that are killed
// don't also kill us:
signal(SIGPIPE, SIG_IGN);
#endif
// Arrange to handle connections from others:
env.taskScheduler().turnOnBackgroundReadHandling(
fRTSPServerSocket,
(TaskScheduler::BackgroundHandlerProc*) &incomingConnectionHandlerRTSP,
this);
}
当收到客户的链接时需保存下表明客户端的新socket,之后用这个socket与这个客户通信。每一个客户未来会对应一个rtp会话,并且各客户的RTSP请求只控制本身的rtp会话,那么最好创建一个会话类,表明各客户的rtsp会话。因而类RTSPServer::RTSPClientSession产生,它保存的表明客户的socket。下为RTSPClientSession的建立过程
[cpp] view plaincopy
void RTSPServer::incomingConnectionHandler(int serverSocket)
{
struct sockaddr_in clientAddr;
SOCKLEN_T clientAddrLen = sizeof clientAddr;
//接受链接
int clientSocket = accept(serverSocket,
(struct sockaddr*) &clientAddr,
&clientAddrLen);
if (clientSocket < 0) {
int err = envir().getErrno();
if (err != EWOULDBLOCK) {
envir().setResultErrMsg("accept() failed: ");
}
return;
}
//设置socket的参数
makeSocketNonBlocking(clientSocket);
increaseSendBufferTo(envir(), clientSocket, 50 * 1024);
#ifdef DEBUG
envir() << "accept()ed connection from " << our_inet_ntoa(clientAddr.sin_addr) << "\n";
#endif
//产生一个sesson id
// Create a new object for this RTSP session.
// (Choose a random 32-bit integer for the session id (it will be encoded as a 8-digit hex number). We don't bother checking for
// a collision; the probability of two concurrent sessions getting the same session id is very low.)
// (We do, however, avoid choosing session id 0, because that has a special use (by "OnDemandServerMediaSubsession").)
unsigned sessionId;
do {
sessionId = (unsigned) our_random();
} while (sessionId == 0);
//建立RTSPClientSession,注意传入的参数
(void) createNewClientSession(sessionId, clientSocket, clientAddr);
}
RTSPClientSession要提供什么功能呢?能够想象:须要监听客户端的rtsp请求并回应它,须要在DESCRIBE请求中返回所请求的流的信息,须要在SETUP请求中创建起RTP会话,须要在TEARDOWN请求中关闭RTP会话,等等...
RTSPClientSession要侦听客户端的请求,就需把本身的socket handler加入计划任务。证据以下:
[cpp] view plaincopy
RTSPServer::RTSPClientSession::RTSPClientSession(
RTSPServer& ourServer,
unsigned sessionId,
int clientSocket,
struct sockaddr_in clientAddr) :
fOurServer(ourServer),
fOurSessionId(sessionId),
fOurServerMediaSession(NULL),
fClientInputSocket(clientSocket),
fClientOutputSocket(clientSocket),
fClientAddr(clientAddr),
fSessionCookie(NULL),
fLivenessCheckTask(NULL),
fIsMulticast(False),
fSessionIsActive(True),
fStreamAfterSETUP(False),
fTCPStreamIdCount(0),
fNumStreamStates(0),
fStreamStates(NULL),
fRecursionCount(0)
{
// Arrange to handle incoming requests:
resetRequestBuffer();
envir().taskScheduler().turnOnBackgroundReadHandling(fClientInputSocket,
(TaskScheduler::BackgroundHandlerProc*) &incomingRequestHandler,
this);
noteLiveness();
}
下面重点讲一下下RTSPClientSession响应DESCRIBE请求的过程:
[cpp] view plaincopy
void RTSPServer::RTSPClientSession::handleCmd_DESCRIBE(
char const* cseq,
char const* urlPreSuffix,
char const* urlSuffix,
char const* fullRequestStr)
{
char* sdpDescription = NULL;
char* rtspURL = NULL;
do {
//整理一下下RTSP地址
char urlTotalSuffix[RTSP_PARAM_STRING_MAX];
if (strlen(urlPreSuffix) + strlen(urlSuffix) + 2
> sizeof urlTotalSuffix) {
handleCmd_bad(cseq);
break;
}
urlTotalSuffix[0] = '\0';
if (urlPreSuffix[0] != '\0') {
strcat(urlTotalSuffix, urlPreSuffix);
strcat(urlTotalSuffix, "/");
}
strcat(urlTotalSuffix, urlSuffix);
//验证账户和密码
if (!authenticationOK("DESCRIBE", cseq, urlTotalSuffix, fullRequestStr))
break;
// We should really check that the request contains an "Accept:" #####
// for "application/sdp", because that's what we're sending back #####
// Begin by looking up the "ServerMediaSession" object for the specified "urlTotalSuffix":
//跟据流的名字查找ServerMediaSession,若是找不到,会建立一个。每一个ServerMediaSession中至少要包含一个
//ServerMediaSubsession。一个ServerMediaSession对应一个媒体,能够认为是Server上的一个文件,或一个实时获取设备。其包含的每一个ServerMediaSubSession表明媒体中的一个Track。因此一个ServerMediaSession对应一个媒体,若是客户请求的媒体名相同,就使用已存在的ServerMediaSession,若是不一样,就建立一个新的。一个流对应一个StreamState,StreamState与ServerMediaSubsession相关,但表明的是动态的,而ServerMediaSubsession表明静态的。
ServerMediaSession* session = fOurServer.lookupServerMediaSession(urlTotalSuffix);
if (session == NULL) {
handleCmd_notFound(cseq);
break;
}
// Then, assemble a SDP description for this session:
//获取SDP字符串,在函数内会依次获取每一个ServerMediaSubSession的字符串然链接起来。
sdpDescription = session->generateSDPDescription();
if (sdpDescription == NULL) {
// This usually means that a file name that was specified for a
// "ServerMediaSubsession" does not exist.
snprintf((char*) fResponseBuffer, sizeof fResponseBuffer,
"RTSP/1.0 404 File Not Found, Or In Incorrect Format\r\n"
"CSeq: %s\r\n"
"%s\r\n", cseq, dateHeader());
break;
}
unsigned sdpDescriptionSize = strlen(sdpDescription);
// Also, generate our RTSP URL, for the "Content-Base:" header
// (which is necessary to ensure that the correct URL gets used in
// subsequent "SETUP" requests).
rtspURL = fOurServer.rtspURL(session, fClientInputSocket);
//造成响应DESCRIBE请求的RTSP字符串。
snprintf((char*) fResponseBuffer, sizeof fResponseBuffer,
"RTSP/1.0 200 OK\r\nCSeq: %s\r\n"
"%s"
"Content-Base: %s/\r\n"
"Content-Type: application/sdp\r\n"
"Content-Length: %d\r\n\r\n"
"%s", cseq, dateHeader(), rtspURL, sdpDescriptionSize,
sdpDescription);
} while (0);
delete[] sdpDescription;
delete[] rtspURL;
//返回后会被当即发送(没有把socket write操做放入计划任务中)。
}
fOurServer.lookupServerMediaSession(urlTotalSuffix)中会在找不到同名ServerMediaSession时新建一个,表明一个RTP流的ServerMediaSession们是被RTSPServer管理的,而不是被RTSPClientSession拥有。为何呢?由于ServerMediaSession表明的是一个静态的流,也就是能够从它里面获取一个流的各类信息,但不能获取传输状态。不一样客户可能链接到同一个流,因此ServerMediaSession应被RTSPServer所拥有。建立一个ServerMediaSession过程值得一观: