Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

195 lines
7.9 KiB

  1. //=========== Copyright Valve Corporation, All rights reserved. ===============//
  2. //
  3. // Purpose: Panorama specific video player code
  4. //=============================================================================//
  5. #ifndef PANORAMA_VIDEO_PLAYER_H
  6. #define PANORAMA_VIDEO_PLAYER_H
  7. #ifdef _WIN32
  8. #pragma once
  9. #endif
  10. #include "tier1/smartptr.h"
  11. #include "../common/video/ivideoplayer.h"
  12. #include "panorama/controls/panelptr.h"
  13. namespace panorama
  14. {
  15. class IUIRenderEngine;
  16. class IUIDoubleBufferedYUV420Texture;
  17. class CPanoramaVideoPlayer;
  18. //-----------------------------------------------------------------------------
  19. // Purpose: Renders video frames from video player for tenfoot
  20. //-----------------------------------------------------------------------------
  21. class CVideoPlayerVideoRenderer : public IVideoPlayerVideoCallback
  22. {
  23. public:
  24. CVideoPlayerVideoRenderer( IUIRenderEngine *pSurface );
  25. virtual ~CVideoPlayerVideoRenderer();
  26. uint32 GetTextureID() { return m_unTextureID; }
  27. uint32 GetTextureWidth();
  28. uint32 GetTextureHeight();
  29. // IVideoPlayerVideoCallback
  30. virtual bool BPresentYUV420Texture( uint nWidth, uint nHeight, void *pPlaneY, void *pPlaneU, void *pPlaneV, uint unStrideY, uint unStrideU, uint unStrideV ) OVERRIDE;
  31. #ifdef DBGFLAG_VALIDATE
  32. void Validate( CValidator &validator, const char *pchName );
  33. #endif
  34. private:
  35. CInterlockedUInt m_unTextureID;
  36. // used by video threads
  37. IUIRenderEngine *m_pSurface;
  38. IUIDoubleBufferedYUV420Texture *m_pYUV420DoubleBufferedTexture;
  39. };
  40. //-----------------------------------------------------------------------------
  41. // Purpose: Renders audio frames from video player for tenfoot
  42. //-----------------------------------------------------------------------------
  43. class CVideoPlayerAudioRenderer : public IVideoPlayerAudioCallback
  44. {
  45. public:
  46. CVideoPlayerAudioRenderer();
  47. virtual ~CVideoPlayerAudioRenderer();
  48. void SetPlaybackVolume( float flVolume );
  49. void MarkShuttingDown();
  50. void Shutdown();
  51. // IVideoPlayerAudioCallback
  52. virtual bool InitAudioOutput( int nSampleRate, int nChannels ) OVERRIDE;
  53. virtual void FreeAudioOutput() OVERRIDE;
  54. virtual bool IsReadyForAudioData() OVERRIDE;
  55. virtual void *GetAudioBuffer() OVERRIDE;
  56. virtual uint32 GetAudioBufferSize() OVERRIDE;
  57. virtual uint32 GetAudioBufferMinSize() OVERRIDE;
  58. virtual void CommitAudioBuffer( uint32 unBytes ) OVERRIDE;
  59. virtual uint32 GetRemainingCommittedAudio() OVERRIDE;
  60. virtual uint32 GetMixedMilliseconds() OVERRIDE;
  61. virtual uint32 GetPlaybackLatency() OVERRIDE;
  62. virtual void Pause() OVERRIDE;
  63. virtual void Resume() OVERRIDE;
  64. #ifdef DBGFLAG_VALIDATE
  65. void Validate( CValidator &validator, const char *pchName );
  66. #endif
  67. private:
  68. bool OnInitAudioMainThread( CVideoPlayerAudioRenderer *pThis, int nSampleRate, int nChannels );
  69. bool OnFreeAudioMainThread( CVideoPlayerAudioRenderer *pThis );
  70. CInterlockedInt m_bShuttingDown;
  71. CThreadEvent m_eventWait;
  72. #ifdef SUPPORTS_AUDIO
  73. IAudioOutputStream *m_pAudioStream;
  74. #endif
  75. float m_flVolume;
  76. };
  77. //-----------------------------------------------------------------------------
  78. // Purpose: Dispatches events to main panorama thread
  79. //-----------------------------------------------------------------------------
  80. typedef CUtlDelegate< void ( EVideoPlayerEvent ) > VideoPlayerEventDelegate_t;
  81. class CVideoPlayerEventDispatcher : public IVideoPlayerEventCallback
  82. {
  83. public:
  84. CVideoPlayerEventDispatcher( CPanoramaVideoPlayer *pPlayer );
  85. ~CVideoPlayerEventDispatcher();
  86. void RegisterEventListener( IUIPanel *pPanel );
  87. void UnregisterEventListener( IUIPanel *pPanel );
  88. void RegisterEventCallback( VideoPlayerEventDelegate_t del );
  89. void UnregisterEventCallback( VideoPlayerEventDelegate_t del );
  90. bool VideoPlayerEventUIThread( CVideoPlayerEventDispatcher *pDispatcher, EVideoPlayerEvent eEvent );
  91. // IVideoPlayerEventCallback
  92. virtual void VideoPlayerEvent( EVideoPlayerEvent eEvent ) OVERRIDE;
  93. #ifdef DBGFLAG_VALIDATE
  94. void Validate( CValidator &validator, const char *pchName );
  95. #endif
  96. private:
  97. void DispatchVideoEvent( EVideoPlayerEvent eEvent, IUIPanel *pTarget );
  98. CPanoramaVideoPlayer *m_pPlayer;
  99. CUtlVector< CPanelPtr< IUIPanel > > m_vecListeners;
  100. CUtlVector< VideoPlayerEventDelegate_t > m_vecCallbacks;
  101. double m_flLastRepeatEventDispatch;
  102. };
  103. //-----------------------------------------------------------------------------
  104. // Purpose: Helper to create a tenfoot video player
  105. //-----------------------------------------------------------------------------
  106. class CPanoramaVideoPlayer : public IVideoPlayer, public ::CRefCount
  107. {
  108. public:
  109. CPanoramaVideoPlayer( IUIPanel *pPanel );
  110. CPanoramaVideoPlayer( IUIRenderEngine *pSurface );
  111. virtual ~CPanoramaVideoPlayer();
  112. virtual uint32 GetTextureID() { return m_videoCallback.GetTextureID(); }
  113. uint32 GetTextureWidth() { return m_videoCallback.GetTextureWidth(); }
  114. uint32 GetTextureHeight() { return m_videoCallback.GetTextureHeight(); }
  115. void RegisterEventListener( IUIPanel *pPanel ) { m_eventCallback.RegisterEventListener( pPanel ); }
  116. void UnregisterEventListener( IUIPanel *pPanel ) { m_eventCallback.UnregisterEventListener( pPanel ); }
  117. void RegisterEventCallback( VideoPlayerEventDelegate_t del ) { m_eventCallback.RegisterEventCallback( del ); }
  118. void UnregisterEventCallback( VideoPlayerEventDelegate_t del ) { m_eventCallback.UnregisterEventCallback( del ); }
  119. void SetPlaybackVolume( float flVolume ) { m_audioCallback.SetPlaybackVolume( flVolume ); }
  120. // IVideoPlayer
  121. virtual bool BLoad( const char *pchURL ) OVERRIDE;
  122. virtual bool BLoad( const byte *pubData, uint cubData ) OVERRIDE;
  123. virtual void Play() OVERRIDE { m_pVideoPlayer->Play(); }
  124. virtual void Stop() OVERRIDE;
  125. virtual void Pause() OVERRIDE { m_pVideoPlayer->Pause(); }
  126. virtual void SetPlaybackSpeed( float flPlaybackSpeed ) OVERRIDE { m_pVideoPlayer->SetPlaybackSpeed( flPlaybackSpeed ); }
  127. virtual void Seek( uint unSeekMS ) OVERRIDE { m_pVideoPlayer->Seek( unSeekMS ); }
  128. virtual void SetRepeat( bool bRepeat ) OVERRIDE { m_pVideoPlayer->SetRepeat( bRepeat ); }
  129. virtual void SuggestMaxVeritcalResolution( int nHeight ) OVERRIDE { m_pVideoPlayer->SuggestMaxVeritcalResolution( nHeight ); }
  130. virtual EVideoPlayerPlaybackState GetPlaybackState() OVERRIDE { return m_pVideoPlayer->GetPlaybackState(); }
  131. virtual bool IsStoppedForBuffering() OVERRIDE { return m_pVideoPlayer->IsStoppedForBuffering(); }
  132. virtual float GetPlaybackSpeed() OVERRIDE { return m_pVideoPlayer->GetPlaybackSpeed(); }
  133. virtual uint32 GetDuration() OVERRIDE { return m_pVideoPlayer->GetDuration(); }
  134. virtual uint32 GetCurrentPlaybackTime() OVERRIDE { return m_pVideoPlayer->GetCurrentPlaybackTime(); }
  135. virtual EVideoPlayerPlaybackError GetPlaybackError() OVERRIDE { return m_pVideoPlayer->GetPlaybackError(); }
  136. virtual void GetVideoResolution( int *pnWidth, int *pnHeight ) OVERRIDE { m_pVideoPlayer->GetVideoResolution( pnWidth, pnHeight ); }
  137. virtual int GetVideoDownloadRate() OVERRIDE { return m_pVideoPlayer->GetVideoDownloadRate(); }
  138. virtual int GetVideoRepresentationCount() OVERRIDE { return m_pVideoPlayer->GetVideoRepresentationCount(); }
  139. virtual bool BGetVideoRepresentationInfo( int iRep, int *pnWidth, int *pnHeight ) OVERRIDE { return m_pVideoPlayer->BGetVideoRepresentationInfo( iRep, pnWidth, pnHeight ); }
  140. virtual int GetCurrentVideoRepresentation() OVERRIDE { return m_pVideoPlayer->GetCurrentVideoRepresentation(); }
  141. virtual void ForceVideoRepresentation( int iRep ) OVERRIDE { return m_pVideoPlayer->ForceVideoRepresentation( iRep ); }
  142. virtual void GetVideoSegmentInfo( int *pnCurrent, int *pnTotal ) OVERRIDE { m_pVideoPlayer->GetVideoSegmentInfo( pnCurrent, pnTotal ); }
  143. virtual bool BHasAudioTrack() OVERRIDE { return m_pVideoPlayer->BHasAudioTrack(); }
  144. #ifdef DBGFLAG_VALIDATE
  145. void Validate( CValidator &validator, const char *pchName );
  146. #endif
  147. private:
  148. IVideoPlayer *m_pVideoPlayer;
  149. CVideoPlayerVideoRenderer m_videoCallback;
  150. CVideoPlayerAudioRenderer m_audioCallback;
  151. CVideoPlayerEventDispatcher m_eventCallback;
  152. };
  153. typedef CSmartPtr< CPanoramaVideoPlayer > CVideoPlayerPtr;
  154. } // namespace panorama
  155. #endif // PANORAMA_VIDEO_PLAYER_H