JackDriver.cpp 6.02 KB
Newer Older
sletz's avatar
sletz committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
Copyright (C) 2001 Paul Davis 
Copyright (C) 2004-2006 Grame

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
Typo    
sletz committed
19
20
21
22
*/

#ifdef WIN32 
#pragma warning (disable : 4786)
sletz's avatar
sletz committed
23
#endif
sletz's avatar
sletz committed
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

#include "JackDriver.h"
#include "JackTime.h"
#include "JackError.h"
#include "JackPort.h"
#include "JackGraphManager.h"
#include "JackGlobals.h"
#include "JackEngineControl.h"
#include "JackClientControl.h"
#include "JackEngine.h"
#include <math.h>
#include <assert.h>

using namespace std;

namespace Jack
{

JackDriver::JackDriver(const char* name, JackEngine* engine, JackSynchro** table)
{
    assert(strlen(name) < JACK_CLIENT_NAME_SIZE);
    fSynchroTable = table;
    fClientControl = new JackClientControl(name);
    fEngine = engine;
    fGraphManager = NULL;
    fLastWaitUst = 0;
    fIsMaster = true;
}

JackDriver::JackDriver()
{
    fSynchroTable = NULL;
    fClientControl = NULL;
    fEngine = NULL;
    fGraphManager = NULL;
    fLastWaitUst = 0;
    fIsMaster = true;
}

JackDriver::~JackDriver()
{
    JackLog("~JackDriver\n");
    delete fClientControl;
}

int JackDriver::Open()
{
    int refnum = -1;

73
	if (fEngine->ClientInternalOpen(fClientControl->fName, &refnum, &fEngineControl, &fGraphManager, this, false) != 0) {
sletz's avatar
sletz committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
        jack_error("Cannot allocate internal client for audio driver");
        return -1;
    }

    fClientControl->fRefNum = refnum;
    fClientControl->fActive = true;
    fGraphManager->DirectConnect(fClientControl->fRefNum, fClientControl->fRefNum); // Connect driver to itself for sync

    /*
    	In ASYNC mode, the server does not synchronize itself on the output drivers, thus it would never "consume" the activations.
    	The synchronization primitives for drivers are setup in "flush" mode that to not keep unneeded activations.
    */
    if (!fEngineControl->fSyncMode) {
        fSynchroTable[AUDIO_DRIVER_REFNUM]->SetFlush(true);
        fSynchroTable[FREEWHEEL_DRIVER_REFNUM]->SetFlush(true);
        fSynchroTable[LOOPBACK_DRIVER_REFNUM]->SetFlush(true);
    }
    return 0;
}

int JackDriver::Open(jack_nframes_t nframes,
                     jack_nframes_t samplerate,
                     int capturing,
                     int 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)
{
    JackLog("JackDriver::Open capture_driver_name = %s\n", capture_driver_name);
    JackLog("JackDriver::Open playback_driver_name = %s\n", playback_driver_name);
    int refnum = -1;

110
    if (fEngine->ClientInternalOpen(fClientControl->fName, &refnum, &fEngineControl, &fGraphManager, this, false) != 0) {
sletz's avatar
sletz committed
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
        jack_error("Cannot allocate internal client for audio driver");
        return -1;
    }

    fClientControl->fRefNum = refnum;
    fClientControl->fActive = true;
    fEngineControl->fBufferSize = nframes;
    fEngineControl->fSampleRate = samplerate;
    fCaptureLatency = capture_latency;
    fPlaybackLatency = playback_latency;

    assert(strlen(capture_driver_name) < JACK_CLIENT_NAME_SIZE);
    assert(strlen(playback_driver_name) < JACK_CLIENT_NAME_SIZE);

    strcpy(fCaptureDriverName, capture_driver_name);
    strcpy(fPlaybackDriverName, playback_driver_name);

    fEngineControl->fPeriodUsecs = (jack_time_t)floor((((float)nframes) / (float)samplerate) * 1000000.0f);
sletz's avatar
Typo    
sletz committed
129
    if (fEngineControl->fTimeOutUsecs == 0)  /* usecs; if zero, use 2 period size. */
sletz's avatar
sletz committed
130
        fEngineControl->fTimeOutUsecs = (jack_time_t)(2.f * fEngineControl->fPeriodUsecs);
sletz's avatar
sletz committed
131

sletz's avatar
sletz committed
132
    fGraphManager->SetBufferSize(nframes);
sletz's avatar
sletz committed
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
    fGraphManager->DirectConnect(fClientControl->fRefNum, fClientControl->fRefNum); // Connect driver to itself for sync

    /*
    	In ASYNC mode, the server does not synchronize itself on the output drivers, thus it would never "consume" the activations.
    	The synchronization primitives for drivers are setup in "flush" mode that to not keep unneeded activations.
    */
    if (!fEngineControl->fSyncMode) {
        fSynchroTable[AUDIO_DRIVER_REFNUM]->SetFlush(true);
        fSynchroTable[FREEWHEEL_DRIVER_REFNUM]->SetFlush(true);
        fSynchroTable[LOOPBACK_DRIVER_REFNUM]->SetFlush(true);
    }
    return 0;
}

int JackDriver::Close()
{
    JackLog("JackDriver::Close\n");
    fGraphManager->DirectDisconnect(fClientControl->fRefNum, fClientControl->fRefNum); // Disconnect driver from itself for sync
    fClientControl->fActive = false;
152
    return fEngine->ClientInternalClose(fClientControl->fRefNum, false);
sletz's avatar
sletz committed
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
}

bool JackDriver::IsRealTime()
{
    return fEngineControl->fRealTime;
}

JackClientControl* JackDriver::GetClientControl() const
{
    return fClientControl;
}

void JackDriver::NotifyXRun(jack_time_t callback_usecs)
{
    fEngine->NotifyXRun(callback_usecs);
}

void JackDriverClient::SetMaster(bool onoff)
{
    fIsMaster = onoff;
}

bool JackDriverClient::GetMaster()
{
    return fIsMaster;
}

void JackDriverClient::AddSlave(JackDriverInterface* slave)
{
    fSlaveList.push_back(slave);
}

void JackDriverClient::RemoveSlave(JackDriverInterface* slave)
{
    fSlaveList.remove(slave);
}

190
int JackDriverClient::ProcessSlaves()
sletz's avatar
sletz committed
191
{
192
	int res = 0;
sletz's avatar
sletz committed
193
194
195
    list<JackDriverInterface*>::const_iterator it;
    for (it = fSlaveList.begin(); it != fSlaveList.end(); it++) {
        JackDriverInterface* slave = *it;
sletz's avatar
sletz committed
196
197
198
        if (slave->Process() < 0)
			res = -1;
    }
199
	return res;
sletz's avatar
sletz committed
200
201
202
}

} // end of namespace