Ejemplo n.º 1
0
        /// <summary>
        /// Calback function for new frame
        /// </summary>
        /// <param name="videoFrame"></param>
        /// <param name="audioPacket"></param>
        public void VideoInputFrameArrived(IDeckLinkVideoInputFrame videoFrame, IDeckLinkAudioInputPacket audioPacket)
        {
            // The conditions under which either of these cases occur are unclear but an overstreched processor doesn't help
            if (videoFrame == null || audioPacket == null)
            {
                return;
            }

            IDeckLinkTimecode timecode;

            videoFrame.GetTimecode(config.TimecodeFormat, out timecode);

            if (audioPacket.GetSampleFrameCount() != config.SamplesPerSlot * config.SlotsPerFrame)
            {
                throw new ApplicationException("Wrong buffer size");
            }

            IntPtr buffer;

            audioPacket.GetBytes(out buffer);
            var audioFingerprints = GetAudioFingerprints(buffer, config);

            var timecodeBcd = timecode?.GetBCD() ?? 0;

            FingerprintCreated?.Invoke(this, new FingerprintEventArgs(timecodeBcd, (byte)config.SlotsPerFrame, 0, audioFingerprints));

            // The documentation suggests that neither of these are necessary
            // BM's own code does the former
            // Including these doesn't make anything go bang so, in for a penny...
            Marshal.ReleaseComObject(videoFrame);
            Marshal.ReleaseComObject(audioPacket);
        }
Ejemplo n.º 2
0
		void IDeckLinkInputCallback.VideoInputFrameArrived(IDeckLinkVideoInputFrame iVideoInputFrame, IDeckLinkAudioInputPacket iAudioPacket)
		{
            try
            {
                if (bAVFrameArrivedAttached)
                {
                    IDeckLinkVideoFrame iVideoFrame = iVideoInputFrame;
                    IntPtr pBytesVideo = IntPtr.Zero, pBytesAudio = IntPtr.Zero;
                    int nBytesVideoQty = 0, nBytesAudioQty = 0;
                    if (null != iVideoFrame)
                    {
                        if (null != _iDLVideoConversion)
                        {
                            _iDLVideoConversion.ConvertFrame(iVideoFrame, _iVideoFrameTarget);
                            iVideoFrame = _iVideoFrameTarget;
                        }
                        _iVideoFrameLast = iVideoFrame;
                    }
                    else
                    {
                        nFramesDroppedVideo++;
                        iVideoFrame = _iVideoFrameLast;
                        (new Logger()).WriteWarning("video frame dropped");
                    }
                    if (null != iVideoFrame)
                    {
                        iVideoFrame.GetBytes(out pBytesVideo);
                        if (IntPtr.Zero != pBytesVideo)
                        {
                            nBytesVideoQty = iVideoFrame.GetRowBytes() * iVideoFrame.GetHeight();
                            nFramesVideo++;
                        }
                        else
                            (new Logger()).WriteWarning("video frame is empty");
                    }
                    if (null != iAudioPacket)
                    {
                        iAudioPacket.GetBytes(out pBytesAudio);
                        if (IntPtr.Zero != pBytesAudio)
                        {
                            nBytesAudioQty = iAudioPacket.GetSampleFrameCount() * ((int)_BMDAudioSampleType.bmdAudioSampleType16bitInteger / 8) * 2;
                            nFramesAudio++;
                        }
                        else
                            (new Logger()).WriteWarning("audio frame is empty");
                    }
                    else
                    {
                        nFramesDroppedAudio++;
                        (new Logger()).WriteWarning("audio frame dropped");
                    }
                    OnAVFrameArrived(nBytesVideoQty, pBytesVideo, nBytesAudioQty, pBytesAudio);
                }
            }
            catch (Exception ex)
            {
                (new Logger()).WriteError(ex);
            }
        }
Ejemplo n.º 3
0
        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);
                }
            }
        }
Ejemplo n.º 4
0
        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);
                }
            }
        }