hxntsrc.cpp
上传用户:dangjiwu
上传日期:2013-07-19
资源大小:42019k
文件大小:180k
源码类别:

Symbian

开发平台:

Visual C++

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Source last modified: $Id: hxntsrc.cpp,v 1.63.2.4 2004/07/13 17:12:04 ping Exp $
  3.  * 
  4.  * Portions Copyright (c) 1995-2004 RealNetworks, Inc. All Rights Reserved.
  5.  * 
  6.  * The contents of this file, and the files included with this file,
  7.  * are subject to the current version of the RealNetworks Public
  8.  * Source License (the "RPSL") available at
  9.  * http://www.helixcommunity.org/content/rpsl unless you have licensed
  10.  * the file under the current version of the RealNetworks Community
  11.  * Source License (the "RCSL") available at
  12.  * http://www.helixcommunity.org/content/rcsl, in which case the RCSL
  13.  * will apply. You may also obtain the license terms directly from
  14.  * RealNetworks.  You may not use this file except in compliance with
  15.  * the RPSL or, if you have a valid RCSL with RealNetworks applicable
  16.  * to this file, the RCSL.  Please see the applicable RPSL or RCSL for
  17.  * the rights, obligations and limitations governing use of the
  18.  * contents of the file.
  19.  * 
  20.  * Alternatively, the contents of this file may be used under the
  21.  * terms of the GNU General Public License Version 2 or later (the
  22.  * "GPL") in which case the provisions of the GPL are applicable
  23.  * instead of those above. If you wish to allow use of your version of
  24.  * this file only under the terms of the GPL, and not to allow others
  25.  * to use your version of this file under the terms of either the RPSL
  26.  * or RCSL, indicate your decision by deleting the provisions above
  27.  * and replace them with the notice and other provisions required by
  28.  * the GPL. If you do not delete the provisions above, a recipient may
  29.  * use your version of this file under the terms of any one of the
  30.  * RPSL, the RCSL or the GPL.
  31.  * 
  32.  * This file is part of the Helix DNA Technology. RealNetworks is the
  33.  * developer of the Original Code and owns the copyrights in the
  34.  * portions it created.
  35.  * 
  36.  * This file, and the files included with this file, is distributed
  37.  * and made available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY
  38.  * KIND, EITHER EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS
  39.  * ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES
  40.  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET
  41.  * ENJOYMENT OR NON-INFRINGEMENT.
  42.  * 
  43.  * Technology Compatibility Kit Test Suite(s) Location:
  44.  *    http://www.helixcommunity.org/content/tck
  45.  * 
  46.  * Contributor(s):
  47.  * 
  48.  * ***** END LICENSE BLOCK ***** */
  49. #include "hxcom.h"
  50. #include "hlxclib/stdio.h"
  51. #if defined (_UNIX) || defined (_WIN16)
  52. #include <stdlib.h>
  53. #endif
  54. #include "hxcomm.h"
  55. #include "hxplugn.h"
  56. #include "ihxpckts.h"
  57. #include "hxpends.h"
  58. #include "hxfiles.h"
  59. #include "hxengin.h"
  60. #include "hxcore.h"
  61. #include "hxprefs.h"
  62. #include "hxpref.h"
  63. #include "hxausvc.h"
  64. #include "hxasm.h"
  65. #include "hxauthn.h"
  66. #include "hxgroup.h"
  67. #include "hxsmbw.h"
  68. #include "hxrsdbf.h"
  69. #include "hxmime.h"
  70. #include "hxslist.h"
  71. #include "hxstring.h"
  72. #include "portaddr.h"
  73. #include "chxeven.h"
  74. #include "chxelst.h"
  75. #include "strminfo.h"
  76. #include "hxbsrc.h"
  77. #include "hxsrc.h"
  78. #include "hxntsrc.h"
  79. #include "hxstrm.h"
  80. #include "hxplay.h"
  81. #include "rtspif.h"
  82. #include "hxprotocol.h"
  83. #include "dtrvtcon.h"
  84. #include "rtspprotocol.h"
  85. #if defined(HELIX_FEATURE_PNA)
  86. #include "hxpnapro.h"
  87. #endif /* HELIX_FEATURE_PNA */
  88. #include "rmfftype.h"
  89. #include "prefdefs.h"
  90. #include "plprefk.h"
  91. #include "hxtick.h"
  92. #include "netbyte.h"
  93. #include "hxcom.h"
  94. #include "hxstrutl.h"
  95. #include "ihxpckts.h"
  96. #include "chxpckts.h"
  97. #include "hxmap.h"
  98. #include "hxstat.h"
  99. #include "hxmangle.h"
  100. #include "srcinfo.h"
  101. #include "srcerrs.h"
  102. #include "errdbg.h"
  103. #include "ihxcookies.h"
  104. #include "pacutil.h"
  105. #if defined(HELIX_FEATURE_NETCHECK)
  106. #include "netchck.h"
  107. #endif /* defined(HELIX_FEATURE_NETCHECK) */
  108. #include "hxrasyn.h"
  109. #include "hxaudstr.h"
  110. #include "hxaudses.h"
  111. #include "verutil.h"
  112. #include "hxrendr.h"
  113. #include "hxcleng.h"
  114. #include "wmbufctl.h"
  115. #include "fbbufctl.h"
  116. #include "clntcore.ver"
  117. #include "hxver.h"
  118. #include "hxwinver.h"
  119. #include "hxheap.h"
  120. #ifdef _DEBUG
  121. #undef HX_THIS_FILE
  122. static const char HX_THIS_FILE[] = __FILE__;
  123. #endif
  124. // maximum level of statistics that we wanna send back to the server
  125. #define MAX_STATS_MASK                  15      // value from server is a bit field
  126. #define MAX_LOGINFO_LENGTH              2048    // maximum of 2K bytes of statistics
  127. /* RTSP Setup may take a lot ot time */
  128. #define NEW_DEF_SETTINGS_TIMEOUT    3000
  129. #define MINIMUM_TIMEOUT             5
  130. const short int MAX_CLOAK_PORTS = 4;
  131. const UINT16 g_uCloakPorts[MAX_CLOAK_PORTS] = {80, 8080, 554, 7070};
  132. #define CLOAKPORT_URL_OPTION    "cloakport"
  133. HXNetSource::HXNetSource() :
  134.       m_bLossCorrection (FALSE)
  135.     , m_pHost (0)
  136.     , m_pPath (NULL)
  137.     , m_pResource (0)
  138.     , m_uPort (0)
  139.     , m_bRTSPProtocol (FALSE)
  140.     , m_pProxy (0)
  141.     , m_uProxyPort (0)
  142.     , m_bUseProxy (FALSE)
  143.     , m_pCloakPortList(NULL)
  144.     , m_nNumberOfCloakPorts(0)
  145.     , m_nCurrPortIdx (0)
  146.     , m_uCurrCloakedPort(0)
  147.     , m_bUseUDPPort (FALSE)
  148.     , m_pProto (0)
  149.     , m_ulStartBuffering (0)
  150.     , m_bSendStatistics (TRUE)
  151.     , m_ulServerTimeOut (90)
  152.     , m_ulConnectionTimeout(DEFAULT_CONN_TIMEOUT)
  153.     , m_bAtInterrupt (FALSE)
  154.     , m_fReBufferPercent ((float)0.)
  155.     , m_pProtocolStatus (0)
  156.     , m_ulMulticastTimeout (NEW_DEF_SETTINGS_TIMEOUT)
  157.     , m_ulUDPTimeout (NEW_DEF_SETTINGS_TIMEOUT)
  158.     , m_ulTCPTimeout(NEW_DEF_SETTINGS_TIMEOUT)
  159.     , m_ulStartDataWait (0)
  160.     , m_ulServerVersion(0)
  161.     , m_ulTransportPrefMask(0)
  162.     , m_bDataWaitStarted(FALSE)
  163.     , m_bConnectionWait(TRUE)
  164.     , m_ulFirstDataArrives (0)
  165.     , m_uProtocolType(unknownProtocol)
  166.     , m_bPerfectPlayPreferenceRead(FALSE)
  167.     , m_bPerfectPlayErrorChecked(FALSE)
  168.     , m_CurrentTransport(UnknownMode)
  169.     , m_PreferredTransport(UnknownMode)
  170.     , m_bServerHasPerfectPlay(FALSE)
  171.     , m_ulSendStatsMask(0)
  172.     , m_ulStatsInterval(0)
  173.     , m_bServerHasTransportSwitching (FALSE)
  174.     , m_bForcePerfectPlay (FALSE)
  175.     , mServerSelRecordSupport(FALSE)
  176.     , mInterframeControlSupport(FALSE)
  177.     , mServerHasBandwidthReport(FALSE)
  178.     , mServerHasFrameControl(FALSE)
  179.     , m_bInRetryMode(FALSE)
  180.     , m_bPushDownSet(FALSE)
  181.     , m_pushDownStatus(PUSHDOWN_NONE)
  182.     , m_bRARVSource(FALSE)
  183.     , m_bRARVSourceVerified(FALSE)
  184.     , m_bBruteForceReconnected(FALSE)
  185.     , m_bBruteForceConnectToBeDone(FALSE)
  186.     , m_lRAStreamNumber(-1)
  187.     , m_pLogInfoList(NULL)
  188.     , m_ulLogInfoLength(0)
  189.     , m_bResendAuthenticationInfo(FALSE)
  190.     , m_bUserHasCalledResume(FALSE)
  191.     , m_bUserHasCalledStartInit(FALSE)
  192.     , m_bTimeBased(FALSE)
  193.     , m_uLastBuffering(0)
  194.     , m_uLastStatusCode(HX_STATUS_BUFFERING)
  195.     , m_bAltURL(FALSE)
  196.     , m_bNoLatency(FALSE)
  197.     , m_bNoLatencySet(FALSE)
  198.     , m_pCookies(NULL)
  199.     , m_pCookies2(NULL)
  200.     , m_ulSeekPendingTime(0)
  201.     , m_bSeekPending(FALSE)
  202.     , m_prefTransportState(PTS_UNKNOWN)
  203.     , m_pPreferredTransport(NULL)
  204.     , m_pPreferredTransportManager(NULL)
  205.     , m_pReconnectCallback(NULL)
  206. #if defined(HELIX_FEATURE_STATS) && defined(HELIX_FEATURE_REGISTRY)
  207.     , m_pStatsCallback(NULL)
  208. #endif /* HELIX_FEATURE_STATS && HELIX_FEATURE_REGISTRY */
  209.     , m_bAttemptReconnect(TRUE)
  210.     , m_pszReconnectServer(NULL)
  211.     , m_pszReconnectProxy(NULL)
  212.     , m_pszReconnectURL(NULL)
  213.     , m_ulReconnectServerPort(0)
  214.     , m_ulReconnectProxyPort(0)
  215.     , m_pszRedirectServer(NULL)
  216.     , m_pszRedirectResource(NULL)
  217.     , m_pszRedirectURL(NULL)
  218.     , m_ulRedirectServerPort(0)
  219.     , m_pPAC(NULL)
  220.     , m_pPACInfoList(NULL)
  221.     , m_PACInfoPosition(0)
  222.     , m_state(NETSRC_READY)
  223.     , m_pBufferCtl(NULL)
  224.     , m_pWMBufferCtl(NULL)
  225. {
  226.     HX_TRACE("HXNetSource::HXNetSource");
  227.     memset(m_szUserName, 0, MAX_DISPLAY_NAME);
  228.     memset(m_szPassword, 0, MAX_DISPLAY_NAME);
  229. }
  230. HXNetSource::~HXNetSource()
  231. {
  232.     DoCleanup();
  233.     HX_DELETE(m_pPACInfoList);
  234.     HX_VECTOR_DELETE(m_pHost);
  235.     HX_VECTOR_DELETE(m_pPath);
  236.     HX_VECTOR_DELETE(m_pResource);
  237.     HX_VECTOR_DELETE(m_pCloakPortList);
  238.     HX_VECTOR_DELETE(m_pszReconnectServer);
  239.     HX_VECTOR_DELETE(m_pszReconnectProxy);
  240.     HX_VECTOR_DELETE(m_pszReconnectURL);
  241.     HX_VECTOR_DELETE(m_pszRedirectServer);
  242.     HX_VECTOR_DELETE(m_pszRedirectResource);
  243.     HX_VECTOR_DELETE(m_pszRedirectURL);
  244.     HX_RELEASE(m_pProtocolStatus);
  245.     HX_RELEASE(m_pCookies);
  246.     HX_RELEASE(m_pCookies2);
  247.     HX_RELEASE(m_pPreferredTransport);
  248.     HX_RELEASE(m_pPreferredTransportManager);
  249.     HX_RELEASE(m_pPAC);
  250.     HX_TRACE("HXNetSource::~HXNetSource");
  251. }
  252. HX_RESULT
  253. HXNetSource::DoCleanup(EndCode endCode)
  254. {
  255.     HX_TRACE("HXNetSource::DoCleanup");
  256.     if (m_pBufferCtl)
  257.     {
  258. m_pBufferCtl->Close();
  259.     }
  260.     HX_RELEASE(m_pBufferCtl);
  261.     HX_RELEASE(m_pWMBufferCtl);
  262.     m_srcEndCode = endCode;
  263. #if defined(HELIX_FEATURE_PAC)
  264.     if (NETSRC_PACPENDING == m_state)
  265.     {
  266.         m_state = NETSRC_READY;
  267.         m_pPAC->AbortProxyInfo(this);
  268.     }
  269. #endif /* HELIX_FEATURE_PAC */
  270. #if defined(HELIX_FEATURE_SMARTERNETWORK)
  271.     // notify the failure of transport detection
  272.     if (m_pPreferredTransport)
  273.     {
  274.         m_pPreferredTransport->RemoveTransportSink(this);
  275.         if (m_pPlayer && m_pHost && m_prefTransportState != PTS_READY)
  276.         {
  277.             m_pPreferredTransport->RemoveTransport();
  278.             HX_RELEASE(m_pPreferredTransport);
  279.         }
  280.     }
  281. #endif /* HELIX_FEATURE_SMARTERNETWORK */
  282.     // sends the stats
  283.     if (m_pProto)
  284.     {
  285.         // log stop action
  286.         LogInformation("STOP", NULL);
  287.         if (m_bSendStatistics)
  288.         {
  289.             m_pProto->send_statistics(m_ulSendStatsMask);
  290.         }
  291.     }
  292.     HXSource::DoCleanup(endCode);
  293.     cleanup_proxy();
  294.     /* UnRegister any previously registered source */
  295.     if (m_pSourceInfo)
  296.     {
  297.         m_pSourceInfo->UnRegister();
  298.     }
  299.     //  Close down the net connection
  300.     if (m_pProto)
  301.     {
  302.         m_pProto->stop();
  303.         HX_RELEASE (m_pProto);
  304.     }
  305.     // cleanup the log list
  306.     if (m_pLogInfoList)
  307.     {
  308.         while (m_pLogInfoList->GetCount() > 0)
  309.         {
  310.             char* pszInfo = (char*) m_pLogInfoList->RemoveHead();
  311.             delete [] pszInfo;
  312.         }
  313.         HX_DELETE(m_pLogInfoList);
  314.     }
  315.     if (m_pReconnectCallback)
  316.     {
  317.         m_pReconnectCallback->CancelCallback();
  318.         HX_RELEASE(m_pReconnectCallback);
  319.     }
  320. #if defined(HELIX_FEATURE_STATS) && defined(HELIX_FEATURE_REGISTRY)
  321.     if (m_pStatsCallback)
  322.     {
  323.         m_pStatsCallback->CancelCallback();
  324.         HX_RELEASE(m_pStatsCallback);
  325.     }
  326. #endif /* HELIX_FEATURE_STATS && HELIX_FEATURE_REGISTRY */
  327. #if defined(HELIX_FEATURE_PAC)
  328.     while (m_pPACInfoList && m_pPACInfoList->GetCount())
  329.     {
  330.         PACInfo* pPACInfo = (PACInfo*)m_pPACInfoList->RemoveHead();
  331.         HX_DELETE(pPACInfo);
  332.     }
  333. #endif /* HELIX_FEATURE_PAC */
  334.     return HXR_OK;
  335. }
  336. /////////////////////////////////////////////////////////////////////////
  337. //      Method:
  338. //              IUnknown::QueryInterface
  339. //      Purpose:
  340. //              Implement this to export the interfaces supported by your
  341. //              object.
  342. //
  343. STDMETHODIMP
  344. HXNetSource::QueryInterface(REFIID riid, void** ppvObj)
  345. {
  346.     if (HXSource::QueryInterface(riid, ppvObj) == HXR_OK)
  347.     {
  348.         return HXR_OK;
  349.     }
  350.     else if (IsEqualIID(riid, IID_IHXRegistryID))
  351.     {
  352.         AddRef();
  353.         *ppvObj = (IHXRegistryID*)this;
  354.         return HXR_OK;
  355.     }
  356.     else if (IsEqualIID(riid, IID_IHXPreferredTransportSink))
  357.     {
  358.         AddRef();
  359.         *ppvObj = (IHXPreferredTransportSink*)this;
  360.         return HXR_OK;
  361.     }
  362.     else if (m_pBufferCtl &&
  363.      m_pBufferCtl->QueryInterface(riid, ppvObj) == HXR_OK)
  364.     {
  365. return HXR_OK;
  366.     }
  367.     else if (m_pProto &&
  368.              m_pProto->QueryInterface(riid, ppvObj) == HXR_OK)
  369.     {
  370.         return HXR_OK;
  371.     }
  372.     else if (m_pPlayer &&
  373.              m_pPlayer->QueryInterface(riid, ppvObj) == HXR_OK)
  374.     {
  375.         return HXR_OK;
  376.     }
  377.     // we don't have m_pPlayer during AutoConfig
  378.     else if (m_pEngine &&
  379.              m_pEngine->QueryInterface(riid, ppvObj) == HXR_OK)
  380.     {
  381.         return HXR_OK;
  382.     }
  383.     *ppvObj = NULL;
  384.     return HXR_NOINTERFACE;
  385. }
  386. /////////////////////////////////////////////////////////////////////////
  387. //      Method:
  388. //              IUnknown::AddRef
  389. //      Purpose:
  390. //              Everyone usually implements this the same... feel free to use
  391. //              this implementation.
  392. //
  393. STDMETHODIMP_(ULONG32)
  394. HXNetSource::AddRef()
  395. {
  396.     return InterlockedIncrement(&m_lRefCount);
  397. }
  398. /////////////////////////////////////////////////////////////////////////
  399. //      Method:
  400. //              IUnknown::Release
  401. //      Purpose:
  402. //              Everyone usually implements this the same... feel free to use
  403. //              this implementation.
  404. //
  405. STDMETHODIMP_(ULONG32)
  406. HXNetSource::Release()
  407. {
  408.     if (InterlockedDecrement(&m_lRefCount) > 0)
  409.     {
  410.         return m_lRefCount;
  411.     }
  412.     delete this;
  413.     return 0;
  414. }
  415. HX_RESULT
  416. HXNetSource::Setup(const char*  host,
  417.                     const char* resource,
  418.                     UINT16      port,
  419.                     BOOL        LossCorrection,
  420.                     const CHXURL* pURL,
  421.                     BOOL        bAltURL)
  422. {
  423.     HX_TRACE("HXNetSource::Setup");
  424.     HX_RESULT       theErr = HXR_OK;
  425.     IHXValues*      pValues = NULL;
  426.     IHXValues*      pRequestHeaders = NULL;
  427.     IHXBuffer*      pBuffer = NULL;
  428.     IHXBuffer*      pPlayerCookies = NULL;
  429.     if (!m_bBruteForceReconnected &&
  430.         !m_bReSetup)
  431.     {
  432.         theErr = SetupRegistry();
  433.         if (HXR_OK != theErr)
  434.         {
  435.             goto cleanup;
  436.         }
  437.     }
  438.     if (!m_bReSetup)
  439.     {
  440.         m_ulOriginalDelay = m_ulDelay;
  441.     }
  442.     // Remember whether or not we ask for loss correction!
  443.     m_bLossCorrection = LossCorrection;
  444.     // delete protocol object if one exists
  445.     HX_RELEASE (m_pProto);
  446.     m_uProtocolType = pURL->GetProtocol();
  447.     if (rtspProtocol == m_uProtocolType || helixSDPProtocol == m_uProtocolType)
  448.     {
  449.         m_bRTSPProtocol = TRUE;
  450.         m_ulSendStatsMask = MAX_STATS_MASK;
  451.         /* Perfect Play Always supported iin RTSP,
  452.          * till we add some preference to disable it.
  453.          */
  454.         BOOL bSupported = TRUE;
  455.         SetOption(HX_PERFECTPLAY_SUPPORTED, &bSupported);
  456.     }
  457.     if (helixSDPProtocol != m_uProtocolType)
  458.     {
  459.         //  get out immediately if we have bogus parameters
  460.         if(!host || !*host)
  461.         {
  462.             theErr = HXR_INVALID_URL_HOST;
  463.             goto cleanup;
  464.         }
  465.         if(!theErr && (!resource || !*resource))
  466.         {
  467.             theErr = HXR_INVALID_URL_PATH;
  468.             goto cleanup;
  469.         }
  470.         if  (m_pHost != host)
  471.         {
  472.             HX_VECTOR_DELETE(m_pHost);
  473.             m_pHost = new char[::strlen(host) + 1];
  474.             if (!m_pHost)
  475.             {
  476.                 theErr = HXR_OUTOFMEMORY;
  477.                 goto cleanup;
  478.             }
  479.             ::strcpy(m_pHost, host); /* Flawfinder: ignore */
  480.         }
  481.         if(m_pResource != resource)
  482.         {
  483.             HX_VECTOR_DELETE(m_pResource);
  484.             m_pResource = new char[::strlen(resource) + 1];
  485.             if (!m_pResource)
  486.             {
  487.                 theErr = HXR_OUTOFMEMORY;
  488.                 goto cleanup;
  489.             }
  490.             ::strcpy(m_pResource, resource); /* Flawfinder: ignore */
  491.         }
  492.     }
  493.     if (m_pURL != pURL)
  494.     {
  495.         HX_DELETE(m_pURL);
  496.         m_pURL = new CHXURL(*pURL);
  497.         if (!m_pURL)
  498.         {
  499.             theErr = HXR_OUTOFMEMORY;
  500.             goto cleanup;
  501.         }
  502.         HX_VECTOR_DELETE(m_pszURL);
  503.         m_pszURL = new char[::strlen(m_pURL->GetURL()) + 1];
  504.         if (!m_pszURL)
  505.         {
  506.             theErr = HXR_OUTOFMEMORY;
  507.             goto cleanup;
  508.         }
  509.         ::strcpy(m_pszURL, m_pURL->GetURL()); /* Flawfinder: ignore */
  510.     }
  511.     m_uPort = port;
  512.     HX_VECTOR_DELETE(m_pPath);
  513.     pValues = m_pURL->GetProperties();
  514.     if (pValues &&
  515.         HXR_OK == pValues->GetPropertyBuffer(PROPERTY_PATH, pBuffer) && pBuffer)
  516.     {
  517.         StrAllocCopy(m_pPath, (char*)pBuffer->GetBuffer());
  518.     }
  519.     HX_RELEASE(pBuffer);
  520.     HX_RELEASE(pValues);
  521.     // save parameters for use with different protocols.
  522. #if defined(HELIX_FEATURE_STATS) && defined(HELIX_FEATURE_REGISTRY)
  523.     // save URL to the registry
  524.     if (m_pStats)
  525.     {
  526.         m_pStats->m_pSourceName->SetStr((char*)pURL);
  527.     }
  528. #endif /* HELIX_FEATURE_STATS && HELIX_FEATURE_REGISTRY */
  529.     // set request
  530.     m_bAltURL = bAltURL;
  531.    SetRequest(m_pURL, bAltURL);
  532.     if (!m_pCookies && HXR_OK != m_pEngine->QueryInterface(IID_IHXCookies, (void**)&m_pCookies))
  533.     {
  534.         m_pCookies = NULL;
  535.     }
  536.     if (!m_pCookies2 && HXR_OK != m_pEngine->QueryInterface(IID_IHXCookies2, (void**)&m_pCookies2))
  537.     {
  538.         m_pCookies2 = NULL;
  539.     }
  540.     if (m_pCookies || m_pCookies2)
  541.     {
  542.         if (HXR_OK == m_pRequest->GetRequestHeaders(pRequestHeaders) && pRequestHeaders)
  543.         {
  544.             HX_RESULT res = HXR_FAIL;
  545.             if(m_pCookies2)
  546.                 res = m_pCookies2->GetCookies(m_pHost, m_pPath, pBuffer, pPlayerCookies);
  547.             else if(m_pCookies)
  548.                 res = m_pCookies->GetCookies(m_pHost, m_pPath, pBuffer);
  549.             if (HXR_OK == res && pBuffer)
  550.             {
  551.                 pRequestHeaders->SetPropertyCString("Cookie", pBuffer);
  552.                 if(pPlayerCookies)
  553.                 {
  554.                     pRequestHeaders->SetPropertyCString("PlayerCookie", pPlayerCookies);
  555.                 }
  556.             }
  557.             HX_RELEASE(pBuffer);
  558.             HX_RELEASE(pPlayerCookies);
  559.         }
  560.     }
  561.     HX_RELEASE(pRequestHeaders);
  562. #if defined(HELIX_FEATURE_SMARTERNETWORK)
  563.     if (!m_pPreferredTransportManager &&
  564.         HXR_OK != m_pEngine->QueryInterface(IID_IHXPreferredTransportManager,
  565.                                             (void**)&m_pPreferredTransportManager))
  566.     {
  567.         theErr = HXR_FAILED;
  568.         goto cleanup;
  569.     }
  570. #endif /* HELIX_FEATURE_SMARTERNETWORK */
  571.     // Read all relevant preferences...
  572.     theErr = ReadPreferences();
  573.     // either we will wait for the first source to negotiate the transport
  574.     //        then connect the remaining sources
  575.     // or we are initiating PAC(ProxyAutoConfig)
  576.     if (HXR_WOULD_BLOCK == theErr)
  577.     {
  578.         theErr = HXR_OK;
  579.     }
  580.     else if (HXR_OK == theErr)
  581.     {
  582.         theErr = FinishSetup();
  583.     }
  584. cleanup:
  585.     return theErr;
  586. }
  587. HX_RESULT
  588. HXNetSource::FinishSetup()
  589. {
  590.     HX_RESULT   rc = HXR_OK;
  591.     BOOL        bSDPInitiated = FALSE;
  592.     CHXString   pString;
  593.     // start off with the preferred protocol
  594.     rc = CreateProtocol();
  595.     if (HXR_OK != rc)
  596.     {
  597.         goto cleanup;
  598.     }
  599.     rc = InitializeProtocol();
  600.     if (HXR_OK != rc)
  601.     {
  602.         goto cleanup;
  603.     }
  604.     if (helixSDPProtocol == m_uProtocolType)
  605.     {
  606.         bSDPInitiated = TRUE;
  607.         CHXURL::decodeURL(m_pURL->GetEscapedURL(), pString);
  608.             
  609.         HX_VECTOR_DELETE(m_pResource);
  610.         m_pResource = new char[pString.GetLength() - 9];
  611.         ::strcpy(m_pResource, (const char*)pString + 10);
  612.     }
  613.                 
  614.     rc = m_pProto->setup(m_pHost,
  615.                          m_pResource,
  616.                          m_uPort,
  617.                          m_bLossCorrection,
  618.                          (HTTPCloakMode == m_CurrentTransport)?TRUE:FALSE,
  619.                          bSDPInitiated,
  620.                          m_uCurrCloakedPort);
  621.     // Mask this error
  622.     // Let the _ProcessIdle() determine whether ReportError() or
  623.     // TransportSwitching for HXR_BAD_TRANSPORT
  624.     if(rc == HXR_BLOCK_CANCELED || rc == HXR_BAD_TRANSPORT)
  625.     {
  626.         rc = HXR_OK;
  627.     }
  628.     if (HXR_OK != rc)
  629.     {
  630.         mLastError = rc;
  631.         goto cleanup;
  632.     }
  633.     // create log info list
  634.     m_pLogInfoList = new CHXSimpleList;
  635.     m_ulLogInfoLength = 0;
  636.     // start time of this source
  637.     m_ulSourceStartTime = HX_GET_TICKCOUNT();
  638.     if (m_pBufferCtl)
  639.     {
  640. m_pBufferCtl->Close();
  641.     }
  642.     HX_RELEASE(m_pBufferCtl);
  643.     HX_RELEASE(m_pWMBufferCtl);
  644.     
  645. #ifdef HELIX_FEATURE_FEEDBACK_BUFFER_CONTROL
  646.     m_pBufferCtl = new HXFeedbackBufferControl();
  647. #else
  648.     m_pBufferCtl = new HXWatermarkBufferControl();
  649. #endif /* HELIX_FEATURE_FEEDBACK_BUFFER_CONTROL */
  650.     if (m_pBufferCtl)
  651.     {
  652. m_pBufferCtl->AddRef();
  653. m_pBufferCtl->QueryInterface(IID_IHXWatermarkBufferControl,
  654.      (void**)&m_pWMBufferCtl);
  655. m_pBufferCtl->Init((IUnknown*)(IHXStreamSource*)this);
  656. if (m_pWMBufferCtl)
  657. {
  658.     m_pWMBufferCtl->SetSource(this);
  659. }
  660.     }
  661. cleanup:
  662.     if (HXR_OK != rc)
  663.     {
  664.         HX_RELEASE(m_pProto);
  665.     }
  666.     return rc;
  667. }
  668. HX_RESULT
  669. HXNetSource::DoSeek(ULONG32 seekTime)
  670. {
  671.     HX_TRACE("HXNetSource::DoSeek");
  672.     BOOL bSeekInsideRecordControl = FALSE;
  673.     if ((!m_pProto && m_state != NETSRC_RECONNECTPENDING) || 
  674.         (mLiveStream && !m_bPlayFromRecordControl && m_bSourceEnd))
  675.     {
  676.         return HXR_OK;
  677.     }
  678.     // log seek action
  679.     LogInformation("SEEK", NULL);
  680.     if (seekTime >= m_ulDelay)
  681.     {
  682.         seekTime -= m_ulDelay;
  683.         m_bDelayed = FALSE;
  684.     }
  685.     else
  686.     {
  687.         seekTime = 0;
  688.         /* This source has not been even started yet...
  689.          * Do not attempt to seek it if the start time = 0
  690.          */
  691.         if (m_bDelayed && m_ulStartTime == 0 && !m_bSourceEnd)
  692.         {
  693.             // will start pre-fetch again in TryResume()
  694.             if (!m_bIsPreBufferingDone)
  695.             {
  696.                 m_bIsPreBufferingStarted = FALSE;
  697.                 // will be registered again in DoResume() or TryResume()
  698.                 if (m_pSourceInfo)
  699.                 {
  700.                     m_pSourceInfo->UnRegister();
  701.                 }
  702.             }
  703.             return HXR_OK;
  704.         }
  705.         m_bDelayed = TRUE;
  706.     }
  707.     /* Add any start time to seek time */
  708.     seekTime    += m_ulStartTime;
  709.     /* Are we seeking past the last expected packet time?
  710.      * If so, don't bother... and mark this source as done
  711.      */
  712.     HX_ASSERT(m_llLastExpectedPacketTime <= MAX_UINT32);
  713.     // XXX HP make sure the source has been initialized otherwise
  714.     // m_llLastExpectedPacketTime could be 0 and we could falsely
  715.     // mark the source ended
  716.     if (m_bInitialized && !mLiveStream && seekTime >= m_llLastExpectedPacketTime)
  717.     {
  718.         if (m_pSourceInfo && m_pSourceInfo->m_bSeekToLastFrame)
  719.         {
  720.             seekTime = INT64_TO_ULONG32(m_llLastExpectedPacketTime);
  721.         }
  722.         else
  723.         {
  724.             m_bSourceEnd = TRUE;
  725.             m_bForcedSourceEnd = TRUE;
  726.             AdjustClipBandwidthStats(FALSE);
  727. #if defined(HELIX_FEATURE_RECORDCONTROL)
  728.             if (m_pRecordControl)
  729.             {
  730.                 m_pRecordControl->Seek(seekTime);
  731.             }
  732. #endif /* HELIX_FEATURE_RECORDCONTROL*/
  733.             goto cleanup;
  734.         }
  735.     }
  736.     // workaround for b#42118 for older servers(<= RealServer8.0 Gold)
  737.     // server sends no more streamdone when reaching the endtime if seek
  738.     // is occurred after streamdone has been sent. fix has been made in the latest server
  739.     if (m_bSourceEnd && m_bCustomEndTime && m_pProto && m_pProto->GetRDTFeatureLevel() < 2)
  740.     {
  741.         m_bRTSPRuleFlagWorkAround = TRUE;
  742.     }
  743.     if (mLiveStream)
  744.     {
  745.         seekTime += m_ulFirstPacketTime;
  746.     }
  747. #if defined(HELIX_FEATURE_RECORDCONTROL)
  748.     if (m_pRecordControl && m_pRecordControl->Seek(seekTime) == HXR_OK &&
  749.         m_bPlayFromRecordControl)
  750.     {
  751.         bSeekInsideRecordControl = TRUE;
  752.     }
  753.     else
  754. #endif /* HELIX_FEATURE_RECORDCONTROL */
  755.     {
  756.         m_ulSeekPendingTime = seekTime;
  757.         m_bSeekPending = TRUE;
  758.         if(m_pProto)
  759.             m_pProto->seek(seekTime, seekTime, FALSE);
  760.         if (mLiveStream)
  761.         {
  762.             seekTime = 0;
  763.         }
  764.         m_bSourceEnd        = FALSE;
  765.         m_bForcedSourceEnd  = FALSE;
  766.         STREAM_INFO*    pStreamInfo = NULL;
  767.         CHXMapLongToObj::Iterator lStreamIterator = mStreamInfoTable->Begin();
  768.         for (; lStreamIterator != mStreamInfoTable->End(); ++lStreamIterator)
  769.         {
  770.             pStreamInfo = (STREAM_INFO*) (*lStreamIterator);
  771.             pStreamInfo->ResetPreReconnectEventList();
  772.         }
  773.     }
  774.     // initiating reconnect NOW ...
  775.     if (NETSRC_RECONNECTPENDING == m_state)
  776.     {
  777.         m_state = NETSRC_RECONNECTFORCED;
  778.         if (m_pReconnectCallback)
  779.         {
  780.             m_pReconnectCallback->CancelCallback();
  781.         }
  782.         StartReconnect();
  783.     }
  784.     m_uLastBuffering                = 0;
  785.     m_bInitialBuffering             = TRUE;
  786.     m_bIsPreBufferingStarted        = FALSE;
  787.     m_bIsPreBufferingDone           = FALSE;
  788.     m_pushDownStatus                = PUSHDOWN_NONE;
  789.     if (m_pBufferCtl)
  790.     {
  791. m_pBufferCtl->OnSeek();
  792.     }
  793.     // Buffer manager needs to know whether we seek inside the RecordControl
  794.     // to not reset variables related to stream completeness.
  795.     m_pBufferManager->DoSeek(seekTime, bSeekInsideRecordControl);
  796. cleanup:
  797.     return HXR_OK;
  798. }
  799. HX_RESULT
  800. HXNetSource::DoPause(void)
  801. {
  802.     HX_TRACE("HXNetSource::DoPause");
  803.     if (!m_pProto || m_bPaused)
  804.     {
  805.         return HXR_OK;
  806.     }
  807.     // log pause action
  808.     LogInformation("PAUSE", NULL);
  809.     m_pBufferManager->DoPause();
  810.     if (m_pBufferCtl)
  811.     {
  812. m_pBufferCtl->OnPause();
  813.     }
  814.     m_bPaused = TRUE;
  815.     if (m_bPlayFromRecordControl && !m_bDelayed)
  816.     {
  817.         return HXR_OK;
  818.     }
  819.     /* Do not call pause if the source has ended. In RTSP, if the transport
  820.      * is paused, it does not give us any packets. We would never issue a
  821.      * resume since the source has ended and this would result in starting
  822.      * the playback without giving any packets to the renderer if the preroll
  823.      * of the renderer happens to be zero.(like RealText).
  824.      */
  825.     /* Call Pause even after source is done. This is to keep track
  826.      * of Accounting information on the server side for PPV content
  827.      * The only time we do not issue a pause is if we have internally
  828.      * stopped the stream because of a end/dur tag.
  829.      *
  830.      * RTSP transport has been fixed so that it returns packets even
  831.      * in a paused state. So the bug reported above should not occur.
  832.      * Moreover, we now call protocol Resume even if the source is done.
  833.      * So the transport should never be really in a Paused state when
  834.      * we need to read packets.
  835.      */
  836.     if (!m_bForcedSourceEnd)
  837.     {
  838.         m_pProto->pause();
  839. #if defined(HELIX_FEATURE_STATS) && defined(HELIX_FEATURE_REGISTRY)
  840.         if (m_pStatsCallback)
  841.         {
  842.             m_pStatsCallback->PauseCallback();
  843.         }
  844. #endif /* HELIX_FEATURE_STATS && HELIX_FEATURE_REGISTRY */
  845.     }
  846.     return HXR_OK;
  847. }
  848. HX_RESULT
  849. HXNetSource::StartInitialization(void)
  850. {
  851.     if (m_pProto)
  852.     {
  853.         m_bUserHasCalledStartInit = TRUE;
  854.         m_pBufferManager->DoResume();
  855.         if (m_pSourceInfo)
  856.         {
  857.             m_pSourceInfo->Resumed();
  858.             /* This is temporary. The source MUST be registered by this point.
  859.              * Fix it after B1.
  860.              * For now, added a work around
  861.              * - Rahul
  862.              */
  863.             if (!m_pSourceInfo->IsRegistered() && m_pSourceInfo->IsActive())
  864.             {
  865.                 m_pSourceInfo->Register();
  866.                 if (m_pPlayer)
  867.                 {
  868.                     m_pPlayer->RegisterSourcesDone();
  869.                 }
  870.             }
  871.         }
  872.         SetNoLatency();
  873.         HX_ASSERT(m_llLastExpectedPacketTime <= MAX_UINT32);
  874. #if defined(HELIX_FEATURE_STATS) && defined(HELIX_FEATURE_REGISTRY)
  875.         if (m_bSendStatistics && m_ulStatsInterval > 0)
  876.         {
  877.             if (!m_pStatsCallback)
  878.             {
  879.                 m_pStatsCallback = new ReconnectCallback(this, TRUE);
  880.                 m_pStatsCallback->AddRef();
  881.             }
  882.             if (m_pStatsCallback->IsPaused())
  883.             {
  884.                 m_pStatsCallback->ResumeCallback();
  885.             }
  886.             else
  887.             {
  888.                 m_pStatsCallback->ScheduleCallback(m_ulStatsInterval);
  889.             }
  890.         }
  891. #endif /* HELIX_FEATURE_STATS && HELIX_FEATURE_REGISTRY */
  892.         return m_pProto->resume(INT64_TO_UINT32(m_llLastExpectedPacketTime));
  893.     }
  894.     return HXR_OK;
  895. }
  896. HX_RESULT
  897. HXNetSource::DoResume(void)
  898. {
  899.     HX_TRACE("HXNetSource::DoResume");
  900.     HX_RESULT theErr = HXR_OK;
  901.     /* This may happen if a new source is added from SMIL renderer during
  902.      * initialization of exisitng sources. We will eventually call Resume
  903.      * on this source once it is initialized in SourceInfo::ProcessIdle
  904.      */
  905.     if (!m_bInitialized)
  906.     {
  907.         return HXR_OK;
  908.     }
  909.     m_bUserHasCalledResume = TRUE;
  910.     // log resume action
  911.     LogInformation("Resume", NULL);
  912.     if(m_pBufferManager && !m_bSourceEnd && CanBeResumed())
  913.     {
  914.         m_pBufferManager->DoResume();
  915.     }
  916.     /* Call Resume even after source is done. This is to keep track
  917.      * of Accounting information on the server side for PPV content
  918.      * Only if we have forced a source end because of end/dur tag
  919.      * or because of seeking past source's duration in SMIL,
  920.      * we do not send Pause/Resume to the server
  921.      */
  922.     if (!m_pProto || (!m_bPaused && !m_bFirstResume))
  923.     {
  924.         return HXR_OK;
  925.     }
  926.     if(m_bPlayFromRecordControl && !m_bDelayed && !m_bFirstResume && !m_bResumePending)
  927.     {
  928.         m_bPaused = FALSE;
  929.         return HXR_OK;
  930.     }
  931.     if (m_bSourceEnd || CanBeResumed())
  932.     {
  933.         m_bResumePending    = FALSE;
  934.         HX_ASSERT(m_pSourceInfo);
  935.         if (!m_bSourceEnd)
  936.         {
  937.             /* This is temporary. This assert is currently tripping...
  938.              * Ideally, it should not. Fix it after B1.
  939.              * For now, added a work around
  940.              * - Rahul
  941.              */
  942.             //  HX_ASSERT(!m_pSourceInfo || m_pSourceInfo->IsRegistered() || !m_pSourceInfo->IsActive());
  943.             if (m_pSourceInfo && !m_pSourceInfo->IsRegistered() && m_pSourceInfo->IsActive())
  944.             {
  945.                 m_pSourceInfo->Register();
  946.                 if (m_pPlayer)
  947.                 {
  948.                     m_pPlayer->RegisterSourcesDone();
  949.                 }
  950.             }
  951.             SetNoLatency();
  952.             if (m_bBruteForceReconnected && m_bSeekPending)
  953.             {
  954.                 theErr = m_pProto->seek(m_ulSeekPendingTime);
  955.             }
  956.             HX_ASSERT(INT64_TO_ULONG32(m_llLastExpectedPacketTime) <= MAX_UINT32);
  957.             theErr              = m_pProto->resume(INT64_TO_UINT32(m_llLastExpectedPacketTime));
  958. #if defined(HELIX_FEATURE_STATS) && defined(HELIX_FEATURE_REGISTRY)
  959.             if (m_bSendStatistics && m_ulStatsInterval > 0)
  960.             {
  961.                 if (!m_pStatsCallback)
  962.                 {
  963.                     m_pStatsCallback = new ReconnectCallback(this, TRUE);
  964.                     m_pStatsCallback->AddRef();
  965.                 }
  966.                 if (m_pStatsCallback->IsPaused())
  967.                 {
  968.                     m_pStatsCallback->ResumeCallback();
  969.                 }
  970.                 else
  971.                 {
  972.                     m_pStatsCallback->ScheduleCallback(m_ulStatsInterval);
  973.                 }
  974.             }
  975. #endif /* HELIX_FEATURE_STATS && HELIX_FEATURE_REGISTRY */
  976.         }
  977.         // resume the audio streams if the source is added
  978.         // while the player is in play
  979.         // CAUTION: this will cause rewind in audio service
  980.         if (m_bFirstResume                          &&
  981.             IsPlaying()                             &&
  982.             (m_state == NETSRC_READY)               &&
  983.             m_pPlayer && m_ulDelay <= m_pPlayer->GetInternalCurrentPlayTime())
  984.         {
  985.             ResumeAudioStreams();
  986.         }
  987.         m_bFirstResume      = FALSE;
  988.         m_bPaused           = FALSE;
  989.         if (m_pSourceInfo)
  990.         {
  991.             m_pSourceInfo->Resumed();
  992.         }
  993. if (m_pBufferCtl)
  994. {
  995.     m_pBufferCtl->OnResume();
  996. }
  997.     }
  998.     if (!m_bIsActive && !m_bDelayed && m_pPlayer &&
  999.         m_pPlayer->GetInternalCurrentPlayTime() >= m_ulDelay)
  1000.     {
  1001.         AdjustClipBandwidthStats(TRUE);
  1002.     }
  1003.     return theErr;
  1004. }
  1005. /************************************************************************
  1006.  *      Method:
  1007.  *          IHXPendingStatus::GetStatus
  1008.  *      Purpose:
  1009.  *          Called by the user to get the current pending status from an object
  1010.  */
  1011. STDMETHODIMP
  1012. HXNetSource::GetStatus
  1013. (
  1014.     REF(UINT16) uStatusCode,
  1015.     REF(IHXBuffer*) pStatusDesc,
  1016.     REF(UINT16) ulPercentDone
  1017. )
  1018. {
  1019.     HX_RESULT   rc = HXR_OK;
  1020.     UINT16      buffer = 100;
  1021.     UINT16      statusCode = HX_STATUS_READY;
  1022.     UINT16      percentDone = 0;
  1023.     /* Default values*/
  1024.     uStatusCode     = statusCode;
  1025.     pStatusDesc     = NULL;
  1026.     ulPercentDone   = buffer;
  1027.     UINT16  uSrcPercentDone = 100;
  1028.     UINT16  uSrcStatusCode = HX_STATUS_READY;
  1029.     if (m_bDelayed)
  1030.     {
  1031.         goto cleanup;
  1032.     }
  1033. #if defined(HELIX_FEATURE_TURBOPLAY)
  1034.     if (m_bFastStart                                                &&
  1035.         m_bRARVSource                                               &&
  1036.         (!m_pSourceInfo || !m_pSourceInfo->m_bIsPersistentSource))
  1037.     {
  1038.         if (HXR_OK == IsFaststartPushdownFullfilled(uStatusCode, ulPercentDone))
  1039.         {
  1040.             goto cleanup;
  1041.         }
  1042.     }
  1043. #endif /* HELIX_FEATURE_TURBOPLAY */
  1044.     if (m_bSourceEnd)
  1045.     {
  1046.         if (!IsRebufferDone())
  1047.         {
  1048.             uStatusCode = HX_STATUS_BUFFERING;
  1049.             ulPercentDone = 99;
  1050.         }
  1051.         else
  1052.         {
  1053.             if (m_bInitialBuffering)
  1054.             {
  1055.                 InitialBufferingDone();
  1056.             }
  1057.             m_uLastBuffering = 100;
  1058.             uStatusCode = HX_STATUS_READY;
  1059.         }
  1060.         goto cleanup;
  1061.     }
  1062.     if (m_bInitialized)
  1063.     {
  1064.         m_pBufferManager->GetStatus(uSrcStatusCode, pStatusDesc, uSrcPercentDone);
  1065.         statusCode = uSrcStatusCode;
  1066.         buffer = uSrcPercentDone;
  1067.     }
  1068.     else
  1069.     {
  1070.         buffer              = 0;
  1071.         statusCode          = HX_STATUS_INITIALIZING;
  1072.         percentDone         = 0;
  1073.         uStatusCode         = statusCode;
  1074.         pStatusDesc         = NULL;
  1075.         ulPercentDone       = buffer;
  1076.     }
  1077. #ifdef _RAHULDEBUG
  1078.     {
  1079.         char str[255]; /* Flawfinder: ignore */
  1080.         ::sprintf(str, "GetStatus: BufMgr %u ", buffer); /* Flawfinder: ignore */
  1081.         OutputDebugString(str);
  1082.     }
  1083. #endif
  1084.     if (buffer > 100)
  1085.         buffer = 0;
  1086.     /* If we have not yet received a single packet, do not claim to
  1087.      * be done with buffering. This is needed to correctyl playback sparse
  1088.      * datatypes with no preroll.
  1089.      */
  1090.     if (!m_bReceivedData && buffer == 100)
  1091.     {
  1092.         buffer = 99;
  1093.     }
  1094.     /* We only aggregate buffering from a lower level if we are in a buffering mode.
  1095.      * Reason: Once the initial bufering is done, we go in buffering more ONLY IF the
  1096.      * renderer tells us that it is in a panic state and we run out of packets.
  1097.      */
  1098.     if (buffer == 100 && !m_bInitialBuffering)
  1099.     {
  1100.         // Rebuffer requested by the Renderer might not be
  1101.         // done yet
  1102.         if (!IsRebufferDone())
  1103.         {
  1104.             uStatusCode = HX_STATUS_BUFFERING;
  1105.             ulPercentDone = 99;
  1106.         }
  1107.         else
  1108.         {
  1109.             uStatusCode = HX_STATUS_READY;
  1110.         }
  1111.         goto cleanup;
  1112.     }
  1113.     if (HX_STATUS_CONTACTING == statusCode)
  1114.     {
  1115.         uStatusCode = HX_STATUS_CONTACTING;
  1116.         ulPercentDone = 0;
  1117.     }
  1118.     else if (HX_STATUS_READY == statusCode && 100 == buffer)
  1119.     {
  1120.         uStatusCode = HX_STATUS_READY;
  1121.         ulPercentDone = 0;
  1122.     }
  1123.     else if (HX_STATUS_INITIALIZING == statusCode || HX_STATUS_INITIALIZING == uStatusCode)
  1124.     {
  1125.         uStatusCode = HX_STATUS_BUFFERING;
  1126.         ulPercentDone = 0;
  1127.     }
  1128.     else
  1129.     {
  1130.         uStatusCode = HX_STATUS_BUFFERING;
  1131.         ulPercentDone = buffer;
  1132.     }
  1133.     if (uStatusCode == HX_STATUS_BUFFERING && m_uLastBuffering < 100)
  1134.     {
  1135.         if (ulPercentDone < m_uLastBuffering)
  1136.         {
  1137.             ulPercentDone = m_uLastBuffering;
  1138.         }
  1139.         else
  1140.         {
  1141.             m_uLastBuffering = ulPercentDone;
  1142.         }
  1143.     }
  1144.     if (m_bInitialBuffering && HX_STATUS_READY == uStatusCode)
  1145.     {
  1146.         InitialBufferingDone();
  1147.         m_uLastBuffering = 100;
  1148.     }
  1149.     /* If we had a delayed start, we do not want to show that we are
  1150.      * in buffering state UNLESS it is really time to give out packets
  1151.      * and we do not have any.
  1152.      */
  1153.     if (m_bInitialized && m_ulDelay > 0 && m_pPlayer &&
  1154.         uStatusCode == HX_STATUS_BUFFERING &&
  1155.         ulPercentDone < 100)
  1156.     {
  1157.         UINT32 ulCurrentTime = m_pPlayer->GetInternalCurrentPlayTime();
  1158.         if ((ulCurrentTime+MIN_BUFFERTIME_BEFORE_DELAY) < m_ulDelay)
  1159.         {
  1160.             ulPercentDone = 100;
  1161.         }
  1162.     }
  1163. cleanup:
  1164.     if (m_pBufferCtl)
  1165.     {
  1166.         if ((HX_STATUS_READY == m_uLastStatusCode) &&
  1167.             (HX_STATUS_BUFFERING == uStatusCode))
  1168.         {
  1169.             UINT32 ulRemainToBufferInMs = 0;
  1170.             UINT32 ulRemainToBuffer = 0;
  1171.             if (m_pBufferManager)
  1172.             {
  1173.                 m_pBufferManager->GetRemainToBuffer(ulRemainToBufferInMs, 
  1174.                                                     ulRemainToBuffer);
  1175.             }
  1176.             
  1177.             m_pBufferCtl->OnBuffering(ulRemainToBufferInMs, ulRemainToBuffer);
  1178.         }
  1179.         else if ((HX_STATUS_BUFFERING == m_uLastStatusCode) &&
  1180.                  (HX_STATUS_READY == uStatusCode))
  1181.         {
  1182.             m_pBufferCtl->OnBufferingDone();
  1183.         }
  1184.     }
  1185.     m_uLastStatusCode = uStatusCode;
  1186.     if (m_bFastStart && !m_turboPlayStats.bBufferDone && HX_STATUS_READY == uStatusCode)
  1187.     {
  1188.         m_turboPlayStats.bBufferDone = TRUE;
  1189.         m_turboPlayStats.ulBufferedTime = CALCULATE_ELAPSED_TICKS(m_ulStartDataWait, HX_GET_TICKCOUNT());
  1190.     }
  1191.     return rc;
  1192. }
  1193. BOOL
  1194. HXNetSource::CanBeFastStarted(void)
  1195. {
  1196.     // In low heap mode we always want TurboPlay off.
  1197. #if defined(HELIX_FEATURE_TURBOPLAY)
  1198.     HXStream*   pStream = NULL;
  1199.     CHXSimpleList::Iterator i;
  1200.     m_bFastStart = TRUE;
  1201.     if (FALSE == m_pPlayer->CanBeFastStarted(m_pSourceInfo))
  1202.     {
  1203.         m_turboPlayStats.tpOffReason = m_pPlayer->m_turboPlayOffReason;
  1204.         m_bFastStart = FALSE;
  1205.         goto cleanup;
  1206.     }
  1207.     // no faststart on non-RTSP protocol
  1208.     if (!m_bRTSPProtocol)
  1209.     {
  1210.         DEBUG_OUT(m_pPlayer, DOL_TRANSPORT, (s,"(%p)Not RTSP - TurboPlay Off", this));
  1211.         m_turboPlayStats.tpOffReason = TP_OFF_BY_NONRTSP;
  1212.         m_bFastStart = FALSE;
  1213.         goto cleanup;
  1214.     }
  1215.     // faststart can be disabled by the server
  1216.     if (TURBO_PLAY_OFF == m_serverTurboPlay)
  1217.     {
  1218.         DEBUG_OUT(m_pPlayer, DOL_TRANSPORT, (s,"(%p)Disabled By Server - TurboPlay Off", this));
  1219.         m_turboPlayStats.tpOffReason = TP_OFF_BY_SERVER;
  1220.         m_bFastStart = FALSE;
  1221.         goto cleanup;
  1222.     }
  1223.     // no faststart on live source served from >= 9.0 server which get
  1224.     // rid of the ring buffer by default
  1225.     if (mLiveStream && HX_GET_MAJOR_VERSION(m_ulServerVersion) >= 9 &&
  1226.         (TURBO_PLAY_ON != m_serverTurboPlay))
  1227.     {
  1228.         DEBUG_OUT(m_pPlayer, DOL_TRANSPORT, (s,"(%p)Live From Server(>=9) - TurboPlay Off", this));
  1229.         m_turboPlayStats.tpOffReason = TP_OFF_BY_LIVE;
  1230.         m_bFastStart = FALSE;
  1231.         goto cleanup;
  1232.     }
  1233.     // no faststart on ROB presentation
  1234.     if (m_pPlayer->m_pEngine->m_lROBActive > 0)
  1235.     {
  1236.         DEBUG_OUT(m_pPlayer, DOL_TRANSPORT, (s,"(%p)ROB Presentation - TurboPlay Off", this));
  1237.         m_turboPlayStats.tpOffReason = TP_OFF_BY_ROB;
  1238.         m_bFastStart = FALSE;
  1239.         goto cleanup;
  1240.     }
  1241.     if (m_bFastStart)
  1242.     {
  1243.         if (!m_bRARVSourceVerified)
  1244.         {
  1245.             m_bRARVSourceVerified = TRUE;
  1246.             m_bRARVSource = IsRARVSource();
  1247.         }
  1248.         EnterFastStart();
  1249.     }
  1250. cleanup:
  1251.     return m_bFastStart;
  1252. #else
  1253.     return FALSE;
  1254. #endif /* HELIX_FEATURE_TURBOPLAY */
  1255. }
  1256. HX_RESULT
  1257. HXNetSource::IsFaststartPushdownFullfilled(REF(UINT16) uStatusCode,
  1258.                                             REF(UINT16) ulPercentDone)
  1259. {
  1260. #if defined(HELIX_FEATURE_TURBOPLAY)
  1261.     HX_RESULT           rc = HXR_OK;
  1262.     UINT32              ulWaitTime = 0;
  1263.     UINT32              ulPlayerTime  = 0;
  1264.     UINT32              ulDiff = 0;
  1265.     UINT32              ulPushdownMS = 0;
  1266.     UINT32              ulNumFrames =0;
  1267.     CHXAudioStream*     pCHXAudioStream = NULL;
  1268.     CHXAudioSession*    pAudioSession = NULL;
  1269.     HXStream*           pStream = NULL;
  1270.     HXAudioData        audioData;
  1271.     IUnknown*           pUnknown = NULL;
  1272.     IHXMediaPushdown*  pMediaPushdown = NULL;
  1273.     UINT32              ulAudioPushDownThreshold;
  1274.     UINT32              ulVideoPushDownThreshold;
  1275.     CHXSimpleList::Iterator i;
  1276.     uStatusCode = HX_STATUS_READY;
  1277.     ulPercentDone = 100;
  1278.     if (PUSHDOWN_ALL_DONE == m_pushDownStatus)
  1279.     {
  1280.         return rc;
  1281.     }
  1282.     if (!m_pPlayer)
  1283.     {
  1284.         return HXR_FAILED;
  1285.     }
  1286.     // compute our thresholds
  1287.     // I know peice wise defined functions are LAME!!
  1288.     // hello patel, this type of code is for you!
  1289.     if (m_maxPossibleAccelRatio > 3.9)
  1290.     {
  1291.         ulAudioPushDownThreshold = m_ulTurboPushDown / 2;
  1292.         ulVideoPushDownThreshold = 4;
  1293.     }
  1294.     else if (m_maxPossibleAccelRatio > 2.5)
  1295.     {
  1296.         ulAudioPushDownThreshold = (ULONG32) (m_ulTurboPushDown / 1.7);
  1297.         ulVideoPushDownThreshold = 8;
  1298.     }
  1299.     else
  1300.     {
  1301.         ulAudioPushDownThreshold = (ULONG32) (m_ulTurboPushDown / 1.6);
  1302.         ulVideoPushDownThreshold = 12;
  1303.     }
  1304.     if (!(PUSHDOWN_AUDIO_DONE & m_pushDownStatus))
  1305.     {
  1306. #if defined(HELIX_FEATURE_AUDIO)
  1307.         if (!m_pAudioStreamList)
  1308.         {
  1309.             CollectAudioStreams(m_pAudioStreamList);
  1310.         }
  1311.         if (m_pAudioStreamList && m_pAudioStreamList->GetCount())
  1312.         {
  1313.             memset(&audioData, 0, sizeof(HXAudioData));
  1314.             ulPlayerTime = m_pPlayer->GetCurrentPlayTime();
  1315.             if (mLiveStream)
  1316.             {
  1317.                 ulPlayerTime += m_ulFirstPacketTime;
  1318.             }
  1319.             for(i = m_pAudioStreamList->Begin(); i != m_pAudioStreamList->End(); ++i)
  1320.             {
  1321.                 pCHXAudioStream = (CHXAudioStream*)*i;
  1322.                 pCHXAudioStream->Write(&audioData);
  1323.                 // check audio push down
  1324.                 if (!m_bPushDownSet)
  1325.                 {
  1326.                     m_bPushDownSet = TRUE;
  1327.                     if (pCHXAudioStream && pCHXAudioStream->m_Owner)
  1328.                     {
  1329.                         pAudioSession = pCHXAudioStream->m_Owner->GetOwner();
  1330.                     }
  1331.                     if (pAudioSession && m_ulTurboPushDown < 2000)
  1332.                     {
  1333.                         pAudioSession->SetAudioPushdown(m_ulTurboPushDown);
  1334.                     }
  1335.                 }
  1336.                 // For live, ulPlayerTime could be > audioData.ulAudioTime if
  1337.                 // the renderer hasn't send 1st audio packet to the audio stream
  1338.                 if (audioData.ulAudioTime >= ulPlayerTime)
  1339.                 {
  1340.                     ulDiff = audioData.ulAudioTime - ulPlayerTime;
  1341.                 }
  1342.                 if (ulDiff >= ulAudioPushDownThreshold)
  1343.                 {
  1344.                     m_pushDownStatus = (PushDownStatus)(m_pushDownStatus | PUSHDOWN_AUDIO_DONE);
  1345.                     DEBUG_OUT(m_pPlayer, DOL_TRANSPORT, (s, "(%p)AudioPushDown Satisfied %lu %lu", this, ulDiff, ulAudioPushDownThreshold));
  1346.                     break;
  1347.                 }
  1348.             }
  1349.         }
  1350.         // no audio stream
  1351.         else
  1352. #endif /* HELIX_FEATURE_AUDIO */
  1353.         {
  1354.             m_pushDownStatus = (PushDownStatus)(m_pushDownStatus | PUSHDOWN_AUDIO_DONE);
  1355.             DEBUG_OUT(m_pPlayer, DOL_TRANSPORT, (s, "(%p)AudioPushDown Satisfied(no audio)", this));
  1356.         }
  1357.     }
  1358.     if (!(PUSHDOWN_VIDEO_DONE & m_pushDownStatus))
  1359.     {
  1360.         // check video push down
  1361.         for (i = m_HXStreamList.Begin(); i != m_HXStreamList.End(); ++i)
  1362.         {
  1363.             pStream = (HXStream*) (*i);
  1364.             // there should be 1 renderer per stream
  1365.             if (HXR_OK == pStream->GetRenderer(0, pUnknown))
  1366.             {
  1367.                 if (HXR_OK == pUnknown->QueryInterface(IID_IHXMediaPushdown, (void**)&pMediaPushdown))
  1368.                 {
  1369.                     if (pStream->m_bPostSeekToBeSent)
  1370.                     {
  1371.                         goto cleanup;
  1372.                     }
  1373.                     pMediaPushdown->GetCurrentPushdown(ulPushdownMS, ulNumFrames);
  1374.                     // check to see if we are using G2 video
  1375.                     if (pMediaPushdown->IsG2Video())
  1376.                     {
  1377.                         if (ulNumFrames < 9 && ulNumFrames < ulVideoPushDownThreshold)
  1378.                         {
  1379.                             goto cleanup;
  1380.                         }
  1381.                     }
  1382.                     else
  1383.                     {
  1384.                         if (ulNumFrames < ulVideoPushDownThreshold)
  1385.                         {
  1386.                             goto cleanup;
  1387.                         }
  1388.                     }
  1389.                 }
  1390.                 HX_RELEASE(pMediaPushdown);
  1391.             }
  1392.             HX_RELEASE(pUnknown);
  1393.         }
  1394.         m_pushDownStatus = (PushDownStatus)(m_pushDownStatus | PUSHDOWN_VIDEO_DONE);
  1395.         DEBUG_OUT(m_pPlayer, DOL_TRANSPORT, (s, "(%p)VideoPushDown Satisfied %lu %lu", this, ulNumFrames, ulVideoPushDownThreshold));
  1396.     }
  1397.     // Milko wants to make it such that TimeStampDelivered
  1398.     // IsTimeStampDelivery
  1399. cleanup:
  1400.     HX_RELEASE(pMediaPushdown);
  1401.     HX_RELEASE(pUnknown);
  1402.     if (PUSHDOWN_ALL_DONE == m_pushDownStatus)
  1403.     {
  1404.         if (m_bInitialBuffering)
  1405.         {
  1406.             InitialBufferingDone();
  1407.         }
  1408.         m_uLastBuffering = 100;
  1409.         m_ulTurboStartActiveTime = HX_GET_TICKCOUNT();
  1410.         DEBUG_OUT(m_pPlayer, DOL_TRANSPORT, (s, "(%p)TURBO Started", this));
  1411.     }
  1412.     else
  1413.     {
  1414.         uStatusCode = HX_STATUS_BUFFERING;
  1415.         ulPercentDone = 0;
  1416.         // we switch to the normal status calculation if
  1417.         // we detect the TurboPlay takes unreasonable amount of time to start(>8s)
  1418.         ulWaitTime = CALCULATE_ELAPSED_TICKS(m_ulStartDataWait, HX_GET_TICKCOUNT());
  1419.         if (ulWaitTime >= 8000)
  1420.         {
  1421.             rc = HXR_FAILED;
  1422.         }
  1423.     }
  1424.     return rc;
  1425. #else
  1426.     return HXR_NOTIMPL;
  1427. #endif /* HELIX_FEATURE_TURBOPLAY */
  1428. }
  1429. BOOL
  1430. HXNetSource::IsRARVSource(void)
  1431. {
  1432. #if defined(HELIX_FEATURE_TURBOPLAY)
  1433.     BOOL            bResult = TRUE;
  1434.     IHXBuffer*      pMimeType = NULL;
  1435.     STREAM_INFO*    pStreamInfo = NULL;
  1436.     CHXMapLongToObj::Iterator lStreamIterator = mStreamInfoTable->Begin();
  1437.     for (; lStreamIterator != mStreamInfoTable->End(); ++lStreamIterator)
  1438.     {
  1439.         pStreamInfo = (STREAM_INFO*) (*lStreamIterator);
  1440.         if (pStreamInfo->m_pHeader &&
  1441.             HXR_OK == pStreamInfo->m_pHeader->GetPropertyCString("MimeType", pMimeType))
  1442.         {
  1443.             const char* pszMimeType = (const char*)pMimeType->GetBuffer();
  1444.             // all "-encrypted" mimetypes should have been stripped in FixUpMime()
  1445.             if (strcmp(pszMimeType, REALMEDIA_MIME_TYPE) != 0                   &&
  1446.                 strcmp(pszMimeType, REALAUDIO_MIME_TYPE) != 0                   &&
  1447.                 strcmp(pszMimeType, REALVIDEO_MIME_TYPE) != 0                   &&
  1448.                 strcmp(pszMimeType, REALAUDIO_MULTIRATE_MIME_TYPE) != 0         &&
  1449.                 strcmp(pszMimeType, REALAUDIO_MULTIRATE_LIVE_MIME_TYPE) != 0    &&
  1450.                 strcmp(pszMimeType, REALVIDEO_MULTIRATE_MIME_TYPE) != 0)
  1451.             {
  1452.                 bResult = FALSE;
  1453.                 break;
  1454.             }
  1455.         }
  1456.         HX_RELEASE(pMimeType);
  1457.     }
  1458.     HX_RELEASE(pMimeType);
  1459.     return bResult;
  1460. #else
  1461.     return FALSE;
  1462. #endif /* HELIX_FEATURE_TURBOPLAY */
  1463. }
  1464. UINT16
  1465. HXNetSource::GetNumStreams(void)
  1466. {
  1467.     HX_ASSERT(m_bInitialized);
  1468.     return m_uNumStreams;
  1469. }
  1470. HX_RESULT
  1471. HXNetSource::GetStreamInfo(ULONG32          ulStreamNumber,
  1472.                             STREAM_INFO*&   theStreamInfo)
  1473. {
  1474.     HX_RESULT       theErr = HXR_OK;
  1475.     STREAM_INFO*    pStreamInfo = NULL;
  1476.     STREAM_STATS*   pStreamStats = NULL;
  1477.     if (!mStreamInfoTable->Lookup((LONG32)ulStreamNumber, (void *&)pStreamInfo))
  1478.     {
  1479.         theErr = HXR_INVALID_PARAMETER;
  1480.     }
  1481. #if defined(HELIX_FEATURE_STATS) && defined(HELIX_FEATURE_REGISTRY)
  1482.     if (m_pProto && HXR_OK == m_pProto->GetStreamStatistics(ulStreamNumber, &pStreamStats))
  1483.     {
  1484.         pStreamInfo->m_pStats = pStreamStats;
  1485.         if (!pStreamInfo->m_pStats)
  1486.         {
  1487.             return HXR_UNEXPECTED;
  1488.         }
  1489.     }
  1490. #endif /* HELIX_FEATURE_STATS && HELIX_FEATURE_REGISTRY */
  1491.     theStreamInfo = pStreamInfo;
  1492.     return theErr;
  1493. }
  1494. HX_RESULT
  1495. HXNetSource::GetEvent(UINT16 usStreamNumber, CHXEvent*& pEvent)
  1496. {
  1497.     HX_TRACE("HXNetSource::GetEvent");
  1498.     if (!m_bPlayFromRecordControl)
  1499.     {
  1500.         HX_RESULT nResult = GetEventFromProtocol(usStreamNumber, pEvent);
  1501. #if defined(HELIX_FEATURE_RECORDCONTROL)
  1502.         // Case of record-only RecordControl (no playback support).
  1503.         if (pEvent && m_pRecordControl)
  1504.         {
  1505.             m_pRecordControl->OnPacket(pEvent->GetPacket(), pEvent->GetTimeOffset());
  1506.         }
  1507. #endif /* HELIX_FEATURE_RECORDCONTROL */
  1508.         return nResult;
  1509.     }
  1510.     pEvent = NULL;
  1511.     HX_RESULT nResult = HXR_OK;
  1512.     STREAM_INFO*    pStreamInfo = NULL;
  1513.     if (!mStreamInfoTable->Lookup((LONG32) usStreamNumber, (void *&) pStreamInfo))
  1514.     {
  1515.         HX_ASSERT(FALSE);
  1516.         return HXR_INVALID_PARAMETER;
  1517.     }
  1518.     if (pStreamInfo->m_bReconnectToBeDone)
  1519.     {
  1520.         CHXEventList*  pEventList = &pStreamInfo->m_EventList;
  1521.         if (pEventList->GetNumEvents())
  1522.             pEvent = pEventList->RemoveHead();
  1523.         return pEvent ? HXR_OK : HXR_NO_DATA;
  1524.     }
  1525.     return GetEventFromRecordControl(usStreamNumber, pEvent);
  1526. }
  1527. HX_RESULT
  1528. HXNetSource::GetEventFromRecordControl(UINT16 usStreamNumber, CHXEvent*& pEvent)
  1529. {
  1530. #if defined(HELIX_FEATURE_RECORDCONTROL)
  1531.     if(!m_bPlayFromRecordControl)
  1532.         return HXR_UNEXPECTED;
  1533.     HX_ASSERT(m_pRecordControl);
  1534.     pEvent = NULL;
  1535.     IHXPacket* pPacket = NULL;
  1536.     HX_RESULT nResult = m_pRecordControl->GetPacket(usStreamNumber, pPacket);
  1537.     STREAM_INFO*    pStreamInfo = NULL;
  1538.     mStreamInfoTable->Lookup((LONG32) usStreamNumber, (void *&) pStreamInfo);
  1539.     if(nResult == HXR_OK)
  1540.     {
  1541.         UINT32      ulEventTime = 0;
  1542.         if (pStreamInfo)
  1543.         {
  1544.             if (!CanSendToDataCallback(pPacket))
  1545.             {
  1546. UINT32 ulLastPkt = 
  1547.     pStreamInfo->BufferingState().LastPacketTimestamp();
  1548.                 // Use timestamp from the last packet
  1549.                 ulEventTime = AdjustEventTime(pStreamInfo, ulLastPkt);
  1550.             }
  1551.             else
  1552.             {
  1553.                 ulEventTime = AdjustEventTime(pStreamInfo, pPacket->GetTime());
  1554.                 /* Update buffering info and stats */
  1555.                 DataCallback(pPacket);
  1556.             }
  1557.         }
  1558.         pEvent = new CHXEvent(pPacket, 0);
  1559.         HX_RELEASE(pPacket);
  1560.         if(!pEvent)
  1561.             return HXR_FAILED;
  1562.         pEvent->SetTimeStartPos(ulEventTime);
  1563.         pEvent->SetTimeOffset(m_ulStartTime - m_ulDelay);
  1564.     }
  1565.     else
  1566.     {
  1567.         if(nResult == HXR_NO_DATA && (m_bSourceEnd || pStreamInfo->m_bSrcStreamDone))
  1568.             nResult = HXR_AT_END;
  1569.         if(nResult == HXR_NO_DATA && m_pBufferManager)
  1570.         {
  1571.             UINT32          ulRemainToBufferInMs = 0;
  1572.             UINT32          ulRemainToBuffer = 0;
  1573.             m_pBufferManager->GetRemainToBuffer(ulRemainToBufferInMs, ulRemainToBuffer);
  1574.             if (ulRemainToBufferInMs || ulRemainToBuffer)
  1575.             {
  1576.                 nResult = HXR_BUFFERING;
  1577.             }
  1578.             else if (pStreamInfo->m_unNeeded > 0 &&
  1579.                      pStreamInfo->m_unNeeded != pStreamInfo->m_unAvailable)
  1580.             {
  1581.                 nResult = HXR_BUFFERING;
  1582.                 m_uLastBuffering = 0;
  1583.                 m_pBufferManager->ReBuffer();
  1584.             }
  1585.         }
  1586.     }
  1587.     return nResult;
  1588. #else
  1589.     return HXR_NOTIMPL;
  1590. #endif /* HELIX_FEATURE_RECORDCONTROL */
  1591. }
  1592. HX_RESULT
  1593. HXNetSource::GetEventFromProtocol(UINT16 usStreamNumber, CHXEvent*& pEvent)
  1594. {
  1595.     HX_RESULT       theErr = HXR_OK;
  1596.     UINT32          ulEventTime = 0;
  1597.     UINT32          ulRemainToBufferInMs = 0;
  1598.     UINT32          ulRemainToBuffer = 0;
  1599.     IHXPacket*      pPacket = NULL;
  1600.     STREAM_INFO*    pStreamInfo = NULL;
  1601.     CHXEvent*       pTempEvent = NULL;
  1602.     CHXEventList*  pEventList = NULL;
  1603.     CHXSimpleList::Iterator lIter;
  1604.     pEvent = NULL;
  1605.     if (!m_bInitialized && m_state == NETSRC_READY)
  1606.     {
  1607.         return HXR_NO_DATA;
  1608.     }
  1609.     if (m_bPaused && m_bDelayed)
  1610.     {
  1611.         if (TryResume() && m_pPlayer)
  1612.         {
  1613.             m_pPlayer->RegisterSourcesDone();
  1614.             DoResume();
  1615.         }
  1616.         else
  1617.         {
  1618.             return HXR_NO_DATA;
  1619.         }
  1620.     }
  1621.     /* give some time to the net object...*/
  1622.     theErr = _ProcessIdle();
  1623.     if (theErr)
  1624.     {
  1625.         return theErr;
  1626.     }
  1627.     if (!mStreamInfoTable->Lookup((LONG32) usStreamNumber, (void *&) pStreamInfo))
  1628.     {
  1629.         HX_ASSERT(FALSE);
  1630.         return HXR_INVALID_PARAMETER;
  1631.     }
  1632.     if (pStreamInfo->m_bReconnectToBeDone)
  1633.     {
  1634.         pEventList = &pStreamInfo->m_EventList;
  1635.         if (pEventList->GetNumEvents() && !m_bPlayFromRecordControl)
  1636.         {
  1637.             pEvent = pEventList->RemoveHead();
  1638. //{FILE* f1 = ::fopen("c:\temp\reconnect.txt", "a+"); ::fprintf(f1, "GetEventFromPreReconnectt%lut%lun", usStreamNumber, pEvent->GetPacket()->GetTime());::fclose(f1);}
  1639.         }
  1640.         if (m_pProto)
  1641.         {
  1642.             while (TRUE)
  1643.             {
  1644.                 theErr = m_pProto->GetEvent(usStreamNumber, pTempEvent);
  1645.                 if (theErr)
  1646.                 {
  1647.                     // Mask off any non-crucial errors
  1648.                     switch (theErr)
  1649.                     {
  1650.                         case HXR_AT_END:
  1651.                         case HXR_NO_DATA:
  1652.                         case HXR_BUFFERING:
  1653.                             theErr = HXR_OK;
  1654.                             break;
  1655.                         default:
  1656.                             break;
  1657.                     }
  1658.                     // if there is still an error, it needs to be reported
  1659.                     if (theErr)
  1660.                     {
  1661.                         return theErr;
  1662.                     }
  1663.                     break;
  1664.                 }
  1665.                 HX_ASSERT(pTempEvent);
  1666.                 if (!pStreamInfo->m_pPosReconnectEventList)
  1667.                 {
  1668.                     pStreamInfo->m_pPosReconnectEventList = new CHXEventList;
  1669.                 }
  1670. //{FILE* f1 = ::fopen("c:\temp\reconnect.txt", "a+"); ::fprintf(f1, "AddEventToPosReconnectt%lut%lun", usStreamNumber, pTempEvent->GetPacket()->GetTime());::fclose(f1);}
  1671.                 pStreamInfo->m_pPosReconnectEventList->InsertEvent(pTempEvent);
  1672.             }
  1673.             ProcessReconnect(pStreamInfo);
  1674.         }
  1675.     }
  1676.     else
  1677.     {
  1678.         pEventList = pStreamInfo->m_pPosReconnectEventList;
  1679.         if (pEventList && pEventList->GetNumEvents())
  1680.         {
  1681.             pEvent = pEventList->RemoveHead();
  1682. //{FILE* f1 = ::fopen("c:\temp\reconnect.txt", "a+"); ::fprintf(f1, "GetEventFromPosReconnectt%lut%lun", usStreamNumber, pEvent->GetPacket()->GetTime());::fclose(f1);}
  1683.         }
  1684.         if (!pEvent && m_pProto)
  1685.         {
  1686.             while (TRUE)
  1687.             {
  1688.                 theErr = m_pProto->GetEvent(usStreamNumber, pEvent);
  1689.                 if (theErr)
  1690.                 {
  1691.                     // Mask off any non-crucial errors
  1692.                     switch (theErr)
  1693.                     {
  1694.                         case HXR_AT_END:
  1695.                         case HXR_NO_DATA:
  1696.                         case HXR_BUFFERING:
  1697.                             theErr = HXR_OK;
  1698.                             break;
  1699.                         default:
  1700.                             break;
  1701.                     }
  1702.                     // if there is still an error, it needs to be reported
  1703.                     if (theErr)
  1704.                     {
  1705.                         return theErr;
  1706.                     }
  1707.                     break;
  1708.                 }
  1709.                 HX_ASSERT(pEvent);
  1710.                 if (!pStreamInfo->m_ulReconnectOverlappedPackets)
  1711.                 {
  1712.                     break;
  1713.                 }
  1714. //{FILE* f1 = ::fopen("c:\temp\reconnect.txt", "a+"); ::fprintf(f1, "DeleteEvent(overlapped)t%lut%lun", usStreamNumber, pEvent->GetPacket()->GetTime());::fclose(f1);}
  1715.                 HX_DELETE(pEvent);
  1716.                 pStreamInfo->m_ulReconnectOverlappedPackets--;
  1717.             }
  1718.             if (pEvent)
  1719.             {
  1720. //{FILE* f1 = ::fopen("c:\temp\reconnect.txt", "a+"); ::fprintf(f1, "GetEventFromTransportt%lut%lun", usStreamNumber, pEvent->GetPacket()->GetTime());::fclose(f1);}
  1721.             }
  1722.         }
  1723.         if (pEvent)
  1724.         {
  1725.             AddToPreReconnectEventList(pStreamInfo, pEvent);
  1726.         }
  1727.     }
  1728.     if (pEvent)
  1729.     {
  1730.         pPacket = pEvent->GetPacket();
  1731.         if (pPacket)
  1732.         {
  1733.             if (!m_bPlayFromRecordControl &&
  1734. CanSendToDataCallback(pPacket)&& !pEvent->IsPreSeekEvent())
  1735.             {
  1736.                 /* Update buffering info and stats */
  1737.                 DataCallback(pPacket);
  1738.             }
  1739.             if (pPacket->IsLost())
  1740.             {
  1741. UINT32 ulLastPkt = 
  1742.     pStreamInfo->BufferingState().LastPacketTimestamp();
  1743.                 // Use timestamp from the last packet
  1744.                 ulEventTime = AdjustEventTime(pStreamInfo, ulLastPkt);
  1745.             }
  1746.             else
  1747.             {
  1748.                 ulEventTime = AdjustEventTime(pStreamInfo, pPacket->GetTime());
  1749.             }
  1750.             pEvent->SetTimeStartPos(ulEventTime);
  1751.         }
  1752.         pEvent->SetTimeOffset(m_ulStartTime - m_ulDelay);
  1753.     }
  1754.     else if (m_bSourceEnd || pStreamInfo->m_bSrcStreamDone)
  1755.     {
  1756.         theErr = HXR_AT_END;
  1757.     }
  1758.     else
  1759.     {
  1760.         if(m_bPlayFromRecordControl)
  1761.         {
  1762.             theErr = HXR_NO_DATA;
  1763.         }
  1764.         else
  1765.         {
  1766.             m_pBufferManager->GetRemainToBuffer(ulRemainToBufferInMs, ulRemainToBuffer);
  1767.             if (ulRemainToBufferInMs || ulRemainToBuffer)
  1768.             {
  1769.                 theErr = HXR_BUFFERING;
  1770.             }
  1771.             else if (pStreamInfo->m_unNeeded > 0 &&
  1772.                      pStreamInfo->m_unNeeded != pStreamInfo->m_unAvailable)
  1773.             {
  1774.                 theErr = HXR_BUFFERING;
  1775.                 m_uLastBuffering = 0;
  1776.                 m_pBufferManager->ReBuffer();
  1777.             }
  1778.             else
  1779.             {
  1780.                 theErr = HXR_NO_DATA;
  1781.             }
  1782.         }
  1783.     }
  1784.     if (m_pBufferCtl)
  1785.     {
  1786. if (theErr == HXR_BUFFERING)
  1787. {
  1788.     /* Make sure we have not paused the server when we are in a
  1789.      * buffering state
  1790.      */
  1791.     m_pBufferCtl->OnBuffering(ulRemainToBufferInMs, ulRemainToBuffer);
  1792. }
  1793. else if (theErr == HXR_AT_END)
  1794. {
  1795.     m_pBufferCtl->OnClipEnd();
  1796. }
  1797.     }
  1798. #ifdef LOSS_HACK
  1799.     if (m_ulLossHack > 0 && ((UINT32) (rand() % 100) < m_ulLossHack) &&
  1800.         !theErr && pEvent && !(pEvent->GetPacket())->IsLost())
  1801.     {
  1802.         GenerateFakeLostPacket(pEvent);
  1803.         /* Update the stats */
  1804.         pStreamInfo->m_ulLost++;
  1805.     }
  1806. #endif /* LOSS_HACK */
  1807.     return theErr;
  1808. }
  1809. void
  1810. HXNetSource::ReBuffer()
  1811. {
  1812.     UINT32  ulRemainToBufferInMs = 0;
  1813.     UINT32  ulRemainToBuffer = 0;
  1814.     m_pBufferManager->GetRemainToBuffer(ulRemainToBufferInMs,
  1815.                                         ulRemainToBuffer);
  1816. //    DEBUG_OUT(m_pPlayer, DOL_GENERIC, (s,
  1817. //      "Rebuffer %p CurrentTime: %lu RemainMs: %lu RemainBytes: %lu", this,
  1818. //                  HX_GET_TICKCOUNT(), ulRemainToBufferInMs, ulRemainToBuffer));
  1819.     if (ulRemainToBufferInMs == 0 &&
  1820.         ulRemainToBuffer == 0)
  1821.     {
  1822.         m_uLastBuffering = 0;
  1823.         m_pBufferManager->ReBuffer();
  1824.     }
  1825. }
  1826. HX_RESULT
  1827. HXNetSource::UpdateRegistry(UINT32 ulRegistryID)
  1828. {
  1829. #if defined(HELIX_FEATURE_STATS) && defined(HELIX_FEATURE_REGISTRY)
  1830.     HX_RESULT       theErr = HXR_OK;
  1831.     UINT32          ulRepeatedRegistryID = 0;
  1832.     UINT32          ulRegId = 0;
  1833.     char            szRegName[MAX_DISPLAY_NAME] = {0}; /* Flawfinder: ignore */
  1834.     IHXBuffer*      pParentName = NULL;
  1835.     IHXBuffer*      pRepeatRegName = NULL;
  1836.     STREAM_INFO*    pStreamInfo = NULL;
  1837.     SOURCE_STATS*   pNewStats = NULL;
  1838.     CHXMapLongToObj::Iterator   ndxStream;
  1839.     m_ulRegistryID = ulRegistryID;
  1840.     if (!m_pStats)
  1841.     {
  1842.         // XXX HP, Hummmmmmmm ....
  1843.         HX_ASSERT(FALSE);
  1844.         SetupRegistry();
  1845.     }
  1846.     else if (m_ulRegistryID != m_pStats->m_ulRegistryID)
  1847.     {
  1848. #if defined(HELIX_FEATURE_SMIL_REPEAT)
  1849.         // repeated source
  1850.         if (!m_pSourceInfo->m_bLeadingSource ||
  1851.              m_pSourceInfo->m_pRepeatList)
  1852.         {
  1853.             if (m_pStatsManager)
  1854.             {
  1855.                 m_pStatsManager->UpdateRegistry(m_ulRegistryID);
  1856.             }
  1857.             else if (m_pRegistry &&
  1858.                      HXR_OK == m_pRegistry->GetPropName(m_pPlayer->m_ulRepeatedRegistryID, pRepeatRegName))
  1859.             {
  1860.                 SafeSprintf(szRegName, MAX_DISPLAY_NAME, "%s.%ld%ld%ld",
  1861.                         pRepeatRegName->GetBuffer(),
  1862.                         m_pSourceInfo->m_uGroupID,
  1863.                         m_pSourceInfo->m_uTrackID,
  1864.                         (int)m_pSourceInfo->m_bLeadingSource);
  1865.                 ulRepeatedRegistryID = m_pRegistry->GetId(szRegName);
  1866.                 if (!ulRepeatedRegistryID)
  1867.                 {
  1868.                     ulRepeatedRegistryID = m_pRegistry->AddComp(szRegName);
  1869.                 }
  1870.                 m_pStatsManager = new StatsManager(m_pRegistry, m_ulRegistryID, ulRepeatedRegistryID);
  1871.                 m_pStatsManager->AddRef();
  1872.                 pNewStats = new SOURCE_STATS(m_pRegistry, ulRepeatedRegistryID);
  1873.             }
  1874.             else
  1875.             {
  1876.                 // why stats' creation failed??
  1877.                 HX_ASSERT(FALSE);
  1878.             }
  1879.             HX_RELEASE(pRepeatRegName);
  1880.         }
  1881.         // normal source
  1882.         else
  1883. #endif /* HELIX_FEATURE_SMIL_REPEAT */
  1884.         {
  1885.             pNewStats = new SOURCE_STATS(m_pRegistry, m_ulRegistryID);
  1886.         }
  1887.         if (pNewStats && m_pPlayer)
  1888.         {
  1889.             *pNewStats = *m_pStats;
  1890.             ndxStream = mStreamInfoTable->Begin();
  1891.             for(; ndxStream != mStreamInfoTable->End(); ++ndxStream)
  1892.             {
  1893.                 pStreamInfo = (STREAM_INFO*) (*ndxStream);
  1894.                 if (m_pRegistry     &&
  1895.                     pNewStats       &&
  1896.                     HXR_OK == m_pRegistry->GetPropName(pNewStats->m_ulRegistryID, pParentName))
  1897.                 {
  1898.                     SafeSprintf(szRegName, MAX_DISPLAY_NAME, "%s.Stream%ld", pParentName->GetBuffer(),
  1899.                             pStreamInfo->m_uStreamNumber);
  1900.                     ulRegId = m_pRegistry->GetId(szRegName);
  1901.                     if (!ulRegId)
  1902.                     {
  1903.                         ulRegId = m_pRegistry->AddComp(szRegName);
  1904.                     }
  1905.                     if(m_pProto)
  1906.                         m_pProto->UpdateRegistry(pStreamInfo->m_uStreamNumber, ulRegId);
  1907.                 }
  1908.                 HX_RELEASE(pParentName);
  1909.             }
  1910.             HX_DELETE(m_pStats);
  1911.             m_pStats = pNewStats;
  1912.         }
  1913.     }
  1914.     return theErr;
  1915. #else
  1916.     return HXR_NOTIMPL;
  1917. #endif /* HELIX_FEATURE_STATS && HELIX_FEATURE_REGISTRY */
  1918. }
  1919. void
  1920. HXNetSource::LeavePrefetch(void)
  1921. {
  1922. #if defined(HELIX_FEATURE_PREFETCH)
  1923.     m_bPrefetch = FALSE;
  1924.     if (m_pProto)
  1925.     {
  1926.         m_pProto->LeavePrefetch();
  1927.     }
  1928.     // send prefetch notification so that SMIL
  1929.     // renderer can resolve the duration on this prefetch track
  1930.     if (m_pSourceInfo)
  1931.     {
  1932.         m_pPlayer->PrefetchTrackDone(m_pSourceInfo->m_uGroupID,
  1933.                                      m_pSourceInfo->m_uTrackID,
  1934.                                      HXR_OK);
  1935.     }
  1936. #endif /* HELIX_FEATURE_PREFETCH */
  1937.     return;
  1938. }
  1939. void
  1940. HXNetSource::EnterFastStart(void)
  1941. {
  1942. #if defined(HELIX_FEATURE_TURBOPLAY)
  1943.     DEBUG_OUT(m_pPlayer, DOL_TRANSPORT, (s, "(%p)Enter TurboPlay", this));
  1944.     m_bFastStart = TRUE;
  1945.     if (m_pProto)
  1946.     {
  1947.         m_pProto->EnterFastStart();
  1948.     }
  1949. #endif /* HELIX_FEATURE_TURBOPLAY */
  1950.     return;
  1951. }
  1952. void
  1953. HXNetSource::LeaveFastStart(TurboPlayOffReason leftReason)
  1954. {
  1955. #if defined(HELIX_FEATURE_TURBOPLAY)
  1956.     DEBUG_OUT(m_pPlayer, DOL_TRANSPORT, (s, "(%p)Leave TurboPlay", this));
  1957.     m_turboPlayStats.tpOffReason = leftReason;
  1958.     m_bFastStart = FALSE;
  1959.     if (m_pProto)
  1960.     {
  1961.         m_pProto->LeaveFastStart();
  1962.     }
  1963. #endif /* HELIX_FEATURE_TURBOPLAY */
  1964.     return;
  1965. }
  1966. BOOL
  1967. HXNetSource::IsPrefetchEnded(void)
  1968. {
  1969. #if defined(HELIX_FEATURE_PREFETCH)
  1970.     BOOL    bResult = FALSE;
  1971.     UINT16  uStreamDone = 0;
  1972.     UINT32  ulNumBytes = 0;
  1973.     INT64   llLowestTimestamp = MAX_UINT32;
  1974.     INT64   llHighestTimestamp = 0;
  1975.     CHXMapLongToObj::Iterator lStreamIterator = mStreamInfoTable->Begin();
  1976.     for (; lStreamIterator != mStreamInfoTable->End(); ++lStreamIterator)
  1977.     {
  1978.         STREAM_INFO*    pStreamInfo = (STREAM_INFO*) (*lStreamIterator);
  1979.         UINT16          uStreamNumber = pStreamInfo->m_uStreamNumber;
  1980.         INT64   llStreamLowestTimestamp = 0;
  1981.         INT64   llStreamHighestTimestamp = 0;
  1982.         UINT32  ulStreamNumBytes = 0;
  1983.         BOOL    bStreamDone = FALSE;
  1984.         GetCurrentBuffering(uStreamNumber,
  1985.                             llStreamLowestTimestamp,
  1986.                             llStreamHighestTimestamp,
  1987.                             ulStreamNumBytes,
  1988.                             bStreamDone);
  1989.         if (llLowestTimestamp > llStreamLowestTimestamp)
  1990.         {
  1991.             llLowestTimestamp = llStreamLowestTimestamp;
  1992.         }
  1993.         if (llHighestTimestamp < llStreamHighestTimestamp)
  1994.         {
  1995.             llHighestTimestamp = llStreamHighestTimestamp;
  1996.         }
  1997.         ulNumBytes += ulStreamNumBytes;
  1998.         if (bStreamDone)
  1999.         {
  2000.             uStreamDone++;
  2001.         }
  2002.     }
  2003.     // prefetch done once we have cached all the data
  2004.     if (uStreamDone == mStreamInfoTable->GetCount())
  2005.     {
  2006.         bResult = TRUE;
  2007.     }
  2008.     // verify the status of prefetch based on prefetch type/value
  2009.     else
  2010.     {
  2011.         switch (m_prefetchType)
  2012.         {
  2013.         case PrefetchTime:
  2014.             if (llHighestTimestamp - llLowestTimestamp >= m_ulPrefetchValue)
  2015.             {
  2016.                 bResult = TRUE;
  2017.             }
  2018.             break;
  2019.         case PrefetchTimePercent:
  2020.             // convert to PrefetchTime
  2021.             m_prefetchType = PrefetchTime;
  2022.             m_ulPrefetchValue = (UINT32)(m_ulOriginalDuration * m_ulPrefetchValue / 100.0);
  2023.             break;
  2024.         case PrefetchBytes:
  2025.             if (ulNumBytes >= m_ulPrefetchValue)
  2026.             {
  2027.                 bResult = TRUE;
  2028.             }
  2029.             break;
  2030.         case PrefetchBytesPercent:
  2031.             // convert to PrefetchBytes
  2032.             m_prefetchType = PrefetchBytes;
  2033. #if defined(HELIX_FEATURE_STATS) && defined(HELIX_FEATURE_REGISTRY)
  2034.             m_ulPrefetchValue = (UINT32)(m_pStats->m_pClipBandwidth->GetInt() * m_ulOriginalDuration * m_ulPrefetchValue / 800.0);
  2035. #endif /* HELIX_FEATURE_STATS && HELIX_FEATURE_REGISTRY */
  2036.             break;
  2037.         case PrefetchBandwidth:
  2038.             // no bandwidth control for PNA
  2039.             if (!m_bRTSPProtocol)
  2040.             {
  2041.                 bResult = TRUE;
  2042.             }
  2043.             else
  2044.             {
  2045.                 IHXThinnableSource* pThin = NULL;
  2046.                 if (m_pProto &&
  2047.                     HXR_OK == m_pProto->QueryInterface(IID_IHXThinnableSource, (void **)&pThin))
  2048.                 {
  2049.                     pThin->SetDeliveryBandwidth(m_ulPrefetchValue, 0);
  2050.                 }
  2051.                 HX_RELEASE(pThin);
  2052.                 // change the prefetch type so we only SetDeliveryBandwidth once
  2053.                 m_prefetchType = PrefetchUnknown;
  2054.                 m_ulPrefetchValue = 0;
  2055.             }
  2056.             break;
  2057.         case PrefetchBandwidthPercent:
  2058.             {
  2059.                 // convert to PrefetchBandwidth
  2060.                 m_prefetchType = PrefetchBandwidth;
  2061.                 // get total bandwidth from the preferences
  2062.                 UINT32 ulBandwidth = 0;
  2063.                 if (HXR_OK == ReadPrefINT32(m_pPreferences, "Bandwidth", ulBandwidth))
  2064.                 {
  2065.                     m_ulPrefetchValue = (UINT32)(ulBandwidth * m_ulPrefetchValue / 100.0);
  2066.                 }
  2067.             }
  2068.             break;
  2069.         case PrefetchMaxAllowedPlus1:
  2070.             // what's this???
  2071.             break;
  2072.         default:
  2073.             break;
  2074.         }
  2075.     }
  2076.     return bResult;
  2077. #else
  2078.     return TRUE;
  2079. #endif /* HELIX_FEATURE_PREFETCH */
  2080. }
  2081. HX_RESULT
  2082. HXNetSource::UpdateStatistics(void)
  2083. {
  2084. #if defined(HELIX_FEATURE_STATS) && defined(HELIX_FEATURE_REGISTRY)
  2085.     ULONG32     ulSourceTotal = 0;
  2086.     ULONG32     ulSourceReceived = 0;
  2087.     ULONG32     ulSourceNormal = 0;
  2088.     UINT32      ulSourceTotal30 = 0;
  2089.     UINT32      ulSourceLost30 = 0;
  2090.     ULONG32     ulSourceRecovered = 0;
  2091.     ULONG32     ulSourceDuplicate = 0;
  2092.     ULONG32     ulSourceOutOfOrder = 0;
  2093.     ULONG32     ulSourceLost = 0;
  2094.     ULONG32     ulSourceLate = 0;
  2095.     ULONG32     ulSourceResendRequested = 0;
  2096.     ULONG32     ulSourceResendReceived = 0;
  2097.     ULONG32     ulSourceBandwidth = 0;
  2098.     ULONG32     ulSourceCurBandwidth = 0;
  2099.     ULONG32     ulSourceAvgBandwidth = 0;
  2100.     INT32       lAvgLatency = 0;
  2101.     INT32       lHighLatency = 0;
  2102.     INT32       lLowLatency = 0xFFFF;
  2103.     IHXStatistics* pStatistics = NULL;
  2104.     if (!m_bInitialized)
  2105.     {
  2106.         return HXR_OK;
  2107.     }
  2108.     if (m_pProto && HXR_OK == m_pProto->QueryInterface(IID_IHXStatistics, (void**) &pStatistics))
  2109.     {
  2110.         pStatistics->UpdateStatistics();
  2111.         pStatistics->Release();
  2112.         pStatistics = NULL;
  2113.     }
  2114.     CHXMapLongToObj::Iterator lStreamIterator = mStreamInfoTable->Begin();
  2115.     for (; lStreamIterator != mStreamInfoTable->End(); ++lStreamIterator)
  2116.     {
  2117.         ULONG32         ulStreamNumber = 0;
  2118.         STREAM_INFO*    pStreamInfo    = (STREAM_INFO*) (*lStreamIterator);
  2119.         STREAM_STATS*   pStreamStats   = NULL;
  2120.         if (m_pProto && HXR_OK == m_pProto->GetStreamStatistics((ULONG32) pStreamInfo->m_uStreamNumber, &pStreamStats))
  2121.         {
  2122.             if (!pStreamStats || !pStreamStats->m_bInitialized)
  2123.             {
  2124.                 continue;
  2125.             }
  2126.             ulSourceTotal           += pStreamStats->m_pTotal->GetInt();
  2127.             ulSourceReceived        += pStreamStats->m_pReceived->GetInt();
  2128.             ulSourceNormal          += pStreamStats->m_pNormal->GetInt();
  2129.             ulSourceRecovered       += pStreamStats->m_pRecovered->GetInt();
  2130.             ulSourceDuplicate       += pStreamStats->m_pDuplicate->GetInt();
  2131.             ulSourceOutOfOrder      += pStreamStats->m_pOutOfOrder->GetInt();
  2132.             ulSourceLost            += pStreamStats->m_pLost->GetInt();
  2133.             ulSourceLate            += pStreamStats->m_pLate->GetInt();
  2134.             ulSourceResendRequested += pStreamStats->m_pResendRequested->GetInt();
  2135.             ulSourceResendReceived  += pStreamStats->m_pResendReceived->GetInt();
  2136.             if (m_ulLossHack > 0 && pStreamInfo->m_ulLost > 0)
  2137.             {
  2138.                 ulSourceReceived    -= pStreamInfo->m_ulLost;
  2139.                 ulSourceNormal      -= pStreamInfo->m_ulLost;
  2140.                 ulSourceLost        += pStreamInfo->m_ulLost;
  2141.                 pStreamStats->m_pReceived->SetInt((INT32) ulSourceReceived);
  2142.                 pStreamStats->m_pNormal->SetInt((INT32) ulSourceNormal);
  2143.                 pStreamStats->m_pLost->SetInt((INT32) ulSourceLost);
  2144.             }
  2145.             ulSourceTotal30         += pStreamStats->m_pTotal30->GetInt();
  2146.             ulSourceLost30          += pStreamStats->m_pLost30->GetInt();
  2147.             ulSourceAvgBandwidth    += pStreamStats->m_pAvgBandwidth->GetInt();
  2148.             ulSourceCurBandwidth    += pStreamStats->m_pCurBandwidth->GetInt();
  2149.             ulSourceBandwidth       += pStreamStats->m_pClipBandwidth->GetInt();
  2150.             lAvgLatency             += pStreamStats->m_pAvgLatency->GetInt();
  2151.             if (lHighLatency < pStreamStats->m_pHighLatency->GetInt())
  2152.             {
  2153.                 lHighLatency = pStreamStats->m_pHighLatency->GetInt();
  2154.             }
  2155.             if (lLowLatency > pStreamStats->m_pLowLatency->GetInt())
  2156.             {
  2157.                 lLowLatency = pStreamStats->m_pLowLatency->GetInt();
  2158.             }
  2159.         }
  2160.     }
  2161.     if (m_bSourceEnd)
  2162.     {
  2163.         ulSourceCurBandwidth = 0;
  2164.         ulSourceAvgBandwidth = 0;
  2165.     }
  2166.     if (m_pStats->m_pNormal)            m_pStats->m_pNormal->SetInt((INT32)ulSourceNormal);
  2167.     if (m_pStats->m_pRecovered)         m_pStats->m_pRecovered->SetInt((INT32)ulSourceRecovered);
  2168.     if (m_pStats->m_pDuplicate)         m_pStats->m_pDuplicate->SetInt((INT32)ulSourceDuplicate);
  2169.     if (m_pStats->m_pOutOfOrder)        m_pStats->m_pOutOfOrder->SetInt((INT32)ulSourceOutOfOrder);
  2170.     if (m_pStats->m_pReceived)          m_pStats->m_pReceived->SetInt((INT32)ulSourceReceived);
  2171.     if (m_pStats->m_pLost)              m_pStats->m_pLost->SetInt((INT32)ulSourceLost);
  2172.     if (m_pStats->m_pLate)              m_pStats->m_pLate->SetInt((INT32)ulSourceLate);
  2173.     if (m_pStats->m_pTotal)             m_pStats->m_pTotal->SetInt((INT32)ulSourceTotal);
  2174.     if (m_pStats->m_pTotal30)           m_pStats->m_pTotal30->SetInt((INT32)ulSourceTotal30);
  2175.     if (m_pStats->m_pLost30)            m_pStats->m_pLost30->SetInt((INT32)ulSourceLost30);
  2176.     if (m_pStats->m_pResendRequested)   m_pStats->m_pResendRequested->SetInt((INT32)ulSourceResendRequested);
  2177.     if (m_pStats->m_pResendReceived)    m_pStats->m_pResendReceived->SetInt((INT32)ulSourceResendReceived);
  2178.     if (m_pStats->m_pClipBandwidth)     m_pStats->m_pClipBandwidth->SetInt((INT32)ulSourceBandwidth);
  2179.     if (m_pStats->m_pCurBandwidth)      m_pStats->m_pCurBandwidth->SetInt((INT32)ulSourceCurBandwidth);
  2180.     if (m_pStats->m_pAvgBandwidth)      m_pStats->m_pAvgBandwidth->SetInt((INT32)ulSourceAvgBandwidth);
  2181.     if (m_pStats->m_pAvgLatency)        m_pStats->m_pAvgLatency->SetInt((INT32)lAvgLatency);
  2182.     if (m_pStats->m_pHighLatency)       m_pStats->m_pHighLatency->SetInt((INT32)lHighLatency);
  2183.     if (m_pStats->m_pLowLatency)        m_pStats->m_pLowLatency->SetInt((INT32)lLowLatency);
  2184.     // XXXHP: we should only update these info. once
  2185.     // update transport mode
  2186.     switch (m_CurrentTransport)
  2187.     {
  2188.     case UnknownMode:
  2189.         m_pStats->m_pTransportMode->SetStr("Unknown");
  2190.         break;
  2191.     case MulticastMode:
  2192.         if (helixSDPProtocol == m_uProtocolType)
  2193.         {
  2194.             m_pStats->m_pTransportMode->SetStr("Scalable Multicast");
  2195.         }
  2196.         else
  2197.         {
  2198.             m_pStats->m_pTransportMode->SetStr("Multicast");
  2199.         }
  2200.         break;
  2201.     case UDPMode:
  2202.         m_pStats->m_pTransportMode->SetStr("UDP");
  2203.         break;
  2204.     case TCPMode:
  2205.         m_pStats->m_pTransportMode->SetStr("TCP");
  2206.         break;
  2207.     case HTTPCloakMode:
  2208.         if (m_bRTSPProtocol)
  2209.         {
  2210.             m_pStats->m_pTransportMode->SetStr("RTSPvHTTP");
  2211.         }
  2212.         else
  2213.         {
  2214.             m_pStats->m_pTransportMode->SetStr("PNAvHTTP");
  2215.         }
  2216.         break;
  2217.     default:
  2218.         break;
  2219.     }
  2220.     // update buffering mode(local machine)
  2221.     if (m_pStats->m_pBufferingMode)
  2222.     {
  2223.         if (m_bForcePerfectPlay && m_bPerfectPlay)
  2224.         {
  2225.             if (m_bCannotBufferEntireClip)
  2226.             {
  2227.                 m_pStats->m_pBufferingMode->SetInt(BUFFERED_PLAY_NOT_ENTIRE_CLIP);
  2228.             }
  2229.             else
  2230.             {
  2231.                 m_pStats->m_pBufferingMode->SetInt(BUFFERED_PLAY);
  2232.             }
  2233.         }
  2234.         else if (m_bPerfectPlay)
  2235.         {
  2236.             if (m_bCannotBufferEntireClip)
  2237.             {
  2238.                 m_pStats->m_pBufferingMode->SetInt(PERFECT_PLAY_NOT_ENTIRE_CLIP);
  2239.             }
  2240.             else
  2241.             {
  2242.                 m_pStats->m_pBufferingMode->SetInt(PERFECT_PLAY);
  2243.             }
  2244.         }
  2245.         else if (m_bBufferedPlay)
  2246.         {
  2247.             m_pStats->m_pBufferingMode->SetInt(BUFFERED_PLAY);
  2248.         }
  2249.         else
  2250.         {
  2251.             m_pStats->m_pBufferingMode->SetInt(NORMAL_PLAY);
  2252.         }
  2253.     }
  2254. #endif /* HELIX_FEATURE_STATS && HELIX_FEATURE_REGISTRY */
  2255.     return HXR_OK;
  2256. }
  2257. HX_RESULT
  2258. HXNetSource::_ProcessIdle(BOOL atInterrupt)
  2259. {
  2260.     HX_RESULT   theErr = HXR_OK;
  2261.     UINT32      ulAlert = 0;
  2262.     const char* pszAlert = NULL;
  2263.     if (m_bLocked)
  2264.     {
  2265.         return HXR_OK;
  2266.     }
  2267.     theErr = _ProcessIdleExt(atInterrupt);
  2268.     if (HXR_ABORT == theErr)
  2269.     {
  2270.         return HXR_OK;
  2271.     }
  2272.     m_bLocked   = TRUE;
  2273.     if (m_bRedirectPending && !m_bPartOfNextGroup)
  2274.     {
  2275.         m_bRedirectPending = FALSE;
  2276.         HX_ASSERT(m_pszRedirectURL);
  2277. theErr = m_pSourceInfo->HandleRedirectRequest(m_pszRedirectURL);
  2278.         HX_VECTOR_DELETE(m_pszRedirectURL);
  2279.         
  2280.         goto exit;
  2281.     }
  2282.     switch (m_state)
  2283.     {
  2284.     case NETSRC_PACREADY:
  2285.     case NETSRC_TRANSPORTREADY:
  2286.         m_state = NETSRC_READY;
  2287.         theErr = FinishSetup();
  2288.         goto exit;
  2289.         break;
  2290.     case NETSRC_PACPENDING:
  2291.     case NETSRC_TRANSPORTPENDING:
  2292.         goto exit;
  2293.         break;
  2294.     default:
  2295.         break;
  2296.     }
  2297.     if (!m_pProto)
  2298.     {
  2299.         goto exit;
  2300.     }
  2301. #ifdef _MACINTOSH
  2302.     if (m_bBruteForceConnectToBeDone)
  2303.     {
  2304.         BOOL bAtInterrupt = FALSE;
  2305.         IHXInterruptState* pInterruptState = NULL;
  2306.         if (m_pEngine &&
  2307.             m_pEngine->QueryInterface(IID_IHXInterruptState, (void**) &pInterruptState) == HXR_OK)
  2308.         {
  2309.             bAtInterrupt = pInterruptState->AtInterruptTime();
  2310.             HX_RELEASE(pInterruptState);
  2311.         }
  2312.         if (!bAtInterrupt)
  2313.         {
  2314.             m_bBruteForceConnectToBeDone = FALSE;
  2315.             theErr = handleTransportSwitch();
  2316.         }
  2317.         goto exit;
  2318.     }
  2319. #endif
  2320.     if (m_bBruteForceReconnected && m_bInitialized)
  2321.     {
  2322.         /* If user has not called DoResume yet, do not call it here */
  2323.         if (m_bUserHasCalledResume)
  2324.         {
  2325.             if (CanBeResumed())
  2326.             {
  2327.                 if (m_pSourceInfo)
  2328.                 {
  2329.                     m_pSourceInfo->Register();
  2330.                 }
  2331.                 if (m_pPlayer)
  2332.                 {
  2333.                     m_pPlayer->RegisterSourcesDone();
  2334.                 }
  2335.                 DoResume();
  2336.             }
  2337.         }
  2338.         else if (m_bUserHasCalledStartInit)
  2339.         {
  2340.             StartInitialization();
  2341.         }
  2342.         m_bBruteForceReconnected = FALSE;
  2343.     }
  2344.     if (atInterrupt)
  2345.     {
  2346.         m_bAtInterrupt = TRUE;
  2347.         theErr = m_pProto->process_idle(TRUE);
  2348.         m_bAtInterrupt = FALSE;
  2349.     }
  2350.     else
  2351.     {
  2352.         BOOL bAlreadySwitchedPorts = FALSE;
  2353.         theErr = m_pProto->process_idle(FALSE);
  2354.         if (!theErr && mLastError)
  2355.         {
  2356.             theErr = mLastError;
  2357.         }
  2358.         if (m_bInitialized &&
  2359.             m_bPerfectPlay &&
  2360.             !m_bPerfectPlayErrorChecked)
  2361.         {
  2362.             m_bPerfectPlayErrorChecked = TRUE;
  2363.             if(!m_bServerHasPerfectPlay)
  2364.             {
  2365.                 theErr = HXR_PERFECTPLAY_NOT_SUPPORTED;
  2366.             }
  2367.             else
  2368.             {
  2369.                 // Check for attempt to use perfect play on live stream...
  2370.                 if (mLiveStream)
  2371.                 {
  2372.                     theErr = HXR_NO_LIVE_PERFECTPLAY;
  2373.                 }
  2374.                 // we will not allow perfect play if perfect play bit is NOT set.
  2375.                 if(!m_bPerfectPlayAllowed)
  2376.                 {
  2377.                     theErr = HXR_PERFECTPLAY_NOT_ALLOWED;
  2378.                 }
  2379.             }
  2380.         }
  2381.         if (theErr == HXR_UNEXPECTED_STREAM_END)
  2382.         {
  2383.             theErr = AttemptReconnect();
  2384.             mLastError = theErr;
  2385.         }
  2386.         // handle HXR_REDIRECTION explicitly
  2387.         if (theErr == HXR_REDIRECTION)
  2388.         {
  2389.             // determine whether it's too late to do redirect within the same
  2390.             // source - any redirect received before SetupResponse received is OK
  2391.             // since we haven't initialized all the renderers yet
  2392.             //
  2393.             // we used to decide upon m_bReceivedHeader which is set within
  2394.             // HandleDescribeResponse, we now enlarge the window a bit by
  2395.             // deciding upon m_bInitialized which is set within HandleSetupResponse
  2396.             if (!m_bInitialized)
  2397.             {
  2398.                 // redirect at source level
  2399.                 // we don't need to tear down the source since we
  2400.                 // haven't receive any headers yet
  2401.                 m_CurrentTransport = UnknownMode;
  2402.                 mLastError = HXR_OK;
  2403.                 DeleteStreamTable();
  2404.                 theErr = handleRedirect();
  2405.             }
  2406.             else if (m_pSourceInfo && m_pszRedirectURL)
  2407.             {
  2408.                 if (m_bPartOfNextGroup)
  2409.                 {
  2410.                     m_bRedirectPending = TRUE;
  2411.                 }
  2412.                 else
  2413.                 {
  2414.                     // too late to do the Redirect at source level
  2415.                     // let the player to handle it
  2416.                     theErr = m_pSourceInfo->HandleRedirectRequest(m_pszRedirectURL);
  2417.                 }
  2418.             }
  2419.         }
  2420.         else if (!m_bReceivedData && theErr)
  2421.         {
  2422.             // NOTE: Handle automatic transport switching here...
  2423.             switch(theErr)
  2424.             {
  2425.             case HXR_NET_CONNECT:
  2426.             case HXR_SERVER_DISCONNECTED:
  2427.                 if (HTTPCloakMode == m_CurrentTransport)
  2428.                 {
  2429.                     theErr = handleProxySwitch(theErr);
  2430.                 }
  2431.                 if (HXR_OK != theErr)
  2432.                 {
  2433.                     theErr = switch_to_next_transport(theErr);
  2434.                 }
  2435.                 break;
  2436.             case HXR_MULTICAST_UDP:
  2437.             case HXR_NET_UDP:
  2438.             case HXR_NET_TCP:
  2439.             case HXR_BAD_TRANSPORT:
  2440.                 theErr = switch_to_next_transport(theErr);
  2441.                 break;
  2442.             case HXR_PERFECTPLAY_NOT_SUPPORTED:
  2443.             case HXR_NO_LIVE_PERFECTPLAY:
  2444.             case HXR_PERFECTPLAY_NOT_ALLOWED:
  2445.                 theErr = switch_out_of_perfectplay();
  2446.                 break;
  2447.             case HXR_FORCE_PERFECTPLAY:
  2448.                 // if we have connected to an old server and we wanted PerfectPlay, we
  2449.                 // we need to reconnect to the server using the Protocol 8 PerfectPlay
  2450.                 // mechanism to ensure that we get the data via UDP
  2451.                 m_bPerfectPlay      = TRUE;
  2452.                 /* If the server is forcing us into PerfectPlay, it means
  2453.                  * it does allow it AND the clip is perfectPlay-able.
  2454.                  */
  2455.                 m_bPerfectPlayAllowed   = TRUE;
  2456.                 m_bServerHasPerfectPlay = TRUE;
  2457.                 WritePerfectPlayToRegistry();
  2458.                 theErr = switch_to_next_transport(HXR_OK);
  2459.                 break;
  2460.             default:
  2461.                 break;
  2462.             };
  2463.             if (!m_bRTSPProtocol                            &&
  2464.                 HTTPCloakMode == m_CurrentTransport         &&
  2465.                 m_pCloakPortList                            &&
  2466.                 m_nCurrPortIdx < m_nNumberOfCloakPorts-1    &&
  2467.                 (HXR_NET_CONNECT == theErr                  ||
  2468.                  HXR_DNR == theErr                          ||
  2469.                  HXR_SERVER_DISCONNECTED == theErr          ||
  2470.                  HXR_DOC_MISSING == theErr                  ||
  2471.                  HXR_BAD_SERVER == theErr                   ||
  2472.                  HXR_PROXY_NET_CONNECT == theErr))
  2473.             {
  2474.                 m_PreferredTransport = HTTPCloakMode;
  2475.                 m_uCurrCloakedPort = m_pCloakPortList[++m_nCurrPortIdx];
  2476.                 theErr = handleTransportSwitch();
  2477.                 mLastError = theErr;
  2478.             }
  2479.         }
  2480.     }
  2481.     if (theErr)
  2482.     {
  2483.         if (theErr == HXR_NET_CONNECT && m_bUseProxy)
  2484.         {
  2485.             mLastError = HXR_PROXY_NET_CONNECT;
  2486.         }
  2487.         else if (theErr == HXR_NET_CONNECT && HTTPCloakMode == m_CurrentTransport)
  2488.         {
  2489.             // we mask to HTTP connection error only if we are in HTTP only mode
  2490.             if (m_ulTransportPrefMask == ATTEMPT_HTTPCLOAK)
  2491.             {
  2492.                 mLastError = HXR_HTTP_CONNECT;
  2493.             }
  2494.             else
  2495.             {
  2496.                 mLastError = theErr;
  2497.             }
  2498.         }
  2499.         else if (theErr == HXR_DNR && m_bUseProxy)
  2500.         {
  2501.             mLastError = HXR_PROXY_DNR;
  2502.         }
  2503.         else
  2504.         {
  2505.             mLastError = theErr;
  2506.         }
  2507.     }
  2508.     if (m_bReceivedData && HXR_OK != mLastError)
  2509.     {
  2510.         if (m_state == NETSRC_RECONNECTSTARTED)
  2511.         {
  2512.             DEBUG_OUT(m_pPlayer, DOL_TRANSPORT, (s, "(%p)Reconnect Failed", this));
  2513.         }
  2514.         // we are reconnecting now
  2515.         else if (m_state == NETSRC_RECONNECTPENDING)
  2516.         {
  2517.             mLastError = theErr = HXR_OK;
  2518.         }
  2519.         // issue reconnect on the following network errors
  2520.         else
  2521.         {
  2522.             if (HXR_SERVER_DISCONNECTED == mLastError   ||
  2523.                 HXR_SERVER_TIMEOUT == mLastError        ||
  2524.                 HXR_NET_SOCKET_INVALID == mLastError    ||
  2525.                 HXR_GENERAL_NONET == mLastError         ||
  2526.                 IS_SERVER_ALERT(mLastError))
  2527.             {
  2528.                 HX_ASSERT(m_state == NETSRC_READY);
  2529.                 if (m_bSourceEnd)
  2530.                 {
  2531.                     mLastError = theErr = HXR_OK;
  2532.                 }
  2533.                 else if (IsNetworkAvailable() && m_bAttemptReconnect)
  2534.                 {
  2535.                     if (IS_SERVER_ALERT(mLastError))
  2536.                     {
  2537.                         pszAlert = m_pProto->GetLastAlertInfo(ulAlert);
  2538.                         if (PE_PROXY_ORIGIN_DISCONNECTED == ulAlert)
  2539.                         {
  2540.                             theErr = AttemptReconnect();
  2541.                             mLastError = theErr;
  2542.                         }
  2543.                     }
  2544.                     else
  2545.                     {
  2546.                         theErr = AttemptReconnect();
  2547.                         mLastError = theErr;
  2548.                     }
  2549.                 }
  2550.             }
  2551.             else
  2552.             {
  2553.                 // hummm, what error is this??
  2554.                 HX_ASSERT(FALSE);
  2555.             }
  2556.         }
  2557.     }
  2558.     /* tell the player about the error...
  2559.      * This is crucial...
  2560.      */
  2561.     if (m_pPlayer && mLastError)
  2562.     {
  2563. #if defined(HELIX_FEATURE_SMARTERNETWORK)
  2564.         if (m_pPreferredTransport)
  2565.         {
  2566.             m_pPreferredTransport->RemoveTransport();
  2567.             m_pPreferredTransport->RemoveTransportSink(this);
  2568.             HX_RELEASE(m_pPreferredTransport);
  2569.         }
  2570. #endif /* HELIX_FEATURE_SMARTERNETWORK */
  2571.         char* pErrorBuf = NULL;
  2572.         if (IS_SERVER_ALERT(mLastError))
  2573.         {
  2574.             pszAlert = m_pProto->GetLastAlertInfo(ulAlert);
  2575.             if(pszAlert)
  2576.             {
  2577.                 int lenBuf = strlen(pszAlert) + strlen(m_pszURL) + 5;
  2578.                 pErrorBuf = new char[lenBuf];
  2579.                 SafeSprintf(pErrorBuf, lenBuf, "%srn%s", pszAlert, m_pszURL); /* Flawfinder: ignore */
  2580.             }
  2581.         }
  2582.         if (!pErrorBuf && helixSDPProtocol != m_uProtocolType)
  2583.         {
  2584.             pErrorBuf = new char[strlen(m_pszURL) + 1];
  2585.             strcpy(pErrorBuf, m_pszURL); /* Flawfinder: ignore */
  2586.         }
  2587.         m_pPlayer->ReportError(this, mLastError, pErrorBuf);
  2588.         delete[] pErrorBuf;
  2589.     }
  2590.     if (!theErr && m_bInitialized)
  2591.     {
  2592.         UINT32 ulCurrentTime = m_pPlayer->GetInternalCurrentPlayTime();
  2593.         // set m_bDelayed to FALSE as soon as it's due to begin playback
  2594.         // even it's still in pre-fetch mode
  2595.         if (!m_bPaused && m_bDelayed)
  2596.         {
  2597.             UINT32 ulStartTime = 0;
  2598.             if (m_ulDelay > m_ulPreRollInMs + NETWORK_FUDGE_FACTOR)
  2599.             {
  2600.                 ulStartTime = m_ulDelay - (m_ulPreRollInMs + NETWORK_FUDGE_FACTOR);
  2601.             }
  2602.             if (ulCurrentTime >= ulStartTime)
  2603.             {
  2604.                 m_bDelayed = FALSE;
  2605.             }
  2606.         }
  2607. #if defined(HELIX_FEATURE_PREFETCH)
  2608.         // verify the status of prefetch
  2609.         if (m_bPrefetch)
  2610.         {
  2611.             BOOL bPrefetchDone = IsPrefetchEnded();
  2612.             // stop prefetch when either the prefetch has been done or
  2613.             // the active source itself started playback
  2614.             if (bPrefetchDone ||
  2615.                 (!m_bDelayed && !m_bPartOfPrefetchGroup))
  2616.             {
  2617.                 LeavePrefetch();
  2618.                 // pause delayed source
  2619.                 if (bPrefetchDone && m_bDelayed)
  2620.                 {
  2621.                     DoPause();
  2622.                 }
  2623.             }
  2624.         }
  2625. #endif /* HELIX_FEATURE_PREFETCH */
  2626.         if  (!m_bSourceEnd)
  2627.         {
  2628.             /* Get Current buffering status every 1 sec. */
  2629.             UINT32 ulCurrentSystemTime = HX_GET_TICKCOUNT();
  2630.             if (CALCULATE_ELAPSED_TICKS(m_ulLastBufferingCalcTime,
  2631.                                         ulCurrentSystemTime) > 1000)
  2632.             {
  2633.                 m_ulLastBufferingCalcTime = ulCurrentSystemTime;
  2634.                 CalculateCurrentBuffering();
  2635.             }
  2636.         }
  2637.         // rebuffer if the source hasn't satisfy its initial preroll when it's
  2638.         // time to start
  2639.         // resume in InitialBufferingDone()
  2640.         if (IsPlaying()             &&
  2641.             !m_bPartOfPrefetchGroup &&
  2642.             m_bInitialBuffering     &&
  2643.             !m_bRebufferingRequired &&
  2644.             (ulCurrentTime + MIN_BUFFERTIME_BEFORE_DELAY) >= m_ulDelay)
  2645.         {
  2646.             DoRebuffer();
  2647.         }
  2648.     }
  2649. exit:
  2650.     if (!theErr && !m_bIsActive && !m_bDelayed && m_pPlayer &&
  2651.         m_bInitialized && m_pPlayer->GetInternalCurrentPlayTime() >= m_ulDelay)
  2652.     {
  2653.         AdjustClipBandwidthStats(TRUE);
  2654.     }
  2655.     m_bLocked = FALSE;
  2656.     return(theErr);
  2657. }
  2658. HX_RESULT
  2659. HXNetSource::_ProcessIdleExt(BOOL atInterrupt)
  2660. {
  2661.     return HXR_OK;
  2662. }
  2663. // set the various parameters that are used by all the protocols
  2664. HX_RESULT
  2665. HXNetSource::InitializeProtocol()
  2666. {
  2667.     HX_TRACE("HXNetSource::InitializeProtocol");
  2668.     HX_RESULT       theErr = HXR_OK;