/// <summary> /// sets the codec token to be used for video compression /// </summary> public void SetVideoCodecToken(IDisposable token) { if (token is CodecToken) currVideoCodecToken = (CodecToken)token; else throw new ArgumentException("AviWriter only takes its own Codec Tokens!"); }
public void OpenFile(string destPath, Parameters parameters, CodecToken videoCodecToken) { this.parameters = parameters; this.currVideoCodecToken = videoCodecToken; //TODO - try creating the file once first before we let vfw botch it up? //open the avi output file handle if (File.Exists(destPath)) { File.Delete(destPath); } if (Win32.FAILED(Win32.AVIFileOpenW(ref pAviFile, destPath, Win32.OpenFileStyle.OF_CREATE | Win32.OpenFileStyle.OF_WRITE, 0))) { throw new InvalidOperationException("Couldnt open dest path for avi file: " + destPath); } //initialize the video stream Win32.AVISTREAMINFOW vidstream_header = new Win32.AVISTREAMINFOW(); Win32.BITMAPINFOHEADER bmih = new Win32.BITMAPINFOHEADER(); parameters.PopulateBITMAPINFOHEADER24(ref bmih); vidstream_header.fccType = Win32.mmioFOURCC("vids"); vidstream_header.dwRate = parameters.fps; vidstream_header.dwScale = parameters.fps_scale; vidstream_header.dwSuggestedBufferSize = (int)bmih.biSizeImage; if (Win32.FAILED(Win32.AVIFileCreateStreamW(pAviFile, out pAviRawVideoStream, ref vidstream_header))) { CloseFile(); throw new InvalidOperationException("Failed opening raw video stream. Not sure how this could happen"); } //initialize audio stream Win32.AVISTREAMINFOW audstream_header = new Win32.AVISTREAMINFOW(); Win32.WAVEFORMATEX wfex = new Win32.WAVEFORMATEX(); parameters.PopulateWAVEFORMATEX(ref wfex); audstream_header.fccType = Win32.mmioFOURCC("auds"); audstream_header.dwQuality = -1; audstream_header.dwScale = wfex.nBlockAlign; audstream_header.dwRate = (int)wfex.nAvgBytesPerSec; audstream_header.dwSampleSize = wfex.nBlockAlign; audstream_header.dwInitialFrames = 1; // ??? optimal value? if (Win32.FAILED(Win32.AVIFileCreateStreamW(pAviFile, out pAviRawAudioStream, ref audstream_header))) { CloseFile(); throw new InvalidOperationException("Failed opening raw audio stream. Not sure how this could happen"); } outStatus = new OutputStatus(); IsOpen = true; }
public void SetDefaultVideoCodecToken() { CodecToken ct = new CodecToken(); // load from config and sanitize int t = Math.Min(Math.Max(Global.Config.JMDThreads, 1), 6); int c = Math.Min(Math.Max(Global.Config.JMDCompression, Deflater.NO_COMPRESSION), Deflater.BEST_COMPRESSION); ct.compressionlevel = c; ct.numthreads = t; token = ct; }
public void SetDefaultVideoCodecToken(Config config) { CodecToken ct = new CodecToken(); // load from config and sanitize int t = Math.Min(Math.Max(config.JmdThreads, 1), 6); int c = Math.Min(Math.Max(config.JmdCompression, NO_COMPRESSION), BEST_COMPRESSION); ct.CompressionLevel = c; ct.NumThreads = t; _token = ct; }
private static CodecToken DeSerializeFromByteArray(byte[] data) { var m = new MemoryStream(data, false); var b = new BinaryReader(m); AVIWriterImports.AVICOMPRESSOPTIONS comprOptions = new AVIWriterImports.AVICOMPRESSOPTIONS(); byte[] Format; byte[] Parms; try { comprOptions.fccType = b.ReadInt32(); comprOptions.fccHandler = b.ReadInt32(); comprOptions.dwKeyFrameEvery = b.ReadInt32(); comprOptions.dwQuality = b.ReadInt32(); comprOptions.dwBytesPerSecond = b.ReadInt32(); comprOptions.dwFlags = b.ReadInt32(); //comprOptions.lpFormat = b.ReadInt32(); comprOptions.cbFormat = b.ReadInt32(); //comprOptions.lpParms = b.ReadInt32(); comprOptions.cbParms = b.ReadInt32(); comprOptions.dwInterleaveEvery = b.ReadInt32(); Format = b.ReadBytes(comprOptions.cbFormat); Parms = b.ReadBytes(comprOptions.cbParms); } catch (IOException) { // ran off end of array most likely return(null); } finally { b.Close(); } var ret = new CodecToken { _comprOptions = comprOptions, Format = Format, Parms = Parms, codec = Decode_mmioFOURCC(comprOptions.fccHandler) }; return(ret); }
/// <summary> /// begin recording /// </summary> public void OpenStreams() { if (currVideoCodecToken == null) { throw new InvalidOperationException("set a video codec token before opening the streams!"); } // open compressed video stream Win32.AVICOMPRESSOPTIONS opts = new Win32.AVICOMPRESSOPTIONS(); currVideoCodecToken.AllocateToAVICOMPRESSOPTIONS(ref opts); bool failed = Win32.FAILED(Win32.AVIMakeCompressedStream(out pAviCompressedVideoStream, pAviRawVideoStream, ref opts, IntPtr.Zero)); CodecToken.DeallocateAVICOMPRESSOPTIONS(ref opts); if (failed) { CloseStreams(); throw new InvalidOperationException("Failed making compressed video stream"); } // set the compressed video stream input format Win32.BITMAPINFOHEADER bmih = new Win32.BITMAPINFOHEADER(); if (bit32) { parameters.PopulateBITMAPINFOHEADER32(ref bmih); } else { parameters.PopulateBITMAPINFOHEADER24(ref bmih); } if (Win32.FAILED(Win32.AVIStreamSetFormat(pAviCompressedVideoStream, 0, ref bmih, Marshal.SizeOf(bmih)))) { bit32 = true; // we'll try again CloseStreams(); throw new InvalidOperationException("Failed setting compressed video stream input format"); } // set audio stream input format Win32.WAVEFORMATEX wfex = new Win32.WAVEFORMATEX(); parameters.PopulateWAVEFORMATEX(ref wfex); if (Win32.FAILED(Win32.AVIStreamSetFormat(pAviRawAudioStream, 0, ref wfex, Marshal.SizeOf(wfex)))) { CloseStreams(); throw new InvalidOperationException("Failed setting raw audio stream input format"); } }
public static CodecToken CreateFromAVICOMPRESSOPTIONS(ref Win32.AVICOMPRESSOPTIONS opts) { CodecToken ret = new CodecToken(); ret.comprOptions = opts; ret.codec = Win32.decode_mmioFOURCC(opts.fccHandler); ret.Format = new byte[opts.cbFormat]; ret.Parms = new byte[opts.cbParms]; if (opts.lpFormat != IntPtr.Zero) { Marshal.Copy(opts.lpFormat, ret.Format, 0, opts.cbFormat); } if (opts.lpParms != IntPtr.Zero) { Marshal.Copy(opts.lpParms, ret.Parms, 0, opts.cbParms); } return(ret); }
/// <summary> /// Acquires a video codec configuration from the user /// </summary> public IDisposable AcquireVideoCodecToken(IntPtr hwnd, CodecToken lastCodecToken) { if (!IsOpen) { throw new InvalidOperationException("File must be opened before acquiring a codec token (or else the stream formats wouldnt be known)"); } if (lastCodecToken != null) { currVideoCodecToken = lastCodecToken; } //encoder params Win32.AVICOMPRESSOPTIONS comprOptions = new Win32.AVICOMPRESSOPTIONS(); if (currVideoCodecToken != null) { currVideoCodecToken.AllocateToAVICOMPRESSOPTIONS(ref comprOptions); } bool result = AVISaveOptions(pAviRawVideoStream, ref comprOptions, hwnd) != 0; CodecToken ret = CodecToken.CreateFromAVICOMPRESSOPTIONS(ref comprOptions); //so, AVISaveOptions may have changed some of the pointers //if it changed the pointers, did it it free the old ones? we don't know //let's assume it frees them. if we're wrong, we leak. if we assume otherwise and we're wrong, we may crash. //so that means any pointers that come in here are either //1. ones we allocated a minute ago //2. ones VFW allocated //guess what? doesn't matter. We'll free them all ourselves. CodecToken.DeallocateAVICOMPRESSOPTIONS(ref comprOptions); if (result) { // save to config and return it Global.Config.AVICodecToken = ret.Serialize(); return(ret); } else { return(null); } }
public IDisposable AcquireVideoCodecToken(Config config) { var ret = new CodecToken(); // load from config and sanitize int t = Math.Min(Math.Max(config.JmdThreads, 1), 6); int c = Math.Min(Math.Max(config.JmdCompression, NO_COMPRESSION), BEST_COMPRESSION); if (!JmdForm.DoCompressionDlg(ref t, ref c, 1, 6, NO_COMPRESSION, BEST_COMPRESSION, _dialogParent.AsWinFormsHandle())) { return(null); } config.JmdThreads = ret.NumThreads = t; config.JmdCompression = ret.CompressionLevel = c; return(ret); }
/// <summary> /// obtain a set of recording compression parameters /// </summary> /// <param name="hwnd">hwnd to attach to if the user is shown config dialog</param> /// <returns>codec token, dispose of it when you're done with it</returns> public IDisposable AcquireVideoCodecToken(IWin32Window hwnd) { var ret = new CodecToken(); // load from config and sanitize int t = Math.Min(Math.Max(Global.Config.JmdThreads, 1), 6); int c = Math.Min(Math.Max(Global.Config.JmdCompression, Deflater.NO_COMPRESSION), Deflater.BEST_COMPRESSION); if (!JmdForm.DoCompressionDlg(ref t, ref c, 1, 6, Deflater.NO_COMPRESSION, Deflater.BEST_COMPRESSION, hwnd)) { return(null); } Global.Config.JmdThreads = ret.NumThreads = t; Global.Config.JmdCompression = ret.CompressionLevel = c; return(ret); }
/// <summary> /// obtain a set of recording compression parameters /// </summary> /// <param name="hwnd">hwnd to attach to if the user is shown config dialog</param> /// <returns>codec token, dispose of it when you're done with it</returns> public IDisposable AcquireVideoCodecToken(System.Windows.Forms.IWin32Window hwnd) { CodecToken ret = new CodecToken(); // load from config and sanitize int t = Math.Min(Math.Max(Global.Config.JMDThreads, 1), 6); int c = Math.Min(Math.Max(Global.Config.JMDCompression, Deflater.NO_COMPRESSION), Deflater.BEST_COMPRESSION); if (!JMDForm.DoCompressionDlg(ref t, ref c, 1, 6, Deflater.NO_COMPRESSION, Deflater.BEST_COMPRESSION, hwnd)) { return(null); } Global.Config.JMDThreads = ret.numthreads = t; Global.Config.JMDCompression = ret.compressionlevel = c; return(ret); }
public static CodecToken CreateFromAVICOMPRESSOPTIONS(ref AVIWriterImports.AVICOMPRESSOPTIONS opts) { var ret = new CodecToken { _comprOptions = opts, codec = Decode_mmioFOURCC(opts.fccHandler), Format = new byte[opts.cbFormat], Parms = new byte[opts.cbParms] }; if (opts.lpFormat != IntPtr.Zero) { Marshal.Copy(opts.lpFormat, ret.Format, 0, opts.cbFormat); } if (opts.lpParms != IntPtr.Zero) { Marshal.Copy(opts.lpParms, ret.Parms, 0, opts.cbParms); } return(ret); }
/// <summary> /// Acquires a video codec configuration from the user. you may save it for future use, but you must dispose of it when youre done with it. /// returns null if the user canceled the dialog /// </summary> public IDisposable AcquireVideoCodecToken(System.Windows.Forms.IWin32Window hwnd) //, CodecToken lastToken) { var temp_params = new Parameters(); temp_params.height = 256; temp_params.width = 256; temp_params.fps = 60; temp_params.fps_scale = 1; temp_params.a_bits = 16; temp_params.a_samplerate = 44100; temp_params.a_channels = 2; var temp = new AviWriterSegment(); string tempfile = Path.GetTempFileName(); File.Delete(tempfile); tempfile = Path.ChangeExtension(tempfile, "avi"); temp.OpenFile(tempfile, temp_params, null); //lastToken); CodecToken token = (CodecToken)temp.AcquireVideoCodecToken(hwnd.Handle); temp.CloseFile(); File.Delete(tempfile); return(token); }
/// <summary> /// Acquires a video codec configuration from the user. you may save it for future use, but you must dispose of it when you're done with it. /// returns null if the user canceled the dialog /// </summary> public IDisposable AcquireVideoCodecToken(IWin32Window hwnd) { var tempParams = new Parameters { height = 256, width = 256, fps = 60, fps_scale = 1, a_bits = 16, a_samplerate = 44100, a_channels = 2 }; var temp = new AviWriterSegment(); string tempfile = Path.GetTempFileName(); File.Delete(tempfile); tempfile = Path.ChangeExtension(tempfile, "avi"); temp.OpenFile(tempfile, tempParams, null); CodecToken token = (CodecToken)temp.AcquireVideoCodecToken(hwnd.Handle, _currVideoCodecToken); temp.CloseFile(); File.Delete(tempfile); return(token); }
/// <summary> /// sets the codec token to be used for video compression /// </summary> public void SetVideoCodecToken(IDisposable token) { if (token is CodecToken) this.token = (CodecToken)token; else throw new ArgumentException("codec token must be of right type"); }
/// <summary> /// sets default (probably wrong) parameters /// </summary> public JMDWriter() { fpsnum = 25; fpsden = 1; audiosamplerate = 22050; audiochannels = 1; audiobits = 8; token = null; moviemetadata = null; }
/// <exception cref="InvalidOperationException">unmanaged call failed</exception> public void OpenFile(string destPath, Parameters parameters, CodecToken videoCodecToken) {
static CodecToken DeSerializeFromByteArray(byte[] data) { var m = new MemoryStream(data, false); var b = new BinaryReader(m); Win32.AVICOMPRESSOPTIONS comprOptions = new Win32.AVICOMPRESSOPTIONS(); byte[] Format; byte[] Params; try { comprOptions.fccType = b.ReadInt32(); comprOptions.fccHandler = b.ReadInt32(); comprOptions.dwKeyFrameEvery = b.ReadInt32(); comprOptions.dwQuality = b.ReadInt32(); comprOptions.dwBytesPerSecond = b.ReadInt32(); comprOptions.dwFlags = b.ReadInt32(); //comprOptions.lpFormat = b.ReadInt32(); comprOptions.cbFormat = b.ReadInt32(); //comprOptions.lpParms = b.ReadInt32(); comprOptions.cbParms = b.ReadInt32(); comprOptions.dwInterleaveEvery = b.ReadInt32(); Format = b.ReadBytes(comprOptions.cbFormat); Params = b.ReadBytes(comprOptions.cbParms); } catch (IOException) { // ran off end of array most likely return(null); } finally { b.Close(); } // create unmanaged copies of Format, Params if (comprOptions.cbFormat != 0) { IntPtr lpFormat = Marshal.AllocHGlobal(comprOptions.cbFormat); Marshal.Copy(Format, 0, lpFormat, comprOptions.cbFormat); comprOptions.lpFormat = (int)lpFormat; } else { comprOptions.lpFormat = (int)IntPtr.Zero; } if (comprOptions.cbParms != 0) { IntPtr lpParms = Marshal.AllocHGlobal(comprOptions.cbParms); Marshal.Copy(Params, 0, lpParms, comprOptions.cbParms); comprOptions.lpParms = (int)lpParms; } else { comprOptions.lpParms = (int)IntPtr.Zero; } CodecToken ret = new CodecToken(); ret.marshaled = true; ret.comprOptions = comprOptions; ret.codec = Win32.decode_mmioFOURCC(comprOptions.fccHandler); return(ret); }
/// <summary> /// Acquires a video codec configuration from the user /// </summary> public IDisposable AcquireVideoCodecToken(IntPtr hwnd, CodecToken lastCodecToken) { if (!IsOpen) throw new InvalidOperationException("File must be opened before acquiring a codec token (or else the stream formats wouldnt be known)"); if (lastCodecToken != null) currVideoCodecToken = lastCodecToken; //encoder params Win32.AVICOMPRESSOPTIONS comprOptions = new Win32.AVICOMPRESSOPTIONS(); if (currVideoCodecToken != null) { currVideoCodecToken.AllocateToAVICOMPRESSOPTIONS(ref comprOptions); } bool result = AVISaveOptions(pAviRawVideoStream, ref comprOptions, hwnd) != 0; CodecToken ret = CodecToken.CreateFromAVICOMPRESSOPTIONS(ref comprOptions); //so, AVISaveOptions may have changed some of the pointers //if it changed the pointers, did it it free the old ones? we don't know //let's assume it frees them. if we're wrong, we leak. if we assume otherwise and we're wrong, we may crash. //so that means any pointers that come in here are either //1. ones we allocated a minute ago //2. ones VFW allocated //guess what? doesn't matter. We'll free them all ourselves. CodecToken.DeallocateAVICOMPRESSOPTIONS(ref comprOptions); if(result) { // save to config and return it Global.Config.AVICodecToken = ret.Serialize(); return ret; } else return null; }
public void SetDefaultVideoCodecToken() { CodecToken ct = CodecToken.DeSerialize(Global.Config.AVICodecToken); if (ct == null) throw new Exception("No default AVICodecToken in config!"); currVideoCodecToken = ct; }
public void OpenFile(string destPath, Parameters parameters, CodecToken videoCodecToken) { this.parameters = parameters; this.currVideoCodecToken = videoCodecToken; //TODO - try creating the file once first before we let vfw botch it up? //open the avi output file handle if (File.Exists(destPath)) File.Delete(destPath); if (Win32.FAILED(Win32.AVIFileOpenW(ref pAviFile, destPath, Win32.OpenFileStyle.OF_CREATE | Win32.OpenFileStyle.OF_WRITE, 0))) throw new InvalidOperationException("Couldnt open dest path for avi file: " + destPath); //initialize the video stream Win32.AVISTREAMINFOW vidstream_header = new Win32.AVISTREAMINFOW(); Win32.BITMAPINFOHEADER bmih = new Win32.BITMAPINFOHEADER(); parameters.PopulateBITMAPINFOHEADER24(ref bmih); vidstream_header.fccType = Win32.mmioFOURCC("vids"); vidstream_header.dwRate = parameters.fps; vidstream_header.dwScale = parameters.fps_scale; vidstream_header.dwSuggestedBufferSize = (int)bmih.biSizeImage; if (Win32.FAILED(Win32.AVIFileCreateStreamW(pAviFile, out pAviRawVideoStream, ref vidstream_header))) { CloseFile(); throw new InvalidOperationException("Failed opening raw video stream. Not sure how this could happen"); } //initialize audio stream Win32.AVISTREAMINFOW audstream_header = new Win32.AVISTREAMINFOW(); Win32.WAVEFORMATEX wfex = new Win32.WAVEFORMATEX(); parameters.PopulateWAVEFORMATEX(ref wfex); audstream_header.fccType = Win32.mmioFOURCC("auds"); audstream_header.dwQuality = -1; audstream_header.dwScale = wfex.nBlockAlign; audstream_header.dwRate = (int)wfex.nAvgBytesPerSec; audstream_header.dwSampleSize = wfex.nBlockAlign; audstream_header.dwInitialFrames = 1; // ??? optimal value? if (Win32.FAILED(Win32.AVIFileCreateStreamW(pAviFile, out pAviRawAudioStream, ref audstream_header))) { CloseFile(); throw new InvalidOperationException("Failed opening raw audio stream. Not sure how this could happen"); } outStatus = new OutputStatus(); IsOpen = true; }
static CodecToken DeSerializeFromByteArray(byte[] data) { var m = new MemoryStream(data, false); var b = new BinaryReader(m); Win32.AVICOMPRESSOPTIONS comprOptions = new Win32.AVICOMPRESSOPTIONS(); byte[] Format; byte[] Params; try { comprOptions.fccType = b.ReadInt32(); comprOptions.fccHandler = b.ReadInt32(); comprOptions.dwKeyFrameEvery = b.ReadInt32(); comprOptions.dwQuality = b.ReadInt32(); comprOptions.dwBytesPerSecond = b.ReadInt32(); comprOptions.dwFlags = b.ReadInt32(); //comprOptions.lpFormat = b.ReadInt32(); comprOptions.cbFormat = b.ReadInt32(); //comprOptions.lpParms = b.ReadInt32(); comprOptions.cbParms = b.ReadInt32(); comprOptions.dwInterleaveEvery = b.ReadInt32(); Format = b.ReadBytes(comprOptions.cbFormat); Params = b.ReadBytes(comprOptions.cbParms); } catch (IOException) { // ran off end of array most likely return null; } finally { b.Close(); } // create unmanaged copies of Format, Params if (comprOptions.cbFormat != 0) { IntPtr lpFormat = Marshal.AllocHGlobal(comprOptions.cbFormat); Marshal.Copy(Format, 0, lpFormat, comprOptions.cbFormat); comprOptions.lpFormat = (int)lpFormat; } else comprOptions.lpFormat = (int)IntPtr.Zero; if (comprOptions.cbParms != 0) { IntPtr lpParms = Marshal.AllocHGlobal(comprOptions.cbParms); Marshal.Copy(Params, 0, lpParms, comprOptions.cbParms); comprOptions.lpParms = (int)lpParms; } else comprOptions.lpParms = (int)IntPtr.Zero; CodecToken ret = new CodecToken(); ret.marshaled = true; ret.comprOptions = comprOptions; ret.codec = Win32.decode_mmioFOURCC(comprOptions.fccHandler); return ret; }
public static CodecToken TakePossession(Win32.AVICOMPRESSOPTIONS comprOptions) { CodecToken ret = new CodecToken(); ret.allocated = true; ret.comprOptions = comprOptions; ret.codec = Win32.decode_mmioFOURCC(comprOptions.fccHandler); return ret; }
public static CodecToken CreateFromAVICOMPRESSOPTIONS(ref Win32.AVICOMPRESSOPTIONS opts) { CodecToken ret = new CodecToken(); ret.comprOptions = opts; ret.codec = Win32.decode_mmioFOURCC(opts.fccHandler); ret.Format = new byte[opts.cbFormat]; ret.Parms = new byte[opts.cbParms]; if(opts.lpFormat != 0) Marshal.Copy(new IntPtr(opts.lpFormat), ret.Format, 0, opts.cbFormat); if (opts.lpParms != 0) Marshal.Copy(new IntPtr(opts.lpParms), ret.Parms, 0, opts.cbParms); return ret; }
/// <summary> /// obtain a set of recording compression parameters /// </summary> /// <param name="hwnd">hwnd to attach to if the user is shown config dialog</param> /// <returns>codec token, dispose of it when you're done with it</returns> public IDisposable AcquireVideoCodecToken(System.Windows.Forms.IWin32Window hwnd) { CodecToken ret = new CodecToken(); // load from config and sanitize int t = Math.Min(Math.Max(Global.Config.JMDThreads, 1), 6); int c = Math.Min(Math.Max(Global.Config.JMDCompression, Deflater.NO_COMPRESSION), Deflater.BEST_COMPRESSION); if (!JMDForm.DoCompressionDlg(ref t, ref c, 1, 6, Deflater.NO_COMPRESSION, Deflater.BEST_COMPRESSION, hwnd)) return null; Global.Config.JMDThreads = ret.numthreads = t; Global.Config.JMDCompression = ret.compressionlevel = c; return ret; }
static CodecToken DeSerializeFromByteArray(byte[] data) { var m = new MemoryStream(data, false); var b = new BinaryReader(m); Win32.AVICOMPRESSOPTIONS comprOptions = new Win32.AVICOMPRESSOPTIONS(); byte[] Format; byte[] Parms; try { comprOptions.fccType = b.ReadInt32(); comprOptions.fccHandler = b.ReadInt32(); comprOptions.dwKeyFrameEvery = b.ReadInt32(); comprOptions.dwQuality = b.ReadInt32(); comprOptions.dwBytesPerSecond = b.ReadInt32(); comprOptions.dwFlags = b.ReadInt32(); //comprOptions.lpFormat = b.ReadInt32(); comprOptions.cbFormat = b.ReadInt32(); //comprOptions.lpParms = b.ReadInt32(); comprOptions.cbParms = b.ReadInt32(); comprOptions.dwInterleaveEvery = b.ReadInt32(); Format = b.ReadBytes(comprOptions.cbFormat); Parms = b.ReadBytes(comprOptions.cbParms); } catch (IOException) { // ran off end of array most likely return null; } finally { b.Close(); } CodecToken ret = new CodecToken(); ret.comprOptions = comprOptions; ret.Format = Format; ret.Parms = Parms; ret.codec = Win32.decode_mmioFOURCC(comprOptions.fccHandler); return ret; }