21 #include "JackSystemDeps.h"    22 #include "JackAudioDriver.h"    24 #include "JackError.h"    25 #include "JackEngineControl.h"    27 #include "JackGraphManager.h"    28 #include "JackLockedEngine.h"    29 #include "JackException.h"    37 JackAudioDriver::JackAudioDriver(
const char* name, 
const char* alias, JackLockedEngine* engine, JackSynchro* table)
    38         : JackDriver(name, alias, engine, table)
    41 JackAudioDriver::~JackAudioDriver()
    44 int JackAudioDriver::SetBufferSize(jack_nframes_t buffer_size)
    47     fEngineControl->fBufferSize = buffer_size;
    48     fGraphManager->SetBufferSize(buffer_size);
    50     fEngineControl->UpdateTimeOut();
    54     return JackDriver::SetBufferSize(buffer_size);
    57 int JackAudioDriver::SetSampleRate(jack_nframes_t sample_rate)
    59     fEngineControl->fSampleRate = sample_rate;
    60     fEngineControl->UpdateTimeOut();
    63     return JackDriver::SetSampleRate(sample_rate);
    66 int JackAudioDriver::Open(jack_nframes_t buffer_size,
    67                           jack_nframes_t samplerate,
    73                           const char* capture_driver_name,
    74                           const char* playback_driver_name,
    75                           jack_nframes_t capture_latency,
    76                           jack_nframes_t playback_latency)
    78     fCaptureChannels = inchannels;
    79     fPlaybackChannels = outchannels;
    80     fWithMonitorPorts = monitor;
    81     memset(fCapturePortList, 0, 
sizeof(jack_port_id_t) * DRIVER_PORT_NUM);
    82     memset(fPlaybackPortList, 0, 
sizeof(jack_port_id_t) * DRIVER_PORT_NUM);
    83     memset(fMonitorPortList, 0, 
sizeof(jack_port_id_t) * DRIVER_PORT_NUM);
    84     return JackDriver::Open(buffer_size, samplerate, capturing, playing, inchannels, outchannels,
    85         monitor, capture_driver_name, playback_driver_name, capture_latency, playback_latency);
    88 void JackAudioDriver::UpdateLatencies()
    94     for (
int i = 0; i < fCaptureChannels; i++) {
    95         input_range.
max = input_range.
min = fEngineControl->fBufferSize + fCaptureLatency;
    96         fGraphManager->GetPort(fCapturePortList[i])->SetLatencyRange(JackCaptureLatency, &input_range);
    99     for (
int i = 0; i < fPlaybackChannels; i++) {
   100         output_range.
max = output_range.
min  = fPlaybackLatency;
   101         if (fEngineControl->fSyncMode) {
   102             output_range.
max = output_range.
min += fEngineControl->fBufferSize;
   104             output_range.
max = output_range.
min += fEngineControl->fBufferSize * 2;
   106         fGraphManager->GetPort(fPlaybackPortList[i])->SetLatencyRange(JackPlaybackLatency, &output_range);
   107         if (fWithMonitorPorts) {
   108             monitor_range.
min = monitor_range.
max = fEngineControl->fBufferSize;
   109             fGraphManager->GetPort(fMonitorPortList[i])->SetLatencyRange(JackCaptureLatency, &monitor_range);
   114 int JackAudioDriver::Attach()
   117     jack_port_id_t port_index;
   118     char name[REAL_JACK_PORT_NAME_SIZE+1];
   119     char alias[REAL_JACK_PORT_NAME_SIZE+1];
   122     jack_log(
"JackAudioDriver::Attach fBufferSize = %ld fSampleRate = %ld", fEngineControl->fBufferSize, fEngineControl->fSampleRate);
   124     for (i = 0; i < fCaptureChannels; i++) {
   125         snprintf(alias, 
sizeof(alias), 
"%s:%s:out%d", fAliasName, fCaptureDriverName, i + 1);
   126         snprintf(name, 
sizeof(name), 
"%s:capture_%d", fClientControl.fName, i + 1);
   127         if (fEngine->PortRegister(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, CaptureDriverFlags, fEngineControl->fBufferSize, &port_index) < 0) {
   128             jack_error(
"driver: cannot register port for %s", name);
   131         port = fGraphManager->GetPort(port_index);
   132         port->SetAlias(alias);
   133         fCapturePortList[i] = port_index;
   134         jack_log(
"JackAudioDriver::Attach fCapturePortList[i] port_index = %ld", port_index);
   137     for (i = 0; i < fPlaybackChannels; i++) {
   138         snprintf(alias, 
sizeof(alias), 
"%s:%s:in%d", fAliasName, fPlaybackDriverName, i + 1);
   139         snprintf(name, 
sizeof(name), 
"%s:playback_%d", fClientControl.fName, i + 1);
   140         if (fEngine->PortRegister(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, PlaybackDriverFlags, fEngineControl->fBufferSize, &port_index) < 0) {
   141             jack_error(
"driver: cannot register port for %s", name);
   144         port = fGraphManager->GetPort(port_index);
   145         port->SetAlias(alias);
   146         fPlaybackPortList[i] = port_index;
   147         jack_log(
"JackAudioDriver::Attach fPlaybackPortList[i] port_index = %ld", port_index);
   150         if (fWithMonitorPorts) {
   152             snprintf(name, 
sizeof(name), 
"%s:monitor_%u", fClientControl.fName, i + 1);
   153             if (fEngine->PortRegister(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, fEngineControl->fBufferSize, &port_index) < 0) {
   154                 jack_error(
"Cannot register monitor port for %s", name);
   157                 fMonitorPortList[i] = port_index;
   166 int JackAudioDriver::Detach()
   169     jack_log(
"JackAudioDriver::Detach");
   171     for (i = 0; i < fCaptureChannels; i++) {
   172         fEngine->PortUnRegister(fClientControl.fRefNum, fCapturePortList[i]);
   175     for (i = 0; i < fPlaybackChannels; i++) {
   176         fEngine->PortUnRegister(fClientControl.fRefNum, fPlaybackPortList[i]);
   177         if (fWithMonitorPorts) {
   178             fEngine->PortUnRegister(fClientControl.fRefNum, fMonitorPortList[i]);
   185 int JackAudioDriver::Write()
   187     for (
int i = 0; i < fPlaybackChannels; i++) {
   188         if (fGraphManager->GetConnectionsNum(fPlaybackPortList[i]) > 0) {
   189             jack_default_audio_sample_t* buffer = GetOutputBuffer(i);
   190             int size = 
sizeof(jack_default_audio_sample_t) * fEngineControl->fBufferSize;
   192             if (fWithMonitorPorts && fGraphManager->GetConnectionsNum(fMonitorPortList[i]) > 0) {
   193                 memcpy(GetMonitorBuffer(i), buffer, size);
   200 int JackAudioDriver::Process()
   202     return (fEngineControl->fSyncMode) ? ProcessSync() : ProcessAsync();
   210 int JackAudioDriver::ProcessAsync()
   214         jack_error(
"JackAudioDriver::ProcessAsync: read error, stopping...");
   220         jack_error(
"JackAudioDriver::ProcessAsync: write error, stopping...");
   228     JackDriver::CycleTakeEndTime();
   232 void JackAudioDriver::ProcessGraphAsync()
   236         ProcessGraphAsyncMaster();
   238         ProcessGraphAsyncSlave();
   246 void JackAudioDriver::ProcessGraphAsyncMaster()
   249     if (!fEngine->Process(fBeginDateUst, fEndDateUst)) {
   250         jack_error(
"JackAudioDriver::ProcessGraphAsyncMaster: Process error");
   253     if (ResumeRefNum() < 0) {
   254         jack_error(
"JackAudioDriver::ProcessGraphAsyncMaster: ResumeRefNum error");
   257     if (ProcessReadSlaves() < 0) {
   258         jack_error(
"JackAudioDriver::ProcessGraphAsyncMaster: ProcessReadSlaves error");
   261     if (ProcessWriteSlaves() < 0) {
   262         jack_error(
"JackAudioDriver::ProcessGraphAsyncMaster: ProcessWriteSlaves error");
   272 void JackAudioDriver::ProcessGraphAsyncSlave()
   274     if (ResumeRefNum() < 0) {
   275         jack_error(
"JackAudioDriver::ProcessGraphAsyncSlave: ResumeRefNum error");
   284 int JackAudioDriver::ProcessSync()
   288         jack_error(
"JackAudioDriver::ProcessSync: read error, stopping...");
   297         jack_error(
"JackAudioDriver::ProcessSync: write error, stopping...");
   302     JackDriver::CycleTakeEndTime();
   306 void JackAudioDriver::ProcessGraphSync()
   310         ProcessGraphSyncMaster();
   312         ProcessGraphSyncSlave();
   320 void JackAudioDriver::ProcessGraphSyncMaster()
   323     if (fEngine->Process(fBeginDateUst, fEndDateUst)) {
   325         if (ResumeRefNum() < 0) {
   326             jack_error(
"JackAudioDriver::ProcessGraphSyncMaster: ResumeRefNum error");
   329         if (ProcessReadSlaves() < 0) {
   330             jack_error(
"JackAudioDriver::ProcessGraphSync: ProcessReadSlaves error, engine may now behave abnormally!!");
   333         if (ProcessWriteSlaves() < 0) {
   334             jack_error(
"JackAudioDriver::ProcessGraphSync: ProcessWriteSlaves error, engine may now behave abnormally!!");
   338         if (SuspendRefNum() < 0) {
   339             jack_error(
"JackAudioDriver::ProcessGraphSync: SuspendRefNum error, engine may now behave abnormally!!");
   343         jack_error(
"JackAudioDriver::ProcessGraphSync: Process error");
   351 void JackAudioDriver::ProcessGraphSyncSlave()
   353     if (ResumeRefNum() < 0) {
   354         jack_error(
"JackAudioDriver::ProcessGraphSyncSlave: ResumeRefNum error");
   358 jack_default_audio_sample_t* JackAudioDriver::GetInputBuffer(
int port_index)
   360     return fCapturePortList[port_index]
   361         ? (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fCapturePortList[port_index], fEngineControl->fBufferSize)
   365 jack_default_audio_sample_t* JackAudioDriver::GetOutputBuffer(
int port_index)
   367     return fPlaybackPortList[port_index]
   368         ? (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fPlaybackPortList[port_index], fEngineControl->fBufferSize)
   372 jack_default_audio_sample_t* JackAudioDriver::GetMonitorBuffer(
int port_index)
   374     return fPlaybackPortList[port_index]
   375         ? (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fMonitorPortList[port_index], fEngineControl->fBufferSize)
   379 int JackAudioDriver::ClientNotify(
int refnum, 
const char* name, 
int notify, 
int sync, 
const char* message, 
int value1, 
int value2)
   383         case kLatencyCallback:
   384             HandleLatencyCallback(value1);
   388             JackDriver::ClientNotify(refnum, name, notify, sync, message, value1, value2);
   395 void JackAudioDriver::HandleLatencyCallback(
int status)
   397     jack_latency_callback_mode_t mode = (status == 0) ? JackCaptureLatency : JackPlaybackLatency;
   399     for (
int i = 0; i < fCaptureChannels; i++) {
   400         if (mode == JackPlaybackLatency) {
   401            fGraphManager->RecalculateLatency(fCapturePortList[i], mode);
   405     for (
int i = 0; i < fPlaybackChannels; i++) {
   406         if (mode == JackCaptureLatency) {
   407             fGraphManager->RecalculateLatency(fPlaybackPortList[i], mode);
 
SERVER_EXPORT void jack_error(const char *fmt,...)
SERVER_EXPORT void jack_log(const char *fmt,...)