Ejemplo n.º 1
0
        //private FileStream fs = new FileStream(@"D:\IvsVideoStreambefore.264", FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
        //private int writeCount = 0;

        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="videoData"></param>
        /// <param name="sender"></param>
        public void SendData(MediaData mediaData, string sender)
        {
            if (mediaData.StreamType == StreamType.VIDEO_H264)
            {
                //if (writeCount < 1000)
                //{
                //    fs.Write(mediaData.Data, 0, (int)mediaData.Size);
                //    writeCount++;
                //}

                SendVideoData(mediaData, sender);
            }
            if (mediaData.StreamType == StreamType.AUDIO_G711A || mediaData.StreamType == StreamType.AUDIO_G711U)
            {
                SendAudioData(mediaData, sender);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 开始读取 码流数据
        /// </summary>
        /// <param name="str"></param>
        public void ReadData(Common.DataCallBack dataCallBack)
        {
            NLogEx.LoggerEx logEx = new NLogEx.LoggerEx(log);

            Thread th = new Thread(new ThreadStart(() =>
            {
                while (!shouldStop)
                {
                    try
                    {
                        if (pipedStream != null && pipedStream.IsConnected)
                        {
                            byte[] byteArray     = new byte[4]; //unit字节长度
                            pipedStream.ReadMode = PipeTransmissionMode.Byte;

                            MediaData mediaData = new MediaData();

                            pipedStream.Read(byteArray, 0, 4);
                            mediaData.Size = (uint)BitConverter.ToInt32(byteArray, 0);
                            if (mediaData.Size == 0)
                            {
                                continue;
                            }
                            pipedStream.Read(byteArray, 0, 4);
                            mediaData.StreamType = (StreamType)BitConverter.ToInt32(byteArray, 0);
                            pipedStream.Read(byteArray, 0, 4);
                            mediaData.DataType = (MediaDataType)BitConverter.ToInt32(byteArray, 0);
                            pipedStream.Read(byteArray, 0, 4);
                            mediaData.FrameType = (FrameDataType)BitConverter.ToInt32(byteArray, 0);

                            byte[] dataByteArray = new byte[mediaData.Size];
                            //读取管道中的流
                            pipedStream.Read(dataByteArray, 0, (int)mediaData.Size);
                            mediaData.Data = dataByteArray;

                            mediaDataSize = mediaData.Size;

                            //if (wCount < 1200)
                            //{
                            //    fs.Write(mediaData.Data, 0, (int)mediaData.Size);
                            //    wCount++;
                            //}

                            //回调函数
                            dataCallBack(cameraNo, mediaData, cameraNo.Substring(0, cameraNo.IndexOf("#")));
                        }
                    }
                    catch (Exception ex)
                    {
                        logEx.Error("VideoPipe.ReadData is error,ex={0}", ex.ToString());
                        if (pipedStream != null && !pipedStream.IsConnected)
                        {
                            VideoPipeManage.Instance().DeleteVideoPipe(cameraNo);
                            Stop();
                        }
                    }
                }
            }));

            th.IsBackground = true;
            th.Start();
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 发送视频码流,
        /// 对于H264的帧数据,如果监控平台回调的码流,SPS PPS IDR是分开发送的,则需要进行拼帧处理,将SPS+PPS+IDR拼接在一起发送
        /// </summary>
        /// <param name="mediaData"></param>
        /// <param name="sender"></param>
        public void SendVideoData(MediaData mediaData, string sender)
        {
            //对于H264的帧数据,如果监控平台回调的码流,SPS PPS IDR是分开发送的,则需要进行拼帧处理,将SPS+PPS+IDR拼接在一起发送
            if (mediaData.DataType == MediaDataType.FRAME_DATA)
            {
                switch (mediaData.FrameType)
                {
                case FrameDataType.H264_SEI_NALU_TYPE:
                    //SEI的包,直接丢弃
                    return;

                case FrameDataType.H264_SPS_NALU_TYPE:
                    //SPS的类型,先清空缓存
                    //this.CacheData = new byte[mediaData.Size];
                    //将SPS的数据缓存
                    this.CacheData          = mediaData.Data;
                    this.LastCacheFrameType = mediaData.FrameType;

                    //直接return,暂不发送
                    return;

                case FrameDataType.H264_PPS_NALU_TYPE:
                    //如果缓存最后一帧数据不为SPS或者PPS,说明数据乱序,舍弃。
                    if ((this.LastCacheFrameType != FrameDataType.H264_SPS_NALU_TYPE) && (this.LastCacheFrameType != FrameDataType.H264_PPS_NALU_TYPE))
                    {
                        return;
                    }

                    //将PPS的数据缓存,暂不发送
                    byte[] byteTemp = new byte[this.CacheData.Length + mediaData.Data.Length];
                    this.CacheData.CopyTo(byteTemp, 0);
                    mediaData.Data.CopyTo(byteTemp, this.CacheData.Length);
                    this.CacheData = byteTemp;

                    this.LastCacheFrameType = mediaData.FrameType;

                    //直接return,暂不发送
                    return;

                case FrameDataType.H264_IDR_NALU_TYPE:
                    //如果缓存是上最后一帧数据不为PPS,说明数据乱序,舍弃
                    if (this.LastCacheFrameType != FrameDataType.H264_PPS_NALU_TYPE)
                    {
                        return;
                    }

                    byteTemp = new byte[this.CacheData.Length + mediaData.Data.Length];
                    this.CacheData.CopyTo(byteTemp, 0);
                    mediaData.Data.CopyTo(byteTemp, this.CacheData.Length);
                    mediaData.Data = byteTemp;
                    mediaData.Size = (uint)mediaData.Data.Length;
                    //break跳出 switch,进行发送
                    break;

                default:
                    //其他类型的数据,直接发送
                    break;
                }
            }

            this.DataCallBack(this.CameraNo, mediaData, sender);
        }
Ejemplo n.º 4
0
 /// <summary>
 /// 发送音频码流
 /// </summary>
 /// <param name="videoData"></param>
 /// <param name="sender"></param>
 public void SendAudioData(MediaData videoData, string sender)
 {
     this.DataCallBack(this.CameraNo, videoData, sender);
 }