1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
|
/*
Copyright (C) 2001 Paul Davis
Copyright (C) 2004-2008 Grame
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "JackTransportEngine.h"
#include "JackClientInterface.h"
#include "JackClientControl.h"
#include "JackEngineControl.h"
#include "JackGlobals.h"
#include "JackError.h"
#include "JackTime.h"
#include <assert.h>
#include <math.h>
#include <stdlib.h>
using namespace std;
namespace Jack
{
JackTransportEngine::JackTransportEngine(): JackAtomicArrayState<jack_position_t>()
{
fTransportState = JackTransportStopped;
fTransportCmd = fPreviousCmd = TransportCommandStop;
fSyncTimeout = 10000000; /* 10 seconds default...
in case of big netjack1 roundtrip */
fSyncTimeLeft = 0;
fTimeBaseMaster = -1;
fWriteCounter = 0;
fConditionnal = false;
fPendingPos = false;
fNetworkSync = false;
}
// compute the number of cycle for timeout
void JackTransportEngine::SyncTimeout(jack_nframes_t frame_rate, jack_nframes_t buffer_size)
{
long buf_usecs = (long)((buffer_size * (jack_time_t)1000000) / frame_rate);
fSyncTimeLeft = fSyncTimeout / buf_usecs;
jack_log("SyncTimeout fSyncTimeout = %ld fSyncTimeLeft = %ld", (long)fSyncTimeout, (long)fSyncTimeLeft);
}
// Server
int JackTransportEngine::ResetTimebase(int refnum)
{
if (fTimeBaseMaster == refnum) {
jack_position_t* request = WriteNextStateStart(2); // To check
request->valid = (jack_position_bits_t)0;
WriteNextStateStop(2);
fTimeBaseMaster = -1;
return 0;
} else {
return EINVAL;
}
}
// Server
int JackTransportEngine::SetTimebaseMaster(int refnum, bool conditionnal)
{
if (conditionnal && fTimeBaseMaster > 0) {
if (refnum != fTimeBaseMaster) {
jack_log("conditional timebase for ref = %ld failed: %ld is already the master", refnum, fTimeBaseMaster);
return EBUSY;
} else {
jack_log("ref = %ld was already timebase master", refnum);
return 0;
}
} else {
fTimeBaseMaster = refnum;
fConditionnal = conditionnal;
jack_log("new timebase master: ref = %ld", refnum);
return 0;
}
}
// RT
bool JackTransportEngine::CheckAllRolling(JackClientInterface** table)
{
for (int i = GetEngineControl()->fDriverNum; i < CLIENT_NUM; i++) {
JackClientInterface* client = table[i];
if (client && client->GetClientControl()->fTransportState != JackTransportRolling) {
jack_log("CheckAllRolling ref = %ld is not rolling", i);
return false;
}
}
jack_log("CheckAllRolling");
return true;
}
// RT
void JackTransportEngine::MakeAllStartingLocating(JackClientInterface** table)
{
for (int i = GetEngineControl()->fDriverNum; i < CLIENT_NUM; i++) {
JackClientInterface* client = table[i];
if (client) {
JackClientControl* control = client->GetClientControl();
// Inactive clients don't have their process function called at all, so they must appear as already "rolling" for the transport....
control->fTransportState = (control->fActive && control->fCallback[kRealTimeCallback]) ? JackTransportStarting : JackTransportRolling;
control->fTransportSync = true;
control->fTransportTimebase = true;
jack_log("MakeAllStartingLocating ref = %ld", i);
}
}
}
// RT
void JackTransportEngine::MakeAllStopping(JackClientInterface** table)
{
for (int i = GetEngineControl()->fDriverNum; i < CLIENT_NUM; i++) {
JackClientInterface* client = table[i];
if (client) {
JackClientControl* control = client->GetClientControl();
control->fTransportState = JackTransportStopped;
control->fTransportSync = false;
control->fTransportTimebase = false;
jack_log("MakeAllStopping ref = %ld", i);
}
}
}
// RT
void JackTransportEngine::MakeAllLocating(JackClientInterface** table)
{
for (int i = GetEngineControl()->fDriverNum; i < CLIENT_NUM; i++) {
JackClientInterface* client = table[i];
if (client) {
JackClientControl* control = client->GetClientControl();
control->fTransportState = JackTransportStopped;
control->fTransportSync = true;
control->fTransportTimebase = true;
jack_log("MakeAllLocating ref = %ld", i);
}
}
}
// RT
void JackTransportEngine::CycleBegin(jack_nframes_t frame_rate, jack_time_t time)
{
jack_position_t* pending = WriteNextStateStart(1); // Update "pending" state
pending->usecs = time;
pending->frame_rate = frame_rate;
WriteNextStateStop(1);
}
// RT
void JackTransportEngine::CycleEnd(JackClientInterface** table, jack_nframes_t frame_rate, jack_nframes_t buffer_size)
{
TrySwitchState(1); // Switch from "pending" to "current", it always works since there is always a pending state
/* Handle any new transport command from the last cycle. */
transport_command_t cmd = fTransportCmd;
if (cmd != fPreviousCmd) {
fPreviousCmd = cmd;
jack_log("transport command: %s", (cmd == TransportCommandStart ? "Transport start" : "Transport stop"));
} else {
cmd = TransportCommandNone;
}
/* state transition switch */
switch (fTransportState) {
case JackTransportStopped:
// Set a JackTransportStarting for the current cycle, if all clients are ready (no slow_sync) ==> JackTransportRolling next state
if (cmd == TransportCommandStart) {
jack_log("transport stopped ==> starting frame = %d", ReadCurrentState()->frame);
fTransportState = JackTransportStarting;
MakeAllStartingLocating(table);
SyncTimeout(frame_rate, buffer_size);
} else if (fPendingPos) {
jack_log("transport stopped ==> stopped (locating) frame = %d", ReadCurrentState()->frame);
MakeAllLocating(table);
}
break;
case JackTransportStarting:
if (cmd == TransportCommandStop) {
jack_log("transport starting ==> stopped frame = %d", ReadCurrentState()->frame);
fTransportState = JackTransportStopped;
MakeAllStopping(table);
} else if (fPendingPos) {
jack_log("transport starting ==> starting frame = %d", ReadCurrentState()->frame);
fTransportState = JackTransportStarting;
MakeAllStartingLocating(table);
SyncTimeout(frame_rate, buffer_size);
} else if (--fSyncTimeLeft == 0 || CheckAllRolling(table)) { // Slow clients may still catch up
if (fNetworkSync) {
jack_log("transport starting ==> netstarting frame = %d");
fTransportState = JackTransportNetStarting;
} else {
jack_log("transport starting ==> rolling fSyncTimeLeft = %ld", fSyncTimeLeft);
fTransportState = JackTransportRolling;
}
}
break;
case JackTransportRolling:
if (cmd == TransportCommandStop) {
jack_log("transport rolling ==> stopped");
fTransportState = JackTransportStopped;
MakeAllStopping(table);
} else if (fPendingPos) {
jack_log("transport rolling ==> starting");
fTransportState = JackTransportStarting;
MakeAllStartingLocating(table);
SyncTimeout(frame_rate, buffer_size);
}
break;
case JackTransportNetStarting:
break;
default:
jack_error("Invalid JACK transport state: %d", fTransportState);
}
/* Update timebase, if needed. */
if (fTransportState == JackTransportRolling) {
jack_position_t* pending = WriteNextStateStart(1); // Update "pending" state
pending->frame += buffer_size;
WriteNextStateStop(1);
}
/* See if an asynchronous position request arrived during the last cycle. */
jack_position_t* request = WriteNextStateStart(2, &fPendingPos);
if (fPendingPos) {
jack_log("New pos = %ld", request->frame);
jack_position_t* pending = WriteNextStateStart(1);
CopyPosition(request, pending);
WriteNextStateStop(1);
}
}
// Client
void JackTransportEngine::ReadCurrentPos(jack_position_t* pos)
{
UInt16 next_index = GetCurrentIndex();
UInt16 cur_index;
do {
cur_index = next_index;
memcpy(pos, ReadCurrentState(), sizeof(jack_position_t));
next_index = GetCurrentIndex();
} while (cur_index != next_index); // Until a coherent state has been read
}
void JackTransportEngine::RequestNewPos(jack_position_t* pos)
{
jack_position_t* request = WriteNextStateStart(2);
pos->unique_1 = pos->unique_2 = GenerateUniqueID();
CopyPosition(pos, request);
jack_log("RequestNewPos pos = %ld", pos->frame);
WriteNextStateStop(2);
}
jack_transport_state_t JackTransportEngine::Query(jack_position_t* pos)
{
if (pos)
ReadCurrentPos(pos);
return GetState();
}
jack_nframes_t JackTransportEngine::GetCurrentFrame()
{
jack_position_t pos;
ReadCurrentPos(&pos);
if (fTransportState == JackTransportRolling) {
float usecs = GetMicroSeconds() - pos.usecs;
jack_nframes_t elapsed = (jack_nframes_t)floor((((float) pos.frame_rate) / 1000000.0f) * usecs);
return pos.frame + elapsed;
} else {
return pos.frame;
}
}
// RT, client
void JackTransportEngine::CopyPosition(jack_position_t* from, jack_position_t* to)
{
int tries = 0;
long timeout = 1000;
do {
/* throttle the busy wait if we don't get the answer
* very quickly. See comment above about this
* design.
*/
if (tries > 10) {
JackSleep(20);
tries = 0;
/* debug code to avoid system hangs... */
if (--timeout == 0) {
jack_error("hung in loop copying position B");
abort();
}
}
*to = *from;
tries++;
} while (to->unique_1 != to->unique_2);
}
} // end of namespace
|