private RtmpHeader ReadHeader() { int num1 = (int)this._reader.ReadByte(); AmfReader reader = this._reader; int chunkStreamId = RtmpPacketReader.GetChunkStreamId((byte)num1, reader); int num2 = 6; ChunkMessageHeaderType messageHeaderType = (ChunkMessageHeaderType)(num1 >> num2); RtmpHeader rtmpHeader1 = new RtmpHeader(); rtmpHeader1.StreamId = chunkStreamId; int num3 = (uint)messageHeaderType > 0U ? 1 : 0; rtmpHeader1.IsTimerRelative = num3 != 0; RtmpHeader rtmpHeader2 = rtmpHeader1; RtmpHeader rtmpHeader3; if (!this._rtmpHeaders.TryGetValue(chunkStreamId, out rtmpHeader3) && messageHeaderType != ChunkMessageHeaderType.New) { rtmpHeader3 = rtmpHeader2.Clone(); } switch (messageHeaderType) { case ChunkMessageHeaderType.New: rtmpHeader2.Timestamp = this._reader.ReadUInt24(); rtmpHeader2.PacketLength = this._reader.ReadUInt24(); rtmpHeader2.MessageType = (MessageType)this._reader.ReadByte(); rtmpHeader2.MessageStreamId = this._reader.ReadReverseInt(); break; case ChunkMessageHeaderType.SameSource: rtmpHeader2.Timestamp = this._reader.ReadUInt24(); rtmpHeader2.PacketLength = this._reader.ReadUInt24(); rtmpHeader2.MessageType = (MessageType)this._reader.ReadByte(); rtmpHeader2.MessageStreamId = rtmpHeader3.MessageStreamId; break; case ChunkMessageHeaderType.TimestampAdjustment: rtmpHeader2.Timestamp = this._reader.ReadUInt24(); rtmpHeader2.PacketLength = rtmpHeader3.PacketLength; rtmpHeader2.MessageType = rtmpHeader3.MessageType; rtmpHeader2.MessageStreamId = rtmpHeader3.MessageStreamId; break; case ChunkMessageHeaderType.Continuation: rtmpHeader2.Timestamp = rtmpHeader3.Timestamp; rtmpHeader2.PacketLength = rtmpHeader3.PacketLength; rtmpHeader2.MessageType = rtmpHeader3.MessageType; rtmpHeader2.MessageStreamId = rtmpHeader3.MessageStreamId; rtmpHeader2.IsTimerRelative = rtmpHeader3.IsTimerRelative; break; default: throw new SerializationException("Unexpected header type: " + (object)(int)messageHeaderType); } if (rtmpHeader2.Timestamp == 16777215) { rtmpHeader2.Timestamp = this._reader.ReadInt32(); } return(rtmpHeader2); }
private RtmpEvent ParsePacket(RtmpPacket packet) { switch (packet.Header.MessageType) { case MessageType.SetChunkSize: return(this.ParsePacket(packet, (Func <AmfReader, RtmpEvent>)(r => (RtmpEvent) new ChunkSize(r.ReadInt32())))); case MessageType.AbortMessage: return(this.ParsePacket(packet, (Func <AmfReader, RtmpEvent>)(r => (RtmpEvent) new Abort(r.ReadInt32())))); case MessageType.Acknowledgement: return(this.ParsePacket(packet, (Func <AmfReader, RtmpEvent>)(r => (RtmpEvent) new Acknowledgement(r.ReadInt32())))); case MessageType.UserControlMessage: return(this.ParsePacket(packet, (Func <AmfReader, RtmpEvent>)(r => { ushort num = r.ReadUInt16(); List <int> intList = new List <int>(); while (r.Length - r.Position >= 4L) { intList.Add(r.ReadInt32()); } return (RtmpEvent) new UserControlMessage((UserControlMessageType)num, intList.ToArray()); }))); case MessageType.WindowAcknowledgementSize: return(this.ParsePacket(packet, (Func <AmfReader, RtmpEvent>)(r => (RtmpEvent) new WindowAcknowledgementSize(r.ReadInt32())))); case MessageType.SetPeerBandwidth: return(this.ParsePacket(packet, (Func <AmfReader, RtmpEvent>)(r => (RtmpEvent) new PeerBandwidth(r.ReadInt32(), r.ReadByte())))); case MessageType.Audio: return(this.ParsePacket(packet, (Func <AmfReader, RtmpEvent>)(r => (RtmpEvent) new AudioData(packet.Buffer)))); case MessageType.Video: return(this.ParsePacket(packet, (Func <AmfReader, RtmpEvent>)(r => (RtmpEvent) new VideoData(packet.Buffer)))); case MessageType.DataAmf3: return(this.ParsePacket(packet, (Func <AmfReader, RtmpEvent>)(r => RtmpPacketReader.ReadCommandOrData(r, (Command) new NotifyAmf3())))); case MessageType.CommandAmf3: return(this.ParsePacket(packet, (Func <AmfReader, RtmpEvent>)(r => { int num = (int)r.ReadByte(); return RtmpPacketReader.ReadCommandOrData(r, (Command) new InvokeAmf3()); }))); case MessageType.DataAmf0: return(this.ParsePacket(packet, (Func <AmfReader, RtmpEvent>)(r => RtmpPacketReader.ReadCommandOrData(r, (Command) new NotifyAmf0())))); case MessageType.CommandAmf0: return(this.ParsePacket(packet, (Func <AmfReader, RtmpEvent>)(r => RtmpPacketReader.ReadCommandOrData(r, (Command) new InvokeAmf0())))); default: return((RtmpEvent)null); } }
public RtmpConnect(Socket client_socket, Stream stream, RtmpServer server, ushort client_id, SerializationContext context, ObjectEncoding objectEncoding = ObjectEncoding.Amf0, bool asyncMode = false) { ClientId = client_id; clientSocket = client_socket; this.server = server; this.objectEncoding = objectEncoding; writer = new RtmpPacketWriter(new AmfWriter(stream, context, ObjectEncoding.Amf0, asyncMode), ObjectEncoding.Amf0); reader = new RtmpPacketReader(new AmfReader(stream, context, asyncMode)); reader.EventReceived += EventReceivedCallback; reader.Disconnected += OnPacketProcessorDisconnected; writer.Disconnected += OnPacketProcessorDisconnected; callbackManager = new TaskCallbackManager <int, object>(); }
public void EstablishThreads(Stream stream) { this._writer = new RtmpPacketWriter(new AmfWriter(stream, this._serializationContext), ObjectEncoding.Amf3); this._reader = new RtmpPacketReader(new AmfReader(stream, this._serializationContext)); this._reader.EventReceived += new EventHandler <EventReceivedEventArgs>(this.EventReceivedCallback); this._reader.Disconnected += new EventHandler <ExceptionalEventArgs>(this.OnPacketProcessorDisconnected); this._writer.Disconnected += new EventHandler <ExceptionalEventArgs>(this.OnPacketProcessorDisconnected); this._writerThread = new Thread(new ThreadStart(this._reader.ReadLoop)) { IsBackground = true }; this._readerThread = new Thread(new ThreadStart(this._writer.WriteLoop)) { IsBackground = true }; this._writerThread.Start(); this._readerThread.Start(); }
public void EstablishThreads(Stream stream) { writer = new RtmpPacketWriter(new AmfWriter(stream, serializationContext), ObjectEncoding.Amf3); reader = new RtmpPacketReader(new AmfReader(stream, serializationContext)); reader.EventReceived += EventReceivedCallback; reader.Disconnected += OnPacketProcessorDisconnected; writer.Disconnected += OnPacketProcessorDisconnected; writerThread = new Thread(reader.ReadLoop) { IsBackground = true }; readerThread = new Thread(writer.WriteLoop) { IsBackground = true }; writerThread.Start(); readerThread.Start(); }
public WebsocketConnect(IWebSocketConnection connection, IO.SerializationContext context, IO.ObjectEncoding encoding) { this.connection = connection; IsPlaying = true; sendPing = connection.SendPing; connectTime = DateTime.UtcNow; connection.OnPong += d => callbackManager.SetResult(BitConverter.ToInt32(d, 0), null); connection.OnClose += () => { OnDisconnected(new ExceptionalEventArgs("Closed")); }; connection.OnError += (e) => { OnDisconnected(new ExceptionalEventArgs(e.Message, e)); }; var stream = new WebsocketStream(connection); writer = new FlvPacketWriter(new IO.AmfWriter(stream, context), encoding); reader = new RtmpPacketReader(new IO.AmfReader(stream, context)); }
public async Task ConnectAsync() { if (hasConnected) { return; } var client = CreateTcpClient(); client.NoDelay = NoDelay; client.ReceiveTimeout = ReceiveTimeout; client.SendTimeout = SendTimeout; client.ExclusiveAddressUse = ExclusiveAddressUse; await client.ConnectAsync(uri.Host, uri.Port); var stream = await GetRtmpStreamAsync(client); var random = new Random(); var randomBytes = new byte[1528]; random.NextBytes(randomBytes); // write c0+c1 var c01 = new Handshake() { Version = 3, Time = (uint)Environment.TickCount, Time2 = 0, Random = randomBytes }; await Handshake.WriteAsync(stream, c01, true); // read s0+s1 var s01 = await Handshake.ReadAsync(stream, true); // write c2 var c2 = s01.Clone(); c2.Time2 = (uint)Environment.TickCount; await Handshake.WriteAsync(stream, c2, false); // read s2 var s2 = await Handshake.ReadAsync(stream, false); // handshake check if (!c01.Random.SequenceEqual(s2.Random) || c01.Time != s2.Time) { throw new ProtocolViolationException(); } writer = new RtmpPacketWriter(new AmfWriter(stream, context), ObjectEncoding.Amf3); reader = new RtmpPacketReader(new AmfReader(stream, context)); reader.EventReceived += EventReceivedCallback; reader.Disconnected += OnPacketProcessorDisconnected; writer.Disconnected += OnPacketProcessorDisconnected; writerThread = new Thread(reader.ReadLoop) { IsBackground = true }; readerThread = new Thread(writer.WriteLoop) { IsBackground = true }; writerThread.Start(); readerThread.Start(); // call `connect` var connectResult = await ConnectInvokeAsync(null, null, uri.ToString()); object cId; if (connectResult.TryGetValue("clientId", out cId)) { ClientId = cId as string; } hasConnected = true; }