/// <summary> /// Adds headers to provided data. /// </summary> /// <param name="data">The data to add headers to.</param> /// <returns>The data with added headers.</returns> public byte[] AddHeaders(byte[] data) { var result = new List <byte>(); // Header byte result.Add(0x03); // Timestamp long timediff = Utilities.CurrentTimeMillis() - _startTime; result.Add((byte)((timediff & 0xFF0000) >> 16)); result.Add((byte)((timediff & 0x00FF00) >> 8)); result.Add((byte)(timediff & 0x0000FF)); // Content type result.Add(0x11); // Source ID result.Add(0x00); result.Add(0x00); result.Add(0x00); result.Add(0x00); // Add body for (int i = 0; i < data.Length; ++i) { result.Add(data[i]); if (i % 128 == 127 && i < data.Length - 1) { result.Add(0xC3); } } return(result.ToArray()); }
/// <summary> /// Executes a full RTMP handshake /// </summary> private void doHandshake() { BinaryWriter binWriter = new BinaryWriter(output); // C0 byte C0 = 0x03; binWriter.Write(C0); // C1 long timestampC1 = Utilities.CurrentTimeMillis(); byte[] randC1 = new byte[1528]; rand.NextBytes(randC1); binWriter.Write((int)timestampC1); binWriter.Write(0); binWriter.Write(randC1, 0, 1528); binWriter.Flush(); // S0 byte S0 = (byte)input.ReadByte(); if (S0 != 0x03) { throw new Exception("Server returned incorrect version in handshake: " + S0); } // S1 byte[] S1 = new byte[1536]; input.Read(S1, 0, 1536); // C2 long timestampC2 = Utilities.CurrentTimeMillis(); binWriter.Write(S1, 0, 4); binWriter.Write((int)timestampC1); binWriter.Write(S1, 8, 1528); binWriter.Flush(); // S2 byte[] S2 = new byte[1536]; input.Read(S1, 0, 1536); // Validate handshake bool valid = true; for (int i = 8; i < 1536; i++) { if (randC1[i - 8] != S2[i]) { valid = false; break; } } if (!valid) { throw new Exception("Handshake was not valid."); } }