JackThreadedDriver.cpp 5.37 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
6
7
8
9
10
11
12
13
14
15
16
17
18
19

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 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 General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/
sletz's avatar
Cleanup    
sletz committed
20

21
#include "JackSystemDeps.h"
sletz's avatar
sletz committed
22
23
24
25
26
27
28
29
#include "JackThreadedDriver.h"
#include "JackError.h"
#include "JackGlobals.h"
#include "JackEngineControl.h"

namespace Jack
{

sletz's avatar
sletz committed
30
JackThreadedDriver::JackThreadedDriver(JackDriver* driver):fThread(this)
sletz's avatar
sletz committed
31
32
33
34
35
36
37
38
39
{
    fDriver = driver;
}

JackThreadedDriver::~JackThreadedDriver()
{
    delete fDriver;
}

sletz's avatar
sletz committed
40
41
42
43
44
int JackThreadedDriver::Open()
{
    return fDriver->Open();
}

sletz's avatar
sletz committed
45
int JackThreadedDriver::Open(jack_nframes_t buffer_size,
sletz's avatar
sletz committed
46
47
48
49
50
51
52
53
54
55
56
                 jack_nframes_t samplerate,
                 bool capturing,
                 bool playing,
                 int inchannels,
                 int outchannels,
                 bool monitor,
                 const char* capture_driver_name,
                 const char* playback_driver_name,
                 jack_nframes_t capture_latency,
                 jack_nframes_t playback_latency)
{
sletz's avatar
sletz committed
57
    return fDriver->Open(buffer_size, samplerate, capturing, playing, inchannels, outchannels, monitor, capture_driver_name, playback_driver_name, capture_latency, playback_latency);
sletz's avatar
sletz committed
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
}

int JackThreadedDriver::Close()
{
    return fDriver->Close();
}

int JackThreadedDriver::Process()
{
    return fDriver->Process();
}

int JackThreadedDriver::ProcessNull()
{
    return fDriver->ProcessNull();
}

int JackThreadedDriver::Attach()
{
    return fDriver->Attach();
}

int JackThreadedDriver::Detach()
{
    return fDriver->Detach();
}

int JackThreadedDriver::Read()
{
    return fDriver->Read();
}

int JackThreadedDriver::Write()
{
    return fDriver->Write();
}

95
96
97
98
99
bool JackThreadedDriver::IsFixedBufferSize()
{
    return fDriver->IsFixedBufferSize();
}

sletz's avatar
sletz committed
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
int JackThreadedDriver::SetBufferSize(jack_nframes_t buffer_size)
{
    return fDriver->SetBufferSize(buffer_size);
}

int JackThreadedDriver::SetSampleRate(jack_nframes_t sample_rate)
{
    return fDriver->SetSampleRate(sample_rate);
}

void JackThreadedDriver::SetMaster(bool onoff)
{
    fDriver->SetMaster(onoff);
}

bool JackThreadedDriver::GetMaster()
{
    return fDriver->GetMaster();
}

void JackThreadedDriver::AddSlave(JackDriverInterface* slave)
{
    fDriver->AddSlave(slave);
}

void JackThreadedDriver::RemoveSlave(JackDriverInterface* slave)
{
    fDriver->RemoveSlave(slave);
}

int JackThreadedDriver::ProcessSlaves()
{
    return fDriver->ProcessSlaves();
}

135
136
137
138
139
std::list<JackDriverInterface*> JackThreadedDriver::GetSlaves()
{
    return fDriver->GetSlaves();
}

140
int JackThreadedDriver::ClientNotify(int refnum, const char* name, int notify, int sync, const char* message, int value1, int value2)
sletz's avatar
sletz committed
141
{
142
    return fDriver->ClientNotify(refnum, name, notify, sync, message, value1, value2);
sletz's avatar
sletz committed
143
144
145
146
147
148
149
150
151
152
153
154
}

JackClientControl* JackThreadedDriver::GetClientControl() const
{
    return fDriver->GetClientControl();
}

bool JackThreadedDriver::IsRealTime() const
{
    return fDriver->IsRealTime();
}

sletz's avatar
sletz committed
155
156
int JackThreadedDriver::Start()
{
sletz's avatar
sletz committed
157
    jack_log("JackThreadedDriver::Start");
sletz's avatar
sletz committed
158

159
    if (fDriver->Start() < 0) {
sletz's avatar
sletz committed
160
        jack_error("Cannot start driver");
161
        return -1;
sletz's avatar
sletz committed
162
    }
163
    if (fThread.StartSync() < 0) {
sletz's avatar
sletz committed
164
        jack_error("Cannot start thread");
165
        return -1;
sletz's avatar
sletz committed
166
167
168
169
170
171
172
    }

    return 0;
}

int JackThreadedDriver::Stop()
{
sletz's avatar
sletz committed
173
    jack_log("JackThreadedDriver::Stop");
174
    
175
    switch (fThread.GetStatus()) {
176
177
178
            
        // Kill the thread in Init phase
        case JackThread::kStarting:
sletz's avatar
sletz committed
179
        case JackThread::kIniting:
180
            if (fThread.Kill() < 0) {
181
                jack_error("Cannot kill thread");
182
                return -1;
183
184
185
186
187
            }
            break;
           
        // Stop when the thread cycle is finished
        case JackThread::kRunning:
188
            if (fThread.Stop() < 0) {
189
                jack_error("Cannot stop thread"); 
190
                return -1;
191
192
            }
            break;
sletz's avatar
sletz committed
193
194
195
            
        default:
            break;
sletz's avatar
sletz committed
196
    }
197

198
    if (fDriver->Stop() < 0) {
sletz's avatar
sletz committed
199
        jack_error("Cannot stop driver");
200
        return -1;
sletz's avatar
sletz committed
201
202
203
204
205
206
    }
    return 0;
}

bool JackThreadedDriver::Execute()
{
sletz's avatar
sletz committed
207
    return (Process() == 0);
sletz's avatar
sletz committed
208
209
}

210
211
bool JackThreadedDriver::Init()
{
212
    if (fDriver->Initialize())  {
213
214
215
        if (fDriver->IsRealTime()) {
            jack_log("JackThreadedDriver::Init IsRealTime");
            // Will do "something" on OSX only...
sletz's avatar
sletz committed
216
            GetEngineControl()->fPeriod = GetEngineControl()->fConstraint = GetEngineControl()->fPeriodUsecs * 1000;
217
            fThread.SetParams(GetEngineControl()->fPeriod, GetEngineControl()->fComputation, GetEngineControl()->fConstraint);
218
            if (fThread.AcquireRealTime(GetEngineControl()->fServerPriority) < 0) {
219
                jack_error("AcquireRealTime error");
220
221
222
            } else {
                set_threaded_log_function(); 
            }
223
224
225
226
227
        }
        return true;
    } else {
        return false;
    }
228
229
}

sletz's avatar
sletz committed
230
} // end of namespace