[Live-devel] Removing sessions from RTSP Server

Greg Bothe greg1 at netrats.cc
Fri Jul 2 12:32:47 PDT 2004


Well, my fix for removing a session from the RTSP server was not complete. 
The application removes the session from the RTSP server then closes the
stream data associated with the stream (rtcpInstance, sink, source, sockets,
etc).  This causes a client to receive a BYE, causing it to send a TEAR_DOWN
and the RTSPClientSession in the server will try to use the pointer to the
session (fOurServerMediaSession).  The application will then rightfully crash
because the session was deleted in the remove for the RTSPServer.

To fix this quickly, I added a simple reference count to the
ServerMediaSession class (fRefCount).  It is initialized to 1 in the
constructor (if you're constructing, the someone is referencing it).  I then
added the following methods:

void ServerMediaSession::incrementRefCount()
{
   fRefCount++;
}

void ServerMediaSession::decrementRefCount()
{
   fRefCount--;
   if (fRefCount == 0) {
      delete this;
   }
}

Then, in the RTSPServer.cpp, I changed all delete's on the session to
decrementRefCount().  For the destructor of RTSPClientSession, I added a
decrementRefCount() if the session is not NULL.  Likewise, where the session
is lookup up in RTSPClientSession, I increment the reference count,
incrementRefCount().  For example, the removeServerMediaSession() method becomes:

Boolean RTSPServer
::removeServerMediaSession(ServerMediaSession* serverMediaSession) {
  char const* sessionName = serverMediaSession->streamName();
  if (sessionName == NULL) sessionName = "";
  ServerMediaSession* existingSession
    = (ServerMediaSession*)
    (fServerMediaSessions->Lookup(sessionName));
  if(existingSession != NULL) {
    fServerMediaSessions->Remove(sessionName);
    existingSession->decrementRefCount();
    return True;
  }
  else {
    return False;
  }
}

I realize that this is a pretty quick hack.  Does anybody have any better
suggestions or am I going about this all wrong?  Will the feature of being
able to remove streams from the RTSP server eventually be added?

Thanks.

---

Greg Bothe


---------- Original Message -----------
From: "Greg Bothe" <greg1 at netrats.cc>
To: live-devel at ns.live.com
Sent: Thu, 1 Jul 2004 15:58:56 -0600
Subject: [Live-devel] Removing sessions from RTSP Server

> I ran into a small difficulty when managing a RTSP server.  For my
> application, sessions come and go dynamically; the RTSP server does 
> not send out a constant set of audio streams.  The RTSPServer class 
> has a addServerMediaSession() method but no corresponding 
> removeServerMediaSession() method.  I suppose on most applications 
> this isn't a problem, as all media sessions will get removed when 
> RTSPServer destructs.  However, for applications such as mine, I 
> need to tear down sessions throughout the lifetime of the 
> application.  I may be going about this wrong, but I believe I have 
> solved my problem by adding the following method to RTSPServer:
> 
> Boolean RTSPServer
> ::removeServerMediaSession(ServerMediaSession* serverMediaSession) {
>   char const* sessionName = serverMediaSession->streamName();
>   if (sessionName == NULL) sessionName = "";
>   ServerMediaSession* existingSession
>     = (ServerMediaSession*)
>     (fServerMediaSessions->Lookup(sessionName));
>   if(existingSession != NULL) {
>     // Session is found, so remove it
>     fServerMediaSessions->Remove(sessionName);
>     delete existingSession;
>     return True;
>   }
>   else {
>     return False;
>   }
> }
> 
> In doing this change, I also believe I found a bug.  In 
> HashTable.cpp, I had to change the RemoveNext method from:
> 
> void* HashTable::RemoveNext() {
>   Iterator* iter = Iterator::create(*this);
>   char const* key;
>   void* removedValue = iter->next(key);
>   Remove(key);
> 
>   return removedValue;
> }
> 
> to
> 
> void* HashTable::RemoveNext() {
>   Iterator* iter = Iterator::create(*this);
>   char const* key;
>   void* removedValue = iter->next(key);
>   if (removedValue != 0) {
>     Remove(key);
>   }
> 
>   return removedValue;
> }
> 
> The code doesn't check to see if the next key actually exists before 
> it tries to remove the key.  Thus, Remove() gets passed a random 
> memory address
> (because key isn't initialized to NULL) and the program crashes.
> 
> I've been using the LiveMedia code now for a couple months.  It's 
> been a GREAT help.  For the record, I have the LiveMedia code 
> compiled on Windows as a DLL under Visual Studio 2003 so that I can 
> dynamically link to it.  This took a little bit of doing and it 
> would be WONDERFUL if the standard LiveMedia distribution supported 
> DLL creation.  (If it does already, then I must have way missed 
> something.)  I wish I was developing under UNIX, but unfortunately,
>  I'm stuck on Windows for this project.
> 
> Greg Bothe
> 
> _______________________________________________
> live-devel mailing list
> live-devel at lists.live.com
> http://lists.live.com/mailman/listinfo/live-devel
------- End of Original Message -------



More information about the live-devel mailing list