Пример #1
0
 public void loadBitmapStream()
 {
     //“后台”线程,如果bitmap_stream的长度小于2,就读新的BitmapStream
     //两个BitmapStream的时候这个function就卡住了
     while (true)
     {
         //load 4 buffers
         while (bitmap_stream.Count >= buffer_num)
         {
             Thread.Sleep(200);
             if (reader.finish == 1)
             {
                 return;
             }
         }
         Console.Out.WriteLine("loading buffer...");
         BitmapStream stream_tmp = new BitmapStream();
         //reader的loadBitmapStream是读取bitmapstream
         //本地不用考虑异步、大家read的不对齐的情况,参数直接设-1
         stream_tmp = reader.loadBitmapStream_count(-1);
         bitmap_stream.Add(stream_tmp);
         countBitmap++;
     }
 }
Пример #2
0
        public void listen_video_header()
        {
            while (true)
            {
                Console.Out.WriteLine("listen_video_header is running...");
                Package pack = conn.recv();
                if (pack == null)
                {
                    continue;
                }
                if (pack.type == "ask_video")
                {
                    /*
                     * header for request:
                     * [0] Name
                     */
                    List <String> header   = new List <String>(pack.header);
                    String[]      tmp      = header[0].Split('\\');
                    String        tmp_addr = Utils.search_addr_from_list(tmp[tmp.Length - 1], Client.media);
                    if (tmp_addr != "" && File.Exists(tmp_addr))
                    {
                        //该文件存在,载入到本线程的reader里面
                        //接受的时候不需要reader,只有传的时候需要,所以一个reader就够了

                        /*
                         * type of resp: has_video
                         *
                         * reader the info from the video file and response a header
                         *
                         * header of resp:
                         * [0] FrameRate(给player设置interval)
                         * [1] Height
                         * [2] Width
                         *
                         * 那个wmv是历史遗留问题反正avi也是这个参数。。。
                         *
                         */

                        header = reader.loadFile(tmp_addr, "wmv");


                        for (int i = 0; i < header.Count; i++)
                        {
                            Console.Out.WriteLine("{0}: {1}", i, header[i]);
                        }
                        Package pack_resp = new Package("has_video");
                        pack_resp.header = new List <string>(header);
                        Connector conn_resp = Client.find_conn(Client.conn_video_data, pack.from);
                        conn_resp.send(pack_resp);
                    }
                    else
                    {
                        Package   pack_resp = new Package("no_video");
                        Connector conn_resp = Client.find_conn(Client.conn_video_data, pack.from);
                        conn_resp.send(pack_resp);
                    }
                }
                else if (pack.type == "request_video" && num_frac == 0)
                {
                    /*
                     * header for request:
                     * [0] 第几个bitmapStream
                     */

                    List <String> header = new List <String>(pack.header);

                    if (reader.finish == 1)
                    {
                        //空了,给一个end_video
                        Package          pack_resp = new Package("end_video");
                        List <Connector> conn_resp = Client.find_conns(Client.conn_video_data, pack.from);

                        conn_resp[0].send(pack_resp);
                    }
                    else
                    {
                        BitmapStream bitmapStream = reader.loadBitmapStream_count(Int32.Parse(header[0]));

                        CompressedBitmapStream comp_bitmapStream = new CompressedBitmapStream(bitmapStream);

                        //把object serialize成memoryStream,再到byte[]
                        MemoryStream    stream    = new MemoryStream();
                        BinaryFormatter formatter = new BinaryFormatter();
                        formatter.Serialize(stream, comp_bitmapStream);

                        byte[] data = stream.ToArray();

                        //byte[] m = Compress.compress(data);

                        List <byte[]> data_list = new List <byte[]>();

                        int offset = (int)data.Length / Client.NUM_FRACTION;

                        for (int i = 0; i < Client.NUM_FRACTION; i++)
                        {
                            byte[] tmp_data;
                            if (i < Client.NUM_FRACTION - 1)
                            {
                                tmp_data = new byte[offset];
                            }
                            else
                            {
                                tmp_data = new byte[data.Length - (Client.NUM_FRACTION - 1) * offset];
                            }
                            System.Buffer.BlockCopy(data, i * offset, tmp_data, 0, tmp_data.Length);
                            data_list.Add(tmp_data);
                        }

                        //发回去

                        List <Connector> conn_resp = Client.find_conns(Client.conn_video_data, pack.from);

                        if (conn_resp.Count == 0)
                        {
                            return;
                        }

                        for (int i = 0; i < Client.NUM_FRACTION; i++)
                        {
                            Package pack_resp = new Package("video_resp");
                            pack_resp.data = data_list[i];
                            if (conn_resp[i].ip_send == "")
                            {
                                return;
                            }
                            send_thread a = new send_thread(conn_resp[i], pack_resp);
                            a.start();
                        }
                    }
                }
            }
        }