Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        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);
        }