コード例 #1
0
 protected virtual void NewMediaFrame(MediaFrame frame)
 {
     if (!EnabledFrameSequence)
     {
         //不排序
         OnNewMediaFrame(frame);
     }
     else
     {
         //排序
         if (frame.nIsAudio == 0)
         {
             _qVideoMediaFrame.Enqueue(frame);
         }
         else if (frame.nIsAudio == 1)
         {
             _qAudioMediaFrame.Enqueue(frame);
         }
         while (true)
         {
             if (_qVideoMediaFrame.Count > 0 && _qAudioMediaFrame.Count > 0)
             {
                 var v = _qVideoMediaFrame.Peek();
                 var a = _qAudioMediaFrame.Peek();
                 if (v.nTimetick < a.nTimetick)
                 {
                     v = _qVideoMediaFrame.Dequeue();
                     OnNewMediaFrame(v);
                 }
                 else
                 {
                     a = _qAudioMediaFrame.Dequeue();
                     OnNewMediaFrame(a);
                 }
             }
             else if (_qVideoMediaFrame.Count > 5)
             {
                 var v = _qVideoMediaFrame.Dequeue();
                 OnNewMediaFrame(v);
             }
             else if (_qAudioMediaFrame.Count > 50)
             {
                 var a = _qAudioMediaFrame.Dequeue();
                 OnNewMediaFrame(a);
             }
             else
             {
                 break;
             }
         }
     }
 }
コード例 #2
0
 private void PlayThread()
 {
     _PlayBackwardThread = ThreadEx.ThreadCall(PlayBackwardThread);
     while (_isworking)
     {
         MediaFrame frame = null;
         if (_isForwardPlay)
         {
             lock (_queue) {
                 if (_queue.Count > 0 && !_isPaused)
                 {
                     frame = _queue.Dequeue();
                 }
                 else
                 {
                     frame = null;
                 }
             }
             if (frame != null)
             {
                 PlayForward(frame);
             }
             else
             {
                 ThreadEx.Sleep(10);
             }
         }
         else
         {
             lock (_queue) {
                 if (_queue.Count > 0 && !_isPaused)
                 {
                     frame = _queue.Dequeue();
                 }
                 else
                 {
                     frame = null;
                 }
             }
             if (frame != null)
             {
                 PlayBackward(frame);
             }
             else
             {
                 ThreadEx.Sleep(10);
             }
         }
     }
 }
コード例 #3
0
ファイル: ObjectPool.cs プロジェクト: zz110/GB28181.Solution
 public Item AllowItem()
 {
     lock (_qIdle)
     {
         if (_qIdle.Count > 0)
         {
             Item item = _qIdle.Dequeue();
             item.AddRef();
             lock (_listUsed)
             {
                 if (!_listUsed.Contains(item))
                 {
                     _listUsed.Add(item);
                 }
             }
             return(item);
         }
         if (_count < _max)
         {
             AddItem();
             return(AllowItem());
         }
         return(null);
     }
 }
コード例 #4
0
        private byte[] _Read(int len)
        {
            while (_size < len)
            {
                Thread.Sleep(10);
                //		bool flag = true;
            }
            int size = _size;

            byte[] array = new byte[len];
            int    num   = 0;

            lock (_queueMS.GetLock())
            {
                while (num < len)
                {
                    if (_curMS == null)
                    {
                        _curMS = _queueMS.Dequeue();
                    }
                    num += _curMS.Read(array, num, len - num);
                    if (_curMS.Length == _curMS.Position)
                    {
                        _curMS = null;
                    }
                }
                if (num != len)
                {
                    throw new Exception();
                }
                _size -= len;
            }
            return(array);
        }
コード例 #5
0
ファイル: AudioPlayer.cs プロジェクト: zz110/GB28181.Solution
        protected virtual void PlayThread()
        {
            MediaFrame frame = null;

            while (_isworking)
            {
                lock (_queue) {
                    if (_queue.Count > 0 && !_isPaused)
                    {
                        frame = _queue.Dequeue();
                    }
                    else
                    {
                        frame = null;
                    }
                }
                if (frame != null)
                {
                    if (Speed == 1)
                    {
                        var sleep = (int)(frame.NTimetick - _syncPlayTime);

                        if (sleep < -3000)
                        {
                            lock (_queue)
                            {
                                _queue.Clear();
                            }
                        }
                        if (sleep > 0 && !_reseting)
                        {
                            Thread.Sleep(sleep);
                        }
                    }
                    else
                    {
                        var sleep = (int)(frame.NTimetick - _syncPlayTime);
                        if (sleep > 0 && !_reseting)
                        {
                            Thread.Sleep(sleep);
                        }
                    }
                    if (!_reseting)
                    {
                        _Play(frame);
                        lock (_queue) {
                            if (_curPlayTime < frame.NTimetick && !_reseting)
                            {
                                _curPlayTime = frame.NTimetick;
                            }
                        }
                    }
                    _reseting = false;
                }
                else
                {
                    ThreadEx.Sleep(10);
                }
            }
        }
コード例 #6
0
        public void Should_Enqueue_Two_Items_And_Dequeue_First_Item_Test()
        {
            int expectedValue = 1;
            int expectedCount = 1;

            AQueue <int> queue = InitQueue();

            Assert.AreEqual(queue.Dequeue(), expectedValue);
            Assert.AreEqual(queue.Count, expectedCount);
        }
コード例 #7
0
 private static TimerExecuter GetIdleThread()
 {
     if (_queueIdleThreads.Count > 0)
     {
         return(_queueIdleThreads.Dequeue());
     }
     while (_queueCompleteThreads.Count > 0)
     {
         TimerExecuter timerExecuter = _queueCompleteThreads.Peek();
         timerExecuter = _queueCompleteThreads.Dequeue();
         _queueIdleThreads.Enqueue(timerExecuter);
     }
     if (_queueIdleThreads.Count > 0)
     {
         return(_queueIdleThreads.Dequeue());
     }
     if (_timerThreads.Count <= _maxThread)
     {
         return(CreateThread());
     }
     return(null);
 }
コード例 #8
0
 private void Completed(DespatcheStateModel model)
 {
     if (!_isDisposed)
     {
         _isBeging        = false;
         _lasyAsyncResult = null;
         lock (_lockObj)
         {
             if (_queueCompletedHandleInvoke == null)
             {
                 _queueCompletedHandleInvoke = new AQueue <DespatcheStateModel>();
             }
             _queueCompletedHandleInvoke.Enqueue(model);
             if (!_CompletedHandle_Invokeing)
             {
                 model = _queueCompletedHandleInvoke.Dequeue();
                 _CompletedHandle_Invokeing = true;
                 _CompletedHandle.BeginInvoke(model, _CompletedHandle_AsyncCallback, null);
             }
         }
     }
 }
コード例 #9
0
        private static void Thread2()
        {
            int num = 200;

            while (true)
            {
                bool flag      = true;
                long tickCount = TickCount;
                while (_queueTimers.Count > 0)
                {
                    TimerInfo timerInfo = null;
                    lock (_queueTimers)
                    {
                        if (_queueTimers.Count > 0)
                        {
                            TimerInfo timerInfo2 = _queueTimers.Dequeue();
                            if (timerInfo2 != null && _dicTimers.ContainsKey(timerInfo2.Key))
                            {
                                timerInfo = timerInfo2;
                            }
                        }
                    }
                    if (timerInfo == null)
                    {
                        continue;
                    }
                    TimerExecuter timerExecuter = null;
                    while (true)
                    {
                        flag          = true;
                        timerExecuter = GetIdleThread();
                        if (timerExecuter != null)
                        {
                            break;
                        }
                        Thread.Sleep(1);
                    }
                    if (timerInfo == null)
                    {
                        throw new Exception("item null");
                    }
                    timerExecuter.Run(timerInfo);
                }
                long tickCount2 = TickCount;
                if (tickCount2 - tickCount < 5)
                {
                    Thread.Sleep(5);
                }
            }
        }
コード例 #10
0
        private void PlayBackwardThread()
        {
            long lastTick = 0;

            while (_isworking)
            {
                Stack <MediaFrame> stack = null;
                lock (_queue) {
                    if (_queueRewindStack.Count > 0)
                    {
                        stack = _queueRewindStack.Dequeue();
                    }
                }
                if (stack != null)
                {
                    while (stack.Count > 0 && !_PlayBackwardResetPos)
                    {
                        var frame = stack.Pop();
                        //_drawHandle.BeginInvoke(frame.Data, null, null);
                        _drawHandle(frame.Data);
                        if (lastTick == 0)
                        {
                            lastTick = frame.nTimetick;
                        }
                        int sleep = (int)(lastTick - frame.nTimetick);
                        if (sleep < 0 || sleep >= 100)
                        {
                            sleep = 40;
                        }
                        ThreadEx.Sleep(sleep);
                        //Console.WriteLine(sleep + "   " + (lastTick - frame.nTimetick)+"    "+(Environment.TickCount - sysTick));
                        lock (_queue) {
                            if (!_PlayBackwardResetPos)
                            {
                                _curPlayMediaTimetick = lastTick = frame.nTimetick;
                            }
                        }
                        lock (_queue) {
                            _lastPlaySystemTime = Environment.TickCount;
                        }
                    }
                    GC.Collect();
                }
                else
                {
                    ThreadEx.Sleep(10);
                }
            }
        }
コード例 #11
0
ファイル: PSAnalyze.cs プロジェクト: zz110/GB28181.Solution
        public List <MediaFrame> ReadFrames()
        {
            if (!_isWorking)
            {
                return(new List <MediaFrame>());
            }

            var result = new List <MediaFrame>();

            while (_queueFrame.Count > 0)
            {
                var item = _queueFrame.Dequeue();
                result.Add(item);
            }
            return(result);
        }
コード例 #12
0
        private void PlayThread()
        {
            bool _needBuffer = BuffTime > 0;
            bool _needSleep  = false;

            while (_isworking)
            {
                lock (_queue) {
                    if (_queue.Count > 0 && !_needBuffer && !_isPaused)
                    {
                        var frame = _queue.Dequeue();
                        if (_firstFrameTime == 0)
                        {
                            _firstFrameTime = frame.nTimetick;
                        }
                        if (_startPlayTick == 0)
                        {
                            _startPlayTick = Environment.TickCount;
                        }

                        if (frame.nIsAudio == 0)
                        {
                            if (_firstVideoFrameTime == 0)
                            {
                                _firstVideoFrameTime = frame.nTimetick;
                            }
                        }
                        else if (frame.nIsAudio == 1 && (_vp.IsPlaying || _IgnoreVideoPlay) && ForwardPlay)
                        {
                            if (_firstAudioFrameTime == 0)
                            {
                                _firstAudioFrameTime = frame.nTimetick;
                            }
                        }

                        if (_firstVideoFrameTime != 0)
                        {
                            _vp.SyncPlayTime(_firstVideoFrameTime + (int)((Environment.TickCount - _startPlayTick) * 1));
                        }

                        if (_firstAudioFrameTime != 0)
                        {
                            _ap.SyncPlayTime(_firstAudioFrameTime + (int)((Environment.TickCount - _startPlayTick) * 1));
                        }

                        if (frame.nIsAudio == 0)
                        {
                            _vp.Play(frame);
                        }
                        else if (frame.nIsAudio == 1 && (_vp.IsPlaying || _IgnoreVideoPlay) && ForwardPlay)
                        {
                            _ap.Play(frame);
                        }
                        _needSleep = false;
                    }
                    else
                    {
                        if (!_isPaused)
                        {
                            if (_firstVideoFrameTime != 0)
                            {
                                _vp.SyncPlayTime(_firstVideoFrameTime + (int)((Environment.TickCount - _startPlayTick) * 1));
                            }

                            if (_firstAudioFrameTime != 0)
                            {
                                _ap.SyncPlayTime(_firstAudioFrameTime + (int)((Environment.TickCount - _startPlayTick) * 1));
                            }
                            if (_queue.Count > 0 && _needBuffer)
                            {
                                var mf = _queue.Peek();
                                if (_lastReceiveFrameTick - mf.nTimetick > BuffTime * 1000)
                                {
                                    _needBuffer = false;
                                }
                            }
                        }
                        _needSleep = true;
                    }
                }
                if (_needSleep)
                {
                    Thread.Sleep(10);
                }
            }
        }