Home | History | Annotate | Download | only in mac
      1 /*
      2  *  Copyright (c) 2012 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 #if defined(COCOA_RENDERING)
     13 
     14 #ifndef WEBRTC_MODULES_VIDEO_RENDER_MAIN_SOURCE_MAC_VIDEO_RENDER_NSOPENGL_H_
     15 #define WEBRTC_MODULES_VIDEO_RENDER_MAIN_SOURCE_MAC_VIDEO_RENDER_NSOPENGL_H_
     16 
     17 #import <Cocoa/Cocoa.h>
     18 #import <OpenGL/OpenGL.h>
     19 #import <OpenGL/glext.h>
     20 #import <OpenGL/glu.h>
     21 #include <QuickTime/QuickTime.h>
     22 #include <list>
     23 #include <map>
     24 
     25 #include "webrtc/modules/video_render/include/video_render_defines.h"
     26 
     27 #import "webrtc/modules/video_render/mac/cocoa_full_screen_window.h"
     28 #import "webrtc/modules/video_render/mac/cocoa_render_view.h"
     29 
     30 class Trace;
     31 
     32 namespace webrtc {
     33 class EventWrapper;
     34 class ThreadWrapper;
     35 class VideoRenderNSOpenGL;
     36 class CriticalSectionWrapper;
     37 
     38 class VideoChannelNSOpenGL : public VideoRenderCallback
     39 {
     40 
     41 public:
     42 
     43     VideoChannelNSOpenGL(NSOpenGLContext *nsglContext, int iId, VideoRenderNSOpenGL* owner);
     44     virtual ~VideoChannelNSOpenGL();
     45 
     46     // A new frame is delivered
     47     virtual int DeliverFrame(const I420VideoFrame& videoFrame);
     48 
     49     // Called when the incomming frame size and/or number of streams in mix changes
     50     virtual int FrameSizeChange(int width, int height, int numberOfStreams);
     51 
     52     virtual int UpdateSize(int width, int height);
     53 
     54     // Setup
     55     int SetStreamSettings(int streamId, float startWidth, float startHeight, float stopWidth, float stopHeight);
     56     int SetStreamCropSettings(int streamId, float startWidth, float startHeight, float stopWidth, float stopHeight);
     57 
     58     // Called when it's time to render the last frame for the channel
     59     int RenderOffScreenBuffer();
     60 
     61     // Returns true if a new buffer has been delivered to the texture
     62     int IsUpdated(bool& isUpdated);
     63     virtual int UpdateStretchSize(int stretchHeight, int stretchWidth);
     64 
     65     // ********** new module functions ************ //
     66     virtual int32_t RenderFrame(const uint32_t streamId,
     67                                 I420VideoFrame& videoFrame);
     68 
     69     // ********** new module helper functions ***** //
     70     int ChangeContext(NSOpenGLContext *nsglContext);
     71     int32_t GetChannelProperties(float& left,
     72                                  float& top,
     73                                  float& right,
     74                                  float& bottom);
     75 
     76 private:
     77 
     78     NSOpenGLContext* _nsglContext;
     79     int _id;
     80     VideoRenderNSOpenGL* _owner;
     81     int32_t _width;
     82     int32_t _height;
     83     float _startWidth;
     84     float _startHeight;
     85     float _stopWidth;
     86     float _stopHeight;
     87     int _stretchedWidth;
     88     int _stretchedHeight;
     89     int _oldStretchedHeight;
     90     int _oldStretchedWidth;
     91     unsigned char* _buffer;
     92     int _bufferSize;
     93     int _incommingBufferSize;
     94     bool _bufferIsUpdated;
     95     int _numberOfStreams;
     96     GLenum _pixelFormat;
     97     GLenum _pixelDataType;
     98     unsigned int _texture;
     99 };
    100 
    101 class VideoRenderNSOpenGL
    102 {
    103 
    104 public: // methods
    105     VideoRenderNSOpenGL(CocoaRenderView *windowRef, bool fullScreen, int iId);
    106     ~VideoRenderNSOpenGL();
    107 
    108     static int GetOpenGLVersion(int& nsglMajor, int& nsglMinor);
    109 
    110     // Allocates textures
    111     int Init();
    112     VideoChannelNSOpenGL* CreateNSGLChannel(int streamID, int zOrder, float startWidth, float startHeight, float stopWidth, float stopHeight);
    113     VideoChannelNSOpenGL* ConfigureNSGLChannel(int channel, int zOrder, float startWidth, float startHeight, float stopWidth, float stopHeight);
    114     int DeleteNSGLChannel(int channel);
    115     int DeleteAllNSGLChannels();
    116     int StopThread();
    117     bool IsFullScreen();
    118     bool HasChannels();
    119     bool HasChannel(int channel);
    120     int GetChannels(std::list<int>& channelList);
    121     void LockAGLCntx();
    122     void UnlockAGLCntx();
    123 
    124     // ********** new module functions ************ //
    125     int ChangeWindow(CocoaRenderView* newWindowRef);
    126     int32_t ChangeUniqueID(int32_t id);
    127     int32_t StartRender();
    128     int32_t StopRender();
    129     int32_t DeleteNSGLChannel(const uint32_t streamID);
    130     int32_t GetChannelProperties(const uint16_t streamId,
    131                                  uint32_t& zOrder,
    132                                  float& left,
    133                                  float& top,
    134                                  float& right,
    135                                  float& bottom);
    136 
    137     int32_t SetText(const uint8_t textId,
    138                     const uint8_t* text,
    139                     const int32_t textLength,
    140                     const uint32_t textColorRef,
    141                     const uint32_t backgroundColorRef,
    142                     const float left,
    143                     const float top,
    144                     const float right,
    145                     const float bottom);
    146 
    147     // ********** new module helper functions ***** //
    148     int configureNSOpenGLEngine();
    149     int configureNSOpenGLView();
    150     int setRenderTargetWindow();
    151     int setRenderTargetFullScreen();
    152 
    153 protected: // methods
    154     static bool ScreenUpdateThreadProc(void* obj);
    155     bool ScreenUpdateProcess();
    156     int GetWindowRect(Rect& rect);
    157 
    158 private: // methods
    159 
    160     int CreateMixingContext();
    161     int RenderOffScreenBuffers();
    162     int DisplayBuffers();
    163 
    164 private: // variables
    165 
    166 
    167     CocoaRenderView* _windowRef;
    168     bool _fullScreen;
    169     int _id;
    170     CriticalSectionWrapper& _nsglContextCritSec;
    171     ThreadWrapper* _screenUpdateThread;
    172     EventWrapper* _screenUpdateEvent;
    173     NSOpenGLContext* _nsglContext;
    174     NSOpenGLContext* _nsglFullScreenContext;
    175     CocoaFullScreenWindow* _fullScreenWindow;
    176     Rect _windowRect; // The size of the window
    177     int _windowWidth;
    178     int _windowHeight;
    179     std::map<int, VideoChannelNSOpenGL*> _nsglChannels;
    180     std::multimap<int, int> _zOrderToChannel;
    181     unsigned int _threadID;
    182     bool _renderingIsPaused;
    183     NSView* _windowRefSuperView;
    184     NSRect _windowRefSuperViewFrame;
    185 };
    186 
    187 }  // namespace webrtc
    188 
    189 #endif   // WEBRTC_MODULES_VIDEO_RENDER_MAIN_SOURCE_MAC_VIDEO_RENDER_NSOPENGL_H_
    190 #endif	 // COCOA_RENDERING
    191