JackTransportEngine.h 5.01 KB
Newer Older
sletz's avatar
sletz committed
1
/*
sletz's avatar
sletz committed
2
Copyright (C) 2001 Paul Davis
sletz's avatar
sletz committed
3
Copyright (C) 2004-2008 Grame
sletz's avatar
sletz committed
4
5

This program is free software; you can redistribute it and/or modify
6
7
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
sletz's avatar
sletz committed
8
9
10
11
12
(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
13
GNU Lesser General Public License for more details.
sletz's avatar
sletz committed
14

15
16
17
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.
sletz's avatar
sletz committed
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

*/

#ifndef __JackTransportEngine__
#define __JackTransportEngine__

#include "JackClientInterface.h"
#include "JackConstants.h"
#include "JackAtomicArrayState.h"

namespace Jack
{

typedef enum {
    TransportCommandNone = 0,
    TransportCommandStart = 1,
    TransportCommandStop = 2,
} transport_command_t;

/*!
\brief The client transport structure.
sletz's avatar
sletz committed
39

sletz's avatar
sletz committed
40
We have:
sletz's avatar
sletz committed
41

sletz's avatar
sletz committed
42
43
44
	- a "current" position
	- a "pending" position prepared by the server at each cycle
	- a "request" position wanted by a client
sletz's avatar
sletz committed
45

sletz's avatar
sletz committed
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,
sletz's avatar
sletz committed
47
	the resquest takes precedence on the pending one. The server atomically switches to the new position.
sletz's avatar
sletz committed
48
	The current position can be read by clients.
sletz's avatar
sletz committed
49

sletz's avatar
sletz committed
50
	We use a JackAtomicArrayState pattern that allows to manage several "next" states independantly.
sletz's avatar
sletz committed
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
    
    In jack1 implementation, transport code (jack_transport_cycle_end) was not called if the graph could not be locked (see jack_run_one_cycle).
    Here transport cycle (CycleBegin, CycleEnd) has to run in the RT thread concurrently with code executed from the "command" thread.
    
    Each client maintains a state in it's shared memory area defined by:
    
    - it's current transport state
    - a boolean that is "true" when slow-sync cb has to be called
    - a boolean that is "true" when timebase cb is called with new_pos on
    
    Several operations set the "slow-sync cb" flag to true:
    
        - setting a new cb (client)
        - activate (client)
        - transport start (server)
        - new pos (server)
        
    Slow-sync cb calls stops when:
    
        - the cb return true (client)
        - desactivate (client)
        - transport stop (server)
        
    Several operations set the "timebase cb" flag to true:
    
        - setting a new cb (client)
        - activate (client)
        - transport start (server) ??
        - new pos (server)
        
    Timebase cb "new_pos" argument calls stops when:
    
        - after one cb call with "new_pos" argument true (client)
        - desactivate (client)
        - release (client)
        - transport stop (server)
        
sletz's avatar
sletz committed
88
89
*/

sletz's avatar
sletz committed
90
class EXPORT JackTransportEngine : public JackAtomicArrayState<jack_position_t>
sletz's avatar
sletz committed
91
92
93
94
95
96
97
98
99
100
101
{

    private:

        jack_transport_state_t fTransportState;
        volatile transport_command_t fTransportCmd;
        transport_command_t fPreviousCmd;		/* previous transport_cmd */
        jack_time_t fSyncTimeout;
        int fSyncTimeLeft;
        int fTimeBaseMaster;
        bool fPendingPos;
102
        SInt32 fWriteCounter;
sletz's avatar
sletz committed
103
104

        bool CheckAllRolling(JackClientInterface** table);
sletz's avatar
sletz committed
105
106
107
        
        void MakeAllStartingLocating(JackClientInterface** table);
        void MakeAllStopping(JackClientInterface** table);
sletz's avatar
sletz committed
108
         void MakeAllLocating(JackClientInterface** table);
sletz's avatar
sletz committed
109
        
sletz's avatar
sletz committed
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
        void SyncTimeout(jack_nframes_t frame_rate, jack_nframes_t buffer_size);

    public:

        JackTransportEngine();

        ~JackTransportEngine()
        {}

        void SetCommand(transport_command_t state)
        {
            fTransportCmd = state;
        }

        jack_transport_state_t GetState() const
        {
            return fTransportState;
        }

        int GetTimebaseMaster() const
        {
            return fTimeBaseMaster;
        }

        /*
        	\brief 
        */
        int ResetTimebase(int refnum);

        /*
        	\brief 
        */
        int SetTimebase(int refnum, bool conditionnal);

        /*
        	\brief 
        */
        void CycleBegin(jack_nframes_t frame_rate, jack_time_t time);

        /*
        	\brief 
        */
        void CycleEnd(JackClientInterface** table, jack_nframes_t frame_rate, jack_nframes_t buffer_size);

        /*
        	\brief 
        */
        void SetSyncTimeout(jack_time_t timeout)
        {
            fSyncTimeout = timeout;
        }

        void ReadCurrentPos(jack_position_t* pos);

        jack_unique_t GenerateUniqueID()
        {
            return (jack_unique_t)INC_ATOMIC(&fWriteCounter);
        }
168
169
170
171
        
        void RequestNewPos(jack_position_t* pos);
        
        jack_transport_state_t Query(jack_position_t* pos);
sletz's avatar
sletz committed
172
173
174
175
176
177
178
179
180

        static void TransportCopyPosition(jack_position_t* from, jack_position_t* to);

};


} // end of namespace

#endif