summaryrefslogtreecommitdiffstats
path: root/kopete/protocols/jabber/jingle/libjingle/talk/third_party/mediastreamer/mssync.c
blob: 0a36f49039d1882fdaec04f2c1b1c84d0a2f7c99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
  The mediastreamer library aims at providing modular media processing and I/O
	for linphone, but also for any telephony application.
  Copyright (C) 2001  Simon MORLAT simon.morlat@linphone.org
  										
  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library 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
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1307  USA
*/

#include "mssync.h"
#include <errno.h>

/* TODO:
	-define an uninit function that free the mutex
*/

/**
 * function_name:ms_sync_get_bytes_per_tick
 * @sync:  A #MSSync object.
 *
 * Returns the number of bytes per tick. This is a usefull information for sources, so
 * that they can know how much data they must deliver each time they are called.
 *
 */

/* private */
void ms_sync_init(MSSync *sync)
{	
	sync->klass=NULL;  
	sync->lock=g_mutex_new();
	sync->thread_cond=g_cond_new();
	sync->stop_cond=g_cond_new();
	sync->attached_filters=NULL;
	sync->execution_list=NULL;
	sync->filters=0;
	sync->run=0;
	sync->flags=0;
	sync->samples_per_tick=0;
	sync->ticks=0;
	sync->time=0;
	sync->thread=NULL;
}

void ms_sync_class_init(MSSyncClass *klass)
{
	klass->max_filters=0;
	klass->synchronize=NULL;
	klass->attach=ms_sync_attach_generic;
	klass->detach=ms_sync_detach_generic;
	klass->destroy=NULL;
}

/* public*/


/**
 * ms_sync_attach:
 * @sync:  A #MSSync object.
 * @f:  A #MSFilter object.
 *
 * Attach a chain of filters to a synchronisation source @sync. Filter @f must be the first filter of the processing chain.
 *  In order to be run, each chain of filter must be attached to a synchronisation source, that will be responsible for scheduling
 *  the processing. Multiple chains can be attached to a single synchronisation.
 *
 * Returns: 0 if successfull, a negative value reprensenting the errno.h error.
 */
int ms_sync_attach(MSSync *sync,MSFilter *f)
{
	gint err;
	ms_sync_lock(sync);
	err=sync->klass->attach(sync,f);
	ms_sync_update(sync);
	ms_sync_unlock(sync);
	return(err);
}

int ms_sync_attach_generic(MSSync *sync,MSFilter *f)
{
	int i;
	/* //printf("attr: %i\n",f->klass->attributes); */
	g_return_val_if_fail(f->klass->attributes & FILTER_IS_SOURCE,-EINVAL);
	g_return_val_if_fail(sync->attached_filters!=NULL,-EFAULT);
	

	/* find a free place to attach*/
	for (i=0;i<sync->klass->max_filters;i++)
	{
		if (sync->attached_filters[i]==NULL)
		{
			sync->attached_filters[i]=f;
			sync->filters++;
			ms_trace("Filter succesfully attached to sync.");
			return 0;
		}
	}
	g_warning("No more link on sync !");
	return(-EMLINK);
}

/**
 * ms_sync_detach:
 * @sync:  A #MSSync object.
 * @f:  A #MSFilter object.
 *
 * Dettach a chain of filters to a synchronisation source. Filter @f must be the first filter of the processing chain.
 * The processing chain will no more be executed.
 *
 * Returns: 0 if successfull, a negative value reprensenting the errno.h error.
 */
int ms_sync_detach(MSSync *sync,MSFilter *f)
{	
	gint err;
	ms_sync_lock(sync);
	err=sync->klass->detach(sync,f);
	ms_sync_update(sync);
	ms_sync_unlock(sync);
	return(err);
}

int ms_sync_detach_generic(MSSync *sync,MSFilter *f)
{
	int i;
	g_return_val_if_fail(f->klass->attributes & FILTER_IS_SOURCE,-EINVAL);
	g_return_val_if_fail(sync->attached_filters!=NULL,-EFAULT);
	for (i=0;i<sync->filters;i++)
	{
		if (sync->attached_filters[i]==f)
		{
			sync->attached_filters[i]=NULL;
			sync->filters--;
			return 0;
		}
	}
	return(-EMLINK);
}

void ms_sync_set_samples_per_tick(MSSync *sync,gint size)
{
	if (sync->samples_per_tick==0)
	{
		sync->samples_per_tick=size;
		g_cond_signal(sync->thread_cond);
	}
	else sync->samples_per_tick=size;
}

/* call the setup func of each filter attached to the graph */
void ms_sync_setup(MSSync *sync)
{
	GList *elem=sync->execution_list;
	MSFilter *f;
	while(elem!=NULL){
		f=(MSFilter*)elem->data;
		if (f->klass->setup!=NULL){
			f->klass->setup(f,sync);
		}
		elem=g_list_next(elem);
	}
}

/* call the unsetup func of each filter attached to the graph */
void ms_sync_unsetup(MSSync *sync)
{
	GList *elem=sync->execution_list;
	MSFilter *f;
	while(elem!=NULL){
		f=(MSFilter*)elem->data;
		if (f->klass->unsetup!=NULL){
			f->klass->unsetup(f,sync);
		}
		elem=g_list_next(elem);
	}
}


int ms_sync_uninit(MSSync *sync)
{
	g_mutex_free(sync->lock);
	g_cond_free(sync->thread_cond);
	g_cond_free(sync->stop_cond);
}