private static byte[] CreateBody(int chunk_size) { var s = new MemoryStream(); using (var writer=new RTMPBinaryWriter(s)) { writer.WriteUInt32(Math.Max(1, Math.Min(chunk_size, 0x7FFFFFFF))); } return s.ToArray(); }
private static byte[] CreateBody(int target_chunk_stream) { var s = new MemoryStream(); using (var writer=new RTMPBinaryWriter(s)) { writer.WriteUInt32(target_chunk_stream); } return s.ToArray(); }
private static byte[] CreateBody(long window_size) { var s = new MemoryStream(); using (var writer = new RTMPBinaryWriter(s)) { writer.WriteUInt32(window_size); } return(s.ToArray()); }
private static byte[] CreateBody(long sequence_number) { var s = new MemoryStream(); using (var writer = new RTMPBinaryWriter(s)) { writer.WriteUInt32(sequence_number & 0xFFFFFFFF); } return(s.ToArray()); }
private static byte[] CreateBody(int target_chunk_stream) { var s = new MemoryStream(); using (var writer = new RTMPBinaryWriter(s)) { writer.WriteUInt32(target_chunk_stream); } return(s.ToArray()); }
private static byte[] CreateBody(int chunk_size) { var s = new MemoryStream(); using (var writer = new RTMPBinaryWriter(s)) { writer.WriteUInt32(Math.Max(1, Math.Min(chunk_size, 0x7FFFFFFF))); } return(s.ToArray()); }
private static byte[] CreateBody(long local_timestamp) { var s = new MemoryStream(); using (var writer = new RTMPBinaryWriter(s)) { writer.WriteUInt16((int)UserControlMessageType.PingResponse); writer.WriteUInt32(local_timestamp); } return(s.ToArray()); }
private static byte[] CreateBody(long target_stream_id) { var s = new MemoryStream(); using (var writer = new RTMPBinaryWriter(s)) { writer.WriteUInt16((int)UserControlMessageType.StreamIsRecorded); writer.WriteUInt32(target_stream_id); } return(s.ToArray()); }
private static byte[] CreateBody(UserControlMessageType message_type, byte[] payload) { var s = new MemoryStream(); using (var writer = new RTMPBinaryWriter(s)) { writer.WriteUInt16((int)message_type); writer.Write(payload); } return(s.ToArray()); }
private static byte[] CreateBody(long bandwidth, PeerBandwidthLimitType limit_type) { var s = new MemoryStream(); using (var writer = new RTMPBinaryWriter(s)) { writer.WriteUInt32(bandwidth); writer.Write((byte)limit_type); } return(s.ToArray()); }
private static byte[] CreateBody(long target_stream_id, TimeSpan buffer_length) { var s = new MemoryStream(); using (var writer = new RTMPBinaryWriter(s)) { writer.WriteUInt16((int)UserControlMessageType.SetBufferLength); writer.WriteUInt32(target_stream_id); writer.WriteUInt32((long)buffer_length.TotalMilliseconds); } return(s.ToArray()); }
protected Task SendAsync(Action <RTMPBinaryWriter> proc, CancellationToken cancel_token) { var memstream = new MemoryStream(); using (memstream) { using (var writer = new RTMPBinaryWriter(memstream)) { proc.Invoke(writer); } } return(connection.Stream.WriteAsync(memstream.ToArray(), cancel_token)); }
void SendMessage(int chunk_stream_id, RTMPMessage msg) { int offset = 0; int fmt = 0; while (msg.Body.Length - offset > 0) { switch (fmt) { case 0: Send(s => { using (var writer = new RTMPBinaryWriter(s)) { writer.Write((byte)((fmt << 6) | chunk_stream_id)); if (msg.Timestamp > 0xFFFFFF) { writer.WriteUInt24(0xFFFFFF); } else { writer.WriteUInt24((int)msg.Timestamp); } writer.WriteUInt24(msg.Body.Length); writer.Write((byte)msg.MessageType); writer.WriteUInt32LE(msg.StreamId); if (msg.Timestamp > 0xFFFFFF) { writer.WriteUInt32(msg.Timestamp); } int chunk_len = Math.Min(sendChunkSize, msg.Body.Length - offset); writer.Write(msg.Body, offset, chunk_len); offset += chunk_len; } }); fmt = 3; break; case 3: Send(s => { using (var writer = new RTMPBinaryWriter(s)) { writer.Write((byte)((fmt << 6) | chunk_stream_id)); int chunk_len = Math.Min(sendChunkSize, msg.Body.Length - offset); writer.Write(msg.Body, offset, chunk_len); offset += chunk_len; } }); break; } } }
private static byte[] CreateBody( long timestamp, IEnumerable <RTMPMessage> messages) { var s = new MemoryStream(); using (var writer = new RTMPBinaryWriter(s)) { foreach (var msg in messages) { writer.Write((byte)msg.MessageType); writer.WriteUInt24(msg.Body.Length); var t = msg.Timestamp - timestamp; writer.WriteUInt24((int)t & 0xFFFFFF); writer.Write((byte)((t >> 24) & 0xFF)); writer.WriteUInt24((int)msg.StreamId); writer.Write(msg.Body, 0, msg.Body.Length); writer.Write(msg.Body.Length + 11); } } return(s.ToArray()); }
private bool Handshake() { Logger.Debug("Handshake start"); var rand = new Random(); var c0 = Recv(1); Send(new byte[] { 0x03 }); var s1vec = new byte[1528]; rand.NextBytes(s1vec); Send(s => { using (var writer = new RTMPBinaryWriter(s)) { writer.Write(0); writer.Write(0); writer.Write(s1vec); } }); using (var reader = new RTMPBinaryReader(new MemoryStream(Recv(1536)))) { Send(s => { using (var writer = new RTMPBinaryWriter(s)) { writer.Write(reader.ReadInt32()); writer.Write(reader.ReadInt32()); writer.Write(reader.ReadBytes(1528)); } }); } using (var reader = new RTMPBinaryReader(new MemoryStream(Recv(1536)))) { reader.ReadInt32(); reader.ReadInt32(); if (!s1vec.Equals(reader.ReadBytes(1528))) { Logger.Debug("Handshake failed"); return(false); } } timer.Reset(); timer.Start(); Logger.Debug("Handshake succeeded"); return(true); }
private static byte[] CreateBody(long bandwidth, PeerBandwidthLimitType limit_type) { var s = new MemoryStream(); using (var writer=new RTMPBinaryWriter(s)) { writer.WriteUInt32(bandwidth); writer.Write((byte)limit_type); } return s.ToArray(); }
private static byte[] CreateBody( long timestamp, IEnumerable<RTMPMessage> messages) { var s = new MemoryStream(); using (var writer=new RTMPBinaryWriter(s)) { foreach (var msg in messages) { writer.Write((byte)msg.MessageType); writer.WriteUInt24(msg.Body.Length); var t = msg.Timestamp-timestamp; writer.WriteUInt24((int)t & 0xFFFFFF); writer.Write((byte)((t>>24) & 0xFF)); writer.WriteUInt24((int)msg.StreamId); writer.Write(msg.Body, 0, msg.Body.Length); writer.Write(msg.Body.Length+11); } } return s.ToArray(); }
private static byte[] CreateBody(long sequence_number) { var s = new MemoryStream(); using (var writer=new RTMPBinaryWriter(s)) { writer.WriteUInt32(sequence_number & 0xFFFFFFFF); } return s.ToArray(); }
private static byte[] CreateBody(long target_stream_id) { var s = new MemoryStream(); using (var writer=new RTMPBinaryWriter(s)) { writer.WriteUInt16((int)UserControlMessageType.StreamIsRecorded); writer.WriteUInt32(target_stream_id); } return s.ToArray(); }
private static byte[] CreateBody(long target_stream_id, TimeSpan buffer_length) { var s = new MemoryStream(); using (var writer=new RTMPBinaryWriter(s)) { writer.WriteUInt16((int)UserControlMessageType.SetBufferLength); writer.WriteUInt32(target_stream_id); writer.WriteUInt32((long)buffer_length.TotalMilliseconds); } return s.ToArray(); }
private static byte[] CreateBody(UserControlMessageType message_type, byte[] payload) { var s = new MemoryStream(); using (var writer=new RTMPBinaryWriter(s)) { writer.WriteUInt16((int)message_type); writer.Write(payload); } return s.ToArray(); }
protected Task SendAsync(Action<RTMPBinaryWriter> proc, CancellationToken cancel_token) { var memstream = new MemoryStream(); using (memstream) { using (var writer=new RTMPBinaryWriter(memstream)) { proc.Invoke(writer); } } return connection.Stream.WriteAsync(memstream.ToArray(), cancel_token); }
private void WriteMessage(Stream stream, RTMPMessage msg, long time_origin) { var timestamp = Math.Max(0, msg.Timestamp-time_origin); using (var writer=new RTMPBinaryWriter(stream, true)) { writer.Write((byte)msg.MessageType); writer.WriteUInt24(msg.Body.Length); writer.WriteUInt24((int)timestamp & 0xFFFFFF); writer.Write((byte)((timestamp>>24) & 0xFF)); writer.WriteUInt24(0); writer.Write(msg.Body, 0, msg.Body.Length); writer.Write(msg.Body.Length+11); } }
private void OnHeaderChanged(RTMPMessage msg) { FlushContents(); var s = new MemoryStream(); using (s) { using (var writer=new RTMPBinaryWriter(s, true)) { writer.Write((byte)'F'); writer.Write((byte)'L'); writer.Write((byte)'V'); writer.Write((byte)1); writer.Write((byte)5); writer.WriteUInt32(9); writer.WriteUInt32(0); } if (metadata!=null) WriteMessage(s, metadata, 0xFFFFFFFF); if (audioHeader!=null) WriteMessage(s, audioHeader, 0xFFFFFFFF); if (videoHeader!=null) WriteMessage(s, videoHeader, 0xFFFFFFFF); } streamIndex = TargetChannel.GenerateStreamID(); streamOrigin = DateTime.Now; timestampOrigin = msg.Timestamp; var bytes = s.ToArray(); contents.ContentHeader = new Content(streamIndex, TimeSpan.Zero, position, bytes); position += bytes.Length; }
void SendMessage(int chunk_stream_id, RTMPMessage msg) { int offset = 0; int fmt = 0; while (msg.Body.Length-offset>0) { switch (fmt) { case 0: Send(s => { using (var writer=new RTMPBinaryWriter(s)) { writer.Write((byte)((fmt<<6) | chunk_stream_id)); if (msg.Timestamp>0xFFFFFF) { writer.WriteUInt24(0xFFFFFF); } else { writer.WriteUInt24((int)msg.Timestamp); } writer.WriteUInt24(msg.Body.Length); writer.Write((byte)msg.MessageType); writer.WriteUInt32LE(msg.StreamId); if (msg.Timestamp>0xFFFFFF) { writer.WriteUInt32(msg.Timestamp); } int chunk_len = Math.Min(sendChunkSize, msg.Body.Length-offset); writer.Write(msg.Body, offset, chunk_len); offset += chunk_len; } }); fmt = 3; break; case 3: Send(s => { using (var writer=new RTMPBinaryWriter(s)) { writer.Write((byte)((fmt<<6) | chunk_stream_id)); int chunk_len = Math.Min(sendChunkSize, msg.Body.Length-offset); writer.Write(msg.Body, offset, chunk_len); offset += chunk_len; } }); break; } } }
private static byte[] CreateBody(long window_size) { var s = new MemoryStream(); using (var writer=new RTMPBinaryWriter(s)) { writer.WriteUInt32(window_size); } return s.ToArray(); }
private async Task Send(Func<RTMPBinaryWriter, Task> write_action, CancellationToken cancel_token) { var buf = new MemoryStream(); using (var writer=new RTMPBinaryWriter(buf)) { await write_action.Invoke(writer); } buf.Close(); var ary = buf.ToArray(); await Task.Run(() => { lock (OutputStream) { OutputStream.WriteAsync(ary, 0, ary.Length, cancel_token).Wait(); } }); }
private static byte[] CreateBody(long local_timestamp) { var s = new MemoryStream(); using (var writer=new RTMPBinaryWriter(s)) { writer.WriteUInt16((int)UserControlMessageType.PingResponse); writer.WriteUInt32(local_timestamp); } return s.ToArray(); }
private bool Handshake() { Logger.Debug("Handshake start"); var rand = new Random(); var c0 = Recv(1); Send(new byte[] { 0x03 }); var s1vec = new byte[1528]; rand.NextBytes(s1vec); Send(s => { using (var writer=new RTMPBinaryWriter(s)) { writer.Write(0); writer.Write(0); writer.Write(s1vec); } }); using (var reader=new RTMPBinaryReader(new MemoryStream(Recv(1536)))) { Send(s => { using (var writer=new RTMPBinaryWriter(s)) { writer.Write(reader.ReadInt32()); writer.Write(reader.ReadInt32()); writer.Write(reader.ReadBytes(1528)); } }); } using (var reader=new RTMPBinaryReader(new MemoryStream(Recv(1536)))) { reader.ReadInt32(); reader.ReadInt32(); if (!s1vec.Equals(reader.ReadBytes(1528))) { Logger.Debug("Handshake failed"); return false; } } timer.Reset(); timer.Start(); Logger.Debug("Handshake succeeded"); return true; }