/// <summary> /// 音视频消息体解析 /// </summary> /// <param name="msgBody"></param> /// <returns></returns> public Video Decode(byte[] msgBody) { item = new Video(); try { int indexOffset = 0; item.state = msgBody.ToUInt32(indexOffset += 4); item.V_P_X_CC = msgBody[indexOffset]; item.M_PT = msgBody[indexOffset += 1]; item.num = msgBody.ToUInt16(indexOffset += 1); item.SIM = msgBody.Copy(indexOffset + 2, 6); item.ID = msgBody[indexOffset += 8]; item.type = msgBody[indexOffset += 1]; item.Time = msgBody.Copy(indexOffset + 2, 8); if (BitConvert.ByteToBit(item.type).Substring(0, 4) == "0011") { //音频 item.length = msgBody.ToUInt16(indexOffset += 9); item.data = msgBody.Copy(indexOffset + 2, item.length); } else { //视频 item.Last_I_F = msgBody.ToUInt16(indexOffset += 9); item.Last_F = msgBody.ToUInt16(indexOffset += 2); item.length = msgBody.ToUInt16(indexOffset += 2); item.data = msgBody.Copy(indexOffset + 2, item.length); } return(item); } catch { return(item); } }
/// <summary> /// 处理原始RTP包,分离H264码流 /// </summary> public void RData() { int over = -1; bool flag = true; byte[] temp = null; while (IsEnd) { if (Tools.video.Count > 0) { if (flag == true) { Tools.video.TryDequeue(out temp); flag = false; } try { for (int index = 0; index < temp.Length - 4; index++) { if (temp[index] == 0x30 && temp[index + 1] == 0x31 && temp[index + 2] == 0x63 && temp[index + 3] == 0x64) { over = index; if (over > 0) { ///取出RTP单包 byte[] temps = temp.Take(index).ToArray(); ///解析RTP Videobody = VideoInits.Decode(temps); //视频放入H264队列 Tools.VideoQueues.Enqueue(new ValueTuple <byte[], string, string>(Videobody.data, BitConvert.ByteToBit(Videobody.M_PT).Substring(1, 7), BitConvert.ByteToBit(Videobody.type).Substring(4, 4))); //取出剩余数据包 temp = temp.Skip(index).ToArray(); break; } } } if (over < 1) { ///取出下一个RTP包 if (Tools.video.Count > 0) { byte[] temp1 = temp; Tools.video.TryDequeue(out byte[] temp2); ///上一包的剩余数据与下一包拼接进入下一循环 temp = new byte[temp2.Length + temp1.Length]; temp1.CopyTo(temp, 0); temp2.CopyTo(temp, temp1.Length); } else { while (IsEnd) { if (Tools.video.Count <= 0) { Thread.Sleep(10); } else { byte[] temp1 = temp; Tools.video.TryDequeue(out byte[] temp2); ///上一包的剩余数据与下一包拼接进入下一循环 temp = new byte[temp2.Length + temp1.Length]; temp1.CopyTo(temp, 0); temp2.CopyTo(temp, temp1.Length); break; } } } } over = -1; } catch (Exception e) { MessageBox.Show(e.ToString()); } } else { Thread.Sleep(10); } } }