private static uint GetAudioOutputNumber(IWMSyncReader Reader)

        {
            uint res = InvalidOuput;

            uint OutCounts = 0;

            Reader.GetOutputCount(out OutCounts);

            for (uint i = 0; i < OutCounts; i++)

            {
                IWMOutputMediaProps Props = null;

                Reader.GetOutputProps(i, out Props);

                Guid mt;

                Props.GetType(out mt);

                if (mt == MediaTypes.WMMEDIATYPE_Audio)

                {
                    res = i;

                    break;
                }
            }

            return(res);
        }
        private void TestProps()
        {
            IWMOutputMediaProps p;

            m_read.GetOutputProps(1, out p);
            Debug.Assert(p != null);
            m_read.SetOutputProps(1, p);
        }
示例#3
0
        public AudioDecoder(string path, Stream IO)
        {
            m_path = path;
            isValid(path);
            bool pfIsProtected;

            WMUtils.WMIsContentProtected(path, out pfIsProtected);
            if (pfIsProtected)
            {
                throw new Exception("DRM present");
            }
            WMUtils.WMCreateSyncReader(IntPtr.Zero, Rights.None, out m_syncReader);

            if (path == null)
            {
                m_IO            = IO != null ? IO : new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 0x10000);
                m_streamWrapper = new StreamWrapper(m_IO);
                m_syncReader.OpenStream(m_streamWrapper);
            }
            else
            {
                m_syncReader.Open(path);
            }
            var pProfile = (m_syncReader as IWMProfile);
            int dwStreamCount;

            pProfile.GetStreamCount(out dwStreamCount);
            for (int dwIndex = 0; dwIndex < dwStreamCount; dwIndex++)
            {
                IWMStreamConfig pConfig = null;
                pProfile.GetStream(dwIndex, out pConfig);
                try
                {
                    Guid guid;
                    pConfig.GetStreamType(out guid);
                    if (MediaType.Audio != guid)
                    {
                        continue;
                    }
                    short wStreamNum;
                    pConfig.GetStreamNumber(out wStreamNum);
                    int dwBitrate = -1;
                    pConfig.GetBitrate(out dwBitrate);
                    var pIWMMediaProps = pConfig as IWMMediaProps;
                    int cbType         = 0;
                    pIWMMediaProps.GetMediaType(null, ref cbType);
                    var pMediaType = new AMMediaType();
                    pMediaType.formatSize = cbType;
                    pIWMMediaProps.GetMediaType(pMediaType, ref cbType);
                    if (pMediaType.formatType != FormatType.WaveEx)
                    {
                        continue;
                    }
                    if (pMediaType.subType != MediaSubType.WMAudio_Lossless)
                    {
                        continue;
                    }
                    m_wStreamNum = wStreamNum;
                    pcm          = WaveFormatExtensible.FromMediaType(pMediaType).GetConfig();
                    break;
                }
                finally
                {
                    Marshal.ReleaseComObject(pConfig);
                }
            }
            if (m_wStreamNum == -1)
            {
                throw new Exception("No WMA lossless streams found");
            }

            m_syncReader.SetReadStreamSamples(m_wStreamNum, false);
            bool pfCompressed;

            m_syncReader.GetReadStreamSamples(m_wStreamNum, out pfCompressed);
            if (pfCompressed)
            {
                throw new Exception("doesn't decompress");
            }
            m_syncReader.GetOutputNumberForStream(m_wStreamNum, out m_dwAudioOutputNum);
            IWMOutputMediaProps pProps;

            m_syncReader.GetOutputProps(m_dwAudioOutputNum, out pProps);

            try
            {
                StringBuilder sName      = null;
                AMMediaType   pMediaType = null;
                int           cbType     = 0;

                cbType     = 0;
                pMediaType = null;
                pProps.GetMediaType(pMediaType, ref cbType);

                // Get the name of the output we'll be using
                sName = null;
                short iName = 0;
                pProps.GetConnectionName(sName, ref iName);

                sName = new StringBuilder(iName);
                pProps.GetConnectionName(sName, ref iName);

                if (pcm.ChannelCount > 2)
                {
                    m_syncReader.SetOutputSetting(m_dwAudioOutputNum, Constants.g_wszEnableDiscreteOutput, AttrDataType.BOOL, new byte[] { 1, 0, 0, 0 }, 4);
                    m_syncReader.SetOutputSetting(m_dwAudioOutputNum, Constants.g_wszSpeakerConfig, AttrDataType.DWORD, new byte[] { 0, 0, 0, 0 }, 4);
                }

                pMediaType            = new AMMediaType();
                pMediaType.formatSize = cbType - Marshal.SizeOf(typeof(AMMediaType));

                //
                // Get the value for MediaType
                //
                pProps.GetMediaType(pMediaType, ref cbType);

                try
                {
                    if (MediaType.Audio != pMediaType.majorType)
                    {
                        throw new Exception("not Audio");
                    }
                    if (FormatType.WaveEx != pMediaType.formatType)
                    {
                        throw new Exception("not WaveEx");
                    }
                    var wfe = new WaveFormatExtensible(pcm);
                    Marshal.FreeCoTaskMem(pMediaType.formatPtr);
                    pMediaType.formatPtr  = IntPtr.Zero;
                    pMediaType.formatSize = 0;
                    pMediaType.formatPtr  = Marshal.AllocCoTaskMem(Marshal.SizeOf(wfe));
                    pMediaType.formatSize = Marshal.SizeOf(wfe);
                    Marshal.StructureToPtr(wfe, pMediaType.formatPtr, false);
                    pProps.SetMediaType(pMediaType);
                    m_syncReader.SetOutputProps(m_dwAudioOutputNum, pProps);
                }
                finally
                {
                    WMUtils.FreeWMMediaType(pMediaType);
                }
            }
            finally
            {
                Marshal.ReleaseComObject(pProps);
            }

            //try
            //{
            //    AttrDataType wmtType;
            //    short cbLength = 0;
            //    short wAnyStream = 0;
            //    var pHeaderInfo = m_syncReader as IWMHeaderInfo;
            //    pHeaderInfo.GetAttributeByName(ref wAnyStream, Constants.g_wszWMDuration, out wmtType, null, ref cbLength);
            //    var pbValue = new byte[cbLength];
            //    pHeaderInfo.GetAttributeByName(ref wAnyStream, Constants.g_wszWMDuration, out wmtType, pbValue, ref cbLength);
            //    var m_cnsFileDuration = BitConverter.ToInt64(pbValue, 0);
            //    _sampleCount = m_cnsFileDuration * m_pWfx.nSamplesPerSec / 10000000;
            //    // NOT ACCURATE ENOUGH (~1ms precision observed)
            //}
            //catch (COMException)
            //{
            //}

            //try
            //{
            //    var pHeaderInfo = m_syncReader as IWMHeaderInfo2;
            //    int nCodec;
            //    pHeaderInfo.GetCodecInfoCount(out nCodec);
            //    for (int wIndex = 0; wIndex < nCodec; wIndex++)
            //    {
            //        CodecInfoType enumCodecType;
            //        short cchName = 0;
            //        short cchDescription = 0;
            //        short cbCodecInfo = 0;
            //        pHeaderInfo.GetCodecInfo(wIndex, ref cchName, null,
            //            ref cchDescription, null, out enumCodecType,
            //            ref cbCodecInfo, null);
            //        var pwszName = new StringBuilder(cchName);
            //        var pwszDescription = new StringBuilder(cchDescription);
            //        var pbCodecInfo = new byte[cbCodecInfo];
            //        pHeaderInfo.GetCodecInfo(wIndex, ref cchName, pwszName,
            //            ref cchDescription, pwszDescription, out enumCodecType,
            //            ref cbCodecInfo, pbCodecInfo);
            //        if (enumCodecType == CodecInfoType.Audio)
            //        {
            //            // pbCodecInfo = {99,1} ??/
            //        }
            //    }
            //}
            //catch (COMException)
            //{
            //}

            //int cbMax;
            //m_syncReader.GetMaxOutputSampleSize(m_dwAudioOutputNum, out cbMax);
        }
        public Metadata GetMetadata(object target)
        {
            if (target == null || !(target is string))
            {
                return(null);
            }

            string filename = target.ToString();

            IWMMetadataEditor2 metadataEditor = null;

            IWMSyncReader syncReader = null;

            WMMetadata metadata = new WMMetadata();

            try
            {
                WMFSDKFunctions.WMCreateEditor(out metadataEditor);
                metadataEditor.OpenEx(filename, FILE_ACCESS.GENERIC_READ, FILE_SHARE.FILE_SHARE_NONE);

                IWMHeaderInfo3 header = (IWMHeaderInfo3)metadataEditor;

                ushort attributeCount;
                header.GetAttributeCountEx(StreamNumber, out attributeCount);

                for (int i = 0; i < attributeCount; i++)
                {
                    MetadataField metadataField = GetAttributeByIndex(header, i);

                    metadata.AddMetadataField(metadataField);
                }
            }
            catch (COMException ex)
            {
                // TODO: Logging
            }
            finally
            {
                if (metadataEditor != null)
                {
                    metadataEditor.Close();
                    Marshal.FinalReleaseComObject(metadataEditor);
                    metadataEditor = null;
                }
            }

            try
            {
                WMFSDKFunctions.WMCreateSyncReader(IntPtr.Zero, WMT_RIGHTS.WMT_RIGHT_PLAYBACK, out syncReader);
                syncReader.Open(filename);

                int outputCount;
                syncReader.GetOutputCount(out outputCount);

                IWMOutputMediaProps outputMediaProps = null;

                for (uint i = 0; i < outputCount; i++)
                {
                    IWMOutputMediaProps innerOutputMediaProps;
                    syncReader.GetOutputProps(i, out innerOutputMediaProps);

                    Guid type;
                    innerOutputMediaProps.GetType(out type);

                    if (type == WMFSDKFunctions.WMMEDIATYPE_Video)
                    {
                        outputMediaProps = innerOutputMediaProps;
                        break;
                    }
                }

                if (outputMediaProps != null)
                {
                    int pcbType = 0;
                    outputMediaProps.GetMediaType(IntPtr.Zero, ref pcbType);

                    IntPtr mediaTypeBufferPtr = Marshal.AllocHGlobal(pcbType);

                    outputMediaProps.GetMediaType(mediaTypeBufferPtr, ref pcbType);

                    WM_MEDIA_TYPE     mediaType       = new WM_MEDIA_TYPE();
                    WMVIDEOINFOHEADER videoInfoHeader = new WMVIDEOINFOHEADER();
                    Marshal.PtrToStructure(mediaTypeBufferPtr, mediaType);

                    Marshal.FreeHGlobal(mediaTypeBufferPtr);
                    Marshal.PtrToStructure(mediaType.pbFormat, videoInfoHeader);

                    double frameRate = Math.Round((double)10000000 / videoInfoHeader.AvgTimePerFrame, 2);

                    metadata.AddMetadataField(new MetadataField("FrameRate", frameRate));
                }
            }
            catch (COMException ex)
            {
                // TODO: Logging
            }
            finally
            {
                if (syncReader != null)
                {
                    syncReader.Close();
                    Marshal.FinalReleaseComObject(syncReader);
                    syncReader = null;
                }
            }

            return(metadata);
        }
示例#5
0
 private static uint GetAudioOutputNumber(IWMSyncReader Reader)
 {
     uint res = InvalidOuput;
     uint OutCounts = 0;
     Reader.GetOutputCount(out OutCounts);
     for (uint i = 0; i < OutCounts; i++)
     {
         IWMOutputMediaProps Props = null;
         Reader.GetOutputProps(i, out Props);
         Guid mt;
         Props.GetType(out mt);
         if (mt == MediaTypes.WMMEDIATYPE_Audio)
         {
             res = i;
             break;
         }
     }
     return res;
 }