예제 #1
0
        void thread_audio_send()
        {
            int i;
            //int read_num;
            //T_U32 destip;
            // T_U8 temp[20];
            int data_len;

            //T_U16 *p_liner;
            //T_U8 *pbuf;
            //AudioDataHead *head;
            //int send_size=0;
            //int a=0,b=0;
            byte[] ip   = new byte[4];
            byte[] addr = new byte[6];

            Console.WriteLine("thread_audio_send...");
            //head = (AudioDataHead *)audio_manage.pack_buff;
            AudioPack  head;
            FileStream playFile = null;

            while (true)
            {
                lock (audio_manage.send_sem)
                {
                    Monitor.Wait(audio_manage.send_sem);
                }
                Console.WriteLine("thread_audio_send...");
                try
                {
                    if (playPath != null && playPath != "")
                    {
                        playFile = new FileStream(playPath, FileMode.Open);
                    }
                    else
                    {
                        playFile = null;
                    }
                }
                catch
                {
                    playFile = null;
                }
                if (playFile == null)
                {
                    continue;
                }
                Console.WriteLine("thread_audio_send file ok...");
                while (true)
                {
                    lock (audio_manage.send_lock)
                    {
                        if (audio_manage.send_flag == false)
                        {
                            //AK_Release_Semaphore(audio_manage.send_lock);
                            break;
                        }
                        //if(audio_manage.send_len >= 128)
                        {
                            //打包
                            //data_len = (audio_manage.send_len - (audio_manage.send_len % 128))/2;
                            data_len = (int)(playFile.Length - playFile.Position);
                            if (data_len > 256)
                            {
                                data_len = 256;
                            }


                            head = new AudioPack();
                            //memcpy(head->PublicHead,audio_manage.public_head, 10);  //公共包头
                            //memcpy(head->SourceAddr,audio_manage.source_addr, 6);//源地址
                            head.SourceAddr   = LocalCfg.Addr;
                            head.Type         = 0;
                            head.Fomat        = 0;
                            head.Frameno      = (UInt16)audio_manage.send_frame_no;
                            head.Framelen     = (UInt32)data_len;                               //帧数据长度
                            head.TotalPackage = 1;                                              //总包数
                            head.CurrPackage  = 1;                                              //当前包
                            head.Datalen      = (UInt16)data_len;                               //数据长度
                            head.PackLen      = (UInt16)data_len;
                            head.audioData    = new byte[data_len];
                            playFile.Read(head.audioData, 0, data_len);
                            if (data_len == 0)
                            {
                                playFile.Position = 0;
                            }
                            //转成alaw

                            /*
                             *          p_liner = (T_U16 *)(audio_manage.send_buff+audio_manage.send_rpos);
                             *          for(i=0; i<data_len; i++)
                             *          {
                             *              audio_manage.pack_buff[sizeof(AudioDataHead)+i] = linear2alaw(p_liner[i]);
                             *          }*/

                            audio_manage.send_rpos += data_len * 2;
                            audio_manage.send_len  -= data_len * 2;
                            audio_manage.send_frame_no++;
                            audio_manage.send_frame_total++;

                            /*send_size += data_len;            //统计发送速率
                             *      if(send_size >= 40000)
                             *      {
                             *              a = get_tick_count();
                             *              audio_manage.send_bps = (send_size*1000)/(a-b);
                             *              send_size = 0;
                             *              b = a;
                             *      }*/
                            //printf("audio frame%d: len = %ld\r\n",audio_manage.send_frame_no,head->Datalen = data_len);
                            //AK_Release_Semaphore(audio_manage.send_lock);
                            //发送
                            for (i = 0; i < Remoter.REMOTEMAX; i++)
                            {
                                if (Remoter.Remoter_get_audio_flag(i) == 1)
                                {
                                    Remoter.Remoter_get_ip(i, ip);
                                    Remoter.Remoter_get_addr(i, addr);
                                    head.DestAddr = addr;               //目的地址
                                    audio_manage.udp_trans.Send(head.GetByte(), head.GetLength(), new IPEndPoint(new IPAddress(ip), audio_manage.udp_port));
                                    // Console.WriteLine("thread_audio_send...");
                                }
                            }
                            Thread.Sleep(30);
                        }

                        /*
                         *       else
                         *       {
                         *          //AK_Release_Semaphore(audio_manage.send_lock);
                         *          Thread.Sleep(5);
                         *       }	*/
                    }
                }
                if (playFile != null)
                {
                    playFile.Close();
                }
            }
        }
예제 #2
0
        void thread_audio_recv()
        {
            int free_space;
            int recv_size = 0;

            byte[]    recv_buff;
            AudioPack head;
            //Console.WriteLine("thread_audio_recv...\r\n");
            IPEndPoint recvEP = new IPEndPoint(IPAddress.Broadcast, audio_manage.udp_port);

            while (true)
            {
                lock (audio_manage.recv_sem)
                {
                    Monitor.Wait(audio_manage.recv_sem);
                }
                //Console.WriteLine("thread_audio_recv run...\r\n");
                while (true)
                {
                    recv_buff = audio_manage.udp_trans.Receive(ref recvEP);
                    //Console.WriteLine("thread_audio_recv");
                    if (audio_manage.audio_recv_filter != null)
                    {
                        if (audio_manage.audio_recv_filter(recvEP.Address.GetAddressBytes()) == false)
                        {
                            continue;
                        }
                    }
                    head = new AudioPack(recv_buff);
                    lock (audio_manage.recv_lock)
                    {
                        if (audio_manage.recv_flag == false)
                        {
                            break;
                        }
                        free_space = AUDIO_PLAY_BUFF_SIZE - audio_manage.audio_chanel[0].w_pos;

                        recv_size += head.Datalen;              //统计音频数据接收速率

                        /* if(recv_size>=40000)
                         * {
                         *       a = get_tick_count();
                         *       audio_manage.recv_bps = (recv_size*1000)/(a-b);
                         *       recv_size = 0;
                         *       b = a;
                         * }*/

                        //Console.WriteLine("data len" + head.Datalen);
                        if (head.Datalen > free_space)
                        {
                            Array.Copy(head.audioData, 0, audio_manage.audio_chanel[0].play_buff, audio_manage.audio_chanel[0].w_pos, free_space);
                            Array.Copy(head.audioData, free_space, audio_manage.audio_chanel[0].play_buff, 0, head.Datalen - free_space);

                            audio_manage.audio_chanel[0].w_pos = head.Datalen - free_space;
                            if (audio_manage.audio_chanel[0].w_pos >= audio_manage.audio_chanel[0].r_pos)                 //缓冲区溢出
                            {
                                Console.WriteLine("audio recv thread: buff overflow! " + audio_manage.audio_chanel[0].w_pos + audio_manage.audio_chanel[0].r_pos);
                            }
                        }
                        else
                        {
                            Array.Copy(head.audioData, 0, audio_manage.audio_chanel[0].play_buff, audio_manage.audio_chanel[0].w_pos, head.Datalen);

                            audio_manage.audio_chanel[0].w_pos += head.Datalen;
                            if (audio_manage.audio_chanel[0].w_pos >= AUDIO_PLAY_BUFF_SIZE)
                            {
                                audio_manage.audio_chanel[0].w_pos = 0;
                            }
                        }
                        if (audio_manage.audio_chanel[0].w_pos >= audio_manage.audio_chanel[0].r_pos)       //计算缓冲区中数据的长度
                        {
                            audio_manage.audio_chanel[0].len = audio_manage.audio_chanel[0].w_pos - audio_manage.audio_chanel[0].r_pos;
                        }
                        else
                        {
                            audio_manage.audio_chanel[0].len = AUDIO_PLAY_BUFF_SIZE + audio_manage.audio_chanel[0].w_pos - audio_manage.audio_chanel[0].r_pos;
                        }
                        //printf("Audio net recv:w_pos=%ld,r_pos=%ld,len=%ld\r\n",audio_manage.audio_chanel[0].w_pos,audio_manage.audio_chanel[0].r_pos,audio_manage.audio_chanel[0].len);
                    }
                }
            }
        }