1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #if !defined(AudioStream_h_)
9 #include "AudioSampleFormat.h"
10 #include "AudioChannelCommon.h"
11 #include "nsAutoPtr.h"
12 #include "nsAutoRef.h"
15 #include "mozilla/StaticMutex.h"
17 #include "cubeb/cubeb.h"
20 class nsAutoRefTraits
<cubeb_stream
> : public nsPointerRefTraits
<cubeb_stream
>
23 static void Release(cubeb_stream
* aStream
) { cubeb_stream_destroy(aStream
); }
26 namespace soundtouch
{
37 AudioClock(AudioStream
* aStream
);
38 // Initialize the clock with the current AudioStream. Need to be called
39 // before querying the clock. Called on the audio thread.
41 // Update the number of samples that has been written in the audio backend.
42 // Called on the state machine thread.
43 void UpdateWritePosition(uint32_t aCount
);
44 // Get the read position of the stream, in microseconds.
45 // Called on the state machine thead.
46 uint64_t GetPosition();
47 // Get the read position of the stream, in frames.
48 // Called on the state machine thead.
49 uint64_t GetPositionInFrames();
50 // Set the playback rate.
51 // Called on the audio thread.
52 void SetPlaybackRate(double aPlaybackRate
);
53 // Get the current playback rate.
54 // Called on the audio thread.
55 double GetPlaybackRate();
56 // Set if we are preserving the pitch.
57 // Called on the audio thread.
58 void SetPreservesPitch(bool aPreservesPitch
);
59 // Get the current pitch preservation state.
60 // Called on the audio thread.
61 bool GetPreservesPitch();
62 // Get the number of frames written to the backend.
65 // This AudioStream holds a strong reference to this AudioClock. This
66 // pointer is garanteed to always be valid.
67 AudioStream
* mAudioStream
;
68 // The old output rate, to compensate audio latency for the period inbetween
69 // the moment resampled buffers are pushed to the hardware and the moment the
70 // clock should take the new rate into account for A/V sync.
72 // Position at which the last playback rate change occured
73 int64_t mBasePosition
;
74 // Offset, in frames, at which the last playback rate change occured
76 // Old base offset (number of samples), used when changing rate to compute the
77 // position in the stream.
78 int64_t mOldBaseOffset
;
79 // Old base position (number of microseconds), when changing rate. This is the
80 // time in the media, not wall clock position.
81 int64_t mOldBasePosition
;
82 // Write position at which the playbackRate change occured.
83 int64_t mPlaybackRateChangeOffset
;
84 // The previous position reached in the media, used when compensating
85 // latency, to have the position at which the playbackRate change occured.
86 int64_t mPreviousPosition
;
87 // Number of samples effectivelly written in backend, i.e. write position.
89 // Output rate in Hz (characteristic of the playback rate)
91 // Input rate in Hz (characteristic of the media being played)
93 // True if the we are timestretching, false if we are resampling.
95 // True if we are playing at the old playbackRate after it has been changed.
96 bool mCompensatingLatency
;
99 class CircularByteBuffer
103 : mBuffer(nullptr), mCapacity(0), mStart(0), mCount(0)
106 // Set the capacity of the buffer in bytes. Must be called before any
107 // call to append or pop elements.
108 void SetCapacity(uint32_t aCapacity
) {
109 NS_ABORT_IF_FALSE(!mBuffer
, "Buffer allocated.");
110 mCapacity
= aCapacity
;
111 mBuffer
= new uint8_t[mCapacity
];
118 uint32_t Capacity() {
122 uint32_t Available() {
123 return Capacity() - Length();
126 // Append aLength bytes from aSrc to the buffer. Caller must check that
127 // sufficient space is available.
128 void AppendElements(const uint8_t* aSrc
, uint32_t aLength
) {
129 NS_ABORT_IF_FALSE(mBuffer
&& mCapacity
, "Buffer not initialized.");
130 NS_ABORT_IF_FALSE(aLength
<= Available(), "Buffer full.");
132 uint32_t end
= (mStart
+ mCount
) % mCapacity
;
134 uint32_t toCopy
= std::min(mCapacity
- end
, aLength
);
135 memcpy(&mBuffer
[end
], aSrc
, toCopy
);
136 memcpy(&mBuffer
[0], aSrc
+ toCopy
, aLength
- toCopy
);
140 // Remove aSize bytes from the buffer. Caller must check returned size in
141 // aSize{1,2} before using the pointer returned in aData{1,2}. Caller
142 // must not specify an aSize larger than Length().
143 void PopElements(uint32_t aSize
, void** aData1
, uint32_t* aSize1
,
144 void** aData2
, uint32_t* aSize2
) {
145 NS_ABORT_IF_FALSE(mBuffer
&& mCapacity
, "Buffer not initialized.");
146 NS_ABORT_IF_FALSE(aSize
<= Length(), "Request too large.");
148 *aData1
= &mBuffer
[mStart
];
149 *aSize1
= std::min(mCapacity
- mStart
, aSize
);
150 *aData2
= &mBuffer
[0];
151 *aSize2
= aSize
- *aSize1
;
152 mCount
-= *aSize1
+ *aSize2
;
153 mStart
+= *aSize1
+ *aSize2
;
158 nsAutoArrayPtr
<uint8_t> mBuffer
;
164 // Access to a single instance of this class must be synchronized by
165 // callers, or made from a single thread. One exception is that access to
166 // GetPosition, GetPositionInFrames, SetVolume, and Get{Rate,Channels}
167 // is thread-safe without external synchronization.
168 class AudioStream MOZ_FINAL
171 // Initialize Audio Library. Some Audio backends require initializing the
172 // library before using it.
173 static void InitLibrary();
175 // Shutdown Audio Library. Some Audio backends require shutting down the
176 // library after using it.
177 static void ShutdownLibrary();
179 // Returns the maximum number of channels supported by the audio hardware.
180 static int MaxNumberOfChannels();
182 // Queries the samplerate the hardware/mixer runs at, and stores it.
183 // Can be called on any thread. When this returns, it is safe to call
184 // PreferredSampleRate without locking.
185 static void InitPreferredSampleRate();
186 // Get the aformentionned sample rate. Does not lock.
187 static int PreferredSampleRate();
192 enum LatencyRequest
{
197 // Initialize the audio stream. aNumChannels is the number of audio
198 // channels (1 for mono, 2 for stereo, etc) and aRate is the sample rate
199 // (22050Hz, 44100Hz, etc).
200 nsresult
Init(int32_t aNumChannels
, int32_t aRate
,
201 const dom::AudioChannelType aAudioStreamType
,
202 LatencyRequest aLatencyRequest
);
204 // Closes the stream. All future use of the stream is an error.
207 // Write audio data to the audio hardware. aBuf is an array of AudioDataValues
208 // AudioDataValue of length aFrames*mChannels. If aFrames is larger
209 // than the result of Available(), the write will block until sufficient
210 // buffer space is available. aTime is the time in ms associated with the first sample
211 // for latency calculations
212 nsresult
Write(const AudioDataValue
* aBuf
, uint32_t aFrames
, TimeStamp
* aTime
= nullptr);
214 // Return the number of audio frames that can be written without blocking.
215 uint32_t Available();
217 // Set the current volume of the audio playback. This is a value from
218 // 0 (meaning muted) to 1 (meaning full volume). Thread-safe.
219 void SetVolume(double aVolume
);
221 // Block until buffered audio data has been consumed.
227 // Return the number of frames written so far in the stream. This allow the
228 // caller to check if it is safe to start the stream, if needed.
229 int64_t GetWritten();
231 // Pause audio playback.
234 // Resume audio playback.
237 // Return the position in microseconds of the audio frame being played by
238 // the audio hardware, compensated for playback rate change. Thread-safe.
239 int64_t GetPosition();
241 // Return the position, measured in audio frames played since the stream
242 // was opened, of the audio hardware. Thread-safe.
243 int64_t GetPositionInFrames();
245 // Return the position, measured in audio framed played since the stream was
246 // opened, of the audio hardware, not adjusted for the changes of playback
248 int64_t GetPositionInFramesInternal();
250 // Returns true when the audio stream is paused.
253 int GetRate() { return mOutRate
; }
254 int GetChannels() { return mChannels
; }
255 int GetOutChannels() { return mOutChannels
; }
257 // This should be called before attempting to use the time stretcher.
258 nsresult
EnsureTimeStretcherInitialized();
259 // Set playback rate as a multiple of the intrinsic playback rate. This is to
260 // be called only with aPlaybackRate > 0.0.
261 nsresult
SetPlaybackRate(double aPlaybackRate
);
262 // Switch between resampling (if false) and time stretching (if true, default).
263 nsresult
SetPreservesPitch(bool aPreservesPitch
);
266 static void PrefChanged(const char* aPref
, void* aClosure
);
267 static double GetVolumeScale();
268 static cubeb
* GetCubebContext();
269 static cubeb
* GetCubebContextUnlocked();
270 static uint32_t GetCubebLatency();
271 static bool CubebLatencyPrefSet();
273 static long DataCallback_S(cubeb_stream
*, void* aThis
, void* aBuffer
, long aFrames
)
275 return static_cast<AudioStream
*>(aThis
)->DataCallback(aBuffer
, aFrames
);
278 static void StateCallback_S(cubeb_stream
*, void* aThis
, cubeb_state aState
)
280 static_cast<AudioStream
*>(aThis
)->StateCallback(aState
);
283 long DataCallback(void* aBuffer
, long aFrames
);
284 void StateCallback(cubeb_state aState
);
286 nsresult
EnsureTimeStretcherInitializedUnlocked();
288 // aTime is the time in ms the samples were inserted into MediaStreamGraph
289 long GetUnprocessed(void* aBuffer
, long aFrames
, int64_t &aTime
);
290 long GetTimeStretched(void* aBuffer
, long aFrames
, int64_t &aTime
);
291 long GetUnprocessedWithSilencePadding(void* aBuffer
, long aFrames
, int64_t &aTime
);
293 // Shared implementation of underflow adjusted position calculation.
294 // Caller must own the monitor.
295 int64_t GetPositionInFramesUnlocked();
297 int64_t GetLatencyInFrames();
298 void GetBufferInsertTime(int64_t &aTimeMs
);
300 void StartUnlocked();
302 // The monitor is held to protect all access to member variables. Write()
303 // waits while mBuffer is full; DataCallback() notifies as it consumes
304 // data from mBuffer. Drain() waits while mState is DRAINING;
305 // StateCallback() notifies when mState is DRAINED.
308 // Input rate in Hz (characteristic of the media being played)
310 // Output rate in Hz (characteristic of the playback rate)
314 // Number of frames written to the buffers.
316 AudioClock mAudioClock
;
317 nsAutoPtr
<soundtouch::SoundTouch
> mTimeStretcher
;
318 nsRefPtr
<AsyncLatencyLogger
> mLatencyLog
;
320 // copy of Latency logger's starting time for offset calculations
321 TimeStamp mStartTime
;
322 // Whether we are playing a low latency stream, or a normal stream.
323 LatencyRequest mLatencyRequest
;
324 // Where in the current mInserts[0] block cubeb has read to
326 // Keep track of each inserted block of samples and the time it was inserted
327 // so we can estimate the clock time for a specific sample's insertion (for when
328 // we send data to cubeb). Blocks are aged out as needed.
333 nsAutoTArray
<Inserts
, 8> mInserts
;
335 // Sum of silent frames written when DataCallback requests more frames
336 // than are available in mBuffer.
337 uint64_t mLostFrames
;
339 // Output file for dumping audio
342 // Temporary audio buffer. Filled by Write() and consumed by
343 // DataCallback(). Once mBuffer is full, Write() blocks until sufficient
344 // space becomes available in mBuffer. mBuffer is sized in bytes, not
346 CircularByteBuffer mBuffer
;
348 // Software volume level. Applied during the servicing of DataCallback().
351 // Owning reference to a cubeb_stream. cubeb_stream_destroy is called by
352 // nsAutoRef's destructor.
353 nsAutoRef
<cubeb_stream
> mCubebStream
;
355 uint32_t mBytesPerFrame
;
357 uint32_t BytesToFrames(uint32_t aBytes
) {
358 NS_ASSERTION(aBytes
% mBytesPerFrame
== 0,
359 "Byte count not aligned on frames size.");
360 return aBytes
/ mBytesPerFrame
;
363 uint32_t FramesToBytes(uint32_t aFrames
) {
364 return aFrames
* mBytesPerFrame
;
368 INITIALIZED
, // Initialized, playback has not begun.
369 STARTED
, // Started by a call to Write() (iff INITIALIZED) or Resume().
370 STOPPED
, // Stopped by a call to Pause().
371 DRAINING
, // Drain requested. DataCallback will indicate end of stream
372 // once the remaining contents of mBuffer are requested by
373 // cubeb, after which StateCallback will indicate drain
375 DRAINED
, // StateCallback has indicated that the drain is complete.
376 ERRORED
// Stream disabled due to an internal error.
381 // This mutex protects the static members below.
382 static StaticMutex sMutex
;
383 static cubeb
* sCubebContext
;
385 // Prefered samplerate, in Hz (characteristic of the
386 // hardware/mixer/platform/API used).
387 static uint32_t sPreferredSampleRate
;
389 static double sVolumeScale
;
390 static uint32_t sCubebLatency
;
391 static bool sCubebLatencyPrefSet
;
394 } // namespace mozilla