private void TestProfile() { int hr; IntPtr prof; hr = m_asfw.GetCurrentProfile(out prof); DsError.ThrowExceptionForHR(hr); #if false IWMProfile ipr = o as IWMProfile; int s = 255; StringBuilder sb = new StringBuilder(s, s); ipr.GetName(sb, ref s); Debug.Assert(sb.ToString() == "Intranet - High Speed LAN Multiple Bit Rate Video", "GetProfile"); #endif #if false IntPtr ip = Marshal.GetIUnknownForObject(o); IntPtr ip2; Guid g = typeof(IWMProfile).GUID; Marshal.QueryInterface(ip, ref g, out ip2); #endif hr = m_asfw.ConfigureFilterUsingProfile(prof); DsError.ThrowExceptionForHR(hr); #if false Guid g2 = new Guid("00000000-0000-0000-C000-000000000046"); IntPtr ip3; Marshal.QueryInterface(ip, ref g2, out ip3); hr = m_asfw.ConfigureFilterUsingProfile(ip3); DsError.ThrowExceptionForHR(hr); Marshal.Release(ip3); #endif #if false Marshal.Release(ip); Marshal.Release(ip2); #endif }
// End of NEW /// <summary> /// Configure profile from file to Asf file writer /// </summary> /// <param name="asfWriter"></param> /// <param name="filename"></param> /// <returns></returns> public bool ConfigProfileFromFile(IBaseFilter asfWriter, string filename) { int hr; //string profilePath = "test.prx"; // Set the profile to be used for conversion if ((filename != null) && (File.Exists(filename))) { // Load the profile XML contents string profileData; using (StreamReader reader = new StreamReader(File.OpenRead(filename))) { profileData = reader.ReadToEnd(); } // Create an appropriate IWMProfile from the data // Open the profile manager IWMProfileManager profileManager; IWMProfile wmProfile = null; hr = WMLib.WMCreateProfileManager(out profileManager); if (hr >= 0) { // error message: The profile is invalid (0xC00D0BC6) // E.g. no <prx> tags hr = profileManager.LoadProfileByData(profileData, out wmProfile); } if (profileManager != null) { Marshal.ReleaseComObject(profileManager); profileManager = null; } // Config only if there is a profile retrieved if (hr >= 0) { // Set the profile on the writer IConfigAsfWriter configWriter = (IConfigAsfWriter)asfWriter; hr = configWriter.ConfigureFilterUsingProfile(wmProfile); if (hr >= 0) { return(true); } } } return(false); }
private void InitAsfWriter(out IBaseFilter asfWriter, bool shittyQuality = false) { int hr; asfWriter = (IBaseFilter) new WMAsfWriter(); hr = m_FilterGraph.AddFilter(asfWriter, "ASF Writer"); Marshal.ThrowExceptionForHR(hr); if (shittyQuality) { return; } // Create an appropriate IWMProfile from the data // Open the profile manager IWMProfileManager profileManager; IWMProfile wmProfile = null; WMUtils.WMCreateProfileManager(out profileManager); // error message: The profile is invalid (0xC00D0BC6) // E.g. no <prx> tags string profile = System.Text.Encoding.ASCII.GetString(Convert.FromBase64String(customGoodProfile)); profileManager.LoadProfileByData(profile, out wmProfile); if (profileManager != null) { Marshal.ReleaseComObject(profileManager); profileManager = null; } // Config only if there is a profile retrieved // Set the profile on the writer IConfigAsfWriter configWriter = (IConfigAsfWriter)asfWriter; configWriter.ConfigureFilterUsingProfile(wmProfile); }
private bool AddWmAsfWriter(string fileName, Quality quality, Standard standard) { //add asf file writer IPin pinOut0, pinOut1; IPin pinIn0, pinIn1; Log.Info("DVRMS2WMV: add WM ASF Writer to graph"); string monikerAsfWriter = @"@device:sw:{083863F1-70DE-11D0-BD40-00A0C911CE86}\{7C23220E-55BB-11D3-8B16-00C04FB6BD3D}"; fileWriterbase = Marshal.BindToMoniker(monikerAsfWriter) as IBaseFilter; if (fileWriterbase == null) { Log.Error("DVRMS2WMV:FAILED:Unable to create ASF WM Writer"); Cleanup(); return(false); } int hr = graphBuilder.AddFilter(fileWriterbase, "WM ASF Writer"); if (hr != 0) { Log.Error("DVRMS2WMV:FAILED:Add ASF WM Writer to filtergraph :0x{0:X}", hr); Cleanup(); return(false); } IFileSinkFilter2 fileWriterFilter = fileWriterbase as IFileSinkFilter2; if (fileWriterFilter == null) { Log.Error("DVR2XVID:FAILED:Add unable to get IFileSinkFilter for filewriter"); Cleanup(); return(false); } hr = fileWriterFilter.SetFileName(fileName, null); hr = fileWriterFilter.SetMode(AMFileSinkFlags.OverWrite); Log.Info("DVRMS2WMV: connect audio/video codecs outputs -> ASF WM Writer"); //connect output #0 of videocodec->asf writer pin 1 //connect output #0 of audiocodec->asf writer pin 0 pinOut0 = DsFindPin.ByDirection((IBaseFilter)Mpeg2AudioCodec, PinDirection.Output, 0); pinOut1 = DsFindPin.ByDirection((IBaseFilter)Mpeg2VideoCodec, PinDirection.Output, 0); if (pinOut0 == null || pinOut1 == null) { Log.Error("DVRMS2WMV:FAILED:unable to get outpins of video codec"); Cleanup(); return(false); } pinIn0 = DsFindPin.ByDirection(fileWriterbase, PinDirection.Input, 0); if (pinIn0 == null) { Log.Error("DVRMS2WMV:FAILED:unable to get pins of asf wm writer"); Cleanup(); return(false); } hr = graphBuilder.Connect(pinOut0, pinIn0); if (hr != 0) { Log.Error("DVRMS2WMV:FAILED:unable to connect audio pins :0x{0:X}", hr); Cleanup(); return(false); } pinIn1 = DsFindPin.ByDirection(fileWriterbase, PinDirection.Input, 1); if (pinIn1 == null) { Log.Error("DVRMS2WMV:FAILED:unable to get pins of asf wm writer"); Cleanup(); return(false); } hr = graphBuilder.Connect(pinOut1, pinIn1); if (hr != 0) { Log.Error("DVRMS2WMV:FAILED:unable to connect video pins :0x{0:X}", hr); Cleanup(); return(false); } IConfigAsfWriter config = fileWriterbase as IConfigAsfWriter; IWMProfileManager profileManager = null; IWMProfileManager2 profileManager2 = null; IWMProfile profile = null; hr = WMLib.WMCreateProfileManager(out profileManager); string strprofileType = ""; switch (quality) { case Quality.HiDef: //hr = WMLib.WMCreateProfileManager(out profileManager); if (standard == Standard.Film) { strprofileType = Config.GetFile(Config.Dir.Base, @"Profiles\MPHiDef-FILM.prx"); } if (standard == Standard.NTSC) { strprofileType = Config.GetFile(Config.Dir.Base, @"Profiles\MPHiDef-NTSC.prx"); } if (standard == Standard.PAL) { strprofileType = Config.GetFile(Config.Dir.Base, @"Profiles\MPHiDef-PAL.prx"); } Log.Info("DVRMS2WMV: set WMV HiDef quality profile {0}", strprofileType); break; case Quality.VeryHigh: if (standard == Standard.Film) { strprofileType = Config.GetFile(Config.Dir.Base, @"Profiles\MPVeryHigh-FILM.prx"); } if (standard == Standard.NTSC) { strprofileType = Config.GetFile(Config.Dir.Base, @"Profiles\MPVeryHigh-NTSC.prx"); } if (standard == Standard.PAL) { strprofileType = Config.GetFile(Config.Dir.Base, @"Profiles\MPVeryHigh-PAL.prx"); } Log.Info("DVRMS2WMV: set WMV Very High quality profile {0}", strprofileType); break; case Quality.High: if (standard == Standard.Film) { strprofileType = Config.GetFile(Config.Dir.Base, @"Profiles\MPHigh-FILM.prx"); } if (standard == Standard.NTSC) { strprofileType = Config.GetFile(Config.Dir.Base, @"Profiles\MPHigh-NTSC.prx"); } if (standard == Standard.PAL) { strprofileType = Config.GetFile(Config.Dir.Base, @"Profiles\MPHigh-PAL.prx"); } Log.Info("DVRMS2WMV: set WMV High quality profile {0}", strprofileType); break; case Quality.Medium: if (standard == Standard.Film) { strprofileType = Config.GetFile(Config.Dir.Base, @"Profiles\MPMedium-FILM.prx"); } if (standard == Standard.NTSC) { strprofileType = Config.GetFile(Config.Dir.Base, @"Profiles\MPMedium-NTSC.prx"); } if (standard == Standard.PAL) { strprofileType = Config.GetFile(Config.Dir.Base, @"Profiles\MPMedium-PAL.prx"); } Log.Info("DVRMS2WMV: set WMV Medium quality profile {0}", strprofileType); break; case Quality.Low: if (standard == Standard.Film) { strprofileType = Config.GetFile(Config.Dir.Base, @"Profiles\MPLow-FILM.prx"); } if (standard == Standard.NTSC) { strprofileType = Config.GetFile(Config.Dir.Base, @"Profiles\MPLow-NTSC.prx"); } if (standard == Standard.PAL) { strprofileType = Config.GetFile(Config.Dir.Base, @"Profiles\MPLow-PAL.prx"); } Log.Info("DVRMS2WMV: set WMV Low quality profile {0}", strprofileType); break; case Quality.Portable: if (standard == Standard.Film) { strprofileType = Config.GetFile(Config.Dir.Base, @"Profiles\MPPortable-FILM.prx"); } if (standard == Standard.NTSC) { strprofileType = Config.GetFile(Config.Dir.Base, @"Profiles\MPPortable-NTSC.prx"); } if (standard == Standard.PAL) { strprofileType = Config.GetFile(Config.Dir.Base, @"Profiles\MPPortable-PAL.prx"); } Log.Info("DVRMS2WMV: set WMV Portable quality profile {0}", strprofileType); break; case Quality.Custom: //load custom profile string customBitrate = ""; //Adjust the parameters to suit the custom settings the user has selected. switch (bitrate) { case 0: customBitrate = "100Kbs"; break; case 1: customBitrate = "256Kbs"; break; case 2: customBitrate = "384Kbs"; break; case 3: customBitrate = "768Kbs"; break; case 4: customBitrate = "1536Kbs"; break; case 5: customBitrate = "3072Kbs"; break; case 6: customBitrate = "5376Kbs"; break; } Log.Info("DVRMS2WMV: custom bitrate = {0}", customBitrate); //TODO: get fps values & frame size //TODO: adjust settings required //Call the SetCutomPorfile method to load the custom profile, adjust it's params from user settings & then save it. //SetCutomProfile(videoBitrate, audioBitrate, videoHeight, videoWidth, videoFps); //based on user inputs //We then reload it after as per other quality settings / profiles. strprofileType = Config.GetFile(Config.Dir.Base, @"Profiles\MPCustom.prx"); Log.Info("DVRMS2WMV: set WMV Custom quality profile {0}", strprofileType); break; } //Loads profile from the above quality selection. using (StreamReader prx = new StreamReader(strprofileType)) { String profileContents = prx.ReadToEnd(); profileManager2 = profileManager as IWMProfileManager2; hr = profileManager2.LoadProfileByData(profileContents, out profile); if (hr != 0) { Log.Info("DVRMS2WMV: get WMV profile - FAILED! {0}", hr); Cleanup(); return(false); } } Log.Info("DVRMS2WMV: load profile - SUCCESS!"); //configures the WM ASF Writer to the chosen profile hr = config.ConfigureFilterUsingProfile(profile); if (hr != 0) { Log.Info("DVRMS2WMV: configure profile - FAILED! {0}", hr); Cleanup(); return(false); } Log.Info("DVRMS2WMV: configure profile - SUCCESS!"); //TODO: Add DB recording information into WMV. //Release resorces if (profile != null) { Marshal.ReleaseComObject(profile); profile = null; } if (profileManager != null) { Marshal.ReleaseComObject(profileManager); profileManager = null; } return(true); }
/// <summary> /// WMV 形式ファイル保存用: ビデオのフレームサイズを設定します。 /// </summary> /// <param name="config">ASF Writer から取得したインターフェース</param> /// <param name="size">設定値</param> /// <remarks> /// 第一引数の <c>config</c> は下記のように取得してください。<br/> /// <example lang="c#"> /// var filetype = new Guid(GUID.MEDIASUBTYPE_Asf); /// builder.SetOutputFileName(new IntPtr(&filetype), filename, ref videoRenderer, ref fileSink); /// var config = (IConfigAsfWriter)videoRenderer; /// </example> /// <br/> /// 詳細は下記に記載されています。(但し、言語は C++ です。) <br/> /// <br/> /// <list type="bullet"> /// <item> /// Capturing Video to a Windows Media File <br/> /// https://msdn.microsoft.com/en-us/library/windows/desktop/dd318630(v=vs.85).aspx /// </item> /// <item> /// Creating ASF Files in DirectShow <br/> /// https://msdn.microsoft.com/en-us/library/windows/desktop/dd375008(v=vs.85).aspx /// <list type="bullet"> /// <item> /// Configuring the ASF Writer <br/> /// https://msdn.microsoft.com/en-us/library/windows/desktop/dd387911(v=vs.85).aspx /// </item> /// </list> /// </item> /// <item> /// Using Windows Media in DirectShow <br/> /// https://msdn.microsoft.com/en-us/library/windows/desktop/dd407300(v=vs.85).aspx /// <list type="bullet"> /// <item> /// WM ASF Writer Filter <br/> /// https://msdn.microsoft.com/en-us/library/windows/desktop/dd390985(v=vs.85).aspx /// </item> /// </list> /// </item> /// </list> /// <br/> /// しかし、上記の説明だけでは解決できないので、下記も参考にしています。<br/> /// Social <br/> /// https://social.msdn.microsoft.com/Forums/sqlserver/en-US/962d4370-5fde-4d23-83bc-b8f6191b375e/asf-writer-creates-a-file-that-appears-not-to-match-the-encoding-profile-settings?forum=windowsdirectshowdevelopment <br/> /// https://social.msdn.microsoft.com/forums/windowsdesktop/en-us/fb0a6151-d916-422a-966b-ee1fb164df5f/asf-profile-configurations-fails <br/> /// </remarks> public static void SetVideoFrameSize(IConfigAsfWriter config, Size size) { try { HRESULT hr; // 現在のプロファイルを取得します. IWMProfile profile; hr = (HRESULT)config.GetCurrentProfile(out profile); if (hr < HRESULT.S_OK) { throw new CxDSException(hr); } // ストリームの個数を取得します. uint stream_num; hr = (HRESULT)profile.GetStreamCount(out stream_num); if (hr < HRESULT.S_OK) { throw new CxDSException(hr); } // 映像入力のストリームを探索してフレームサイズを設定します. for (uint index = 0; index < stream_num; index++) { // ストリームを取得します. IWMStreamConfig stream_config; hr = (HRESULT)profile.GetStream(index, out stream_config); if (hr < HRESULT.S_OK) { throw new CxDSException(hr); } // ストリームのメディアタイプを確認します. Guid stream_type; hr = (HRESULT)stream_config.GetStreamType(out stream_type); if (hr < HRESULT.S_OK) { throw new CxDSException(hr); } // 映像以外は無視します. if (stream_type != new Guid(GUID.MEDIATYPE_Video)) { continue; } // メディアタイプ情報を格納するために必要なサイズ (bytes) を取得します. var props = (IWMMediaProps)stream_config; int cbType = 0; hr = (HRESULT)props.GetMediaType(IntPtr.Zero, ref cbType); if (hr < HRESULT.S_OK) { throw new CxDSException(hr); } // メディアタイプ情報を格納する領域を確保します. var pwmt = Marshal.AllocHGlobal(cbType); if (pwmt != IntPtr.Zero) { try { // メディアタイプ情報を取得します. hr = (HRESULT)props.GetMediaType(pwmt, ref cbType); if (hr < HRESULT.S_OK) { throw new CxDSException(hr); } // フレームサイズを指定された値に書き替えます. unsafe { var wmt = (WM_MEDIA_TYPE *)pwmt; var vih = (VIDEOINFOHEADER *)wmt->pbFormat; vih->bmiHeader.biWidth = size.Width; // 幅. vih->bmiHeader.biHeight = size.Height; // 高さ. } // メディアタイプを設定します. hr = (HRESULT)props.SetMediaType(pwmt); if (hr < HRESULT.S_OK) { throw new CxDSException(hr); } } finally { Marshal.FreeHGlobal(pwmt); } } // ストリームを再構成します. hr = (HRESULT)profile.ReconfigStream(stream_config); if (hr < HRESULT.S_OK) { throw new CxDSException(hr); } } // プロファイルを再構成します. hr = (HRESULT)config.ConfigureFilterUsingProfile(profile); if (hr < HRESULT.S_OK) { throw new CxDSException(hr); } } catch (System.Exception ex) { throw ex; } }