Exemplo n.º 1
0
        public static async Task <MediaStreamSample> CreateVideoSample(this VideoTag tag, VideoTag FirstTag, bool hasHead)
        {
            var stream = tag.createVideoStream(FirstTag, hasHead);// flv.createVideoStream(stream.AsStream(), vi);
            var sample = await MediaStreamSample.CreateFromStreamAsync(
                stream.AsInputStream(),
                (uint)stream.Length,
                tag.TimeSpan); //每一段的数据大小

            //sample.Duration = tag.TimeSpan;//BUG
            return(sample);

            #region MyRegion

            //Debug.WriteLine("CreateAudioSample:" + tag.Count + "  " + tag.GetDataStream().Length);

            //Debug.WriteLine(BitConverter.ToString( tag.GetDataInputStream().AsStreamForRead().ReadBytes(30) ));



            /*
             * var stream =  tag.createVideoStream(Videos[0], vi <=2);
             * var si = stream.AsInputStream();
             * var sample = await MediaStreamSample.CreateFromStreamAsync(si, (uint)stream.Length, tag.TimeSpan);//每一段的数据大小
             */
            #endregion
        }
Exemplo n.º 2
0
 public FlvParse(Stream stream)
 {
     Head = new FlvHead(stream);
     if (Head.Signature != "FLV")
     {
         throw new Exception("格式错误:" + Head.Signature);
     }
     while (true)
     {
         var    PreviousTagSize = stream.ReadUInt32();
         FlvTag flvTag          = FlvTag.createTag(stream);
         if (flvTag == null)
         {
             break;
         }
         var      data = flvTag.LoadTagData(stream);
         VideoTag v    = flvTag as VideoTag;
         if (v != null)
         {
             Videos.Add(v);
         }
         AudioTag a = flvTag as AudioTag;
         if (a != null)
         {
             Audios.Add(a);
         }
         ScriptDataTag d = flvTag as ScriptDataTag;
         if (d != null)
         {
             MediaData = d;
         }
         tags.Add(flvTag);
     }
 }
Exemplo n.º 3
0
        public FlvReader(Func <Stream> s)
        {
            this.CreateStream = s;

            Stream stream = this.CreateStream(); // new HttpStream(url);
            var    Head   = new FlvHead(stream); //9

            if (Head.Signature != "FLV")
            {
                throw new Exception("格式错误:" + Head.Signature);
            }
            while (FirstAudio == null || FirstVideo == null || MediaData == null)
            {
                this.readPreviousTag(stream);
            }


            //加载keyframes
            Task.Run(() => {
                double nxf = 0;
                double off = MediaData.Duration / 250;


                var obj = MediaData["keyframes"]?.GetObject();
                if (obj != null)
                {
                    times = obj["times"].GetArray().Select((d) => d.GetNumber()).ToList();
                    kfs   = obj["filepositions"].GetArray().Select((d) => (long)d.GetNumber()).ToList();
                    Debug.WriteLine("@@@ keyframes:ALL");
                    Debug.WriteLine("@@@ keyframes:ALL");
                }
                else
                {
                    Debug.WriteLine("@@@ keyframes:LOADSTART");
                    while (true)
                    {
                        var PreviousTagSize = stream.ReadUInt32();
                        FlvTag flvTag       = FlvTag.createTag(stream);
                        if (flvTag == null)
                        {
                            break;
                        }
                        var data = flvTag.LoadTagData(stream);
                        //关键帧
                        VideoTag tag = flvTag as VideoTag;
                        if (tag != null)
                        {
                            if (flvTag.TimeSpan.TotalSeconds >= nxf)
                            {
                                nxf += off;
                                times.Add(flvTag.TimeSpan.TotalSeconds);
                                kfs.Add(flvTag.Offset); //PreviousTagSize:4
                            }
                        }
                    }
                    Debug.WriteLine("@@@ keyframes:LOAdEND");
                }
            });
        }
Exemplo n.º 4
0
        public static VideoStreamDescriptor CreateVideoDesc(this VideoTag tag)
        {
            VideoEncodingProperties encode = null;

            if (tag.Codec == CodecID.H263)
            {
                encode = VideoEncodingProperties.CreateH264();
            }
            if (tag.Codec == CodecID.AVC)
            {
                encode = VideoEncodingProperties.CreateH264();
            }
            Debug.WriteLine("VideoDesc ## " + tag.Codec + "  " + tag.Type);
            return(new VideoStreamDescriptor(encode));
        }
Exemplo n.º 5
0
        static Stream createVideoStream(this VideoTag tag, VideoTag FirstTag, bool hasHead)
        {
            byte[]       startCode = new byte[] { 0, 0, 1 };
            MemoryStream stream    = new MemoryStream();

            if (hasHead)
            {
                var          mediaStream  = FirstTag.GetDataStream();//.GetDataInputStream().AsStreamForRead();
                BinaryReader binaryReader = new BinaryReader(mediaStream);

                var  configurationVersion  = binaryReader.ReadByte();
                var  avcProfileIndication  = binaryReader.ReadByte();
                var  avcCompatibleProfiles = binaryReader.ReadByte();
                var  avcLevelIndication    = binaryReader.ReadByte();
                byte lengthSizeMinusOne    = binaryReader.ReadByte();
                var  naluLengthSize        = (byte)(1 + (lengthSizeMinusOne & 3));

                byte numOfSequenceParameterSets = (byte)(binaryReader.ReadByte() & 0x1f);
                var  sequenceParameters         = new List <byte[]>(numOfSequenceParameterSets);
                for (uint i = 0; i < numOfSequenceParameterSets; i++)
                {
                    byte[] buffer = new byte[BytesToUInt16BE(binaryReader.ReadBytes(2))];
                    mediaStream.Read(buffer, 0, buffer.Length);
                    sequenceParameters.Add(buffer);
                }

                byte numOfPictureParameterSets = binaryReader.ReadByte();
                var  pictureParameters         = new List <byte[]>(numOfPictureParameterSets);
                for (uint i = 0; i < numOfPictureParameterSets; i++)
                {
                    byte[] buffer = new byte[BytesToUInt16BE(binaryReader.ReadBytes(2))];
                    mediaStream.Read(buffer, 0, buffer.Length);
                    pictureParameters.Add(buffer);
                }

                var sps = sequenceParameters[0];
                var pps = pictureParameters[0];

                stream.Write(startCode, 0, startCode.Length);
                stream.Write(sps, 0, sps.Length);
                stream.Write(startCode, 0, startCode.Length);
                stream.Write(pps, 0, pps.Length);
                // .WriteLine(mediaStream.Position.ToString("X00")+"|"+String.Join(" ", stream.ToArray().Select((s)=> { return s.ToString("X00"); } ) ) );
                //;
            }

            var          tagStream  = tag.GetDataStream(); //.GetDataInputStream().AsStreamForRead();
            BinaryReader reader     = new BinaryReader(tagStream);
            var          sampleSize = tagStream.Length;    //sample.Count;//

            while (sampleSize > 4L)
            {
                var ui32  = reader.ReadUInt32();
                var count = OldSkool.swaplong(ui32);
                stream.Write(startCode, 0, startCode.Length);
                stream.Write(reader.ReadBytes((int)count), 0, (int)count);
                sampleSize -= 4 + (uint)count;
            }
            stream.Position = 0;//非常 重要别删掉(返回的数据流必须从0开始播放
            return(stream);
        }