private async Task RunConnect() { var ct = ProcessCancel.Token; var taskSet = new HashSet <Task>(); var blockSize = (int)(Param.SamplingRate * Param.SampleLength); Logger.LogInformation($"[loop3] blockSize {blockSize}"); var audioInterval = (long)(10_000_000.0 * Param.SampleLength); Logger.LogInformation($"[loop3] audioInterval {audioInterval}"); var videoInterval = (long)(10_000_000.0 / Param.MaxFrameRate); Logger.LogInformation($"[loop3] videoInterval {videoInterval}"); var counter = new ElapsedTimeCounter(); using var audioWaiter = new Waiter(counter, audioInterval); using var videoWaiter = new Waiter(counter, videoInterval); using var buf = new IPCBuffer <float>(Param.EffectName, blockSize * 2 * Param.BufferCount, LoggerFactory); using var vstBufferPool = new BufferPool <VstBuffer2 <float> >(Param.BufferCount, () => new VstBuffer2 <float>(blockSize, 2, buf), LoggerFactory); using var pcmPool = new BufferPool <PcmBuffer <float> >(Param.BufferCount, () => new PcmBuffer <float>(blockSize, 2), LoggerFactory); using var audioPool = new BufferPool <OpusOutputBuffer>(Param.BufferCount, () => new OpusOutputBuffer(5000), LoggerFactory); using var pcmDrowPool = new BufferPool <PcmBuffer <float> >(Param.BufferCount, () => new PcmBuffer <float>(blockSize, 2), LoggerFactory); using var bmpPool = new BufferPool <H264InputBuffer>(Param.BufferCount, () => new H264InputBuffer(Param.Width, Param.Height), LoggerFactory); using var videoPool = new BufferPool <H264OutputBuffer>(Param.BufferCount, () => new H264OutputBuffer(200000), LoggerFactory); using var vst = new AudioMaster <float>(Param.SamplingRate, blockSize, LoggerFactory, counter, DllManager); using var toPcm = new ToPcm <float>(LoggerFactory, counter); using var opus = new OpusEncoder(SamplingRate.Sampling48000, Channels.Stereo, LoggerFactory, counter); using var fft = new FFTEncoder(Param.Width, Param.Height, Param.MaxFrameRate, LoggerFactory, counter); using var h264 = new H264Encoder(Param.Width, Param.Height, Param.TargetBitrate, Param.MaxFrameRate, LoggerFactory, counter); var effect = vst.AddEffect(Param.EffectName); using var ftl = new FtlIngest(StreamKey, IngestHostname, LoggerFactory, counter, audioInterval, videoInterval, default, CaInfoPath);
public async Task ConversionStreamTest() { var path = Res.GetPath(Res.Video_Mp4); var opath = "out-test-v-2.mp4"; try { using var reader = new VideoReader(path); await reader.LoadMetadataAsync(); var encoder = new H264Encoder { Format = "flv" }; using (var filestream = File.Create(opath)) { using (var writer = new VideoWriter(filestream, reader.Metadata.Width, reader.Metadata.Height, reader.Metadata.AvgFramerate, encoder.Create())) { writer.OpenWrite(); reader.Load(); await reader.CopyToAsync(writer); } } using var video = new VideoReader(opath); await video.LoadMetadataAsync(); Assert.True(video.Metadata.Codec == "h264"); Assert.True(video.Metadata.AvgFramerate == reader.Metadata.AvgFramerate); Assert.True(Math.Abs(video.Metadata.Duration - reader.Metadata.Duration) < 0.2); Assert.True(video.Metadata.Width == reader.Metadata.Width); Assert.True(video.Metadata.Height == reader.Metadata.Height); Assert.True(video.Metadata.BitDepth == reader.Metadata.BitDepth); Assert.True(video.Metadata.Streams.Length == 1); // only video } finally { if (File.Exists(opath)) { File.Delete(opath); } } }
private void button1_Click(object sender, EventArgs e) { int width = 640; int height = 480; int fps = 30; H264Encoder enc = new H264Encoder(); enc.SetupEncode(@"c:\users\idril\downloads\encoded.mp4", width, height, fps); Bitmap image = new Bitmap(width, height); Graphics g = Graphics.FromImage(image); g.FillRectangle(Brushes.Black, 0, 0, width, height); Brush[] brushList = new Brush[] { Brushes.Green, Brushes.Red, Brushes.Yellow, Brushes.Pink, Brushes.LimeGreen }; Random rnd = new Random(); for (int i = 0; i < 180; i++) { int rndTmp = rnd.Next(1, 3); g.FillRectangle(brushList[i % 5], (i % width) * 2, (i % height) * 0.5f, i % 30, i % 30); g.FillRectangle(brushList[i % 5], (i % width) * 2, (i % height) * 2, i % 30, i % 30); g.FillRectangle(brushList[i % 5], (i % width) * 0.5f, (i % height) * 2, i % 30, i % 30); g.Save(); Rectangle rect = new Rectangle(0, 0, image.Width, image.Height); BitmapData bmpData = image.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); // Get the address of the first line. IntPtr ptr = bmpData.Scan0; // Declare an array to hold the bytes of the bitmap. int bytes = Math.Abs(bmpData.Stride) * image.Height; byte[] rgbValues = new byte[bytes]; // Copy the RGB values into the array. System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes); enc.WriteFrame(rgbValues); // Unlock the bits. image.UnlockBits(bmpData); } enc.CloseEncode(); MessageBox.Show("Test encode done."); }
public About() { InitializeComponent(); Icon = Main.EmbeddedIcon; string version = Assembly.GetExecutingAssembly().GetCustomAttribute <AssemblyFileVersionAttribute>().Version; string cr = Assembly.GetExecutingAssembly().GetCustomAttribute <AssemblyCopyrightAttribute>().Copyright; labelCopyright.Text = "Duplicator V" + version + Environment.NewLine + cr; var sb = new StringBuilder(); sb.AppendLine("* System:"); sb.AppendLine(); sb.AppendLine(" OS: " + Environment.OSVersion); sb.AppendLine(" Processors: " + Environment.ProcessorCount); sb.AppendLine(" Bitness: " + (Environment.Is64BitProcess ? "64" : "32") + "-bit"); sb.AppendLine(); sb.AppendLine("* Detected H264 encoders:"); sb.AppendLine(); foreach (var dec in H264Encoder.Enumerate()) { sb.AppendLine(" " + dec.FriendlyName); sb.AppendLine(" Clsid : " + dec.Clsid); sb.AppendLine(" DllPath : " + dec.DllPath); sb.AppendLine(" Flags : " + dec.Flags); sb.AppendLine(" D3D11 Aware: " + dec.IsDirect3D11Aware); sb.AppendLine(" Supported Input types:"); foreach (var type in dec.InputTypes) { sb.AppendLine(" " + type); } sb.AppendLine(); } textBoxInfo.Text = sb.ToString(); }
public async Task ConversionStreamTest() { var vpath = Res.GetPath(Res.Video_Mp4); var apath = Res.GetPath(Res.Audio_Mp3); var opath = "out-test-av-2.mp4"; try { using var vreader = new VideoReader(vpath); await vreader.LoadMetadataAsync(); vreader.Load(); using var areader = new AudioReader(apath); await areader.LoadMetadataAsync(); areader.Load(); // Get video and audio stream metadata var vstream = vreader.Metadata.GetFirstVideoStream(); var astream = areader.Metadata.GetFirstAudioStream(); var encoder = new H264Encoder { Format = "flv" }; using (var filestream = File.Create(opath)) { // Prepare writer (Converting to H.264 + AAC video) using (var writer = new AudioVideoWriter(filestream, vstream.Width.Value, vstream.Height.Value, vstream.AvgFrameRateNumber, astream.Channels.Value, astream.SampleRateNumber, 16, encoder.Create(), new AACEncoder().Create())) { // Open for writing (this starts the FFmpeg process) writer.OpenWrite(); // Copy raw data directly from stream to stream var t2 = areader.DataStream.CopyToAsync(writer.InputDataStreamAudio); var t1 = vreader.DataStream.CopyToAsync(writer.InputDataStreamVideo); await t1; await t2; } } using var video = new VideoReader(opath); await video.LoadMetadataAsync(); Assert.True(video.Metadata.Streams.Length == 2); vstream = video.Metadata.GetFirstVideoStream(); astream = video.Metadata.GetFirstAudioStream(); Assert.True(Math.Abs(vstream.AvgFrameRateNumber - vreader.Metadata.AvgFramerate) < 0.1); Assert.True(Math.Abs(video.Metadata.Duration - vreader.Metadata.Duration) < 0.2); Assert.True(vstream.Width.Value == vreader.Metadata.Width); Assert.True(vstream.Height.Value == vreader.Metadata.Height); Assert.True(astream.SampleRateNumber == areader.Metadata.SampleRate); } finally { if (File.Exists(opath)) { File.Delete(opath); } } }
public Form1() { InitializeComponent(); enc = new H264Encoder("192.168.1.168", "admin", "!3nc0d3r!"); }
private async Task RunLocal() { var ct = ProcessCancel.Token; var taskSet = new HashSet <Task>(); var blockSize = (int)(Param.SamplingRate * Param.SampleLength); Logger.LogInformation($"[loop5] blockSize {blockSize}"); var audioInterval = (long)(10_000_000.0 * Param.SampleLength); Logger.LogInformation($"[loop5] audioInterval {audioInterval}"); var videoInterval = (long)(10_000_000.0 / Param.MaxFrameRate); Logger.LogInformation($"[loop5] videoInterval {videoInterval}"); var counter = new ElapsedTimeCounter(); using var audioWaiter = new Waiter(counter, audioInterval); using var videoWaiter = new Waiter(counter, videoInterval); using var buf = new IPCBuffer <float>(Param.EffectName, blockSize * 2 * Param.BufferCount, LoggerFactory); using var vstBufferPool = new BufferPool <VstBuffer2 <float> >(Param.BufferCount, () => new VstBuffer2 <float>(blockSize, 2, buf), LoggerFactory); using var pcmPool = new BufferPool <PcmBuffer <float> >(Param.BufferCount, () => new PcmBuffer <float>(blockSize, 2), LoggerFactory); using var audioPool = new BufferPool <OpusOutputBuffer>(Param.BufferCount, () => new OpusOutputBuffer(5000), LoggerFactory); using var pcmDrowPool = new BufferPool <PcmBuffer <float> >(Param.BufferCount, () => new PcmBuffer <float>(blockSize, 2), LoggerFactory); using var bmpPool = new BufferPool <H264InputBuffer>(Param.BufferCount, () => new H264InputBuffer(Param.Width, Param.Height), LoggerFactory); using var videoPool = new BufferPool <H264OutputBuffer>(Param.BufferCount, () => new H264OutputBuffer(200000), LoggerFactory); using var vst = new AudioMaster <float>(Param.SamplingRate, blockSize, LoggerFactory, counter, DllManager); using var toPcm = new ToPcm <float>(LoggerFactory, counter); using var opus = new OpusEncoder(SamplingRate.Sampling48000, Channels.Stereo, LoggerFactory, counter); using var fft = new FFTEncoder(Param.Width, Param.Height, Param.MaxFrameRate, LoggerFactory, counter); using var h264 = new H264Encoder(Param.Width, Param.Height, Param.TargetBitrate, Param.MaxFrameRate, LoggerFactory, counter); var effect = vst.AddEffect(Param.EffectName); var options = new ExecutionDataflowBlockOptions { CancellationToken = ct, MaxDegreeOfParallelism = 1, MaxMessagesPerTask = DataflowBlockOptions.Unbounded }; { var audioBlock = new ActionBlock <VstBuffer2 <float> >(async buffer => { var pcmTask = pcmPool.ReceiveAsync(ct); var audioTask = audioPool.ReceiveAsync(ct); buffer.Log.Clear(); audioWaiter.Wait(); buffer.Log.Add("[audio] start", counter.NowTicks); //VST var nowTime = counter.NowTicks / 10; effect.ProcessEvent(nowTime, MidiEventInput); effect.ProcessReplacing(nowTime, buffer); //trans var pcm = await pcmTask.ConfigureAwait(false); buffer.Log.Add("[audio] opus input get", counter.NowTicks); toPcm.Execute(buffer, pcm); vstBufferPool.Post(buffer); var audio = await audioTask.ConfigureAwait(false); pcm.Log.Add("[audio] opus output get", counter.NowTicks); opus.Execute(pcm, audio); pcmDrowPool.Post(pcm); //FTL // ftl.Execute(audio); audioPool.Post(audio); }, options); taskSet.Add(audioBlock.Completion); vstBufferPool.LinkTo(audioBlock); } { var midiDataStoreBlock = new ActionBlock <MIDIMessageEvent2>(buffer => { fft.Receive(buffer); }, options); taskSet.Add(midiDataStoreBlock.Completion); MidiEventOutput.LinkTo(midiDataStoreBlock); var pcmDataStoreBlock = new ActionBlock <PcmBuffer <float> >(buffer => { fft.Receive(buffer); pcmPool.Post(buffer); }, options); taskSet.Add(pcmDataStoreBlock.Completion); pcmDrowPool.LinkTo(pcmDataStoreBlock); var intraFrameCount = 0.0; var videoBlock = new ActionBlock <H264InputBuffer>(async buffer => { var videoTask = videoPool.ReceiveAsync(ct); buffer.Log.Clear(); videoWaiter.Wait(); buffer.Log.Add("[video] start", counter.NowTicks); //FFT fft.Execute(buffer); //H264 var video = await videoTask.ConfigureAwait(false); buffer.Log.Add("[video] h264 output get", counter.NowTicks); var insertIntraFrame = (intraFrameCount <= 0); h264.Execute(buffer, video, insertIntraFrame); bmpPool.Post(buffer); if (insertIntraFrame) { intraFrameCount = Param.IntraFrameIntervalUs; } intraFrameCount -= videoInterval; //FTL //ftl.Execute(video); videoPool.Post(video); }, options); taskSet.Add(videoBlock.Completion); bmpPool.LinkTo(videoBlock); } while (taskSet.Count > 0) { var task = await Task.WhenAny(taskSet).ConfigureAwait(false); taskSet.Remove(task); if (task.IsFaulted) { ProcessCancel.Cancel(); Logger.LogError(task.Exception, "Process Exception"); } } }
public static void DoWork(Object pUri) { string uri = pUri.ToString(); int testnumber = 0; int width = 640; int height = 480; int fps = 30; H264Encoder enc = new H264Encoder(); //enc.SetupEncode(@"E:\TEMP\encoded.mp4", width, height, fps); enc.SetupEncode(uri, width, height, fps); // "rtsp://127.0.0.1:8554/live.sdp"; Font drawFont = new Font("Arial", 16); SolidBrush drawBrush = new SolidBrush(Color.Red); StringFormat drawFormat = new StringFormat(); drawFormat.FormatFlags = StringFormatFlags.FitBlackBox; float font_x = 480.0F; float font_y = 450.0F; string drawString; Bitmap image = new Bitmap(width, height); Graphics g = Graphics.FromImage(image); g.FillRectangle(Brushes.Black, 0, 0, width, height); Brush[] brushList = new Brush[] { Brushes.Green, Brushes.Red, Brushes.Yellow, Brushes.Pink, Brushes.LimeGreen }; Random rnd = new Random(); for (int j = 0; j < 100; j++) { for (int i = 0; i < 100; i++) { int rndTmp = rnd.Next(1, 3); g.FillRectangle(brushList[i % 5], (i % width) * 2, (i % height) * 0.5f, i % 30, i % 30); g.FillRectangle(brushList[i % 5], (i % width) * 2, (i % height) * 2, i % 30, i % 30); g.FillRectangle(brushList[i % 5], (i % width) * 0.5f, (i % height) * 2, i % 30, i % 30); drawString = String.Format("i:{0},T:{1}", i, testnumber); g.FillRectangle(Brushes.Black, font_x, font_y, width, height); g.DrawString(drawString, drawFont, drawBrush, font_x, font_y, drawFormat); g.Save(); Rectangle rect = new Rectangle(0, 0, image.Width, image.Height); BitmapData bmpData = image.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); // Get the address of the first line. IntPtr ptr = bmpData.Scan0; // Declare an array to hold the bytes of the bitmap. int bytes = Math.Abs(bmpData.Stride) * image.Height; byte[] rgbValues = new byte[bytes]; // Copy the RGB values into the array. System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes); enc.WriteFrame(rgbValues); testnumber++; System.Threading.Thread.Sleep(10); // Unlock the bits. image.UnlockBits(bmpData); } g.FillRectangle(Brushes.Black, 0, 0, width, height); } }