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

package srtp

import (
	
	
	
	

	
	
)

// Limit the buffer size to 100KB.
const srtcpBufferSize = 100 * 1000

// ReadStreamSRTCP handles decryption for a single RTCP SSRC.
type ReadStreamSRTCP struct {
	mu sync.Mutex

	isClosed chan bool

	session  *SessionSRTCP
	ssrc     uint32
	isInited bool

	buffer io.ReadWriteCloser
}

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

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

	return , 
}

// Used by getOrCreateReadStream.
func newReadStreamSRTCP() readStream {
	return &ReadStreamSRTCP{}
}

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

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

	return , , nil
}

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

// SetReadDeadline sets the deadline for the Read operation.
// Setting to zero means no deadline.
func ( *ReadStreamSRTCP) ( 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 ( *ReadStreamSRTCP) () 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
	}
}

func ( *ReadStreamSRTCP) ( streamSession,  uint32) error {
	,  := .(*SessionSRTCP)

	.mu.Lock()
	defer .mu.Unlock()
	if ! {
		return errFailedTypeAssertion
	} else if .isInited {
		return errStreamAlreadyInited
	}

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

	if .session.bufferFactory != nil {
		.buffer = .session.bufferFactory(packetio.RTCPBufferPacket, )
	} else {
		// Create a buffer and limit it to 100KB
		 := packetio.NewBuffer()
		.SetLimitSize(srtcpBufferSize)
		.buffer = 
	}

	return nil
}

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

// WriteStreamSRTCP is stream for a single Session that is used to encrypt RTCP.
type WriteStreamSRTCP struct {
	session *SessionSRTCP
}

// WriteRTCP encrypts a RTCP header and its payload to the nextConn.
func ( *WriteStreamSRTCP) ( *rtcp.Header,  []byte) (int, error) {
	,  := .Marshal()
	if  != nil {
		return 0, 
	}

	return .session.write(append(, ...))
}

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

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