]> sjero.net Git - linphone/blob - linphone/mediastreamer2/src/msqueue.c
66172a517dbfd87fec48e89a2581047662c1c68f
[linphone] / linphone / mediastreamer2 / src / msqueue.c
1 /*
2 mediastreamer2 library - modular sound and video processing and streaming
3 Copyright (C) 2006  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 #include "mediastreamer2/msqueue.h"
21 #include "mediastreamer2/mscommon.h"
22 #include "mediastreamer2/msvideo.h"
23 #include <string.h>
24
25 MSQueue * ms_queue_new(struct _MSFilter *f1, int pin1, struct _MSFilter *f2, int pin2 ){
26         MSQueue *q=(MSQueue*)ms_new(MSQueue,1);
27         qinit(&q->q);
28         q->prev.filter=f1;
29         q->prev.pin=pin1;
30         q->next.filter=f2;
31         q->next.pin=pin2;
32         return q;
33 }
34
35 void ms_queue_init(MSQueue *q){
36         q->prev.filter=0;
37         q->prev.pin=0;
38         q->next.filter=0;
39         q->next.pin=0;
40         qinit(&q->q);
41 }
42
43 void ms_queue_destroy(MSQueue *q){
44         flushq(&q->q,0);
45         ms_free(q);
46 }
47
48 void ms_queue_flush(MSQueue *q){
49         flushq(&q->q,0);
50 }
51
52
53 void ms_bufferizer_init(MSBufferizer *obj){
54         qinit(&obj->q);
55         obj->size=0;
56 }
57
58 MSBufferizer * ms_bufferizer_new(){
59         MSBufferizer *obj=(MSBufferizer *)ms_new(MSBufferizer,1);
60         ms_bufferizer_init(obj);
61         return obj;
62 }
63
64 void ms_bufferizer_put(MSBufferizer *obj, mblk_t *m){
65         obj->size+=msgdsize(m);
66         putq(&obj->q,m);
67 }
68
69 void ms_bufferizer_put_from_queue(MSBufferizer *obj, MSQueue *q){
70         mblk_t *m;
71         while((m=ms_queue_get(q))!=NULL){
72                 ms_bufferizer_put(obj,m);
73         }
74 }
75
76 int ms_bufferizer_read(MSBufferizer *obj, uint8_t *data, int datalen){
77         if (obj->size>=datalen){
78                 int sz=0;
79                 int cplen;
80                 mblk_t *m=peekq(&obj->q);
81                 /*we can return something */
82                 while(sz<datalen){
83                         cplen=MIN(m->b_wptr-m->b_rptr,datalen-sz);
84                         memcpy(data+sz,m->b_rptr,cplen);
85                         sz+=cplen;
86                         m->b_rptr+=cplen;
87                         if (m->b_rptr==m->b_wptr){
88                                 /* check cont */
89                                 if (m->b_cont!=NULL) {
90                                         m=m->b_cont;
91                                 }
92                                 else{
93                                         mblk_t *remove=getq(&obj->q);
94                                         freemsg(remove);
95                                         m=peekq(&obj->q);
96                                 }
97                         }
98                 }
99                 obj->size-=datalen;
100                 return datalen;
101         }
102         return 0;
103 }
104
105 void ms_bufferizer_flush(MSBufferizer *obj){
106         obj->size=0;
107         flushq(&obj->q,0);
108 }
109
110 void ms_bufferizer_uninit(MSBufferizer *obj){
111         flushq(&obj->q,0);
112 }
113
114 void ms_bufferizer_destroy(MSBufferizer *obj){
115         ms_bufferizer_uninit(obj);
116         ms_free(obj);
117 }