rtsptran.cpp
上传用户:zhongxx05
上传日期:2007-06-06
资源大小:33641k
文件大小:40k
源码类别:

Symbian

开发平台:

C/C++

  1. /* ***** BEGIN LICENSE BLOCK ***** 
  2.  * Version: RCSL 1.0/RPSL 1.0 
  3.  *  
  4.  * Portions Copyright (c) 1995-2002 RealNetworks, Inc. All Rights Reserved. 
  5.  *      
  6.  * The contents of this file, and the files included with this file, are 
  7.  * subject to the current version of the RealNetworks Public Source License 
  8.  * Version 1.0 (the "RPSL") available at 
  9.  * http://www.helixcommunity.org/content/rpsl unless you have licensed 
  10.  * the file under the RealNetworks Community Source License Version 1.0 
  11.  * (the "RCSL") available at http://www.helixcommunity.org/content/rcsl, 
  12.  * in which case the RCSL will apply. You may also obtain the license terms 
  13.  * directly from RealNetworks.  You may not use this file except in 
  14.  * compliance with the RPSL or, if you have a valid RCSL with RealNetworks 
  15.  * applicable to this file, the RCSL.  Please see the applicable RPSL or 
  16.  * RCSL for the rights, obligations and limitations governing use of the 
  17.  * contents of the file.  
  18.  *  
  19.  * This file is part of the Helix DNA Technology. RealNetworks is the 
  20.  * developer of the Original Code and owns the copyrights in the portions 
  21.  * it created. 
  22.  *  
  23.  * This file, and the files included with this file, is distributed and made 
  24.  * available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 
  25.  * EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS ALL SUCH WARRANTIES, 
  26.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS 
  27.  * FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 
  28.  * 
  29.  * Technology Compatibility Kit Test Suite(s) Location: 
  30.  *    http://www.helixcommunity.org/content/tck 
  31.  * 
  32.  * Contributor(s): 
  33.  *  
  34.  * ***** END LICENSE BLOCK ***** */ 
  35. #include "hxtypes.h"
  36. #include "hxassert.h"
  37. #include "debug.h"
  38. #include "hxcom.h"
  39. #include "hxmarsh.h"
  40. #include "hxstrutl.h"
  41. #include "netbyte.h"
  42. #include "hxengin.h"
  43. #include "ihxpckts.h"
  44. #include "hxcomm.h"
  45. #include "hxmon.h"
  46. #include "netbyte.h"
  47. #include "hxstring.h"
  48. #include "chxpckts.h"
  49. #include "hxslist.h"
  50. #include "hxmap.h"
  51. #include "hxdeque.h"
  52. #include "hxbitset.h"
  53. #include "timebuff.h"
  54. #include "timeval.h"
  55. #include "tconverter.h"
  56. #include "rtptypes.h"
  57. #include "rtspmsg.h"
  58. #include "hxcorgui.h"
  59. #include "ntptime.h"
  60. #include "rtspif.h"
  61. #include "rtsptran.h"
  62. //#include "rtpwrap.h" // Wrappers for PMC generated base classes
  63. #include "basepkt.h"
  64. #include "hxtbuf.h"
  65. #include "transbuf.h"
  66. #include "hxtick.h"
  67. #include "random32.h" // random32()
  68. #include "pkthndlr.h" // in rtpmisc for RTCP routine
  69. #include "rtcputil.h" // takes care of RTCP in RTP mode
  70. #include "hxprefs.h" // IHXPreferences
  71. #include "hxmime.h"
  72. #include "hxcore.h"
  73. #include "hxheap.h"
  74. #ifdef PAULM_IHXTCPSCAR
  75. #include "objdbg.h"
  76. #endif
  77. #ifdef PAULM_TNGTCPTRANSTIMING
  78. #include "classtimer.h"
  79. ClassTimer g_TNGTCPTransTimer("TNGTCPTransport", 0, 3600);
  80. #endif
  81. #ifdef _DEBUG
  82. #undef HX_THIS_FILE
  83. static const char HX_THIS_FILE[] = __FILE__;
  84. #endif
  85. #define STREAM_END_DELAY_BASE_TOLERANCE 3000
  86. static const UINT32 TRANSPORT_BUF_GROWTH_RATE  = 1000;
  87. void
  88. dump(const char* pFile, const char* pc)
  89. {
  90.     FILE* fp = fopen(pFile, "a");
  91.     if(!fp)
  92.     {
  93.     return;
  94.     }
  95.     fprintf(fp, "%s", pc);
  96.     fclose(fp);    
  97. }
  98. RTSPStreamData::RTSPStreamData(BOOL needReliable):
  99.     m_seqNo(0),
  100.     m_streamNumber(0),
  101.     m_reliableSeqNo(0),
  102.     m_lastTimestamp(0),
  103.     m_pTransportBuffer(0),
  104.     m_pResendBuffer(0),
  105.     m_pStreamStats(0),
  106.     m_bNeedReliable(needReliable),
  107.     m_packetSent(FALSE),
  108.     m_bReceivedAllPackets(FALSE),
  109.     m_bNeedToACK(FALSE),
  110.     m_bFirstPacket(TRUE),
  111.     m_bUsesRTPPackets(FALSE),
  112.     m_pTSConverter(NULL),
  113.     m_pTSOrderHack(NULL),
  114.     m_eMediaType(RTSPMEDIA_TYPE_UNKNOWN)
  115. {
  116.     ;
  117. }
  118. RTSPStreamData::~RTSPStreamData()
  119. {
  120.     if (m_pTransportBuffer)
  121.     {
  122. delete m_pTransportBuffer;
  123.     }
  124.     if (m_pResendBuffer)
  125.     {
  126. delete m_pResendBuffer;
  127.     }
  128.     HX_DELETE(m_pTSConverter);
  129.     HX_DELETE(m_pTSOrderHack);
  130. }
  131. RTSPStreamHandler::RTSPStreamHandler(RTSPTransport* pOwner)
  132.     : m_pOwner(pOwner)
  133.     , m_lRefCount(0)
  134. {
  135.     m_pStreamDataMap = new CHXMapLongToObj;
  136. }
  137. RTSPStreamHandler::~RTSPStreamHandler()
  138. {
  139.     CHXMapLongToObj::Iterator i;
  140.     RTSPStreamData* pStreamData;
  141.     for(i=m_pStreamDataMap->Begin();i!=m_pStreamDataMap->End();++i)
  142.     {
  143. pStreamData = (RTSPStreamData*)(*i);
  144. delete pStreamData;
  145.     }
  146.     delete m_pStreamDataMap;
  147. }
  148. HX_RESULT
  149. RTSPStreamHandler::initStreamData(
  150.     UINT16 streamNumber, BOOL needReliable, BOOL bIsSource, INT16 rtpPayloadType,
  151.     BOOL bRecordFlag, UINT32 wrapSequenceNumber, UINT32 ulBufferDepth,
  152.     BOOL bHasOutOfOrderTS, CHXTimestampConverter* pTSConverter,
  153.     RTSPMediaType eMediaType)
  154. {
  155.     RTSPStreamData* pStreamData;
  156.     if(!m_pStreamDataMap->Lookup(streamNumber, (void*&)pStreamData))
  157.     {
  158. pStreamData = new RTSPStreamData(needReliable);
  159. pStreamData->m_streamNumber = streamNumber;
  160.         pStreamData->m_pTSConverter = pTSConverter;
  161. pStreamData->m_eMediaType = eMediaType;
  162. pStreamData->m_lastSeqNo = 0;
  163. (*m_pStreamDataMap)[streamNumber] = pStreamData;
  164. if (!bIsSource)
  165. {
  166.     UINT32 ulSetMaximumBufferDepth;
  167.     UINT32 ulSetBufferDepth;
  168.     if (bRecordFlag && ulBufferDepth != TRANSPORT_BUF_DURATION_UNDEF)
  169.     {
  170.      ulSetMaximumBufferDepth = ulBufferDepth;
  171.      ulSetBufferDepth = ulBufferDepth;
  172.     }
  173.     else if (bRecordFlag)
  174.     {
  175.      ulSetMaximumBufferDepth = MAX_TRANSPORT_BUF_DURATION;
  176.      ulSetBufferDepth = MAX_TRANSPORT_BUF_DURATION;
  177.     }
  178.     else
  179.     {
  180.      ulSetMaximumBufferDepth = MAX_TRANSPORT_BUF_DURATION;
  181.      ulSetBufferDepth = TRANSPORT_BUF_DURATION;
  182.     }
  183.     RTSPTransportBuffer* pTransportBuffer = 
  184. new RTSPTransportBuffer(m_pOwner,
  185.                                 streamNumber,
  186.                                 ulSetBufferDepth,
  187.                                 ulSetMaximumBufferDepth,
  188.                                 TRANSPORT_BUF_GROWTH_RATE,
  189.                                 wrapSequenceNumber);
  190.     pStreamData->m_pTransportBuffer = pTransportBuffer;
  191.     pStreamData->m_pStreamStats = NULL;
  192.     pStreamData->m_bUsesRTPPackets = bHasOutOfOrderTS;
  193. }
  194. return HXR_OK;
  195.     }
  196.     return HXR_FAIL;
  197. }
  198. RTSPStreamData*
  199. RTSPStreamHandler::getStreamData(UINT16 streamNumber)
  200. {
  201.     RTSPStreamData* pStreamData = 0;
  202.     if(!m_pStreamDataMap->Lookup(streamNumber, (void*&)pStreamData))
  203.     {
  204. return 0;
  205.     }
  206.     return pStreamData;
  207. }
  208. RTSPStreamData*
  209. RTSPStreamHandler::firstStreamData()
  210. {
  211.     streamIterator = m_pStreamDataMap->Begin();
  212.     if(streamIterator == m_pStreamDataMap->End())
  213.     {
  214. return 0;
  215.     }
  216.     return (RTSPStreamData*)(*streamIterator);
  217. }
  218. RTSPStreamData*
  219. RTSPStreamHandler::nextStreamData()
  220. {
  221.     ++streamIterator;
  222.     if(streamIterator == m_pStreamDataMap->End())
  223.     {
  224. return 0;
  225.     }
  226.     return (RTSPStreamData*)(*streamIterator);
  227. }
  228. BOOL
  229. RTSPStreamHandler::endStreamData()
  230. {
  231.     return (streamIterator == m_pStreamDataMap->End());
  232. }
  233. HX_RESULT
  234. RTSPStreamHandler::createResendBuffer(UINT16 streamNumber,
  235.       UINT32 wrapSequenceNumber)
  236. {
  237.     RTSPStreamData* pStreamData;
  238.     if(!m_pStreamDataMap->Lookup(streamNumber, (void*&)pStreamData))
  239.     {
  240. return HXR_FAILED;
  241.     }
  242.     RTSPResendBuffer* pResendBuffer;
  243.     pResendBuffer = new RTSPResendBuffer(RESEND_BUF_DURATION,
  244.                                          MAX_RESEND_BUF_DURATION,
  245.                                          RESEND_BUF_GROWTH_RATE,
  246.                                          wrapSequenceNumber);
  247.     pStreamData->m_pResendBuffer = pResendBuffer;
  248.     return HXR_OK;
  249. }
  250. RTSPResendBuffer*
  251. RTSPStreamHandler::getResendBuffer(UINT16 streamNumber)
  252. {
  253.     RTSPStreamData* pStreamData = 0;
  254.     if(!m_pStreamDataMap->Lookup(streamNumber, (void*&)pStreamData))
  255.     {
  256. return 0;
  257.     }
  258.     return pStreamData->m_pResendBuffer;
  259. }
  260. /*
  261.  * RTSPTransport methods
  262.  */
  263. RTSPTransport::RTSPTransport(BOOL bIsSource):
  264.     m_pContext(NULL),
  265.     m_pCommonClassFactory(0),
  266.     m_pScheduler(0),
  267.     m_pResp(0),
  268.     m_pStreamHandler(0),
  269.     m_pRegistry(0),
  270.     m_pInternalReset(0),
  271.     m_pSrcBufferStats(0),
  272.     m_bIsSource(bIsSource),
  273.     m_bIsInitialized(FALSE),
  274.     m_bIsUpdated(FALSE),
  275.     m_ulPacketsSent(0),
  276.     m_lBytesSent(0),
  277.     m_bIsReceivedData(FALSE),
  278.     m_bSourceDone(FALSE),
  279.     m_bHackedRecordFlag(FALSE),
  280.     m_wrapSequenceNumber(0),
  281.     m_bMulticast(FALSE),
  282.     m_pPlayerState(NULL),
  283.     m_pPacketFilter(NULL),
  284.     m_pClientPacketList(NULL),
  285.     m_bPrefetch(FALSE),
  286.     m_bFastStart(FALSE),
  287.     m_ulPlayRangeFrom(RTSP_PLAY_RANGE_BLANK),
  288.     m_ulPlayRangeTo(RTSP_PLAY_RANGE_BLANK),
  289.     m_bPlayRequestSent(FALSE),
  290.     m_packets_since_last_drop(0),
  291.     m_ulTotalSuccessfulResends(0),
  292.     m_ulTotalFailedResends(0),
  293.     m_ulSendingTime(0),
  294.     m_drop_packets(FALSE),
  295.     m_bSkipTimeAdjustment(FALSE)
  296. #ifdef RDT_MESSAGE_DEBUG
  297.     ,m_bRDTMessageDebug(FALSE)
  298. #endif    
  299. {
  300.     m_ulStartTime = HX_GET_TICKCOUNT();
  301. }
  302. RTSPTransport::~RTSPTransport()
  303. {
  304.     HX_RELEASE(m_pCommonClassFactory);
  305.     HX_RELEASE(m_pScheduler);
  306.     HX_RELEASE(m_pResp);
  307.     HX_RELEASE(m_pRegistry);
  308.     if (m_pStreamHandler)
  309.     {
  310.         m_pStreamHandler->Release();
  311.         m_pStreamHandler = NULL;
  312.     }
  313.     HX_RELEASE(m_pInternalReset);
  314.     HX_RELEASE(m_pSrcBufferStats);
  315.     HX_RELEASE(m_pPlayerState);
  316.     HX_RELEASE(m_pPacketFilter);
  317.     HX_RELEASE(m_pContext);
  318.     HX_DELETE(m_pClientPacketList);
  319. }
  320. void
  321. RTSPTransport::addStreamInfo(RTSPStreamInfo* pStreamInfo, UINT32 ulBufferDepth)
  322. {
  323.     if(pStreamInfo)
  324.     {
  325. if(!m_pStreamHandler)
  326. {
  327.     m_pStreamHandler = new RTSPStreamHandler(this);
  328.     m_pStreamHandler->AddRef();
  329. }
  330. CHXTimestampConverter*  pTSConverter = NULL;
  331. if (pStreamInfo->m_HXFactor && pStreamInfo->m_RTPFactor)
  332. {
  333.     pTSConverter = new CHXTimestampConverter(CHXTimestampConverter::FACTORS,
  334.      pStreamInfo->m_HXFactor,
  335.      pStreamInfo->m_RTPFactor);
  336.  }
  337.  else if (pStreamInfo->m_sampleRate)
  338.  {
  339.      
  340.      pTSConverter =  new CHXTimestampConverter(CHXTimestampConverter::SAMPLES,
  341.        pStreamInfo->m_sampleRate);
  342.  }
  343.  m_pStreamHandler->initStreamData(
  344.      pStreamInfo->m_streamNumber,
  345.      pStreamInfo->m_bNeedReliablePackets,
  346.      m_bIsSource,
  347.      pStreamInfo->m_rtpPayloadType,
  348.      m_bHackedRecordFlag,
  349.      m_wrapSequenceNumber,
  350.      ulBufferDepth,
  351.      pStreamInfo->m_bHasOutOfOrderTS,
  352.      pTSConverter,
  353.      pStreamInfo->m_eMediaType);
  354.         m_bSkipTimeAdjustment = pStreamInfo->m_bRealMedia;
  355. RTSPStreamData* pStreamData = NULL;
  356. pStreamData = m_pStreamHandler->getStreamData(pStreamInfo->m_streamNumber);
  357. if (pStreamData && pStreamData->m_pTransportBuffer && m_bPrefetch)
  358. {
  359.     pStreamData->m_pTransportBuffer->EnterPrefetch();
  360. }
  361.     }
  362. }
  363. void
  364. RTSPTransport::setFirstTimeStamp(UINT16 uStreamNumber, UINT32 ulTS, 
  365.                                  BOOL bIsRaw)
  366. {
  367.     RTSPStreamData* pStreamData = 
  368. m_pStreamHandler->getStreamData(uStreamNumber);
  369.     if (pStreamData)
  370.     {
  371. if (pStreamData->m_pTSConverter)
  372. {
  373.     pStreamData->m_pTSConverter->setHXAnchor(ulTS);
  374. }
  375.          
  376. HX_DELETE(pStreamData->m_pTSOrderHack);
  377.     }
  378.     if (!m_bIsSource)
  379.     {
  380. RTSPTransportBuffer* pTransportBuffer = pStreamData->m_pTransportBuffer;
  381. if (pTransportBuffer && pStreamData)
  382. {
  383.     if ((m_ulPlayRangeFrom != RTSP_PLAY_RANGE_BLANK) &&
  384. (m_ulPlayRangeTo != RTSP_PLAY_RANGE_BLANK))
  385.     {
  386. if ((pStreamData->m_eMediaType == RTSPMEDIA_TYPE_AUDIO) ||
  387.     (pStreamData->m_eMediaType == RTSPMEDIA_TYPE_VIDEO))
  388. {
  389.     // For audio & video media, we'll inform transport of
  390.     // the media duration to help determining stream termination
  391.     pStreamData->m_pTransportBuffer->InformTimestampRange(
  392. m_ulPlayRangeFrom,
  393. m_ulPlayRangeTo,
  394. STREAM_END_DELAY_BASE_TOLERANCE);
  395. }
  396.     }
  397. }
  398.     }
  399. }
  400. void 
  401. RTSPTransport::setPlayRange(UINT32 ulFrom, UINT32 ulTo)
  402. {
  403.     // this is the Range values in PLAY request in RMA time (ms) called on PLAY 
  404.     // request
  405.     m_ulPlayRangeFrom = ulFrom; 
  406.     m_ulPlayRangeTo = ulTo;
  407. }
  408. void
  409. RTSPTransport::setSessionID(const char* pSessionID)
  410. {
  411.     m_sessionID = pSessionID;
  412. }
  413. UINT16
  414. RTSPTransport::getSeqNum(UINT16 streamNumber)
  415. {
  416.     RTSPStreamData* pStreamData;
  417.     pStreamData = m_pStreamHandler->getStreamData(streamNumber);
  418.     if(pStreamData)
  419.     {
  420. return pStreamData->m_seqNo;
  421.     }
  422.     else
  423.     {
  424. return 0; //XXXBAB - 0xffff?
  425.     }
  426. }
  427. UINT32
  428. RTSPTransport::getTimestamp(UINT16 streamNumber)
  429. {
  430.     RTSPStreamData* pStreamData;
  431.     pStreamData = m_pStreamHandler->getStreamData(streamNumber);
  432.     if(pStreamData)
  433.     {
  434. // XXXGo - this is RTP time w/ offset (NOT RMA time) if RTPUDPTransprot...
  435. return pStreamData->m_lastTimestamp;
  436.     }
  437.     else
  438.     {
  439. return 0;
  440.     }
  441. }
  442. HX_RESULT
  443. RTSPTransport::getPacket(UINT16 uStreamNumber, IHXPacket*& pPacket)
  444. {
  445.     RTSPTransportBuffer* pTransportBuffer = getTransportBuffer(uStreamNumber);
  446.     RTSPStreamData* pStreamData = m_pStreamHandler->getStreamData(uStreamNumber);
  447.     if ((!pTransportBuffer) || (!pStreamData))
  448.     {
  449. return HXR_FAIL;
  450.     }
  451.     ClientPacket* clientPacket;
  452.     HX_RESULT result = pTransportBuffer->GetPacket(clientPacket);
  453.     if (result != HXR_OK)
  454.     {
  455. return result;
  456.     }
  457.     pPacket = clientPacket->GetPacket();
  458.     if (!pPacket)
  459.     {
  460. /*
  461.  * This is a lost packet
  462.  */
  463. result = m_pCommonClassFactory->CreateInstance(CLSID_IHXPacket,
  464.                                                (void**)&pPacket);
  465. if (result != HXR_OK)
  466. {
  467.     return result;
  468. }
  469. UINT8 unASMFlags = 0;
  470. UINT32 ulTime = 0;
  471. if (clientPacket->IsDroppedPacket())
  472. {
  473.     // Preserve dropped flag as an ASM flag.
  474.     // This allows other code along the packet
  475.     // path to differentiate this packet from a 
  476.     // true lost packet.
  477.     unASMFlags |= HX_ASM_DROPPED_PKT;
  478.     // We have a valid timestamp for a dropped
  479.     // packet so lets put it in the IHXPacket
  480.     ulTime = clientPacket->GetTime();
  481. }
  482. pPacket->Set(0, ulTime, uStreamNumber, unASMFlags, 0);
  483. pPacket->SetAsLost();
  484.     }
  485.     else if (pStreamData->m_bUsesRTPPackets)
  486.     {
  487. if (!pStreamData->m_pTSOrderHack)
  488. {
  489.     pStreamData->m_pTSOrderHack = new RTSPStreamData::TSOrderHackInfo();
  490.     
  491.     if (pStreamData->m_pTSOrderHack)
  492.     {
  493. pStreamData->m_pTSOrderHack->m_ulLastSentTS = 
  494.     pPacket->GetTime();
  495. pStreamData->m_pTSOrderHack->m_ulLastRecvTS = 
  496.     pStreamData->m_pTSOrderHack->m_ulLastSentTS;
  497.     }
  498. }
  499. if (pStreamData->m_pTSOrderHack)
  500. {
  501.     IHXBuffer* pBuf=NULL;     
  502.     UINT32 ulHX;
  503.     UINT32 ulRTP;
  504.     UINT16 unStrmNo;
  505.     UINT8  uchASMFlag;
  506.     UINT16 unRuleNo;
  507.     // pkts've been sorted based on seq_no
  508.     IHXRTPPacket* pRTPPacket = NULL;
  509.     pPacket->QueryInterface(IID_IHXRTPPacket, (void**) &pRTPPacket);
  510.     if (pRTPPacket)
  511.     {
  512. // RTP transport generates RTP packets
  513. result = pRTPPacket->GetRTP(pBuf, ulHX, ulRTP, 
  514.     unStrmNo, uchASMFlag, unRuleNo);
  515.     }
  516.     else
  517.     {
  518. // RDT transport generates RMA packets
  519. result = pPacket->Get(pBuf, ulHX,
  520.       unStrmNo, uchASMFlag, unRuleNo);
  521. if (pStreamData->m_pTSConverter)
  522. {
  523.     ulRTP = pStreamData->m_pTSConverter->hxa2rtp(ulHX);
  524. }
  525. else
  526. {
  527.     ulRTP = ulHX;
  528. }
  529.     }
  530.     HX_ASSERT(result == HXR_OK);
  531.     if (result == HXR_OK)
  532.     {
  533. if (((LONG32) (ulHX - pStreamData->m_pTSOrderHack->m_ulLastSentTS)) > 0)
  534. {
  535.     pStreamData->m_pTSOrderHack->m_ulLastSentTS = ulHX;
  536.     pStreamData->m_pTSOrderHack->m_ulLastRecvTS = ulHX;
  537. }
  538. else if (ulHX == pStreamData->m_pTSOrderHack->m_ulLastRecvTS)
  539. {
  540.     ulHX = pStreamData->m_pTSOrderHack->m_ulLastSentTS;          
  541. }
  542. else
  543. {
  544.     pStreamData->m_pTSOrderHack->m_ulLastRecvTS = ulHX;
  545.     ulHX = (++pStreamData->m_pTSOrderHack->m_ulLastSentTS);
  546. }
  547. HX_RELEASE(pRTPPacket);
  548. HX_RELEASE(pPacket);
  549. pRTPPacket = new CHXRTPPacket;
  550. pRTPPacket->AddRef();
  551. result = pRTPPacket->SetRTP(pBuf,
  552.     ulHX,
  553.     ulRTP,
  554.     unStrmNo,
  555.     uchASMFlag,
  556.     unRuleNo);
  557. HX_ASSERT(result == HXR_OK);
  558. pRTPPacket->QueryInterface(IID_IHXPacket, (void**) &pPacket);
  559. HX_ASSERT(pPacket);
  560.     }
  561.     HX_RELEASE(pBuf);
  562.     HX_RELEASE(pRTPPacket);
  563. }
  564.     }
  565.     /*
  566.      * No longer need the ClientPacket wrapper
  567.      */
  568.     HX_RELEASE(clientPacket);    
  569.     return HXR_OK;
  570. }
  571. HX_RESULT
  572. RTSPTransport::startPackets(UINT16 uStreamNumber)
  573. {
  574.     RTSPTransportBuffer* pTransportBuffer = getTransportBuffer(uStreamNumber);
  575.     if (!pTransportBuffer)
  576.     {
  577. return HXR_FAIL;
  578.     }
  579.     return pTransportBuffer->StartPackets();
  580. }
  581. HX_RESULT
  582. RTSPTransport::stopPackets(UINT16 uStreamNumber)
  583. {
  584.     RTSPTransportBuffer* pTransportBuffer = getTransportBuffer(uStreamNumber);
  585.     if (!pTransportBuffer)
  586.     {
  587. return HXR_FAIL;
  588.     }
  589.     return pTransportBuffer->StopPackets();
  590. }
  591. void
  592. RTSPTransport::SetFilterResponse(RawPacketFilter* pResp)
  593. {
  594.     HX_RELEASE(m_pPacketFilter);
  595.     m_pPacketFilter = pResp;
  596.     if (m_pPacketFilter)
  597.     {
  598. m_pPacketFilter->AddRef();
  599.     }
  600. }
  601. void
  602. RTSPTransport::FilterPacket(IHXPacket* pPacket)
  603. {
  604.     UINT16 uStreamNumber;
  605.     ClientPacket* clientPacket = 0;
  606.     clientPacket = (ClientPacket*)m_pClientPacketList->RemoveTail();
  607.     clientPacket->SetPacket(pPacket);
  608.     
  609.     uStreamNumber = clientPacket->GetStreamNumber();
  610.     RTSPTransportBuffer* pTransportBuffer = getTransportBuffer(uStreamNumber);
  611.     pTransportBuffer->Add(clientPacket);
  612. }
  613. void RTSPTransport::GetContext(IUnknown*& pContext)   
  614. {   
  615. pContext = m_pContext;   
  616. if (pContext)   
  617. {   
  618. pContext->AddRef();   
  619. }   
  620. void 
  621. RTSPTransport::LeavePrefetch(void)
  622. {
  623.     RTSPStreamData* pStreamData = NULL; 
  624.     
  625.     m_bPrefetch = FALSE;
  626.     HX_ASSERT(m_pStreamHandler);
  627.     pStreamData = m_pStreamHandler->firstStreamData();
  628.     while(pStreamData)    
  629.     {
  630. if (pStreamData->m_pTransportBuffer)
  631. {
  632.     pStreamData->m_pTransportBuffer->LeavePrefetch();
  633. }
  634. pStreamData = m_pStreamHandler->nextStreamData();
  635.     }
  636.     return;
  637. }
  638. void 
  639. RTSPTransport::EnterFastStart(void)
  640. {
  641.     RTSPStreamData* pStreamData = NULL; 
  642.     
  643.     m_bFastStart = TRUE;
  644.     HX_ASSERT(m_pStreamHandler);
  645.     pStreamData = m_pStreamHandler->firstStreamData();
  646.     while(pStreamData)    
  647.     {
  648. if (pStreamData->m_pTransportBuffer)
  649. {
  650.     pStreamData->m_pTransportBuffer->EnterFastStart();
  651. }
  652. pStreamData = m_pStreamHandler->nextStreamData();
  653.     }
  654.     return;
  655. }
  656. void 
  657. RTSPTransport::LeaveFastStart(void)
  658. {
  659.     RTSPStreamData* pStreamData = NULL; 
  660.     
  661.     m_bFastStart = FALSE;
  662.     HX_ASSERT(m_pStreamHandler);
  663.     pStreamData = m_pStreamHandler->firstStreamData();
  664.     while(pStreamData)    
  665.     {
  666. if (pStreamData->m_pTransportBuffer)
  667. {
  668.     pStreamData->m_pTransportBuffer->LeaveFastStart();
  669. }
  670. pStreamData = m_pStreamHandler->nextStreamData();
  671.     }
  672.     return;
  673. }
  674. BOOL 
  675. RTSPTransport::isSparseStream(UINT16 uStreamNumber)
  676. {
  677.     BOOL bResult = FALSE;
  678.     const char* pMimeType = NULL;
  679.     IUnknown* pUnknown = NULL;
  680.     IHXStreamSource* pStreamSource = NULL;    
  681.     IHXStream* pStream = NULL;
  682.     if (m_pContext  &&
  683. HXR_OK == m_pContext->QueryInterface(IID_IHXStreamSource, (void**)&pStreamSource))
  684.     {
  685. if (HXR_OK == pStreamSource->GetStream(uStreamNumber, pUnknown))
  686. {
  687.     if (HXR_OK == pUnknown->QueryInterface(IID_IHXStream, (void**)&pStream))
  688.     {
  689. pMimeType = pStream->GetStreamType();
  690. // special handling for sparsed streams for multicast
  691. if (pMimeType &&
  692.     (strcasecmp(SYNCMM_MIME_TYPE, pMimeType) == 0 ||
  693.      strcasecmp(REALEVENT_MIME_TYPE, pMimeType) == 0 ||
  694.      strcasecmp("application/vnd.rn-realtext", pMimeType) == 0 ||
  695.      strcasecmp("application/x-pn-realtext", pMimeType) == 0))
  696. {
  697.     bResult = TRUE;
  698. }
  699.     }
  700.     HX_RELEASE(pStream);
  701. }
  702. HX_RELEASE(pUnknown);
  703.     }
  704.     HX_RELEASE(pStreamSource);
  705.     return bResult;
  706. }
  707. HX_RESULT
  708. RTSPTransport::storePacket(IHXPacket* pPacket,
  709.                            UINT16 uStreamNumber,
  710.                            UINT16 uSeqNo,
  711.                            UINT16 uReliableSeqNo,
  712.                            BOOL isReliable)
  713. {
  714.     RTSPTransportBuffer* pTransportBuffer = getTransportBuffer(uStreamNumber);
  715.     if (!pTransportBuffer)
  716.     {
  717. return HXR_FAIL;
  718.     }
  719.     ClientPacket* clientPacket = 0;
  720.     
  721.     m_bIsReceivedData = TRUE;
  722.     if (pPacket->IsLost())
  723.     {
  724.         clientPacket = new ClientPacket(uSeqNo, uReliableSeqNo,
  725.                         pPacket->GetTime(), 0, isReliable,
  726.                         0, pTransportBuffer->GetTime(), FALSE);
  727. clientPacket->AddRef();
  728. return pTransportBuffer->Add(clientPacket);
  729.     }
  730.     
  731.     IHXBuffer* pBuffer = pPacket->GetBuffer();
  732.     if (m_pPacketFilter)
  733.     {
  734. if (!m_pClientPacketList)
  735. {
  736.     m_pClientPacketList = new CHXSimpleList;
  737.     m_pPacketFilter->SetFilterResponse(this);
  738. }
  739. clientPacket = new ClientPacket(uSeqNo, uReliableSeqNo,
  740. pPacket->GetTime(), pBuffer->GetSize(), isReliable,
  741. NULL, pTransportBuffer->GetTime(),
  742. FALSE);
  743. clientPacket->AddRef();
  744. pBuffer->Release();
  745. m_pClientPacketList->AddHead((void*)clientPacket);
  746. m_pPacketFilter->FilterPacket(pPacket);
  747. // just guessing on what this should be?????
  748.         return HXR_OK;
  749.     }
  750.     else
  751.     {
  752.        clientPacket = new ClientPacket(uSeqNo, uReliableSeqNo,
  753. pPacket->GetTime(), pBuffer->GetSize(), isReliable,
  754. pPacket, pTransportBuffer->GetTime(), FALSE);
  755. clientPacket->AddRef();
  756. pBuffer->Release();
  757. return pTransportBuffer->Add(clientPacket);
  758.     }
  759. }
  760. HX_RESULT
  761. RTSPTransport::packetReady(HX_RESULT status, RTSPStreamData* pStreamData, IHXPacket* pPacket)
  762. {
  763.     HX_RESULT result = HXR_OK;
  764.     ASSERT(!m_bIsSource);
  765.     if (!pStreamData)
  766.     {
  767. return HXR_UNEXPECTED;
  768.     }
  769.     HX_ASSERT(pStreamData);
  770.     
  771.     if (pPacket)
  772.     {
  773. if (pStreamData->m_bUsesRTPPackets)
  774. {
  775.     IHXRTPPacket* pRTPPacket = NULL;
  776.     pPacket->QueryInterface(IID_IHXRTPPacket, (void**) &pRTPPacket);
  777.     if (pRTPPacket)
  778.     {
  779. // This already is RTP Packet - proceed
  780. pRTPPacket->Release();
  781.     }
  782.     else
  783.     {
  784. // Must Transfer to an RTP Packet
  785. result = m_pCommonClassFactory->CreateInstance(
  786. CLSID_IHXRTPPacket,
  787. (void**) &pRTPPacket);
  788. if (pRTPPacket)
  789. {
  790.     ULONG32 ulTime;
  791.     ULONG32 ulRTPTime;
  792.     UINT16 uStreamNumber;
  793.     UINT8 unASMFlags;
  794.     UINT16 unASMRuleNumber;
  795.     BOOL bIsLost = pPacket->IsLost();
  796.     IHXBuffer* pBuffer = NULL;
  797.     pPacket->Get(pBuffer,
  798.  ulTime,
  799.  uStreamNumber,
  800.  unASMFlags,
  801.  unASMRuleNumber);
  802.     if (pStreamData->m_pTSConverter)
  803.     {
  804. ulRTPTime = pStreamData->m_pTSConverter->hxa2rtp(ulTime);
  805.     }
  806.     else
  807.     {
  808. ulRTPTime = ulTime;
  809.     }
  810.     pRTPPacket->SetRTP(pBuffer,
  811.        ulTime,
  812.        ulRTPTime,
  813.        uStreamNumber,
  814.        unASMFlags,
  815.        unASMRuleNumber);
  816.     if (bIsLost)
  817.     {
  818. pRTPPacket->SetAsLost();
  819.     }
  820.     HX_RELEASE(pBuffer);
  821.     result = m_pResp->PacketReady(status, m_sessionID, pRTPPacket);
  822.     pRTPPacket->Release();
  823. }
  824. return result;
  825.     }
  826. }
  827.     }
  828.     else
  829.     {
  830. /*
  831.  * This is a lost packet
  832.  */
  833. if (pStreamData->m_bUsesRTPPackets)
  834. {
  835.     result = m_pCommonClassFactory->CreateInstance(
  836. CLSID_IHXRTPPacket,
  837. (void**) &pPacket);
  838. }
  839. else
  840. {
  841.     result = m_pCommonClassFactory->CreateInstance(
  842. CLSID_IHXPacket,
  843. (void**) &pPacket);
  844. }
  845.     
  846. if (pPacket)
  847. {
  848.     pPacket->Set(0, 0, pStreamData->m_streamNumber, 0, 0);
  849.     pPacket->SetAsLost();
  850.     result = m_pResp->PacketReady(status, m_sessionID, pPacket);
  851.     pPacket->Release();
  852. }
  853. return result;
  854.     }
  855.     return m_pResp->PacketReady(status, m_sessionID, pPacket);
  856. }
  857. HX_RESULT
  858. RTSPTransport::packetReady(HX_RESULT status, UINT16 uStreamNumber, IHXPacket* pPacket)
  859. {
  860.     RTSPStreamData* pStreamData = NULL;
  861.     ASSERT(!m_bIsSource);
  862.     pStreamData = m_pStreamHandler->getStreamData(uStreamNumber);
  863.     HX_ASSERT(pStreamData);
  864.     return packetReady(status, pStreamData, pPacket);
  865. }
  866. HX_RESULT
  867. RTSPTransport::getStatus
  868. (
  869.     UINT16& uStatusCode, 
  870.     IHXBuffer*& pStatusDesc, 
  871.     UINT16& ulPercentDone
  872. )
  873. {
  874. #if 0
  875.     if (!m_pStreamHandler)
  876.     {
  877. uStatusCode = HX_STATUS_INITIALIZING;
  878. pStatusDesc = 0;
  879. ulPercentDone = 0;
  880. return HXR_OK;
  881.     }
  882.     uStatusCode = HX_STATUS_READY;
  883.     pStatusDesc = 0;
  884.     ulPercentDone = 100;
  885.     RTSPStreamData* pStreamData = m_pStreamHandler->firstStreamData();
  886.     ASSERT(pStreamData);
  887.     while(pStreamData)
  888.     {
  889. UINT16 tempStatusCode;
  890. UINT16 tempPercentDone;
  891. pStreamData->m_pTransportBuffer->GetStatus(tempStatusCode,
  892.                                            tempPercentDone);
  893. /*
  894.  * The status is always that of the stream which is least ready
  895.  */
  896. if (tempStatusCode < uStatusCode)
  897. {
  898.     uStatusCode     = tempStatusCode;
  899.     ulPercentDone   = tempPercentDone;
  900. }
  901. else if (tempStatusCode == uStatusCode && 
  902.  tempPercentDone < ulPercentDone)
  903. {
  904.     ulPercentDone = tempPercentDone;
  905. }
  906. pStreamData = m_pStreamHandler->nextStreamData();
  907.     }
  908.     return HXR_OK;
  909. #else
  910.     return HXR_NOTIMPL;
  911. #endif
  912. }
  913. HX_RESULT
  914. RTSPTransport::GetCurrentBuffering(UINT16  uStreamNumber,
  915.    INT64&  llLowestTimestamp, 
  916.    INT64&  llHighestTimestamp,
  917.    UINT32& ulNumBytes,
  918.    BOOL&   bDone)
  919. {
  920.     if (!m_pStreamHandler)
  921.     {
  922. return HXR_OK;
  923.     }
  924.     RTSPStreamData* pStreamData;
  925.     pStreamData = m_pStreamHandler->getStreamData(uStreamNumber);
  926.     HX_ASSERT(pStreamData);
  927.     return pStreamData ? 
  928.            pStreamData->m_pTransportBuffer->GetCurrentBuffering(
  929.                                             llLowestTimestamp,
  930.                                             llHighestTimestamp,
  931.                                             ulNumBytes,
  932.                                             bDone) : HXR_OK;
  933. }
  934. HX_RESULT
  935. RTSPTransport::SeekFlush(UINT16 uStreamNumber)
  936. {
  937.     if (!m_pStreamHandler)
  938.     {
  939. return HXR_OK;
  940.     }
  941.     RTSPStreamData* pStreamData;
  942.     pStreamData = m_pStreamHandler->getStreamData(uStreamNumber);
  943.     HX_ASSERT(pStreamData);
  944.     if (pStreamData)
  945.     {
  946. pStreamData->m_pTransportBuffer->SeekFlush();
  947.     }
  948.     return HXR_OK;
  949. }
  950. BOOL
  951. RTSPTransport::IsSourceDone(void)
  952. {
  953.     return m_bSourceDone;
  954. }
  955. void      
  956. RTSPTransport::CheckForSourceDone(UINT16 uStreamNumber)
  957. {
  958.     if (m_bSourceDone)
  959.     {
  960. return;
  961.     }
  962.     HX_ASSERT(m_pStreamHandler);
  963.     if (!m_pStreamHandler)
  964.     {
  965. return;
  966.     }
  967.     RTSPStreamData* pStreamData = m_pStreamHandler->firstStreamData();
  968.     HX_ASSERT(pStreamData);
  969.     m_bSourceDone = TRUE;
  970.     while(pStreamData)
  971.     {
  972. if (pStreamData->m_streamNumber == uStreamNumber)
  973. {
  974.     pStreamData->m_bReceivedAllPackets = TRUE;
  975. }
  976. else if (!pStreamData->m_bReceivedAllPackets)
  977. {
  978.     m_bSourceDone = FALSE;
  979.     return;
  980. }
  981. pStreamData = m_pStreamHandler->nextStreamData();
  982.     }
  983.     /* We have received all the packets... Tell the reposnse object */
  984.     if (!m_bIsSource)
  985.     {
  986. m_pResp->OnSourceDone();
  987.     }
  988. }    
  989. void      
  990. RTSPTransport::HandleBufferError()
  991. {
  992.     if (m_pResp)
  993.     {
  994. m_pResp->OnProtocolError(HXR_BUFFERING);
  995.     }
  996. }
  997. RTSPResendBuffer*
  998. RTSPTransport::getResendBuffer(UINT16 uStreamNumber)
  999. {
  1000.     return m_pStreamHandler->getResendBuffer(uStreamNumber);
  1001. }
  1002. HX_RESULT
  1003. RTSPTransport::initializeStatistics
  1004. (
  1005.     UINT32 ulRegistryID
  1006. )
  1007. {
  1008.     m_bIsInitialized = TRUE;
  1009.     if (m_bIsSource)
  1010.     {
  1011. m_ulRegistryID = ulRegistryID;
  1012.     }
  1013.     else
  1014.     {
  1015. // XXX HP
  1016. // rather than create a new copy of STREAM_STATS, we simply
  1017. // obtain the same STREAM_STATS* from RTSPProtocol via SetStatistics().
  1018. // this could save us on both the memory and several ms during the startup
  1019. /*
  1020. IHXBuffer* pParentName = NULL;
  1021. CHAR RegKeyName[MAX_DISPLAY_NAME] = {0};
  1022. RTSPStreamData* pStreamData = m_pStreamHandler->firstStreamData();
  1023. ASSERT(pStreamData);
  1024. if (!m_pRegistry)
  1025. {
  1026.     return HXR_FAIL;
  1027. }
  1028. HX_RESULT result = m_pRegistry->GetPropName(ulRegistryID, pParentName);
  1029. if (result != HXR_OK)
  1030. {
  1031.     return result;
  1032. }
  1033. // create registry entries of each stream
  1034. while (pStreamData)
  1035. {
  1036.     UINT16 uStreamNumber = pStreamData->m_streamNumber;
  1037.     sprintf(RegKeyName, "%s.Stream%d", pParentName->GetBuffer(),
  1038.                                        uStreamNumber);
  1039.     UINT32 uStreamId = m_pRegistry->GetId(RegKeyName);
  1040.     // create stream reg key if it has not been created
  1041.     // by the source yet
  1042.     if (!uStreamId)
  1043.     {
  1044. return HXR_FAIL;
  1045.     }
  1046.     pStreamData->m_pStreamStats = new STREAM_STATS(m_pRegistry,
  1047.                                                    uStreamId);
  1048.    
  1049.     pStreamData = m_pStreamHandler->nextStreamData();
  1050. }
  1051. pParentName->Release();
  1052. */
  1053.     }
  1054.     return HXR_OK;
  1055. }
  1056. HX_RESULT
  1057. RTSPTransport::SetStatistics(UINT16 uStreamNumber, STREAM_STATS* pStats)
  1058. {
  1059.     HX_RESULT     rc = HXR_OK;
  1060. #if defined(HELIX_FEATURE_STATS) && defined(HELIX_FEATURE_REGISTRY)
  1061.     RTSPStreamData* pStreamData = NULL;
  1062.     if (!m_pStreamHandler)
  1063.     {
  1064. rc = HXR_FAILED;
  1065. goto cleanup;
  1066.     }
  1067.     pStreamData = m_pStreamHandler->getStreamData(uStreamNumber);
  1068.     if (!pStreamData)
  1069.     {
  1070. rc = HXR_FAILED;
  1071. goto cleanup;
  1072.     }
  1073.     pStreamData->m_pStreamStats = pStats;
  1074. cleanup:
  1075. #endif /* HELIX_FEATURE_STATS && HELIX_FEATURE_REGISTRY */
  1076.     return rc;
  1077. }
  1078. HX_RESULT 
  1079. RTSPTransport::updateStatistics(BOOL bUseRegistry)
  1080. {
  1081.     m_bIsUpdated = TRUE;
  1082. #if defined(HELIX_FEATURE_STATS) && defined(HELIX_FEATURE_REGISTRY)
  1083.     if (m_bIsSource)
  1084.     {
  1085. RTSPStreamData* pStreamData = m_pStreamHandler->firstStreamData();
  1086. if (!pStreamData)
  1087. {
  1088.     return HXR_FAIL;
  1089. }
  1090. /*
  1091.  * XXXGH...streams are managed by the PPM and should not show up
  1092.  *         under the transport
  1093.  */
  1094. while (pStreamData)
  1095. {
  1096.     UINT32 ulResendSuccess = 0;
  1097.     UINT32 ulResendFailure = 0;
  1098.     if (pStreamData->m_pResendBuffer)
  1099.     {
  1100. pStreamData->m_pResendBuffer->UpdateStatistics(ulResendSuccess,
  1101.                                                ulResendFailure);
  1102.     }
  1103.     m_ulTotalSuccessfulResends += ulResendSuccess;
  1104.     m_ulTotalFailedResends += ulResendFailure;
  1105.     pStreamData = m_pStreamHandler->nextStreamData();
  1106. }
  1107.         m_ulSendingTime = CALCULATE_ELAPSED_TICKS(m_ulStartTime, HX_GET_TICKCOUNT()) / 1000;
  1108. IHXBuffer* pName;
  1109. if (bUseRegistry
  1110.         &&  m_pRegistry 
  1111.         &&  HXR_OK == m_pRegistry->GetPropName(m_ulRegistryID, pName))
  1112. {
  1113.     char str[512]; /* Flawfinder: ignore */
  1114.     const char* name = (const char*)pName->GetBuffer();
  1115.     char pTemp[32]; /* Flawfinder: ignore */
  1116.     i64toa(m_lBytesSent, pTemp, 10);
  1117.     sprintf(str, "%-.400s.PacketsSent", name); /* Flawfinder: ignore */
  1118.     m_pRegistry->AddInt(str, m_ulPacketsSent);
  1119.     sprintf(str, "%-.400s.BytesSent", name); /* Flawfinder: ignore */
  1120.     m_pRegistry->AddInt(str, INT64_TO_INT32(m_lBytesSent));
  1121.     // Add the total bytes sent as a string because we don't want
  1122.     // to have to truncate the INT64 value that is storing it
  1123.     sprintf(str, "%-.400s.TotalBytesSent", name); /* Flawfinder: ignore */
  1124.     IHXBuffer* pBuffer = new CHXBuffer();
  1125.     pBuffer->AddRef();
  1126.     pBuffer->Set((UCHAR*)pTemp, strlen(pTemp) + 1);
  1127.     m_pRegistry->AddStr(str, pBuffer);
  1128.     HX_RELEASE(pBuffer);
  1129.     sprintf(str, "%-.400s.SendingTime", name); /* Flawfinder: ignore */
  1130.     m_pRegistry->AddInt(str, m_ulSendingTime); 
  1131.     sprintf(str, "%-.400s.ResendSuccess", name); /* Flawfinder: ignore */
  1132.     m_pRegistry->AddInt(str, m_ulTotalSuccessfulResends);
  1133.     sprintf(str, "%-.400s.ResendFailure", name); /* Flawfinder: ignore */
  1134.     m_pRegistry->AddInt(str, m_ulTotalFailedResends);
  1135.     pName->Release();
  1136. }
  1137.     }
  1138.     else
  1139.     {
  1140. ULONG32 ulNormal = 0;
  1141. ULONG32 ulReceived = 0;
  1142. ULONG32 ulLost = 0;
  1143. ULONG32 ulLate = 0;
  1144. ULONG32 ulTotal = 0;
  1145. ULONG32 ulResendRequested = 0;
  1146. ULONG32 ulResendReceived = 0;
  1147. ULONG32 ulAvgBandwidth = 0;
  1148. ULONG32 ulCurBandwidth = 0;
  1149. UINT32 ulTotal30 = 0;
  1150. UINT32 ulLost30 = 0;
  1151. UINT32  ulDuplicate = 0;
  1152. UINT32 ulOutOfOrder = 0;
  1153. RTSPStreamData* pStreamData = m_pStreamHandler->firstStreamData();
  1154. if (!pStreamData)
  1155. {
  1156.     return HXR_FAIL;
  1157. }
  1158. while (pStreamData)
  1159. {
  1160.     STREAM_STATS* pStreamStats = pStreamData->m_pStreamStats;
  1161.             if (!pStreamStats || !pStreamStats->m_bInitialized)
  1162.     {
  1163. goto updateContinue;
  1164.     }
  1165.     pStreamData->m_pTransportBuffer->UpdateStatistics(ulNormal,
  1166.                                                       ulLost,
  1167.                                                       ulLate,
  1168.                                                       ulResendRequested,
  1169.                                                       ulResendReceived,
  1170.                                                       ulAvgBandwidth,
  1171.                                                       ulCurBandwidth,
  1172.       ulTotal30,
  1173.       ulLost30,
  1174.       ulDuplicate,
  1175.       ulOutOfOrder);
  1176.                   
  1177.     ulReceived = ulNormal + ulResendReceived;
  1178.     ulTotal = ulReceived + ulLost + ulLate;
  1179.     pStreamStats->m_pNormal->SetInt((INT32)ulNormal);
  1180.     pStreamStats->m_pRecovered->SetInt((INT32)ulResendReceived);
  1181.     pStreamStats->m_pReceived->SetInt((INT32)ulReceived);
  1182.     pStreamStats->m_pLost->SetInt((INT32)ulLost);
  1183.     pStreamStats->m_pLate->SetInt((INT32)ulLate);
  1184.     pStreamStats->m_pDuplicate->SetInt((INT32)ulDuplicate);
  1185.     pStreamStats->m_pOutOfOrder->SetInt((INT32)ulOutOfOrder);
  1186.     pStreamStats->m_pTotal->SetInt((INT32)ulTotal);
  1187.     pStreamStats->m_pLost30->SetInt((INT32)ulLost30);
  1188.     pStreamStats->m_pTotal30->SetInt((INT32)ulTotal30);
  1189.     pStreamStats->m_pResendRequested->SetInt((INT32)ulResendRequested);
  1190.     pStreamStats->m_pResendReceived->SetInt((INT32)ulResendReceived);
  1191.     pStreamStats->m_pAvgBandwidth->SetInt((INT32)ulAvgBandwidth);
  1192.     pStreamStats->m_pCurBandwidth->SetInt((INT32)ulCurBandwidth);
  1193. updateContinue:
  1194.     pStreamData = m_pStreamHandler->nextStreamData();
  1195. }
  1196.     }
  1197. #endif /* HELIX_FEATURE_STATS && HELIX_FEATURE_REGISTRY */
  1198.     return HXR_OK;
  1199. }
  1200. HX_RESULT 
  1201. RTSPTransport::UpdateRegistry(UINT32 ulStreamNumber,
  1202.       UINT32 ulRegistryID)
  1203. {
  1204.     if (m_bIsSource)
  1205.     {
  1206. m_ulRegistryID = ulRegistryID;
  1207.     }
  1208.     else
  1209.     {
  1210. // XXX HP
  1211. // rather than create a new copy of STREAM_STATS, we simply
  1212. // obtain the same STREAM_STATS* from RTSPProtocol via SetStatistics().
  1213. // this could save us on both the memory and several ms during the startup
  1214. /*
  1215. if (!m_pRegistry)
  1216. {
  1217.     return HXR_FAIL;
  1218. }
  1219. RTSPStreamData* pStreamData = m_pStreamHandler->firstStreamData();
  1220. while (pStreamData)
  1221. {
  1222.     if (pStreamData->m_streamNumber == (UINT16)ulStreamNumber)
  1223.     {
  1224. HX_DELETE(pStreamData->m_pStreamStats);
  1225. pStreamData->m_pStreamStats = new STREAM_STATS(m_pRegistry,
  1226.        ulRegistryID);
  1227. break;
  1228.     }    
  1229.     pStreamData = m_pStreamHandler->nextStreamData();
  1230. }
  1231. */
  1232.     }
  1233.     return HXR_OK;
  1234. }
  1235. RTSPTransportBuffer*
  1236. RTSPTransport::getTransportBuffer(UINT16 uStreamNumber)
  1237. {
  1238.     if (!m_pStreamHandler)
  1239.     {
  1240. return NULL;
  1241.     }
  1242.     RTSPStreamData* pStreamData;     
  1243.     pStreamData = m_pStreamHandler->getStreamData(uStreamNumber);
  1244.     if (!pStreamData)
  1245.     {
  1246. return NULL;
  1247.     }
  1248.     return pStreamData->m_pTransportBuffer;
  1249. }
  1250. HX_RESULT
  1251. RTSPTransport::playReset()
  1252. {
  1253.     m_bSourceDone = FALSE;
  1254.     if(m_pStreamHandler)
  1255.     {
  1256. RTSPStreamData* pStreamData = m_pStreamHandler->firstStreamData();
  1257. while(pStreamData)
  1258. {
  1259.     if (m_bIsSource)
  1260.     {
  1261. pStreamData->m_packetSent = FALSE;
  1262.     }
  1263.     else
  1264.     {
  1265. pStreamData->m_pTransportBuffer->Reset();
  1266. pStreamData->m_bReceivedAllPackets = FALSE;
  1267.     }
  1268.     pStreamData = m_pStreamHandler->nextStreamData();
  1269. }
  1270.     }
  1271.     return HXR_OK;
  1272. }
  1273. HX_RESULT
  1274. RTSPTransport::pauseBuffers()
  1275. {
  1276.     if (m_pStreamHandler)
  1277.     {
  1278. RTSPStreamData* pStreamData = m_pStreamHandler->firstStreamData();
  1279. ASSERT(pStreamData);
  1280. while(pStreamData)
  1281. {
  1282.     pStreamData->m_pTransportBuffer->Pause();
  1283.     pStreamData = m_pStreamHandler->nextStreamData();
  1284. }
  1285.     }
  1286.     return HXR_OK;
  1287. }
  1288. HX_RESULT
  1289. RTSPTransport::resumeBuffers()
  1290. {
  1291.     m_bIsReceivedData = FALSE;
  1292.     if (m_pStreamHandler)
  1293.     {
  1294. RTSPStreamData* pStreamData = m_pStreamHandler->firstStreamData();
  1295. ASSERT(pStreamData);
  1296. while(pStreamData)
  1297. {
  1298.     pStreamData->m_pTransportBuffer->Resume();
  1299.     pStreamData = m_pStreamHandler->nextStreamData();
  1300. }
  1301.     }
  1302.     return HXR_OK;
  1303. }
  1304. HX_RESULT
  1305. RTSPTransport::setFirstSeqNum(UINT16 uStreamNumber, UINT16 uSeqNum)
  1306. {
  1307.     RTSPStreamData* pStreamData;
  1308.     pStreamData = m_pStreamHandler->getStreamData(uStreamNumber);
  1309.     if(pStreamData)
  1310.     {
  1311. if (m_bIsSource)
  1312. {     
  1313.     pStreamData->m_seqNo = uSeqNum;
  1314.     if(pStreamData->m_pResendBuffer)
  1315.     {
  1316. pStreamData->m_pResendBuffer->SetFirstSequenceNumber(uSeqNum);
  1317.     }
  1318. }
  1319. else
  1320. {
  1321.     if (!pStreamData->m_pTransportBuffer)
  1322.     {
  1323. return HXR_FAIL;
  1324.     }
  1325.     if (!m_bMulticast)
  1326.     {
  1327. pStreamData->m_lastSeqNo = uSeqNum;
  1328.      pStreamData->m_pTransportBuffer->Init(uSeqNum);
  1329.     }
  1330. }
  1331.     }
  1332.     return HXR_OK;
  1333. }
  1334. HX_RESULT
  1335. RTSPTransport::Init(IUnknown* pContext)
  1336. {
  1337.     HX_RESULT hresult;
  1338.     
  1339.     if (!m_pContext)
  1340.     {
  1341. m_pContext = pContext;
  1342. m_pContext->AddRef();
  1343.     }
  1344.     hresult = pContext->QueryInterface(IID_IHXCommonClassFactory,
  1345.        (void**)&m_pCommonClassFactory);
  1346.     if (HXR_OK != hresult)
  1347.     {
  1348. return hresult;
  1349.     }
  1350.     
  1351.     hresult = pContext->QueryInterface(IID_IHXScheduler,
  1352.                                        (void**)&m_pScheduler);
  1353.     if (HXR_OK != hresult)
  1354.     {
  1355. DPRINTF(D_INFO, ("could not get scheduler...n"));
  1356. return hresult;
  1357.     }
  1358.     pContext->QueryInterface(IID_IHXRegistry, (void**)&m_pRegistry);
  1359.     pContext->QueryInterface(IID_IHXInternalReset,
  1360.                                (void**)&m_pInternalReset);
  1361.     pContext->QueryInterface(IID_IHXPlayerState,
  1362.                                (void**)&m_pPlayerState);
  1363.     pContext->QueryInterface(IID_IHXSourceBufferingStats2,
  1364.      (void**)&m_pSrcBufferStats);
  1365. #ifdef RDT_MESSAGE_DEBUG
  1366.     IHXPreferences* pPreferences = NULL;
  1367.     
  1368.     if (pContext &&
  1369. (HXR_OK == pContext->QueryInterface(IID_IHXPreferences,
  1370.                                             (void**) &pPreferences)))
  1371.     {
  1372. IHXBuffer* pBuffer = NULL;
  1373.         ReadPrefBOOL(pPreferences, "RDTMessageDebug", m_bRDTMessageDebug);
  1374. if (m_bRDTMessageDebug)
  1375. {
  1376.     if (HXR_OK == pPreferences->ReadPref("RDTMessageDebugFile", pBuffer))
  1377.     {
  1378. if (pBuffer->GetSize() <= 0)
  1379. {
  1380.     // no file name, no log
  1381.     m_bRDTMessageDebug = FALSE;
  1382. }
  1383. else
  1384. {
  1385.     m_RDTmessageDebugFileName = (const char*) pBuffer->GetBuffer();
  1386. }
  1387.     }
  1388.             HX_RELEASE(pBuffer);
  1389. }
  1390.     }
  1391.     HX_RELEASE(pPreferences);
  1392. #endif // RDT_MESSAGE_DEBUG
  1393.     
  1394.     return HXR_OK;
  1395. }
  1396. HX_RESULT
  1397. RTSPTransport::SetResendBufferDepth(UINT32 uMilliseconds)
  1398. {
  1399.     RTSPStreamData* pStreamData = m_pStreamHandler->firstStreamData();
  1400.     ASSERT(pStreamData);
  1401.     while (pStreamData)
  1402.     {
  1403. if (m_bIsSource)
  1404. {
  1405.     if (pStreamData->m_pResendBuffer)
  1406.     {
  1407. pStreamData->m_pResendBuffer->SetBufferDepth(uMilliseconds);
  1408.     }
  1409. }
  1410. else
  1411. {
  1412.     if (pStreamData->m_pTransportBuffer)
  1413.     {
  1414. pStreamData->m_pTransportBuffer->SetBufferDepth(uMilliseconds);
  1415.     }
  1416. }
  1417. pStreamData = m_pStreamHandler->nextStreamData();
  1418.     }
  1419.     return HXR_OK;
  1420. }
  1421. #ifdef RDT_MESSAGE_DEBUG
  1422. void RTSPTransport::RDTmessageFormatDebugFileOut(const char* fmt, ...)
  1423. {
  1424.     if(m_bRDTMessageDebug)
  1425.     {
  1426.         char buf[4096]; /* Flawfinder: ignore */
  1427. va_list args;
  1428. va_start(args, fmt);
  1429. FILE* fp = fopen((const char*)m_RDTmessageDebugFileName, "a");
  1430. if (fp)
  1431. {
  1432.     vsprintf(buf, fmt, args);
  1433.     fprintf(fp, "%sn", buf);
  1434.     fclose(fp);
  1435. }
  1436. va_end(args);
  1437.     }
  1438. }
  1439. #endif // RDT_MESSAGE_DEBUG