Beispiel #1
0
        public bool FindAudioInfo(ref WM_MEDIA_TYPE mediaType, ref WaveFormat waveFormat)
        {
            bool success = false;
            IWMStreamConfig stream = null;
            IWMMediaProps props = null;
            Guid mediaTypeGuid;

            IWMProfile profile = (IWMProfile)_reader;

            for (uint i = 0; i < _readerStreamCount; i++)
            {
                profile.GetStream(i, out stream);

                props = (IWMMediaProps)stream;

                WMMediaProps mediaProps = new WMMediaProps(props);

                mediaType = mediaProps.MediaType;
                mediaTypeGuid = mediaType.majortype;

                if (mediaTypeGuid == MediaTypes.WMMEDIATYPE_Audio)
                {
                    Logger.WriteLogMessage("Found audio stream [" + i + "], format type [" + mediaType.formattype + "].");

                    waveFormat = (WaveFormat)Marshal.PtrToStructure(mediaType.pbFormat, typeof(WaveFormat));
                    success = true;
                    break;
                }
            }

            return success;
        }
Beispiel #2
0
        public void FindAudioOutputFormat(uint outputNum, ref WM_MEDIA_TYPE mediaType, ref Guid subtype, ref WaveFormat waveFormat)
        {
            IWMOutputMediaProps readerOutputProps = null;
            uint bufferSize = (uint)(Marshal.SizeOf(typeof(WM_MEDIA_TYPE)) + Marshal.SizeOf(typeof(WaveFormat)));
            uint formatCount;

            Logger.WriteLogMessage("Finding audio output formats for reader, output [" + outputNum + "].");

            _reader.GetOutputFormatCount(outputNum, out formatCount);

            Logger.WriteLogMessage("Reader can produce " + formatCount + " possible audio output formats.");

            IntPtr buffer = Marshal.AllocCoTaskMem((int)bufferSize);

            try
            {
                for (uint j = 0; j < formatCount; j++)
                {
                    uint size = 0;

                    _reader.GetOutputFormat(outputNum, j, out readerOutputProps);

                    readerOutputProps.GetMediaType(IntPtr.Zero, ref size);

                    if (size > bufferSize)
                    {
                        bufferSize = size;
                        Marshal.FreeCoTaskMem(buffer);
                        buffer = Marshal.AllocCoTaskMem((int)bufferSize);
                    }

                    readerOutputProps.GetMediaType(buffer, ref size);

                    mediaType = (WM_MEDIA_TYPE)Marshal.PtrToStructure(buffer, typeof(WM_MEDIA_TYPE));

                    if (mediaType.formattype == FormatTypes.WMFORMAT_WaveFormatEx)
                    {
                        Logger.WriteLogMessage("Walking output format [" + j + "], format type [" + GetFormatTypeName(mediaType.formattype) + "], subtype [" + GetSubTypeName(mediaType.subtype) + "], sample size [" + mediaType.lSampleSize + "].");

                        //
                        // NOTE: only look for PCM subtypes
                        //
                        if (mediaType.subtype == MediaSubTypes.WMMEDIASUBTYPE_PCM)
                        {
                            subtype = mediaType.subtype;

                            Logger.WriteLogMessage("- Found PCM sub type, grabbing WaveFormat.");

                            waveFormat = (WaveFormat)Marshal.PtrToStructure(mediaType.pbFormat, typeof(WaveFormat));
                            WaveFormats format = (WaveFormats)waveFormat.wFormatTag;

                            Logger.WriteLogMessage("- format [" + format + "], sample rate [" + waveFormat.nSamplesPerSec + "], bits per sample [" + waveFormat.wBitsPerSample +
                                "], bytes/sec [" + waveFormat.nAvgBytesPerSec + "], channels [" + waveFormat.nChannels + "].");

                            _reader.SetOutputProps(outputNum, readerOutputProps);
                            break;
                        }
                    }
                }
            }
            finally
            {
                Marshal.FreeCoTaskMem(buffer);
            }

            Marshal.ReleaseComObject(readerOutputProps);
        }
Beispiel #3
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;
        }