public override void PacketReceived(UdpPacket udpPacket)
 {
     try
     {
         using (Packet packet = PacketPool.Acquire())
         {
             packet.UdpPacket = udpPacket;
             packet.Frame     = packet.UdpPacket.ReadIntVB();
             if (packet.Frame > this._remoteFrameActual)
             {
                 this._remoteFrameAdjust = true;
                 this._remoteFrameActual = packet.Frame;
             }
             this._bitsSecondInAcc += packet.UdpPacket.Size;
             this._packetsReceived++;
             for (int i = 0; i < this._channels.Length; i++)
             {
                 this._channels[i].Read(packet);
             }
             this._packetStatsIn.Enqueue(packet.Stats);
         }
     }
     catch (Exception exception)
     {
         BoltLog.Exception(exception);
         ModAPI.Log.Write(exception.ToString());
     }
 }
Example #2
0
        public void WriteOverflowTest()
        {
            const int PACKET_SIZE = 12;

            var pool   = new PacketPool(PACKET_SIZE, 1);
            var packet = pool.Borrow();
            var bytes  = new byte[PACKET_SIZE + 1];

#if NETFX_CORE
            Assert.ThrowsException <ArgumentException>(() => packet.WriteBytes(bytes));
#else
            packet.WriteBytes(bytes);
#endif
        }
Example #3
0
    public void RegisterPacketHandler <TPacket>(PacketId packetId, IPacketQueueHandler <TPacket> handler) where TPacket : Packet, new()
    {
        if (!this.registeredPacketHandlerActions.ContainsKey(packetId))
        {
            this.registeredPacketHandlerActions[packetId] = new();
        }

        var pool = new PacketPool <TPacket>();

        this.registeredPacketHandlerActions[packetId].Add((client, data) =>
        {
            var packet = pool.GetPacket();
            packet.Read(data);
            handler.EnqueuePacket(client, packet);
        });
        handler.PacketHandled += (packet) => pool.ReturnPacket(packet);
    }
Example #4
0
        public void ReadOverflowTest()
        {
            const int PACKET_SIZE = 12;

            var pool   = new PacketPool(PACKET_SIZE, 1);
            var packet = pool.Borrow();
            var bytes  = new byte[4];

            packet.WriteBytes(bytes);
            packet.ResetAndMakeReadOnly(0);

#if NETFX_CORE
            Assert.ThrowsException <ArgumentException>(() => packet.ReadBytes(bytes.Length + 1));
#else
            packet.ReadBytes(bytes.Length + 1);
#endif
        }
Example #5
0
        public void TestReadWriteVariableLengthInt32()
        {
            int[] valuesToTest = { 0, 1, 127, 128, 16383, 16384, 2097151, 2097152, 268435455, 268435456, Int32.MaxValue };
            var   pool         = new PacketPool(5, 1);
            var   packet       = pool.Borrow();

            int valueAsRead;

            foreach (int i in valuesToTest)
            {
                packet.WriteVariableLengthInt32(i);
                packet.ResetAndMakeReadOnly(-1);
                valueAsRead = packet.ReadVariableLengthInt32();
                Assert.AreEqual(i, valueAsRead, "Value written: {0}, not as read: {1}", i, valueAsRead);
                packet.Init();
            }
        }
Example #6
0
        public void WriteReadBytesTest()
        {
            const int PACKET_SIZE = 10000;

            var pool   = new PacketPool(PACKET_SIZE, 1);
            var packet = pool.Borrow();
            var bytes  = new byte[PACKET_SIZE];
            var rand   = new Random();

            rand.NextBytes(bytes);

            packet.WriteBytes(bytes);

            Assert.AreEqual(PACKET_SIZE, packet.BytesWritten);

            packet.ResetAndMakeReadOnly(0);

            var readBytes = packet.ReadBytes(PACKET_SIZE);

            Assert.AreEqual(0, packet.BytesRemaining);
            Assert.IsTrue(Enumerable.SequenceEqual(bytes, readBytes), "Bytes written not the same when read!");
        }
Example #7
0
        public void RandomStressTest()
        {
            const int PACKET_SIZE  = 123;
            const int NUM_OF_LOOPS = 10000;

            var pool   = new PacketPool(PACKET_SIZE, 1);
            var packet = pool.Borrow();
            var rand   = new Random();

            for (var i = 0; i < NUM_OF_LOOPS; i++)
            {
                packet.Init();
                var bytes = new byte[rand.Next(PACKET_SIZE + 1)];
                rand.NextBytes(bytes);
                packet.WriteBytes(bytes);
                Assert.AreEqual(bytes.Length, packet.BytesWritten);
                packet.ResetAndMakeReadOnly(0);
                var numOfBytesToRead = rand.Next(bytes.Length + 1);
                var readBytes        = packet.ReadBytes(numOfBytesToRead);
                Assert.AreEqual(bytes.Length - numOfBytesToRead, packet.BytesRemaining);
                Assert.IsTrue(Enumerable.SequenceEqual(bytes.Take(numOfBytesToRead), readBytes.Take(numOfBytesToRead)), "Bytes written not the same when read!");
            }
        }
        public int Init(string fileName, System.Windows.Controls.Image image, System.Windows.Controls.Image Dimage, System.Windows.Controls.Label startTime, System.Windows.Controls.Slider slider, System.Windows.Controls.Slider slider2)
        {
            ffmpeg.avcodec_register_all();

            AVFormatContext *pFormatCtx;

            pFormatCtx      = ffmpeg.avformat_alloc_context();
            this.pFormatCtx = pFormatCtx;

            ffmpeg.avformat_open_input(&pFormatCtx, fileName, null, null);
            ffmpeg.avformat_find_stream_info(pFormatCtx, null);

            for (int i = 0; i < pFormatCtx->nb_streams; i++)
            {
                if (pFormatCtx->streams[i]->codec->codec_type == AVMediaType.AVMEDIA_TYPE_VIDEO)
                {
                    videoIndex = i;
                    Console.WriteLine("video.............." + videoIndex);
                }
                if (pFormatCtx->streams[i]->codec->codec_type == AVMediaType.AVMEDIA_TYPE_AUDIO)
                {
                    audioIndex = i;
                    Console.WriteLine("audio.............." + audioIndex);
                }
            }

            if (videoIndex == -1)
            {
                Console.WriteLine("Couldn't find a video stream.");
                return(-1);
            }
            if (audioIndex == -1)
            {
                Console.WriteLine("Couldn't find a audio stream.");
                return(-1);
            }

            if (videoIndex > -1)
            {
                pCodecCtxVideo = pFormatCtx->streams[videoIndex]->codec;
                pCodecVideo    = ffmpeg.avcodec_find_decoder(pCodecCtxVideo->codec_id);

                if (pCodecVideo == null)
                {
                    return(-1);
                }

                pCodecCtxVideo->codec_id = pCodecVideo->id;
                pCodecCtxVideo->lowres   = 0;

                if (pCodecCtxVideo->lowres > pCodecVideo->max_lowres)
                {
                    pCodecCtxVideo->lowres = pCodecVideo->max_lowres;
                }

                pCodecCtxVideo->idct_algo         = ffmpeg.FF_IDCT_AUTO;
                pCodecCtxVideo->error_concealment = 3;

                if (ffmpeg.avcodec_open2(pCodecCtxVideo, pCodecVideo, null) < 0)
                {
                    return(-1);
                }
                Console.WriteLine("Find a video stream. channel = " + videoIndex);

                frameSize = new System.Windows.Size(pCodecCtxVideo->width, pCodecCtxVideo->height);
                double frameRate = ffmpeg.av_q2d(pFormatCtx->streams[videoIndex]->r_frame_rate);
                frameGap = (int)(1000 / frameRate);

                double frameWidth  = frameSize.Width;
                double frameHeight = frameSize.Height;

                //fsrcnn = FSRCNN_construct();
                //FSRCNN_init(fsrcnn, (int)frameHeight, (int)frameWidth);

                image.Dispatcher.BeginInvoke((Action)(() =>
                {
                    slider2.Visibility = System.Windows.Visibility.Visible;
                    if (frameWidth >= 1000)
                    {
                        ((MainWindow)System.Windows.Application.Current.MainWindow).WindowState = WindowState.Maximized;
                        double tempRate = ((MainWindow)System.Windows.Application.Current.MainWindow).Width / frameWidth;
                        frameWidth = ((MainWindow)System.Windows.Application.Current.MainWindow).Width;
                        frameHeight *= tempRate;
                    }
                    ((MainWindow)System.Windows.Application.Current.MainWindow).Width = frameWidth;
                    ((MainWindow)System.Windows.Application.Current.MainWindow).Height = frameHeight + 140;

                    image.Width = frameWidth;
                    image.Height = frameHeight;
                    Dimage.Width = frameWidth;
                    Dimage.Height = frameHeight;
                    grid3.Width = frameWidth;
                    grid.Width = frameWidth;
                    grid2.Width = frameWidth;
                    slider.Width = frameWidth - 140;
                    slider2.Width = frameWidth - 140;


                    if (initCheck == false)
                    {
                        originalHeight = ((MainWindow)System.Windows.Application.Current.MainWindow).Height;
                        originalWidth = ((MainWindow)System.Windows.Application.Current.MainWindow).Width;
                        ((MainWindow)System.Windows.Application.Current.MainWindow).Width = 800;
                        ((MainWindow)System.Windows.Application.Current.MainWindow).Height = 800 * (originalHeight / originalWidth);
                    }

                    ((MainWindow)System.Windows.Application.Current.MainWindow).SizeChanged += new SizeChangedEventHandler(Window_SizeChanged);
                }));

                pFrameVideo = ffmpeg.av_frame_alloc();
                swsCtxVideo = new SwsContext *[4];
                for (int i = 0; i < scalerNum; i++)
                {
                    swsCtxVideo[i] = ffmpeg.sws_getContext(
                        (int)frameSize.Width,
                        (int)frameSize.Height,
                        pCodecCtxVideo->pix_fmt,
                        (int)frameSize.Width,
                        (int)frameSize.Height,
                        AVPixelFormat.AV_PIX_FMT_BGR24,
                        ffmpeg.SWS_FAST_BILINEAR, null, null, null);
                }
            }
            if (audioIndex > -1)
            {
                pCodecAudio   = ffmpeg.avcodec_find_decoder(pFormatCtx->streams[audioIndex]->codecpar->codec_id);
                pCodeCtxAudio = ffmpeg.avcodec_alloc_context3(pCodecAudio);
                ffmpeg.avcodec_parameters_to_context(pCodeCtxAudio, pFormatCtx->streams[audioIndex]->codecpar);

                if (pCodecAudio == null)
                {
                    return(-1);
                }
                if (ffmpeg.avcodec_open2(pCodeCtxAudio, pCodecAudio, null) < 0)
                {
                    return(-1);
                }
                Console.WriteLine("Find a audio stream. channel = " + audioIndex);

                sdlAudio = new SDLAudio();
                sdlAudio.SDL_Init(pCodeCtxAudio);

                pFrameAudio = ffmpeg.av_frame_alloc();
                swrCtxAudio = ffmpeg.swr_alloc();

                ffmpeg.av_opt_set_channel_layout(swrCtxAudio, "in_channel_layout", (long)pCodeCtxAudio->channel_layout, 0);
                ffmpeg.av_opt_set_channel_layout(swrCtxAudio, "out_channel_layout", (long)pCodeCtxAudio->channel_layout, 0);
                ffmpeg.av_opt_set_int(swrCtxAudio, "in_sample_rate", pCodeCtxAudio->sample_rate, 0);
                ffmpeg.av_opt_set_int(swrCtxAudio, "out_sample_rate", pCodeCtxAudio->sample_rate, 0);
                ffmpeg.av_opt_set_sample_fmt(swrCtxAudio, "in_sample_fmt", pCodeCtxAudio->sample_fmt, 0);
                ffmpeg.av_opt_set_sample_fmt(swrCtxAudio, "out_sample_fmt", AVSampleFormat.AV_SAMPLE_FMT_FLT, 0);
                ffmpeg.swr_init(swrCtxAudio);
            }

            this.image     = image;
            this.Dimage    = Dimage;
            entirePlayTime = pFormatCtx->duration;
            this.startTime = startTime;
            this.slider    = slider;


            scalerId = 0;
            rasterId = 0;

            videoPool = new PacketPool();
            audioPool = new PacketPool();
            fPool     = new FrameBuffer(frameSize);

            isEOF = false;
            state = State.Init;

            decodeSeek = false;
            srSeek     = false;
            drawSeek   = false;
            scaleSeek  = new bool[scalerNum];
            for (int i = 0; i < scalerNum; i++)
            {
                scaleSeek[i] = false;
            }
            rasterSeek = new bool[rasterNum];
            for (int i = 0; i < rasterNum; i++)
            {
                rasterSeek[i] = false;
            }

            gmfn = new GMFN();

            return(0);
        }
Example #9
0
 void SetupPacketizer()
 {
     fSenderPacketPool = new PacketPool<CommPacket>(10);
     fPacketizer = new CommFramePacketizer(fSenderPacketPool);
 }
Example #10
0
 public TcpClient()
 {
     packetPool    = new PacketPool();
     messagePacker = new MessagePacker();
     isConnected   = 0;
 }