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: */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2008-11-06 12:53:20 -08:00
|
|
|
#if !defined(nsAudioStream_h_)
|
|
|
|
#define nsAudioStream_h_
|
|
|
|
|
2008-07-29 23:50:14 -07:00
|
|
|
#include "nscore.h"
|
2010-11-16 20:14:19 -08:00
|
|
|
#include "nsISupportsImpl.h"
|
|
|
|
#include "nsIThread.h"
|
2010-11-29 21:37:32 -08:00
|
|
|
#include "nsAutoPtr.h"
|
2008-07-29 23:50:14 -07:00
|
|
|
|
2012-05-31 21:45:01 -07:00
|
|
|
// Access to a single instance of this class must be synchronized by
|
|
|
|
// callers, or made from a single thread. One exception is that access to
|
|
|
|
// GetPosition, GetPositionInFrames, SetVolume, and Get{Rate,Channels,Format}
|
|
|
|
// is thread-safe without external synchronization.
|
2010-11-16 20:14:19 -08:00
|
|
|
class nsAudioStream : public nsISupports
|
2008-07-29 23:50:14 -07:00
|
|
|
{
|
2010-11-16 20:14:19 -08:00
|
|
|
public:
|
|
|
|
|
2008-11-10 00:12:13 -08:00
|
|
|
enum SampleFormat
|
|
|
|
{
|
|
|
|
FORMAT_U8,
|
|
|
|
FORMAT_S16_LE,
|
2009-01-29 23:27:43 -08:00
|
|
|
FORMAT_FLOAT32
|
2008-11-10 00:12:13 -08:00
|
|
|
};
|
|
|
|
|
2012-02-13 20:39:33 -08:00
|
|
|
nsAudioStream()
|
|
|
|
: mRate(0),
|
2012-08-09 22:30:01 -07:00
|
|
|
mChannels(0),
|
|
|
|
mFormat(FORMAT_S16_LE)
|
2012-02-13 20:39:33 -08:00
|
|
|
{}
|
|
|
|
|
2011-01-03 19:55:32 -08:00
|
|
|
virtual ~nsAudioStream();
|
|
|
|
|
2008-11-10 00:12:13 -08:00
|
|
|
// Initialize Audio Library. Some Audio backends require initializing the
|
2011-09-26 20:31:18 -07:00
|
|
|
// library before using it.
|
2008-10-28 09:17:59 -07:00
|
|
|
static void InitLibrary();
|
2008-07-29 23:50:14 -07:00
|
|
|
|
2008-11-10 00:12:13 -08:00
|
|
|
// Shutdown Audio Library. Some Audio backends require shutting down the
|
|
|
|
// library after using it.
|
2008-07-29 23:50:14 -07:00
|
|
|
static void ShutdownLibrary();
|
|
|
|
|
2011-04-02 19:14:00 -07:00
|
|
|
// Thread that is shared between audio streams.
|
2010-11-16 20:14:19 -08:00
|
|
|
// This may return null in the child process
|
2011-01-03 19:55:32 -08:00
|
|
|
nsIThread *GetThread();
|
2010-11-16 20:14:19 -08:00
|
|
|
|
|
|
|
// AllocateStream will return either a local stream or a remoted stream
|
2011-04-02 19:14:00 -07:00
|
|
|
// depending on where you call it from. If you call this from a child process,
|
|
|
|
// you may receive an implementation which forwards to a compositing process.
|
2010-11-16 20:14:19 -08:00
|
|
|
static nsAudioStream* AllocateStream();
|
2008-07-29 23:50:14 -07:00
|
|
|
|
2011-09-26 20:31:18 -07:00
|
|
|
// Initialize the audio stream. aNumChannels is the number of audio
|
|
|
|
// channels (1 for mono, 2 for stereo, etc) and aRate is the sample rate
|
|
|
|
// (22050Hz, 44100Hz, etc).
|
2012-05-31 21:45:01 -07:00
|
|
|
// Unsafe to call with a monitor held due to synchronous event execution
|
|
|
|
// on the main thread, which may attempt to acquire any held monitor.
|
2012-08-09 22:30:01 -07:00
|
|
|
virtual nsresult Init(PRInt32 aNumChannels, PRInt32 aRate, SampleFormat aFormat) = 0;
|
2008-07-29 23:50:14 -07:00
|
|
|
|
|
|
|
// Closes the stream. All future use of the stream is an error.
|
2012-05-31 21:45:01 -07:00
|
|
|
// Unsafe to call with a monitor held due to synchronous event execution
|
|
|
|
// on the main thread, which may attempt to acquire any held monitor.
|
2010-11-16 20:14:19 -08:00
|
|
|
virtual void Shutdown() = 0;
|
2008-07-29 23:50:14 -07:00
|
|
|
|
2011-09-26 20:31:18 -07:00
|
|
|
// Write audio data to the audio hardware. aBuf is an array of frames in
|
|
|
|
// the format specified by mFormat of length aCount. If aFrames is larger
|
|
|
|
// than the result of Available(), the write will block until sufficient
|
|
|
|
// buffer space is available.
|
|
|
|
virtual nsresult Write(const void* aBuf, PRUint32 aFrames) = 0;
|
2008-07-29 23:50:14 -07:00
|
|
|
|
2011-09-26 20:31:18 -07:00
|
|
|
// Return the number of audio frames that can be written without blocking.
|
2010-11-16 20:14:19 -08:00
|
|
|
virtual PRUint32 Available() = 0;
|
2008-10-19 00:39:21 -07:00
|
|
|
|
2008-07-29 23:50:14 -07:00
|
|
|
// Set the current volume of the audio playback. This is a value from
|
2012-05-31 21:45:01 -07:00
|
|
|
// 0 (meaning muted) to 1 (meaning full volume). Thread-safe.
|
2011-01-16 19:03:00 -08:00
|
|
|
virtual void SetVolume(double aVolume) = 0;
|
2008-07-29 23:50:14 -07:00
|
|
|
|
2008-11-06 12:53:20 -08:00
|
|
|
// Block until buffered audio data has been consumed.
|
2012-05-31 21:45:01 -07:00
|
|
|
// Unsafe to call with a monitor held due to synchronous event execution
|
|
|
|
// on the main thread, which may attempt to acquire any held monitor.
|
2010-11-16 20:14:19 -08:00
|
|
|
virtual void Drain() = 0;
|
2008-11-06 12:53:20 -08:00
|
|
|
|
2009-05-14 18:29:05 -07:00
|
|
|
// Pause audio playback
|
2010-11-16 20:14:19 -08:00
|
|
|
virtual void Pause() = 0;
|
2009-05-14 18:29:05 -07:00
|
|
|
|
|
|
|
// Resume audio playback
|
2010-11-16 20:14:19 -08:00
|
|
|
virtual void Resume() = 0;
|
2009-05-14 18:29:05 -07:00
|
|
|
|
2011-09-26 20:31:18 -07:00
|
|
|
// Return the position in microseconds of the audio frame being played by
|
2012-05-31 21:45:01 -07:00
|
|
|
// the audio hardware. Thread-safe.
|
2010-11-16 20:14:19 -08:00
|
|
|
virtual PRInt64 GetPosition() = 0;
|
2010-04-01 20:03:07 -07:00
|
|
|
|
2011-09-26 20:31:18 -07:00
|
|
|
// Return the position, measured in audio frames played since the stream
|
2012-05-31 21:45:01 -07:00
|
|
|
// was opened, of the audio hardware. Thread-safe.
|
2011-09-26 20:31:18 -07:00
|
|
|
virtual PRInt64 GetPositionInFrames() = 0;
|
2010-08-25 06:10:00 -07:00
|
|
|
|
2011-09-29 16:34:37 -07:00
|
|
|
// Returns true when the audio stream is paused.
|
2011-09-28 23:19:26 -07:00
|
|
|
virtual bool IsPaused() = 0;
|
2010-11-29 21:37:32 -08:00
|
|
|
|
2011-09-26 20:31:18 -07:00
|
|
|
// Returns the minimum number of audio frames which must be written before
|
2010-12-19 11:05:40 -08:00
|
|
|
// you can be sure that something will be played.
|
2012-05-31 21:45:01 -07:00
|
|
|
// Unsafe to call with a monitor held due to synchronous event execution
|
|
|
|
// on the main thread, which may attempt to acquire any held monitor.
|
2011-09-26 20:31:18 -07:00
|
|
|
virtual PRInt32 GetMinWriteSize() = 0;
|
2010-12-19 11:05:40 -08:00
|
|
|
|
2012-02-13 20:39:33 -08:00
|
|
|
int GetRate() { return mRate; }
|
|
|
|
int GetChannels() { return mChannels; }
|
2012-08-09 22:30:01 -07:00
|
|
|
SampleFormat GetFormat() { return mFormat; }
|
2012-02-13 20:39:33 -08:00
|
|
|
|
2010-11-29 21:37:32 -08:00
|
|
|
protected:
|
|
|
|
nsCOMPtr<nsIThread> mAudioPlaybackThread;
|
2012-02-13 20:39:33 -08:00
|
|
|
int mRate;
|
|
|
|
int mChannels;
|
|
|
|
SampleFormat mFormat;
|
2008-07-29 23:50:14 -07:00
|
|
|
};
|
2010-11-16 20:14:19 -08:00
|
|
|
|
2008-11-06 12:53:20 -08:00
|
|
|
#endif
|