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

Symbian

开发平台:

Visual C++

  1. /************************************************************************
  2.  * chxavplayerstate.cpp
  3.  * --------------------
  4.  *
  5.  * Synopsis:
  6.  * Contains the implementation of the CHXAvPlayerState class. 
  7.  *
  8.  * Target:
  9.  * Symbian OS
  10.  *
  11.  *
  12.  * (c) 1995-2003 RealNetworks, Inc. Patents pending. All rights reserved.
  13.  *
  14.  *****************************************************************************/
  15. /*****************************************************************************
  16.  user command        hxplayer event                       player state dispatch
  17.  -------             --------------                       ---------------------
  18.  Play
  19.                                                           OnPlayInitiate
  20.                      {if non-local url}                   
  21.                                                           OnNetConnect
  22.                       
  23.                      OnBegin
  24.                                                           OnResume
  25.                      (BeginChangeLayout, 
  26.                       SitesNeeded, 
  27.                       DoneChangeLayout for non-smil case)
  28.                      OnPresentationOpened
  29.                                                           OnLoadSession
  30.                      (BeginChangeLayout, 
  31.                       SitesNeeded, 
  32.                       DoneChangeLayout for smil case)
  33.                     {if non-local url smil track added}                   
  34.                                                           OnNetConnect
  35.                      OnBuffering (BUFFERING_START_UP)
  36.                      (local: 100 once
  37.                       network: N - 100; N usually starts at 0)
  38.                                                           OnBeginBuffering (0 and 100)
  39.                                                           OnBuffering
  40.  Stop
  41.                                                           OnStop
  42.                       OnPresentationClosed
  43.                                                           OnStop
  44.  StartSeek
  45.                                                           (Pause)
  46.                                                           OnPause
  47.                       OnPause
  48.                                                           OnPause
  49.                                                           OnBeginSeek
  50.  SetSeekPoint
  51.                                                           OnNewPos
  52.  EndSeek
  53.                                                           (Seek)
  54.                       OnPreSeek (during call to seek)
  55.                                                          (Begin) * pause or stop also possible here
  56.                       OnBegin
  57.                                                           OnResume           
  58.                       OnBuffering (BUFFERING_SEEK)
  59.                                                           OnBuffering
  60.                       OnPostSeek
  61.  
  62. // XXXLCM we really should switch to using state objects (this class is misnamed and
  63. //        is just a poorly-defined helper class)
  64. //     
  65. **********************************************************************************/
  66. #include "hxstring.h"
  67. #include "hxcore.h"
  68. #include "hxsym_debug.h"
  69. #include "hxsym_memutil.h"
  70. #include "chxavplayerstatedispatch.h"
  71. #include "chxavplayerstate.h"
  72. CHXAvPlayerState::CHXAvPlayerState()
  73. : m_state(Stopped)
  74. , m_bufferState(NotBuffering)
  75. , m_pDispatch(0)
  76. , m_lastBufferPercent(100)
  77. , m_lastTime(-1)
  78. {
  79. }
  80. CHXAvPlayerState::~CHXAvPlayerState()
  81. {
  82.     HX_DELETE(m_pDispatch);
  83. }
  84. void CHXAvPlayerState::ConstructL()
  85. {
  86.     HX_ASSERT(m_pDispatch == NULL);
  87.     m_pDispatch = new (ELeave) CHXAvPlayerStateDispatch();
  88. }
  89. void CHXAvPlayerState::ChangeState(State destState)
  90. {
  91.     DPRINTF(SYMP_INFO, ("  *** PLAYER STATE --> %s ***n", dbg::State(destState)));
  92.     m_state = destState;
  93. }
  94. void CHXAvPlayerState::ChangeBufferState(BufferState destState)
  95. {
  96.     m_bufferState = destState;
  97. }
  98. void CHXAvPlayerState::RemoveObserver(IHXSymPlayerStateObserver* observer)
  99. {
  100.     HX_ASSERT(m_pDispatch != NULL);
  101.     m_pDispatch->RemoveObserver(observer);
  102. }
  103. void CHXAvPlayerState::AddObserver(IHXSymPlayerStateObserver* observer)
  104. {
  105.     HX_ASSERT(m_pDispatch != NULL);
  106.     m_pDispatch->AddObserver(observer);
  107. }
  108. // when user play command issued or play auto-starts
  109. void CHXAvPlayerState::OnPlayInitiate(const char* url)
  110. {
  111.     // start in Initiating state
  112.     if(m_state != Initiating)
  113.     {
  114.         ChangeState(Initiating);
  115.         m_pDispatch->OnPlayInitiate(url);
  116.     } 
  117. }
  118. void CHXAvPlayerState::OnNetConnect()
  119. {
  120.     DPRINTF(SYMP_INFO, ("CHXAvNaviPaneControl::OnNetConnect()n"));
  121.     // sometimes we can get a net connect while paused if we pause a smil pres after open but before first buffer
  122.     if(Connecting != m_state && Paused != m_state)
  123.     {
  124.         ChangeState(Connecting);
  125.         m_pDispatch->OnNetConnect();
  126.     }
  127. }
  128. // when open presentation notification received; headers are available and sites ready only for non-smil
  129. void CHXAvPlayerState::OnLoadSession(IHXRequest *request)
  130. {
  131.     DPRINTF(SYMP_INFO, ("OnLoadSession(): Memory: System Free = %ld; Heap Free = %ldn", MemUtil::GetSystemFree(), MemUtil::GetHeapFree()));
  132.     m_lastTime = -1;
  133.     m_pDispatch->OnLoadSession(request);
  134. }
  135. // when error notification received
  136. void CHXAvPlayerState::OnError(HX_RESULT code)
  137. {
  138.     DPRINTF(SYMP_INFO, ("OnError(): System Free = %ld; Heap Free = %ldn", MemUtil::GetSystemFree(), MemUtil::GetHeapFree()));
  139.     m_pDispatch->OnError(code);
  140. }
  141. // when user advances playlist; when playlist auto-advances
  142. void CHXAvPlayerState::OnAdvancePlaylist()
  143. {
  144.     m_pDispatch->OnAdvancePlaylist();
  145. }
  146. // when OnBegin notification or first timer tick received
  147. void CHXAvPlayerState::OnResume()
  148. {
  149.     DPRINTF(SYMP_INFO, ("OnResume(): System Free = %ld; Heap Free = %ldn", MemUtil::GetSystemFree(), MemUtil::GetHeapFree()));
  150.     if(m_state != Playing)
  151.     {
  152.         m_lastBufferPercent = 100;
  153.         ChangeBufferState(NotBuffering); // buffering will come next
  154.         ChangeState(Playing);
  155.         m_pDispatch->OnResume();
  156.     }
  157. }
  158. // when user stop command issued, or OnStop, after presentation is closed
  159. void CHXAvPlayerState::OnStop()
  160. {
  161.     if(m_state != Stopped)
  162.     {
  163.         m_lastBufferPercent = 100;
  164.         ChangeBufferState(NotBuffering); // in case stop while buffering
  165.         ChangeState(Stopped);
  166.         m_pDispatch->OnStop();
  167.     }
  168. }
  169. // when user pause command issued, when user begins seek
  170. void CHXAvPlayerState::OnPause()
  171. {
  172.     if(m_state != Paused)
  173.     {
  174.         ChangeState(Paused);
  175.         m_pDispatch->OnPause();
  176.     }
  177. }
  178. // when buffering occurs; can occur during play, pause (live) and seek state
  179. void CHXAvPlayerState::OnBuffering(ULONG32 type, UINT16 percent)
  180. {
  181.     // if first buffer after connect/initiate ensure we are playing state
  182.     if( m_state  == Initiating || m_state == Connecting )
  183.     {
  184.         OnResume();
  185.     }
  186.     BufferState dest;
  187.     switch(type)
  188.     {
  189.     case BUFFERING_START_UP:
  190.         dest = BufferLoad; // first buffer
  191.         break;
  192.     case BUFFERING_SEEK:
  193.         dest = BufferSeek; // buffer after seek
  194.         break;
  195.     default:
  196.         dest = BufferPlain; // plain ol' buffering
  197.         break;
  198.     }
  199.     bool bIsNewPercentage = (m_lastBufferPercent != percent);
  200.     m_lastBufferPercent = percent;
  201.     if(dest != m_bufferState)
  202.     {
  203.         //
  204.         // begin transition to new buffering state; this also handles seek during
  205.         // initial buffering, i.e., BUFFERING_START_UP to BUFFERING_CONGESTION
  206.         //
  207.         ChangeBufferState(dest);
  208.         DPRINTF(SYMP_INFO, ("CHXAvPlayerState::OnBuffering(): begin bufferingn"));
  209.         m_pDispatch->OnBeginBuffering(true);
  210.     }
  211.     if(bIsNewPercentage)
  212.     {
  213.         //DPRINTF(SYMP_INFO, ("OnBuffering(): Memory: System Free = %ld; Heap Free = %ldn", MemUtil::GetSystemFree(), MemUtil::GetHeapFree()));
  214.         // forward new buffering position
  215.         m_pDispatch->OnBuffering(percent);
  216.     }
  217.     if(100 == percent)
  218.     {
  219.         // buffering complete; transition out of buffering state
  220.         if(NotBuffering != m_bufferState)
  221.         {
  222.             ChangeBufferState(NotBuffering);
  223.             DPRINTF(SYMP_INFO, ("CHXAvPlayerState::OnBuffering(): end bufferingn"));
  224.             m_pDispatch->OnBeginBuffering(false);
  225.         }
  226.     }
  227.     
  228. }
  229. // when user command to begin seek interaction issued
  230. void CHXAvPlayerState::OnBeginSeek()
  231. {
  232.     if(m_state != Seeking)
  233.     {
  234.         ChangeState(Seeking);
  235.         m_pDispatch->OnBeginSeek();
  236.     }
  237. }
  238. // when user sets new position via some ui control or playback advances to new position in timeline
  239. void CHXAvPlayerState::OnNewPos(ULONG32 time)
  240. {
  241.     if( time != m_lastTime)
  242.     {
  243.         m_lastTime = time;
  244.         m_pDispatch->OnNewPos(time);
  245.     }
  246. }
  247. // when volume command issued
  248. void CHXAvPlayerState::OnVolume(unsigned int percentVol)
  249. {
  250.     m_pDispatch->OnVolume(percentVol);
  251. }
  252. // when mute command issued
  253. void CHXAvPlayerState::OnMute(bool bMute)
  254. {
  255.     m_pDispatch->OnMute(bMute);
  256. }