Merge branch 'master' into develop
[jack2.git] / common / JackTransportEngine.h
blobe00a313847f2a717b2f58f5fa257366a0b2cae44
1 /*
2 Copyright (C) 2001 Paul Davis
3 Copyright (C) 2004-2008 Grame
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #ifndef __JackTransportEngine__
22 #define __JackTransportEngine__
24 #include "JackAtomicArrayState.h"
25 #include "JackCompilerDeps.h"
26 #include "types.h"
28 namespace Jack
31 typedef enum {
32 TransportCommandNone = 0,
33 TransportCommandStart = 1,
34 TransportCommandStop = 2,
35 } transport_command_t;
37 /*!
38 \brief The client transport structure.
40 We have:
42 - a "current" position
43 - a "pending" position prepared by the server at each cycle
44 - a "request" position wanted by a client
46 At the beginning of a cycle the server needs to select a new current position. When a request and a pending position are available,
47 the request takes precedence on the pending one. The server atomically switches to the new position.
48 The current position can be read by clients.
50 We use a JackAtomicArrayState pattern that allows to manage several "next" states independently.
52 In jack1 implementation, transport code (jack_transport_cycle_end) was not called if the graph could not be locked (see jack_run_one_cycle).
53 Here transport cycle (CycleBegin, CycleEnd) has to run in the RT thread concurrently with code executed from the "command" thread.
55 Each client maintains a state in it's shared memory area defined by:
57 - it's current transport state
58 - a boolean that is "true" when slow-sync cb has to be called
59 - a boolean that is "true" when timebase cb is called with new_pos on
61 Several operations set the "slow-sync cb" flag to true:
63 - setting a new cb (client)
64 - activate (client)
65 - transport start (server)
66 - new pos (server)
68 Slow-sync cb calls stops when:
70 - the cb return true (client)
71 - deactivate (client)
72 - transport stop (server)
74 Several operations set the "timebase cb" flag to true:
76 - setting a new cb (client)
77 - activate (client)
78 - transport start (server) ??
79 - new pos (server)
81 Timebase cb "new_pos" argument calls stops when:
83 - after one cb call with "new_pos" argument true (client)
84 - deactivate (client)
85 - release (client)
86 - transport stop (server)
90 class JackClientInterface;
92 PRE_PACKED_STRUCTURE
93 class SERVER_EXPORT JackTransportEngine : public JackAtomicArrayState<jack_position_t>
96 private:
98 jack_transport_state_t fTransportState;
99 volatile transport_command_t fTransportCmd;
100 transport_command_t fPreviousCmd; /* previous transport_cmd */
101 jack_time_t fSyncTimeout;
102 int fSyncTimeLeft;
103 int fTimeBaseMaster;
104 bool fPendingPos;
105 bool fNetworkSync;
106 bool fConditionnal;
107 SInt32 fWriteCounter;
109 bool CheckAllRolling(JackClientInterface** table);
110 void MakeAllStartingLocating(JackClientInterface** table);
111 void MakeAllStopping(JackClientInterface** table);
112 void MakeAllLocating(JackClientInterface** table);
114 void SyncTimeout(jack_nframes_t frame_rate, jack_nframes_t buffer_size);
116 public:
118 JackTransportEngine();
120 ~JackTransportEngine()
123 void SetCommand(transport_command_t state)
125 fTransportCmd = state;
128 jack_transport_state_t GetState() const
130 return fTransportState;
133 void SetState(jack_transport_state_t state)
135 fTransportState = state;
139 \brief
141 int ResetTimebase(int refnum);
144 \brief
146 int SetTimebaseMaster(int refnum, bool conditionnal);
148 void GetTimebaseMaster(int& refnum, bool& conditionnal)
150 refnum = fTimeBaseMaster;
151 conditionnal = fConditionnal;
155 \brief
157 void CycleBegin(jack_nframes_t frame_rate, jack_time_t time);
160 \brief
162 void CycleEnd(JackClientInterface** table, jack_nframes_t frame_rate, jack_nframes_t buffer_size);
165 \brief
167 void SetSyncTimeout(jack_time_t timeout)
169 fSyncTimeout = timeout;
172 void ReadCurrentPos(jack_position_t* pos);
174 jack_unique_t GenerateUniqueID()
176 return (jack_unique_t)INC_ATOMIC(&fWriteCounter);
179 void RequestNewPos(jack_position_t* pos);
181 jack_transport_state_t Query(jack_position_t* pos);
183 jack_nframes_t GetCurrentFrame();
185 static void CopyPosition(jack_position_t* from, jack_position_t* to);
187 bool GetNetworkSync() const
189 return fNetworkSync;
192 void SetNetworkSync(bool sync)
194 fNetworkSync = sync;
197 } POST_PACKED_STRUCTURE;
199 } // end of namespace
201 #endif