diff --git a/doc/video_file_format_spec_v10.pdf b/doc/video_file_format_spec_v10.pdf new file mode 100644 index 0000000..e14f34b Binary files /dev/null and b/doc/video_file_format_spec_v10.pdf differ diff --git a/src/JT1078.Flv.Test/FlvMessagePackWriter_Amf3_Test.cs b/src/JT1078.Flv.Test/FlvMessagePackWriter_Amf3_Test.cs new file mode 100644 index 0000000..18049e7 --- /dev/null +++ b/src/JT1078.Flv.Test/FlvMessagePackWriter_Amf3_Test.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Xunit; +using JT1078.Flv.MessagePack; +using JT1078.Flv.Metadata; +using JT1078.Flv.Extensions; + +namespace JT1078.Flv.Test +{ + + public class FlvMessagePackWriter_Amf3_Test + { + [Fact] + public void FlvMessagePackWriter_Amf3_Test_1() { + Span buffer = new byte[1024]; + FlvMessagePackWriter flvMessagePackWriter = new FlvMessagePackWriter(buffer); + flvMessagePackWriter.WriteAmf3(new Flv.Metadata.Amf3 { + Amf3Metadatas=new List { + new Amf3Metadata_Duration{ + Value=7.22100 + }, + new Amf3Metadata_FileSize{ + Value=2005421.00000 + } + } + }); + + + Amf3Metadata_Duration amf3Metadata = new Amf3Metadata_Duration(); + amf3Metadata.Value = 7.22100; + var hex = amf3Metadata.ToBuffer().ToArray().ToHexString(); + Assert.Equal("00086475726174696F6E00401CE24DD2F1A9FC", buffer.Slice(5, hex.Length/2).ToArray().ToHexString()); + + + Amf3Metadata_FileSize amf3Metadata_FileSize = new Amf3Metadata_FileSize(); + amf3Metadata_FileSize.Value = 2005421.00000; + var hex1 = amf3Metadata_FileSize.ToBuffer().ToArray().ToHexString(); + Assert.Equal("000866696C6573697A6500413E99AD00000000", buffer.Slice(hex.Length/2+5, hex1.Length/2).ToArray().ToHexString()); + + } + } +} diff --git a/src/JT1078.Flv.Test/JT1078.Flv.Test.csproj b/src/JT1078.Flv.Test/JT1078.Flv.Test.csproj index baad934..da12930 100644 --- a/src/JT1078.Flv.Test/JT1078.Flv.Test.csproj +++ b/src/JT1078.Flv.Test/JT1078.Flv.Test.csproj @@ -17,4 +17,8 @@ + + + + diff --git a/src/JT1078.Flv.Test/Metadata/Amf3MetadataTest.cs b/src/JT1078.Flv.Test/Metadata/Amf3MetadataTest.cs index f7643ee..c7e8ea5 100644 --- a/src/JT1078.Flv.Test/Metadata/Amf3MetadataTest.cs +++ b/src/JT1078.Flv.Test/Metadata/Amf3MetadataTest.cs @@ -25,5 +25,59 @@ namespace JT1078.Flv.Test.Metadata var buffer = BitConverter.GetBytes(7.22100); //flv需要倒序的 } + + [Fact] + public void Amf3Metadata_FileSize_Test_1_1() + { + Amf3Metadata_FileSize amf3Metadata_FileSize = new Amf3Metadata_FileSize(); + amf3Metadata_FileSize.Value = 2005421.00000; + var hex = amf3Metadata_FileSize.ToBuffer().ToArray().ToHexString(); + Assert.Equal("000866696C6573697A6500413E99AD00000000", hex); + } + + [Fact] + public void Amf3Metadata_FrameRate_Test_1_1() + { + Amf3Metadata_FrameRate amf3Metadata_FrameRate = new Amf3Metadata_FrameRate(); + amf3Metadata_FrameRate.Value = 29.16667; + var hex = amf3Metadata_FrameRate.ToBuffer().ToArray().ToHexString(); + Assert.Equal("00096672616D657261746500403D2AAAE297396D", hex); + } + + [Fact] + public void Amf3Metadata_Height_Test_1_1() + { + Amf3Metadata_Height amf3Metadata_Height = new Amf3Metadata_Height(); + amf3Metadata_Height.Value = 960.00000; + var hex = amf3Metadata_Height.ToBuffer().ToArray().ToHexString(); + Assert.Equal("000668656967687400408E000000000000", hex); + } + + [Fact] + public void Amf3Metadata_VideoCodecId_Test_1_1() + { + Amf3Metadata_VideoCodecId amf3Metadata_VideoCodecId = new Amf3Metadata_VideoCodecId(); + amf3Metadata_VideoCodecId.Value = 7.00000; + var hex = amf3Metadata_VideoCodecId.ToBuffer().ToArray().ToHexString(); + Assert.Equal("000C766964656F636F646563696400401C000000000000", hex); + } + + [Fact] + public void Amf3Metadata_VideoDataRate_Test_1_1() + { + Amf3Metadata_VideoDataRate amf3Metadata_VideoDataRate = new Amf3Metadata_VideoDataRate(); + amf3Metadata_VideoDataRate.Value = 0.00000; + var hex = amf3Metadata_VideoDataRate.ToBuffer().ToArray().ToHexString(); + Assert.Equal("000D766964656F6461746172617465000000000000000000", hex); + } + + [Fact] + public void Amf3Metadata_Width_Test_1_1() + { + Amf3Metadata_Width amf3Metadata_Width = new Amf3Metadata_Width(); + amf3Metadata_Width.Value = 544.00000; + var hex = amf3Metadata_Width.ToBuffer().ToArray().ToHexString(); + Assert.Equal("00057769647468004081000000000000", hex); + } } } diff --git a/src/JT1078.Flv/MessagePack/FlvMessagePackWriter_Amf3.cs b/src/JT1078.Flv/MessagePack/FlvMessagePackWriter_Amf3.cs index 0486a20..6808cc0 100644 --- a/src/JT1078.Flv/MessagePack/FlvMessagePackWriter_Amf3.cs +++ b/src/JT1078.Flv/MessagePack/FlvMessagePackWriter_Amf3.cs @@ -1,8 +1,10 @@ using JT1078.Flv.Metadata; using System; using System.Collections.Generic; +using System.Runtime.CompilerServices; using System.Text; +[assembly: InternalsVisibleTo("JT1078.Flv.Test")] namespace JT1078.Flv.MessagePack { ref partial struct FlvMessagePackWriter diff --git a/src/JT1078.Flv/Metadata/Amf3Metadata_FileSize.cs b/src/JT1078.Flv/Metadata/Amf3Metadata_FileSize.cs index 4a94386..83c91a4 100644 --- a/src/JT1078.Flv/Metadata/Amf3Metadata_FileSize.cs +++ b/src/JT1078.Flv/Metadata/Amf3Metadata_FileSize.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Text; using System.Buffers.Binary; +using JT1078.Flv.Extensions; namespace JT1078.Flv.Metadata { @@ -17,9 +18,9 @@ namespace JT1078.Flv.Metadata Span tmp = new byte[2+8+1+8]; var b1 = Encoding.ASCII.GetBytes(FieldName); BinaryPrimitives.WriteUInt16BigEndian(tmp, (ushort)b1.Length); - b1.CopyTo(tmp.Slice(4)); - tmp[11] = DataType; - BinaryPrimitives.WriteInt64BigEndian(tmp.Slice(12), (long)Value); + b1.CopyTo(tmp.Slice(2)); + tmp[10] = DataType; + this.WriteDouble(tmp.Slice(11)); return tmp; } } diff --git a/src/JT1078.Flv/Metadata/Amf3Metadata_FrameRate.cs b/src/JT1078.Flv/Metadata/Amf3Metadata_FrameRate.cs index baf88bc..c4ba87f 100644 --- a/src/JT1078.Flv/Metadata/Amf3Metadata_FrameRate.cs +++ b/src/JT1078.Flv/Metadata/Amf3Metadata_FrameRate.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Text; using System.Buffers.Binary; +using JT1078.Flv.Extensions; namespace JT1078.Flv.Metadata { @@ -14,12 +15,12 @@ namespace JT1078.Flv.Metadata public ReadOnlySpan ToBuffer() { - Span tmp = new byte[4+9+1+8]; + Span tmp = new byte[2+9+1+8]; var b1 = Encoding.ASCII.GetBytes(FieldName); BinaryPrimitives.WriteUInt16BigEndian(tmp, (ushort)b1.Length); - b1.CopyTo(tmp.Slice(4)); + b1.CopyTo(tmp.Slice(2)); tmp[11] = DataType; - BinaryPrimitives.WriteInt64BigEndian(tmp.Slice(12), (long)Value); + this.WriteDouble(tmp.Slice(12)); return tmp; } } diff --git a/src/JT1078.Flv/Metadata/Amf3Metadata_Height.cs b/src/JT1078.Flv/Metadata/Amf3Metadata_Height.cs index 1f46f04..75f199b 100644 --- a/src/JT1078.Flv/Metadata/Amf3Metadata_Height.cs +++ b/src/JT1078.Flv/Metadata/Amf3Metadata_Height.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Text; using System.Buffers.Binary; +using JT1078.Flv.Extensions; namespace JT1078.Flv.Metadata { @@ -14,12 +15,12 @@ namespace JT1078.Flv.Metadata public ReadOnlySpan ToBuffer() { - Span tmp = new byte[4+6+1+8]; + Span tmp = new byte[2+6+1+8]; var b1 = Encoding.ASCII.GetBytes(FieldName); BinaryPrimitives.WriteUInt16BigEndian(tmp, (ushort)b1.Length); - b1.CopyTo(tmp.Slice(3)); - tmp[11] = DataType; - BinaryPrimitives.WriteInt64BigEndian(tmp.Slice(12), (long)Value); + b1.CopyTo(tmp.Slice(2)); + tmp[8] = DataType; + this.WriteDouble(tmp.Slice(9)); return tmp; } } diff --git a/src/JT1078.Flv/Metadata/Amf3Metadata_VideoCodecId.cs b/src/JT1078.Flv/Metadata/Amf3Metadata_VideoCodecId.cs index a44a25b..9372420 100644 --- a/src/JT1078.Flv/Metadata/Amf3Metadata_VideoCodecId.cs +++ b/src/JT1078.Flv/Metadata/Amf3Metadata_VideoCodecId.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Text; using System.Buffers.Binary; +using JT1078.Flv.Extensions; namespace JT1078.Flv.Metadata { @@ -14,12 +15,12 @@ namespace JT1078.Flv.Metadata public ReadOnlySpan ToBuffer() { - Span tmp = new byte[4+12+1+8]; + Span tmp = new byte[2+12+1+8]; var b1 = Encoding.ASCII.GetBytes(FieldName); BinaryPrimitives.WriteUInt16BigEndian(tmp, (ushort)b1.Length); - b1.CopyTo(tmp.Slice(4)); - tmp[11] = DataType; - BinaryPrimitives.WriteInt64BigEndian(tmp.Slice(12), (long)Value); + b1.CopyTo(tmp.Slice(2)); + tmp[14] = DataType; + this.WriteDouble(tmp.Slice(15)); return tmp; } } diff --git a/src/JT1078.Flv/Metadata/Amf3Metadata_VideoDataRate.cs b/src/JT1078.Flv/Metadata/Amf3Metadata_VideoDataRate.cs index f62a900..7070608 100644 --- a/src/JT1078.Flv/Metadata/Amf3Metadata_VideoDataRate.cs +++ b/src/JT1078.Flv/Metadata/Amf3Metadata_VideoDataRate.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Text; using System.Buffers.Binary; +using JT1078.Flv.Extensions; namespace JT1078.Flv.Metadata { @@ -14,12 +15,12 @@ namespace JT1078.Flv.Metadata public ReadOnlySpan ToBuffer() { - Span tmp = new byte[4+13+1+8]; + Span tmp = new byte[2+13+1+8]; var b1 = Encoding.ASCII.GetBytes(FieldName); BinaryPrimitives.WriteUInt16BigEndian(tmp, (ushort)b1.Length); - b1.CopyTo(tmp.Slice(4)); - tmp[11] = DataType; - BinaryPrimitives.WriteInt64BigEndian(tmp.Slice(12), (long)Value); + b1.CopyTo(tmp.Slice(2)); + tmp[15] = DataType; + this.WriteDouble(tmp.Slice(16)); return tmp; } } diff --git a/src/JT1078.Flv/Metadata/Amf3Metadata_Width.cs b/src/JT1078.Flv/Metadata/Amf3Metadata_Width.cs index e133ff8..622933a 100644 --- a/src/JT1078.Flv/Metadata/Amf3Metadata_Width.cs +++ b/src/JT1078.Flv/Metadata/Amf3Metadata_Width.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Text; using System.Buffers.Binary; +using JT1078.Flv.Extensions; namespace JT1078.Flv.Metadata { @@ -14,12 +15,12 @@ namespace JT1078.Flv.Metadata public ReadOnlySpan ToBuffer() { - Span tmp = new byte[4+5+1+8]; + Span tmp = new byte[2+5+1+8]; var b1 = Encoding.ASCII.GetBytes(FieldName); BinaryPrimitives.WriteUInt16BigEndian(tmp, (ushort)b1.Length); - b1.CopyTo(tmp.Slice(3)); - tmp[11] = DataType; - BinaryPrimitives.WriteInt64BigEndian(tmp.Slice(12), (long)Value); + b1.CopyTo(tmp.Slice(2)); + tmp[7] = DataType; + this.WriteDouble(tmp.Slice(8)); return tmp; } }