private void CreateComponents()
    {
        switch (_sourceType)
        {
        case SourceType.Scene:
            _captureScene = (AVProMovieCaptureFromScene)GameObject.FindObjectOfType(typeof(AVProMovieCaptureFromScene));
            if (_captureScene == null)
            {
                GameObject go = new GameObject(TempGameObjectName);
                _captureScene = go.AddComponent <AVProMovieCaptureFromScene>();
            }
            _capture = _captureScene;
            break;

        case SourceType.Camera:
            _captureCamera = _cameraNode.gameObject.GetComponent <AVProMovieCaptureFromCamera>();
            if (_captureCamera == null)
            {
                _captureCamera = _cameraNode.gameObject.AddComponent <AVProMovieCaptureFromCamera>();
            }
            _captureCamera._useFastPixelFormat = _cameraFastPixel;
            _capture = _captureCamera;
            break;
        }

        _audioCapture = null;
        if (_captureAudio && _audioDeviceIndex == 0)
        {
            _audioCapture = (AVProUnityAudioCapture)GameObject.FindObjectOfType(typeof(AVProUnityAudioCapture));
            if (_audioCapture == null && Camera.main != null)
            {
                _audioCapture = Camera.main.gameObject.AddComponent <AVProUnityAudioCapture>();
            }
        }
    }
 private void StopCapture()
 {
     if (_capture != null)
     {
         if (_capture.IsCapturing())
         {
             _capture.StopCapture();
             _lastCapturePath = _capture.LastFilePath;
         }
         _capture       = null;
         _captureScene  = null;
         _captureCamera = null;
     }
     _audioCapture = null;
 }
 private void StopCapture()
 {
     if (_capture != null)
     {
         if (_capture.IsCapturing())
         {
             _capture.StopCapture();
             _lastCapturePath = _capture.LastFilePath;
         }
         _capture = null;
         _captureScene = null;
         _captureCamera = null;
     }
     _audioCapture = null;
 }
    private void CreateComponents()
    {
        switch (_sourceType)
        {
        case SourceType.Scene:
            _captureScene = (AVProMovieCaptureFromScene)GameObject.FindObjectOfType(typeof(AVProMovieCaptureFromScene));
            if (_captureScene == null)
            {
                GameObject go = new GameObject(TempGameObjectName);
                _captureScene = go.AddComponent<AVProMovieCaptureFromScene>();
            }
            _capture = _captureScene;
            break;
        case SourceType.Camera:
            _captureCamera = _cameraNode.gameObject.GetComponent<AVProMovieCaptureFromCamera>();
            if (_captureCamera == null)
            {
                _captureCamera = _cameraNode.gameObject.AddComponent<AVProMovieCaptureFromCamera>();
            }
            _captureCamera._useFastPixelFormat = _cameraFastPixel;
            _capture = _captureCamera;
            break;
        }

        _audioCapture = null;
        if (_captureAudio && _audioDeviceIndex == 0)
        {
            _audioCapture = (AVProUnityAudioCapture)GameObject.FindObjectOfType(typeof(AVProUnityAudioCapture));
            if (_audioCapture == null && Camera.main != null)
            {
                _audioCapture = Camera.main.gameObject.AddComponent<AVProUnityAudioCapture>();
            }
        }
    }
Esempio n. 5
0
    public virtual bool PrepareCapture()
    {
        // Delete file if it already exists
        if (File.Exists(_filePath))
        {
            File.Delete(_filePath);
        }

#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
        if (_minimumDiskSpaceMB > 0)
        {
            ulong freespace = 0;
            if (DriveFreeBytes(System.IO.Path.GetPathRoot(_filePath), out freespace))
            {
                _freeDiskSpaceMB = (long)(freespace / (1024 * 1024));
            }

            if (!IsEnoughDiskSpace())
            {
                Debug.LogError("[AVProMovieCapture] Not enough free space to start capture.  Stopping capture.");
                return(false);
            }
        }
#endif

        // Disable vsync
        if (_allowVSyncDisable && !Screen.fullScreen && QualitySettings.vSyncCount > 0)
        {
            _oldVSyncCount             = QualitySettings.vSyncCount;
            QualitySettings.vSyncCount = 0;
        }

        if (_isRealTime)
        {
            if (_allowFrameRateChange)
            {
                _oldTargetFrameRate         = Application.targetFrameRate;
                Application.targetFrameRate = (int)_frameRate;
            }
        }
        else
        {
            if (_useMotionBlur && _motionBlurSamples > 1 && _motionBlurCameras.Length > 0)
            {
                Time.captureFramerate = _motionBlurSamples * (int)_frameRate;

                // Setup the motion blur filters
                foreach (Camera camera in _motionBlurCameras)
                {
                    AVProMovieCaptureMotionBlur mb = camera.GetComponent <AVProMovieCaptureMotionBlur>();
                    if (mb == null)
                    {
                        mb = camera.gameObject.AddComponent <AVProMovieCaptureMotionBlur>();
                    }
                    if (mb != null)
                    {
                        mb.NumSamples = _motionBlurSamples;
                        _motionBlur   = mb;
                    }

                    mb.enabled = true;
                }
            }
            else
            {
                Time.captureFramerate = (int)_frameRate;
            }

            // Change physics update speed
            _oldFixedDeltaTime  = Time.fixedDeltaTime;
            Time.fixedDeltaTime = 1.0f / Time.captureFramerate;
        }

        int  audioDeviceIndex = _audioDeviceIndex;
        int  audioCodecIndex  = _audioCodecIndex;
        bool noAudio          = _noAudio;
        if (!_isRealTime)
        {
            noAudio = true;
        }

        // We if try to capture audio from Unity but there isn't an AVProUnityAudioCapture component set
        if (!noAudio && _audioCapture == null && _audioDeviceIndex < 0)
        {
            // Try to find it locally
            _audioCapture = this.GetComponent <AVProUnityAudioCapture>();
            if (_audioCapture == null)
            {
                // Try to find it globally
                _audioCapture = GameObject.FindObjectOfType <AVProUnityAudioCapture>();
            }

            if (_audioCapture == null)
            {
                AudioListener audioListener = this.GetComponent <AudioListener>();
                if (audioListener == null)
                {
                    audioListener = GameObject.FindObjectOfType <AudioListener>();
                }
                if (audioListener != null)
                {
                    _audioCapture = audioListener.gameObject.AddComponent <AVProUnityAudioCapture>();
                    Debug.LogWarning("[AVProMovieCapture] Capturing audio from Unity without an AVProUnityAudioCapture assigned so we had to create one manually (very slow).  Consider adding a AVProUnityAudioCapture component to your scene and assigned it to this MovieCapture component.");
                }
                else
                {
                    noAudio = true;
                    Debug.LogWarning("[AVProMovieCapture] No audio listener found in scene.  Unable to capture audio from Untiy.");
                }
            }
            else
            {
                Debug.LogWarning("[AVProMovieCapture] Capturing audio from Unity without an AVProUnityAudioCapture assigned so we had to search for one manually (very slow)");
            }
        }

        if (noAudio || (_audioCapture == null && _audioDeviceIndex < 0))
        {
            audioCodecIndex  = audioDeviceIndex = -1;
            _audioDeviceName = "none";
            noAudio          = true;
        }

        _unityAudioSampleRate   = -1;
        _unityAudioChannelCount = -1;
        if (!noAudio && _audioDeviceIndex < 0 && _audioCapture != null)
        {
            if (!_audioCapture.enabled)
            {
                _audioCapture.enabled = true;
            }
            _unityAudioSampleRate   = AudioSettings.outputSampleRate;
            _unityAudioChannelCount = _audioCapture.NumChannels;
        }

        string info = string.Format("{0}x{1} @ {2}fps [{3}]", _targetWidth, _targetHeight, ((int)_frameRate).ToString(), _pixelFormat.ToString());
        info += string.Format(" vcodec:'{0}'", _codecName);
        if (!noAudio)
        {
            if (_audioDeviceIndex >= 0)
            {
                info += string.Format(" audio device:'{0}'", _audioDeviceName);
            }
            else
            {
                info += string.Format(" audio device:'Unity' {0}hz {1} channels", _unityAudioSampleRate, _unityAudioChannelCount);
            }
            info += string.Format(" acodec:'{0}'", _audioCodecName);
        }
        info += string.Format(" to file: '{0}'", _filePath);

        // If the user has overriden the vertical flip
        if (_flipVertically)
        {
            _isTopDown = !_isTopDown;
        }

        if (_outputType == OutputType.VideoFile)
        {
            // TOOD: make _frameRate floating point, or add timeLapse time system
            Debug.Log("[AVProMovieCapture] Start File Capture: " + info);
            _handle = AVProMovieCapturePlugin.CreateRecorderAVI(_filePath, (uint)_targetWidth, (uint)_targetHeight, (int)_frameRate,
                                                                (int)(_pixelFormat), _isTopDown, _codecIndex, !noAudio, _unityAudioSampleRate, _unityAudioChannelCount, audioDeviceIndex, audioCodecIndex, _isRealTime, _useMediaFoundationH264, _supportAlpha);
        }
        else if (_outputType == OutputType.NamedPipe)
        {
            Debug.Log("[AVProMovieCapture] Start Pipe Capture: " + info);
            _handle = AVProMovieCapturePlugin.CreateRecorderPipe(_filePath, (uint)_targetWidth, (uint)_targetHeight, (int)_frameRate,
                                                                 (int)(_pixelFormat), _isTopDown, _supportAlpha);
        }

        if (_handle < 0)
        {
            Debug.LogError("[AVProMovieCapture] Failed to create recorder");
            StopCapture();
        }

        return(_handle >= 0);
    }