~ubuntu-branches/ubuntu/trusty/linphone/trusty

« back to all changes in this revision

Viewing changes to mediastreamer2/src/base/eventqueue.c

  • Committer: Package Import Robot
  • Author(s): Luk Claes
  • Date: 2013-09-11 19:08:43 UTC
  • mfrom: (1.1.19) (16.1.12 sid)
  • Revision ID: package-import@ubuntu.com-20130911190843-fkydjxsdvy1fmx24
Tags: 3.6.1-2.1
* Non-maintainer upload.
* Apply Sebastian Ramacher's patch to fix FTBFS (Closes: #720668).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
mediastreamer2 library - modular sound and video processing and streaming
 
3
Copyright (C) 2010  Simon MORLAT (simon.morlat@linphone.org)
 
4
 
 
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.
 
9
 
 
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.
 
14
 
 
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.
 
18
*/
 
19
 
 
20
 
 
21
#include "mediastreamer2/mseventqueue.h"
 
22
#include "mediastreamer2/msfilter.h"
 
23
 
 
24
#ifndef MS_EVENT_BUF_SIZE
 
25
#define MS_EVENT_BUF_SIZE 8192
 
26
#endif
 
27
 
 
28
struct _MSEventQueue{
 
29
        ms_mutex_t mutex; /*could be replaced by an atomic counter for freeroom*/
 
30
        uint8_t *rptr;
 
31
        uint8_t *wptr;
 
32
        uint8_t *endptr;
 
33
        uint8_t *lim;
 
34
        int freeroom;
 
35
        int size;
 
36
        uint8_t buffer[MS_EVENT_BUF_SIZE];
 
37
};
 
38
 
 
39
static void write_event(MSEventQueue *q, MSFilter *f, unsigned int ev_id, void *arg){
 
40
        int argsize=ev_id & 0xff;
 
41
        int size=argsize+16;
 
42
        uint8_t *nextpos=q->wptr+size;
 
43
 
 
44
        if (q->freeroom<size){
 
45
                ms_error("Dropped event, no more free space in event buffer !");
 
46
                return;
 
47
        }
 
48
        
 
49
        if (nextpos>q->lim){
 
50
                /* need to wrap around */
 
51
                q->endptr=q->wptr;
 
52
                q->wptr=q->buffer;
 
53
                nextpos=q->wptr+size;
 
54
        }
 
55
        *(long*)q->wptr=(long)f;
 
56
        *(long*)(q->wptr+8)=(long)ev_id;
 
57
        if (argsize>0) memcpy(q->wptr+16,arg,argsize);
 
58
        q->wptr=nextpos;
 
59
        ms_mutex_lock(&q->mutex);
 
60
        q->freeroom-=size;
 
61
        ms_mutex_unlock(&q->mutex);
 
62
}
 
63
 
 
64
static bool_t read_event(MSEventQueue *q){
 
65
        int available=q->size-q->freeroom;
 
66
        if (available>0){
 
67
                MSFilter *f;
 
68
                unsigned int id;
 
69
                void *data;
 
70
                int argsize;
 
71
                int evsize;
 
72
                
 
73
                f=(MSFilter *)*(long*)(q->rptr);
 
74
                id=(unsigned int)*(long*)(q->rptr+8);
 
75
                argsize=id & 0xff;
 
76
                evsize=argsize+16;
 
77
                data=q->rptr+16;
 
78
                if (f->notify!=NULL)
 
79
                        f->notify(f->notify_ud,f,id,argsize>0 ? data : NULL);
 
80
                q->rptr+=evsize;
 
81
                if (q->rptr>=q->endptr){
 
82
                        q->rptr=q->buffer;
 
83
                }
 
84
                ms_mutex_lock(&q->mutex);
 
85
                q->freeroom+=evsize;
 
86
                ms_mutex_unlock(&q->mutex);
 
87
                return TRUE;
 
88
        }
 
89
        return FALSE;
 
90
}
 
91
 
 
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;
 
97
        q->freeroom=bufsize;
 
98
        q->wptr=q->rptr=q->buffer;
 
99
        q->endptr=q->lim;
 
100
        q->size=bufsize;
 
101
        return q;
 
102
}
 
103
 
 
104
void ms_event_queue_destroy(MSEventQueue *q){
 
105
        ms_mutex_destroy(&q->mutex);
 
106
        ms_free(q);
 
107
}
 
108
 
 
109
static MSEventQueue *ms_global_event_queue=NULL;
 
110
 
 
111
void ms_set_global_event_queue(MSEventQueue *q){
 
112
        ms_global_event_queue=q;
 
113
}
 
114
 
 
115
void ms_event_queue_skip(MSEventQueue *q){
 
116
        int bufsize=q->size;
 
117
        q->lim=q->buffer+bufsize;
 
118
        q->freeroom=bufsize;
 
119
        q->wptr=q->rptr=q->buffer;
 
120
        q->endptr=q->lim;
 
121
}
 
122
 
 
123
 
 
124
void ms_event_queue_pump(MSEventQueue *q){
 
125
        while(read_event(q)){
 
126
        }
 
127
}
 
128
 
 
129
 
 
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);
 
135
                }else{
 
136
                        write_event(ms_global_event_queue,f,id,arg);
 
137
                }
 
138
        }
 
139
}
 
140
 
 
141
void ms_filter_notify_synchronous(MSFilter *f, unsigned int id, void *arg){
 
142
        if (f->notify){
 
143
                f->notify(f->notify_ud,f,id,arg);
 
144
        }
 
145
}
 
146
 
 
147
void ms_filter_notify_no_arg(MSFilter *f, unsigned int id){
 
148
        ms_filter_notify(f,id,NULL);
 
149
}