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); }
//------------------------------------------------------------------------------ // Name: CWMVCopy::GetProfileInfo() // Desc: Gets the profile information from the reader. //------------------------------------------------------------------------------ protected void GetProfileInfo() { IWMStreamConfig pStreamConfig = null; IWMMediaProps pMediaProperty = null; // // Get the profile of the reader // m_pReaderProfile = m_pReader as IWMProfile; // // Get stream count // m_pReaderProfile.GetStreamCount(out m_dwStreamCount); // // Allocate memory for the stream type array and stream number array // m_pguidStreamType = new Guid[m_dwStreamCount]; m_pwStreamNumber = new short[m_dwStreamCount]; for (int i = 0; i < m_dwStreamCount; i++) { m_pReaderProfile.GetStream(i, out pStreamConfig); try { // // Get the stream number of the current stream // pStreamConfig.GetStreamNumber(out m_pwStreamNumber[i]); // // Set the stream to be received in compressed mode // m_pReaderAdvanced.SetReceiveStreamSamples(m_pwStreamNumber[i], true); pMediaProperty = pStreamConfig as IWMMediaProps; // // Get the stream type of the current stream // pMediaProperty.GetType(out m_pguidStreamType[i]); } finally { Marshal.ReleaseComObject(pStreamConfig); } } }
/// <summary> /// WMStreamConfig constructor /// </summary> /// <param name="config">IWMStreamConfig to wrap</param> public WMStreamConfig(IWMStreamConfig config) { streamConfig = config; }
/// <summary> /// WMStreamConfig constructor /// </summary> /// <param name="config">IWMStreamConfig to wrap</param> public WMStreamConfig(IWMStreamConfig config) { m_StreamConfig = config; }
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); }
/// <summary> /// Wraps IWMProfile.ReconfigStream /// </summary> /// <param name="strconfig">IWMStreamConfig stream to reconfig</param> public void ReconfigStream(IWMStreamConfig strconfig) { m_Profile.ReconfigStream(strconfig); }
// Use IWMStreamConfig interface to access codec names /////////////////////////////////////////////////////////////////////////////// void PrintCodecName(IWMStreamConfig pConfig) { IWMMediaProps pMediaProps = null; pMediaProps = (IWMMediaProps)pConfig; int cbType = 0; pMediaProps.GetMediaType(null, ref cbType); AMMediaType mt = new AMMediaType(); mt.formatSize = cbType; pMediaProps.GetMediaType(mt, ref cbType); try { // // Audio Codec Names // if (mt.subType == MediaSubType.WMAudioV9) { Console.WriteLine("Codec Name: Windows Media Audio V9"); } else if (mt.subType == MediaSubType.WMAudio_Lossless) { Console.WriteLine("Codec Name: Windows Media Audio V9 (Lossless Mode)"); } else if (mt.subType == MediaSubType.WMAudioV7) { Console.WriteLine("Codec Name: Windows Media Audio V7/V8"); } else if (mt.subType == MediaSubType.WMSP1) { Console.WriteLine("Codec Name: Windows Media Speech Codec V9"); } else if (mt.subType == MediaSubType.WMAudioV2) { Console.WriteLine("Codec Name: Windows Media Audio V2"); } else if (mt.subType == MediaSubType.ACELPnet) { Console.WriteLine("Codec Name: ACELP.net"); } // Video Codec Names // else if (mt.subType == MediaSubType.WMV1) { Console.WriteLine("Codec Name: Windows Media Video V7"); } else if (mt.subType == MediaSubType.MSS1) { Console.WriteLine("Codec Name: Windows Media Screen V7"); } else if (mt.subType == MediaSubType.MSS2) { Console.WriteLine("Codec Name: Windows Media Screen V9"); } else if (mt.subType == MediaSubType.WMV2) { Console.WriteLine("Codec Name: Windows Media Video V8"); } else if (mt.subType == MediaSubType.WMV3) { Console.WriteLine("Codec Name: Windows Media Video V9"); } else if (mt.subType == MediaSubType.MP43) { Console.WriteLine("Codec Name: Microsoft MPEG-4 Video Codec V3 "); } else if (mt.subType == MediaSubType.MP4S) { Console.WriteLine("Codec Name: ISO MPEG-4 Video V1"); } else { Console.WriteLine("Codec Name: {0}", AMToString.MediaSubTypeToString(mt.subType)); } Console.WriteLine(); } finally { WMUtils.FreeWMMediaType(mt); } }
// Use IWMStreamConfig interface to access // number of streams, each stream number, and bitrate /////////////////////////////////////////////////////////////////////////////// void GetPropertiesFromProfile(IWMProfile pProfile) { int dwStreamCount = 0; pProfile.GetStreamCount(out dwStreamCount); Console.WriteLine(string.Format("This Windows Media file has {0} stream(s)", 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.Video == guid) { short wStreamNum = -1; int dwBitrate = -1; try { pConfig.GetStreamNumber(out wStreamNum); } catch { } try { pConfig.GetBitrate(out dwBitrate); } catch { } Console.WriteLine("Video Stream properties:"); Console.WriteLine(string.Format("Stream number: {0}", wStreamNum)); Console.WriteLine(string.Format("Bitrate: {0} bps", dwBitrate)); PrintCodecName(pConfig); } else if (MediaType.Audio == guid) { short wStreamNum = -1; int dwBitrate = -1; try { pConfig.GetStreamNumber(out wStreamNum); } catch { } try { pConfig.GetBitrate(out dwBitrate); } catch { } Console.WriteLine("Audio Stream properties:"); Console.WriteLine(string.Format("Stream number: {0}", wStreamNum)); Console.WriteLine(string.Format("Bitrate: {0} bps", dwBitrate)); PrintCodecName(pConfig); } else if (MediaType.ScriptCommand == guid) { short wStreamNum = -1; int dwBitrate = -1; try { pConfig.GetStreamNumber(out wStreamNum); } catch { } try { pConfig.GetBitrate(out dwBitrate); } catch { } Console.WriteLine("Script Stream properties:"); Console.WriteLine(string.Format("Stream number: {0}", wStreamNum)); Console.WriteLine(string.Format("Bitrate: {0} bps\n", dwBitrate)); } } finally { Marshal.ReleaseComObject(pConfig); } } }
/// <summary> /// Wraps IWMProfile.RemoveStream /// </summary> /// <param name="strconfig">IWMStreamConfig stream to remove</param> public void RemoveStream(IWMStreamConfig strconfig) { m_Profile.RemoveStream(strconfig); }
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 void RemoveStream(IWMStreamConfig strconfig) { _profile.RemoveStream(strconfig); }
public void ReconfigStream(IWMStreamConfig strconfig) { _profile.ReconfigStream(strconfig); }
/// <summary> /// Add profile item to the list /// </summary> /// <param name="avformat"></param> /// <param name="profile"></param> /// <param name="guid"></param> /// <param name="filename"></param> /// <returns></returns> public bool AddProfileItem(AsfFormatSelection avformat, IWMProfile profile, Guid guid, string filename) { if (profile == null) { return(false); } try { StringBuilder profileName = new StringBuilder(MAXLENPROFNAME); StringBuilder profileDescription = new StringBuilder(MAXLENPROFDESC); int profileNameLen = MAXLENPROFNAME; int profileDescLen = MAXLENPROFDESC; int streamCount = 0; bool audio = false; bool video = false; int audioBitrate = 0; int videoBitrate = 0; profileName.Length = 0; profileDescription.Length = 0; int hr = profile.GetName(profileName, ref profileNameLen); #if DEBUG if (hr < 0) { Debug.WriteLine("GetName failed"); } #endif if (hr >= 0) { hr = profile.GetDescription(profileDescription, ref profileDescLen); #if DEBUG if (hr < 0) { Debug.WriteLine("GetDescription failed"); } #endif } if (hr >= 0) { hr = profile.GetStreamCount(out streamCount); #if DEBUG if (hr < 0) { Debug.WriteLine("GetStreamCount failed"); } #endif } if ((streamCount > 0) && (hr >= 0)) { IWMStreamConfig streamConfig = null; Guid streamGuid = Guid.Empty; audio = false; video = false; audioBitrate = 0; videoBitrate = 0; for (short i = 1; (i <= streamCount) && (hr >= 0); i++) { hr = profile.GetStreamByNumber(i, out streamConfig); if ((hr >= 0) && (streamConfig != null)) { hr = streamConfig.GetStreamType(out streamGuid); if (hr >= 0) { if (streamGuid == MediaType.Video) { video = true; hr = streamConfig.GetBitrate(out videoBitrate); if (hr < 0) { videoBitrate = 0; } } else if (streamGuid == MediaType.Audio) { audio = true; hr = streamConfig.GetBitrate(out audioBitrate); if (hr < 0) { audioBitrate = 0; } } hr = 0; // Allow possible unreadable bitrates } #if DEBUG else { Debug.WriteLine("GetStreamByNumber failed"); } #endif } // for i } // Create profile format item if (hr >= 0) { WMProfileData profileInfo = new WMProfileData( guid, profileName.ToString(), profileDescription.ToString(), audioBitrate, videoBitrate, audio, video); bool StoreInfo = false; // Check if all profiles are allowed switch (avformat) { case AsfFormatSelection.AllFormats: StoreInfo = true; break; case AsfFormatSelection.AudioOnly: if ((profileInfo.Audio) && (!profileInfo.Video)) { StoreInfo = true; } break; case AsfFormatSelection.Video: if (profileInfo.Video) { StoreInfo = true; } break; case AsfFormatSelection.VideoOnly: if ((profileInfo.Video) && (!profileInfo.Audio)) { StoreInfo = true; } break; } if (StoreInfo) { if ((guid == Guid.Empty) && (filename != null) && (filename.Length > 0)) { // Store filename profileInfo.Filename = filename; } else if (guid != Guid.Empty) { profileInfo.Filename = ""; } else { // Either a filename or guid must be supplied profileInfo.Dispose(); return(false); } InnerList.Add(profileInfo); return(true); } } } } catch { // Fatal error occured ... } return(false); }
// Use IWMStreamConfig interface to access codec names /////////////////////////////////////////////////////////////////////////////// void PrintCodecName(IWMStreamConfig pConfig) { IWMMediaProps pMediaProps = null; pMediaProps = (IWMMediaProps)pConfig; int cbType = 0; pMediaProps.GetMediaType(null, ref cbType); AMMediaType mt = new AMMediaType(); mt.formatSize = cbType; pMediaProps.GetMediaType(mt, ref cbType); try { // // Audio Codec Names // if (mt.subType == MediaSubType.WMAudioV9) { Console.WriteLine("Codec Name: Windows Media Audio V9"); } else if (mt.subType == MediaSubType.WMAudio_Lossless) { Console.WriteLine("Codec Name: Windows Media Audio V9 (Lossless Mode)"); } else if (mt.subType == MediaSubType.WMAudioV7) { Console.WriteLine("Codec Name: Windows Media Audio V7/V8"); } else if (mt.subType == MediaSubType.WMSP1) { Console.WriteLine("Codec Name: Windows Media Speech Codec V9"); } else if (mt.subType == MediaSubType.WMAudioV2) { Console.WriteLine("Codec Name: Windows Media Audio V2"); } else if (mt.subType == MediaSubType.ACELPnet) { Console.WriteLine("Codec Name: ACELP.net"); } // Video Codec Names // else if (mt.subType == MediaSubType.WMV1) { Console.WriteLine("Codec Name: Windows Media Video V7"); } else if (mt.subType == MediaSubType.MSS1) { Console.WriteLine("Codec Name: Windows Media Screen V7"); } else if (mt.subType == MediaSubType.MSS2) { Console.WriteLine("Codec Name: Windows Media Screen V9"); } else if (mt.subType == MediaSubType.WMV2) { Console.WriteLine("Codec Name: Windows Media Video V8"); } else if (mt.subType == MediaSubType.WMV3) { Console.WriteLine("Codec Name: Windows Media Video V9"); } else if (mt.subType == MediaSubType.MP43) { Console.WriteLine("Codec Name: Microsoft MPEG-4 Video Codec V3 "); } else if (mt.subType == MediaSubType.MP4S) { Console.WriteLine("Codec Name: ISO MPEG-4 Video V1"); } else { Console.WriteLine("Codec Name: {0}", AMToString.MediaSubTypeToString(mt.subType)); } Console.WriteLine(); } finally { WMUtils.FreeWMMediaType(mt); } }