/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code 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
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
#define USE_ERROR
#define USE_TRACE
#include "PLATFORM_API_BsdOS_ALSA_PCMUtils.h"
#include "PLATFORM_API_BsdOS_ALSA_CommonUtils.h"
#include "DirectAudio.h"
#if USE_DAUDIO == TRUE
// GetPosition method 1: based on how many bytes are passed to the kernel driver
// + does not need much processor resources
// - not very exact, "jumps"
// GetPosition method 2: ask kernel about actual position of playback.
// - very exact
// - switch to kernel layer for each call
// GetPosition method 3: use snd_pcm_avail() call - not yet in official ALSA
// quick tests on a Pentium 200MMX showed max. 1.5% processor usage
// for playing back a CD-quality file and printing 20x per second a line
// on the console with the current time. So I guess performance is not such a
// factor here.
//#define GET_POSITION_METHOD1
#define GET_POSITION_METHOD2
// The default time for a period in microseconds.
// For very small buffers, only 2 periods are used.
///// implemented functions of DirectAudio.h
return (INT32) getAudioDeviceCount();
}
INT32 DAUDIO_GetDirectAudioDeviceDescription(INT32 mixerIndex, DirectAudioDeviceDescription* description) {
return getAudioDeviceDescriptionByIndex(&adesc);
}
// returns
// 6: for anything above 24-bit
// 5: for 4 bytes sample size, 24-bit
// 4: for 3 bytes sample size, 24-bit
// 3: for 3 bytes sample size, 20-bit
// 2: for 2 bytes sample size, 16-bit
// 1: for 1 byte sample size, 8-bit
// 0: for anything else
if (sampleSizeInBytes == 3) {
}
return 0;
}
switch(bitIndex) {
case 1: return 1;
case 2: return 2;
case 3: /* fall through */
case 4: return 3;
case 5: return 4;
}
return sampleSizeInBytes;
}
switch(bitIndex) {
case 1: return 8;
case 2: return 16;
case 3: return 20;
case 4: /* fall through */
case 5: return 24;
}
return significantBits;
}
int ret;
return;
}
if (ret != 0) {
} else {
if (ret != 0) {
} else {
/* snd_pcm_hw_params_any can return a positive value on success too */
if (ret < 0) {
} else {
/* for the logic following this code, set ret to 0 to indicate success */
ret = 0;
}
}
if (ret == 0) {
if (ret != 0) {
}
}
if (ret == 0) {
if (ret != 0) {
}
}
// since we queried the hw: device, for many soundcards, it will only
// report the maximum number of channels (which is the only way to talk
// to the hw: device). Since we will, however, open the plughw: device
// when opening the Source/TargetDataLine, we can safely assume that
// also the channels 1..maxChannels are available.
#ifdef ALSA_PCM_USE_PLUGHW
minChannels = 1;
#endif
if (ret == 0) {
// plughw: supports any sample rate
rate = -1;
// format exists
// now if we use plughw:, we can use any bit size below the
// natively supported ones. Some ALSA drivers only support the maximum
// bit size, so we add any sample rates below the reported one.
// E.g. this iteration reports support for 16-bit.
// getBitIndex will return 2, so it will add entries for
// 16-bit (bitIndex=2) and in the next do-while loop iteration,
// it will decrease bitIndex and will therefore add 8-bit support.
do {
if (bitIndex == 0
|| bitIndex == MAX_BIT_INDEX
|| !handledBits[bitIndex]) {
// avoid too many channels explicitly listed
// just add -1, min, and max
} else {
}
}
}
#ifndef ALSA_PCM_USE_PLUGHW
// without plugin, do not add fake formats
break;
#endif
} while (--bitIndex > 0);
} else {
}
} else {
//TRACE1("Format %d not supported\n", format);
}
} // for loop
}
}
}
/** Workaround for cr 7033899, 7030629:
* dmix plugin doesn't like flush (snd_pcm_drop) when the buffer is empty
* (just opened, underruned or already flushed).
* Sometimes it causes PCM falls to -EBADFD error,
* sometimes causes bufferSize change.
* To prevent unnecessary flushes AlsaPcmInfo::isRunning & isFlushed are used.
*/
/* ******* ALSA PCM INFO ******************** */
typedef struct tag_AlsaPcmInfo {
int bufferSizeInBytes;
unsigned int periods;
#ifdef GET_POSITION_METHOD2
// to be used exclusively by getBytePosition!
#endif
} AlsaPcmInfo;
int ret;
int threshold;
if (useThreshold) {
// start device whenever anything is written to the buffer
threshold = 1;
} else {
// never start the device automatically
}
if (ret < 0) {
return FALSE;
}
return TRUE;
}
int ret = 0;
ret = -1;
}
if (ret == 0) {
// commit it
if (ret < 0) {
}
}
}
// returns TRUE if successful
float sampleRate,
int channels,
int bufferSizeInFrames,
/* choose all parameters */
if (ret < 0) {
return FALSE;
}
if (ret < 0) {
return FALSE;
}
/* set the sample format */
if (ret < 0) {
return FALSE;
}
/* set the count of channels */
if (ret < 0) {
return FALSE;
}
/* set the stream rate */
dir = 0;
if (ret < 0) {
return FALSE;
}
return FALSE;
}
/* set the buffer time */
ret = snd_pcm_hw_params_set_buffer_size_near(info->handle, info->hwParams, &alsaBufferSizeInFrames);
if (ret < 0) {
ERROR2("Unable to set buffer size to %d frames: %s\n",
return FALSE;
}
bufferSizeInFrames = (int) alsaBufferSizeInFrames;
/* set the period time */
if (bufferSizeInFrames > 1024) {
dir = 0;
if (ret < 0) {
return FALSE;
}
} else {
/* set the period count for very small buffer sizes to 2 */
dir = 0;
periods = 2;
if (ret < 0) {
return FALSE;
}
}
/* write the parameters to device */
if (ret < 0) {
return FALSE;
}
return TRUE;
}
// returns 1 if successful
int ret;
/* get the current swparams */
if (ret < 0) {
return FALSE;
}
/* never start the transfer automatically */
return FALSE;
}
/* allow the transfer when at least period_size samples can be processed */
if (ret < 0) {
return FALSE;
}
/* write the parameters to the playback device */
if (ret < 0) {
return FALSE;
}
return TRUE;
}
int dir;
int ret = 0;
/* snd_pcm_uframes_t is 64 bit on 64-bit systems */
TRACE0("> DAUDIO_Open\n");
#ifdef USE_TRACE
// for using ALSA debug dump methods
if (ALSA_OUTPUT == NULL) {
}
#endif
if (!info) {
ERROR0("Out of memory\n");
return NULL;
}
// initial values are: stopped, flushed
if (ret == 0) {
// set to blocking mode
if (ret != 0) {
} else {
ret = -1;
if (setHWParams(info,
format)) {
if (ret < 0) {
}
TRACE3(" DAUDIO_Open: period size = %d frames, periods = %d. Buffer size: %d bytes.\n",
}
}
}
if (ret == 0) {
// set software parameters
if (ret != 0) {
} else {
if (!setSWParams(info)) {
ret = -1;
}
}
}
if (ret == 0) {
// prepare device
if (ret < 0) {
}
}
#ifdef GET_POSITION_METHOD2
if (ret == 0) {
if (ret != 0) {
}
}
#endif
}
if (ret != 0) {
} else {
// set to non-blocking mode
TRACE1("< DAUDIO_Open: Opened device successfully. Handle=%p\n",
}
return (void*) info;
}
#ifdef USE_TRACE
if (state == SND_PCM_STATE_OPEN) {
TRACE0("State: SND_PCM_STATE_OPEN\n");
}
else if (state == SND_PCM_STATE_SETUP) {
TRACE0("State: SND_PCM_STATE_SETUP\n");
}
else if (state == SND_PCM_STATE_PREPARED) {
TRACE0("State: SND_PCM_STATE_PREPARED\n");
}
else if (state == SND_PCM_STATE_RUNNING) {
TRACE0("State: SND_PCM_STATE_RUNNING\n");
}
else if (state == SND_PCM_STATE_XRUN) {
TRACE0("State: SND_PCM_STATE_XRUN\n");
}
else if (state == SND_PCM_STATE_DRAINING) {
TRACE0("State: SND_PCM_STATE_DRAINING\n");
}
else if (state == SND_PCM_STATE_PAUSED) {
TRACE0("State: SND_PCM_STATE_PAUSED\n");
}
else if (state == SND_PCM_STATE_SUSPENDED) {
TRACE0("State: SND_PCM_STATE_SUSPENDED\n");
}
}
#endif
int ret;
TRACE0("> DAUDIO_Start\n");
// set to blocking mode
// set start mode so that it always starts as soon as data is there
if (state == SND_PCM_STATE_PAUSED) {
// in case it was stopped previously
TRACE0(" Un-pausing...\n");
if (ret != 0) {
}
}
if (state == SND_PCM_STATE_SUSPENDED) {
TRACE0(" Resuming...\n");
if (ret < 0) {
}
}
}
if (state == SND_PCM_STATE_SETUP) {
TRACE0("need to call prepare again...\n");
// prepare device
if (ret < 0) {
}
}
// in case there is still data in the buffers
if (ret != 0) {
}
}
// set to non-blocking mode
if (ret != 0) {
}
#ifdef USE_TRACE
#endif
|| (state == SND_PCM_STATE_RUNNING)
|| (state == SND_PCM_STATE_XRUN)
|| (state == SND_PCM_STATE_SUSPENDED);
if (ret) {
// source line should keep isFlushed value until Write() is called;
// for target data line reset it right now.
if (!isSource) {
}
}
}
int ret;
TRACE0("> DAUDIO_Stop\n");
// set to blocking mode
setStartThreshold(info, FALSE /* don't use threshold */); // device will not start after buffer xrun
// set to non-blocking mode
if (ret != 0) {
return FALSE;
}
TRACE0("< DAUDIO_Stop success\n");
return TRUE;
}
TRACE0("DAUDIO_Close\n");
}
}
}
#ifdef GET_POSITION_METHOD2
if (info->positionStatus) {
}
#endif
}
}
/*
* Underrun and suspend recovery
* returns
* 0: exit native and return 0
* -1: error - exit native with return value -1
*/
int ret;
if (ret < 0) {
return -1;
}
return 1;
TRACE0("xrun_recovery: suspended.\n");
if (ret < 0) {
return 0; /* wait until the suspend flag is released */
}
return -1;
}
if (ret < 0) {
return -1;
}
return 1;
TRACE0("xrun_recovery: EAGAIN try again flag.\n");
return 0;
}
return -1;
}
// returns -1 on error
/* sanity */
ERROR2(" DAUDIO_Write: byteSize=%d, frameSize=%d!\n",
TRACE0("< DAUDIO_Write returning -1\n");
return -1;
}
//frameSize = snd_pcm_bytes_to_frames(info->handle, byteSize);
do {
if (writtenFrames < 0) {
if (ret <= 0) {
return ret;
}
if (count-- <= 0) {
return -1;
}
} else {
break;
}
} while (TRUE);
//ret = snd_pcm_frames_to_bytes(info->handle, writtenFrames);
if (writtenFrames > 0) {
// reset "flushed" flag
}
return ret;
}
// returns -1 on error
/*TRACE3(" info=%p, data=%p, byteSize=%d\n",
(void*) info, (void*) data, (int) byteSize);
TRACE2(" info->frameSize=%d, info->handle=%p\n",
(int) info->frameSize, (void*) info->handle);
*/
/* sanity */
ERROR2(" DAUDIO_Read: byteSize=%d, frameSize=%d!\n",
TRACE0("< DAUDIO_Read returning -1\n");
return -1;
}
// PCM has nothing to read
return 0;
}
//frameSize = snd_pcm_bytes_to_frames(info->handle, byteSize);
do {
if (readFrames < 0) {
if (ret <= 0) {
return ret;
}
if (count-- <= 0) {
return -1;
}
} else {
break;
}
} while (TRUE);
//ret = snd_pcm_frames_to_bytes(info->handle, readFrames);
return ret;
}
return info->bufferSizeInBytes;
}
//printState(state);
//TRACE1("Still draining: %s\n", (state != SND_PCM_STATE_XRUN)?"TRUE":"FALSE");
}
int ret;
TRACE0("DAUDIO_Flush\n");
// nothing to drop
return 1;
}
if (ret != 0) {
return FALSE;
}
}
return ret;
}
int ret;
// if in xrun state then we have the entire buffer available,
// not 0 as alsa reports
} else {
if (availableInFrames < 0) {
ret = 0;
} else {
//ret = snd_pcm_frames_to_bytes(info->handle, availableInFrames);
}
}
return ret;
}
INT64 estimatePositionFromAvail(AlsaPcmInfo* info, int isSource, INT64 javaBytePos, int availInBytes) {
// estimate the current position with the buffer size and
// the available bytes to read or write in the buffer.
// not an elegant solution - bytePos will stop on xruns,
// and in race conditions it may jump backwards
// Advantage is that it is indeed based on the samples that go through
// the system (rather than time-based methods)
if (isSource) {
// javaBytePos is the position that is reached when the current
// buffer is played completely
} else {
// javaBytePos is the position that was when the current buffer was empty
}
}
int ret;
#ifdef GET_POSITION_METHOD2
// note: slight race condition if this is called simultaneously from 2 threads
if (ret != 0) {
} else {
// calculate from time value, or from available bytes
}
#endif
#ifdef GET_POSITION_METHOD3
if (ret != 0) {
} else {
}
#endif
#ifdef GET_POSITION_METHOD1
#endif
}
//printf("getbyteposition: javaBytePos=%d , return=%d\n", (int) javaBytePos, (int) result);
return result;
}
/* save to ignore, since GetBytePosition
* takes the javaBytePos param into account
*/
}
// never need servicing on Bsd
return FALSE;
}
// never need servicing on Bsd
}
#endif // USE_DAUDIO