private static WaveFormat GetOutputFormat(IWMSyncReader reader, uint outputNumber, uint formatNumber) { IWMOutputMediaProps Props = null; int size = 0; WaveFormat fmt = null; reader.GetOutputFormat(outputNumber, formatNumber, out Props); Props.GetMediaType(IntPtr.Zero, ref size); IntPtr buffer = Marshal.AllocCoTaskMem(Math.Max(size, Marshal.SizeOf(typeof(WM_MEDIA_TYPE)) + Marshal.SizeOf(typeof(WaveFormat)))); try { Props.GetMediaType(buffer, ref size); var mt = (WM_MEDIA_TYPE)Marshal.PtrToStructure(buffer, typeof(WM_MEDIA_TYPE)); if ((mt.majortype == MediaTypes.WMMEDIATYPE_Audio) && (mt.subtype == MediaTypes.WMMEDIASUBTYPE_PCM) && (mt.formattype == MediaTypes.WMFORMAT_WaveFormatEx) && (mt.cbFormat >= WAVE_FORMAT_EX_SIZE)) { fmt = new WaveFormat(44100, 16, 2); Marshal.PtrToStructure(mt.pbFormat, fmt); } else { throw new ArgumentException(string.Format("The format {0} of the output {1} is not a valid PCM format", formatNumber, outputNumber)); } } finally { Marshal.FreeCoTaskMem(buffer); } return(fmt); }
public void CanOpenWmaFile() { IWMSyncReader reader = OpenWmaFile(); reader.Close(); Marshal.ReleaseComObject(reader); }
/// <summary> /// Wraps the WMCreateSyncReader fucntion /// </summary> /// <param name="Rights">Indicates the desired operation. See WMF SDK documentation</param> /// <returns>The reader object</returns> public static IWMSyncReader CreateSyncReader(WMT_RIGHTS Rights) { IWMSyncReader res = null; Marshal.ThrowExceptionForHR(WMCreateSyncReader(IntPtr.Zero, Rights, out res)); return(res); }
/// <summary> /// Create WmaStream with specific format for for uncompressed audio data. /// </summary> /// <param name="FileName">Name of asf file</param> /// <param name="OutputFormat">WaveFormat that define the desired audio data format</param> public WmaStream(string FileName, WaveFormat OutputFormat) { m_Reader = WM.CreateSyncReader(WMT_RIGHTS.WMT_RIGHT_NO_DRM); try { m_Reader.Open(FileName); Init(OutputFormat); } catch { try { m_Reader.Close(); } catch {} m_Reader = null; throw; } }
public void CanGetNextSample() { IWMSyncReader reader = OpenWmaFile(); INSSBuffer buffer; long sampleTime; long duration; uint flags; uint outputNum; ushort streamNum; uint hresult = reader.GetNextSample(0, out buffer, out sampleTime, out duration, out flags, out outputNum, out streamNum); Assert.AreEqual(0, hresult); //byte[] theBuffer; //buffer.GetBuffer(out theBuffer); hresult = reader.Close(); Assert.AreEqual(0, hresult); Marshal.ReleaseComObject(reader); /* * //char[] nameBuffer = new char[256]; * ushort length = 256; * string name = new string(' ', 256); * props.GetConnectionName(name, ref length); * //string name = new string(nameBuffer, 0, length); * Assert.IsFalse(String.IsNullOrEmpty(name));*/ }
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); }
protected override void Dispose(bool disposing) { if (!_disposed) { if (disposing) { if (_reader != null) { try { _reader.Close(); Logger.WriteLogMessage("Disposed IWMSyncReader."); } catch (Exception) { // // eat exception // } _reader = null; } Logger.WriteLogMessage("Completed dispose."); } } base.Dispose(disposing); }
public void CanOpenWmaFile() { IWMSyncReader reader = OpenWmaFile(); uint hresult = reader.Close(); Assert.AreEqual(0, hresult); Marshal.ReleaseComObject(reader); }
public override void Close() { if (m_Reader != null) { m_Reader.Close(); m_Reader = null; } base.Close(); }
public override void Close() { if (m_Reader != null) { m_Reader.Close(); Marshal.ReleaseComObject(m_Reader); m_Reader = null; } base.Close(); }
public void CanGetOutputFormat() { IWMSyncReader reader = OpenWmaFile(); IWMOutputMediaProps props; reader.GetOutputFormat(0, 0, out props); Assert.IsNotNull(props); reader.Close(); Marshal.ReleaseComObject(reader); }
public void CanGetOutputCount() { IWMSyncReader reader = OpenWmaFile(); uint outputs; reader.GetOutputCount(out outputs); Assert.AreEqual(1, outputs, "Output Count"); reader.Close(); Marshal.ReleaseComObject(reader); }
private IWMSyncReader OpenWmaFile() { if (!File.Exists(testWmaFile)) { Assert.Ignore("Test WMA File Not Found"); } IWMSyncReader reader = WM.CreateSyncReader(WMT_RIGHTS.WMT_RIGHT_NO_DRM); Assert.IsNotNull(reader); reader.Open(testWmaFile); return(reader); }
public void Dispose() { if (syncReader != null) { Marshal.FinalReleaseComObject(syncReader); } syncReader = null; if (stream != null) { stream.Dispose(); } stream = null; }
private static int[] GetPCMOutputNumbers(IWMSyncReader Reader, uint OutputNumber) { var result = new List <int>(); uint FormatCount = 0; Reader.GetOutputFormatCount(OutputNumber, out FormatCount); int BufferSize = Marshal.SizeOf(typeof(WM_MEDIA_TYPE)) + Marshal.SizeOf(typeof(WaveFormat)); IntPtr buffer = Marshal.AllocCoTaskMem(BufferSize); try { for (int i = 0; i < FormatCount; i++) { IWMOutputMediaProps Props = null; int size = 0; WM_MEDIA_TYPE mt; Reader.GetOutputFormat(OutputNumber, (uint)i, out Props); Props.GetMediaType(IntPtr.Zero, ref size); if (size > BufferSize) { BufferSize = size; Marshal.FreeCoTaskMem(buffer); buffer = Marshal.AllocCoTaskMem(BufferSize); } Props.GetMediaType(buffer, ref size); mt = (WM_MEDIA_TYPE)Marshal.PtrToStructure(buffer, typeof(WM_MEDIA_TYPE)); if ((mt.majortype == MediaTypes.WMMEDIATYPE_Audio) && (mt.subtype == MediaTypes.WMMEDIASUBTYPE_PCM) && (mt.formattype == MediaTypes.WMFORMAT_WaveFormatEx) && (mt.cbFormat >= WAVE_FORMAT_EX_SIZE)) { result.Add(i); } } } finally { Marshal.FreeCoTaskMem(buffer); } return(result.ToArray()); }
/// <summary> /// Create WmaStream with specific format for for uncompressed audio data. /// </summary> /// <param name="inputStream">Name of asf stream</param> /// <param name="OutputFormat">WaveFormat that define the desired audio data format</param> public WmaStreamReader(Stream inputStream, WaveFormat OutputFormat) { m_Reader = WM.CreateSyncReader(WMT_RIGHTS.WMT_RIGHT_NO_DRM); try { m_Reader.OpenStream(new ComStreamWrapper(inputStream)); Init(OutputFormat); } catch { try { m_Reader.Close(); } finally { m_Reader = null; } throw; } }
public void Close() { //if (m_streamWrapper != null) // m_streamWrapper.Close(); if (m_IO != null) { m_IO.Close(); } if (m_pSample != null) { Marshal.ReleaseComObject(m_pSample); } if (m_syncReader != null) { m_syncReader.Close(); Marshal.ReleaseComObject(m_syncReader); } m_IO = null; m_pSample = null; m_syncReader = null; }
public static extern uint WMCreateSyncReader( [In] IntPtr pUnkCert, // must be set to null [In] WMT_RIGHTS dwRights, [Out] out IWMSyncReader ppSyncReader);
public WmvReader() { _reader = Helpers.CreateSyncReader(WMT_RIGHTS.WMT_RIGHT_NO_DRM); }
protected override void Dispose(bool disposing) { if (disposing) { if (m_reader != null) { m_reader.Close(); m_reader = null; } } }
private static int[] GetPCMOutputNumbers(IWMSyncReader Reader, uint OutputNumber) { var result = new List<int>(); uint FormatCount = 0; Reader.GetOutputFormatCount(OutputNumber, out FormatCount); int BufferSize = Marshal.SizeOf(typeof(WM_MEDIA_TYPE)) + Marshal.SizeOf(typeof(WaveFormat)); IntPtr buffer = Marshal.AllocCoTaskMem(BufferSize); try { for (int i = 0; i < FormatCount; i++) { IWMOutputMediaProps Props = null; int size = 0; WM_MEDIA_TYPE mt; Reader.GetOutputFormat(OutputNumber, (uint)i, out Props); Props.GetMediaType(IntPtr.Zero, ref size); if (size > BufferSize) { BufferSize = size; Marshal.FreeCoTaskMem(buffer); buffer = Marshal.AllocCoTaskMem(BufferSize); } Props.GetMediaType(buffer, ref size); mt = (WM_MEDIA_TYPE)Marshal.PtrToStructure(buffer, typeof(WM_MEDIA_TYPE)); if ((mt.majortype == MediaTypes.WMMEDIATYPE_Audio) && (mt.subtype == MediaTypes.WMMEDIASUBTYPE_PCM) && (mt.formattype == MediaTypes.WMFORMAT_WaveFormatEx) && (mt.cbFormat >= WAVE_FORMAT_EX_SIZE)) { result.Add(i); } } } finally { Marshal.FreeCoTaskMem(buffer); } return result.ToArray(); }
public static void CopyWmv(string inputFilePath, string outputFilePath, ulong startTime, long duration) { IWMWriterAdvanced writerAdvanced = null; IWMProfile profile = null; IWMStreamConfig streamConfig = null; uint streamCount = 0; uint inputCount = 0; ushort[] streamNumbers = null; WMT_STREAM_SELECTION[] streamSelections = null; ulong sampleTime, sampleDuration; uint flags, outputNum; ushort streamNum; INSSBuffer sample = null; IWMSyncReader reader = Helpers.CreateSyncReader(WMT_RIGHTS.WMT_RIGHT_NO_DRM); IWMWriter writer = Helpers.CreateWriter(); try { reader.Open(inputFilePath); Logger.WriteLogMessage("Opened file [" + inputFilePath + "] for reading."); profile = (IWMProfile)reader; profile.GetStreamCount(out streamCount); streamNumbers = new ushort[streamCount]; streamSelections = new WMT_STREAM_SELECTION[streamCount]; for (uint i = 0; i < streamCount; i++) { profile.GetStream(i, out streamConfig); streamConfig.GetStreamNumber(out streamNumbers[i]); streamSelections[i] = WMT_STREAM_SELECTION.WMT_ON; // // Read compressed samples // reader.SetReadStreamSamples(streamNumbers[i], true); } // // select all streams // reader.SetStreamsSelected((ushort)streamCount, streamNumbers, streamSelections); writer.SetProfile(profile); writer.GetInputCount(out inputCount); for (uint i = 0; i < inputCount; i++) { writer.SetInputProps(i, null); // write compressed samples } writer.SetOutputFilename(outputFilePath); Logger.WriteLogMessage("Set output filename [" + outputFilePath + "] for writing."); writerAdvanced = (IWMWriterAdvanced)writer; // Copy attributes avoided // Copy Codec Info avoided // Copy all scripts in the header avoided writer.BeginWriting(); // // startTime, duration are in 100-nsec ticks // reader.SetRange(startTime, duration); // seek Logger.WriteLogMessage("Set range on reader, startTime [" + startTime + "], duration [" + duration + "]."); for (uint streamsRead = 0; streamsRead < streamCount;) { try { streamNum = 0; reader.GetNextSample(0, out sample, out sampleTime, out sampleDuration, out flags, out outputNum, out streamNum); Logger.WriteLogMessage("Grabbed next video sample, sampleTime [" + sampleTime + "], duration [" + sampleDuration + "], flags [" + flags + "], outputNum [" + outputNum + "], streamNum [" + streamNum + "]."); writerAdvanced.WriteStreamSample(streamNum, sampleTime, 0, sampleDuration, flags, sample); Logger.WriteLogMessage("Wrote sample, sampleTime [" + sampleTime + "], duration [" + sampleDuration + "], flags [" + flags + "], outputNum [" + outputNum + "], streamNum [" + streamNum + "]."); } catch (COMException e) { if (e.ErrorCode == Constants.NS_E_NO_MORE_SAMPLES) { streamsRead++; } else { throw; } } } writer.EndWriting(); } finally { reader.Close(); } }
public override void Close() { if (m_Reader != null) { m_Reader.Close(); m_Reader = null; } base.Close (); }
public static extern int WMCreateSyncReader( IntPtr pUnkCert, Rights dwRights, out IWMSyncReader ppSyncReader);
public static extern uint WMCreateSyncReader( [In, MarshalAs(UnmanagedType.Interface)] object pUnkReserved, // Always null. [In] uint dwRights, [Out, MarshalAs(UnmanagedType.Interface)] out IWMSyncReader ppSyncReader);
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); }
public static extern WinErrorCode WMCreateSyncReader(IntPtr pUnkCert, WMT_RIGHTS dwRights, [Out, MarshalAs(UnmanagedType.Interface)] out IWMSyncReader ppSyncReader);
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 static extern int WMCreateSyncReader(IntPtr pUnkCert, WMT_RIGHTS dwRights, [Out, MarshalAs(UnmanagedType.Interface)] out IWMSyncReader ppSyncReader);
private static WaveFormat GetOutputFormat(IWMSyncReader reader, uint outputNumber, uint formatNumber) { IWMOutputMediaProps Props = null; int size = 0; WaveFormat fmt = null; reader.GetOutputFormat(outputNumber, formatNumber, out Props); Props.GetMediaType(IntPtr.Zero, ref size); IntPtr buffer = Marshal.AllocCoTaskMem(Math.Max(size, Marshal.SizeOf(typeof(WM_MEDIA_TYPE)) + Marshal.SizeOf(typeof(WaveFormat)))); try { Props.GetMediaType(buffer, ref size); var mt = (WM_MEDIA_TYPE)Marshal.PtrToStructure(buffer, typeof(WM_MEDIA_TYPE)); if ((mt.majortype == MediaTypes.WMMEDIATYPE_Audio) && (mt.subtype == MediaTypes.WMMEDIASUBTYPE_PCM) && (mt.formattype == MediaTypes.WMFORMAT_WaveFormatEx) && (mt.cbFormat >= WAVE_FORMAT_EX_SIZE)) { fmt = new WaveFormat(44100, 16, 2); Marshal.PtrToStructure(mt.pbFormat, fmt); } else { throw new ArgumentException(string.Format("The format {0} of the output {1} is not a valid PCM format", formatNumber, outputNumber)); } } finally { Marshal.FreeCoTaskMem(buffer); } return fmt; }
public void Dispose() { if (syncReader != null) Marshal.FinalReleaseComObject(syncReader); syncReader = null; if (stream != null) stream.Dispose(); stream = null; }