상속: IDisposable
예제 #1
0
 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();
 }
예제 #2
0
 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();
 }
예제 #3
0
        private static byte[] CreateBody(long window_size)
        {
            var s = new MemoryStream();

            using (var writer = new RTMPBinaryWriter(s)) {
                writer.WriteUInt32(window_size);
            }
            return(s.ToArray());
        }
예제 #4
0
        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());
        }
예제 #5
0
        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());
        }
예제 #6
0
        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());
        }
예제 #7
0
            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());
            }
예제 #8
0
            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());
            }
예제 #9
0
        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());
        }
예제 #10
0
        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());
        }
예제 #11
0
            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());
            }
예제 #12
0
        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));
        }
예제 #13
0
        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;
                }
            }
        }
예제 #14
0
        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());
        }
예제 #15
0
        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);
        }
예제 #16
0
 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();
 }
예제 #17
0
   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();
   }
예제 #18
0
 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();
 }
예제 #19
0
 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();
 }
예제 #20
0
 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();
 }
예제 #21
0
 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();
 }
예제 #22
0
 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);
 }
예제 #23
0
 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);
       }
 }
예제 #24
0
 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;
 }
예제 #25
0
 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;
     }
       }
 }
예제 #26
0
 private static byte[] CreateBody(long window_size)
 {
     var s = new MemoryStream();
       using (var writer=new RTMPBinaryWriter(s)) {
     writer.WriteUInt32(window_size);
       }
       return s.ToArray();
 }
예제 #27
0
		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();
				}
			});
		}
예제 #28
0
 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();
 }
예제 #29
0
 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;
 }