Beispiel #1
0
        private void RtpReceiver_RtpPacketReceived(RtpPacket packet)
        {
            if (h264Session.SSRC != packet.SSRC)
            {
                logger.Warn("Invalid SSRC " + h264Session.SSRC + " != " + packet.SSRC);
                return;
            }

            var currentTime = MediaTimer.GetRelativeTime();

            byte[] rtpPayload = packet.Payload.ToArray();

            //totalPayloadReceivedSize += rtpPayload.Length;
            // logger.Debug("totalPayloadReceivedSize " + totalPayloadReceivedSize);


            var frame = h264Session.Depacketize(packet);

            if (frame != null)
            {
                sw.Restart();

                var frameData = frame.Data;
                var frameTime = frame.Time;
                if (frameData != null)
                {
                    Decode(frameData, frameTime);

                    receiverStats.Update(currentTime, frameData.Length, sw.ElapsedMilliseconds);
                }
            }
        }
Beispiel #2
0
        // FileStream file = new FileStream(@"d:\test_enc3.h264", FileMode.Create);
        //private void MfEncoder_DataReady(byte[] buf)
        private void VideoEncoder_DataEncoded(byte[] buf, double _time)
        {
            //throw new NotImplementedException();
            var time = MediaTimer.GetRelativeTime();

            // var memo = new MemoryStream(buf);
            // memo.CopyTo(file);

            RtpSender.Push(buf, time);

            // rtpStreamer.Send(buf, time);
            var processingTime = sw.ElapsedMilliseconds;

            //logger.Debug(processingTime);


            //var ts = hwContext.sw.ElapsedMilliseconds;
            //Console.WriteLine("ElapsedMilliseconds " + ts);
            streamStats.Update(time, buf.Length, processingTime);
        }
Beispiel #3
0
        private void AudioSource_DataAvailable(byte[] data)
        {
            if (closing)
            {
                return;
            }

            if (data.Length > 0)
            {
                //bufferedWaveProvider.AddSamples(data, 0, data.Length);

                //var audioBuffer = new float[data.Length];

                //sampleChannel.Read(audioBuffer, 0, data.Length);

                byte[] dest = null;
                audioResampler.Resample2(data, out dest);
                if (dest != null && dest.Length > 0)
                {
                    //Debug.WriteLine("dest.Length " + dest.Length);

                    rtpTimestamp += (uint)(sw.ElapsedMilliseconds * 8.0);
                    sw.Restart();

                    double ralativeTime = MediaTimer.GetRelativeTime();
                    //uint rtpTime = (uint)(ralativeTime * 8000);

                    // streamer.Push(dest, ralativeTime);

                    RtpSender.Push(dest, ralativeTime);

                    //fs.Write(dest, 0, dest.Length);
                    //fs.Write(a.Buffer, 0, a.BytesRecorded);
                }
            }
        }
Beispiel #4
0
            internal Task StartClientTask(TcpClient tcpClient)
            {
                logger.Debug("StartClientTask(...)");

                return(Task.Run(() =>
                {
                    running = true;

                    var socket = tcpClient.Client;

                    var remoteAddr = socket.RemoteEndPoint;
                    logger.Info("Client connected: " + remoteAddr.ToString());

                    try
                    {
                        var stream = tcpClient.GetStream();

                        string request = "";
                        if (stream.CanRead && stream.DataAvailable)
                        {
                            byte[] buf = new byte[1024];
                            StringBuilder sb = new StringBuilder();
                            int bytesRead = 0;
                            do
                            {
                                bytesRead = stream.Read(buf, 0, buf.Length);
                                var str = Encoding.ASCII.GetString(buf, 0, bytesRead);
                                sb.Append(str);
                            }while (stream.DataAvailable);

                            request = sb.ToString();
                        }

                        //TODO: process request...
                        if (request != null)
                        {
                            logger.Debug("Client request:\r\n" + request);
                        }

                        if (streamer.clients.Count > 5)
                        {
                            string resp = "HTTP/1.0 503 Service Unavailable\r\n" + "Connectin: close\r\n";
                            byte[] bytes = Encoding.ASCII.GetBytes(resp);
                            stream.Write(bytes, 0, bytes.Length);

                            return;
                        }

                        // Send response...
                        string response = "HTTP/1.0 200 OK\r\n" +
                                          "Content-type: multipart/x-mixed-replace; boundary=" + BOUNDARY + "\r\n" +
                                          "Cache-Control: no-cache\r\n\r\n";

                        byte[] respBytes = Encoding.ASCII.GetBytes(response);

                        logger.Debug("Serv response: " + response);
                        stream.Write(respBytes, 0, respBytes.Length);

                        logger.Verb("Response loop BEGIN");
                        while (running)
                        {
                            if (!syncEvent.WaitOne(1000))
                            {
                                continue;
                            }

                            byte[] bytesToSend = streamer.TryToPullResponse();

                            if (!running)
                            {
                                break;
                            }

                            if (bytesToSend != null)
                            {
                                stream.Write(bytesToSend, 0, bytesToSend.Length);

                                this.streamer.statCounter.Update(MediaTimer.GetRelativeTime(), bytesToSend.Length);

                                // Statistic.RtpStats.Update(MediaTimer.GetRelativeTime(), bytesToSend.Length);

                                //logger.Debug("stream.Write(...) " + remoteAddr.ToString() + " "+ frameCount);
                            }

                            if (stream.CanRead && stream.DataAvailable)
                            { // Получаем от клиента запрос х.з что с ним делать...
                                string _request = "";
                                byte[] buf = new byte[1024];
                                StringBuilder sb = new StringBuilder();
                                int bytesRead = 0;
                                do
                                {
                                    bytesRead = stream.Read(buf, 0, buf.Length);
                                    var str = Encoding.ASCII.GetString(buf, 0, bytesRead);
                                    sb.Append(str);
                                }while (stream.DataAvailable);

                                _request = sb.ToString();

                                if (_request != null)
                                {
                                    logger.Debug("Client request:\r\n" + _request);
                                }
                            }
                        }// next frame...

                        logger.Verb("Response loop END");
                    }
                    catch (IOException ex)
                    {
                        var iex = ex.InnerException;
                        if (iex != null)
                        {
                            var socketException = iex as SocketException;
                            if (socketException != null)
                            {
                                var code = socketException.SocketErrorCode;
                                if (code == SocketError.ConnectionAborted || code == SocketError.ConnectionReset)
                                {
                                    logger.Warn(code + " " + remoteAddr.ToString());
                                    return;
                                }
                            }
                        }

                        throw;
                    }
                    finally
                    {
                        running = false;
                        tcpClient?.Close();

                        logger.Info("Client disconnected: " + remoteAddr.ToString());
                    }
                }));
            }