コード例 #1
0
        public Program()
        {
            WebSocketServer webSocketServer = new WebSocketServer(IPAddress.Any, 8081);
            TestCard        testCard        = new TestCard(WIDTH, HEIGHT, FPS);
            Timer           sendTimer       = new Timer((state) =>
            {
                if (!SendBuffer.IsEmpty && ConnectedPeer != null && ConnectedPeer.connectionState == RTCPeerConnectionState.connected)
                {
                    byte[] imageData;

                    if (!SendBuffer.TryDequeue(out imageData))
                    {
                        return;
                    }
                    SendFrame(90000 / FPS, imageData);
                }
            }, null, 0, 33);

            webSocketServer.AddWebSocketService <WebRTCWebSocketPeer>("/", (peer) => peer.CreatePeerConnection = CreatePeerConnection);
            webSocketServer.Start();
            Console.WriteLine("Started WebRTC Server.");
            Codec = new H264Codec(WIDTH, HEIGHT, FPS, OnImageEncoded);
            Console.WriteLine("Initialized Codec.");
            testCard.ReceivedYUVFrame += TestCard_ReceivedYUVFrame;
            Console.WriteLine("TestCard is now sending frames.");
            Console.ReadKey();
        }
コード例 #2
0
        private void ProcessSpsPps(byte[] payload, out byte[] spsPps)
        {
            var h264 = new H264Codec();

            h264.Version         = payload[0];
            h264.ProfileHigh     = payload[1];
            h264.Compatibility   = payload[2];
            h264.Level           = payload[3];
            h264.Reserved6AndNal = payload[4];
            h264.Reserved3AndSps = payload[5];
            h264.LengthOfSps     = (short)(((payload[6] & 255) << 8) + (payload[7] & 255));

            var sequence = new byte[h264.LengthOfSps];

            Array.Copy(payload, 8, sequence, 0, h264.LengthOfSps);
            h264.SequenceParameterSet = sequence;
            h264.NumberOfPps          = payload[h264.LengthOfSps + 8];
            h264.LengthOfPps          = (short)(((payload[h264.LengthOfSps + 9] & 2040) + payload[h264.LengthOfSps + 10]) & 255);

            var picture = new byte[h264.LengthOfPps];

            Array.Copy(payload, h264.LengthOfSps + 11, picture, 0, h264.LengthOfPps);
            h264.PictureParameterSet = picture;

            if (h264.LengthOfSps + h264.LengthOfPps < 102400)
            {
                var spsPpsLen = h264.LengthOfSps + h264.LengthOfPps + 8;
                spsPps = new byte[spsPpsLen];

                spsPps[0] = 0;
                spsPps[1] = 0;
                spsPps[2] = 0;
                spsPps[3] = 1;

                Array.Copy(h264.SequenceParameterSet, 0, spsPps, 4, h264.LengthOfSps);

                spsPps[h264.LengthOfSps + 4] = 0;
                spsPps[h264.LengthOfSps + 5] = 0;
                spsPps[h264.LengthOfSps + 6] = 0;
                spsPps[h264.LengthOfSps + 7] = 1;

                Array.Copy(h264.PictureParameterSet, 0, spsPps, h264.LengthOfSps + 8, h264.LengthOfPps);
            }
            else
            {
                spsPps = null;
            }
        }