public byte[] GetBytes() { using (MemoryStream ms = new MemoryStream()) { ms.Write(Header, 0, Header.Length); if (!this.Meta.ContainsKey("duration")) { // assure that we add space for the duration key this.Meta["duration"] = 0.0; // place holder.. } if (!this.Meta.ContainsKey("lasttimestamp")) { // assure that we add space for the lasttimestamp this.Meta["lasttimestamp"] = 0.0; // place holder.. } byte[] meta = new AmfEncoderDecoder().EncodeMetaData(this.Meta, this.DebugOrder); Tag.TagSize = meta.Length; byte[] metahead = Tag.GetBytes(); ms.Write(metahead, 0, metahead.Length); //ms.Write(Tag.Data, 0, Tag.Data.Length); ms.Write(meta, 0, meta.Length); return(ms.GetBuffer().Take((int)ms.Position).ToArray()); } }
private void GrabHeader(byte[] buffer) { _ms.Write(buffer, 0, buffer.Length); if (_ms.Position >= BUF_LEN) { // have we filled enough? _ms.Position = 0; _ms.Read(_header.Header, 0, _header.Header.Length); // read the header. _ms.Read(_header.Tag.PrevSize, 0, sizeof(int)); // read prev tag len (should be 0). _header.Tag.TagType = (byte)_ms.ReadByte(); // Tag type (0x12 for Meta) Debug.Write(string.Format("Tag Type: {0}\n", _header.Tag.TagType)); Debug.Assert(_header.Tag.TagType == (byte)TagType.META); byte[] tagSize = new byte[sizeof(int)]; _ms.Read(tagSize, 1, 3); // read 24 bit body length. Note: Body length + 11 is the entire TAG size tagSize[0] = 0; Array.Reverse(tagSize); _header.Tag.TagSize = BitConverter.ToInt32(tagSize, 0); Debug.Write(string.Format("Tag Size: {0}\n", _header.Tag.TagSize)); byte[] tagMs = new byte[sizeof(int)]; _ms.Read(tagMs, 1, 3); // read 24 bit body length. Note: Body length + 11 is the entire TAG size tagMs[0] = 0; Array.Reverse(tagMs); _header.Tag.TimeStamp = BitConverter.ToInt32(tagMs, 0); Debug.Write(string.Format("Tag TimeStamp: {0}\n", _header.Tag.TimeStamp)); _header.Tag.TimeExtra = (byte)_ms.ReadByte(); _ms.Read(_header.Tag.StreamId, 0, _header.Tag.StreamId.Length); _header.Tag.Data = new byte[_header.Tag.TagSize]; _ms.Read(_header.Tag.Data, 0, _header.Tag.Data.Length); AmfEncoderDecoder amf = new AmfEncoderDecoder(); _header.Meta = amf.DecodeMetaData(_header.Tag.Data); _header.DebugOrder = amf.DebugOrder; byte[] header = _header.GetBytes(); _fs.Write(header, 0, header.Length); // Now that we have what we came for.. lets start writing the file out Tag by Tag... hasHeader = true; _readPos = 0; byte[] rest = _ms.GetBuffer().Skip((int)_ms.Position).Take((int)(_ms.Length - _ms.Position)).ToArray(); _ms.Position = 0; Write(rest); } }