context: #if UNITY_5 && !UNITY_5_0 && !UNITY_5_1
  	#define AVPRO_MOVIECAPTURE_GLISSUEEVENT_52
  #endif
  using UnityEngine;
  using System.IO;
  using System;
  using System.Runtime.InteropServices;
  
  //-----------------------------------------------------------------------------
  // Copyright 2012-2016 RenderHeads Ltd.  All rights reserved.
  //-----------------------------------------------------------------------------
  
  public class AVProMovieCaptureBase : MonoBehaviour 
  {
  	public enum FrameRate
  	{
  		Fifteen = 15,
  		TwentyFour = 24,
  		TwentyFive = 25,
  		Thirty = 30,
  		Fifty = 50,
  		Sixty = 60,
  		SeventyFive = 75,
  		Ninety = 90,
  		OneTwenty = 120,
  	}
  
  	public enum Resolution
  	{
  		POW2_4096x4096,
  		POW2_4096x2048,
  		POW2_2048x2048,
  		POW2_2048x1024,
  		POW2_1024x1024,
  		UHD_3840x2160,
  		HD_1920x1080,
  		HD_1280x720,
  		SD_1024x768,
  		SD_800x600,
  		SD_800x450,
  		SD_640x480,
  		SD_640x360,
  		SD_320x240,
  		Original,
  		Custom,
  	}
  	
  	public enum CubemapDepth
  	{
  		Depth_24 = 24,
  		Depth_16 = 16,
  		Depth_Zero = 0,
  	}
  	
  	public enum CubemapResolution
  	{
  		POW2_4096 = 4096,
  		POW2_2048 = 2048,
  		POW2_1024 = 1024,
  		POW2_512 = 512,
  		POW2_256 = 256,
  	}	
  
  	public enum AntiAliasingLevel
  	{
  		UseCurrent,
  		ForceNone,
  		ForceSample2,
  		ForceSample4,
  		ForceSample8,
  	}
  	
  	public enum DownScale
  	{
  		Original = 1,
  		Half = 2,
  		Quarter = 4,
  		Eighth = 8,
  		Sixteenth = 16,
  		Custom = 100,
  	}
  	
  	public enum OutputPath
  	{
  		RelativeToProject,
  		RelativeToPeristentData,
  		Absolute,
  	}
  
  	public enum OutputExtension
  	{
  		AVI,
  		MP4,
  		PNG,
  		Custom = 100,
  	}
  
  	public enum OutputType
  	{
  		VideoFile,
  		NamedPipe,
  	}
  
  	[Header("General")]
  
  	public KeyCode _captureKey = KeyCode.None;
  	public bool _captureOnStart = false;
  	public bool _startPaused = false;
  	public bool _listVideoCodecsOnStart = false;
  	public bool _isRealTime = true;
  
  	[Header("Video")]
  
  	public string[] _videoCodecPriority = { "Lagarith Lossless Codec",
  											"x264vfw - H.264/MPEG-4 AVC codec",
  											"Xvid MPEG-4 Codec" };
  	public FrameRate _frameRate = FrameRate.Thirty;
  	public DownScale _downScale = DownScale.Original;
  	public Vector2 _maxVideoSize = Vector2.zero;
  	public int _forceVideoCodecIndex = -1;
  	public bool _flipVertically = false;
  	public bool _supportAlpha = false;
  
  	[Header("Audio")]
  
  	public bool _noAudio = true;
  	public string[] _audioCodecPriority = { };
  	public int _forceAudioCodecIndex = -1;
  	public int _forceAudioDeviceIndex = -1;
  	public AVProUnityAudioCapture _audioCapture;
  
  	[Header("Output")]
  
  	public bool _autoGenerateFilename = true;
  	public OutputPath _outputFolderType = OutputPath.RelativeToProject;
  	public string _outputFolderPath;
  	public string _autoFilenamePrefix = "MovieCapture";
  	public string _autoFilenameExtension = "avi";
  	public string _forceFilename = "movie.avi";
  	public OutputType _outputType = OutputType.VideoFile;
  
  	[Header("Camera Specific")]
  
  	public Resolution _renderResolution = Resolution.Original;
  	public int _renderWidth = 0;
  	public int _renderHeight = 0;
  	public int _renderAntiAliasing = -1;
  
  	[Header("Motion Blur")]
  
  	public bool _useMotionBlur = false;
  	[Range(0, 64)]
  	public int _motionBlurSamples = 0;
  	public Camera[] _motionBlurCameras;
  	protected AVProMovieCaptureMotionBlur _motionBlur;
  
  	[Header("Performance")]
  
  	public bool _allowVSyncDisable = true;
  	public bool _allowFrameRateChange = true;
  
  	[Header("Experimental")]
  	public bool _useMediaFoundationH264 = false;
  
  	
  
  	[System.NonSerialized]
  	public string _codecName = "uncompressed";
  	[System.NonSerialized]
  	public int _codecIndex = -1;
  
  	[System.NonSerialized]
  	public string _audioCodecName = "uncompressed";
  	[System.NonSerialized]
  	public int _audioCodecIndex = -1;
  
  	[System.NonSerialized]
  	public string _audioDeviceName = "Unity";
  	[System.NonSerialized]
  	public int _audioDeviceIndex = -1;
  	
  	[System.NonSerialized]
  	public int _unityAudioSampleRate = -1;
  	[System.NonSerialized]
  	public int _unityAudioChannelCount = -1;
  
  	protected Texture2D _texture;
  	protected int _handle = -1;
  	protected int _targetWidth, _targetHeight;
  	protected bool _capturing = false;
  	protected bool _paused = false;
  	protected string _filePath;
  	protected FileInfo _fileInfo;
  	protected AVProMovieCapturePlugin.PixelFormat _pixelFormat = AVProMovieCapturePlugin.PixelFormat.YCbCr422_YUY2;
  	private int _oldVSyncCount = 0;
  	private int _oldTargetFrameRate = -1;
  	private float _oldFixedDeltaTime = 0f;
  	protected bool _isTopDown = true;
  	protected bool _isDirectX11 = false;
  	private bool _queuedStartCapture = false;
  	private bool _queuedStopCapture = false;
  
  	public string LastFilePath  
  	{
  		get { return _filePath; }
  	}
  
  	[Header("Other")]
  	public long _minimumDiskSpaceMB = 16;
  	private long _freeDiskSpaceMB;
  	
  	// Stats
  	private uint _numDroppedFrames;
  	private uint _numDroppedEncoderFrames;
  	private uint _numEncodedFrames;
  	private uint _totalEncodedSeconds;
  
  #if AVPRO_MOVIECAPTURE_GLISSUEEVENT_52
  	protected System.IntPtr _renderEventFunction = System.IntPtr.Zero;
  	protected System.IntPtr _freeEventFunction = System.IntPtr.Zero;
  #endif
  
  	public uint NumDroppedFrames
  	{
  		get { return _numDroppedFrames; }
  	}
  	
  	public uint NumDroppedEncoderFrames
  	{
  		get { return _numDroppedEncoderFrames; }
  	}
  
  	public uint NumEncodedFrames
  	{
  		get { return _numEncodedFrames; }
  	}
  
  	public uint TotalEncodedSeconds
  	{
  		get { return _totalEncodedSeconds; }
  	}
  
  	public void Awake()
  	{
  		try
  		{
  			if (AVProMovieCapturePlugin.Init())
  			{
  				Debug.Log("[AVProMovieCapture] Init plugin version: " + AVProMovieCapturePlugin.GetPluginVersion().ToString("F2") + " with GPU " + SystemInfo.graphicsDeviceVersion);
  #if AVPRO_MOVIECAPTURE_GLISSUEEVENT_52
  				_renderEventFunction = AVProMovieCapturePlugin.GetRenderEventFunc();
  				_freeEventFunction = AVProMovieCapturePlugin.GetFreeResourcesEventFunc();
  #endif
  			}
  			else
  			{
  				Debug.LogError("[AVProMovieCapture] Failed to initialise plugin version: " + AVProMovieCapturePlugin.GetPluginVersion().ToString("F2") + " with GPU " + SystemInfo.graphicsDeviceVersion);
  			}
  		}
  		catch (DllNotFoundException e)
  		{
  			Debug.LogError("[AVProMovieCapture] Unity couldn't find the DLL, did you move the 'Plugins' folder to the root of your project?");
  			throw e;
  		}
  
  		_isDirectX11 = SystemInfo.graphicsDeviceVersion.StartsWith("Direct3D 11");
  		
  		SelectCodec(_listVideoCodecsOnStart);
  		SelectAudioCodec(_listVideoCodecsOnStart);
  		SelectAudioDevice(_listVideoCodecsOnStart);		
  	}
  	
  	public virtual void Start() 
  	{
  		Application.runInBackground = true;
  		
  		if (_captureOnStart)
  		{
  			StartCapture();
  		}
  	}
  	
  	public void SelectCodec(bool listCodecs)
  	{
  		// Enumerate video codecs
  		int numVideoCodecs = AVProMovieCapturePlugin.GetNumAVIVideoCodecs();
  		if (listCodecs)
  		{
  			for (int i = 0; i < numVideoCodecs; i++)
  			{
  				Debug.Log("VideoCodec " + i + ": " + AVProMovieCapturePlugin.GetAVIVideoCodecName(i));
  			}
  		}
  		
  		// The user has specified their own codec index
  		if (_forceVideoCodecIndex >= 0)
  		{
  			if (_forceVideoCodecIndex < numVideoCodecs)
  			{
  				_codecName = AVProMovieCapturePlugin.GetAVIVideoCodecName(_forceVideoCodecIndex);
  				_codecIndex = _forceVideoCodecIndex;
  			}
  		}
  		else
  		{
  			// Try to find the codec based on the priority list
  			if (_videoCodecPriority != null)
  			{
  				foreach (string codec in _videoCodecPriority)
  				{
  					string codecName = codec.Trim();
  					// Empty string means uncompressed
  					if (string.IsNullOrEmpty(codecName))
  						break;
  					
  					for (int i = 0; i < numVideoCodecs; i++)
  					{
  						if (codecName == AVProMovieCapturePlugin.GetAVIVideoCodecName(i))
  						{
  							_codecName = codecName;
  							_codecIndex = i;
  							break;
  						}
  					}
  					
  					if (_codecIndex >= 0)
  						break;
  				}
  			}
  		}
  		
  		if (_codecIndex < 0)
  		{
  			_codecName = "Uncompressed";
  			Debug.LogWarning("[AVProMovieCapture] Codec not found.  Video will be uncompressed.");
  		}
  	}
  	
  
  	public void SelectAudioCodec(bool listCodecs)
  	{
  		// Enumerate audio codecs
  		int numAudioCodecs = AVProMovieCapturePlugin.GetNumAVIAudioCodecs();
  		if (listCodecs)
  		{
  			for (int i = 0; i < numAudioCodecs; i++)
  			{
  				Debug.Log("AudioCodec " + i + ": " + AVProMovieCapturePlugin.GetAVIAudioCodecName(i));
  			}
  		}
  		
  		// The user has specified their own codec index
  		if (_forceAudioCodecIndex >= 0)
  		{
  			if (_forceAudioCodecIndex < numAudioCodecs)
  			{
  				_audioCodecName = AVProMovieCapturePlugin.GetAVIAudioCodecName(_forceAudioCodecIndex);
  				_audioCodecIndex = _forceAudioCodecIndex;
  			}
  		}
  		else
  		{
  			// Try to find the codec based on the priority list
  			if (_audioCodecPriority != null)
  			{
  				foreach (string codec in _audioCodecPriority)
  				{
  					string codecName = codec.Trim();
  					// Empty string means uncompressed
  					if (string.IsNullOrEmpty(codecName))
  						break;
  					
  					for (int i = 0; i < numAudioCodecs; i++)
  					{
  						if (codecName == AVProMovieCapturePlugin.GetAVIAudioCodecName(i))
  						{
  							_audioCodecName = codecName;
  							_audioCodecIndex = i;
  							break;
  						}
  					}
  					
  					if (_audioCodecIndex >= 0)
  						break;
  				}
  			}
  		}
  		
  		if (_audioCodecIndex < 0)
  		{
  			_audioCodecName = "Uncompressed";
  			Debug.LogWarning("[AVProMovieCapture] Codec not found.  Audio will be uncompressed.");
  		}
  	}	
  
  	public void SelectAudioDevice(bool display)
  	{
  		// Enumerate
  		int num = AVProMovieCapturePlugin.GetNumAVIAudioInputDevices();
  		if (display)
  		{
  			for (int i = 0; i < num; i++)
  			{
  				Debug.Log("AudioDevice " + i + ": " + AVProMovieCapturePlugin.GetAVIAudioInputDeviceName(i));
  			}
  		}
  
  		// The user has specified their own device index
  
gt: 		if (_forceAudioDeviceIndex >= 0)