JackTransportEngine.cpp
/*
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
_jack_position
Definition: types.h:554
Jack
Definition: JackAC3Encoder.cpp:30
Jack::JackAtomicArrayState< jack_position_t >::GetCurrentIndex
UInt16 GetCurrentIndex()
Returns the current switch counter.
Definition: JackAtomicArrayState.h:181
std
jack_error
SERVER_EXPORT void jack_error(const char *fmt,...)
Definition: JackError.cpp:92
_jack_position::valid
jack_position_bits_t valid
Definition: types.h:562
Jack::JackAtomicArrayState< jack_position_t >::ReadCurrentState
jack_position_t * ReadCurrentState()
Returns the current state : only valid in the RT reader thread.
Definition: JackAtomicArrayState.h:172
_jack_position::unique_1
jack_unique_t unique_1
Definition: types.h:557
_jack_position::frame_rate
jack_nframes_t frame_rate
Definition: types.h:559
Jack::JackAtomicArrayState< jack_position_t >::TrySwitchState
jack_position_t * TrySwitchState(int state)
Tries to switch to the next state and returns the new current state (either the same as before if cas...
Definition: JackAtomicArrayState.h:190
_jack_position::unique_2
jack_unique_t unique_2
Definition: types.h:618
_jack_position::usecs
jack_time_t usecs
Definition: types.h:558
_jack_position::frame
jack_nframes_t frame
Definition: types.h:560
Jack::JackAtomicArrayState< jack_position_t >::WriteNextStateStop
void WriteNextStateStop(int state)
Stop write operation : make the next state ready to be used by the RT thread.
Definition: JackAtomicArrayState.h:247
Jack::JackAtomicArrayState< jack_position_t >::WriteNextStateStart
jack_position_t * WriteNextStateStart(int state)
Start write operation : setup and returns the next state to update, check for recursive write calls...
Definition: JackAtomicArrayState.h:230
jack_log
SERVER_EXPORT void jack_log(const char *fmt,...)
Definition: JackError.cpp:108