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); } } } } }
protected void streamingThreadLoop() { try { CancellationToken cancellationToken = streamingCancelTokenSource.Token; Uri serverUri = new Uri(Url); ConnectionParameters connectionParameters; if (!string.IsNullOrWhiteSpace(Username)) { connectionParameters = new ConnectionParameters(serverUri, new NetworkCredential(Username, Password)); } else { connectionParameters = new ConnectionParameters(serverUri); } connectionParameters.RtpTransport = RtpTransportProtocol.TCP; string openH264DllPath = Globals.ApplicationDirectoryBase + "openh264-1.8.0-win" + (Environment.Is64BitProcess ? "64" : "32") + ".dll"; using (OpenH264Lib.Decoder decoder = new OpenH264Lib.Decoder(openH264DllPath)) using (RtspClient rtspClient = new RtspClient(connectionParameters)) { rtspClient.FrameReceived += (sender2, frame) => { //process (e.g. decode/save to file) encoded frame here or //make deep copy to use it later because frame buffer (see FrameSegment property) will be reused by client if (frame is RawH264IFrame) { RawH264IFrame iFrame = frame as RawH264IFrame; DecodeFrame(decoder, iFrame.SpsPpsSegment.ToArray(), frame.Timestamp); DecodeFrame(decoder, iFrame.FrameSegment.ToArray(), frame.Timestamp); } else if (frame is RawH264PFrame) { DecodeFrame(decoder, frame.FrameSegment.ToArray(), frame.Timestamp); } }; rtspClient.ConnectAsync(cancellationToken).Wait(); rtspClient.ReceiveAsync(cancellationToken).Wait(); } } catch (ThreadAbortException) { } catch (TaskCanceledException) { Stop(); } catch (Exception ex) { if (ex is AggregateException) { AggregateException aex = ex as AggregateException; if (aex.InnerExceptions.Count == 1 && aex.InnerException is TaskCanceledException) { return; } } Logger.Debug(ex); OnError(this, ex); Stop(); } }
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}"); } }