Exemplo n.º 1
0
 /// <summary>
 /// 编码首帧音频,即audioTag[0]
 /// <para>
 /// 注意:本方法已写入<see cref="previousTagSize"/>
 /// </para>
 /// </summary>
 /// <returns></returns>
 public byte[] EncoderFirstAudioTag(ulong timestamp)
 {
     byte[] buffer = FlvArrayPool.Rent(2048);
     try
     {
         FlvMessagePackWriter flvMessagePackWriter = new FlvMessagePackWriter(buffer);
         //flv body audio tag
         //flv body tag header
         FlvTags flvTags = new FlvTags
         {
             Type      = TagType.Audio,
             Timestamp = (uint)timestamp,
             //flv body tag body
             AudioTagsData = new AudioTags(AACPacketType.AudioSpecificConfig)
         };
         flvMessagePackWriter.WriteUInt32(previousTagSize);
         flvMessagePackWriter.WriteFlvTag(flvTags);
         var data = flvMessagePackWriter.FlushAndGetArray();
         previousTagSize = (uint)(flvTags.DataSize + 11);
         return(data);
     }
     finally
     {
         FlvArrayPool.Return(buffer);
     }
 }
Exemplo n.º 2
0
        private void CreateVideoTagOtherFrame(ref FlvMessagePackWriter flvMessagePackWriter, uint previousTagSize, H264NALU nALU)
        {
            //flv body PreviousTagSize
            flvMessagePackWriter.WriteUInt32(previousTagSize);
            //flv body video tag
            //flv body tag header
            FlvTags flvTags = new FlvTags();

            flvTags.Type         = TagType.Video;
            flvTags.Timestamp    = nALU.LastIFrameInterval;
            flvTags.TimestampExt = 0;
            flvTags.StreamId     = 0;
            //flv body tag body
            flvTags.VideoTagsData = new VideoTags();
            if (nALU.NALUHeader.NalUnitType == 5)
            {
                flvTags.VideoTagsData.FrameType = FrameType.KeyFrame;
            }
            else
            {
                flvTags.VideoTagsData.FrameType = FrameType.InterFrame;
            }
            flvTags.VideoTagsData.VideoData = new AvcVideoPacke();
            flvTags.VideoTagsData.VideoData.AvcPacketType   = AvcPacketType.Raw;
            flvTags.VideoTagsData.VideoData.CompositionTime = nALU.LastIFrameInterval;
            flvTags.VideoTagsData.VideoData.Data            = nALU.RawData;
            flvMessagePackWriter.WriteFlvTag(flvTags);
        }
Exemplo n.º 3
0
 /// <summary>
 /// 编码脚本Tag
 /// </summary>
 /// <param name="spsInfo">解析后的sps信息</param>
 /// <param name="hasAudio">是否有音频</param>
 /// <param name="frameRate">帧率 默认25d 即每秒25帧</param>
 /// <returns></returns>
 public byte[] EncoderScriptTag(SPSInfo spsInfo, bool hasAudio = false, double frameRate = 25d)
 {
     byte[] buffer = FlvArrayPool.Rent(1024);
     try
     {
         FlvMessagePackWriter flvMessagePackWriter = new FlvMessagePackWriter(buffer);
         //flv body script tag
         //flv body tag header
         FlvTags flvTags = new FlvTags
         {
             Type = TagType.ScriptData,
             //flv body tag body
             DataTagsData = new Amf3
             {
                 Amf3Metadatas = new List <IAmf3Metadata>
                 {
                     new Amf3Metadata_Duration {
                         Value = 0d
                     },
                     new Amf3Metadata_VideoDataRate {
                         Value = 0d
                     },
                     new Amf3Metadata_VideoCodecId {
                         Value = 7d
                     },
                     new Amf3Metadata_FrameRate {
                         Value = frameRate
                     },
                     new Amf3Metadata_Width()
                     {
                         Value = spsInfo.width
                     },
                     new Amf3Metadata_Height()
                     {
                         Value = spsInfo.height
                     },
                 }
             }
         };
         if (hasAudio)
         {
             flvTags.DataTagsData.Amf3Metadatas.Add(new Amf3Metadata_AudioCodecId());
             flvTags.DataTagsData.Amf3Metadatas.Add(new Amf3Metadata_AudioSampleRate());
             flvTags.DataTagsData.Amf3Metadatas.Add(new Amf3Metadata_AudioSampleSize());
             flvTags.DataTagsData.Amf3Metadatas.Add(new Amf3Metadata_AudioStereo());
         }
         flvMessagePackWriter.WriteFlvTag(flvTags);
         flvMessagePackWriter.WriteUInt32((uint)(flvTags.DataSize + 11));
         return(flvMessagePackWriter.FlushAndGetArray());
     }
     finally
     {
         FlvArrayPool.Return(buffer);
     }
 }
Exemplo n.º 4
0
 /// <summary>
 /// 编码首帧视频,即videoTag[0]
 /// <para>
 /// 注意:本方法已写入<see cref="previousTagSize"/>
 /// </para>
 /// </summary>
 /// <param name="sps"></param>
 /// <param name="pps"></param>
 /// <param name="sei"></param>
 /// <returns></returns>
 public byte[] EncoderFirstVideoTag(H264NALU sps, H264NALU pps, H264NALU sei)
 {
     byte[] buffer = FlvArrayPool.Rent(2048);
     try
     {
         FlvMessagePackWriter flvMessagePackWriter = new FlvMessagePackWriter(buffer);
         var             rawData          = h264Decoder.DiscardEmulationPreventionBytes(sps.RawData);
         ExpGolombReader h264GolombReader = new ExpGolombReader(rawData);
         SPSInfo         spsInfo          = h264GolombReader.ReadSPS();
         //flv body video tag
         //flv body tag header
         FlvTags flvTags = new FlvTags
         {
             Type         = TagType.Video,
             Timestamp    = (uint)sps.Timestamp,
             TimestampExt = 0,
             StreamId     = 0,
             //flv body tag body
             VideoTagsData = new VideoTags()
         };
         flvTags.VideoTagsData.FrameType = FrameType.KeyFrame;
         flvTags.VideoTagsData.VideoData = new AvcVideoPacke
         {
             AvcPacketType   = AvcPacketType.SequenceHeader,
             CompositionTime = 0
         };
         AVCDecoderConfigurationRecord aVCDecoderConfigurationRecord = new AVCDecoderConfigurationRecord
         {
             AVCProfileIndication      = spsInfo.profileIdc,
             ProfileCompatibility      = (byte)spsInfo.profileCompat,
             AVCLevelIndication        = spsInfo.levelIdc,
             NumOfPictureParameterSets = 1,
             PPSBuffer = pps.RawData,
             SPSBuffer = sps.RawData
         };
         flvTags.VideoTagsData.VideoData.AVCDecoderConfiguration = aVCDecoderConfigurationRecord;
         flvMessagePackWriter.WriteUInt32(previousTagSize);
         flvMessagePackWriter.WriteFlvTag(flvTags);
         var data = flvMessagePackWriter.FlushAndGetArray();
         previousTagSize = (uint)(flvTags.DataSize + 11);
         return(data);
     }
     finally
     {
         FlvArrayPool.Return(buffer);
     }
 }
Exemplo n.º 5
0
        private void CreateScriptTagFrame(ref FlvMessagePackWriter flvMessagePackWriter, int width, int height)
        {
            //flv body PreviousTagSize awalys 0
            flvMessagePackWriter.WriteUInt32(0);
            //flv body script tag
            //flv body tag header
            FlvTags flvTags = new FlvTags();

            flvTags.Type         = TagType.ScriptData;
            flvTags.Timestamp    = 0;
            flvTags.TimestampExt = 0;
            flvTags.StreamId     = 0;
            //flv body tag body
            flvTags.DataTagsData = new Amf3();
            flvTags.DataTagsData.Amf3Metadatas = new List <IAmf3Metadata>();
            flvTags.DataTagsData.Amf3Metadatas.Add(new Amf3Metadata_Duration
            {
                Value = 0d
            });
            flvTags.DataTagsData.Amf3Metadatas.Add(new Amf3Metadata_VideoDataRate
            {
                Value = 0d
            });
            flvTags.DataTagsData.Amf3Metadatas.Add(new Amf3Metadata_VideoCodecId
            {
                Value = 7d
            });
            flvTags.DataTagsData.Amf3Metadatas.Add(new Amf3Metadata_FrameRate
            {
                Value = 25d
            });
            flvTags.DataTagsData.Amf3Metadatas.Add(new Amf3Metadata_Width
            {
                Value = width
            });
            flvTags.DataTagsData.Amf3Metadatas.Add(new Amf3Metadata_Height
            {
                Value = height
            });
            flvMessagePackWriter.WriteFlvTag(flvTags);
        }
Exemplo n.º 6
0
        private void CreateVideoTag0Frame(ref FlvMessagePackWriter flvMessagePackWriter, uint previousTagSize, AVCDecoderConfigurationRecord aVCDecoderConfigurationRecord)
        {
            //flv body PreviousTagSize ScriptTag
            flvMessagePackWriter.WriteUInt32(previousTagSize);
            //flv body video tag
            //flv body tag header
            FlvTags flvTags = new FlvTags();

            flvTags.Type         = TagType.Video;
            flvTags.Timestamp    = 0;
            flvTags.TimestampExt = 0;
            flvTags.StreamId     = 0;
            //flv body tag body
            flvTags.VideoTagsData           = new VideoTags();
            flvTags.VideoTagsData.FrameType = FrameType.KeyFrame;
            flvTags.VideoTagsData.VideoData = new AvcVideoPacke();
            flvTags.VideoTagsData.VideoData.AvcPacketType           = AvcPacketType.SequenceHeader;
            flvTags.VideoTagsData.VideoData.CompositionTime         = 0;
            flvTags.VideoTagsData.VideoData.AVCDecoderConfiguration = aVCDecoderConfigurationRecord;
            flvMessagePackWriter.WriteFlvTag(flvTags);
        }