public int Write(byte[] buffer, int offset) { if (Session.State != MediaSessionState.Authenticated) { throw new InvalidOperationException("Connection has been closed."); } lock (Session.VoiceLock) { if (_nextTick == -1) { _nextTick = Environment.TickCount; } else { long distance = _nextTick - Environment.TickCount; if (distance > 0) { Thread.Sleep((int)distance); } } byte[] opusFrame = new byte[OpusEncoder.FrameBytes]; int frameSize = _encoder.EncodeFrame(buffer, offset, opusFrame, 0); byte[] packet = new RTPPacketHeader() { // Version = 0x80, Type = DiscordMediaSession.SupportedCodecs["opus"].PayloadType, Sequence = Session.Sequence, Timestamp = Session.Timestamp, SSRC = Session.SSRC.Audio }.Write(Session.SecretKey, opusFrame, 0, frameSize); Session.UdpClient.Send(packet, packet.Length); _nextTick += OpusEncoder.TimeBetweenFrames; Session.Sequence++; Session.Timestamp += OpusEncoder.FrameSamplesPerChannel; } return(offset + OpusEncoder.FrameBytes); }
public int Write(byte[] buffer, int offset) { if (_client.State < MediaConnectionState.Ready) { throw new InvalidOperationException("Client is not currently connected"); } lock (_voiceLock) { if (_nextTick == -1) { _nextTick = Environment.TickCount; } else { long distance = _nextTick - Environment.TickCount; if (distance > 0) { Thread.Sleep((int)distance); } } byte[] opusFrame = new byte[OpusConverter.FrameBytes]; int frameSize = _encoder.EncodeFrame(buffer, offset, opusFrame, 0); byte[] packet = new RTPPacketHeader() { Type = DiscordMediaConnection.SupportedCodecs["opus"].PayloadType, Sequence = _sequence, Timestamp = _timestamp, SSRC = _client.Connection.SSRC.Audio }.Write(_client.Connection.SecretKey, opusFrame, 0, frameSize); _client.Connection.UdpClient.Send(packet, packet.Length); _nextTick += OpusConverter.TimeBetweenFrames; _sequence++; _timestamp += OpusConverter.FrameSamplesPerChannel; } return(offset + OpusConverter.FrameBytes); }