private void H264Decode(string path, int fps) { var decoder = new OpenH264Lib.Decoder("openh264-1.7.0-win32.dll"); var aviFile = System.IO.File.OpenRead(path); var riff = new RiffFile(aviFile); var frames = riff.Chunks.OfType <RiffChunk>().Where(x => x.FourCC == "00dc"); var enumerator = frames.GetEnumerator(); var timer = new System.Timers.Timer(1000 / fps) { SynchronizingObject = this, AutoReset = true }; timer.Elapsed += (s, e) => { if (enumerator.MoveNext() == false) { timer.Stop(); return; } var chunk = enumerator.Current; var frame = chunk.ReadToEnd(); var image = decoder.Decode(frame, frame.Length); if (image == null) { return; } pbxScreen.Image = image; }; timer.Start(); }
protected void DecodeFrame(OpenH264Lib.Decoder decoder, byte[] frame, DateTime timestamp) { Bitmap bmp = decoder.Decode(frame, frame.Length); if (bmp != null) { FrameQueue.Enqueue(new SpeedometerFrame(bmp, timestamp)); } }
private void BtnTestA_Click(object sender, EventArgs e) { var dialog = new OpenFileDialog() { Multiselect = true }; dialog.Filter = "Image Files (*.bmp, *.png, *.jpg)|*.bmp;*.png;*.jpg"; if (dialog.ShowDialog() == DialogResult.OK) { using (OpenH264Lib.Encoder encoder = new OpenH264Lib.Encoder(dllPath)) using (OpenH264Lib.Decoder decoder = new OpenH264Lib.Decoder(dllPath)) { // 1フレームエンコードするごとにライターに書き込み OpenH264Lib.Encoder.OnEncodeCallback onEncode = (data, length, frameType) => { bool keyFrame = (frameType == OpenH264Lib.Encoder.FrameType.IDR) || (frameType == OpenH264Lib.Encoder.FrameType.I); Bitmap image = decoder.Decode(data.Take(length).ToArray(), length); Image old = pbxScreen.Image; pbxScreen.Image = image; if (old != null) { old.Dispose(); } Application.DoEvents(); }; for (int i = 0; i < dialog.FileNames.Length; i++) { using (Bitmap bmp = new Bitmap(dialog.FileNames[i])) { if (i == 0) { encoder.Setup(bmp.Width, bmp.Height, 5000000, (float)nudFps.Value, 2.0f, onEncode); } encoder.Encode(bmp, nudFps.Value == 0 ? 0.5f : i / (float)nudFps.Value); } } } } }
private void Receive(Socket socket) { while (!abort) { try { byte[] num = new byte[4]; socket.Receive(num); byte[] buffer = new byte[BitConverter.ToInt32(num, 0)]; socket.Receive(buffer); Image frame; try { frame = decoder.Decode(buffer, buffer.Length); } catch { frame = null; } Image cont = videoFrame.Image; if (frame != null) { videoFrame.Image = (Image)frame.Clone(); frame.Dispose(); } if (cont != null) { cont.Dispose(); } num = null; buffer = null; } catch { CloseVideoRoom(); } } }
private void StartTcpClient() { try { tcpClient = new TcpClient(); tcpClient.Connect(this.HostName, this.Port); NetworkStream stream = tcpClient.GetStream(); var binary = new BinaryFormatter(); binary.Serialize(stream, (uint)ClientType.Receiver); binary.Serialize(stream, GuidTransmitters.ToByteArray()); //Reader Task.Factory.StartNew(() => { var decoder = new OpenH264Lib.Decoder("openh264-2.0.0-win32.dll"); while (tcpClient?.Connected == true) { try { var bin = new BinaryFormatter(); var data = (byte[])bin.Deserialize(stream); var bmp = decoder.Decode(data, data.Length); if (bmp != null) { System.Windows.Application.Current?.Dispatcher?.Invoke(() => { SetBmp(bmp); this.InvalidateVisual(); }); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine($"Failed read data. Ex: {ex.Message}"); } } }); //Writer Task.Factory.StartNew(() => { while (tcpClient?.Connected == true) { var data = sendData.Dequeue(); if (stream.CanWrite) { var bin = new BinaryFormatter(); bin.Serialize(stream, data); } else { sendData.Enqueue(data); } } }); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine($"Failed connection. Ex: {ex.Message}"); } }