<commit_before>
#include "MediaRx.h"
#include "MediaPortManager.h"

extern "C" {
#include "util/sdp-manager.h"
}

using namespace media;

MediaRx::MediaRx(MediaPort* mediaPort, const char* sdp, int max_delay,
							CodecType codec_type)
: Media()
{
	_sdp = sdp;
	_mediaPort = mediaPort;
	_max_delay = max_delay;
	_codec_type = codec_type;
	_mutex = new Lock();
	_freeLock = new Lock();
	LOG_TAG = "media-rx";
}

MediaRx::~MediaRx()
{
	delete _mutex;
	delete _freeLock;
}

bool
MediaRx::getReceive()
{
	bool ret;
	_mutex->lock();
	ret = _receive;
	_mutex->unlock();

	return ret;
}

void
MediaRx::setReceive(bool receive)
{
	_mutex->lock();
	_receive = receive;
	_mutex->unlock();
}

int
MediaRx::openFormatContext(AVFormatContext **c)
{
	AVFormatContext *pFormatCtx = NULL;
	AVFormatParameters params, *ap = &params;
	URLContext *urlContext;
	int ret;
	char buf[256];

	media_log(MEDIA_LOG_DEBUG, LOG_TAG, "sdp: %s", _sdp);

	urlContext = _mediaPort->getConnection();

	for(;;) {
		if (!this->getReceive())
			return -1;

		pFormatCtx = avformat_alloc_context();
		pFormatCtx->max_delay = _max_delay * 1000;
		ap->prealloced_context = 1;

		// Open sdp
		if ((ret = av_open_input_sdp(&pFormatCtx, _sdp, ap, urlContext)) != 0 ) {
			av_strerror(ret, buf, sizeof(buf));
			media_log(MEDIA_LOG_ERROR, LOG_TAG, "Couldn't process sdp: %s", buf);
			return ret;
		}

		// Retrieve stream information
		if ((ret = av_find_stream_info(pFormatCtx)) < 0) {
			av_strerror(ret, buf, sizeof(buf));
			media_log(MEDIA_LOG_WARN, LOG_TAG, "Couldn't find stream information: %s", buf);
			_mediaPort->closeContext(pFormatCtx);

		} else
			break;
	}
	*c = pFormatCtx;

	return 0;
}


int
MediaRx::start()
{
	AVCodec *pDecodec = NULL;

	AVPacket avpkt;
	uint8_t *avpkt_data_init;

	int i, ret;
	int64_t rx_time;

	_freeLock->lock();
	this->setReceive(true);
	if ((ret = MediaRx::openFormatContext(&_pFormatCtx)) < 0)
		goto end;
	media_log(MEDIA_LOG_INFO, LOG_TAG, "max_delay: %d ms", _pFormatCtx->max_delay/1000);

	// Find the first stream
	_stream = -1;
	for (i = 0; i < _pFormatCtx->nb_streams; i++) {
		if (_pFormatCtx->streams[i]->codec->codec_type == _codec_type) {
			_stream = i;
			break;
		}
	}
	if (_stream == -1) {
		media_log(MEDIA_LOG_ERROR, LOG_TAG, "Didn't find a stream");
		ret = -4;
		goto end;
	}

	// Get a pointer to the codec context for the stream
	_pDecodecCtx = _pFormatCtx->streams[_stream]->codec;

	// Find the decoder for the stream
	pDecodec = avcodec_find_decoder(_pDecodecCtx->codec_id);
	if (pDecodec == NULL) {
		media_log(MEDIA_LOG_ERROR, LOG_TAG, "Unsupported codec");
		ret = -5; // Codec not found
		goto end;
	}<commit_msg>Clean MediaRx.cpp.<commit_after>
#include "MediaRx.h"
#include "MediaPortManager.h"

extern "C" {
#include "util/sdp-manager.h"
}

using namespace media;

MediaRx::MediaRx(MediaPort* mediaPort, const char* sdp, int max_delay,
							CodecType codec_type)
: Media()
{
	_sdp = sdp;
	_mediaPort = mediaPort;
	_max_delay = max_delay;
	_codec_type = codec_type;
	_mutex = new Lock();
	_freeLock = new Lock();
	LOG_TAG = "media-rx";
}

MediaRx::~MediaRx()
{
	delete _mutex;
	delete _freeLock;
}

bool
MediaRx::getReceive()
{
	bool ret;
	_mutex->lock();
	ret = _receive;
	_mutex->unlock();

	return ret;
}

void
MediaRx::setReceive(bool receive)
{
	_mutex->lock();
	_receive = receive;
	_mutex->unlock();
}

int
MediaRx::openFormatContext(AVFormatContext **c)
{
	AVFormatContext *pFormatCtx = NULL;
	AVFormatParameters params, *ap = &params;
	URLContext *urlContext;
	int ret;
	char buf[256];

	media_log(MEDIA_LOG_DEBUG, LOG_TAG, "sdp: %s", _sdp);

	urlContext = _mediaPort->getConnection();

	for(;;) {
		if (!this->getReceive())
			return -1;

		pFormatCtx = avformat_alloc_context();
		pFormatCtx->max_delay = _max_delay * 1000;
		ap->prealloced_context = 1;

		// Open sdp
		ret = av_open_input_sdp(&pFormatCtx, _sdp, ap, urlContext);
		if (ret != 0 ) {
			av_strerror(ret, buf, sizeof(buf));
			media_log(MEDIA_LOG_ERROR, LOG_TAG,
						"Couldn't process sdp: %s", buf);
			return ret;
		}

		// Retrieve stream information
		if ((ret = av_find_stream_info(pFormatCtx)) < 0) {
			av_strerror(ret, buf, sizeof(buf));
			media_log(MEDIA_LOG_WARN, LOG_TAG,
				"Couldn't find stream information: %s", buf);
			_mediaPort->closeContext(pFormatCtx);

		} else
			break;
	}
	*c = pFormatCtx;

	return 0;
}


int
MediaRx::start()
{
	AVCodec *pDecodec = NULL;

	AVPacket avpkt;
	uint8_t *avpkt_data_init;

	int i, ret;
	int64_t rx_time;

	_freeLock->lock();
	this->setReceive(true);
	if ((ret = MediaRx::openFormatContext(&_pFormatCtx)) < 0)
		goto end;

	// Find the first stream
	_stream = -1;
	for (i = 0; i < _pFormatCtx->nb_streams; i++) {
		if (_pFormatCtx->streams[i]->codec->codec_type == _codec_type) {
			_stream = i;
			break;
		}
	}
	if (_stream == -1) {
		media_log(MEDIA_LOG_ERROR, LOG_TAG, "Didn't find a stream");
		ret = -4;
		goto end;
	}

	// Get a pointer to the codec context for the stream
	_pDecodecCtx = _pFormatCtx->streams[_stream]->codec;

	// Find the decoder for the stream
	pDecodec = avcodec_find_decoder(_pDecodecCtx->codec_id);
	if (pDecodec == NULL) {
		media_log(MEDIA_LOG_ERROR, LOG_TAG, "Unsupported codec");
		ret = -5; // Codec not found
		goto end;
	}
{'fraction_non_alphanumeric': 0.14493307839388145, 'fraction_numerical': 0.008795411089866157, 'mean_word_length': 4.252008032128514, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '878329', 'n_tokens_mistral': 2231, 'n_tokens_neox': 2074, 'n_words': 464}