Beispiel #1
0
        public static FLVTag Parse(HDSBinaryReader br)
        {
            if (!br.BaseStream.CanRead || (br.BytesAvailable <= TAG_HEADER_BYTE_COUNT))
            {
                return(null);
            }
            FLVTag tag;

            byte    b    = br.ReadByte();
            TagType type = (TagType)(b & 0x1f);

            switch (type)
            {
            case TagType.AUDIO:
            case TagType.AKAMAI_ENC_AUDIO: tag = new FLVTagAudio(type); break;

            case TagType.VIDEO:
            case TagType.AKAMAI_ENC_VIDEO: tag = new FLVTagVideo(type); break;

            default:                       tag = new FLVTag(type);      break;
            }
            tag.Filter = (b & 0x20) > 0;
            uint dataSize = br.ReadUInt24();

            tag.Timestamp = br.ReadUInt24() + (uint)(br.ReadByte() << 24);
            uint StreamID = br.ReadUInt24();

            tag.Data = br.ReadBytes((int)dataSize);
            if (br.BytesAvailable > 3)
            {
                tag.SizeOfPreviousPacket = br.ReadUInt32();
            }
            return(tag);
        }
Beispiel #2
0
        public void Write(FLVTag tag)
        {
            if (tag == null || tag.Data == null || tag.Data.Length == 0)
            {
                return;
            }

            if (tag.Filter)
            {
                throw new InvalidOperationException("This media encrypted with Adobe Access DRM. Not supported. Sorry. :`(");
            }

            lock (lock4write) {
                if (!FLVHeaderWritten)
                {
                    WriteFlvHeader();
                }

                if (tag is FLVTagAudio)
                {
                    var tagAudio = tag as FLVTagAudio;
                    if (!AAC_HeaderWritten && tagAudio.IsAACSequenceHeader)
                    {
                        AAC_HeaderWritten = true;
                    }
                    else if (AAC_HeaderWritten && tagAudio.IsAACSequenceHeader)
                    {
                        return;
                    }

                    SizeAudio += tag.DataSize;
                }
                else if (tag is FLVTagVideo)
                {
                    var tagVideo = tag as FLVTagVideo;
                    if (!AVC_HeaderWritten && tagVideo.CodecID == FLVTagVideo.Codec.AVC && tagVideo.AvcPacketType == FLVTagVideo.AVCPacket.SEQUENCE_HEADER)
                    {
                        AVC_HeaderWritten = true;
                    }
                    else if (AVC_HeaderWritten && tagVideo.CodecID == FLVTagVideo.Codec.AVC && tagVideo.AvcPacketType == FLVTagVideo.AVCPacket.SEQUENCE_HEADER)
                    {
                        return;
                    }

                    SizeVideo += tag.DataSize;
                }

                HDSDumper.FixTimestamp(DecoderState, tag);

                if (LastTimestamp < tag.Timestamp)
                {
                    LastTimestamp = tag.Timestamp;
                }

                WriteData(tag.GetBytes());

                Frames++;
            }
        }
Beispiel #3
0
        public static void GetVideoAndAudioTags(TagsStore tagsStore, byte[] data)
        {
            MemoryStream stream = null;

            try {
                stream = new MemoryStream(data);
                using (HDSBinaryReader br = new HDSBinaryReader(stream)) {
                    stream = null;
                    FLVTag tag = Parse(br);
                    while (tag != null)
                    {
                        // only audio or video and skipping small sized packet
                        if (tag.DataSize > 2 && (tag.Type == TagType.AUDIO || tag.Type == TagType.AKAMAI_ENC_AUDIO || tag.Type == TagType.VIDEO || tag.Type == TagType.AKAMAI_ENC_VIDEO))
                        {
                            if (!tagsStore.hasAudio && tag is FLVTagAudio audioTag)
                            {
                                tagsStore.hasAudio      = true;
                                tagsStore.AudioFormat   = audioTag.SoundFormat;
                                tagsStore.AudioRate     = audioTag.SoundRate;
                                tagsStore.AudioChannels = audioTag.SoundChannels;
                            }

                            if (!tagsStore.hasVideo && tag is FLVTagVideo videoTag)
                            {
                                tagsStore.hasVideo   = true;
                                tagsStore.VideoCodec = videoTag.CodecID;
                            }

                            tagsStore.Enqueue(tag);

                            if (tag.Timestamp > tagsStore.lastTS)
                            {
                                tagsStore.lastTS = tag.Timestamp;
                            }
                            if (!tagsStore.isAkamaiEncrypted && tag.IsAkamaiEncrypted)
                            {
                                tagsStore.isAkamaiEncrypted = true;
                            }
                        }
                        tag = Parse(br);
                    }
                }
            } finally {
                if (stream != null)
                {
                    stream.Dispose();
                }
            }
        }
 public void DecryptFLVTag(flv.FLVTag tag, string baseUrl, string auth)
 {
     if (tag.Type == flv.FLVTag.TagType.AKAMAI_ENC_AUDIO)
     {
         tag.Type = flv.FLVTag.TagType.AUDIO;
     }
     else if (tag.Type == flv.FLVTag.TagType.AKAMAI_ENC_VIDEO)
     {
         tag.Type = flv.FLVTag.TagType.VIDEO;
     }
     else
     {
         return;
     }
     tag.Data = Decrypt(tag.Data, 0, baseUrl, auth);
 }
Beispiel #5
0
        public static List <FLVTag> GetTags(byte[] data)
        {
            List <FLVTag> tags   = new List <FLVTag>();
            MemoryStream  stream = null;

            try {
                stream = new MemoryStream(data);
                using (HDSBinaryReader br = new HDSBinaryReader(stream)) {
                    stream = null;
                    FLVTag tag = Parse(br);
                    while (tag != null)
                    {
                        tags.Add(tag);
                        tag = Parse(br);
                    }
                }
            } finally {
                if (stream != null)
                {
                    stream.Dispose();
                }
            }
            return(tags);
        }