Home | History | Annotate | Download | only in windows
      1 /*
      2  *  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
      3  *
      4  *  Use of this source code is governed by a BSD-style license
      5  *  that can be found in the LICENSE file in the root of the source
      6  *  tree. An additional intellectual property rights grant can be found
      7  *  in the file PATENTS.  All contributing project authors may
      8  *  be found in the AUTHORS file in the root of the source tree.
      9  */
     10 
     11 #ifndef WEBRTC_MODULES_VIDEO_RENDER_MAIN_SOURCE_WINDOWS_VIDEO_RENDER_DIRECT3D9_H_
     12 #define WEBRTC_MODULES_VIDEO_RENDER_MAIN_SOURCE_WINDOWS_VIDEO_RENDER_DIRECT3D9_H_
     13 
     14 // WebRtc includes
     15 #include "webrtc/modules/video_render/windows/i_video_render_win.h"
     16 
     17 #include <d3d9.h>
     18 #include <d3dx9.h>
     19 #include <ddraw.h>
     20 
     21 #include <Map>
     22 
     23 // Added
     24 #include "webrtc/modules/video_render/include/video_render_defines.h"
     25 
     26 #pragma comment(lib, "d3d9.lib")       // located in DirectX SDK
     27 
     28 namespace webrtc {
     29 class CriticalSectionWrapper;
     30 class EventWrapper;
     31 class Trace;
     32 class ThreadWrapper;
     33 
     34 class D3D9Channel: public VideoRenderCallback
     35 {
     36 public:
     37     D3D9Channel(LPDIRECT3DDEVICE9 pd3DDevice,
     38                     CriticalSectionWrapper* critSect, Trace* trace);
     39 
     40     virtual ~D3D9Channel();
     41 
     42     // Inherited from VideoRencerCallback, called from VideoAPI class.
     43     // Called when the incomming frame size and/or number of streams in mix changes
     44     virtual int FrameSizeChange(int width, int height, int numberOfStreams);
     45 
     46     // A new frame is delivered.
     47     virtual int DeliverFrame(const I420VideoFrame& videoFrame);
     48     virtual int32_t RenderFrame(const uint32_t streamId,
     49                                 I420VideoFrame& videoFrame);
     50 
     51     // Called to check if the video frame is updated.
     52     int IsUpdated(bool& isUpdated);
     53     // Called after the video frame has been render to the screen
     54     int RenderOffFrame();
     55     // Called to get the texture that contains the video frame
     56     LPDIRECT3DTEXTURE9 GetTexture();
     57     // Called to get the texture(video frame) size
     58     int GetTextureWidth();
     59     int GetTextureHeight();
     60     //
     61     void SetStreamSettings(uint16_t streamId,
     62                            uint32_t zOrder,
     63                            float startWidth,
     64                            float startHeight,
     65                            float stopWidth,
     66                            float stopHeight);
     67     int GetStreamSettings(uint16_t streamId,
     68                           uint32_t& zOrder,
     69                           float& startWidth,
     70                           float& startHeight,
     71                           float& stopWidth,
     72                           float& stopHeight);
     73 
     74     int ReleaseTexture();
     75     int RecreateTexture(LPDIRECT3DDEVICE9 pd3DDevice);
     76 
     77 protected:
     78 
     79 private:
     80     //critical section passed from the owner
     81     CriticalSectionWrapper* _critSect;
     82     LPDIRECT3DDEVICE9 _pd3dDevice;
     83     LPDIRECT3DTEXTURE9 _pTexture;
     84 
     85     bool _bufferIsUpdated;
     86     // the frame size
     87     int _width;
     88     int _height;
     89     //sream settings
     90     //TODO support multiple streams in one channel
     91     uint16_t _streamId;
     92     uint32_t _zOrder;
     93     float _startWidth;
     94     float _startHeight;
     95     float _stopWidth;
     96     float _stopHeight;
     97 };
     98 
     99 class VideoRenderDirect3D9: IVideoRenderWin
    100 {
    101 public:
    102     VideoRenderDirect3D9(Trace* trace, HWND hWnd, bool fullScreen);
    103     ~VideoRenderDirect3D9();
    104 
    105 public:
    106     //IVideoRenderWin
    107 
    108     /**************************************************************************
    109      *
    110      *   Init
    111      *
    112      ***************************************************************************/
    113     virtual int32_t Init();
    114 
    115     /**************************************************************************
    116      *
    117      *   Incoming Streams
    118      *
    119      ***************************************************************************/
    120     virtual VideoRenderCallback
    121             * CreateChannel(const uint32_t streamId,
    122                             const uint32_t zOrder,
    123                             const float left,
    124                             const float top,
    125                             const float right,
    126                             const float bottom);
    127 
    128     virtual int32_t DeleteChannel(const uint32_t streamId);
    129 
    130     virtual int32_t GetStreamSettings(const uint32_t channel,
    131                                       const uint16_t streamId,
    132                                       uint32_t& zOrder,
    133                                       float& left, float& top,
    134                                       float& right, float& bottom);
    135 
    136     /**************************************************************************
    137      *
    138      *   Start/Stop
    139      *
    140      ***************************************************************************/
    141 
    142     virtual int32_t StartRender();
    143     virtual int32_t StopRender();
    144 
    145     /**************************************************************************
    146      *
    147      *   Properties
    148      *
    149      ***************************************************************************/
    150 
    151     virtual bool IsFullScreen();
    152 
    153     virtual int32_t SetCropping(const uint32_t channel,
    154                                 const uint16_t streamId,
    155                                 const float left, const float top,
    156                                 const float right, const float bottom);
    157 
    158     virtual int32_t ConfigureRenderer(const uint32_t channel,
    159                                       const uint16_t streamId,
    160                                       const unsigned int zOrder,
    161                                       const float left, const float top,
    162                                       const float right, const float bottom);
    163 
    164     virtual int32_t SetTransparentBackground(const bool enable);
    165 
    166     virtual int32_t ChangeWindow(void* window);
    167 
    168     virtual int32_t GetGraphicsMemory(uint64_t& totalMemory,
    169                                       uint64_t& availableMemory);
    170 
    171     virtual int32_t SetText(const uint8_t textId,
    172                             const uint8_t* text,
    173                             const int32_t textLength,
    174                             const uint32_t colorText,
    175                             const uint32_t colorBg,
    176                             const float left, const float top,
    177                             const float rigth, const float bottom);
    178 
    179     virtual int32_t SetBitmap(const void* bitMap,
    180                               const uint8_t pictureId,
    181                               const void* colorKey,
    182                               const float left, const float top,
    183                               const float right, const float bottom);
    184 
    185 public:
    186     // Get a channel by channel id
    187     D3D9Channel* GetD3DChannel(int channel);
    188     int UpdateRenderSurface();
    189 
    190 protected:
    191     // The thread rendering the screen
    192     static bool ScreenUpdateThreadProc(void* obj);
    193     bool ScreenUpdateProcess();
    194 
    195 private:
    196     // Init/close the d3d device
    197     int InitDevice();
    198     int CloseDevice();
    199 
    200     // Transparent related functions
    201     int SetTransparentColor(LPDIRECT3DTEXTURE9 pTexture,
    202                             DDCOLORKEY* transparentColorKey,
    203                             DWORD width,
    204                             DWORD height);
    205 
    206     CriticalSectionWrapper& _refD3DCritsect;
    207     Trace* _trace;
    208     ThreadWrapper* _screenUpdateThread;
    209     EventWrapper* _screenUpdateEvent;
    210 
    211     HWND _hWnd;
    212     bool _fullScreen;
    213     RECT _originalHwndRect;
    214     //FIXME we probably don't need this since all the information can be get from _d3dChannels
    215     int _channel;
    216     //Window size
    217     UINT _winWidth;
    218     UINT _winHeight;
    219 
    220     // Device
    221     LPDIRECT3D9 _pD3D; // Used to create the D3DDevice
    222     LPDIRECT3DDEVICE9 _pd3dDevice; // Our rendering device
    223     LPDIRECT3DVERTEXBUFFER9 _pVB; // Buffer to hold Vertices
    224     LPDIRECT3DTEXTURE9 _pTextureLogo;
    225 
    226     std::map<int, D3D9Channel*> _d3dChannels;
    227     std::multimap<int, unsigned int> _d3dZorder;
    228 
    229     // The position where the logo will be placed
    230     float _logoLeft;
    231     float _logoTop;
    232     float _logoRight;
    233     float _logoBottom;
    234 
    235     typedef HRESULT (WINAPI *DIRECT3DCREATE9EX)(UINT SDKVersion, IDirect3D9Ex**);
    236     LPDIRECT3DSURFACE9 _pd3dSurface;
    237 
    238     DWORD GetVertexProcessingCaps();
    239     int InitializeD3D(HWND hWnd, D3DPRESENT_PARAMETERS* pd3dpp);
    240 
    241     D3DPRESENT_PARAMETERS _d3dpp;
    242     int ResetDevice();
    243 
    244     int UpdateVerticeBuffer(LPDIRECT3DVERTEXBUFFER9 pVB, int offset,
    245                             float startWidth, float startHeight,
    246                             float stopWidth, float stopHeight);
    247 
    248     //code for providing graphics settings
    249     DWORD _totalMemory;
    250     DWORD _availableMemory;
    251 };
    252 
    253 }  // namespace webrtc
    254 
    255 #endif // WEBRTC_MODULES_VIDEO_RENDER_MAIN_SOURCE_WINDOWS_VIDEO_RENDER_DIRECT3D9_H_
    256