<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=us-ascii"><meta name=Generator content="Microsoft Word 12 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:purple;
text-decoration:underline;}
span.EmailStyle17
{mso-style-type:personal-compose;
font-family:"Calibri","sans-serif";
color:windowtext;}
.MsoChpDefault
{mso-style-type:export-only;}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--></head><body lang=EN-US link=blue vlink=purple><div class=WordSection1><p class=MsoNormal style='margin-bottom:10.0pt;line-height:115%'>Lately I've seen some random exceptions when streams are being shut down. I believe these were introduced when I changed the code so it waits for a TEARDOWN response. This was btw done as Cisco mentioned their server requires that.<o:p></o:p></p><p class=MsoNormal style='margin-bottom:10.0pt;line-height:115%'>I'm probably doing something wrong, as it appears the error happens in, or are related to, the TEARDOWN response handler.<o:p></o:p></p><p class=MsoNormal style='margin-bottom:10.0pt;line-height:115%'>With the shutdownStream() function in testRTSPClient as a model I basically added a response handler to the sendTeardownCommand() and added an additional cleanup function that should take care of the final cleanup and properly close the rtspClient.<o:p></o:p></p><p class=MsoNormal style='margin-bottom:10.0pt;line-height:115%'><b>- Can't I assume the rtspClient is valid after TEARDOWN?<o:p></o:p></b></p><p class=MsoNormal style='margin-bottom:10.0pt;line-height:115%'><b>- If not, how should I then properly execute Medium::close(rtspClient)?<o:p></o:p></b></p><p class=MsoNormal style='margin-bottom:10.0pt;line-height:115%'><b>- In cleanUpStream() I delete all event triggers. This needs to be done, right, or is it somehow that's taken care of automatically?<o:p></o:p></b></p><p class=MsoNormal style='margin-bottom:10.0pt;line-height:115%'><o:p> </o:p></p><p class=MsoNormal style='margin-bottom:10.0pt;line-height:115%'>The exception has never occurred while stepping through the code, but the exception appears to occur right after sendTeardownCommand() has been called.<o:p></o:p></p><p class=MsoNormal style='margin-bottom:10.0pt;line-height:115%'>Sample code below. Is that the proper way to clean up?<o:p></o:p></p><p class=MsoNormal style='margin-bottom:10.0pt;line-height:115%'><o:p> </o:p></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New";color:blue'>void</span><span style='font-family:"Courier New"'> continueAfterTEARDOWN(RTSPClient* rtspClient, <span style='color:blue'>int</span> resultCode, <span style='color:blue'>char</span>* resultString)<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'>{<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'> <span style='color:blue'>if</span> (resultCode != 0)<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'> {<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'> UsageEnvironment& env = rtspClient->envir();<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'> env << *rtspClient << <span style='color:#A31515'>"TEARDOWN result code: "</span> << resultCode << <span style='color:#A31515'>".\n"</span>;<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'> }<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'><o:p> </o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'> cleanUpStream(rtspClient);<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'>}<o:p></o:p></span></p><p class=MsoNormal style='margin-bottom:10.0pt;line-height:115%'><o:p> </o:p></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New";color:blue'>void</span><span style='font-family:"Courier New"'> shutdownStream(RTSPClient* rtspClient, <span style='color:blue'>int</span> exitCode)<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'>{<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'> UsageEnvironment& env = rtspClient->envir();<span style='color:green'><o:p></o:p></span></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'> StreamClientState& scs = ((OurRTSPClient*)rtspClient)->scs;<span style='color:green'> <o:p></o:p></span></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'><o:p> </o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'> <span style='color:green'>// First, check whether any subsessions have still to be closed:<o:p></o:p></span></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'> <span style='color:blue'>if</span> (scs.session != NULL)<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'> { <o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'> Boolean someSubsessionsWereActive = False;<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'> MediaSubsessionIterator iter(*scs.session);<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'> MediaSubsession* subsession;<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'><o:p> </o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'> <span style='color:blue'>while</span> ((subsession = iter.next()) != NULL)<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'> {<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'> <span style='color:blue'>if</span> (subsession->sink != NULL)<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'> {<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'> Medium::close(subsession->sink);<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'> subsession->sink = NULL;<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'><o:p> </o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'> <span style='color:blue'>if</span> (subsession->rtcpInstance() != NULL)<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'> {<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'> <span style='color:green'>// in case the server sends a RTCP "BYE" while handling "TEARDOWN"<o:p></o:p></span></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'> subsession->rtcpInstance()->setByeHandler(NULL, NULL);<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'> }<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'><o:p> </o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'> someSubsessionsWereActive = True;<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'> }<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'> }<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'><o:p> </o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'> <span style='color:blue'>if</span> (someSubsessionsWereActive)<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'> {<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'> <span style='color:green'>// Send a RTSP "TEARDOWN" command, to tell the server to shutdown the stream.<o:p></o:p></span></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'> <span style='color:green'>// We now want to handle the response.<o:p></o:p></span></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'> rtspClient->sendTeardownCommand(*scs.session, continueAfterTEARDOWN);<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'> }<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'> <span style='color:blue'>else<o:p></o:p></span></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'> {<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'> cleanUpStream(rtspClient);<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'> }<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'> }<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'> <span style='color:blue'>else<o:p></o:p></span></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'> {<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'> cleanUpStream(rtspClient);<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'> }<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'>}<o:p></o:p></span></p><p class=MsoNormal style='margin-bottom:10.0pt;line-height:115%'><o:p> </o:p></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New";color:green'>// Aka shutdownStream part 2. We do the final cleanup in a separate function as we<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New";color:green'>// want to wait until we receive the TEARDOWN response.<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New";color:blue'>void</span><span style='font-family:"Courier New"'> cleanUpStream(RTSPClient* rtspClient)<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'>{<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'> UsageEnvironment& env = rtspClient->envir();<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'><o:p> </o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'> env.taskScheduler().deleteEventTrigger(((OurRTSPClient*)rtspClient)->myStartStreamEvent);<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'> env.taskScheduler().deleteEventTrigger(((OurRTSPClient*)rtspClient)->myStopStreamEvent);<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'> env.taskScheduler().deleteEventTrigger(((OurRTSPClient*)rtspClient)->mySeekAbsoluteEvent);<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'><o:p> </o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'> env << *rtspClient << <span style='color:#A31515'>"Closing the stream.\n"</span>;<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'> Medium::close(rtspClient);<o:p></o:p></span></p><p class=MsoNormal style='text-autospace:none'><span style='font-family:"Courier New"'> <span style='color:green'>// Note that this will also cause this stream's "StreamClientState" structure to get reclaimed.<o:p></o:p></span></span></p><p class=MsoNormal style='margin-bottom:10.0pt;line-height:115%'><span style='font-family:"Courier New"'>}</span><o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>/Claes<o:p></o:p></p></div></body></html>