/// <summary> /// Wraps IWMProfile.GetStream /// </summary> /// <param name="index">Index of desired stream</param> /// <returns>IWMStreamConfig</returns> public IWMStreamConfig GetStream(int index) { IWMStreamConfig res; m_Profile.GetStream((uint)index, out res); return(res); }
private void TestStream() { int i; IWMStreamConfig sc, sc2, sc3; m_pProfile.CreateNewStream(MediaType.Video, out sc); Debug.Assert(sc != null); sc.SetStreamNumber(12); m_pProfile.AddStream(sc); m_pProfile.GetStreamCount(out i); Debug.Assert(i == 1); m_pProfile.GetStream(0, out sc2); Debug.Assert(sc2 != null); m_pProfile.ReconfigStream(sc2); m_pProfile.GetStreamByNumber(12, out sc3); Debug.Assert(sc3 != null); m_pProfile.RemoveStream(sc3); m_pProfile.GetStreamCount(out i); Debug.Assert(i == 0); m_pProfile.AddStream(sc); m_pProfile.GetStreamCount(out i); Debug.Assert(i == 1); m_pProfile.RemoveStreamByNumber(12); m_pProfile.GetStreamCount(out i); Debug.Assert(i == 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); }
private void SetStreams(string sName) { // First find the mapping between the output name and // the stream number IWMProfile pProfile = m_pReader as IWMProfile; int iCount; pProfile.GetStreamCount(out iCount); short[] sss = new short[iCount]; StreamSelection[] ss = new StreamSelection[iCount]; StringBuilder sSName; for (short j = 0; j < iCount; j++) { IWMStreamConfig pConfig; pProfile.GetStream(j, out pConfig); try { pConfig.GetStreamNumber(out sss[j]); short iSName = 0; sSName = null; pConfig.GetConnectionName(sSName, ref iSName); sSName = new StringBuilder(iSName); pConfig.GetConnectionName(sSName, ref iSName); } finally { Marshal.ReleaseComObject(pConfig); } // Turn the stream on or off depending on whether // this is the one that matches the output we're // looking for if (sSName.ToString() == sName) { ss[j] = StreamSelection.On; } else { ss[j] = StreamSelection.Off; } } // Use the array we've built to specify the streams we want IWMReaderAdvanced ra = m_pReader as IWMReaderAdvanced; ra.SetStreamsSelected((short)iCount, sss, ss); // Learn the maximum sample size that will be send to OnSample ra.GetMaxOutputSampleSize(m_dwAudioOutputNum, out m_MaxSampleSize); // Have the Samples allocated using IWMReaderCallbackAdvanced::AllocateForOutput ra.SetAllocateForOutput(m_dwAudioOutputNum, true); }
//------------------------------------------------------------------------------ // 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); } } }
private void SetCutomProfile(int vidbitrate, int audbitrate, int vidheight, int vidwidth, double fps) { //seperate method atm braindump for adjusting an existing profile (prx file) //method call is not enabled yet IWMProfileManager profileManager = null; IWMProfileManager2 profileManager2 = null; IWMProfile profile = null; IWMStreamConfig streamConfig; //IWMInputMediaProps inputProps = null; IWMProfileManagerLanguage profileManagerLanguage = null; WMVersion wmversion = WMVersion.V8_0; int nbrProfiles = 0; short langID; StringBuilder profileName = new StringBuilder(MAXLENPROFNAME); StringBuilder profileDescription = new StringBuilder(MAXLENPROFDESC); int profileNameLen = MAXLENPROFNAME; int profileDescLen = MAXLENPROFDESC; profileName.Length = 0; profileDescription.Length = 0; double videoFps = fps; long singleFramePeriod = (long)((10000000L / fps)); //Guid guidInputType; //int dwInputCount = 0; int hr; int videoBitrate = vidbitrate; int audioBitrate = audbitrate; int videoHeight = vidheight; int videoWidth = vidwidth; double videofps = fps; int streamCount = 0; IWMMediaProps streamMediaProps = null; IntPtr mediaTypeBufferPtr = IntPtr.Zero; uint mediaTypeBufferSize = 0; Guid streamType = Guid.Empty; WmMediaType videoMediaType = new WmMediaType(); //Set WMVIDEOHEADER WMVIDEOINFOHEADER videoInfoHeader = new WMVIDEOINFOHEADER(); //Setup the profile manager hr = WMLib.WMCreateProfileManager(out profileManager); profileManager2 = (IWMProfileManager2)profileManager; //Set profile version - possibly not needed in this case. profileManager2.SetSystemProfileVersion(WMVersion.V8_0); //get the profile to modify string strprofileType = Config.GetFile(Config.Dir.Base, @"Profiles\MPCustom.prx"); //read the profile contents string profileContents = ""; using (StreamReader prx = new StreamReader(strprofileType)) { profileContents = prx.ReadToEnd(); } profileManager2 = profileManager as IWMProfileManager2; profileManagerLanguage = profileManager as IWMProfileManagerLanguage; hr = profileManager2.GetSystemProfileVersion(out wmversion); Log.Info("DVRMS2WMV: WM version=" + wmversion.ToString()); hr = profileManagerLanguage.GetUserLanguageID(out langID); Log.Info("DVRMS2WMV: WM language ID=" + langID.ToString()); hr = profileManager2.SetSystemProfileVersion(DefaultWMversion); hr = profileManager2.GetSystemProfileCount(out nbrProfiles); Log.Info("DVRMS2WMV: ProfileCount=" + nbrProfiles.ToString()); //load the profile contents hr = profileManager.LoadProfileByData(profileContents, out profile); //get the profile name hr = profile.GetName(profileName, ref profileNameLen); Log.Info("DVRMS2WMV: profile name {0}", profileName.ToString()); //get the profile description hr = profile.GetDescription(profileDescription, ref profileDescLen); Log.Info("DVRMS2WMV: profile description {0}", profileDescription.ToString()); //get the stream count hr = profile.GetStreamCount(out streamCount); for (int i = 0; i < streamCount; i++) { profile.GetStream(i, out streamConfig); streamMediaProps = (IWMMediaProps)streamConfig; streamConfig.GetStreamType(out streamType); if (streamType == MediaType.Video) { //adjust the video details based on the user input values. streamConfig.SetBitrate(videoBitrate); streamConfig.SetBufferWindow(-1); //3 or 5 seconds ??? streamMediaProps.GetMediaType(IntPtr.Zero, ref mediaTypeBufferSize); mediaTypeBufferPtr = Marshal.AllocHGlobal((int)mediaTypeBufferSize); streamMediaProps.GetMediaType(mediaTypeBufferPtr, ref mediaTypeBufferSize); Marshal.PtrToStructure(mediaTypeBufferPtr, videoMediaType); Marshal.FreeHGlobal(mediaTypeBufferPtr); Marshal.PtrToStructure(videoMediaType.pbFormat, videoInfoHeader); videoInfoHeader.TargetRect.right = 0; // set to zero to take source size videoInfoHeader.TargetRect.bottom = 0; // set to zero to take source size videoInfoHeader.BmiHeader.Width = videoWidth; videoInfoHeader.BmiHeader.Height = videoHeight; videoInfoHeader.BitRate = videoBitrate; videoInfoHeader.AvgTimePerFrame = singleFramePeriod; //Need to check how this is to be calculated IntPtr vidInfoPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(WMVIDEOINFOHEADER))); Marshal.StructureToPtr(videoInfoHeader, vidInfoPtr, false); videoMediaType.pbFormat = vidInfoPtr; hr = streamMediaProps.SetMediaType(videoMediaType); Marshal.FreeHGlobal(vidInfoPtr); } if (streamType == MediaType.Audio) { //adjust the audio details based on the user input //audio is determined from bitrate selection and thus effects audio profile. hr = streamConfig.SetBitrate(audioBitrate); hr = streamConfig.SetBufferWindow(-1); //3 or 5 seconds ??? //TODO: set the WaveformatEx profile info etc } //recofigures the stream ready for saving hr = profile.ReconfigStream(streamConfig); } //save the profile //You should make two calls to SaveProfile. //On the first call, pass NULL as pwszProfile. int profileLength = 0; hr = profileManager2.SaveProfile(profile, null, ref profileLength); //On return, the value of pdwLength is set to the length required to hold the profile in string form. //TODO: set memory buffer to profileLength //Then you can allocate the required amount of memory for the buffer and pass a pointer to it as pwszProfile on the second call. hr = profileManager2.SaveProfile(profile, profileContents, ref profileLength); }
// 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); } } }
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(); } }