public void PopulateWAVEFORMATEX(ref Win32.WAVEFORMATEX wfex) { int bytes = 0; if (a_bits == 16) { bytes = 2; } else if (a_bits == 8) { bytes = 1; } else { throw new InvalidOperationException("only 8/16 bits audio are supported by AviWriter and you chose: " + a_bits); } if (a_channels == 1) { } else if (a_channels == 2) { } else { throw new InvalidOperationException("only 1/2 channels audio are supported by AviWriter and you chose: " + a_channels); } wfex.Init(); wfex.nBlockAlign = (ushort)(bytes * a_channels); wfex.nChannels = (ushort)a_channels; wfex.wBitsPerSample = (ushort)a_bits; wfex.wFormatTag = Win32.WAVE_FORMAT_PCM; wfex.nSamplesPerSec = (uint)a_samplerate; wfex.nAvgBytesPerSec = (uint)(wfex.nBlockAlign * a_samplerate); }
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; }
/// <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"); } }
/// <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 if (Win32.FAILED(Win32.AVIMakeCompressedStream(out pAviCompressedVideoStream, pAviRawVideoStream, ref currVideoCodecToken.comprOptions, IntPtr.Zero))) { 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 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; }