Пример #1
0
        private void FileEmergencyPlayThread(object obj)
        {
            UdpClient  udpserver = new UdpClient(9999);
            IPEndPoint ep        = new IPEndPoint(IPAddress.Any, 0);

            WorkProcessHandler h = new WorkProcessHandler(FileEmergencyPlayProcess);

            ThreadParam ap = (ThreadParam)obj;

            DeviceListener.Device d = deviceListener.Find(ap.id);
            if (d == null)
            {
                return;
            }
            int ret = deviceListener.FileEmergencyPlayStart(d, "0.0.0.0", 9999, ap.filename, ap.streamtype, ap.volume, d.id.ToString());

            if (ret != 0)
            {
                return;
            }

            FileStream fs = File.OpenRead(ap.filename);

            if (fs == null)
            {
                return;
            }

            try
            {
                long   starttime = DateTime.Now.Ticks;
                string d_ip      = null;
                int    d_port    = 0;
                while ((DateTime.Now.Ticks - starttime) < 3000 * 10000)//3000ms
                {
                    if (udpserver.Available > 0)
                    {
                        byte[] bs = udpserver.Receive(ref ep);
                        string s  = System.Text.Encoding.Default.GetString(bs);
                        if (s.Contains(d.id.ToString()))
                        {
                            d_ip   = ep.Address.ToString();
                            d_port = ep.Port;
                            break;
                        }
                    }
                }

                if (d_port != 0)
                {
                    int     playfiletime   = 0;
                    int     time           = System.Environment.TickCount;
                    int     starttime1     = time;
                    bool    eof            = false;
                    byte[]  fbs            = new byte[320];
                    short[] pcm            = new short[160];
                    int     sequenceNumber = 0;
                    long    timestamp      = 0;
                    fs.Read(fbs, 0, 44);
                    long writebytes = 44;
                    while (!eof)
                    {
                        int runtime = System.Environment.TickCount - starttime1;
                        if ((runtime + 500) >= playfiletime)
                        {
                            int readbytes = fs.Read(fbs, 0, fbs.Length);

                            IntPtr p = Marshal.UnsafeAddrOfPinnedArrayElement(fbs, 0);
                            Marshal.Copy(p, pcm, 0, 160);
                            //GroovyCodecs.G711.uLaw.ULawEncoder g711encode = new GroovyCodecs.G711.uLaw.ULawEncoder();
                            //byte[] ba = g711encode.Process(pcm);
                            byte[] ba = g711.g711Encode_ulwa(pcm);

                            RtpPacket packet = new RtpPacket(RtpPayloadType.G711_uLaw, sequenceNumber, timestamp, ba, ba.Length);
                            sequenceNumber++;
                            timestamp    += 20;// ba.Length;
                            playfiletime += 20;
                            udpserver.Send(packet.ToArray(), packet.Length, d_ip, d_port);

                            writebytes += readbytes;
                            if (readbytes < fbs.Length)
                            {
                                eof = true;
                            }
                            int process = (int)(100 * writebytes / fs.Length);
                            this.BeginInvoke(h, d, ap.filename, false, process);
                        }
                    }
                }
                udpserver.Close();
                Thread.Sleep(500);
                deviceListener.FileEmergencyPlayStop(d);
                this.BeginInvoke(h, d, ap.filename, true, 0);
            }
            catch (ThreadAbortException abortException)
            {
                udpserver.Close();
                dataThread = null;
            }
        }