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

Symbian

开发平台:

C/C++

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