1 |
/* |
---|
2 |
* Copyright (C) 2005-2007 by Jonathan Woithe |
---|
3 |
* Copyright (C) 2005-2007 by Pieter Palmers |
---|
4 |
* |
---|
5 |
* This file is part of FFADO |
---|
6 |
* FFADO = Free Firewire (pro-)audio drivers for linux |
---|
7 |
* |
---|
8 |
* FFADO is based upon FreeBoB. |
---|
9 |
* |
---|
10 |
* This library is free software; you can redistribute it and/or |
---|
11 |
* modify it under the terms of the GNU Lesser General Public |
---|
12 |
* License version 2.1, as published by the Free Software Foundation; |
---|
13 |
* |
---|
14 |
* This library 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 GNU |
---|
17 |
* Lesser General Public License for more details. |
---|
18 |
* |
---|
19 |
* You should have received a copy of the GNU Lesser General Public |
---|
20 |
* License along with this library; if not, write to the Free Software |
---|
21 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
---|
22 |
* MA 02110-1301 USA |
---|
23 |
*/ |
---|
24 |
|
---|
25 |
#include "MotuStreamProcessor.h" |
---|
26 |
#include "Port.h" |
---|
27 |
#include "MotuPort.h" |
---|
28 |
|
---|
29 |
#include <math.h> |
---|
30 |
|
---|
31 |
#include <netinet/in.h> |
---|
32 |
|
---|
33 |
#include "cycletimer.h" |
---|
34 |
|
---|
35 |
// in ticks |
---|
36 |
#define TRANSMIT_TRANSFER_DELAY 6000U |
---|
37 |
// the number of cycles to send a packet in advance of it's timestamp |
---|
38 |
#define TRANSMIT_ADVANCE_CYCLES 1U |
---|
39 |
|
---|
40 |
namespace Streaming { |
---|
41 |
|
---|
42 |
IMPL_DEBUG_MODULE( MotuTransmitStreamProcessor, MotuTransmitStreamProcessor, DEBUG_LEVEL_NORMAL ); |
---|
43 |
IMPL_DEBUG_MODULE( MotuReceiveStreamProcessor, MotuReceiveStreamProcessor, DEBUG_LEVEL_NORMAL ); |
---|
44 |
|
---|
45 |
// Set to 1 to enable the generation of a 1 kHz test tone in analog output 1 |
---|
46 |
#define TESTTONE 1 |
---|
47 |
|
---|
48 |
// A macro to extract specific bits from a native endian quadlet |
---|
49 |
#define get_bits(_d,_start,_len) (((_d)>>((_start)-(_len)+1)) & ((1<<(_len))-1)) |
---|
50 |
|
---|
51 |
// Convert an SPH timestamp as received from the MOTU to a full timestamp in ticks. |
---|
52 |
static inline uint32_t sphRecvToFullTicks(uint32_t sph, uint32_t ct_now) { |
---|
53 |
|
---|
54 |
uint32_t timestamp = CYCLE_TIMER_TO_TICKS(sph & 0x1ffffff); |
---|
55 |
uint32_t now_cycles = CYCLE_TIMER_GET_CYCLES(ct_now); |
---|
56 |
|
---|
57 |
uint32_t ts_sec = CYCLE_TIMER_GET_SECS(ct_now); |
---|
58 |
// If the cycles have wrapped, correct ts_sec so it represents when timestamp |
---|
59 |
// was received. The timestamps sent by the MOTU are always 1 or two cycles |
---|
60 |
// in advance of the cycle timer (reasons unknown at this stage). In addition, |
---|
61 |
// iso buffering can delay the arrival of packets for quite a number of cycles |
---|
62 |
// (have seen a delay >12 cycles). |
---|
63 |
// Every so often we also see sph wrapping ahead of ct_now, so deal with that |
---|
64 |
// too. |
---|
65 |
if (CYCLE_TIMER_GET_CYCLES(sph) > now_cycles + 1000) { |
---|
66 |
//debugOutput(DEBUG_LEVEL_VERBOSE, "now=%d, ct=%d\n", now_cycles, CYCLE_TIMER_GET_CYCLES(sph)); |
---|
67 |
if (ts_sec) |
---|
68 |
ts_sec--; |
---|
69 |
else |
---|
70 |
ts_sec = 127; |
---|
71 |
} else |
---|
72 |
if (now_cycles > CYCLE_TIMER_GET_CYCLES(sph) + 1000) { |
---|
73 |
//debugOutput(DEBUG_LEVEL_VERBOSE, "inverted wrap: now=%d, ct=%d\n", now_cycles, CYCLE_TIMER_GET_CYCLES(sph)); |
---|
74 |
if (ts_sec == 127) |
---|
75 |
ts_sec = 0; |
---|
76 |
else |
---|
77 |
ts_sec++; |
---|
78 |
} |
---|
79 |
return timestamp + ts_sec*TICKS_PER_SECOND; |
---|
80 |
} |
---|
81 |
|
---|
82 |
// Convert a full timestamp into an SPH timestamp as required by the MOTU |
---|
83 |
static inline uint32_t fullTicksToSph(int64_t timestamp) { |
---|
84 |
return TICKS_TO_CYCLE_TIMER(timestamp) & 0x1ffffff; |
---|
85 |
} |
---|
86 |
|
---|
87 |
/* transmit */ |
---|
88 |
MotuTransmitStreamProcessor::MotuTransmitStreamProcessor(int port, int framerate, |
---|
89 |
unsigned int event_size) |
---|
90 |
: TransmitStreamProcessor(port, framerate), m_event_size(event_size), |
---|
91 |
m_tx_dbc(0), |
---|
92 |
m_closedown_count(-1), m_streaming_active(0) { |
---|
93 |
} |
---|
94 |
|
---|
95 |
MotuTransmitStreamProcessor::~MotuTransmitStreamProcessor() { |
---|
96 |
|
---|
97 |
} |
---|
98 |
|
---|
99 |
bool MotuTransmitStreamProcessor::init() { |
---|
100 |
|
---|
101 |
debugOutput( DEBUG_LEVEL_VERBOSE, "Initializing (%p)...\n"); |
---|
102 |
// call the parent init |
---|
103 |
// this has to be done before allocating the buffers, |
---|
104 |
// because this sets the buffersizes from the processormanager |
---|
105 |
if(!TransmitStreamProcessor::init()) { |
---|
106 |
debugFatal("Could not do base class init (%p)\n",this); |
---|
107 |
return false; |
---|
108 |
} |
---|
109 |
|
---|
110 |
return true; |
---|
111 |
} |
---|
112 |
|
---|
113 |
void MotuTransmitStreamProcessor::setVerboseLevel(int l) { |
---|
114 |
setDebugLevel(l); // sets the debug level of the current object |
---|
115 |
TransmitStreamProcessor::setVerboseLevel(l); // also set the level of the base class |
---|
116 |
} |
---|
117 |
|
---|
118 |
|
---|
119 |
enum raw1394_iso_disposition |
---|
120 |
MotuTransmitStreamProcessor::getPacket(unsigned char *data, unsigned int *length, |
---|
121 |
unsigned char *tag, unsigned char *sy, |
---|
122 |
int cycle, unsigned int dropped, unsigned int max_length) { |
---|
123 |
|
---|
124 |
// FIXME: the actual delays in the system need to be worked out so |
---|
125 |
// we can get this thing synchronised. For now this seems to work. |
---|
126 |
int fc; |
---|
127 |
int64_t ts_head; |
---|
128 |
ffado_timestamp_t ts_tmp; |
---|
129 |
quadlet_t *quadlet = (quadlet_t *)data; |
---|
130 |
signed int i; |
---|
131 |
|
---|
132 |
// The number of events per packet expected by the MOTU is solely |
---|
133 |
// dependent on the current sample rate. An 'event' is one sample from |
---|
134 |
// all channels plus possibly other midi and control data. |
---|
135 |
signed n_events = m_framerate<=48000?8:(m_framerate<=96000?16:32); |
---|
136 |
|
---|
137 |
m_last_cycle=cycle; |
---|
138 |
|
---|
139 |
// determine if we want to send a packet or not |
---|
140 |
// note that we can't use getCycleTimer directly here, |
---|
141 |
// because packets are queued in advance. This means that |
---|
142 |
// we the packet we are constructing will be sent out |
---|
143 |
// on 'cycle', not 'now'. |
---|
144 |
unsigned int ctr=m_handler->getCycleTimer(); |
---|
145 |
int now_cycles = (int)CYCLE_TIMER_GET_CYCLES(ctr); |
---|
146 |
|
---|
147 |
// the difference between the cycle this |
---|
148 |
// packet is intended for and 'now' |
---|
149 |
int cycle_diff = diffCycles(cycle, now_cycles); |
---|
150 |
|
---|
151 |
//debugOutput(DEBUG_LEVEL_VERBOSE,"tx: enabled=%d, cycle=%d, now_cycles=%d, diff=%d\n", |
---|
152 |
// !m_is_disabled,cycle, now_cycles, cycle_diff); |
---|
153 |
|
---|
154 |
// Signal that streaming is still active |
---|
155 |
m_streaming_active = 1; |
---|
156 |
|
---|
157 |
// as long as the cycle parameter is not in sync with |
---|
158 |
// the current time, the stream is considered not |
---|
159 |
// to be 'running' |
---|
160 |
// NOTE: this works only at startup |
---|
161 |
if (!m_running && cycle_diff >= 0 && cycle >= 0) { |
---|
162 |
debugOutput(DEBUG_LEVEL_VERBOSE, "Xmit StreamProcessor %p started running at cycle %d\n",this, cycle); |
---|
163 |
m_running=true; |
---|
164 |
} |
---|
165 |
|
---|
166 |
if (!m_disabled && m_is_disabled) { |
---|
167 |
// this means that we are trying to enable |
---|
168 |
|
---|
169 |
// check if we are on or past the enable point |
---|
170 |
signed int cycles_past_enable=diffCycles(cycle, m_cycle_to_enable_at); |
---|
171 |
|
---|
172 |
if (cycles_past_enable >= 0) { |
---|
173 |
m_is_disabled=false; |
---|
174 |
|
---|
175 |
debugOutput(DEBUG_LEVEL_VERBOSE,"Enabling Tx StreamProcessor %p at %u\n", this, cycle); |
---|
176 |
|
---|
177 |
// initialize the buffer head & tail |
---|
178 |
m_SyncSource->m_data_buffer->getBufferHeadTimestamp(&ts_tmp, &fc); // thread safe |
---|
179 |
ts_head = (int64_t)ts_tmp; |
---|
180 |
|
---|
181 |
// the number of cycles the sync source lags (> 0) |
---|
182 |
// or leads (< 0) |
---|
183 |
int sync_lag_cycles=diffCycles(cycle, m_SyncSource->getLastCycle()); |
---|
184 |
|
---|
185 |
// account for the cycle lag between sync SP and this SP |
---|
186 |
// the last update of the sync source's timestamps was sync_lag_cycles |
---|
187 |
// cycles before the cycle we are calculating the timestamp for. |
---|
188 |
// if we were to use one-frame buffers, you would expect the |
---|
189 |
// frame that is sent on cycle CT to have a timestamp T1. |
---|
190 |
// ts_head however is for cycle CT-sync_lag_cycles, and lies |
---|
191 |
// therefore sync_lag_cycles * TICKS_PER_CYCLE earlier than |
---|
192 |
// T1. |
---|
193 |
ts_head = addTicks((int64_t)ts_head, sync_lag_cycles * TICKS_PER_CYCLE); |
---|
194 |
|
---|
195 |
// These are just copied from AmdtpStreamProcessor. At some point we should |
---|
196 |
// verify that they make sense for the MOTU. |
---|
197 |
ts_head = substractTicks((int64_t)ts_head, TICKS_PER_CYCLE); |
---|
198 |
// account for the number of cycles we are too late to enable |
---|
199 |
ts_head = addTicks((int64_t)ts_head, cycles_past_enable * TICKS_PER_CYCLE); |
---|
200 |
// account for one extra packet of frames |
---|
201 |
ts_head = substractTicks((int64_t)ts_head, |
---|
202 |
(uint32_t)((float)n_events * m_SyncSource->m_data_buffer->getRate())); |
---|
203 |
|
---|
204 |
m_data_buffer->setBufferTailTimestamp(ts_head); |
---|
205 |
|
---|
206 |
#ifdef DEBUG |
---|
207 |
if ((unsigned int)m_data_buffer->getFrameCounter() != m_data_buffer->getBufferSize()) { |
---|
208 |
debugWarning("m_data_buffer->getFrameCounter() != m_data_buffer->getBufferSize()\n"); |
---|
209 |
} |
---|
210 |
#endif |
---|
211 |
debugOutput(DEBUG_LEVEL_VERBOSE,"XMIT TS SET: TS=%10lld, LAG=%03d, FC=%4d\n", |
---|
212 |
ts_head, sync_lag_cycles, m_data_buffer->getFrameCounter()); |
---|
213 |
} else { |
---|
214 |
static int foo=0; |
---|
215 |
if (!foo) { |
---|
216 |
debugOutput(DEBUG_LEVEL_VERBOSE, |
---|
217 |
"will enable tx StreamProcessor %p at %u, now is %d\n", |
---|
218 |
this, m_cycle_to_enable_at, cycle); |
---|
219 |
foo=1; |
---|
220 |
} |
---|
221 |
debugOutput(DEBUG_LEVEL_VERY_VERBOSE, |
---|
222 |
"will enable StreamProcessor %p at %u, now is %d\n", |
---|
223 |
this, m_cycle_to_enable_at, cycle); |
---|
224 |
} |
---|
225 |
} else if (m_disabled && !m_is_disabled) { |
---|
226 |
// trying to disable |
---|
227 |
debugOutput(DEBUG_LEVEL_VERBOSE,"disabling StreamProcessor %p at %u\n", |
---|
228 |
this, cycle); |
---|
229 |
m_is_disabled=true; |
---|
230 |
} |
---|
231 |
|
---|
232 |
// Do housekeeping expected for all packets sent to the MOTU, even |
---|
233 |
// for packets containing no audio data. |
---|
234 |
*sy = 0x00; |
---|
235 |
*tag = 1; // All MOTU packets have a CIP-like header |
---|
236 |
|
---|
237 |
|
---|
238 |
// the base timestamp is the one of the next sample in the buffer |
---|
239 |
m_data_buffer->getBufferHeadTimestamp(&ts_tmp, &fc); // thread safe |
---|
240 |
ts_head = (int64_t)ts_tmp; |
---|
241 |
int64_t timestamp = ts_head; |
---|
242 |
|
---|
243 |
//debugOutput(DEBUG_LEVEL_VERBOSE,"tx cycle %d, base timestamp %lld\n",cycle, ts_head); |
---|
244 |
#if 0 |
---|
245 |
if (cycle<10000) { |
---|
246 |
debugOutput(DEBUG_LEVEL_VERBOSE,"cycle %d at %3d:%04d:%04d, timestamp=%3d:%04d:%04d (%d)\n", |
---|
247 |
cycle, |
---|
248 |
CYCLE_TIMER_GET_SECS(ctr), CYCLE_TIMER_GET_CYCLES(ctr), CYCLE_TIMER_GET_OFFSET(ctr), |
---|
249 |
TICKS_TO_SECS((uint32_t)timestamp), TICKS_TO_CYCLES((uint32_t)timestamp),TICKS_TO_OFFSET((uint32_t)timestamp),(uint32_t)timestamp); |
---|
250 |
} |
---|
251 |
#endif |
---|
252 |
// we send a packet some cycles in advance, to avoid the |
---|
253 |
// following situation: |
---|
254 |
// suppose we are only a few ticks away from |
---|
255 |
// the moment to send this packet. therefore we decide |
---|
256 |
// not to send the packet, but send it in the next cycle. |
---|
257 |
// This means that the next time point will be 3072 ticks |
---|
258 |
// later, making that the timestamp will be expired when the |
---|
259 |
// packet is sent, unless TRANSFER_DELAY > 3072. |
---|
260 |
// this means that we need at least one cycle of extra buffering. |
---|
261 |
int64_t ticks_to_advance = TICKS_PER_CYCLE * TRANSMIT_ADVANCE_CYCLES; |
---|
262 |
|
---|
263 |
// if cycle lies cycle_diff cycles in the future, we should |
---|
264 |
// queue this packet cycle_diff * TICKS_PER_CYCLE earlier than |
---|
265 |
// we would if it were to be sent immediately. |
---|
266 |
ticks_to_advance += (int64_t)cycle_diff * TICKS_PER_CYCLE; |
---|
267 |
|
---|
268 |
// determine the 'now' time in ticks |
---|
269 |
uint64_t cycle_timer=CYCLE_TIMER_TO_TICKS(ctr); |
---|
270 |
|
---|
271 |
// time until the packet is to be sent (if > 0: send packet) |
---|
272 |
//debugOutput(DEBUG_LEVEL_VERBOSE,"pre: timestamp=%lld, cycle_timer=%lld, adv=%lld, cdiff=%d\n", |
---|
273 |
// timestamp, cycle_timer,ticks_to_advance,cycle_diff); |
---|
274 |
int32_t until_next=diffTicks(timestamp, cycle_timer + ticks_to_advance); |
---|
275 |
//debugOutput(DEBUG_LEVEL_VERBOSE,"post\n"); |
---|
276 |
|
---|
277 |
until_next = (cycle >= TICKS_TO_CYCLES(timestamp))?-1:1; |
---|
278 |
|
---|
279 |
// Size of a single data frame in quadlets |
---|
280 |
unsigned dbs = m_event_size / 4; |
---|
281 |
|
---|
282 |
// don't process the stream when it is not enabled, not running |
---|
283 |
// or when the next sample is not due yet. |
---|
284 |
if((until_next>0) || m_is_disabled || !m_running) { |
---|
285 |
// send dummy packet |
---|
286 |
|
---|
287 |
// construct the packet CIP-like header. Even if this is a data-less |
---|
288 |
// packet the dbs field is still set as if there were data blocks |
---|
289 |
// present. For data-less packets the dbc is the same as the previously |
---|
290 |
// transmitted block. |
---|
291 |
*quadlet = htonl(0x00000400 | ((getNodeId()&0x3f)<<24) | m_tx_dbc | (dbs<<16)); |
---|
292 |
quadlet++; |
---|
293 |
*quadlet = htonl(0x8222ffff); |
---|
294 |
quadlet++; |
---|
295 |
*length = 8; |
---|
296 |
|
---|
297 |
#warning high-pitched sound protection removed! |
---|
298 |
// In the disabled state simply zero all data sent to the MOTU. If |
---|
299 |
// a stream of empty packets are sent once iso streaming is enabled |
---|
300 |
// the MOTU tends to emit high-pitched audio (approx 10 kHz) for |
---|
301 |
// some reason. This is not completely sufficient, however (zeroed |
---|
302 |
// packets must also be sent on iso closedown). |
---|
303 |
|
---|
304 |
// FIXME: Currently we simply send empty packets to the MOTU when |
---|
305 |
// the stream is disabled so the "m_disabled == 0" code is never |
---|
306 |
// executed. However, this may change in future so it's left in |
---|
307 |
// for the moment for reference. |
---|
308 |
// FIXME: Currently we don't read the buffer at all during closedown. |
---|
309 |
// We could (and silently junk the contents) if it turned out to be |
---|
310 |
// more helpful. |
---|
311 |
|
---|
312 |
//if (!m_is_disabled && m_running) |
---|
313 |
// return RAW1394_ISO_OK; |
---|
314 |
//else |
---|
315 |
return RAW1394_ISO_DEFER; |
---|
316 |
} |
---|
317 |
|
---|
318 |
// add the transmit transfer delay to construct the playout time |
---|
319 |
ffado_timestamp_t ts=addTicks(timestamp, TRANSMIT_TRANSFER_DELAY); |
---|
320 |
|
---|
321 |
if (m_data_buffer->readFrames(n_events, (char *)(data + 8))) { |
---|
322 |
|
---|
323 |
// Increment the dbc (data block count). This is only done if the |
---|
324 |
// packet will contain events - that is, we are due to send some |
---|
325 |
// data. Otherwise a pad packet is sent which contains the DBC of |
---|
326 |
// the previously sent packet. This regime also means that the very |
---|
327 |
// first packet containing data will have a DBC of n_events, which |
---|
328 |
// matches what is observed from other systems. |
---|
329 |
m_tx_dbc += n_events; |
---|
330 |
if (m_tx_dbc > 0xff) |
---|
331 |
m_tx_dbc -= 0x100; |
---|
332 |
|
---|
333 |
// construct the packet CIP-like header. Even if this is a data-less |
---|
334 |
// packet the dbs field is still set as if there were data blocks |
---|
335 |
// present. For data-less packets the dbc is the same as the previously |
---|
336 |
// transmitted block. |
---|
337 |
*quadlet = htonl(0x00000400 | ((getNodeId()&0x3f)<<24) | m_tx_dbc | (dbs<<16)); |
---|
338 |
quadlet++; |
---|
339 |
*quadlet = htonl(0x8222ffff); |
---|
340 |
quadlet++; |
---|
341 |
|
---|
342 |
*length = n_events*m_event_size + 8; |
---|
343 |
|
---|
344 |
// FIXME: if we choose to read the buffer even during closedown, |
---|
345 |
// here is where the data is silenced. |
---|
346 |
// if (m_closedown_count >= 0) |
---|
347 |
// memset(data+8, 0, read_size); |
---|
348 |
if (m_closedown_count > 0) |
---|
349 |
m_closedown_count--; |
---|
350 |
|
---|
351 |
// Set up each frames's SPH. Note that the (int) typecast |
---|
352 |
// appears to do rounding. |
---|
353 |
|
---|
354 |
float ticks_per_frame = m_SyncSource->m_data_buffer->getRate(); |
---|
355 |
//debugOutput(DEBUG_LEVEL_VERBOSE, "ticks per frame=%10.6f\n",ticks_per_frame); |
---|
356 |
for (i=0; i<n_events; i++, quadlet += dbs) { |
---|
357 |
//FIXME: not sure which is best for the MOTU |
---|
358 |
// int64_t ts_frame = addTicks(ts, (unsigned int)(i * ticks_per_frame)); |
---|
359 |
int64_t ts_frame = addTicks(timestamp, (unsigned int)(i * ticks_per_frame)); |
---|
360 |
*quadlet = htonl(fullTicksToSph(ts_frame)); |
---|
361 |
#if 0 |
---|
362 |
if (i==0) { |
---|
363 |
debugOutput(DEBUG_LEVEL_VERBOSE," ts_frame=%8x (%3d:%04d:%04d)\n",ts_frame, |
---|
364 |
TICKS_TO_SECS(ts_frame), TICKS_TO_CYCLES(ts_frame), TICKS_TO_OFFSET(ts_frame)); |
---|
365 |
} |
---|
366 |
#endif |
---|
367 |
#if 0 |
---|
368 |
if (cycle<2) { |
---|
369 |
debugOutput(DEBUG_LEVEL_VERBOSE,"cycle %d: %d %d %d\n", |
---|
370 |
cycle, |
---|
371 |
TICKS_TO_SECS(ts_frame), |
---|
372 |
TICKS_TO_CYCLES(ts_frame), |
---|
373 |
TICKS_TO_OFFSET(ts_frame)); |
---|
374 |
} |
---|
375 |
#endif |
---|
376 |
#if TESTTONE |
---|
377 |
// FIXME: remove this hacked in 1 kHz test signal to |
---|
378 |
// analog-1 when testing is complete. Note that the tone is |
---|
379 |
// *never* added during closedown. |
---|
380 |
if (m_closedown_count<0) { |
---|
381 |
static signed int a_cx = 0; |
---|
382 |
signed int val; |
---|
383 |
val = (int)(0x7fffff*sin(1000.0*2.0*M_PI*(a_cx/24576000.0))); |
---|
384 |
if ((a_cx+=512) >= 24576000) { |
---|
385 |
a_cx -= 24576000; |
---|
386 |
} |
---|
387 |
*(data+8+i*m_event_size+16) = (val >> 16) & 0xff; |
---|
388 |
*(data+8+i*m_event_size+17) = (val >> 8) & 0xff; |
---|
389 |
*(data+8+i*m_event_size+18) = val & 0xff; |
---|
390 |
} |
---|
391 |
#endif |
---|
392 |
|
---|
393 |
} |
---|
394 |
|
---|
395 |
// Process all ports that should be handled on a per-packet base |
---|
396 |
// this is MIDI for AMDTP (due to the need of DBC, which is lost |
---|
397 |
// when putting the events in the ringbuffer) |
---|
398 |
// for motu this might also be control data, however as control |
---|
399 |
// data isn't time specific I would also include it in the period |
---|
400 |
// based processing |
---|
401 |
|
---|
402 |
// FIXME: m_tx_dbc probably needs to be initialised to a non-zero |
---|
403 |
// value somehow so MIDI sync is possible. For now we ignore |
---|
404 |
// this issue. |
---|
405 |
if (!encodePacketPorts((quadlet_t *)(data+8), n_events, m_tx_dbc)) { |
---|
406 |
debugWarning("Problem encoding Packet Ports\n"); |
---|
407 |
} |
---|
408 |
|
---|
409 |
return RAW1394_ISO_OK; |
---|
410 |
|
---|
411 |
} else if (now_cycles<cycle) { |
---|
412 |
// we can still postpone the queueing of the packets |
---|
413 |
return RAW1394_ISO_AGAIN; |
---|
414 |
} else { // there is no more data in the ringbuffer |
---|
415 |
|
---|
416 |
// FIXME: port to new timestamp type |
---|
417 |
// debugWarning("Transmit buffer underrun (now %d, queue %d, target %d)\n", |
---|
418 |
// now_cycles, cycle, TICKS_TO_CYCLES(ts)); |
---|
419 |
|
---|
420 |
// signal underrun |
---|
421 |
m_xruns++; |
---|
422 |
|
---|
423 |
// disable the processing, will be re-enabled when |
---|
424 |
// the xrun is handled |
---|
425 |
m_disabled=true; |
---|
426 |
m_is_disabled=true; |
---|
427 |
|
---|
428 |
// compose a no-data packet, we should always |
---|
429 |
// send a valid packet |
---|
430 |
|
---|
431 |
// send dummy packet |
---|
432 |
|
---|
433 |
// construct the packet CIP-like header. Even if this is a data-less |
---|
434 |
// packet the dbs field is still set as if there were data blocks |
---|
435 |
// present. For data-less packets the dbc is the same as the previously |
---|
436 |
// transmitted block. |
---|
437 |
*quadlet = htonl(0x00000400 | ((getNodeId()&0x3f)<<24) | m_tx_dbc | (dbs<<16)); |
---|
438 |
quadlet++; |
---|
439 |
*quadlet = htonl(0x8222ffff); |
---|
440 |
quadlet++; |
---|
441 |
*length = 8; |
---|
442 |
|
---|
443 |
return RAW1394_ISO_DEFER; |
---|
444 |
} |
---|
445 |
|
---|
446 |
// we shouldn't get here |
---|
447 |
return RAW1394_ISO_ERROR; |
---|
448 |
|
---|
449 |
} |
---|
450 |
|
---|
451 |
int MotuTransmitStreamProcessor::getMinimalSyncDelay() { |
---|
452 |
return 0; |
---|
453 |
} |
---|
454 |
|
---|
455 |
bool MotuTransmitStreamProcessor::prefill() { |
---|
456 |
// this is needed because otherwise there is no data to be |
---|
457 |
// sent when the streaming starts |
---|
458 |
|
---|
459 |
int i = m_nb_buffers; |
---|
460 |
while (i--) { |
---|
461 |
if(!transferSilence(m_period)) { |
---|
462 |
debugFatal("Could not prefill transmit stream\n"); |
---|
463 |
return false; |
---|
464 |
} |
---|
465 |
} |
---|
466 |
return true; |
---|
467 |
} |
---|
468 |
|
---|
469 |
bool MotuTransmitStreamProcessor::reset() { |
---|
470 |
|
---|
471 |
debugOutput( DEBUG_LEVEL_VERBOSE, "Resetting...\n"); |
---|
472 |
|
---|
473 |
// we have to make sure that the buffer HEAD timestamp |
---|
474 |
// lies in the future for every possible buffer fill case. |
---|
475 |
int offset=(int)(m_data_buffer->getBufferSize()*m_ticks_per_frame); |
---|
476 |
|
---|
477 |
m_data_buffer->setTickOffset(offset); |
---|
478 |
|
---|
479 |
// reset all non-device specific stuff |
---|
480 |
// i.e. the iso stream and the associated ports |
---|
481 |
if (!TransmitStreamProcessor::reset()) { |
---|
482 |
debugFatal("Could not do base class reset\n"); |
---|
483 |
return false; |
---|
484 |
} |
---|
485 |
|
---|
486 |
// we should prefill the event buffer |
---|
487 |
if (!prefill()) { |
---|
488 |
debugFatal("Could not prefill buffers\n"); |
---|
489 |
return false; |
---|
490 |
} |
---|
491 |
|
---|
492 |
return true; |
---|
493 |
} |
---|
494 |
|
---|
495 |
bool MotuTransmitStreamProcessor::prepare() { |
---|
496 |
|
---|
497 |
debugOutput( DEBUG_LEVEL_VERBOSE, "Preparing...\n"); |
---|
498 |
|
---|
499 |
// prepare all non-device specific stuff |
---|
500 |
// i.e. the iso stream and the associated ports |
---|
501 |
if (!TransmitStreamProcessor::prepare()) { |
---|
502 |
debugFatal("Could not prepare base class\n"); |
---|
503 |
return false; |
---|
504 |
} |
---|
505 |
|
---|
506 |
m_PeriodStat.setName("XMT PERIOD"); |
---|
507 |
m_PacketStat.setName("XMT PACKET"); |
---|
508 |
m_WakeupStat.setName("XMT WAKEUP"); |
---|
509 |
|
---|
510 |
debugOutput( DEBUG_LEVEL_VERBOSE, "Event size: %d\n", m_event_size); |
---|
511 |
|
---|
512 |
// allocate the event buffer |
---|
513 |
unsigned int ringbuffer_size_frames=m_nb_buffers * m_period; |
---|
514 |
|
---|
515 |
// allocate the internal buffer |
---|
516 |
m_ticks_per_frame = (TICKS_PER_SECOND*1.0) / ((float)m_framerate); |
---|
517 |
|
---|
518 |
assert(m_data_buffer); |
---|
519 |
// Note: terminology is slightly confused here. From the point of view |
---|
520 |
// of the buffer the event size is the size of a single complete "event" |
---|
521 |
// in the MOTU datastream, which consists of one sample from each audio |
---|
522 |
// channel plus a timestamp and other control data. Almost by |
---|
523 |
// definition then, the buffer's "events per frame" must be 1. With |
---|
524 |
// these values, data copies to/from the MOTU data stream can be handled |
---|
525 |
// by the generic copying functions. |
---|
526 |
m_data_buffer->setBufferSize(ringbuffer_size_frames); |
---|
527 |
m_data_buffer->setEventSize(m_event_size); |
---|
528 |
m_data_buffer->setEventsPerFrame(1); |
---|
529 |
|
---|
530 |
m_data_buffer->setUpdatePeriod(m_period); |
---|
531 |
m_data_buffer->setNominalRate(m_ticks_per_frame); |
---|
532 |
|
---|
533 |
// FIXME: check if the timestamp wraps at one second |
---|
534 |
m_data_buffer->setWrapValue(128L*TICKS_PER_SECOND); |
---|
535 |
|
---|
536 |
m_data_buffer->prepare(); |
---|
537 |
|
---|
538 |
// Set the parameters of ports we can: we want the audio ports to be |
---|
539 |
// period buffered, and the midi ports to be packet buffered. |
---|
540 |
for ( PortVectorIterator it = m_Ports.begin(); |
---|
541 |
it != m_Ports.end(); |
---|
542 |
++it ) { |
---|
543 |
debugOutput(DEBUG_LEVEL_VERBOSE, "Setting up port %s\n",(*it)->getName().c_str()); |
---|
544 |
if(!(*it)->setBufferSize(m_period)) { |
---|
545 |
debugFatal("Could not set buffer size to %d\n",m_period); |
---|
546 |
return false; |
---|
547 |
} |
---|
548 |
|
---|
549 |
switch ((*it)->getPortType()) { |
---|
550 |
case Port::E_Audio: |
---|
551 |
if (!(*it)->setSignalType(Port::E_PeriodSignalled)) { |
---|
552 |
debugFatal("Could not set signal type to PeriodSignalling"); |
---|
553 |
return false; |
---|
554 |
} |
---|
555 |
break; |
---|
556 |
|
---|
557 |
case Port::E_Midi: |
---|
558 |
if (!(*it)->setSignalType(Port::E_PacketSignalled)) { |
---|
559 |
debugFatal("Could not set signal type to PacketSignalling"); |
---|
560 |
return false; |
---|
561 |
} |
---|
562 |
if (!(*it)->setBufferType(Port::E_RingBuffer)) { |
---|
563 |
debugFatal("Could not set buffer type"); |
---|
564 |
return false; |
---|
565 |
} |
---|
566 |
if (!(*it)->setDataType(Port::E_MidiEvent)) { |
---|
567 |
debugFatal("Could not set data type"); |
---|
568 |
return false; |
---|
569 |
} |
---|
570 |
// FIXME: probably need rate control too. See |
---|
571 |
// Port::useRateControl() and AmdtpStreamProcessor. |
---|
572 |
break; |
---|
573 |
|
---|
574 |
case Port::E_Control: |
---|
575 |
if (!(*it)->setSignalType(Port::E_PeriodSignalled)) { |
---|
576 |
debugFatal("Could not set signal type to PeriodSignalling"); |
---|
577 |
return false; |
---|
578 |
} |
---|
579 |
break; |
---|
580 |
|
---|
581 |
default: |
---|
582 |
debugWarning("Unsupported port type specified\n"); |
---|
583 |
break; |
---|
584 |
} |
---|
585 |
} |
---|
586 |
|
---|
587 |
// The API specific settings of the ports are already set before |
---|
588 |
// this routine is called, therefore we can init&prepare the ports |
---|
589 |
if (!initPorts()) { |
---|
590 |
debugFatal("Could not initialize ports!\n"); |
---|
591 |
return false; |
---|
592 |
} |
---|
593 |
|
---|
594 |
if(!preparePorts()) { |
---|
595 |
debugFatal("Could not initialize ports!\n"); |
---|
596 |
return false; |
---|
597 |
} |
---|
598 |
|
---|
599 |
return true; |
---|
600 |
} |
---|
601 |
|
---|
602 |
bool MotuTransmitStreamProcessor::prepareForStop() { |
---|
603 |
|
---|
604 |
// If the stream is disabled or isn't running there's no need to |
---|
605 |
// wait since the MOTU *should* still be in a "zero data" state. |
---|
606 |
// |
---|
607 |
// If the m_streaming_active flag is 0 it indicates that the |
---|
608 |
// transmit callback hasn't been called since a closedown was |
---|
609 |
// requested when this function was last called. This effectively |
---|
610 |
// signifies that the streaming thread has been exitted due to an |
---|
611 |
// xrun in either the receive or transmit handlers. In this case |
---|
612 |
// there's no point in waiting for the closedown count to hit zero |
---|
613 |
// because it never will; the zero data will never get to the MOTU. |
---|
614 |
// It's best to allow an immediate stop and let the xrun handler |
---|
615 |
// proceed as best it can. |
---|
616 |
// |
---|
617 |
// The ability to detect the lack of streaming also prevents the |
---|
618 |
// "wait for stop" in the stream processor manager's stop() method |
---|
619 |
// from hitting its timeout which in turn seems to increase the |
---|
620 |
// probability of a successful recovery. |
---|
621 |
if (m_is_disabled || !isRunning() || !m_streaming_active) |
---|
622 |
return true; |
---|
623 |
|
---|
624 |
if (m_closedown_count < 0) { |
---|
625 |
// No closedown has been initiated, so start one now. Set |
---|
626 |
// the closedown count to the number of zero packets which |
---|
627 |
// will be sent to the MOTU before closing off the iso |
---|
628 |
// streams. FIXME: 128 packets (each containing 8 frames at |
---|
629 |
// 48 kHz) is the experimentally-determined figure for 48 |
---|
630 |
// kHz with a period size of 1024. It seems that at least |
---|
631 |
// one period of zero samples need to be sent to allow for |
---|
632 |
// inter-thread communication occuring on period boundaries. |
---|
633 |
// This needs to be confirmed for other rates and period |
---|
634 |
// sizes. |
---|
635 |
signed n_events = m_framerate<=48000?8:(m_framerate<=96000?16:32); |
---|
636 |
m_closedown_count = m_period / n_events; |
---|
637 |
|
---|
638 |
// Set up a test to confirm that streaming is still active. |
---|
639 |
// If the streaming function hasn't been called by the next |
---|
640 |
// iteration through this function there's no point in |
---|
641 |
// continuing since it means the zero data will never get to |
---|
642 |
// the MOTU. |
---|
643 |
m_streaming_active = 0; |
---|
644 |
return false; |
---|
645 |
} |
---|
646 |
|
---|
647 |
// We are "go" for closedown once all requested zero packets |
---|
648 |
// (initiated by a previous call to this function) have been sent to |
---|
649 |
// the MOTU. |
---|
650 |
return m_closedown_count == 0; |
---|
651 |
} |
---|
652 |
|
---|
653 |
bool MotuTransmitStreamProcessor::prepareForStart() { |
---|
654 |
// Reset some critical variables required so the stream starts cleanly. This |
---|
655 |
// method is called once on every stream restart. Initialisations which should |
---|
656 |
// be done once should be placed in the init() method instead. |
---|
657 |
m_running = 0; |
---|
658 |
m_closedown_count = -1; |
---|
659 |
m_streaming_active = 0; |
---|
660 |
|
---|
661 |
// At this point we'll also disable the stream processor here. |
---|
662 |
// At this stage stream processors are always explicitly re-enabled |
---|
663 |
// after being started, so by starting in the disabled state we |
---|
664 |
// ensure that every start will be exactly the same. |
---|
665 |
disable(); |
---|
666 |
|
---|
667 |
return true; |
---|
668 |
} |
---|
669 |
|
---|
670 |
bool MotuTransmitStreamProcessor::prepareForEnable(uint64_t time_to_enable_at) { |
---|
671 |
|
---|
672 |
debugOutput(DEBUG_LEVEL_VERBOSE,"Preparing to enable...\n"); |
---|
673 |
|
---|
674 |
// for the transmit SP, we have to initialize the |
---|
675 |
// buffer timestamp to something sane, because this timestamp |
---|
676 |
// is used when it is SyncSource |
---|
677 |
|
---|
678 |
// the time we initialize to will determine the time at which |
---|
679 |
// the first sample in the buffer will be sent, so we should |
---|
680 |
// make it at least 'time_to_enable_at' |
---|
681 |
|
---|
682 |
uint64_t now=m_handler->getCycleTimer(); |
---|
683 |
unsigned int now_secs=CYCLE_TIMER_GET_SECS(now); |
---|
684 |
|
---|
685 |
// check if a wraparound on the secs will happen between |
---|
686 |
// now and the time we start |
---|
687 |
int until_enable=(int)time_to_enable_at - (int)CYCLE_TIMER_GET_CYCLES(now); |
---|
688 |
|
---|
689 |
if(until_enable>4000) { |
---|
690 |
// wraparound on CYCLE_TIMER_GET_CYCLES(now) |
---|
691 |
// this means that we are late starting up, |
---|
692 |
// and that the start lies in the previous second |
---|
693 |
if (now_secs==0) now_secs=127; |
---|
694 |
else now_secs--; |
---|
695 |
} else if (until_enable<-4000) { |
---|
696 |
// wraparound on time_to_enable_at |
---|
697 |
// this means that we are early and that the start |
---|
698 |
// point lies in the next second |
---|
699 |
now_secs++; |
---|
700 |
if (now_secs>=128) now_secs=0; |
---|
701 |
} |
---|
702 |
|
---|
703 |
//// uint64_t ts_head= now_secs*TICKS_PER_SECOND; |
---|
704 |
// uint64_t ts_head = time_to_enable_at*TICKS_PER_CYCLE; |
---|
705 |
uint64_t ts_head= now_secs*TICKS_PER_SECOND; |
---|
706 |
ts_head+=time_to_enable_at*TICKS_PER_CYCLE; |
---|
707 |
|
---|
708 |
// we also add the nb of cycles we transmit in advance |
---|
709 |
ts_head=addTicks(ts_head, TRANSMIT_ADVANCE_CYCLES*TICKS_PER_CYCLE); |
---|
710 |
|
---|
711 |
m_data_buffer->setBufferTailTimestamp(ts_head); |
---|
712 |
|
---|
713 |
if (!StreamProcessor::prepareForEnable(time_to_enable_at)) { |
---|
714 |
debugError("StreamProcessor::prepareForEnable failed\n"); |
---|
715 |
return false; |
---|
716 |
} |
---|
717 |
|
---|
718 |
return true; |
---|
719 |
} |
---|
720 |
|
---|
721 |
bool MotuTransmitStreamProcessor::transferSilence(unsigned int size) { |
---|
722 |
bool retval; |
---|
723 |
|
---|
724 |
// This function should tranfer 'size' frames of 'silence' to the event buffer |
---|
725 |
char *dummybuffer=(char *)calloc(size,m_event_size); |
---|
726 |
|
---|
727 |
transmitSilenceBlock(dummybuffer, size, 0); |
---|
728 |
|
---|
729 |
// add the silence data to the ringbuffer |
---|
730 |
if(m_data_buffer->writeFrames(size, dummybuffer, 0)) { |
---|
731 |
retval=true; |
---|
732 |
} else { |
---|
733 |
debugWarning("Could not write to event buffer\n"); |
---|
734 |
retval=false; |
---|
735 |
} |
---|
736 |
|
---|
737 |
free(dummybuffer); |
---|
738 |
|
---|
739 |
return retval; |
---|
740 |
} |
---|
741 |
|
---|
742 |
bool MotuTransmitStreamProcessor::putFrames(unsigned int nbframes, int64_t ts) { |
---|
743 |
m_PeriodStat.mark(m_data_buffer->getBufferFill()); |
---|
744 |
|
---|
745 |
debugOutput(DEBUG_LEVEL_VERY_VERBOSE, "MotuTransmitStreamProcessor::putFrames(%d, %llu)\n", nbframes, ts); |
---|
746 |
|
---|
747 |
// transfer the data |
---|
748 |
#if 0 |
---|
749 |
debugOutput(DEBUG_LEVEL_VERBOSE, "1 - timestamp is %d\n", ts); |
---|
750 |
#endif |
---|
751 |
m_data_buffer->blockProcessWriteFrames(nbframes, ts); |
---|
752 |
#if 0 |
---|
753 |
debugOutput(DEBUG_LEVEL_VERBOSE, " done\n"); |
---|
754 |
#endif |
---|
755 |
debugOutput(DEBUG_LEVEL_VERY_VERBOSE, " New timestamp: %llu\n", ts); |
---|
756 |
|
---|
757 |
return true; |
---|
758 |
} |
---|
759 |
|
---|
760 |
/* |
---|
761 |
* write received events to the stream ringbuffers. |
---|
762 |
*/ |
---|
763 |
|
---|
764 |
bool MotuTransmitStreamProcessor::processWriteBlock(char *data, |
---|
765 |
unsigned int nevents, unsigned int offset) { |
---|
766 |
bool no_problem=true; |
---|
767 |
unsigned int i; |
---|
768 |
|
---|
769 |
// FIXME: ensure the MIDI and control streams are all zeroed until |
---|
770 |
// such time as they are fully implemented. |
---|
771 |
for (i=0; i<nevents; i++) { |
---|
772 |
memset(data+4+i*m_event_size, 0x00, 6); |
---|
773 |
} |
---|
774 |
|
---|
775 |
for ( PortVectorIterator it = m_PeriodPorts.begin(); |
---|
776 |
it != m_PeriodPorts.end(); |
---|
777 |
++it ) { |
---|
778 |
// If this port is disabled, don't process it |
---|
779 |
if((*it)->isDisabled()) {continue;}; |
---|
780 |
|
---|
781 |
//FIXME: make this into a static_cast when not DEBUG? |
---|
782 |
Port *port=dynamic_cast<Port *>(*it); |
---|
783 |
|
---|
784 |
switch(port->getPortType()) { |
---|
785 |
|
---|
786 |
case Port::E_Audio: |
---|
787 |
if (encodePortToMotuEvents(static_cast<MotuAudioPort *>(*it), (quadlet_t *)data, offset, nevents)) { |
---|
788 |
debugWarning("Could not encode port %s to MBLA events",(*it)->getName().c_str()); |
---|
789 |
no_problem=false; |
---|
790 |
} |
---|
791 |
break; |
---|
792 |
// midi is a packet based port, don't process |
---|
793 |
// case MotuPortInfo::E_Midi: |
---|
794 |
// break; |
---|
795 |
|
---|
796 |
default: // ignore |
---|
797 |
break; |
---|
798 |
} |
---|
799 |
} |
---|
800 |
return no_problem; |
---|
801 |
} |
---|
802 |
|
---|
803 |
int MotuTransmitStreamProcessor::transmitSilenceBlock(char *data, |
---|
804 |
unsigned int nevents, unsigned int offset) { |
---|
805 |
// This is the same as the non-silence version, except that is |
---|
806 |
// doesn't read from the port buffers. |
---|
807 |
|
---|
808 |
int problem=0; |
---|
809 |
|
---|
810 |
for ( PortVectorIterator it = m_PeriodPorts.begin(); |
---|
811 |
it != m_PeriodPorts.end(); |
---|
812 |
++it ) { |
---|
813 |
//FIXME: make this into a static_cast when not DEBUG? |
---|
814 |
Port *port=dynamic_cast<Port *>(*it); |
---|
815 |
|
---|
816 |
switch(port->getPortType()) { |
---|
817 |
|
---|
818 |
case Port::E_Audio: |
---|
819 |
if (encodeSilencePortToMotuEvents(static_cast<MotuAudioPort *>(*it), (quadlet_t *)data, offset, nevents)) { |
---|
820 |
debugWarning("Could not encode port %s to MBLA events",(*it)->getName().c_str()); |
---|
821 |
problem=1; |
---|
822 |
} |
---|
823 |
break; |
---|
824 |
// midi is a packet based port, don't process |
---|
825 |
// case MotuPortInfo::E_Midi: |
---|
826 |
// break; |
---|
827 |
|
---|
828 |
default: // ignore |
---|
829 |
break; |
---|
830 |
} |
---|
831 |
} |
---|
832 |
return problem; |
---|
833 |
} |
---|
834 |
|
---|
835 |
/** |
---|
836 |
* @brief decode a packet for the packet-based ports |
---|
837 |
* |
---|
838 |
* @param data Packet data |
---|
839 |
* @param nevents number of events in data (including events of other ports & port types) |
---|
840 |
* @param dbc DataBlockCount value for this packet |
---|
841 |
* @return true if all successfull |
---|
842 |
*/ |
---|
843 |
bool MotuTransmitStreamProcessor::encodePacketPorts(quadlet_t *data, unsigned int nevents, |
---|
844 |
unsigned int dbc) { |
---|
845 |
bool ok=true; |
---|
846 |
char byte; |
---|
847 |
|
---|
848 |
// Use char here since the target address won't necessarily be |
---|
849 |
// aligned; use of an unaligned quadlet_t may cause issues on |
---|
850 |
// certain architectures. Besides, the target for MIDI data going |
---|
851 |
// directly to the MOTU isn't structured in quadlets anyway; it is a |
---|
852 |
// sequence of 3 unaligned bytes. |
---|
853 |
unsigned char *target = NULL; |
---|
854 |
|
---|
855 |
for ( PortVectorIterator it = m_PacketPorts.begin(); |
---|
856 |
it != m_PacketPorts.end(); |
---|
857 |
++it ) { |
---|
858 |
|
---|
859 |
Port *port=static_cast<Port *>(*it); |
---|
860 |
assert(port); // this should not fail!! |
---|
861 |
|
---|
862 |
// Currently the only packet type of events for MOTU |
---|
863 |
// is MIDI in mbla. However in future control data |
---|
864 |
// might also be sent via "packet" events. |
---|
865 |
// assert(pinfo->getFormat()==MotuPortInfo::E_Midi); |
---|
866 |
|
---|
867 |
// FIXME: MIDI output is completely untested at present. |
---|
868 |
switch (port->getPortType()) { |
---|
869 |
case Port::E_Midi: { |
---|
870 |
MotuMidiPort *mp=static_cast<MotuMidiPort *>(*it); |
---|
871 |
|
---|
872 |
// Send a byte if we can. MOTU MIDI data is |
---|
873 |
// sent using a 3-byte sequence starting at |
---|
874 |
// the port's position. For now we'll |
---|
875 |
// always send in the first event of a |
---|
876 |
// packet, but this might need refinement |
---|
877 |
// later. |
---|
878 |
if (mp->canRead()) { |
---|
879 |
mp->readEvent(&byte); |
---|
880 |
target = (unsigned char *)data + mp->getPosition(); |
---|
881 |
*(target++) = 0x01; |
---|
882 |
*(target++) = 0x00; |
---|
883 |
*(target++) = byte; |
---|
884 |
} |
---|
885 |
break; |
---|
886 |
} |
---|
887 |
default: |
---|
888 |
debugOutput(DEBUG_LEVEL_VERBOSE, "Unknown packet-type port type %d\n",port->getPortType()); |
---|
889 |
return ok; |
---|
890 |
} |
---|
891 |
} |
---|
892 |
|
---|
893 |
return ok; |
---|
894 |
} |
---|
895 |
|
---|
896 |
int MotuTransmitStreamProcessor::encodePortToMotuEvents(MotuAudioPort *p, quadlet_t *data, |
---|
897 |
unsigned int offset, unsigned int nevents) { |
---|
898 |
// Encodes nevents worth of data from the given port into the given buffer. The |
---|
899 |
// format of the buffer is precisely that which will be sent to the MOTU. |
---|
900 |
// The basic idea: |
---|
901 |
// iterate over the ports |
---|
902 |
// * get port buffer address |
---|
903 |
// * loop over events |
---|
904 |
// - pick right sample in event based upon PortInfo |
---|
905 |
// - convert sample from Port format (E_Int24, E_Float, ..) to MOTU |
---|
906 |
// native format |
---|
907 |
// |
---|
908 |
// We include the ability to start the transfer from the given offset within |
---|
909 |
// the port (expressed in frames) so the 'efficient' transfer method can be |
---|
910 |
// utilised. |
---|
911 |
|
---|
912 |
unsigned int j=0; |
---|
913 |
|
---|
914 |
// Use char here since the target address won't necessarily be |
---|
915 |
// aligned; use of an unaligned quadlet_t may cause issues on certain |
---|
916 |
// architectures. Besides, the target (data going directly to the MOTU) |
---|
917 |
// isn't structured in quadlets anyway; it mainly consists of packed |
---|
918 |
// 24-bit integers. |
---|
919 |
unsigned char *target; |
---|
920 |
target = (unsigned char *)data + p->getPosition(); |
---|
921 |
|
---|
922 |
switch(p->getDataType()) { |
---|
923 |
default: |
---|
924 |
case Port::E_Int24: |
---|
925 |
{ |
---|
926 |
quadlet_t *buffer=(quadlet_t *)(p->getBufferAddress()); |
---|
927 |
|
---|
928 |
assert(nevents + offset <= p->getBufferSize()); |
---|
929 |
|
---|
930 |
// Offset is in frames, but each port is only a single |
---|
931 |
// channel, so the number of frames is the same as the |
---|
932 |
// number of quadlets to offset (assuming the port buffer |
---|
933 |
// uses one quadlet per sample, which is the case currently). |
---|
934 |
buffer+=offset; |
---|
935 |
|
---|
936 |
for(j = 0; j < nevents; j += 1) { // Decode nsamples |
---|
937 |
*target = (*buffer >> 16) & 0xff; |
---|
938 |
*(target+1) = (*buffer >> 8) & 0xff; |
---|
939 |
*(target+2) = (*buffer) & 0xff; |
---|
940 |
|
---|
941 |
buffer++; |
---|
942 |
target+=m_event_size; |
---|
943 |
} |
---|
944 |
} |
---|
945 |
break; |
---|
946 |
case Port::E_Float: |
---|
947 |
{ |
---|
948 |
const float multiplier = (float)(0x7FFFFF); |
---|
949 |
float *buffer=(float *)(p->getBufferAddress()); |
---|
950 |
|
---|
951 |
assert(nevents + offset <= p->getBufferSize()); |
---|
952 |
|
---|
953 |
buffer+=offset; |
---|
954 |
|
---|
955 |
for(j = 0; j < nevents; j += 1) { // decode max nsamples |
---|
956 |
unsigned int v = (int)(*buffer * multiplier); |
---|
957 |
*target = (v >> 16) & 0xff; |
---|
958 |
*(target+1) = (v >> 8) & 0xff; |
---|
959 |
*(target+2) = v & 0xff; |
---|
960 |
|
---|
961 |
buffer++; |
---|
962 |
target+=m_event_size; |
---|
963 |
} |
---|
964 |
} |
---|
965 |
break; |
---|
966 |
} |
---|
967 |
|
---|
968 |
return 0; |
---|
969 |
} |
---|
970 |
|
---|
971 |
int MotuTransmitStreamProcessor::encodeSilencePortToMotuEvents(MotuAudioPort *p, quadlet_t *data, |
---|
972 |
unsigned int offset, unsigned int nevents) { |
---|
973 |
unsigned int j=0; |
---|
974 |
unsigned char *target = (unsigned char *)data + p->getPosition(); |
---|
975 |
|
---|
976 |
switch (p->getDataType()) { |
---|
977 |
default: |
---|
978 |
case Port::E_Int24: |
---|
979 |
case Port::E_Float: |
---|
980 |
for (j = 0; j < nevents; j++) { |
---|
981 |
*target = *(target+1) = *(target+2) = 0; |
---|
982 |
target += m_event_size; |
---|
983 |
} |
---|
984 |
break; |
---|
985 |
} |
---|
986 |
|
---|
987 |
return 0; |
---|
988 |
} |
---|
989 |
|
---|
990 |
/* --------------------- RECEIVE ----------------------- */ |
---|
991 |
|
---|
992 |
MotuReceiveStreamProcessor::MotuReceiveStreamProcessor(int port, int framerate, |
---|
993 |
unsigned int event_size) |
---|
994 |
: ReceiveStreamProcessor(port, framerate), m_event_size(event_size), |
---|
995 |
m_closedown_active(0) { |
---|
996 |
|
---|
997 |
} |
---|
998 |
|
---|
999 |
MotuReceiveStreamProcessor::~MotuReceiveStreamProcessor() { |
---|
1000 |
|
---|
1001 |
} |
---|
1002 |
|
---|
1003 |
bool MotuReceiveStreamProcessor::init() { |
---|
1004 |
|
---|
1005 |
// call the parent init |
---|
1006 |
// this has to be done before allocating the buffers, |
---|
1007 |
// because this sets the buffersizes from the processormanager |
---|
1008 |
if(!ReceiveStreamProcessor::init()) { |
---|
1009 |
debugFatal("Could not do base class init (%d)\n",this); |
---|
1010 |
return false; |
---|
1011 |
} |
---|
1012 |
|
---|
1013 |
return true; |
---|
1014 |
} |
---|
1015 |
|
---|
1016 |
// NOTE by PP: timestamp based sync fixes this automagically by |
---|
1017 |
// enforcing that the roundtrip latency is constant: |
---|
1018 |
// Detect missed receive cycles |
---|
1019 |
// FIXME: it would be nice to advance the rx buffer by the amount of |
---|
1020 |
// frames missed. However, since the MOTU transmits more frames per |
---|
1021 |
// cycle than the average and "catches up" with periodic empty |
---|
1022 |
// cycles it's not trivial to work out precisely how many frames |
---|
1023 |
// were missed. Ultimately I think we need to do so if sync is to |
---|
1024 |
// be maintained across a transient receive failure. |
---|
1025 |
|
---|
1026 |
enum raw1394_iso_disposition |
---|
1027 |
MotuReceiveStreamProcessor::putPacket(unsigned char *data, unsigned int length, |
---|
1028 |
unsigned char channel, unsigned char tag, unsigned char sy, |
---|
1029 |
unsigned int cycle, unsigned int dropped) { |
---|
1030 |
|
---|
1031 |
enum raw1394_iso_disposition retval=RAW1394_ISO_OK; |
---|
1032 |
// this is needed for the base class getLastCycle() to work. |
---|
1033 |
// this avoids a function call like StreamProcessor::updateLastCycle() |
---|
1034 |
m_last_cycle=cycle; |
---|
1035 |
|
---|
1036 |
//debugOutput(DEBUG_LEVEL_VERBOSE,"rx: enabled=%d, cycle=%d\n",!m_is_disabled,cycle); |
---|
1037 |
|
---|
1038 |
// check our enable status |
---|
1039 |
if (!m_disabled && m_is_disabled) { |
---|
1040 |
// this means that we are trying to enable |
---|
1041 |
|
---|
1042 |
// check if we are on or past the enable point |
---|
1043 |
int cycles_past_enable=diffCycles(cycle, m_cycle_to_enable_at); |
---|
1044 |
|
---|
1045 |
if (cycles_past_enable >= 0) { |
---|
1046 |
m_is_disabled=false; |
---|
1047 |
debugOutput(DEBUG_LEVEL_VERBOSE,"Enabling Rx StreamProcessor %p at %d\n", |
---|
1048 |
this, cycle); |
---|
1049 |
|
---|
1050 |
// the previous timestamp is the one we need to start with |
---|
1051 |
// because we're going to update the buffer again this loop |
---|
1052 |
// using writeframes |
---|
1053 |
// m_data_buffer->setBufferTailTimestamp(m_last_timestamp2); |
---|
1054 |
m_data_buffer->setBufferTailTimestamp(m_last_timestamp); |
---|
1055 |
|
---|
1056 |
debugOutput(DEBUG_LEVEL_VERBOSE,"On enable: last ts=%lld, ts2=%lld = %lld (%p)\n", |
---|
1057 |
m_last_timestamp, m_last_timestamp2, m_last_timestamp-m_last_timestamp2, |
---|
1058 |
m_data_buffer); |
---|
1059 |
|
---|
1060 |
} else { |
---|
1061 |
static int foo=0; |
---|
1062 |
if (!foo) { |
---|
1063 |
debugOutput(DEBUG_LEVEL_VERBOSE, |
---|
1064 |
"will enable rx StreamProcessor %p at %u, now is %d\n", |
---|
1065 |
this, m_cycle_to_enable_at, cycle); |
---|
1066 |
foo=1; |
---|
1067 |
} |
---|
1068 |
debugOutput(DEBUG_LEVEL_VERY_VERBOSE, |
---|
1069 |
"will enable StreamProcessor %p at %u, now is %d\n", |
---|
1070 |
this, m_cycle_to_enable_at, cycle); |
---|
1071 |
} |
---|
1072 |
} else if (m_disabled && !m_is_disabled) { |
---|
1073 |
// trying to disable |
---|
1074 |
debugOutput(DEBUG_LEVEL_VERBOSE,"disabling StreamProcessor %p at %u\n", this, cycle); |
---|
1075 |
m_is_disabled=true; |
---|
1076 |
} |
---|
1077 |
|
---|
1078 |
// If the packet length is 8 bytes (ie: just a CIP-like header) |
---|
1079 |
// there is no isodata. |
---|
1080 |
if (length > 8) { |
---|
1081 |
// The iso data blocks from the MOTUs comprise a CIP-like |
---|
1082 |
// header followed by a number of events (8 for 1x rates, 16 |
---|
1083 |
// for 2x rates, 32 for 4x rates). |
---|
1084 |
quadlet_t *quadlet = (quadlet_t *)data; |
---|
1085 |
unsigned int dbs = get_bits(ntohl(quadlet[0]), 23, 8); // Size of one event in terms of fdf_size |
---|
1086 |
unsigned int fdf_size = get_bits(ntohl(quadlet[1]), 23, 8) == 0x22 ? 32:0; // Event unit size in bits |
---|
1087 |
|
---|
1088 |
// Don't even attempt to process a packet if it isn't what |
---|
1089 |
// we expect from a MOTU. Yes, an FDF value of 32 bears |
---|
1090 |
// little relationship to the actual data (24 bit integer) |
---|
1091 |
// sent by the MOTU - it's one of those areas where MOTU |
---|
1092 |
// have taken a curious detour around the standards. |
---|
1093 |
if (tag!=1 || fdf_size!=32) { |
---|
1094 |
return RAW1394_ISO_OK; |
---|
1095 |
} |
---|
1096 |
|
---|
1097 |
// put this after the check because event_length can become 0 on invalid packets |
---|
1098 |
unsigned int event_length = (fdf_size * dbs) / 8; // Event size in bytes |
---|
1099 |
unsigned int n_events = (length-8) / event_length; |
---|
1100 |
|
---|
1101 |
//=> store the previous timestamp |
---|
1102 |
m_last_timestamp2=m_last_timestamp; |
---|
1103 |
|
---|
1104 |
//=> convert the SYT to a full timestamp in ticks |
---|
1105 |
// m_last_timestamp=sytRecvToFullTicks((uint32_t)ntohl(*(quadlet_t *)(data+8)), |
---|
1106 |
// cycle, m_handler->getCycleTimer()); |
---|
1107 |
//*** |
---|
1108 |
// FIXME: it given that we later advance this to be the timestamp of the sample immediately following |
---|
1109 |
// this packet, it perhaps makes more sense to acquire the timestamp of the last frame in the packet. |
---|
1110 |
// Then it's just a matter of adding m_ticks_per_frame rather than frame_size times this. |
---|
1111 |
#if 0 |
---|
1112 |
uint32_t first_sph = ntohl(*(quadlet_t *)(data+8)); |
---|
1113 |
//uint32_t first_sph = ntohl(*(quadlet_t *)(data+8+(event_length*(n_events-1)))); |
---|
1114 |
// m_last_timestamp = ((first_sph & 0x1fff000)>>12)*3072 + (first_sph & 0xfff); |
---|
1115 |
// m_last_timestamp = CYCLE_TIMER_TO_TICKS(first_sph & 0x1ffffff); |
---|
1116 |
m_last_timestamp = sphRecvToFullTicks(first_sph, m_handler->getCycleTimer()); |
---|
1117 |
float frame_size=m_framerate<=48000?8:(m_framerate<=96000?16:32); |
---|
1118 |
uint64_t ts=addTicks(m_last_timestamp, (uint64_t)((frame_size-1) * m_ticks_per_frame)); |
---|
1119 |
m_last_timestamp = ts; |
---|
1120 |
#endif |
---|
1121 |
|
---|
1122 |
#if 1 |
---|
1123 |
uint32_t last_sph = ntohl(*(quadlet_t *)(data+8+(n_events-1)*event_length)); |
---|
1124 |
m_last_timestamp = sphRecvToFullTicks(last_sph, m_handler->getCycleTimer()); |
---|
1125 |
#endif |
---|
1126 |
//debugOutput(DEBUG_LEVEL_VERBOSE,"ave ticks=%g\n",(m_last_timestamp-m_last_timestamp2)/8.0); |
---|
1127 |
|
---|
1128 |
// Signal that we're running |
---|
1129 |
if(!m_running && n_events && m_last_timestamp2 && m_last_timestamp) { |
---|
1130 |
debugOutput(DEBUG_LEVEL_VERBOSE,"Receive StreamProcessor %p started running at %d\n", this, cycle); |
---|
1131 |
m_running=true; |
---|
1132 |
} |
---|
1133 |
|
---|
1134 |
//=> don't process the stream samples when it is not enabled. |
---|
1135 |
if(m_is_disabled) { |
---|
1136 |
|
---|
1137 |
// we keep track of the timestamp here |
---|
1138 |
// this makes sure that we will have a somewhat accurate |
---|
1139 |
// estimate as to when a period might be ready. i.e. it will not |
---|
1140 |
// be ready earlier than this timestamp + period time |
---|
1141 |
|
---|
1142 |
// the next (possible) sample is not this one, but lies |
---|
1143 |
// SYT_INTERVAL * rate later |
---|
1144 |
float frames_per_packet=m_framerate<=48000?8:(m_framerate<=96000?16:32); |
---|
1145 |
uint64_t ts=addTicks(m_last_timestamp, |
---|
1146 |
(uint64_t)(frames_per_packet * m_ticks_per_frame)); |
---|
1147 |
// uint64_t ts=addTicks(m_last_timestamp, |
---|
1148 |
// (uint64_t)(m_ticks_per_frame)); |
---|
1149 |
|
---|
1150 |
// set the timestamp as if there will be a sample put into |
---|
1151 |
// the buffer by the next packet. This means we use the timestamp |
---|
1152 |
// corresponding to the last frame which would have been added to the |
---|
1153 |
// buffer this cycle if we weren't disabled. |
---|
1154 |
// |
---|
1155 |
// FIXME: in theory m_last_timestamp is already equal to the |
---|
1156 |
// timestamp of the last frame/sample in this packet since |
---|
1157 |
// that's what we used to set it in the first place. However, |
---|
1158 |
// if m_last_timestamp is used to set the buffer tail we get a |
---|
1159 |
// series of "difference too large" warnings during the early |
---|
1160 |
// stages after enabling. These tend to indicate that the |
---|
1161 |
// buffer timestamp is one packet out. Using ts (which predicts |
---|
1162 |
// the timestamp of the last frame of the *next* packet) seems |
---|
1163 |
// to stop these warnings most of the time and allow for a |
---|
1164 |
// smoother startup. |
---|
1165 |
// On second thoughts, perhaps it doesn't help much after all. |
---|
1166 |
// m_data_buffer->setBufferTailTimestamp(ts); |
---|
1167 |
m_data_buffer->setBufferTailTimestamp(m_last_timestamp); |
---|
1168 |
//debugOutput(DEBUG_LEVEL_VERBOSE,"%p, last ts=%lld, ts=%lld, lts2=%lld\n", m_data_buffer, m_last_timestamp, ts, m_last_timestamp2); |
---|
1169 |
|
---|
1170 |
return RAW1394_ISO_DEFER; |
---|
1171 |
} |
---|
1172 |
|
---|
1173 |
debugOutput( DEBUG_LEVEL_VERY_VERBOSE, "put packet...\n"); |
---|
1174 |
//debugOutput(DEBUG_LEVEL_VERBOSE,"rx cycle=%d, last ts=%lld\n",cycle, m_last_timestamp); |
---|
1175 |
|
---|
1176 |
//=> process the packet |
---|
1177 |
// add the data payload to the ringbuffer |
---|
1178 |
// Note: the last argument to writeFrames is the timestamp of the *last sample* being |
---|
1179 |
// added. |
---|
1180 |
if(m_data_buffer->writeFrames(n_events, (char *)(data+8), m_last_timestamp)) { |
---|
1181 |
retval=RAW1394_ISO_OK; |
---|
1182 |
int dbc = get_bits(ntohl(quadlet[0]), 8, 8); |
---|
1183 |
|
---|
1184 |
// process all ports that should be handled on a per-packet base |
---|
1185 |
// this is MIDI for AMDTP (due to the need of DBC) |
---|
1186 |
if (!decodePacketPorts((quadlet_t *)(data+8), n_events, dbc)) { |
---|
1187 |
debugWarning("Problem decoding Packet Ports\n"); |
---|
1188 |
retval=RAW1394_ISO_DEFER; |
---|
1189 |
} |
---|
1190 |
|
---|
1191 |
} else { |
---|
1192 |
|
---|
1193 |
debugWarning("Receive buffer overrun (cycle %d, FC=%d, PC=%d)\n", |
---|
1194 |
cycle, m_data_buffer->getFrameCounter(), m_handler->getPacketCount()); |
---|
1195 |
|
---|
1196 |
m_xruns++; |
---|
1197 |
|
---|
1198 |
// disable the processing, will be re-enabled when |
---|
1199 |
// the xrun is handled |
---|
1200 |
m_disabled=true; |
---|
1201 |
m_is_disabled=true; |
---|
1202 |
|
---|
1203 |
retval=RAW1394_ISO_DEFER; |
---|
1204 |
} |
---|
1205 |
} |
---|
1206 |
|
---|
1207 |
return retval; |
---|
1208 |
} |
---|
1209 |
|
---|
1210 |
// returns the delay between the actual (real) time of a timestamp as received, |
---|
1211 |
// and the timestamp that is passed on for the same event. This is to cope with |
---|
1212 |
// ISO buffering |
---|
1213 |
int MotuReceiveStreamProcessor::getMinimalSyncDelay() { |
---|
1214 |
unsigned int n_events = m_framerate<=48000?8:(m_framerate<=96000?16:32); |
---|
1215 |
|
---|
1216 |
return (int)(m_handler->getWakeupInterval() * n_events * m_ticks_per_frame); |
---|
1217 |
} |
---|
1218 |
|
---|
1219 |
bool MotuReceiveStreamProcessor::reset() { |
---|
1220 |
|
---|
1221 |
debugOutput( DEBUG_LEVEL_VERBOSE, "Resetting...\n"); |
---|
1222 |
|
---|
1223 |
m_data_buffer->setTickOffset(0); |
---|
1224 |
|
---|
1225 |
// reset all non-device specific stuff |
---|
1226 |
// i.e. the iso stream and the associated ports |
---|
1227 |
if(!ReceiveStreamProcessor::reset()) { |
---|
1228 |
debugFatal("Could not do base class reset\n"); |
---|
1229 |
return false; |
---|
1230 |
} |
---|
1231 |
|
---|
1232 |
return true; |
---|
1233 |
} |
---|
1234 |
|
---|
1235 |
bool MotuReceiveStreamProcessor::prepare() { |
---|
1236 |
|
---|
1237 |
// prepare all non-device specific stuff |
---|
1238 |
// i.e. the iso stream and the associated ports |
---|
1239 |
if(!ReceiveStreamProcessor::prepare()) { |
---|
1240 |
debugFatal("Could not prepare base class\n"); |
---|
1241 |
return false; |
---|
1242 |
} |
---|
1243 |
|
---|
1244 |
debugOutput( DEBUG_LEVEL_VERBOSE, "Preparing...\n"); |
---|
1245 |
|
---|
1246 |
m_PeriodStat.setName("RCV PERIOD"); |
---|
1247 |
m_PacketStat.setName("RCV PACKET"); |
---|
1248 |
m_WakeupStat.setName("RCV WAKEUP"); |
---|
1249 |
|
---|
1250 |
// setup any specific stuff here |
---|
1251 |
// FIXME: m_frame_size would be a better name |
---|
1252 |
debugOutput( DEBUG_LEVEL_VERBOSE, "Event size: %d\n", m_event_size); |
---|
1253 |
|
---|
1254 |
// prepare the framerate estimate |
---|
1255 |
m_ticks_per_frame = (TICKS_PER_SECOND*1.0) / ((float)m_framerate); |
---|
1256 |
|
---|
1257 |
// initialize internal buffer |
---|
1258 |
unsigned int ringbuffer_size_frames=m_nb_buffers * m_period; |
---|
1259 |
|
---|
1260 |
unsigned int events_per_frame = m_framerate<=48000?8:(m_framerate<=96000?16:32); |
---|
1261 |
|
---|
1262 |
assert(m_data_buffer); |
---|
1263 |
m_data_buffer->setBufferSize(ringbuffer_size_frames); |
---|
1264 |
m_data_buffer->setEventSize(m_event_size); |
---|
1265 |
m_data_buffer->setEventsPerFrame(1); |
---|
1266 |
|
---|
1267 |
// JMW: The rx buffer receives a new timestamp once per received frame so I think the |
---|
1268 |
// buffer update period is events_per_frame, not events per period. |
---|
1269 |
// m_data_buffer->setUpdatePeriod(m_period); |
---|
1270 |
m_data_buffer->setUpdatePeriod(events_per_frame); |
---|
1271 |
m_data_buffer->setNominalRate(m_ticks_per_frame); |
---|
1272 |
|
---|
1273 |
m_data_buffer->setWrapValue(128L*TICKS_PER_SECOND); |
---|
1274 |
|
---|
1275 |
m_data_buffer->prepare(); |
---|
1276 |
|
---|
1277 |
// set the parameters of ports we can: |
---|
1278 |
// we want the audio ports to be period buffered, |
---|
1279 |
// and the midi ports to be packet buffered |
---|
1280 |
for ( PortVectorIterator it = m_Ports.begin(); |
---|
1281 |
it != m_Ports.end(); |
---|
1282 |
++it ) |
---|
1283 |
{ |
---|
1284 |
debugOutput(DEBUG_LEVEL_VERBOSE, "Setting up port %s\n",(*it)->getName().c_str()); |
---|
1285 |
|
---|
1286 |
if(!(*it)->setBufferSize(m_period)) { |
---|
1287 |
debugFatal("Could not set buffer size to %d\n",m_period); |
---|
1288 |
return false; |
---|
1289 |
} |
---|
1290 |
|
---|
1291 |
switch ((*it)->getPortType()) { |
---|
1292 |
case Port::E_Audio: |
---|
1293 |
if(!(*it)->setSignalType(Port::E_PeriodSignalled)) { |
---|
1294 |
debugFatal("Could not set signal type to PeriodSignalling"); |
---|
1295 |
return false; |
---|
1296 |
} |
---|
1297 |
break; |
---|
1298 |
case Port::E_Midi: |
---|
1299 |
if(!(*it)->setSignalType(Port::E_PacketSignalled)) { |
---|
1300 |
debugFatal("Could not set signal type to PacketSignalling"); |
---|
1301 |
return false; |
---|
1302 |
} |
---|
1303 |
if (!(*it)->setBufferType(Port::E_RingBuffer)) { |
---|
1304 |
debugFatal("Could not set buffer type"); |
---|
1305 |
return false; |
---|
1306 |
} |
---|
1307 |
if (!(*it)->setDataType(Port::E_MidiEvent)) { |
---|
1308 |
debugFatal("Could not set data type"); |
---|
1309 |
return false; |
---|
1310 |
} |
---|
1311 |
// FIXME: probably need rate control too. See |
---|
1312 |
// Port::useRateControl() and AmdtpStreamProcessor. |
---|
1313 |
break; |
---|
1314 |
case Port::E_Control: |
---|
1315 |
if(!(*it)->setSignalType(Port::E_PeriodSignalled)) { |
---|
1316 |
debugFatal("Could not set signal type to PeriodSignalling"); |
---|
1317 |
return false; |
---|
1318 |
} |
---|
1319 |
break; |
---|
1320 |
default: |
---|
1321 |
debugWarning("Unsupported port type specified\n"); |
---|
1322 |
break; |
---|
1323 |
} |
---|
1324 |
|
---|
1325 |
} |
---|
1326 |
|
---|
1327 |
// The API specific settings of the ports are already set before |
---|
1328 |
// this routine is called, therefore we can init&prepare the ports |
---|
1329 |
if(!initPorts()) { |
---|
1330 |
debugFatal("Could not initialize ports!\n"); |
---|
1331 |
return false; |
---|
1332 |
} |
---|
1333 |
|
---|
1334 |
if(!preparePorts()) { |
---|
1335 |
debugFatal("Could not initialize ports!\n"); |
---|
1336 |
return false; |
---|
1337 |
} |
---|
1338 |
|
---|
1339 |
return true; |
---|
1340 |
|
---|
1341 |
} |
---|
1342 |
|
---|
1343 |
|
---|
1344 |
bool MotuReceiveStreamProcessor::prepareForStop() { |
---|
1345 |
|
---|
1346 |
// A MOTU receive stream can stop at any time. However, signify |
---|
1347 |
// that stopping is in progress because other streams (notably the |
---|
1348 |
// transmit stream) may keep going for some time and cause an |
---|
1349 |
// overflow in the receive buffers. If a closedown is in progress |
---|
1350 |
// the receive handler simply throws all incoming data away so |
---|
1351 |
// no buffer overflow can occur. |
---|
1352 |
m_closedown_active = 1; |
---|
1353 |
return true; |
---|
1354 |
} |
---|
1355 |
|
---|
1356 |
bool MotuReceiveStreamProcessor::prepareForStart() { |
---|
1357 |
// Reset some critical variables required so the stream starts cleanly. This |
---|
1358 |
// method is called once on every stream restart, including those during |
---|
1359 |
// xrun recovery. Initialisations which should be done once should be |
---|
1360 |
// placed in the init() method instead. |
---|
1361 |
m_running = 0; |
---|
1362 |
m_closedown_active = 0; |
---|
1363 |
|
---|
1364 |
// At this point we'll also disable the stream processor here. |
---|
1365 |
// At this stage stream processors are always explicitly re-enabled |
---|
1366 |
// after being started, so by starting in the disabled state we |
---|
1367 |
// ensure that every start will be exactly the same. |
---|
1368 |
disable(); |
---|
1369 |
|
---|
1370 |
return true; |
---|
1371 |
} |
---|
1372 |
|
---|
1373 |
bool MotuReceiveStreamProcessor::getFrames(unsigned int nbframes, int64_t ts) { |
---|
1374 |
|
---|
1375 |
m_PeriodStat.mark(m_data_buffer->getBufferFill()); |
---|
1376 |
|
---|
1377 |
// ask the buffer to process nbframes of frames |
---|
1378 |
// using it's registered client's processReadBlock(), |
---|
1379 |
// which should be ours |
---|
1380 |
m_data_buffer->blockProcessReadFrames(nbframes); |
---|
1381 |
|
---|
1382 |
return true; |
---|
1383 |
} |
---|
1384 |
|
---|
1385 |
/** |
---|
1386 |
* \brief write received events to the port ringbuffers. |
---|
1387 |
*/ |
---|
1388 |
bool MotuReceiveStreamProcessor::processReadBlock(char *data, |
---|
1389 |
unsigned int nevents, unsigned int offset) |
---|
1390 |
{ |
---|
1391 |
bool no_problem=true; |
---|
1392 |
for ( PortVectorIterator it = m_PeriodPorts.begin(); |
---|
1393 |
it != m_PeriodPorts.end(); |
---|
1394 |
++it ) { |
---|
1395 |
if((*it)->isDisabled()) {continue;}; |
---|
1396 |
|
---|
1397 |
//FIXME: make this into a static_cast when not DEBUG? |
---|
1398 |
Port *port=dynamic_cast<Port *>(*it); |
---|
1399 |
|
---|
1400 |
switch(port->getPortType()) { |
---|
1401 |
|
---|
1402 |
case Port::E_Audio: |
---|
1403 |
if(decodeMotuEventsToPort(static_cast<MotuAudioPort *>(*it), (quadlet_t *)data, offset, nevents)) { |
---|
1404 |
debugWarning("Could not decode packet MBLA to port %s",(*it)->getName().c_str()); |
---|
1405 |
no_problem=false; |
---|
1406 |
} |
---|
1407 |
break; |
---|
1408 |
// midi is a packet based port, don't process |
---|
1409 |
// case MotuPortInfo::E_Midi: |
---|
1410 |
// break; |
---|
1411 |
|
---|
1412 |
default: // ignore |
---|
1413 |
break; |
---|
1414 |
} |
---|
1415 |
} |
---|
1416 |
return no_problem; |
---|
1417 |
} |
---|
1418 |
|
---|
1419 |
/** |
---|
1420 |
* @brief decode a packet for the packet-based ports |
---|
1421 |
* |
---|
1422 |
* @param data Packet data |
---|
1423 |
* @param nevents number of events in data (including events of other ports & port types) |
---|
1424 |
* @param dbc DataBlockCount value for this packet |
---|
1425 |
* @return true if all successfull |
---|
1426 |
*/ |
---|
1427 |
bool MotuReceiveStreamProcessor::decodePacketPorts(quadlet_t *data, unsigned int nevents, |
---|
1428 |
unsigned int dbc) { |
---|
1429 |
bool ok=true; |
---|
1430 |
|
---|
1431 |
// Use char here since the source address won't necessarily be |
---|
1432 |
// aligned; use of an unaligned quadlet_t may cause issues on |
---|
1433 |
// certain architectures. Besides, the source for MIDI data going |
---|
1434 |
// directly to the MOTU isn't structured in quadlets anyway; it is a |
---|
1435 |
// sequence of 3 unaligned bytes. |
---|
1436 |
unsigned char *src = NULL; |
---|
1437 |
|
---|
1438 |
for ( PortVectorIterator it = m_PacketPorts.begin(); |
---|
1439 |
it != m_PacketPorts.end(); |
---|
1440 |
++it ) { |
---|
1441 |
|
---|
1442 |
Port *port=dynamic_cast<Port *>(*it); |
---|
1443 |
assert(port); // this should not fail!! |
---|
1444 |
|
---|
1445 |
// Currently the only packet type of events for MOTU |
---|
1446 |
// is MIDI in mbla. However in future control data |
---|
1447 |
// might also be sent via "packet" events, so allow |
---|
1448 |
// for this possible expansion. |
---|
1449 |
|
---|
1450 |
// FIXME: MIDI input is completely untested at present. |
---|
1451 |
switch (port->getPortType()) { |
---|
1452 |
case Port::E_Midi: { |
---|
1453 |
MotuMidiPort *mp=static_cast<MotuMidiPort *>(*it); |
---|
1454 |
signed int sample; |
---|
1455 |
unsigned int j = 0; |
---|
1456 |
// Get MIDI bytes if present anywhere in the |
---|
1457 |
// packet. MOTU MIDI data is sent using a |
---|
1458 |
// 3-byte sequence starting at the port's |
---|
1459 |
// position. It's thought that there can never |
---|
1460 |
// be more than one MIDI byte per packet, but |
---|
1461 |
// for completeness we'll check the entire packet |
---|
1462 |
// anyway. |
---|
1463 |
src = (unsigned char *)data + mp->getPosition(); |
---|
1464 |
while (j < nevents) { |
---|
1465 |
if (*src==0x01 && *(src+1)==0x00) { |
---|
1466 |
sample = *(src+2); |
---|
1467 |
if (!mp->writeEvent(&sample)) { |
---|
1468 |
debugWarning("MIDI packet port events lost\n"); |
---|
1469 |
ok = false; |
---|
1470 |
} |
---|
1471 |
} |
---|
1472 |
j++; |
---|
1473 |
src += m_event_size; |
---|
1474 |
} |
---|
1475 |
break; |
---|
1476 |
} |
---|
1477 |
default: |
---|
1478 |
debugOutput(DEBUG_LEVEL_VERBOSE, "Unknown packet-type port format %d\n",port->getPortType()); |
---|
1479 |
return ok; |
---|
1480 |
} |
---|
1481 |
} |
---|
1482 |
|
---|
1483 |
return ok; |
---|
1484 |
} |
---|
1485 |
|
---|
1486 |
signed int MotuReceiveStreamProcessor::decodeMotuEventsToPort(MotuAudioPort *p, |
---|
1487 |
quadlet_t *data, unsigned int offset, unsigned int nevents) |
---|
1488 |
{ |
---|
1489 |
unsigned int j=0; |
---|
1490 |
|
---|
1491 |
// Use char here since a port's source address won't necessarily be |
---|
1492 |
// aligned; use of an unaligned quadlet_t may cause issues on |
---|
1493 |
// certain architectures. Besides, the source (data coming directly |
---|
1494 |
// from the MOTU) isn't structured in quadlets anyway; it mainly |
---|
1495 |
// consists of packed 24-bit integers. |
---|
1496 |
|
---|
1497 |
unsigned char *src_data; |
---|
1498 |
src_data = (unsigned char *)data + p->getPosition(); |
---|
1499 |
|
---|
1500 |
switch(p->getDataType()) { |
---|
1501 |
default: |
---|
1502 |
case Port::E_Int24: |
---|
1503 |
{ |
---|
1504 |
quadlet_t *buffer=(quadlet_t *)(p->getBufferAddress()); |
---|
1505 |
|
---|
1506 |
assert(nevents + offset <= p->getBufferSize()); |
---|
1507 |
|
---|
1508 |
// Offset is in frames, but each port is only a single |
---|
1509 |
// channel, so the number of frames is the same as the |
---|
1510 |
// number of quadlets to offset (assuming the port buffer |
---|
1511 |
// uses one quadlet per sample, which is the case currently). |
---|
1512 |
buffer+=offset; |
---|
1513 |
|
---|
1514 |
for(j = 0; j < nevents; j += 1) { // Decode nsamples |
---|
1515 |
*buffer = (*src_data<<16)+(*(src_data+1)<<8)+*(src_data+2); |
---|
1516 |
// Sign-extend highest bit of 24-bit int. |
---|
1517 |
// FIXME: this isn't strictly needed since E_Int24 is a 24-bit, |
---|
1518 |
// but doing so shouldn't break anything and makes the data |
---|
1519 |
// easier to deal with during debugging. |
---|
1520 |
if (*src_data & 0x80) |
---|
1521 |
*buffer |= 0xff000000; |
---|
1522 |
|
---|
1523 |
buffer++; |
---|
1524 |
src_data+=m_event_size; |
---|
1525 |
} |
---|
1526 |
} |
---|
1527 |
break; |
---|
1528 |
case Port::E_Float: |
---|
1529 |
{ |
---|
1530 |
const float multiplier = 1.0f / (float)(0x7FFFFF); |
---|
1531 |
float *buffer=(float *)(p->getBufferAddress()); |
---|
1532 |
|
---|
1533 |
assert(nevents + offset <= p->getBufferSize()); |
---|
1534 |
|
---|
1535 |
buffer+=offset; |
---|
1536 |
|
---|
1537 |
for(j = 0; j < nevents; j += 1) { // decode max nsamples |
---|
1538 |
|
---|
1539 |
unsigned int v = (*src_data<<16)+(*(src_data+1)<<8)+*(src_data+2); |
---|
1540 |
|
---|
1541 |
// sign-extend highest bit of 24-bit int |
---|
1542 |
int tmp = (int)(v << 8) / 256; |
---|
1543 |
|
---|
1544 |
*buffer = tmp * multiplier; |
---|
1545 |
|
---|
1546 |
buffer++; |
---|
1547 |
src_data+=m_event_size; |
---|
1548 |
} |
---|
1549 |
} |
---|
1550 |
break; |
---|
1551 |
} |
---|
1552 |
|
---|
1553 |
return 0; |
---|
1554 |
} |
---|
1555 |
|
---|
1556 |
signed int MotuReceiveStreamProcessor::setEventSize(unsigned int size) { |
---|
1557 |
m_event_size = size; |
---|
1558 |
return 0; |
---|
1559 |
} |
---|
1560 |
|
---|
1561 |
unsigned int MotuReceiveStreamProcessor::getEventSize(void) { |
---|
1562 |
// |
---|
1563 |
// Return the size of a single event sent by the MOTU as part of an iso |
---|
1564 |
// data packet in bytes. |
---|
1565 |
// |
---|
1566 |
return m_event_size; |
---|
1567 |
} |
---|
1568 |
|
---|
1569 |
void MotuReceiveStreamProcessor::setVerboseLevel(int l) { |
---|
1570 |
setDebugLevel(l); |
---|
1571 |
ReceiveStreamProcessor::setVerboseLevel(l); |
---|
1572 |
} |
---|
1573 |
|
---|
1574 |
} // end of namespace Streaming |
---|