public void VideoInputFrameArrived(IDeckLinkVideoInputFrame videoFrame, IDeckLinkAudioInputPacket audioPacket) { try { if (videoFrame != null) { IntPtr srcPtr; videoFrame.GetBytes(out srcPtr); System.Runtime.InteropServices.Marshal.Copy(srcPtr, mediaFrameBuffer, 0, videoFrame.GetRowBytes() * videoFrame.GetHeight()); pipeServer.SendPacket(mediaFrameBuffer, 0, videoFrame.GetRowBytes() * videoFrame.GetHeight()); System.Runtime.InteropServices.Marshal.ReleaseComObject(videoFrame); //unsafe //{ // fixed (byte* p = mediaFrameBuffer) // { // IntPtr ptr = (IntPtr)p; // Bitmap bmp = new Bitmap(960, 1080, 1920*2, System.Drawing.Imaging.PixelFormat.Format32bppRgb, ptr); // frameCount++; // //if (frameCount % 25 == 0) // { // if (InvokeRequired) this.Invoke((Action)(() => // { // //this.frameCounterLabel.Text = "Frame count: " + frameCount++; // pictureBox.Image = bmp; // })); // } // } //} } if (audioPacket != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(audioPacket); } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }
// Video Input Frame Arrival public void VideoInputFrameArrived(IDeckLinkVideoInputFrame videoFrame, IDeckLinkAudioInputPacket audioPacket) { IntPtr data; int size = videoFrame.GetRowBytes() * videoFrame.GetHeight(); videoFrame.GetBytes(out data); byte[] array = new byte[size]; Marshal.Copy(data, array, 0, size); frameBuffer = array; /*int pixel; * bool fullVideo = false; * bool fullGraphics = false; * bool raceTag = false; * bool doubleBox = false; * bool lowerThird = false; * bool racing = false; * * Console.Clear(); * * // Race Tag * if (PixelMatchesColor(array, 64200, 142, 48, 115) || PixelMatchesColor(array, 64200, 142, 48, 116)) * { * raceTag = true; * Console.WriteLine("Race Tag: TRUE"); * } * else * Console.WriteLine("Race Tag: FALSE"); * PrintColor(array, 64200); * * // Full Graphics * if (PixelMatchesColor(array, 25620, 141, 35, 118)) * { * fullGraphics = true; * Console.WriteLine("Full Graphics: TRUE"); * } * else * Console.WriteLine("Full Graphics: FALSE"); * PrintColor(array, 25620); * * // Lower Third * if (PixelMatchesColor(array, 832030, 140, 61, 119)) * { * lowerThird = true; * Console.WriteLine("Lower Third: TRUE"); * } * else * Console.WriteLine("Lower Third: FALSE"); * PrintColor(array, 832030); * * // Full Video * if (!PixelMatchesColor(array, 256150, 131, 216, 126)) * { * fullVideo = true; * Console.WriteLine("Full Video: TRUE"); * } * else * Console.WriteLine("Full Video: FALSE"); * PrintColor(array, 256150); * * // Double Box * if (PixelMatchesColor(array, 705200, 143, 48, 115)) * { * doubleBox = true; * Console.WriteLine("Double Box: TRUE"); * } * else * Console.WriteLine("Double Box: FALSE"); * PrintColor(array, 705200); * * // Racing * if (raceTag && !lowerThird && fullVideo && !fullGraphics) * { * racing = true; * Console.WriteLine("Racing: TRUE"); * } * else * Console.WriteLine("Racing: FALSE"); */ }
public void VideoInputFrameArrived(IDeckLinkVideoInputFrame videoFrame, IDeckLinkAudioInputPacket audioPacket) { if (videoFrame == null && audioPacket == null) { return; } try { if (!_Streaming) { return; } if (!_VideoLock.Wait(TimeSpan.Zero)) { return; } try { Interlocked.Increment(ref _FrameCount); Run(() => frameCount.Text = _FrameCount.ToString()); if (videoFrame != null) { var rowBytes = videoFrame.GetRowBytes(); var height = videoFrame.GetHeight(); IntPtr framePointer; videoFrame.GetBytes(out framePointer); var frame = new byte[rowBytes * height]; Marshal.Copy(framePointer, frame, 0, frame.Length); if (writeRaw.Checked) { _VideoWriter.Write(frame); } if (writeEncoded.Checked) { _EncodeTask.Write(frame, 0, frame.Length); } } if (audioPacket != null) { IntPtr audioPointer; audioPacket.GetBytes(out audioPointer); var frameCount = audioPacket.GetSampleFrameCount(); var audio = new byte[frameCount * _AudioChannels * (_AudioSampleDepth / 8)]; Marshal.Copy(audioPointer, audio, 0, audio.Length); if (writeRaw.Checked) { _AudioWriter.Write(audio); } } } finally { _VideoLock.Release(); } } finally { if (videoFrame != null) { Marshal.ReleaseComObject(videoFrame); } if (audioPacket != null) { Marshal.ReleaseComObject(audioPacket); } } }
void IDeckLinkInputCallback.VideoInputFrameArrived(IDeckLinkVideoInputFrame videoFrame, IDeckLinkAudioInputPacket audioPacket) { if (videoFrame != null) { try { var frameFlags = videoFrame.GetFlags(); bool inputSignal = frameFlags.HasFlag(_BMDFrameFlags.bmdFrameHasNoInputSource); if (inputSignal != validInputSignal) { validInputSignal = inputSignal; InputSignalChanged?.Invoke(validInputSignal); } else { int width = videoFrame.GetWidth(); int height = videoFrame.GetHeight(); int stride = videoFrame.GetRowBytes(); var format = videoFrame.GetPixelFormat(); var bufferLength = stride * height; videoFrame.GetBytes(out IntPtr pBuffer); VideoDataArrived?.Invoke(pBuffer, bufferLength, 0); //var f = File.Create(@"d:\testBMP2\" + DateTime.Now.ToString("HH_mm_ss_fff") + " " + width + "x" + height + "_" + format + ".raw"); //byte[] data = new byte[bufferLength]; //Marshal.Copy(pBuffer, data, 0, data.Length); //f.Write(data, 0, data.Length); //f.Close(); } } finally { Marshal.ReleaseComObject(videoFrame); } } if (audioPacket != null) { try { long packetTime = 0; //audioPacket.GetPacketTime(out packetTime, 30000); int sampleSize = ((int)AudioSampleType / 8); //32bit int samplesCount = audioPacket.GetSampleFrameCount(); int dataLength = sampleSize * AudioChannelsCount * samplesCount; if (dataLength > 0) { audioPacket.GetBytes(out IntPtr pBuffer); if (pBuffer != IntPtr.Zero) { byte[] data = new byte[dataLength]; Marshal.Copy(pBuffer, data, 0, data.Length); AudioDataArrived?.Invoke(data, packetTime); } } } finally { Marshal.ReleaseComObject(audioPacket); } } }