2008-07-29 23:50:14 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is the Mozilla Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2007
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Chris Double <chris.double@double.co.nz>
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include "prlog.h"
|
|
|
|
#include "prmem.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsAudioStream.h"
|
2008-11-10 14:45:09 -08:00
|
|
|
#include "nsAlgorithm.h"
|
2008-07-29 23:50:14 -07:00
|
|
|
extern "C" {
|
2008-10-15 20:16:29 -07:00
|
|
|
#include "sydneyaudio/sydney_audio.h"
|
2008-07-29 23:50:14 -07:00
|
|
|
}
|
2010-04-01 20:03:07 -07:00
|
|
|
#include "mozilla/TimeStamp.h"
|
|
|
|
|
2010-08-11 18:52:36 -07:00
|
|
|
#if defined(XP_MACOSX)
|
|
|
|
#define SA_PER_STREAM_VOLUME 1
|
|
|
|
#endif
|
|
|
|
|
2010-04-01 20:03:07 -07:00
|
|
|
using mozilla::TimeStamp;
|
2008-07-29 23:50:14 -07:00
|
|
|
|
|
|
|
#ifdef PR_LOGGING
|
|
|
|
PRLogModuleInfo* gAudioStreamLog = nsnull;
|
|
|
|
#endif
|
|
|
|
|
2008-11-06 12:53:20 -08:00
|
|
|
#define FAKE_BUFFER_SIZE 176400
|
|
|
|
|
2008-10-28 09:17:59 -07:00
|
|
|
void nsAudioStream::InitLibrary()
|
2008-07-29 23:50:14 -07:00
|
|
|
{
|
|
|
|
#ifdef PR_LOGGING
|
|
|
|
gAudioStreamLog = PR_NewLogModule("nsAudioStream");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsAudioStream::ShutdownLibrary()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAudioStream::nsAudioStream() :
|
|
|
|
mVolume(1.0),
|
|
|
|
mAudioHandle(0),
|
|
|
|
mRate(0),
|
2008-11-06 12:53:20 -08:00
|
|
|
mChannels(0),
|
2010-04-01 20:03:07 -07:00
|
|
|
mFormat(FORMAT_S16_LE),
|
|
|
|
mPaused(PR_FALSE)
|
2008-07-29 23:50:14 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-03-24 19:19:58 -07:00
|
|
|
nsAudioStream::~nsAudioStream()
|
|
|
|
{
|
|
|
|
Shutdown();
|
|
|
|
}
|
|
|
|
|
2010-08-18 10:04:31 -07:00
|
|
|
void nsAudioStream::Init(PRInt32 aNumChannels, PRInt32 aRate, SampleFormat aFormat)
|
2008-07-29 23:50:14 -07:00
|
|
|
{
|
|
|
|
mRate = aRate;
|
|
|
|
mChannels = aNumChannels;
|
2008-11-10 00:12:13 -08:00
|
|
|
mFormat = aFormat;
|
2008-07-29 23:50:14 -07:00
|
|
|
if (sa_stream_create_pcm(reinterpret_cast<sa_stream_t**>(&mAudioHandle),
|
|
|
|
NULL,
|
|
|
|
SA_MODE_WRONLY,
|
2009-01-29 23:27:43 -08:00
|
|
|
SA_PCM_FORMAT_S16_NE,
|
2008-07-29 23:50:14 -07:00
|
|
|
aRate,
|
|
|
|
aNumChannels) != SA_SUCCESS) {
|
|
|
|
mAudioHandle = nsnull;
|
|
|
|
PR_LOG(gAudioStreamLog, PR_LOG_ERROR, ("nsAudioStream: sa_stream_create_pcm error"));
|
2010-08-18 10:04:31 -07:00
|
|
|
return;
|
2008-07-29 23:50:14 -07:00
|
|
|
}
|
2010-08-18 10:04:31 -07:00
|
|
|
|
2008-11-10 00:12:13 -08:00
|
|
|
if (sa_stream_open(static_cast<sa_stream_t*>(mAudioHandle)) != SA_SUCCESS) {
|
|
|
|
sa_stream_destroy(static_cast<sa_stream_t*>(mAudioHandle));
|
2008-07-29 23:50:14 -07:00
|
|
|
mAudioHandle = nsnull;
|
|
|
|
PR_LOG(gAudioStreamLog, PR_LOG_ERROR, ("nsAudioStream: sa_stream_open error"));
|
2010-08-18 10:04:31 -07:00
|
|
|
return;
|
2008-07-29 23:50:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-28 09:17:59 -07:00
|
|
|
void nsAudioStream::Shutdown()
|
2008-07-29 23:50:14 -07:00
|
|
|
{
|
|
|
|
if (!mAudioHandle)
|
2008-10-28 09:17:59 -07:00
|
|
|
return;
|
2008-07-29 23:50:14 -07:00
|
|
|
|
2008-11-10 00:12:13 -08:00
|
|
|
sa_stream_destroy(static_cast<sa_stream_t*>(mAudioHandle));
|
2008-07-29 23:50:14 -07:00
|
|
|
mAudioHandle = nsnull;
|
|
|
|
}
|
|
|
|
|
2010-08-18 10:04:31 -07:00
|
|
|
void nsAudioStream::Write(const void* aBuf, PRUint32 aCount, PRBool aBlocking)
|
2008-07-29 23:50:14 -07:00
|
|
|
{
|
2008-11-10 00:12:13 -08:00
|
|
|
NS_ABORT_IF_FALSE(aCount % mChannels == 0,
|
|
|
|
"Buffer size must be divisible by channel count");
|
2010-04-01 20:03:07 -07:00
|
|
|
NS_ASSERTION(!mPaused, "Don't write audio when paused, you'll block");
|
2008-11-10 00:12:13 -08:00
|
|
|
|
2008-12-15 15:06:22 -08:00
|
|
|
PRUint32 offset = mBufferOverflow.Length();
|
2010-04-01 20:03:07 -07:00
|
|
|
PRUint32 count = aCount + offset;
|
2008-11-06 12:53:20 -08:00
|
|
|
|
2008-07-29 23:50:14 -07:00
|
|
|
if (!mAudioHandle)
|
2010-08-18 10:04:31 -07:00
|
|
|
return;
|
2008-07-29 23:50:14 -07:00
|
|
|
|
2008-12-15 15:06:22 -08:00
|
|
|
nsAutoArrayPtr<short> s_data(new short[count]);
|
2008-07-29 23:50:14 -07:00
|
|
|
|
|
|
|
if (s_data) {
|
2008-12-15 15:06:22 -08:00
|
|
|
for (PRUint32 i=0; i < offset; ++i) {
|
|
|
|
s_data[i] = mBufferOverflow.ElementAt(i);
|
|
|
|
}
|
|
|
|
mBufferOverflow.Clear();
|
|
|
|
|
2008-11-10 00:12:13 -08:00
|
|
|
switch (mFormat) {
|
2008-12-15 15:06:22 -08:00
|
|
|
case FORMAT_U8: {
|
|
|
|
const PRUint8* buf = static_cast<const PRUint8*>(aBuf);
|
|
|
|
PRInt32 volume = PRInt32((1 << 16) * mVolume);
|
|
|
|
for (PRUint32 i = 0; i < aCount; ++i) {
|
|
|
|
s_data[i + offset] = short(((PRInt32(buf[i]) - 128) * volume) >> 8);
|
|
|
|
}
|
|
|
|
break;
|
2008-07-29 23:50:14 -07:00
|
|
|
}
|
2008-12-15 15:06:22 -08:00
|
|
|
case FORMAT_S16_LE: {
|
|
|
|
const short* buf = static_cast<const short*>(aBuf);
|
|
|
|
PRInt32 volume = PRInt32((1 << 16) * mVolume);
|
|
|
|
for (PRUint32 i = 0; i < aCount; ++i) {
|
2009-01-29 23:27:43 -08:00
|
|
|
short s = buf[i];
|
|
|
|
#if defined(IS_BIG_ENDIAN)
|
|
|
|
s = ((s & 0x00ff) << 8) | ((s & 0xff00) >> 8);
|
|
|
|
#endif
|
|
|
|
s_data[i + offset] = short((PRInt32(s) * volume) >> 16);
|
2008-12-15 15:06:22 -08:00
|
|
|
}
|
|
|
|
break;
|
2008-07-29 23:50:14 -07:00
|
|
|
}
|
2009-01-29 23:27:43 -08:00
|
|
|
case FORMAT_FLOAT32: {
|
2008-12-15 15:06:22 -08:00
|
|
|
const float* buf = static_cast<const float*>(aBuf);
|
|
|
|
for (PRUint32 i = 0; i < aCount; ++i) {
|
|
|
|
float scaled_value = floorf(0.5 + 32768 * buf[i] * mVolume);
|
|
|
|
if (buf[i] < 0.0) {
|
|
|
|
s_data[i + offset] = (scaled_value < -32768.0) ?
|
|
|
|
-32768 :
|
|
|
|
short(scaled_value);
|
|
|
|
} else {
|
|
|
|
s_data[i+offset] = (scaled_value > 32767.0) ?
|
|
|
|
32767 :
|
|
|
|
short(scaled_value);
|
|
|
|
}
|
2008-11-10 00:12:13 -08:00
|
|
|
}
|
2008-12-15 15:06:22 -08:00
|
|
|
break;
|
2008-11-10 00:12:13 -08:00
|
|
|
}
|
2008-11-06 12:53:20 -08:00
|
|
|
}
|
2008-12-15 15:06:22 -08:00
|
|
|
|
2010-04-01 20:03:07 -07:00
|
|
|
if (!aBlocking) {
|
|
|
|
// We're running in non-blocking mode, crop the data to the amount
|
|
|
|
// which is available in the audio buffer, and save the rest for
|
|
|
|
// subsequent calls.
|
|
|
|
PRUint32 available = Available();
|
|
|
|
if (available < count) {
|
|
|
|
mBufferOverflow.AppendElements(s_data.get() + available, (count - available));
|
|
|
|
count = available;
|
|
|
|
}
|
2008-11-06 12:53:20 -08:00
|
|
|
}
|
|
|
|
|
2008-12-15 15:06:22 -08:00
|
|
|
if (sa_stream_write(static_cast<sa_stream_t*>(mAudioHandle),
|
2010-04-01 20:03:07 -07:00
|
|
|
s_data.get(),
|
|
|
|
count * sizeof(short)) != SA_SUCCESS)
|
|
|
|
{
|
2008-11-06 12:53:20 -08:00
|
|
|
PR_LOG(gAudioStreamLog, PR_LOG_ERROR, ("nsAudioStream: sa_stream_write error"));
|
|
|
|
Shutdown();
|
|
|
|
}
|
2008-07-29 23:50:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-01 20:03:07 -07:00
|
|
|
PRUint32 nsAudioStream::Available()
|
2008-10-19 00:39:21 -07:00
|
|
|
{
|
2008-11-06 12:53:20 -08:00
|
|
|
// If the audio backend failed to open, lie and say we'll accept some
|
|
|
|
// data.
|
2008-10-19 00:39:21 -07:00
|
|
|
if (!mAudioHandle)
|
2008-11-06 12:53:20 -08:00
|
|
|
return FAKE_BUFFER_SIZE;
|
2008-10-19 00:39:21 -07:00
|
|
|
|
|
|
|
size_t s = 0;
|
2009-05-14 18:29:05 -07:00
|
|
|
if (sa_stream_get_write_size(static_cast<sa_stream_t*>(mAudioHandle), &s) != SA_SUCCESS)
|
|
|
|
return 0;
|
|
|
|
|
2008-10-19 00:39:21 -07:00
|
|
|
return s / sizeof(short);
|
|
|
|
}
|
|
|
|
|
2008-10-28 09:17:59 -07:00
|
|
|
void nsAudioStream::SetVolume(float aVolume)
|
2008-07-29 23:50:14 -07:00
|
|
|
{
|
2008-11-10 00:12:13 -08:00
|
|
|
NS_ASSERTION(aVolume >= 0.0 && aVolume <= 1.0, "Invalid volume");
|
2010-08-11 18:52:36 -07:00
|
|
|
#if defined(SA_PER_STREAM_VOLUME)
|
|
|
|
if (sa_stream_set_volume_abs(static_cast<sa_stream_t*>(mAudioHandle), aVolume) != SA_SUCCESS) {
|
|
|
|
PR_LOG(gAudioStreamLog, PR_LOG_ERROR, ("nsAudioStream: sa_stream_set_volume_abs error"));
|
|
|
|
Shutdown();
|
|
|
|
}
|
|
|
|
#else
|
2008-07-29 23:50:14 -07:00
|
|
|
mVolume = aVolume;
|
2010-08-11 18:52:36 -07:00
|
|
|
#endif
|
2008-07-29 23:50:14 -07:00
|
|
|
}
|
2008-11-06 12:53:20 -08:00
|
|
|
|
|
|
|
void nsAudioStream::Drain()
|
|
|
|
{
|
2009-02-16 20:56:16 -08:00
|
|
|
if (!mAudioHandle)
|
2008-11-06 12:53:20 -08:00
|
|
|
return;
|
|
|
|
|
2008-12-15 15:06:22 -08:00
|
|
|
// Write any remaining unwritten sound data in the overflow buffer
|
|
|
|
if (!mBufferOverflow.IsEmpty()) {
|
|
|
|
if (sa_stream_write(static_cast<sa_stream_t*>(mAudioHandle),
|
|
|
|
mBufferOverflow.Elements(),
|
|
|
|
mBufferOverflow.Length() * sizeof(short)) != SA_SUCCESS)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-04-11 15:26:34 -07:00
|
|
|
int r = sa_stream_drain(static_cast<sa_stream_t*>(mAudioHandle));
|
|
|
|
if (r != SA_SUCCESS && r != SA_ERROR_INVALID) {
|
|
|
|
PR_LOG(gAudioStreamLog, PR_LOG_ERROR, ("nsAudioStream: sa_stream_drain error"));
|
|
|
|
Shutdown();
|
2008-11-06 12:53:20 -08:00
|
|
|
}
|
|
|
|
}
|
2009-05-14 18:29:05 -07:00
|
|
|
|
|
|
|
void nsAudioStream::Pause()
|
|
|
|
{
|
|
|
|
if (!mAudioHandle)
|
|
|
|
return;
|
2010-04-01 20:03:07 -07:00
|
|
|
mPaused = PR_TRUE;
|
2009-05-14 18:29:05 -07:00
|
|
|
sa_stream_pause(static_cast<sa_stream_t*>(mAudioHandle));
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsAudioStream::Resume()
|
|
|
|
{
|
|
|
|
if (!mAudioHandle)
|
|
|
|
return;
|
2010-04-01 20:03:07 -07:00
|
|
|
mPaused = PR_FALSE;
|
2009-05-14 18:29:05 -07:00
|
|
|
sa_stream_resume(static_cast<sa_stream_t*>(mAudioHandle));
|
|
|
|
}
|
|
|
|
|
2010-04-01 20:03:07 -07:00
|
|
|
PRInt64 nsAudioStream::GetPosition()
|
2009-05-14 18:29:05 -07:00
|
|
|
{
|
|
|
|
if (!mAudioHandle)
|
2010-04-01 20:03:07 -07:00
|
|
|
return -1;
|
2009-05-14 18:29:05 -07:00
|
|
|
|
2009-06-02 15:32:11 -07:00
|
|
|
sa_position_t positionType = SA_POSITION_WRITE_SOFTWARE;
|
|
|
|
#if defined(XP_WIN)
|
|
|
|
positionType = SA_POSITION_WRITE_HARDWARE;
|
|
|
|
#endif
|
2009-05-14 18:29:05 -07:00
|
|
|
PRInt64 position = 0;
|
|
|
|
if (sa_stream_get_position(static_cast<sa_stream_t*>(mAudioHandle),
|
2009-06-02 15:32:11 -07:00
|
|
|
positionType, &position) == SA_SUCCESS) {
|
2010-08-18 10:04:31 -07:00
|
|
|
return ((1000 * position) / mRate / mChannels / sizeof(short));
|
2009-05-14 18:29:05 -07:00
|
|
|
}
|
|
|
|
|
2010-04-01 20:03:07 -07:00
|
|
|
return -1;
|
2009-05-14 18:29:05 -07:00
|
|
|
}
|
2010-08-18 10:04:31 -07:00
|
|
|
|