source: roaraudio/libroar/vio_buffer_store.c @ 5353:de96f27919bf

Last change on this file since 5353:de96f27919bf was 5300:190af1adf91c, checked in by phi, 12 years ago

first commit to move away from old roar_buffer_add() to roar_buffer_moveinto() (See: #126)

File size: 4.5 KB
Line 
1//vio_buffer_store.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010-2011
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
53 if ( inst != NULL )
54  *inst = self;
55
56 calls->inst     = self;
57 calls->close    = roar_vio_buffer_store_close;
58 calls->sync     = roar_vio_buffer_store_sync;
59 calls->ctl      = roar_vio_buffer_store_ctl;
60 calls->write    = roar_vio_buffer_store_write;
61 calls->read     = roar_vio_buffer_store_read;
62 calls->lseek    = roar_vio_buffer_store_lseek;
63
64 return 0;
65}
66
67int     roar_vio_buffer_store_close   (struct roar_vio_calls * vio) {
68 struct roar_vio_buffer_store * self = vio->inst;
69 int ret = 0;
70
71 if ( self->in != NULL )
72  if ( roar_buffer_free(self->in) != 0 )
73   ret = -1;
74
75 if ( self->out != NULL )
76  if ( roar_buffer_free(self->out) != 0 )
77   ret = -1;
78
79 return ret;
80}
81
82ssize_t roar_vio_buffer_store_read    (struct roar_vio_calls * vio, void *buf, size_t count) {
83 struct roar_vio_buffer_store * self = vio->inst;
84
85 ROAR_DBG("roar_vio_buffer_store_read(vio=%p, buf=%p, count=%llu) = ?", vio, buf, (long long unsigned int)count);
86
87 if ( count == 0 )
88  return 0;
89
90 if ( buf == NULL )
91  return -1;
92
93 if ( self->out == NULL )
94  return 0;
95
96 ROAR_DBG("roar_vio_buffer_store_read(vio=%p, buf=%p, count=%llu) = ?", vio, buf, (long long unsigned int)count);
97
98 if ( roar_buffer_shift_out(&(self->out), buf, &count) == -1 )
99  return -1;
100
101 ROAR_DBG("roar_vio_buffer_store_read(*) = %llu", (long long unsigned int)count);
102
103 return count;
104}
105
106ssize_t roar_vio_buffer_store_write   (struct roar_vio_calls * vio, void *buf, size_t count) {
107 struct roar_vio_buffer_store * self = vio->inst;
108 struct roar_buffer * nbuf;
109 void * data;
110
111 if ( count == 0 )
112  return 0;
113
114 if ( buf == NULL )
115  return -1;
116
117 if ( roar_buffer_new_data(&nbuf, count, &data) == -1 )
118  return -1;
119
120 memcpy(data, buf, count);
121
122 if ( self->in == NULL ) {
123  self->in = nbuf;
124 } else {
125  if ( roar_buffer_moveinto(self->in, &nbuf) == -1 ) {
126   roar_buffer_free(nbuf);
127   return -1;
128  }
129 }
130
131 return count;
132}
133
134roar_off_t   roar_vio_buffer_store_lseek   (struct roar_vio_calls * vio, roar_off_t offset, int whence) {
135 // TODO: implement support to seek forward as wide as the buffer allows us to seek.
136 return (roar_off_t)-1;
137}
138
139int     roar_vio_buffer_store_sync    (struct roar_vio_calls * vio) {
140 return 0; // we are always sync.
141}
142
143int     roar_vio_buffer_store_ctl     (struct roar_vio_calls * vio, roar_vio_ctl_t cmd, void * data) {
144 if (vio == NULL || cmd == -1)
145  return -1;
146
147 ROAR_DBG("roar_vio_buffer_store_ctl(vio=%p, cmd=0x%.8x, data=%p) = ?", vio, cmd, data);
148
149 switch (cmd) {
150  case ROAR_VIO_CTL_GET_NAME:
151    if ( data == NULL )
152     return -1;
153
154    *(char**)data = "buffer_store";
155    return 0;
156   break;
157  case ROAR_VIO_CTL_NONBLOCK:
158    return 0; // if we are in nonblock or not is the same for us.
159   break;
160 }
161
162 return -1;
163}
164
165//ll
Note: See TracBrowser for help on using the repository browser.