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(); } } } } }
public void setLocalInfo(String addr, String type) { address = addr; current_type = type; finish = 0; if (Local.exist(addr)) { isLocal = true; } else { isLocal = false; } header = reader.loadFile(address, current_type); if (header != null) { Console.Out.WriteLine("local video"); loadStream = new Thread(loadBitmapStream); loadStream.Start(); isLocal = true; //本地文件 //调整窗口大小 form_container.Height = Int32.Parse(header[1]); form_container.Width = Int32.Parse(header[2]); window.Height = 710 > Int32.Parse(header[1]) + 300 ? 710 : Int32.Parse(header[1]) + 300; window.Width = 820 > Int32.Parse(header[2]) + 300 ? 820 : Int32.Parse(header[2]) + 300; // window.Height = form_container.Height + 50; // window.Width = form_container.Width + 50; //给第一个留时间缓冲,等待缓存 start = 0; while (bitmap_stream.Count < 2) { Thread.Sleep(300); } start = 1; } else { Console.Out.WriteLine("not local video"); isLocal = false; header = client.askVideoHeader(address); if (header != null && header.Count > 0) { //设置同步 //reader.bmpPerStream = Int32.Parse(header[0]); loadStream = new Thread(loadBitmapStreamP2P); loadStream.Start(); //调整窗口大小 form_container.Height = Int32.Parse(header[1]); form_container.Width = Int32.Parse(header[2]); window.Height = 710 > Int32.Parse(header[1]) + 300 ? 710 : Int32.Parse(header[1]) + 300; window.Width = 820 > Int32.Parse(header[2]) + 300 ? 820 : Int32.Parse(header[2]) + 300; //给第一个留时间缓冲 start = 0; while (bitmap_stream.Count < 2) { Thread.Sleep(200); } start = 1; } } }