public BitmapStream askBitmapStream(int streamCount) { video_stream_fractions = new List <byte[]>(NUM_FRACTION); video = new List <int>(NUM_FRACTION); video_writing = new List <int>(NUM_FRACTION); for (int i = 0; i < NUM_FRACTION; i++) { video_stream_fractions.Add(new byte[0]); video.Add(0); video_writing.Add(0); } video_stream = new CompressedBitmapStream(); //“有这个视频”名单空的,返回null if (video_client.Count == 0) { return(null); } //给所有名单里的人发请求 Connector conn = find_conn(conn_video_header, video_client[streamCount % video_client.Count]); Package pack = new Package("request_video"); pack.header.Add("" + streamCount); conn.send(pack); while (true) { //等待回复 //如果某个线程知道有回复了,就把video改成1,然后读data写到video_stream(buffer)里面 int tag = 1; for (int i = 0; i < NUM_FRACTION; i++) { if (video[i] == 0) { tag = 0; break; } } if (tag == 1) { MemoryStream stream = new MemoryStream(); BinaryFormatter formatter = new BinaryFormatter(); for (int i = 0; i < NUM_FRACTION; i++) { stream.Write(video_stream_fractions[i], 0, video_stream_fractions[i].Length); } stream.Position = 0; //解压缩一下。。。 /*byte[] data = stream.ToArray(); * stream = new MemoryStream(); * GZipStream decomp = new GZipStream(stream,CompressionMode.Decompress); * decomp.Read(data, 0, data.Length); */ video_stream = (CompressedBitmapStream)formatter.Deserialize(stream); return(video_stream.toBitmapStream()); } } }
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(); } } } } }