• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 // A ring buffer to hold arbitrary data. Provides no thread safety. Unless
12 // otherwise specified, functions return 0 on success and -1 on error.
13 
14 #include "common_audio/ring_buffer.h"
15 
16 #include <stddef.h>  // size_t
17 #include <stdlib.h>
18 #include <string.h>
19 
20 // Get address of region(s) from which we can read data.
21 // If the region is contiguous, |data_ptr_bytes_2| will be zero.
22 // If non-contiguous, |data_ptr_bytes_2| will be the size in bytes of the second
23 // region. Returns room available to be read or |element_count|, whichever is
24 // smaller.
GetBufferReadRegions(RingBuffer * buf,size_t element_count,void ** data_ptr_1,size_t * data_ptr_bytes_1,void ** data_ptr_2,size_t * data_ptr_bytes_2)25 static size_t GetBufferReadRegions(RingBuffer* buf,
26                                    size_t element_count,
27                                    void** data_ptr_1,
28                                    size_t* data_ptr_bytes_1,
29                                    void** data_ptr_2,
30                                    size_t* data_ptr_bytes_2) {
31 
32   const size_t readable_elements = WebRtc_available_read(buf);
33   const size_t read_elements = (readable_elements < element_count ?
34       readable_elements : element_count);
35   const size_t margin = buf->element_count - buf->read_pos;
36 
37   // Check to see if read is not contiguous.
38   if (read_elements > margin) {
39     // Write data in two blocks that wrap the buffer.
40     *data_ptr_1 = buf->data + buf->read_pos * buf->element_size;
41     *data_ptr_bytes_1 = margin * buf->element_size;
42     *data_ptr_2 = buf->data;
43     *data_ptr_bytes_2 = (read_elements - margin) * buf->element_size;
44   } else {
45     *data_ptr_1 = buf->data + buf->read_pos * buf->element_size;
46     *data_ptr_bytes_1 = read_elements * buf->element_size;
47     *data_ptr_2 = NULL;
48     *data_ptr_bytes_2 = 0;
49   }
50 
51   return read_elements;
52 }
53 
WebRtc_CreateBuffer(size_t element_count,size_t element_size)54 RingBuffer* WebRtc_CreateBuffer(size_t element_count, size_t element_size) {
55   RingBuffer* self = NULL;
56   if (element_count == 0 || element_size == 0) {
57     return NULL;
58   }
59 
60   self = malloc(sizeof(RingBuffer));
61   if (!self) {
62     return NULL;
63   }
64 
65   self->data = malloc(element_count * element_size);
66   if (!self->data) {
67     free(self);
68     self = NULL;
69     return NULL;
70   }
71 
72   self->element_count = element_count;
73   self->element_size = element_size;
74   WebRtc_InitBuffer(self);
75 
76   return self;
77 }
78 
WebRtc_InitBuffer(RingBuffer * self)79 void WebRtc_InitBuffer(RingBuffer* self) {
80   self->read_pos = 0;
81   self->write_pos = 0;
82   self->rw_wrap = SAME_WRAP;
83 
84   // Initialize buffer to zeros
85   memset(self->data, 0, self->element_count * self->element_size);
86 }
87 
WebRtc_FreeBuffer(void * handle)88 void WebRtc_FreeBuffer(void* handle) {
89   RingBuffer* self = (RingBuffer*)handle;
90   if (!self) {
91     return;
92   }
93 
94   free(self->data);
95   free(self);
96 }
97 
WebRtc_ReadBuffer(RingBuffer * self,void ** data_ptr,void * data,size_t element_count)98 size_t WebRtc_ReadBuffer(RingBuffer* self,
99                          void** data_ptr,
100                          void* data,
101                          size_t element_count) {
102 
103   if (self == NULL) {
104     return 0;
105   }
106   if (data == NULL) {
107     return 0;
108   }
109 
110   {
111     void* buf_ptr_1 = NULL;
112     void* buf_ptr_2 = NULL;
113     size_t buf_ptr_bytes_1 = 0;
114     size_t buf_ptr_bytes_2 = 0;
115     const size_t read_count = GetBufferReadRegions(self,
116                                                    element_count,
117                                                    &buf_ptr_1,
118                                                    &buf_ptr_bytes_1,
119                                                    &buf_ptr_2,
120                                                    &buf_ptr_bytes_2);
121     if (buf_ptr_bytes_2 > 0) {
122       // We have a wrap around when reading the buffer. Copy the buffer data to
123       // |data| and point to it.
124       memcpy(data, buf_ptr_1, buf_ptr_bytes_1);
125       memcpy(((char*) data) + buf_ptr_bytes_1, buf_ptr_2, buf_ptr_bytes_2);
126       buf_ptr_1 = data;
127     } else if (!data_ptr) {
128       // No wrap, but a memcpy was requested.
129       memcpy(data, buf_ptr_1, buf_ptr_bytes_1);
130     }
131     if (data_ptr) {
132       // |buf_ptr_1| == |data| in the case of a wrap.
133       *data_ptr = read_count == 0 ? NULL : buf_ptr_1;
134     }
135 
136     // Update read position
137     WebRtc_MoveReadPtr(self, (int) read_count);
138 
139     return read_count;
140   }
141 }
142 
WebRtc_WriteBuffer(RingBuffer * self,const void * data,size_t element_count)143 size_t WebRtc_WriteBuffer(RingBuffer* self,
144                           const void* data,
145                           size_t element_count) {
146   if (!self) {
147     return 0;
148   }
149   if (!data) {
150     return 0;
151   }
152 
153   {
154     const size_t free_elements = WebRtc_available_write(self);
155     const size_t write_elements = (free_elements < element_count ? free_elements
156         : element_count);
157     size_t n = write_elements;
158     const size_t margin = self->element_count - self->write_pos;
159 
160     if (write_elements > margin) {
161       // Buffer wrap around when writing.
162       memcpy(self->data + self->write_pos * self->element_size,
163              data, margin * self->element_size);
164       self->write_pos = 0;
165       n -= margin;
166       self->rw_wrap = DIFF_WRAP;
167     }
168     memcpy(self->data + self->write_pos * self->element_size,
169            ((const char*) data) + ((write_elements - n) * self->element_size),
170            n * self->element_size);
171     self->write_pos += n;
172 
173     return write_elements;
174   }
175 }
176 
WebRtc_MoveReadPtr(RingBuffer * self,int element_count)177 int WebRtc_MoveReadPtr(RingBuffer* self, int element_count) {
178   if (!self) {
179     return 0;
180   }
181 
182   {
183     // We need to be able to take care of negative changes, hence use "int"
184     // instead of "size_t".
185     const int free_elements = (int) WebRtc_available_write(self);
186     const int readable_elements = (int) WebRtc_available_read(self);
187     int read_pos = (int) self->read_pos;
188 
189     if (element_count > readable_elements) {
190       element_count = readable_elements;
191     }
192     if (element_count < -free_elements) {
193       element_count = -free_elements;
194     }
195 
196     read_pos += element_count;
197     if (read_pos > (int) self->element_count) {
198       // Buffer wrap around. Restart read position and wrap indicator.
199       read_pos -= (int) self->element_count;
200       self->rw_wrap = SAME_WRAP;
201     }
202     if (read_pos < 0) {
203       // Buffer wrap around. Restart read position and wrap indicator.
204       read_pos += (int) self->element_count;
205       self->rw_wrap = DIFF_WRAP;
206     }
207 
208     self->read_pos = (size_t) read_pos;
209 
210     return element_count;
211   }
212 }
213 
WebRtc_available_read(const RingBuffer * self)214 size_t WebRtc_available_read(const RingBuffer* self) {
215   if (!self) {
216     return 0;
217   }
218 
219   if (self->rw_wrap == SAME_WRAP) {
220     return self->write_pos - self->read_pos;
221   } else {
222     return self->element_count - self->read_pos + self->write_pos;
223   }
224 }
225 
WebRtc_available_write(const RingBuffer * self)226 size_t WebRtc_available_write(const RingBuffer* self) {
227   if (!self) {
228     return 0;
229   }
230 
231   return self->element_count - WebRtc_available_read(self);
232 }
233