Merge branch 'master' into develop
[jack2.git] / common / JackThreadedDriver.cpp
blobb56a7343a1ef5c3955b208959c974cfb6bc77834
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 General Public License as published by
7 the Free Software Foundation; either version 2 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "JackSystemDeps.h"
22 #include "JackThreadedDriver.h"
23 #include "JackError.h"
24 #include "JackTools.h"
25 #include "JackGlobals.h"
26 #include "JackEngineControl.h"
28 namespace Jack
31 JackThreadedDriver::JackThreadedDriver(JackDriver* driver):fThread(this),fDriver(driver)
34 JackThreadedDriver::~JackThreadedDriver()
36 delete fDriver;
39 int JackThreadedDriver::Open()
41 return fDriver->Open();
44 int JackThreadedDriver::Open(jack_nframes_t buffer_size,
45 jack_nframes_t samplerate,
46 bool capturing,
47 bool playing,
48 int inchannels,
49 int outchannels,
50 bool monitor,
51 const char* capture_driver_name,
52 const char* playback_driver_name,
53 jack_nframes_t capture_latency,
54 jack_nframes_t playback_latency)
56 return fDriver->Open(buffer_size,
57 samplerate, capturing,
58 playing, inchannels,
59 outchannels, monitor,
60 capture_driver_name, playback_driver_name,
61 capture_latency, playback_latency);
64 int JackThreadedDriver::Close()
66 return fDriver->Close();
69 int JackThreadedDriver::Process()
71 return fDriver->Process();
74 int JackThreadedDriver::Attach()
76 return fDriver->Attach();
79 int JackThreadedDriver::Detach()
81 return fDriver->Detach();
84 int JackThreadedDriver::Read()
86 return fDriver->Read();
89 int JackThreadedDriver::Write()
91 return fDriver->Write();
94 bool JackThreadedDriver::IsFixedBufferSize()
96 return fDriver->IsFixedBufferSize();
99 int JackThreadedDriver::SetBufferSize(jack_nframes_t buffer_size)
101 return fDriver->SetBufferSize(buffer_size);
104 int JackThreadedDriver::SetSampleRate(jack_nframes_t sample_rate)
106 return fDriver->SetSampleRate(sample_rate);
109 void JackThreadedDriver::SetMaster(bool onoff)
111 fDriver->SetMaster(onoff);
114 bool JackThreadedDriver::GetMaster()
116 return fDriver->GetMaster();
119 void JackThreadedDriver::AddSlave(JackDriverInterface* slave)
121 fDriver->AddSlave(slave);
124 void JackThreadedDriver::RemoveSlave(JackDriverInterface* slave)
126 fDriver->RemoveSlave(slave);
129 int JackThreadedDriver::ProcessReadSlaves()
131 return fDriver->ProcessReadSlaves();
134 int JackThreadedDriver::ProcessWriteSlaves()
136 return fDriver->ProcessWriteSlaves();
139 int JackThreadedDriver::ProcessRead()
141 return fDriver->ProcessRead();
144 int JackThreadedDriver::ProcessWrite()
146 return fDriver->ProcessWrite();
149 int JackThreadedDriver::ProcessReadSync()
151 return fDriver->ProcessReadSync();
154 int JackThreadedDriver::ProcessWriteSync()
156 return fDriver->ProcessWriteSync();
159 int JackThreadedDriver::ProcessReadAsync()
161 return fDriver->ProcessReadAsync();
164 int JackThreadedDriver::ProcessWriteAsync()
166 return fDriver->ProcessWriteAsync();
169 std::list<JackDriverInterface*> JackThreadedDriver::GetSlaves()
171 return fDriver->GetSlaves();
174 int JackThreadedDriver::ClientNotify(int refnum, const char* name, int notify, int sync, const char* message, int value1, int value2)
176 return fDriver->ClientNotify(refnum, name, notify, sync, message, value1, value2);
179 JackClientControl* JackThreadedDriver::GetClientControl() const
181 return fDriver->GetClientControl();
184 bool JackThreadedDriver::IsRealTime() const
186 return fDriver->IsRealTime();
189 bool JackThreadedDriver::IsRunning() const
191 return fDriver->IsRunning();
194 int JackThreadedDriver::Start()
196 jack_log("JackThreadedDriver::Start");
198 if (fDriver->Start() < 0) {
199 jack_error("Cannot start driver");
200 return -1;
202 if (fThread.StartSync() < 0) {
203 jack_error("Cannot start thread");
204 return -1;
207 return 0;
210 int JackThreadedDriver::Stop()
212 jack_log("JackThreadedDriver::Stop");
214 switch (fThread.GetStatus()) {
216 // Kill the thread in Init phase
217 case JackThread::kStarting:
218 case JackThread::kIniting:
219 if (fThread.Kill() < 0) {
220 jack_error("Cannot kill thread");
222 break;
224 // Stop when the thread cycle is finished
225 case JackThread::kRunning:
226 if (fThread.Stop() < 0) {
227 jack_error("Cannot stop thread");
229 break;
231 default:
232 break;
235 if (fDriver->Stop() < 0) {
236 jack_error("Cannot stop driver");
237 return -1;
239 return 0;
242 bool JackThreadedDriver::Execute()
244 return (Process() == 0);
247 bool JackThreadedDriver::Init()
249 if (fDriver->Initialize()) {
250 SetRealTime();
251 return true;
252 } else {
253 return false;
257 void JackThreadedDriver::SetRealTime()
259 if (fDriver->IsRealTime()) {
260 jack_log("JackThreadedDriver::Init real-time");
261 // Will do "something" on OSX only...
262 GetEngineControl()->fPeriod = GetEngineControl()->fConstraint = GetEngineControl()->fPeriodUsecs * 1000;
263 GetEngineControl()->fComputation = JackTools::ComputationMicroSec(GetEngineControl()->fBufferSize) * 1000;
264 fThread.SetParams(GetEngineControl()->fPeriod, GetEngineControl()->fComputation, GetEngineControl()->fConstraint);
265 if (fThread.AcquireSelfRealTime(GetEngineControl()->fServerPriority) < 0) {
266 jack_error("AcquireSelfRealTime error");
267 } else {
268 set_threaded_log_function();
270 } else {
271 jack_log("JackThreadedDriver::Init non-realtime");
276 } // end of namespace