source: roaraudio/libroar/vio_buffer_store.c @ 5393:f50243718494

Last change on this file since 5393:f50243718494 was 5388:e5cc8e03a3e1, checked in by phi, 12 years ago

Added support for refcount based VIOs as well as dynamically allocated VIOs (non-stack or global VIOs) (Closes: #127)

File size: 4.5 KB
Line 
1//vio_buffer_store.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010-2012
5 *
6 *  This file is part of libroar a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  libroar is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 *  NOTE for everyone want's to change something and send patches:
25 *  read README and HACKING! There a addition information on
26 *  the license of this document you need to read before you send
27 *  any patches.
28 *
29 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
30 *  or libpulse*:
31 *  The libs libroaresd, libroararts and libroarpulse link this lib
32 *  and are therefore GPL. Because of this it may be illigal to use
33 *  them with any software that uses libesd, libartsc or libpulse*.
34 */
35
36#include "libroar.h"
37
38int     roar_vio_open_buffer_store    (struct roar_vio_calls * calls, struct roar_vio_buffer_store ** inst) {
39 struct roar_vio_buffer_store * self;
40
41 if ( calls == NULL )
42  return -1;
43
44 self = roar_mm_malloc(sizeof(struct roar_vio_buffer_store));
45
46 if ( self == NULL )
47  return -1;
48
49 memset(self, 0, sizeof(struct roar_vio_buffer_store));
50
51 memset(calls, 0, sizeof(struct roar_vio_calls));
52 calls->flags    = ROAR_VIO_FLAGS_NONE;
53 calls->refc     = 1;
54
55 if ( inst != NULL )
56  *inst = self;
57
58 calls->inst     = self;
59 calls->close    = roar_vio_buffer_store_close;
60 calls->sync     = roar_vio_buffer_store_sync;
61 calls->ctl      = roar_vio_buffer_store_ctl;
62 calls->write    = roar_vio_buffer_store_write;
63 calls->read     = roar_vio_buffer_store_read;
64 calls->lseek    = roar_vio_buffer_store_lseek;
65
66 return 0;
67}
68
69int     roar_vio_buffer_store_close   (struct roar_vio_calls * vio) {
70 struct roar_vio_buffer_store * self = vio->inst;
71 int ret = 0;
72
73 if ( self->in != NULL )
74  if ( roar_buffer_free(self->in) != 0 )
75   ret = -1;
76
77 if ( self->out != NULL )
78  if ( roar_buffer_free(self->out) != 0 )
79   ret = -1;
80
81 return ret;
82}
83
84ssize_t roar_vio_buffer_store_read    (struct roar_vio_calls * vio, void *buf, size_t count) {
85 struct roar_vio_buffer_store * self = vio->inst;
86
87 ROAR_DBG("roar_vio_buffer_store_read(vio=%p, buf=%p, count=%llu) = ?", vio, buf, (long long unsigned int)count);
88
89 if ( count == 0 )
90  return 0;
91
92 if ( buf == NULL )
93  return -1;
94
95 if ( self->out == NULL )
96  return 0;
97
98 ROAR_DBG("roar_vio_buffer_store_read(vio=%p, buf=%p, count=%llu) = ?", vio, buf, (long long unsigned int)count);
99
100 if ( roar_buffer_shift_out(&(self->out), buf, &count) == -1 )
101  return -1;
102
103 ROAR_DBG("roar_vio_buffer_store_read(*) = %llu", (long long unsigned int)count);
104
105 return count;
106}
107
108ssize_t roar_vio_buffer_store_write   (struct roar_vio_calls * vio, void *buf, size_t count) {
109 struct roar_vio_buffer_store * self = vio->inst;
110 struct roar_buffer * nbuf;
111 void * data;
112
113 if ( count == 0 )
114  return 0;
115
116 if ( buf == NULL )
117  return -1;
118
119 if ( roar_buffer_new_data(&nbuf, count, &data) == -1 )
120  return -1;
121
122 memcpy(data, buf, count);
123
124 if ( self->in == NULL ) {
125  self->in = nbuf;
126 } else {
127  if ( roar_buffer_moveinto(self->in, &nbuf) == -1 ) {
128   roar_buffer_free(nbuf);
129   return -1;
130  }
131 }
132
133 return count;
134}
135
136roar_off_t   roar_vio_buffer_store_lseek   (struct roar_vio_calls * vio, roar_off_t offset, int whence) {
137 // TODO: implement support to seek forward as wide as the buffer allows us to seek.
138 return (roar_off_t)-1;
139}
140
141int     roar_vio_buffer_store_sync    (struct roar_vio_calls * vio) {
142 return 0; // we are always sync.
143}
144
145int     roar_vio_buffer_store_ctl     (struct roar_vio_calls * vio, roar_vio_ctl_t cmd, void * data) {
146 if (vio == NULL || cmd == -1)
147  return -1;
148
149 ROAR_DBG("roar_vio_buffer_store_ctl(vio=%p, cmd=0x%.8x, data=%p) = ?", vio, cmd, data);
150
151 switch (cmd) {
152  case ROAR_VIO_CTL_GET_NAME:
153    if ( data == NULL )
154     return -1;
155
156    *(char**)data = "buffer_store";
157    return 0;
158   break;
159  case ROAR_VIO_CTL_NONBLOCK:
160    return 0; // if we are in nonblock or not is the same for us.
161   break;
162 }
163
164 return -1;
165}
166
167//ll
Note: See TracBrowser for help on using the repository browser.