<commit_before>/*
 * Kurento Android Media: Android Media Library based on FFmpeg.
 * Copyright (C) 2011  Tikal Technologies
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * 
 * @author <NAME>
 * 
 */

#include "video-rx.h"
#include <util/log.h>
#include <util/utils.h>
#include <init-media.h>
#include <socket-manager.h>

#include <pthread.h>

#include "libswscale/swscale.h"

#include "sdp-manager.h"

#include <util/utils.h>

static char buf[256];
static char* LOG_TAG = "media-video-rx";

static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static int receive = 0;
static int sws_flags = SWS_BICUBIC;

int
stop_video_rx() {
	pthread_mutex_lock(&mutex);
	receive = 0;
	set_interrrupt_cb(1);
	pthread_mutex_unlock(&mutex);
	return 0;
}

int
start_video_rx(const char* sdp, int maxDelay, FrameManager *frame_manager) {
	AVFormatContext *pFormatCtx = NULL;
	AVFormatParameters params, *ap = &params;
	AVCodecContext *pDecodecCtxVideo = NULL;
	AVCodec *pDecodecVideo = NULL;
	AVFrame *pFrame = NULL;
	DecodedFrame *decoded_frame;

	AVPacket avpkt;
	uint8_t *avpkt_data_init;

	int i, ret, videoStream, buffer_nbytes, picture_nbytes, len, got_picture;
	int current_width, current_height;
	int n_packet;
	int64_t rx_time;

	struct SwsContext *img_convert_ctx;

//	struct timespec start, end, t2;
//	uint64_t time;
//	uint64_t total_time = 0;

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

	if ((ret = init_media()) != 0) {
		media_log(MEDIA_LOG_ERROR, LOG_TAG, "Couldn't init media");
		goto end;
	}
	set_interrrupt_cb(0);

	pthread_mutex_lock(&mutex);
	receive = 1;
	pthread_mutex_unlock(&mutex);
	for(;;) {
		pthread_mutex_lock(&mutex);
		if (!receive) {
			pthread_mutex_unlock(&mutex);
			goto end;
		}
		pthread_mutex_unlock(&mutex);

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

		// Open video file
		if ((ret = av_open_input_sdp(&pFormatCtx, sdp, ap)) != 0 ) {
			av_strerror(ret, buf, sizeof(buf));
			media_log(MEDIA_LOG_ERROR, LOG_TAG, "Couldn't process sdp: %s", buf);
			ret = -2;
			goto end;
		}

		// 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);
			close_context(pFormatCtx);
		} else
			break;
	}

	media_log(MEDIA_LOG_INFO, LOG_TAG, "max_delay: %d ms", pFormatCtx->max_delay/1000);

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

	// Get a pointer to the codec context for the video stream
	pDecodecCtxVideo = pFormatCtx->streams[videoStream]->codec;

	// Find the decoder for the video stream
	pDecodecVideo = avcodec_find_decoder(pDecodecCtxVideo->codec_id);
	if (pDecodecVideo == NULL) {
		media_log(MEDIA_LOG_ERROR, LOG_TAG, "Unsupported video codec");
		ret = -5; // Codec not found<commit_msg>Log stream information in video-rx.<commit_after>/*
 * Kurento Android Media: Android Media Library based on FFmpeg.
 * Copyright (C) 2011  Tikal Technologies
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * 
 * @author <NAME>
 * 
 */

#include "video-rx.h"
#include <util/log.h>
#include <util/utils.h>
#include <init-media.h>
#include <socket-manager.h>

#include <pthread.h>

#include "libswscale/swscale.h"

#include "sdp-manager.h"

#include <util/utils.h>

static char buf[256];
static char* LOG_TAG = "media-video-rx";

static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static int receive = 0;
static int sws_flags = SWS_BICUBIC;

int
stop_video_rx() {
	pthread_mutex_lock(&mutex);
	receive = 0;
	set_interrrupt_cb(1);
	pthread_mutex_unlock(&mutex);
	return 0;
}

int
start_video_rx(const char* sdp, int maxDelay, FrameManager *frame_manager) {
	AVFormatContext *pFormatCtx = NULL;
	AVFormatParameters params, *ap = &params;
	AVCodecContext *pDecodecCtxVideo = NULL;
	AVCodec *pDecodecVideo = NULL;
	AVFrame *pFrame = NULL;
	DecodedFrame *decoded_frame;

	AVPacket avpkt;
	uint8_t *avpkt_data_init;

	int i, ret, videoStream, buffer_nbytes, picture_nbytes, len, got_picture;
	int current_width, current_height;
	int n_packet;
	int64_t rx_time;

	struct SwsContext *img_convert_ctx;

//	struct timespec start, end, t2;
//	uint64_t time;
//	uint64_t total_time = 0;

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

	if ((ret = init_media()) != 0) {
		media_log(MEDIA_LOG_ERROR, LOG_TAG, "Couldn't init media");
		goto end;
	}
	set_interrrupt_cb(0);

	pthread_mutex_lock(&mutex);
	receive = 1;
	pthread_mutex_unlock(&mutex);
	for(;;) {
		pthread_mutex_lock(&mutex);
		if (!receive) {
			pthread_mutex_unlock(&mutex);
			goto end;
		}
		pthread_mutex_unlock(&mutex);

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

		// Open video file
		if ((ret = av_open_input_sdp(&pFormatCtx, sdp, ap)) != 0 ) {
			av_strerror(ret, buf, sizeof(buf));
			media_log(MEDIA_LOG_ERROR, LOG_TAG, "Couldn't process sdp: %s", buf);
			ret = -2;
			goto end;
		}

		// 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);
			close_context(pFormatCtx);
		} else
			break;
	}

	media_log(MEDIA_LOG_INFO, LOG_TAG, "max_delay: %d ms", pFormatCtx->max_delay/1000);

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

	// Get a pointer to the codec context for the video stream
	pDecodecCtxVideo = pFormatCtx->streams[videoStream]->codec;

media_log(MEDIA_LOG_DEBUG, LOG_TAG, "time_base.num: %d", pFormatCtx->streams[videoStream]->time_base.num);
media_log(MEDIA_LOG_DEBUG, LOG_TAG, "time_base.den: %d", pFormatCtx->streams[videoStream]->time_base.den);
media_log(MEDIA_LOG_DEBUG, LOG_TAG, "start_time: %lld", pFormatCtx->streams[videoStream]->start_time);
media_log(MEDIA_LOG_DEBUG, LOG_TAG, "duration: %lld", pFormatCtx->streams[videoStream]->duration);
media_log(MEDIA_LOG_DEBUG, LOG_TAG, "r_frame_rate.num: %d", pFormatCtx->streams[videoStream]->r_frame_rate.num);
media_log(MEDIA_LOG_DEBUG, LOG_TAG, "r_frame_rate.den: %d", pFormatCtx->streams[videoStream]->r_frame_rate.den);
media_log(MEDIA_LOG_DEBUG, LOG_TAG, "avg_frame_rate.num: %d", pFormatCtx->streams[videoStream]->avg_frame_rate.num);
media_log(MEDIA_LOG_DEBUG, LOG_TAG, "avg_frame_rate.den: %d", pFormatCtx->streams[videoStream]->avg_frame_rate.den);
media_log(MEDIA_LOG_DEBUG, LOG_TAG, "sample_aspect_ratio.den: %d", pFormatCtx->streams[videoStream]->sample_aspect_ratio.den);
media_log(MEDIA_LOG_DEBUG, LOG_TAG, "sample_aspect_ratio.den: %d", pFormatCtx->streams[videoStream]->sample_aspect_ratio.den);


	// Find the decoder for the video stream
	pDecodecVideo = avcodec_find_decoder(pDecodecCtxVideo->codec_id);
	if (pDecodecVideo == NULL) {
		media_log(MEDIA_LOG_ERROR, LOG_TAG, "Unsupported video codec");
		ret = -5; // Codec not found
{'fraction_non_alphanumeric': 0.12403282532239156, 'fraction_numerical': 0.009613130128956624, 'mean_word_length': 4.839151266255989, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4850443', 'n_tokens_mistral': 3295, 'n_tokens_neox': 3135, 'n_words': 779}