<commit_before>/*
 * Copyright 2008 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifdef WIN32
#pragma warning(disable: 4127)
#endif
#include <apr_ring.h> 
#include "mpf_context.h"
#include "mpf_termination.h"
#include "mpf_stream.h"
#include "mpf_bridge.h"
#include "mpf_multiplier.h"
#include "mpf_mixer.h"
#include "apt_log.h"

/** Item of the association matrix */
typedef struct {
	unsigned char on;
} matrix_item_t;

/** Item of the association matrix header */
typedef struct {
	mpf_termination_t *termination;
	unsigned char      tx_count;
	unsigned char      rx_count;
} header_item_t;

/** Media processing context */
struct mpf_context_t {
	/** Ring entry */
	APR_RING_ENTRY(mpf_context_t) link;
	/** Back pointer to the context factory */
	mpf_context_factory_t        *factory;
	/** Pool to allocate memory from */
	apr_pool_t                   *pool;
	/** External object */
	void                         *obj;

	/** Max number of terminations in the context */
	apr_size_t                    capacity;
	/** Current number of terminations in the context */
	apr_size_t                    count;
	/** Header of the association matrix */
	header_item_t                *header;
	/** Association matrix, which represents the topology */
	matrix_item_t                **matrix;

	/** Array of media processing objects constructed while 
	applying topology based on association matrix */
	apr_array_header_t           *mpf_objects;
};

/** Factory of media contexts */
struct mpf_context_factory_t {
	/** Ring head */
	APR_RING_HEAD(mpf_context_head_t, mpf_context_t) head;
};


static APR_INLINE apt_bool_t stream_direction_compatibility_check(mpf_termination_t *termination1, mpf_termination_t *termination2);
static mpf_object_t* mpf_context_bridge_create(mpf_context_t *context, apr_size_t i);
static mpf_object_t* mpf_context_multiplier_create(mpf_context_t *context, apr_size_t i);
static mpf_object_t* mpf_context_mixer_create(mpf_context_t *context, apr_size_t j);


MPF_DECLARE(mpf_context_factory_t*) mpf_context_factory_create(apr_pool_t *pool)
{
	mpf_context_factory_t *factory = apr_palloc(pool, sizeof(mpf_context_factory_t));
	APR_RING_INIT(&factory->head, mpf_context_t, link);
	return factory;
}

MPF_DECLARE(void) mpf_context_factory_destroy(mpf_context_factory_t *factory)
{
	mpf_context_t *context;
	while(!APR_RING_EMPTY(&factory->head, mpf_context_t, link)) {
		context = APR_RING_FIRST(&factory->head);
		mpf_context_destroy(context);
		APR_RING_REMOVE(context, link);
	}
}

MPF_DECLARE(apt_bool_t) mpf_context_factory_process(mpf_context_factory_t *factory)
{
	mpf_context_t *context;
	for(context = APR_RING_FIRST(&factory->head);
			context != APR_RING_SENTINEL(&factory->head, mpf_context_t, link);
				context = APR_RING_NEXT(context, link)) {
		
		mpf_context_process(context);
	}

	return TRUE;
}

 
MPF_DECLARE(mpf_context_t*) mpf_context_create(
								mpf_context_factory_t *factory,
								void *obj,
								apr_size_t max_termination_count,
								apr_pool_t *pool)
{
	apr_size_t i,j;
	matrix_item_t *matrix_item;
	header_item_t *header_item;
	mpf_context_t *context = apr_palloc(pool,sizeof(mpf_context_t));
	context->factory = factory;
	context->obj = obj;
	context->pool = pool;
	context->capacity = max_termination_count;
	context->count = 0;
	context->mpf_objects = apr_array_make(pool,1,sizeof(mpf_object_t*));
	context->header = apr_palloc(pool,context->capacity * sizeof(header_item_t));
	context->matrix = apr_palloc(pool,context->capacity * sizeof(matrix_item_t*));
	for(i=0; i<context->capacity; i++) {
		header_item = &context->header[i];
		header_item->termination = NULL;
		header_item->tx_count = 0;
		header_item->rx_count = 0;
		context->matrix[i] = apr_palloc(pool,context->capacity * sizeof(matrix_item_t));
		for(j=0; j<context->capacity; j++) {
			matrix_item = &context->matrix[i][j];
			matrix_item->on = 0;
		}
	}

	return context;
}

MPF_DECLARE(apt_bool_t) mpf_context_destroy(mpf_context_t *context)
{
	apr_size_t i;
	mpf_termination_t *termination;
	for(i=0; i<context->capacity; i++){
		termination = context->header[i].termination;
		if(termination) {
			mpf_context_termination_subtract(context,termination);
			if(termination->audio_stream) {
				mpf_audio_stream_destroy(termination->audio_stream);
			}
		}
	}
	return TRUE;
}

MPF_DECLARE(void*) mpf_context_object_get(mpf_context_t *context)
{
	return context->obj;
}

MPF_DECLARE(apt_bool_t) mpf_context_termination_add(mpf_context_t *context, mpf_termination_t *termination)
{
	apr_size_t i;<commit_msg>Do not destroy audio streams while destroying media context, which still contains terminations. Instead just subtract terminations.<commit_after>/*
 * Copyright 2008 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifdef WIN32
#pragma warning(disable: 4127)
#endif
#include <apr_ring.h> 
#include "mpf_context.h"
#include "mpf_termination.h"
#include "mpf_stream.h"
#include "mpf_bridge.h"
#include "mpf_multiplier.h"
#include "mpf_mixer.h"
#include "apt_log.h"

/** Item of the association matrix */
typedef struct {
	unsigned char on;
} matrix_item_t;

/** Item of the association matrix header */
typedef struct {
	mpf_termination_t *termination;
	unsigned char      tx_count;
	unsigned char      rx_count;
} header_item_t;

/** Media processing context */
struct mpf_context_t {
	/** Ring entry */
	APR_RING_ENTRY(mpf_context_t) link;
	/** Back pointer to the context factory */
	mpf_context_factory_t        *factory;
	/** Pool to allocate memory from */
	apr_pool_t                   *pool;
	/** External object */
	void                         *obj;

	/** Max number of terminations in the context */
	apr_size_t                    capacity;
	/** Current number of terminations in the context */
	apr_size_t                    count;
	/** Header of the association matrix */
	header_item_t                *header;
	/** Association matrix, which represents the topology */
	matrix_item_t                **matrix;

	/** Array of media processing objects constructed while 
	applying topology based on association matrix */
	apr_array_header_t           *mpf_objects;
};

/** Factory of media contexts */
struct mpf_context_factory_t {
	/** Ring head */
	APR_RING_HEAD(mpf_context_head_t, mpf_context_t) head;
};


static APR_INLINE apt_bool_t stream_direction_compatibility_check(mpf_termination_t *termination1, mpf_termination_t *termination2);
static mpf_object_t* mpf_context_bridge_create(mpf_context_t *context, apr_size_t i);
static mpf_object_t* mpf_context_multiplier_create(mpf_context_t *context, apr_size_t i);
static mpf_object_t* mpf_context_mixer_create(mpf_context_t *context, apr_size_t j);


MPF_DECLARE(mpf_context_factory_t*) mpf_context_factory_create(apr_pool_t *pool)
{
	mpf_context_factory_t *factory = apr_palloc(pool, sizeof(mpf_context_factory_t));
	APR_RING_INIT(&factory->head, mpf_context_t, link);
	return factory;
}

MPF_DECLARE(void) mpf_context_factory_destroy(mpf_context_factory_t *factory)
{
	mpf_context_t *context;
	while(!APR_RING_EMPTY(&factory->head, mpf_context_t, link)) {
		context = APR_RING_FIRST(&factory->head);
		mpf_context_destroy(context);
		APR_RING_REMOVE(context, link);
	}
}

MPF_DECLARE(apt_bool_t) mpf_context_factory_process(mpf_context_factory_t *factory)
{
	mpf_context_t *context;
	for(context = APR_RING_FIRST(&factory->head);
			context != APR_RING_SENTINEL(&factory->head, mpf_context_t, link);
				context = APR_RING_NEXT(context, link)) {
		
		mpf_context_process(context);
	}

	return TRUE;
}

 
MPF_DECLARE(mpf_context_t*) mpf_context_create(
								mpf_context_factory_t *factory,
								void *obj,
								apr_size_t max_termination_count,
								apr_pool_t *pool)
{
	apr_size_t i,j;
	matrix_item_t *matrix_item;
	header_item_t *header_item;
	mpf_context_t *context = apr_palloc(pool,sizeof(mpf_context_t));
	context->factory = factory;
	context->obj = obj;
	context->pool = pool;
	context->capacity = max_termination_count;
	context->count = 0;
	context->mpf_objects = apr_array_make(pool,1,sizeof(mpf_object_t*));
	context->header = apr_palloc(pool,context->capacity * sizeof(header_item_t));
	context->matrix = apr_palloc(pool,context->capacity * sizeof(matrix_item_t*));
	for(i=0; i<context->capacity; i++) {
		header_item = &context->header[i];
		header_item->termination = NULL;
		header_item->tx_count = 0;
		header_item->rx_count = 0;
		context->matrix[i] = apr_palloc(pool,context->capacity * sizeof(matrix_item_t));
		for(j=0; j<context->capacity; j++) {
			matrix_item = &context->matrix[i][j];
			matrix_item->on = 0;
		}
	}

	return context;
}

MPF_DECLARE(apt_bool_t) mpf_context_destroy(mpf_context_t *context)
{
	apr_size_t i;
	mpf_termination_t *termination;
	for(i=0; i<context->capacity; i++){
		termination = context->header[i].termination;
		if(termination) {
			mpf_context_termination_subtract(context,termination);
			mpf_termination_subtract(termination);
		}
	}
	return TRUE;
}

MPF_DECLARE(void*) mpf_context_object_get(mpf_context_t *context)
{
	return context->obj;
}

MPF_DECLARE(apt_bool_t) mpf_context_termination_add(mpf_context_t *context, mpf_termination_t *termination)
{
	apr_size_t i;
{'fraction_non_alphanumeric': 0.10535100638193422, 'fraction_numerical': 0.00471281296023564, 'mean_word_length': 4.758055398530243, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 65, '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': '3070957', 'n_tokens_mistral': 3740, 'n_tokens_neox': 3498, 'n_words': 766}