public MfAudioWriter(string FileName, Guid MediaSubtype, WaveFormat Wf, int AudioQuality) { _writer = MediaFactory.CreateSinkWriterFromURL(FileName, null, null); _audioInBytesPerSecond = Wf.SampleRate * Wf.Channels * Wf.BitsPerSample / 8; using (var audioTypeOut = MfWriter.GetMediaType(Wf)) { audioTypeOut.Set(MediaTypeAttributeKeys.Subtype, MediaSubtype); if (MediaSubtype == AudioFormatGuids.Aac) { audioTypeOut.Set(MediaTypeAttributeKeys.AudioAvgBytesPerSecond, MfWriter.GetAacBitrate(AudioQuality)); } _writer.AddStream(audioTypeOut, out _); } using (var audioTypeIn = MfWriter.GetMediaType(Wf)) { audioTypeIn.Set(MediaTypeAttributeKeys.Subtype, AudioFormatGuids.Pcm); _writer.SetInputMediaType(StreamIndex, audioTypeIn, null); } _writer.BeginWriting(); }
public MfColorConverter(int Width, int Height, Device Device) { var transforms = MediaFactory.FindTransform(TransformCategoryGuids.VideoProcessor, TransformEnumFlag.All); _colorConverter = transforms[0].ActivateObject <Transform>(); _deviceMan = new DXGIDeviceManager(); _deviceMan.ResetDevice(Device); _colorConverter.ProcessMessage(TMessageType.SetD3DManager, _deviceMan.NativePointer); using (var mediaTypeIn = new MediaType()) { mediaTypeIn.Set(MediaTypeAttributeKeys.MajorType, MediaTypeGuids.Video); mediaTypeIn.Set(MediaTypeAttributeKeys.Subtype, VideoFormatGuids.Rgb32); mediaTypeIn.Set(MediaTypeAttributeKeys.FrameSize, MfWriter.PackLong(Width, Height)); mediaTypeIn.Set(MediaTypeAttributeKeys.DefaultStride, Width * 4); mediaTypeIn.Set(MediaTypeAttributeKeys.FixedSizeSamples, 1); mediaTypeIn.Set(MediaTypeAttributeKeys.SampleSize, Width * Height * 4); _colorConverter.SetInputType(0, mediaTypeIn, 0); } var outputStride = Width * 12 / 8; var outputSampleSize = Height * outputStride; using (var mediaTypeOut = new MediaType()) { mediaTypeOut.Set(MediaTypeAttributeKeys.MajorType, MediaTypeGuids.Video); mediaTypeOut.Set(MediaTypeAttributeKeys.Subtype, VideoFormatGuids.NV12); mediaTypeOut.Set(MediaTypeAttributeKeys.FrameSize, MfWriter.PackLong(Width, Height)); mediaTypeOut.Set(MediaTypeAttributeKeys.DefaultStride, outputStride); mediaTypeOut.Set(MediaTypeAttributeKeys.FixedSizeSamples, 1); mediaTypeOut.Set(MediaTypeAttributeKeys.SampleSize, outputSampleSize); _colorConverter.SetOutputType(0, mediaTypeOut, 0); } _colorConverter.ProcessMessage(TMessageType.NotifyBeginStreaming, IntPtr.Zero); _copyTexture = new Texture2D(Device, new Texture2DDescription { CpuAccessFlags = CpuAccessFlags.None, BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource, Format = Format.B8G8R8A8_UNorm, Width = Width, Height = Height, OptionFlags = ResourceOptionFlags.None, MipLevels = 1, ArraySize = 1, SampleDescription = { Count = 1, Quality = 0 }, Usage = ResourceUsage.Default }); _inputSample = MediaFactory.CreateVideoSampleFromSurface(null); // Create the media buffer from the texture MediaFactory.CreateDXGISurfaceBuffer(typeof(Texture2D).GUID, _copyTexture, 0, false, out var inputBuffer); using (var buffer2D = inputBuffer.QueryInterface <Buffer2D>()) inputBuffer.CurrentLength = buffer2D.ContiguousLength; // Attach the created buffer to the sample _inputSample.AddBuffer(inputBuffer); }