2
mediastreamer2 library - modular sound and video processing and streaming
3
Copyright (C) 2010 Simon MORLAT (simon.morlat@linphone.org)
5
This program is free software; you can redistribute it and/or
6
modify it under the terms of the GNU General Public License
7
as published by the Free Software Foundation; either version 2
8
of the License, or (at your option) any later version.
10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
GNU General Public License for more details.
15
You should have received a copy of the GNU General Public License
16
along with this program; if not, write to the Free Software
17
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20
#include "mediastreamer2/mseventqueue.h"
21
#include "mediastreamer2/msfilter.h"
23
#ifndef MS_EVENT_BUF_SIZE
24
#define MS_EVENT_BUF_SIZE 8192
28
ms_mutex_t mutex; /*could be replaced by an atomic counter for freeroom*/
35
uint8_t buffer[MS_EVENT_BUF_SIZE];
38
static void write_event(MSEventQueue *q, MSFilter *f, unsigned int ev_id, void *arg){
39
int argsize=ev_id & 0xffff;
41
uint8_t *nextpos=q->wptr+size;
43
if (q->freeroom<size){
44
ms_error("Dropped event, no more free space in event buffer !");
49
/* need to wrap around */
54
*(long*)q->wptr=(long)f;
55
*(long*)(q->wptr+8)=(long)ev_id;
56
if (argsize>0) memcpy(q->wptr+16,arg,argsize);
58
ms_mutex_lock(&q->mutex);
60
ms_mutex_unlock(&q->mutex);
63
static bool_t read_event(MSEventQueue *q){
64
int available=q->size-q->freeroom;
72
f=(MSFilter *)*(long*)(q->rptr);
73
id=(unsigned int)*(long*)(q->rptr+8);
78
f->notify(f->notify_ud,id,argsize>0 ? data : NULL);
80
if (q->rptr>=q->endptr){
83
ms_mutex_lock(&q->mutex);
85
ms_mutex_unlock(&q->mutex);
91
MSEventQueue *ms_event_queue_new(){
92
MSEventQueue *q=ms_new0(MSEventQueue,1);
93
int bufsize=MS_EVENT_BUF_SIZE;
94
ms_mutex_init(&q->mutex,NULL);
95
q->lim=q->buffer+bufsize;
97
q->wptr=q->rptr=q->buffer;
103
void ms_event_queue_destroy(MSEventQueue *q){
104
ms_mutex_destroy(&q->mutex);
108
static MSEventQueue *ms_global_event_queue=NULL;
110
void ms_set_global_event_queue(MSEventQueue *q){
111
ms_global_event_queue=q;
114
void ms_event_queue_pump(MSEventQueue *q){
115
while(read_event(q)){
120
void ms_filter_notify(MSFilter *f, unsigned int id, void *arg){
121
if (f->notify!=NULL){
122
if (ms_global_event_queue==NULL){
123
/* synchronous notification */
124
f->notify(f->notify_ud,id,arg);
126
write_event(ms_global_event_queue,f,id,arg);
131
void ms_filter_notify_no_arg(MSFilter *f, unsigned int id){
132
ms_filter_notify(f,id,NULL);