Home | History | Annotate | Download | only in mac
      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 #include "webrtc/engine_configurations.h"
     12 
     13 #if defined(CARBON_RENDERING)
     14 
     15 #ifndef WEBRTC_MODULES_VIDEO_RENDER_MAIN_SOURCE_MAC_VIDEO_RENDER_AGL_H_
     16 #define WEBRTC_MODULES_VIDEO_RENDER_MAIN_SOURCE_MAC_VIDEO_RENDER_AGL_H_
     17 
     18 #include "webrtc/modules/video_render/include/video_render_defines.h"
     19 
     20 #define NEW_HIVIEW_PARENT_EVENT_HANDLER 1
     21 #define NEW_HIVIEW_EVENT_HANDLER 1
     22 #define USE_STRUCT_RGN
     23 
     24 #include <AGL/agl.h>
     25 #include <Carbon/Carbon.h>
     26 #include <OpenGL/OpenGL.h>
     27 #include <OpenGL/glext.h>
     28 #include <OpenGL/glu.h>
     29 #include <list>
     30 #include <map>
     31 
     32 class VideoRenderAGL;
     33 
     34 namespace webrtc {
     35 class CriticalSectionWrapper;
     36 class EventWrapper;
     37 class ThreadWrapper;
     38 
     39 class VideoChannelAGL : public VideoRenderCallback {
     40  public:
     41 
     42   VideoChannelAGL(AGLContext& aglContext, int iId, VideoRenderAGL* owner);
     43   virtual ~VideoChannelAGL();
     44   virtual int FrameSizeChange(int width, int height, int numberOfStreams);
     45   virtual int DeliverFrame(const I420VideoFrame& videoFrame);
     46   virtual int UpdateSize(int width, int height);
     47   int SetStreamSettings(int streamId, float startWidth, float startHeight,
     48                         float stopWidth, float stopHeight);
     49   int SetStreamCropSettings(int streamId, float startWidth, float startHeight,
     50                             float stopWidth, float stopHeight);
     51   int RenderOffScreenBuffer();
     52   int IsUpdated(bool& isUpdated);
     53   virtual int UpdateStretchSize(int stretchHeight, int stretchWidth);
     54   virtual int32_t RenderFrame(const uint32_t streamId,
     55                               I420VideoFrame& videoFrame);
     56 
     57  private:
     58 
     59   AGLContext _aglContext;
     60   int _id;
     61   VideoRenderAGL* _owner;
     62   int _width;
     63   int _height;
     64   int _stretchedWidth;
     65   int _stretchedHeight;
     66   float _startHeight;
     67   float _startWidth;
     68   float _stopWidth;
     69   float _stopHeight;
     70   int _xOldWidth;
     71   int _yOldHeight;
     72   int _oldStretchedHeight;
     73   int _oldStretchedWidth;
     74   unsigned char* _buffer;
     75   int _bufferSize;
     76   int _incommingBufferSize;
     77   bool _bufferIsUpdated;
     78   bool _sizeInitialized;
     79   int _numberOfStreams;
     80   bool _bVideoSizeStartedChanging;
     81   GLenum _pixelFormat;
     82   GLenum _pixelDataType;
     83   unsigned int _texture;
     84 };
     85 
     86 class VideoRenderAGL {
     87  public:
     88   VideoRenderAGL(WindowRef windowRef, bool fullscreen, int iId);
     89   VideoRenderAGL(HIViewRef windowRef, bool fullscreen, int iId);
     90   ~VideoRenderAGL();
     91 
     92   int Init();
     93   VideoChannelAGL* CreateAGLChannel(int channel, int zOrder, float startWidth,
     94                                     float startHeight, float stopWidth,
     95                                     float stopHeight);
     96   VideoChannelAGL* ConfigureAGLChannel(int channel, int zOrder,
     97                                        float startWidth, float startHeight,
     98                                        float stopWidth, float stopHeight);
     99   int DeleteAGLChannel(int channel);
    100   int DeleteAllAGLChannels();
    101   int StopThread();
    102   bool IsFullScreen();
    103   bool HasChannels();
    104   bool HasChannel(int channel);
    105   int GetChannels(std::list<int>& channelList);
    106   void LockAGLCntx();
    107   void UnlockAGLCntx();
    108 
    109   static int GetOpenGLVersion(int& aglMajor, int& aglMinor);
    110 
    111   // ********** new module functions ************ //
    112   int ChangeWindow(void* newWindowRef);
    113   int32_t ChangeUniqueID(int32_t id);
    114   int32_t StartRender();
    115   int32_t StopRender();
    116   int32_t DeleteAGLChannel(const uint32_t streamID);
    117   int32_t GetChannelProperties(const uint16_t streamId, uint32_t& zOrder,
    118                                float& left, float& top, float& right,
    119                                float& bottom);
    120 
    121  protected:
    122   static bool ScreenUpdateThreadProc(void* obj);
    123   bool ScreenUpdateProcess();
    124   int GetWindowRect(Rect& rect);
    125 
    126  private:
    127   int CreateMixingContext();
    128   int RenderOffScreenBuffers();
    129   int SwapAndDisplayBuffers();
    130   int UpdateClipping();
    131   int CalculateVisibleRegion(ControlRef control, RgnHandle& visibleRgn,
    132                              bool clipChildren);
    133   bool CheckValidRegion(RgnHandle rHandle);
    134   void ParentWindowResized(WindowRef window);
    135 
    136   // Carbon GUI event handlers
    137   static pascal OSStatus sHandleWindowResized(
    138       EventHandlerCallRef nextHandler, EventRef theEvent, void* userData);
    139   static pascal OSStatus sHandleHiViewResized(
    140       EventHandlerCallRef nextHandler, EventRef theEvent, void* userData);
    141 
    142   HIViewRef _hiviewRef;
    143   WindowRef _windowRef;
    144   bool _fullScreen;
    145   int _id;
    146   webrtc::CriticalSectionWrapper& _renderCritSec;
    147   webrtc::ThreadWrapper* _screenUpdateThread;
    148   webrtc::EventWrapper* _screenUpdateEvent;
    149   bool _isHIViewRef;
    150   AGLContext _aglContext;
    151   int _windowWidth;
    152   int _windowHeight;
    153   int _lastWindowWidth;
    154   int _lastWindowHeight;
    155   int _lastHiViewWidth;
    156   int _lastHiViewHeight;
    157   int _currentParentWindowHeight;
    158   int _currentParentWindowWidth;
    159   Rect _currentParentWindowBounds;
    160   bool _windowHasResized;
    161   Rect _lastParentWindowBounds;
    162   Rect _currentHIViewBounds;
    163   Rect _lastHIViewBounds;
    164   Rect _windowRect;
    165   std::map<int, VideoChannelAGL*> _aglChannels;
    166   std::multimap<int, int> _zOrderToChannel;
    167   EventHandlerRef _hiviewEventHandlerRef;
    168   EventHandlerRef _windowEventHandlerRef;
    169   HIRect _currentViewBounds;
    170   HIRect _lastViewBounds;
    171   bool _renderingIsPaused;
    172   unsigned int _threadID;
    173 
    174 };
    175 
    176 }  // namespace webrtc
    177 
    178 #endif  // WEBRTC_MODULES_VIDEO_RENDER_MAIN_SOURCE_MAC_VIDEO_RENDER_AGL_H_
    179 
    180 #endif  // CARBON_RENDERING
    181