// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT

package srtp

import (
	
	
	
	

	
	
)

// Limit the buffer size to 1MB.
const srtpBufferSize = 1000 * 1000

// ReadStreamSRTP handles decryption for a single RTP SSRC.
type ReadStreamSRTP struct {
	mu sync.Mutex

	isClosed chan bool

	session  *SessionSRTP
	ssrc     uint32
	isInited bool

	buffer io.ReadWriteCloser
}

// Used by getOrCreateReadStream.
func newReadStreamSRTP() readStream {
	return &ReadStreamSRTP{}
}

func ( *ReadStreamSRTP) ( streamSession,  uint32) error {
	,  := .(*SessionSRTP)

	.mu.Lock()
	defer .mu.Unlock()

	if ! {
		return errFailedTypeAssertion
	} else if .isInited {
		return errStreamAlreadyInited
	}

	.session = 
	.ssrc = 
	.isInited = true
	.isClosed = make(chan bool)

	// Create a buffer with a 1MB limit
	if .session.bufferFactory != nil {
		.buffer = .session.bufferFactory(packetio.RTPBufferPacket, )
	} else {
		 := packetio.NewBuffer()
		.SetLimitSize(srtpBufferSize)
		.buffer = 
	}

	return nil
}

func ( *ReadStreamSRTP) ( []byte) ( int,  error) {
	,  = .buffer.Write()

	if errors.Is(, packetio.ErrFull) {
		// Silently drop data when the buffer is full.
		return len(), nil
	}

	return , 
}

// Read reads and decrypts full RTP packet from the nextConn.
func ( *ReadStreamSRTP) ( []byte) (int, error) {
	return .buffer.Read()
}

// ReadRTP reads and decrypts full RTP packet and its header from the nextConn.
func ( *ReadStreamSRTP) ( []byte) (int, *rtp.Header, error) {
	,  := .Read()
	if  != nil {
		return 0, nil, 
	}

	 := &rtp.Header{}

	_,  = .Unmarshal([:])
	if  != nil {
		return 0, nil, 
	}

	return , , nil
}

// SetReadDeadline sets the deadline for the Read operation.
// Setting to zero means no deadline.
func ( *ReadStreamSRTP) ( time.Time) error {
	if ,  := .buffer.(interface {
		(time.Time) error
	});  {
		return .()
	}

	return nil
}

// Close removes the ReadStream from the session and cleans up any associated state.
func ( *ReadStreamSRTP) () error {
	.mu.Lock()
	defer .mu.Unlock()

	if !.isInited {
		return errStreamNotInited
	}

	select {
	case <-.isClosed:
		return errStreamAlreadyClosed
	default:
		 := .buffer.Close()
		if  != nil {
			return 
		}

		.session.removeReadStream(.ssrc)

		return nil
	}
}

// GetSSRC returns the SSRC we are demuxing for.
func ( *ReadStreamSRTP) () uint32 {
	return .ssrc
}

// WriteStreamSRTP is stream for a single Session that is used to encrypt RTP.
type WriteStreamSRTP struct {
	session *SessionSRTP
}

// WriteRTP encrypts a RTP packet and writes to the connection.
func ( *WriteStreamSRTP) ( *rtp.Header,  []byte) (int, error) {
	return .session.writeRTP(, )
}

// Write encrypts and writes a full RTP packets to the nextConn.
func ( *WriteStreamSRTP) ( []byte) (int, error) {
	return .session.write()
}

// SetWriteDeadline sets the deadline for the Write operation.
// Setting to zero means no deadline.
func ( *WriteStreamSRTP) ( time.Time) error {
	return .session.setWriteDeadline()
}