public AsfImageLoader(AsfFile file) { stream = new AsfStream(file, AsfStreamType.asfImage, 0); WMUtils.WMCreateSyncReader(IntPtr.Zero, Rights.Playback, out syncReader); syncReader.OpenStream(new AsfIStream(stream)); syncReader.SetReadStreamSamples((short)stream.Configuration.AsfVideoStreamId, false); Width = stream.Configuration.ImageWidth; Height = stream.Configuration.ImageHeight; }
private void Config() { IWMSyncReader read; m_Status = 0; WMUtils.WMCreateSyncReader(IntPtr.Zero, Rights.Playback, out read); m_read2 = read as IWMSyncReader2; }
private void Config() { DoIndex(IndexerType.FrameNumbers); IWMSyncReader read; WMUtils.WMCreateSyncReader(IntPtr.Zero, Rights.Playback, out read); m_read = read as IWMSyncReader2; m_read.Open(sFileName); }
/// <summary> /// Get the bitmap image /// </summary> public Bitmap GetImage() { if (_sampleBitmap == null) { try { _asfMemoryStream = new AsfIStream(_asfStream); IWMSyncReader syncReader; WMUtils.WMCreateSyncReader(IntPtr.Zero, Rights.Playback, out syncReader); syncReader.OpenStream(_asfMemoryStream); short videoStreamNum = (short)_asfStream.Configuration.AsfVideoStreamId; syncReader.SetReadStreamSamples(videoStreamNum, false); long cnsSampleTime; long cnsSampleDuration; SampleFlag dwFlags; INSSBuffer pSample; int dwOutputNum = 0; short dwStreamNum = 0; bool isBitmapCreated = false; while (!isBitmapCreated) { syncReader.GetNextSample(0, out pSample, out cnsSampleTime, out cnsSampleDuration, out dwFlags, out dwOutputNum, out dwStreamNum); if ((dwFlags & SampleFlag.CleanPoint) == SampleFlag.CleanPoint) { //Get the bitmap from the frame IntPtr pBuffer; int bufferLength; pSample.GetBufferAndLength(out pBuffer, out bufferLength); byte[] sampleData = new byte[bufferLength]; Marshal.Copy(pBuffer, sampleData, 0, bufferLength); _sampleBitmap = CopyDataToBitmap(sampleData, _asfStream.Configuration.ImageWidth, _asfStream.Configuration.ImageHeight); isBitmapCreated = true; } Marshal.FinalReleaseComObject(pSample); } Marshal.FinalReleaseComObject(syncReader); } catch (Exception) //catch and ignore, returned bitmap will be null { } finally { Dispose(); } } return(_sampleBitmap); }
private void Config() { IWMSyncReader reader; WMUtils.WMCreateSyncReader(IntPtr.Zero, Rights.Playback, out reader); readerTimecode = (IWMReaderTimecode)reader; reader.Open(@"C:\so_lesson3c.wmv"); int outputs = -1; reader.GetOutputCount(out outputs); Debug.Assert(outputs > 0); }
private void Config() { IWMIndexer pIndex; WMUtils.WMCreateIndexer(out pIndex); IWMIndexer2 pIndex2 = pIndex as IWMIndexer2; pIndex2.Configure(0, IndexerType.FrameNumbers, null, null); pIndex2.StartIndexing(sFileName, this, IntPtr.Zero); while (!m_IndexComplete) { System.Threading.Thread.Sleep(0); } WMUtils.WMCreateSyncReader(IntPtr.Zero, Rights.Playback, out m_read); }
/// <summary> /// Get audio sample bytes from the underlying stream /// </summary> public byte[] GetSampleBytes(int maxSampleCount) { _asfMemoryStream = new AsfIStream(_asfStream); WMUtils.WMCreateSyncReader(IntPtr.Zero, Rights.Playback, out _syncReader); _syncReader.OpenStream(_asfMemoryStream); short audioStreamNum = (short)_asfStream.Configuration.AsfAudioStreamId; _syncReader.SetReadStreamSamples(audioStreamNum, false); long cnsSampleTime; long cnsSampleDuration; SampleFlag dwFlags; INSSBuffer pSample; int dwOutputNum = 0; short dwStreamNum = 0; List <byte> sampleList = new List <byte>(); long samplesRead = 0; int sampleSize = _asfStream.Configuration.AudioBitsPerSample / 8; try { while (samplesRead < maxSampleCount) { _syncReader.GetNextSample(audioStreamNum, out pSample, out cnsSampleTime, out cnsSampleDuration, out dwFlags, out dwOutputNum, out dwStreamNum); IntPtr pBuffer; int bufferLength; pSample.GetBufferAndLength(out pBuffer, out bufferLength); byte[] sampleData = new byte[bufferLength]; Marshal.Copy(pBuffer, sampleData, 0, bufferLength); Marshal.FinalReleaseComObject(pSample); samplesRead += sampleData.Length / (sampleSize * _asfStream.Configuration.AudioChannels); sampleList.AddRange(sampleData); } } catch (COMException) // no more samples or corrupted content { } return(sampleList.Take(maxSampleCount).ToArray()); }
/// <summary> /// Write the PCM audio data to a stream /// </summary> public void WriteTo(Stream stream) { _asfMemoryStream = new AsfIStream(_asfStream); WMUtils.WMCreateSyncReader(IntPtr.Zero, Rights.Playback, out _syncReader); _syncReader.OpenStream(_asfMemoryStream); short audioStreamNum = (short)_asfStream.Configuration.AsfAudioStreamId; _syncReader.SetReadStreamSamples(audioStreamNum, false); long cnsSampleTime; long cnsSampleDuration; SampleFlag dwFlags; INSSBuffer pSample; int dwOutputNum = 0; short dwStreamNum = 0; try { while (true) { _syncReader.GetNextSample(audioStreamNum, out pSample, out cnsSampleTime, out cnsSampleDuration, out dwFlags, out dwOutputNum, out dwStreamNum); IntPtr pBuffer; int bufferLength; pSample.GetBufferAndLength(out pBuffer, out bufferLength); byte[] sampleData = new byte[bufferLength]; Marshal.Copy(pBuffer, sampleData, 0, bufferLength); Marshal.FinalReleaseComObject(pSample); stream.Write(sampleData, 0, sampleData.Length); } } catch (COMException) // no more samples or corrupted content { } }
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 IEnumerable <AudioSample> GetSamples(int maxSampleCount = 0) { _asfMemoryStream = new AsfIStream(_asfStream); WMUtils.WMCreateSyncReader(IntPtr.Zero, Rights.Playback, out _syncReader); _syncReader.OpenStream(_asfMemoryStream); short audioStreamNum = (short)_asfStream.Configuration.AsfAudioStreamId; _syncReader.SetReadStreamSamples(audioStreamNum, false); long cnsSampleTime; long cnsSampleDuration; SampleFlag dwFlags; INSSBuffer pSample; int dwOutputNum = 0; short dwStreamNum = 0; int totalSampleCount = 0; while (true) { if (_sampleBuffer.Count > 0) { yield return(_sampleBuffer.Dequeue()); } else { if (totalSampleCount >= maxSampleCount) { yield break; } try { _syncReader.GetNextSample(audioStreamNum, out pSample, out cnsSampleTime, out cnsSampleDuration, out dwFlags, out dwOutputNum, out dwStreamNum); IntPtr pBuffer; int bufferLength; pSample.GetBufferAndLength(out pBuffer, out bufferLength); byte[] sampleData = new byte[bufferLength]; Marshal.Copy(pBuffer, sampleData, 0, bufferLength); Marshal.FinalReleaseComObject(pSample); float sample = 0; float[] samples = new float[_asfStream.Configuration.AudioChannels]; int fullSampleSize = _asfStream.Configuration.AudioChannels * (_asfStream.Configuration.AudioBitsPerSample / 8); int takeSamplesPerSec = 10000; int sampleStep = (int)(fullSampleSize * (_asfStream.Configuration.AudioSampleRate / takeSamplesPerSec)); for (int sampleOffset = 0; sampleOffset < sampleData.Length; sampleOffset += sampleStep) { for (int i = 0; i < _asfStream.Configuration.AudioChannels; i++) { if (_asfStream.Configuration.AudioBitsPerSample == 16) { sample = BitConverter.ToInt16(sampleData, sampleOffset + i * 2); sample = sample / Int16.MaxValue; } else if (_asfStream.Configuration.AudioBitsPerSample == 32) { sample = BitConverter.ToInt32(sampleData, sampleOffset + i * 4); sample = sample / Int32.MaxValue; } samples[i] = sample; } totalSampleCount++; if (maxSampleCount == 0 || totalSampleCount <= maxSampleCount) { _sampleBuffer.Enqueue(new AudioSample(samples)); } else { continue; } } } catch (COMException) // no more samples or corrupted content { totalSampleCount = maxSampleCount; } } } }