root/branches/streaming-rework/src/libstreaming/StreamProcessor.cpp

Revision 395, 8.3 kB (checked in by pieterpalmers, 17 years ago)

debugmodule.cpp:
- removed path from the source file name when printing debug messages

IsoStream?.cpp, StreamProcessor?.cpp:
- debug message modifications

StreamProcessorManager?.cpp:
- removed obsolete debug code

AmdtpStreamProcessor?.cpp:
- debug message modifications
- removed DLL that calculates framerate, as it is also calculated in the

TimestampedBuffer?

- converted code to use the new offset feature of the TimestampedBuffer?
- converted code to use the new frame timestamp calculation feature

of the TimestampedBuffer?

- first try at xmit sync code (unfinished)
- fixed 'unable to start' bug in receive SP

bebob_avdevice.cpp:
- added some #ifdef code to test with xmit SP's only (temporary)

TimestampedBuffer?.cpp:
- add offset feature
- add abitrary frame timestamp calculation

Line 
1 /* $Id$ */
2
3 /*
4  *   FreeBob Streaming API
5  *   FreeBob = Firewire (pro-)audio for linux
6  *
7  *   http://freebob.sf.net
8  *
9  *   Copyright (C) 2005,2006 Pieter Palmers <pieterpalmers@users.sourceforge.net>
10  *
11  *   This program is free software {} you can redistribute it and/or modify
12  *   it under the terms of the GNU General Public License as published by
13  *   the Free Software Foundation {} either version 2 of the License, or
14  *   (at your option) any later version.
15  *
16  *   This program is distributed in the hope that it will be useful,
17  *   but WITHOUT ANY WARRANTY {} without even the implied warranty of
18  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *   GNU General Public License for more details.
20  *
21  *   You should have received a copy of the GNU General Public License
22  *   along with this program {} if not, write to the Free Software
23  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  *
25  *
26  *
27  */
28
29 #include "libutil/Atomic.h"
30
31 #include "StreamProcessor.h"
32 #include "StreamProcessorManager.h"
33 #include "cycletimer.h"
34
35 #include <assert.h>
36
37 namespace FreebobStreaming {
38
39 IMPL_DEBUG_MODULE( StreamProcessor, StreamProcessor, DEBUG_LEVEL_VERBOSE );
40 IMPL_DEBUG_MODULE( ReceiveStreamProcessor, ReceiveStreamProcessor, DEBUG_LEVEL_VERBOSE );
41 IMPL_DEBUG_MODULE( TransmitStreamProcessor, TransmitStreamProcessor, DEBUG_LEVEL_VERBOSE );
42
43 StreamProcessor::StreamProcessor(enum IsoStream::EStreamType type, int port, int framerate)
44         : IsoStream(type, port)
45         , m_nb_buffers(0)
46         , m_period(0)
47         , m_xruns(0)
48         , m_framerate(framerate)
49         , m_manager(NULL)
50         , m_running(false)
51         , m_disabled(true)
52         , m_is_disabled(true)
53         , m_cycle_to_enable_at(0)
54         , m_SyncSource(NULL)
55         , m_ticks_per_frame(0)
56 {
57     // create the timestamped buffer and register ourselves as its client
58     m_data_buffer=new FreebobUtil::TimestampedBuffer(this);
59
60 }
61
62 StreamProcessor::~StreamProcessor() {
63     if (m_data_buffer) delete m_data_buffer;
64 }
65
66 void StreamProcessor::dumpInfo()
67 {
68     debugOutputShort( DEBUG_LEVEL_NORMAL, " StreamProcessor information\n");
69     debugOutputShort( DEBUG_LEVEL_NORMAL, "  Iso stream info:\n");
70    
71     IsoStream::dumpInfo();
72     debugOutputShort( DEBUG_LEVEL_NORMAL, "  StreamProcessor info:\n");
73     debugOutputShort( DEBUG_LEVEL_NORMAL, "  Now                   : %011u\n",m_handler->getCycleTimerTicks());
74     debugOutputShort( DEBUG_LEVEL_NORMAL, "  Xruns                 : %d\n", m_xruns);
75     debugOutputShort( DEBUG_LEVEL_NORMAL, "  Running               : %d\n", m_running);
76     debugOutputShort( DEBUG_LEVEL_NORMAL, "  Enabled               : %s\n", m_disabled ? "No" : "Yes");
77     debugOutputShort( DEBUG_LEVEL_NORMAL, "   enable status        : %s\n", m_is_disabled ? "No" : "Yes");
78    
79     debugOutputShort( DEBUG_LEVEL_NORMAL, "  Device framerate      : Sync: %f, Buffer %f\n",
80         24576000.0*m_SyncSource->m_data_buffer->getRate(),
81         24576000.0*m_data_buffer->getRate()
82         );
83    
84     m_data_buffer->dumpInfo();
85    
86 //     m_PeriodStat.dumpInfo();
87 //     m_PacketStat.dumpInfo();
88 //     m_WakeupStat.dumpInfo();
89
90 }
91
92 bool StreamProcessor::init()
93 {
94     debugOutput( DEBUG_LEVEL_VERY_VERBOSE, "enter...\n");
95    
96     m_data_buffer->init();
97    
98     return IsoStream::init();
99 }
100
101 /**
102  * Resets the frame counter, the xrun counter, the ports and the iso stream.
103  * @return true if reset succeeded
104  */
105 bool StreamProcessor::reset() {
106
107     debugOutput( DEBUG_LEVEL_VERBOSE, "Resetting...\n");
108
109     // reset the event buffer, discard all content
110     if (!m_data_buffer->reset()) {
111         debugFatal("Could not reset data buffer\n");
112         return false;
113     }
114    
115     resetXrunCounter();
116
117     // loop over the ports to reset them
118     if (!PortManager::resetPorts()) {
119         debugFatal("Could not reset ports\n");
120         return false;
121     }
122
123     // reset the iso stream
124     if (!IsoStream::reset()) {
125         debugFatal("Could not reset isostream\n");
126         return false;
127     }
128     return true;
129        
130 }
131    
132 bool StreamProcessor::prepareForEnable() {
133     debugOutput(DEBUG_LEVEL_VERBOSE," StreamProcessor::prepareForEnable for (%p)\n",this);
134     debugOutput(DEBUG_LEVEL_VERBOSE," Now                   : %011u\n",m_handler->getCycleTimerTicks());
135     m_data_buffer->dumpInfo();
136     return true;
137 }
138
139 bool StreamProcessor::prepareForDisable() {
140     debugOutput(DEBUG_LEVEL_VERBOSE," StreamProcessor::prepareForDisable for (%p)\n",this);
141     debugOutput(DEBUG_LEVEL_VERBOSE," Now                   : %011u\n",m_handler->getCycleTimerTicks());
142     m_data_buffer->dumpInfo();
143     return true;
144 }
145
146 bool StreamProcessor::prepare() {
147
148         debugOutput( DEBUG_LEVEL_VERBOSE, "Preparing...\n");
149        
150         // init the ports
151        
152         if(!m_manager) {
153                 debugFatal("Not attached to a manager!\n");
154                 return -1;
155         }
156
157         m_nb_buffers=m_manager->getNbBuffers();
158         debugOutput( DEBUG_LEVEL_VERBOSE, "Setting m_nb_buffers  : %d\n", m_nb_buffers);
159
160         m_period=m_manager->getPeriodSize();
161         debugOutput( DEBUG_LEVEL_VERBOSE, "Setting m_period      : %d\n", m_period);
162
163         // loop over the ports to reset them
164         PortManager::preparePorts();
165
166         // reset the iso stream
167         IsoStream::prepare();
168        
169         return true;
170
171 }
172
173 // /**
174 //  * @brief Notify the StreamProcessor that frames were written
175 //  *
176 //  * This notifies the StreamProcessor of the fact that frames were written to the internal
177 //  * buffer. This is for framecounter & timestamp bookkeeping.
178 //  *
179 //  * @param nbframes the number of frames that are written to the internal buffers
180 //  * @param ts the new timestamp of the 'tail' of the buffer, i.e. the last sample
181 //  *           present in the buffer.
182 //  * @return true if successful
183 //  */
184 // bool StreamProcessor::putFrames(unsigned int nbframes, int64_t ts) {
185 //
186 //      debugOutput( DEBUG_LEVEL_VERY_VERBOSE, "Putting %d frames for %llu into frame buffer...\n", nbframes,ts);
187 // //         m_data_buffer->incrementFrameCounter(nbframes, ts);
188 //      return true;
189 // }
190
191 // /**
192 // * @brief Notify the StreamProcessor that frames were read
193 // *
194 // * This notifies the StreamProcessor of the fact that frames were read from the internal
195 // * buffer. This is for framecounter & timestamp bookkeeping.
196 // *
197 // * @param nbframes the number of frames that are read from the internal buffers
198 // * @return true if successful
199 // */
200 // bool StreamProcessor::getFrames(unsigned int nbframes) {
201 //
202 //      debugOutput( DEBUG_LEVEL_VERY_VERBOSE, "Getting %d frames from frame buffer...\n", nbframes);
203 // //         m_data_buffer->decrementFrameCounter(nbframes);
204 //      return true;
205 // }
206
207 uint64_t StreamProcessor::getTimeNow() {
208     return m_handler->getCycleTimerTicks();
209 }
210
211
212 bool StreamProcessor::isRunning() {
213         return m_running;
214 }
215
216 bool StreamProcessor::enable(uint64_t time_to_enable_at)  {
217     // FIXME: time_to_enable_at will be in 'time' not cycles
218     m_cycle_to_enable_at=time_to_enable_at;
219    
220     if(!m_running) {
221             debugWarning("The StreamProcessor is not running yet, enable() might not be a good idea.\n");
222     }
223
224 #ifdef DEBUG
225     uint64_t now_cycles=TICKS_TO_CYCLES(m_handler->getCycleTimerTicks());
226     const int64_t max=(int64_t)(TICKS_PER_SECOND/2);
227    
228     int64_t diff=m_cycle_to_enable_at-now_cycles;
229    
230     if (diff > max) {
231         diff-=TICKS_PER_SECOND;
232     } else if (diff < -max) {
233         diff+=TICKS_PER_SECOND;
234     }
235    
236     if (diff<0) {
237         debugWarning("Request to enable streamprocessor %d cycles ago.\n",diff);
238     }
239 #endif
240
241     m_disabled=false;
242    
243     return true;
244 }
245
246 bool StreamProcessor::disable()  {
247    
248     m_disabled=true;
249
250     return true;
251
252 }
253
254 bool StreamProcessor::setSyncSource(StreamProcessor *s) {
255     m_SyncSource=s;
256     return true;
257 }
258
259 /**
260  * Resets the xrun counter, in a atomic way. This
261  * is thread safe.
262  */
263 void StreamProcessor::resetXrunCounter() {
264         ZERO_ATOMIC((SInt32 *)&m_xruns);
265 }
266
267 void StreamProcessor::setVerboseLevel(int l) {
268         setDebugLevel(l);
269         IsoStream::setVerboseLevel(l);
270         PortManager::setVerboseLevel(l);
271
272 }
273
274 ReceiveStreamProcessor::ReceiveStreamProcessor(int port, int framerate)
275         : StreamProcessor(IsoStream::EST_Receive, port, framerate) {
276
277 }
278
279 ReceiveStreamProcessor::~ReceiveStreamProcessor() {
280
281 }
282
283 void ReceiveStreamProcessor::setVerboseLevel(int l) {
284         setDebugLevel(l);
285         StreamProcessor::setVerboseLevel(l);
286
287 }
288
289
290 TransmitStreamProcessor::TransmitStreamProcessor( int port, int framerate)
291         : StreamProcessor(IsoStream::EST_Transmit, port, framerate) {
292
293 }
294
295 TransmitStreamProcessor::~TransmitStreamProcessor() {
296
297 }
298
299 void TransmitStreamProcessor::setVerboseLevel(int l) {
300         setDebugLevel(l);
301         StreamProcessor::setVerboseLevel(l);
302
303 }
304
305 }
Note: See TracBrowser for help on using the browser.