/// <summary> /// Configure audio media type. /// </summary> public bool ConfigAudio(_WMMediaType mt) { if (audioProps == null) { Debug.WriteLine("Failed to configure audio: properties is null."); return(false); } try { audioProps.SetMediaType(ref mt); writer.SetInputProps(audioInput, audioProps); } catch (Exception e) { eventLog.WriteEntry("Failed to set audio properties: " + e.ToString(), EventLogEntryType.Error, 1000); Debug.WriteLine("Failed to set audio properties: " + e.ToString()); return(false); } return(true); }
/// <summary> /// Read the properties of the first bitmap to finish initializing the writer. /// </summary> /// <param name="hBitmap">First bitmap</param> private void Initialize(Bitmap hBitmap) { AMMediaType mt = new AMMediaType(); VideoInfoHeader videoInfo = new VideoInfoHeader(); // Create the VideoInfoHeader using info from the bitmap videoInfo.BmiHeader.Size = Marshal.SizeOf(typeof(BitmapInfoHeader)); videoInfo.BmiHeader.Width = hBitmap.Width; videoInfo.BmiHeader.Height = hBitmap.Height; videoInfo.BmiHeader.Planes = 1; // compression thru clrimportant don't seem to be used. Init them anyway videoInfo.BmiHeader.Compression = 0; videoInfo.BmiHeader.ImageSize = 0; videoInfo.BmiHeader.XPelsPerMeter = 0; videoInfo.BmiHeader.YPelsPerMeter = 0; videoInfo.BmiHeader.ClrUsed = 0; videoInfo.BmiHeader.ClrImportant = 0; switch (hBitmap.PixelFormat) { case PixelFormat.Format32bppRgb: mt.subType = MediaSubType.RGB32; videoInfo.BmiHeader.BitCount = 32; break; case PixelFormat.Format24bppRgb: mt.subType = MediaSubType.RGB24; videoInfo.BmiHeader.BitCount = 24; break; case PixelFormat.Format16bppRgb555: mt.subType = MediaSubType.RGB555; videoInfo.BmiHeader.BitCount = 16; break; default: throw new Exception("Unrecognized Pixelformat in bitmap"); } videoInfo.SrcRect = new Rectangle(0, 0, hBitmap.Width, hBitmap.Height); videoInfo.TargetRect = videoInfo.SrcRect; videoInfo.BmiHeader.ImageSize = hBitmap.Width * hBitmap.Height * (videoInfo.BmiHeader.BitCount / 8); videoInfo.BitRate = videoInfo.BmiHeader.ImageSize * m_iFrameRate; videoInfo.BitErrorRate = 0; videoInfo.AvgTimePerFrame = 10000 * 1000 / m_iFrameRate; mt.majorType = MediaType.Video; mt.fixedSizeSamples = true; mt.temporalCompression = false; mt.sampleSize = videoInfo.BmiHeader.ImageSize; mt.formatType = FormatType.VideoInfo; mt.unkPtr = IntPtr.Zero; mt.formatSize = Marshal.SizeOf(typeof(VideoInfoHeader)); // Lock the videoInfo structure, and put the pointer // into the mediatype structure GCHandle gHan = GCHandle.Alloc(videoInfo, GCHandleType.Pinned); try { // Set the inputprops using the structures mt.formatPtr = gHan.AddrOfPinnedObject(); m_pInputProps.SetMediaType(mt); } finally { gHan.Free(); mt.formatPtr = IntPtr.Zero; } // Now take the inputprops, and set them on the file writer m_pWMWriter.SetInputProps(m_dwVideoInput, m_pInputProps); // Done with config, prepare to write m_pWMWriter.BeginWriting(); m_Init = true; }
public bool FindAudioInputFormat(uint inputNum, Guid subtype, WaveFormat readerWaveFormat) { bool success = false; IWMInputMediaProps writerInputProps = null; WM_MEDIA_TYPE mediaType; uint bufferSize = (uint)(Marshal.SizeOf(typeof(WM_MEDIA_TYPE)) + Marshal.SizeOf(typeof(WaveFormat))); uint formatCount; Logger.WriteLogMessage("Finding audio input formats for writer, input [" + inputNum + "]."); _writer.GetInputFormatCount(inputNum, out formatCount); Logger.WriteLogMessage("Audio writer can consume " + formatCount + " possible audio input formats."); IntPtr buffer = Marshal.AllocCoTaskMem((int)bufferSize); try { for (uint j = 0; j < formatCount; j++) { uint size = 0; try { _writer.GetInputFormat(inputNum, j, out writerInputProps); writerInputProps.GetMediaType(IntPtr.Zero, ref size); if (size > bufferSize) { bufferSize = size; Marshal.FreeCoTaskMem(buffer); buffer = Marshal.AllocCoTaskMem((int)bufferSize); } writerInputProps.GetMediaType(buffer, ref size); mediaType = (WM_MEDIA_TYPE)Marshal.PtrToStructure(buffer, typeof(WM_MEDIA_TYPE)); if (mediaType.formattype == FormatTypes.WMFORMAT_WaveFormatEx) { Logger.WriteLogMessage("Found writer audio input format [" + j + "], format type [" + GetFormatTypeName(mediaType.formattype) + "], subtype [" + GetSubTypeName(mediaType.subtype) + "], sample size [" + mediaType.lSampleSize + "]."); WaveFormat waveFormat = (WaveFormat)Marshal.PtrToStructure(mediaType.pbFormat, typeof(WaveFormat)); WaveFormats format = (WaveFormats)waveFormat.wFormatTag; Logger.WriteLogMessage("Found audio stream, format [" + format + "], sample rate [" + waveFormat.nSamplesPerSec + "], bits per sample [" + waveFormat.wBitsPerSample + "], bytes/sec [" + waveFormat.nAvgBytesPerSec + "], channels [" + waveFormat.nChannels + "]."); if (waveFormat.nSamplesPerSec == readerWaveFormat.nSamplesPerSec && waveFormat.nChannels == readerWaveFormat.nChannels && waveFormat.wBitsPerSample == readerWaveFormat.wBitsPerSample && waveFormat.wFormatTag == readerWaveFormat.wFormatTag) { writerInputProps.SetMediaType(ref mediaType); _writer.SetInputProps(inputNum, writerInputProps); success = true; break; } } } catch (Exception) { // error handle throw; } finally { Marshal.ReleaseComObject(writerInputProps); writerInputProps = null; } } } catch (Exception) { // error handle throw; } finally { Marshal.FreeCoTaskMem(buffer); } return(success); }
public bool FindVideoInputFormat(uint inputNum, Guid subtype, ref VideoInfoHeader inputVideoInfoHeader, bool enableCompressedSamples) { bool success = false; IWMInputMediaProps writerInputProps = null; WM_MEDIA_TYPE mediaType; uint bufferSize = (uint)(Marshal.SizeOf(typeof(WM_MEDIA_TYPE)) + Marshal.SizeOf(typeof(VideoInfoHeader))); uint formatCount; Logger.WriteLogMessage("Finding video input formats for writer, input [" + inputNum + "]."); _writer.GetInputFormatCount(inputNum, out formatCount); Logger.WriteLogMessage("Video writer can consume " + formatCount + " possible video input formats."); IntPtr buffer = Marshal.AllocCoTaskMem((int)bufferSize); try { for (uint j = 0; j < formatCount; j++) { uint size = 0; try { _writer.GetInputFormat(inputNum, j, out writerInputProps); writerInputProps.GetMediaType(IntPtr.Zero, ref size); if (size > bufferSize) { bufferSize = size; Marshal.FreeCoTaskMem(buffer); buffer = Marshal.AllocCoTaskMem((int)bufferSize); } writerInputProps.GetMediaType(buffer, ref size); mediaType = (WM_MEDIA_TYPE)Marshal.PtrToStructure(buffer, typeof(WM_MEDIA_TYPE)); if (mediaType.formattype == FormatTypes.WMFORMAT_VideoInfo) { Logger.WriteLogMessage("Found video writer input format [" + j + "], format type [" + GetFormatTypeName(mediaType.formattype) + "], subtype [" + GetSubTypeName(mediaType.subtype) + "], sample size [" + mediaType.lSampleSize + "]."); inputVideoInfoHeader = (VideoInfoHeader)Marshal.PtrToStructure(mediaType.pbFormat, typeof(VideoInfoHeader)); Logger.WriteLogMessage("Found input video stream, width [" + inputVideoInfoHeader.bmiHeader.biWidth + "], height [" + inputVideoInfoHeader.bmiHeader.biHeight + "], bit count [" + inputVideoInfoHeader.bmiHeader.biBitCount + "], image size [" + inputVideoInfoHeader.bmiHeader.biSizeImage + "]."); if (mediaType.subtype == subtype) { writerInputProps.SetMediaType(ref mediaType); if (!enableCompressedSamples) { _writer.SetInputProps(inputNum, writerInputProps); } else { _writer.SetInputProps(inputNum, null); } success = true; break; } } } catch (Exception) { // error handle throw; } finally { Marshal.ReleaseComObject(writerInputProps); writerInputProps = null; } } } catch (Exception) { // error handle throw; } finally { Marshal.FreeCoTaskMem(buffer); } return(success); }