root/trunk/libffado/src/libutil/PacketBuffer.cpp

Revision 742, 4.1 kB (checked in by ppalmers, 13 years ago)

- Remove some obsolete support files and dirs

- Clean up the license statements in the source files. Everything is

GPL version 3 now.

- Add license and copyright notices to scons scripts

- Clean up some other text files

Line 
1 /*
2  * Copyright (C) 2005-2007 by Pieter Palmers
3  *
4  * This file is part of FFADO
5  * FFADO = Free Firewire (pro-)audio drivers for linux
6  *
7  * FFADO is based upon FreeBoB.
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program 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 program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  */
23
24 #include "PacketBuffer.h"
25 #include <assert.h>
26
27
28 namespace Streaming {
29
30 IMPL_DEBUG_MODULE( PacketBuffer, PacketBuffer, DEBUG_LEVEL_VERBOSE );
31
32
33 PacketBuffer::~PacketBuffer() {
34     if(payload_buffer) ffado_ringbuffer_free(payload_buffer);
35     if(header_buffer) ffado_ringbuffer_free(header_buffer);
36     if(len_buffer) ffado_ringbuffer_free(len_buffer);
37 }
38
39 int PacketBuffer::initialize() {
40     debugOutput( DEBUG_LEVEL_VERBOSE, "enter...\n");
41
42     if(payload_buffer) ffado_ringbuffer_free(payload_buffer);
43     if(header_buffer) ffado_ringbuffer_free(header_buffer);
44     if(len_buffer) ffado_ringbuffer_free(len_buffer);
45
46     payload_buffer=ffado_ringbuffer_create(m_buffersize * m_max_packetsize * sizeof(quadlet_t));
47     if(!payload_buffer) {
48         debugFatal("Could not allocate payload buffer\n");
49         return -1;
50     }
51
52     header_buffer=ffado_ringbuffer_create(m_buffersize * (m_headersize) * sizeof(quadlet_t));
53     if(!header_buffer) {
54         debugFatal("Could not allocate header buffer\n");
55         return -1;
56     }
57
58     len_buffer=ffado_ringbuffer_create(m_buffersize * sizeof(unsigned int));
59     if(!len_buffer) {
60         debugFatal("Could not allocate len buffer\n");
61         return -1;
62     }
63     debugOutput( DEBUG_LEVEL_VERBOSE, "exit...\n");
64
65     return 0;
66 }
67
68 void PacketBuffer::flush() {
69     if(header_buffer) {
70         ffado_ringbuffer_reset(header_buffer);
71     }
72     if(payload_buffer) {
73         ffado_ringbuffer_reset(payload_buffer);
74     }
75     if(len_buffer) {
76         ffado_ringbuffer_reset(len_buffer);
77     }
78 }
79
80 int PacketBuffer::addPacket(quadlet_t *packet, int packet_len) {
81
82     unsigned int payload_bytes=sizeof(quadlet_t)*(packet_len-m_headersize);
83     unsigned int header_bytes=sizeof(quadlet_t)*(m_headersize);
84
85     debugOutput( DEBUG_LEVEL_VERY_VERBOSE,
86                  "add packet: length=%d\n",
87                  packet_len);
88
89     if ((ffado_ringbuffer_write_space(payload_buffer) > payload_bytes)
90         && (ffado_ringbuffer_write_space(header_buffer) > header_bytes)) {
91
92         ffado_ringbuffer_write(payload_buffer,(char *)(packet)+header_bytes, payload_bytes);
93         ffado_ringbuffer_write(len_buffer,(char *)(&payload_bytes),sizeof(unsigned int));
94         ffado_ringbuffer_write(header_buffer,(char *)(packet), header_bytes);
95
96     } else return -1;
97     return 0;
98
99 }
100
101 int PacketBuffer::getNextPacket(quadlet_t *packet, int max_packet_len) {
102
103     unsigned int bytes=sizeof(quadlet_t)*(m_headersize);
104     quadlet_t *ptr=packet+m_headersize;
105
106     debugOutput( DEBUG_LEVEL_VERY_VERBOSE,
107                  "getNextPacket\n");
108
109     if(max_packet_len<m_headersize) return -2;
110
111     if (ffado_ringbuffer_read(header_buffer,(char *)packet,bytes) < bytes) {
112         return -1;
113     }
114
115     if (ffado_ringbuffer_read(len_buffer,(char *)&bytes, sizeof(unsigned int)) < sizeof(unsigned int)) {
116         return -3;
117     }
118
119     if(bytes>(max_packet_len-m_headersize)*sizeof(quadlet_t)) return -2;
120
121     if(ffado_ringbuffer_read(payload_buffer,(char *)(ptr),bytes) < bytes) {
122         return -3;
123     }
124
125     return bytes/sizeof(quadlet_t)+m_headersize;
126 }
127
128 int PacketBuffer::getBufferFillPackets() {
129
130     return ffado_ringbuffer_read_space(len_buffer)/sizeof(unsigned int);
131 }
132
133 // in quadlets
134 int PacketBuffer::getBufferFillPayload() {
135
136     return ffado_ringbuffer_read_space(payload_buffer)/sizeof(quadlet_t);
137 }
138
139 }
Note: See TracBrowser for help on using the browser.