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.
21
#include "mediastreamer2/mseventqueue.h"
22
#include "mediastreamer2/msfilter.h"
24
#ifndef MS_EVENT_BUF_SIZE
25
#define MS_EVENT_BUF_SIZE 8192
29
ms_mutex_t mutex; /*could be replaced by an atomic counter for freeroom*/
36
uint8_t buffer[MS_EVENT_BUF_SIZE];
39
static void write_event(MSEventQueue *q, MSFilter *f, unsigned int ev_id, void *arg){
40
int argsize=ev_id & 0xff;
42
uint8_t *nextpos=q->wptr+size;
44
if (q->freeroom<size){
45
ms_error("Dropped event, no more free space in event buffer !");
50
/* need to wrap around */
55
*(long*)q->wptr=(long)f;
56
*(long*)(q->wptr+8)=(long)ev_id;
57
if (argsize>0) memcpy(q->wptr+16,arg,argsize);
59
ms_mutex_lock(&q->mutex);
61
ms_mutex_unlock(&q->mutex);
64
static bool_t read_event(MSEventQueue *q){
65
int available=q->size-q->freeroom;
73
f=(MSFilter *)*(long*)(q->rptr);
74
id=(unsigned int)*(long*)(q->rptr+8);
79
f->notify(f->notify_ud,f,id,argsize>0 ? data : NULL);
81
if (q->rptr>=q->endptr){
84
ms_mutex_lock(&q->mutex);
86
ms_mutex_unlock(&q->mutex);
92
MSEventQueue *ms_event_queue_new(){
93
MSEventQueue *q=ms_new0(MSEventQueue,1);
94
int bufsize=MS_EVENT_BUF_SIZE;
95
ms_mutex_init(&q->mutex,NULL);
96
q->lim=q->buffer+bufsize;
98
q->wptr=q->rptr=q->buffer;
104
void ms_event_queue_destroy(MSEventQueue *q){
105
ms_mutex_destroy(&q->mutex);
109
static MSEventQueue *ms_global_event_queue=NULL;
111
void ms_set_global_event_queue(MSEventQueue *q){
112
ms_global_event_queue=q;
115
void ms_event_queue_skip(MSEventQueue *q){
117
q->lim=q->buffer+bufsize;
119
q->wptr=q->rptr=q->buffer;
124
void ms_event_queue_pump(MSEventQueue *q){
125
while(read_event(q)){
130
void ms_filter_notify(MSFilter *f, unsigned int id, void *arg){
131
if (f->notify!=NULL){
132
if (ms_global_event_queue==NULL){
133
/* synchronous notification */
134
f->notify(f->notify_ud,f,id,arg);
136
write_event(ms_global_event_queue,f,id,arg);
141
void ms_filter_notify_synchronous(MSFilter *f, unsigned int id, void *arg){
143
f->notify(f->notify_ud,f,id,arg);
147
void ms_filter_notify_no_arg(MSFilter *f, unsigned int id){
148
ms_filter_notify(f,id,NULL);