public void SetNewLiveFrameCallback(OnNewLiveFrame callbackFunction) { m_onNewLiveFrame = callbackFunction; }
public static AsyncRequestObject LiveStream(Socket socket, OnNewLiveFrame NewLiveFrameCallback, CancellationTokenSource cts = null, TaskScheduler ts = null) { AsyncRequestObject aro = new AsyncRequestObject(cts); ts = ts == null?TaskScheduler.FromCurrentSynchronizationContext() : ts; BlockingCollection <LiveCompressedFrame> queueCompressed = new BlockingCollection <LiveCompressedFrame>(1); #region Streaming task aro.task = Task.Factory.StartNew(() => { byte[] bytes = null; HiResTimer t = new HiResTimer(); Int64 t1 = t.Value; Int64 frames = 0; while (!aro.CancellationToken.WaitHandle.WaitOne(0)) { bytes = IPServices.ReceiveOnSocket(socket, aro.CancellationToken, 30000, Marshal.SizeOf(typeof(DATA_FRAME_HEADER))); if (bytes != null && bytes.Length != 0) { // Full header received GCHandle pinnedPacket = GCHandle.Alloc(bytes, GCHandleType.Pinned); DATA_FRAME_HEADER dfh = (DATA_FRAME_HEADER)Marshal.PtrToStructure(pinnedPacket.AddrOfPinnedObject(), typeof(DATA_FRAME_HEADER)); pinnedPacket.Free(); // Get frame bytes = IPServices.ReceiveOnSocket(socket, aro.CancellationToken, 5000, dfh.TotalLength - Marshal.SizeOf(typeof(DATA_FRAME_HEADER))); if (bytes != null && bytes.Length != 0) { //queueCompressed.Add(new LiveCompressedFrame(dfh, bytes), aro.CancellationToken); Debug.Print(dfh.CameraID.ToString() + " " + bytes.Length.ToString()); // add code here to put frame into decoder input queue } else { break; } } else { // Server closed connection? break; } ++frames; if (frames % 100 == 0) { Int64 timeTicks = t.Value - t1; Int64 timeElapseInSeconds = timeTicks / t.Frequency; Debug.Print("Frame rate = {0}", (float)frames / (float)timeElapseInSeconds); } } //fs.Close(); }, aro.CancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Default); #endregion return(aro); }