2012-05-11 21:35:36 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*/
|
2012-04-30 07:11:26 +04: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/. */
|
|
|
|
|
2013-02-04 14:04:25 +04:00
|
|
|
#include "MediaStreamGraphImpl.h"
|
2014-04-13 23:41:07 +04:00
|
|
|
#include "mozilla/MathAlgorithms.h"
|
2014-03-15 23:00:16 +04:00
|
|
|
#include "mozilla/unused.h"
|
2012-04-30 07:11:26 +04:00
|
|
|
|
|
|
|
#include "AudioSegment.h"
|
|
|
|
#include "VideoSegment.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsIObserver.h"
|
2014-04-13 22:08:10 +04:00
|
|
|
#include "nsPrintfCString.h"
|
2012-04-30 07:11:26 +04:00
|
|
|
#include "nsServiceManagerUtils.h"
|
2014-04-13 22:08:10 +04:00
|
|
|
#include "prerror.h"
|
2015-05-19 21:15:34 +03:00
|
|
|
#include "mozilla/Logging.h"
|
2012-06-19 06:30:09 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2012-07-31 16:17:21 +04:00
|
|
|
#include "TrackUnionStream.h"
|
2012-08-21 08:06:46 +04:00
|
|
|
#include "ImageContainer.h"
|
2015-07-24 15:28:16 +03:00
|
|
|
#include "AudioCaptureStream.h"
|
2014-04-10 21:39:20 +04:00
|
|
|
#include "AudioChannelService.h"
|
2013-01-14 02:46:57 +04:00
|
|
|
#include "AudioNodeStream.h"
|
2013-07-24 15:29:39 +04:00
|
|
|
#include "AudioNodeExternalInputStream.h"
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
#include "mozilla/dom/AudioContextBinding.h"
|
2016-01-22 21:49:54 +03:00
|
|
|
#include "mozilla/media/MediaUtils.h"
|
2013-01-15 16:22:03 +04:00
|
|
|
#include <algorithm>
|
2013-05-03 09:02:55 +04:00
|
|
|
#include "DOMMediaStream.h"
|
2013-07-19 18:40:57 +04:00
|
|
|
#include "GeckoProfiler.h"
|
2014-03-15 23:00:16 +04:00
|
|
|
#include "mozilla/unused.h"
|
2016-03-16 18:00:34 +03:00
|
|
|
#include "mozilla/media/MediaUtils.h"
|
2014-04-21 11:15:34 +04:00
|
|
|
#ifdef MOZ_WEBRTC
|
2014-04-02 21:58:19 +04:00
|
|
|
#include "AudioOutputObserver.h"
|
2014-04-21 11:15:34 +04:00
|
|
|
#endif
|
2016-01-21 19:51:35 +03:00
|
|
|
#include "mtransport/runnable_utils.h"
|
2012-04-30 07:11:26 +04:00
|
|
|
|
2014-10-10 06:30:28 +04:00
|
|
|
#include "webaudio/blink/HRTFDatabaseLoader.h"
|
|
|
|
|
2012-04-30 07:11:26 +04:00
|
|
|
using namespace mozilla::layers;
|
2012-11-16 07:25:26 +04:00
|
|
|
using namespace mozilla::dom;
|
2013-12-31 13:06:12 +04:00
|
|
|
using namespace mozilla::gfx;
|
2016-03-16 18:00:34 +03:00
|
|
|
using namespace mozilla::media;
|
2012-04-30 07:11:26 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2015-11-15 16:49:01 +03:00
|
|
|
LazyLogModule gMediaStreamGraphLog("MediaStreamGraph");
|
2015-05-21 23:22:04 +03:00
|
|
|
#define STREAM_LOG(type, msg) MOZ_LOG(gMediaStreamGraphLog, type, msg)
|
2012-04-30 07:11:26 +04:00
|
|
|
|
2016-04-22 17:24:19 +03:00
|
|
|
// #define ENABLE_LIFECYCLE_LOG
|
2014-08-31 16:19:48 +04:00
|
|
|
|
|
|
|
// We don't use NSPR log here because we want this interleaved with adb logcat
|
|
|
|
// on Android/B2G
|
|
|
|
#ifdef ENABLE_LIFECYCLE_LOG
|
|
|
|
# ifdef ANDROID
|
|
|
|
# include "android/log.h"
|
|
|
|
# define LIFECYCLE_LOG(...) __android_log_print(ANDROID_LOG_INFO, "Gecko - MSG", ## __VA_ARGS__); printf(__VA_ARGS__);printf("\n");
|
|
|
|
# else
|
|
|
|
# define LIFECYCLE_LOG(...) printf(__VA_ARGS__);printf("\n");
|
|
|
|
# endif
|
|
|
|
#else
|
|
|
|
# define LIFECYCLE_LOG(...)
|
|
|
|
#endif
|
|
|
|
|
2012-04-30 07:11:26 +04:00
|
|
|
/**
|
2015-08-25 11:17:31 +03:00
|
|
|
* A hash table containing the graph instances, one per AudioChannel.
|
2012-04-30 07:11:26 +04:00
|
|
|
*/
|
2014-11-17 19:07:55 +03:00
|
|
|
static nsDataHashtable<nsUint32HashKey, MediaStreamGraphImpl*> gGraphs;
|
2012-04-30 07:11:26 +04:00
|
|
|
|
2013-11-18 15:48:04 +04:00
|
|
|
MediaStreamGraphImpl::~MediaStreamGraphImpl()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(IsEmpty(),
|
|
|
|
"All streams should have been destroyed by messages from the main thread");
|
2015-06-04 01:25:57 +03:00
|
|
|
STREAM_LOG(LogLevel::Debug, ("MediaStreamGraph %p destroyed", this));
|
2014-08-31 16:19:48 +04:00
|
|
|
LIFECYCLE_LOG("MediaStreamGraphImpl::~MediaStreamGraphImpl\n");
|
2013-11-18 15:48:04 +04:00
|
|
|
}
|
|
|
|
|
2012-04-30 07:11:26 +04:00
|
|
|
void
|
|
|
|
MediaStreamGraphImpl::FinishStream(MediaStream* aStream)
|
|
|
|
{
|
|
|
|
if (aStream->mFinished)
|
|
|
|
return;
|
2015-06-04 01:25:57 +03:00
|
|
|
STREAM_LOG(LogLevel::Debug, ("MediaStream %p will finish", aStream));
|
2016-02-05 15:20:20 +03:00
|
|
|
#ifdef DEBUG
|
2016-01-26 05:49:01 +03:00
|
|
|
for (StreamTracks::TrackIter track(aStream->mTracks);
|
2016-02-05 15:20:20 +03:00
|
|
|
!track.IsEnded(); track.Next()) {
|
|
|
|
if (!track->IsEnded()) {
|
|
|
|
STREAM_LOG(LogLevel::Error,
|
|
|
|
("MediaStream %p will finish, but track %d has not ended.",
|
|
|
|
aStream, track->GetID()));
|
|
|
|
NS_ASSERTION(false, "Finished stream cannot contain live track");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2012-04-30 07:11:26 +04:00
|
|
|
aStream->mFinished = true;
|
2016-01-26 05:49:01 +03:00
|
|
|
aStream->mTracks.AdvanceKnownTracksTime(STREAM_TIME_MAX);
|
2014-01-29 17:34:35 +04:00
|
|
|
|
|
|
|
SetStreamOrderDirty();
|
2012-04-30 07:11:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-08-12 02:29:35 +03:00
|
|
|
MediaStreamGraphImpl::AddStreamGraphThread(MediaStream* aStream)
|
2012-04-30 07:11:26 +04:00
|
|
|
{
|
2016-01-26 05:49:01 +03:00
|
|
|
aStream->mTracksStartTime = mProcessedTime;
|
2015-09-16 07:15:21 +03:00
|
|
|
if (aStream->IsSuspended()) {
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
mSuspendedStreams.AppendElement(aStream);
|
2015-06-04 01:25:57 +03:00
|
|
|
STREAM_LOG(LogLevel::Debug, ("Adding media stream %p to the graph, in the suspended stream array", aStream));
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
} else {
|
|
|
|
mStreams.AppendElement(aStream);
|
2016-03-08 20:11:09 +03:00
|
|
|
STREAM_LOG(LogLevel::Debug, ("Adding media stream %p to graph %p, count %lu", aStream, this, mStreams.Length()));
|
|
|
|
LIFECYCLE_LOG("Adding media stream %p to graph %p, count %lu", aStream, this, mStreams.Length());
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
}
|
2014-01-29 17:34:35 +04:00
|
|
|
|
|
|
|
SetStreamOrderDirty();
|
2012-04-30 07:11:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-08-12 02:29:35 +03:00
|
|
|
MediaStreamGraphImpl::RemoveStreamGraphThread(MediaStream* aStream)
|
2012-04-30 07:11:26 +04:00
|
|
|
{
|
|
|
|
// Remove references in mStreamUpdates before we allow aStream to die.
|
|
|
|
// Pending updates are not needed (since the main thread has already given
|
|
|
|
// up the stream) so we will just drop them.
|
2015-06-11 22:10:06 +03:00
|
|
|
{
|
2014-08-26 19:01:33 +04:00
|
|
|
MonitorAutoLock lock(mMonitor);
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < mStreamUpdates.Length(); ++i) {
|
2012-04-30 07:11:26 +04:00
|
|
|
if (mStreamUpdates[i].mStream == aStream) {
|
2012-07-30 18:20:58 +04:00
|
|
|
mStreamUpdates[i].mStream = nullptr;
|
2012-04-30 07:11:26 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-25 00:36:07 +04:00
|
|
|
// Ensure that mFirstCycleBreaker and mMixer are updated when necessary.
|
2014-01-29 17:34:35 +04:00
|
|
|
SetStreamOrderDirty();
|
|
|
|
|
2015-09-16 07:15:21 +03:00
|
|
|
if (aStream->IsSuspended()) {
|
|
|
|
mSuspendedStreams.RemoveElement(aStream);
|
|
|
|
} else {
|
|
|
|
mStreams.RemoveElement(aStream);
|
|
|
|
}
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
|
2016-03-08 20:11:09 +03:00
|
|
|
STREAM_LOG(LogLevel::Debug, ("Removed media stream %p from graph %p, count %lu",
|
2016-04-22 17:24:17 +03:00
|
|
|
aStream, this, mStreams.Length()));
|
2016-03-08 20:11:09 +03:00
|
|
|
LIFECYCLE_LOG("Removed media stream %p from graph %p, count %lu",
|
|
|
|
aStream, this, mStreams.Length());
|
2012-04-30 07:11:26 +04:00
|
|
|
|
2016-03-08 20:11:08 +03:00
|
|
|
NS_RELEASE(aStream); // probably destroying it
|
2012-04-30 07:11:26 +04:00
|
|
|
}
|
|
|
|
|
2012-04-30 07:11:40 +04:00
|
|
|
void
|
2012-07-20 23:36:03 +04:00
|
|
|
MediaStreamGraphImpl::ExtractPendingInput(SourceMediaStream* aStream,
|
|
|
|
GraphTime aDesiredUpToTime,
|
|
|
|
bool* aEnsureNextIteration)
|
2012-04-30 07:11:40 +04:00
|
|
|
{
|
|
|
|
bool finished;
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(aStream->mMutex);
|
2012-12-07 15:06:55 +04:00
|
|
|
if (aStream->mPullEnabled && !aStream->mFinished &&
|
|
|
|
!aStream->mListeners.IsEmpty()) {
|
|
|
|
// Compute how much stream time we'll need assuming we don't block
|
2015-09-08 07:50:55 +03:00
|
|
|
// the stream at all.
|
|
|
|
StreamTime t = aStream->GraphTimeToStreamTime(aDesiredUpToTime);
|
2015-06-04 01:25:57 +03:00
|
|
|
STREAM_LOG(LogLevel::Verbose, ("Calling NotifyPull aStream=%p t=%f current end=%f", aStream,
|
2013-11-21 07:02:42 +04:00
|
|
|
MediaTimeToSeconds(t),
|
2016-01-26 05:49:01 +03:00
|
|
|
MediaTimeToSeconds(aStream->mTracks.GetEnd())));
|
|
|
|
if (t > aStream->mTracks.GetEnd()) {
|
2012-12-07 15:06:55 +04:00
|
|
|
*aEnsureNextIteration = true;
|
2013-03-07 12:53:45 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
if (aStream->mListeners.Length() == 0) {
|
2015-06-04 01:25:57 +03:00
|
|
|
STREAM_LOG(LogLevel::Error, ("No listeners in NotifyPull aStream=%p desired=%f current end=%f",
|
2013-11-21 07:02:42 +04:00
|
|
|
aStream, MediaTimeToSeconds(t),
|
2016-01-26 05:49:01 +03:00
|
|
|
MediaTimeToSeconds(aStream->mTracks.GetEnd())));
|
2013-03-07 12:53:45 +04:00
|
|
|
aStream->DumpTrackInfo();
|
|
|
|
}
|
|
|
|
#endif
|
2012-12-07 15:06:55 +04:00
|
|
|
for (uint32_t j = 0; j < aStream->mListeners.Length(); ++j) {
|
|
|
|
MediaStreamListener* l = aStream->mListeners[j];
|
|
|
|
{
|
|
|
|
MutexAutoUnlock unlock(aStream->mMutex);
|
|
|
|
l->NotifyPull(this, t);
|
|
|
|
}
|
2012-07-20 23:36:03 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-04-30 07:11:40 +04:00
|
|
|
finished = aStream->mUpdateFinished;
|
2016-04-07 06:44:00 +03:00
|
|
|
bool shouldNotifyTrackCreated = false;
|
2012-08-22 19:56:38 +04:00
|
|
|
for (int32_t i = aStream->mUpdateTracks.Length() - 1; i >= 0; --i) {
|
2012-04-30 07:11:40 +04:00
|
|
|
SourceMediaStream::TrackData* data = &aStream->mUpdateTracks[i];
|
2013-05-30 08:44:43 +04:00
|
|
|
aStream->ApplyTrackDisabling(data->mID, data->mData);
|
2016-03-03 19:27:59 +03:00
|
|
|
StreamTime offset = (data->mCommands & SourceMediaStream::TRACK_CREATE)
|
2016-01-26 05:49:01 +03:00
|
|
|
? data->mStart : aStream->mTracks.FindTrack(data->mID)->GetSegment()->GetDuration();
|
2015-02-06 12:38:11 +03:00
|
|
|
for (MediaStreamListener* l : aStream->mListeners) {
|
2014-09-18 03:51:13 +04:00
|
|
|
l->NotifyQueuedTrackChanges(this, data->mID,
|
2012-04-30 07:12:50 +04:00
|
|
|
offset, data->mCommands, *data->mData);
|
|
|
|
}
|
2016-03-03 19:27:59 +03:00
|
|
|
for (TrackBound<MediaStreamTrackListener>& b : aStream->mTrackListeners) {
|
|
|
|
if (b.mTrackID != data->mID) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
b.mListener->NotifyQueuedChanges(this, offset, *data->mData);
|
|
|
|
if (data->mCommands & SourceMediaStream::TRACK_END) {
|
|
|
|
b.mListener->NotifyEnded();
|
|
|
|
}
|
|
|
|
}
|
2012-04-30 07:11:40 +04:00
|
|
|
if (data->mCommands & SourceMediaStream::TRACK_CREATE) {
|
|
|
|
MediaSegment* segment = data->mData.forget();
|
2015-06-04 01:25:57 +03:00
|
|
|
STREAM_LOG(LogLevel::Debug, ("SourceMediaStream %p creating track %d, start %lld, initial end %lld",
|
2014-09-18 03:50:02 +04:00
|
|
|
aStream, data->mID, int64_t(data->mStart),
|
2013-11-21 07:02:42 +04:00
|
|
|
int64_t(segment->GetDuration())));
|
2014-03-24 14:06:05 +04:00
|
|
|
|
2014-12-30 04:54:01 +03:00
|
|
|
data->mEndOfFlushedData += segment->GetDuration();
|
2016-01-26 05:49:01 +03:00
|
|
|
aStream->mTracks.AddTrack(data->mID, data->mStart, segment);
|
2012-04-30 07:11:40 +04:00
|
|
|
// The track has taken ownership of data->mData, so let's replace
|
|
|
|
// data->mData with an empty clone.
|
|
|
|
data->mData = segment->CreateEmptyClone();
|
|
|
|
data->mCommands &= ~SourceMediaStream::TRACK_CREATE;
|
2016-04-07 06:44:00 +03:00
|
|
|
shouldNotifyTrackCreated = true;
|
2012-04-30 07:11:40 +04:00
|
|
|
} else if (data->mData->GetDuration() > 0) {
|
2016-01-26 05:49:01 +03:00
|
|
|
MediaSegment* dest = aStream->mTracks.FindTrack(data->mID)->GetSegment();
|
2015-06-04 01:25:57 +03:00
|
|
|
STREAM_LOG(LogLevel::Verbose, ("SourceMediaStream %p track %d, advancing end from %lld to %lld",
|
2013-11-21 07:02:42 +04:00
|
|
|
aStream, data->mID,
|
|
|
|
int64_t(dest->GetDuration()),
|
|
|
|
int64_t(dest->GetDuration() + data->mData->GetDuration())));
|
2014-12-30 04:54:01 +03:00
|
|
|
data->mEndOfFlushedData += data->mData->GetDuration();
|
2012-04-30 07:11:40 +04:00
|
|
|
dest->AppendFrom(data->mData);
|
|
|
|
}
|
|
|
|
if (data->mCommands & SourceMediaStream::TRACK_END) {
|
2016-01-26 05:49:01 +03:00
|
|
|
aStream->mTracks.FindTrack(data->mID)->SetEnded();
|
2012-04-30 07:11:40 +04:00
|
|
|
aStream->mUpdateTracks.RemoveElementAt(i);
|
|
|
|
}
|
|
|
|
}
|
2016-04-07 06:44:00 +03:00
|
|
|
if (shouldNotifyTrackCreated) {
|
2015-02-06 12:38:11 +03:00
|
|
|
for (MediaStreamListener* l : aStream->mListeners) {
|
|
|
|
l->NotifyFinishedTrackCreation(this);
|
|
|
|
}
|
|
|
|
}
|
2014-05-19 00:26:54 +04:00
|
|
|
if (!aStream->mFinished) {
|
2016-01-26 05:49:01 +03:00
|
|
|
aStream->mTracks.AdvanceKnownTracksTime(aStream->mUpdateKnownTracksTime);
|
2014-05-19 00:26:54 +04:00
|
|
|
}
|
2012-04-30 07:11:40 +04:00
|
|
|
}
|
2016-01-26 05:49:01 +03:00
|
|
|
if (aStream->mTracks.GetEnd() > 0) {
|
2013-03-20 15:19:39 +04:00
|
|
|
aStream->mHasCurrentData = true;
|
|
|
|
}
|
2012-04-30 07:11:40 +04:00
|
|
|
if (finished) {
|
|
|
|
FinishStream(aStream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-30 07:11:26 +04:00
|
|
|
StreamTime
|
2015-09-08 06:41:00 +03:00
|
|
|
MediaStreamGraphImpl::GraphTimeToStreamTimeWithBlocking(MediaStream* aStream,
|
2015-09-16 07:35:16 +03:00
|
|
|
GraphTime aTime)
|
2012-04-30 07:11:26 +04:00
|
|
|
{
|
2015-08-13 07:23:17 +03:00
|
|
|
MOZ_ASSERT(aTime <= mStateComputedTime,
|
2015-09-04 09:44:43 +03:00
|
|
|
"Don't ask about times where we haven't made blocking decisions yet");
|
|
|
|
return std::max<StreamTime>(0,
|
2016-01-26 05:49:01 +03:00
|
|
|
std::min(aTime, aStream->mStartBlocking) - aStream->mTracksStartTime);
|
2013-02-04 14:04:26 +04:00
|
|
|
}
|
|
|
|
|
2014-04-25 18:09:30 +04:00
|
|
|
GraphTime
|
2015-05-13 16:34:56 +03:00
|
|
|
MediaStreamGraphImpl::IterationEnd() const
|
2014-04-25 18:09:30 +04:00
|
|
|
{
|
|
|
|
return CurrentDriver()->IterationEnd();
|
|
|
|
}
|
|
|
|
|
2015-05-13 16:34:56 +03:00
|
|
|
void
|
2015-09-04 15:42:53 +03:00
|
|
|
MediaStreamGraphImpl::UpdateCurrentTimeForStreams(GraphTime aPrevCurrentTime)
|
2015-05-13 16:34:56 +03:00
|
|
|
{
|
2015-09-08 07:18:15 +03:00
|
|
|
for (MediaStream* stream : AllStreams()) {
|
2015-09-08 06:55:40 +03:00
|
|
|
bool isAnyBlocked = stream->mStartBlocking < mStateComputedTime;
|
|
|
|
bool isAnyUnblocked = stream->mStartBlocking > aPrevCurrentTime;
|
|
|
|
|
2015-09-08 07:18:15 +03:00
|
|
|
// Calculate blocked time and fire Blocked/Unblocked events
|
2015-09-04 15:42:53 +03:00
|
|
|
GraphTime blockedTime = mStateComputedTime - stream->mStartBlocking;
|
2015-09-04 09:44:43 +03:00
|
|
|
NS_ASSERTION(blockedTime >= 0, "Error in blocking time");
|
2015-09-08 06:55:40 +03:00
|
|
|
stream->AdvanceTimeVaryingValuesToCurrentTime(mStateComputedTime,
|
|
|
|
blockedTime);
|
|
|
|
STREAM_LOG(LogLevel::Verbose,
|
|
|
|
("MediaStream %p bufferStartTime=%f blockedTime=%f", stream,
|
2016-01-26 05:49:01 +03:00
|
|
|
MediaTimeToSeconds(stream->mTracksStartTime),
|
2015-09-08 06:55:40 +03:00
|
|
|
MediaTimeToSeconds(blockedTime)));
|
|
|
|
stream->mStartBlocking = mStateComputedTime;
|
2015-09-04 09:44:43 +03:00
|
|
|
|
2015-09-08 06:55:40 +03:00
|
|
|
if (isAnyUnblocked && stream->mNotifiedBlocked) {
|
2015-09-04 09:44:43 +03:00
|
|
|
for (uint32_t j = 0; j < stream->mListeners.Length(); ++j) {
|
|
|
|
MediaStreamListener* l = stream->mListeners[j];
|
|
|
|
l->NotifyBlockingChanged(this, MediaStreamListener::UNBLOCKED);
|
2015-09-08 07:18:15 +03:00
|
|
|
}
|
2015-09-04 09:44:43 +03:00
|
|
|
stream->mNotifiedBlocked = false;
|
|
|
|
}
|
2015-09-08 06:55:40 +03:00
|
|
|
if (isAnyBlocked && !stream->mNotifiedBlocked) {
|
2015-09-04 09:44:43 +03:00
|
|
|
for (uint32_t j = 0; j < stream->mListeners.Length(); ++j) {
|
|
|
|
MediaStreamListener* l = stream->mListeners[j];
|
|
|
|
l->NotifyBlockingChanged(this, MediaStreamListener::BLOCKED);
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
}
|
2015-09-04 09:44:43 +03:00
|
|
|
stream->mNotifiedBlocked = true;
|
2015-09-08 07:18:15 +03:00
|
|
|
}
|
2015-04-09 14:44:27 +03:00
|
|
|
|
2015-09-08 06:55:40 +03:00
|
|
|
if (isAnyUnblocked) {
|
2015-09-04 15:45:14 +03:00
|
|
|
NS_ASSERTION(!stream->mNotifiedFinished,
|
|
|
|
"Shouldn't have already notified of finish *and* have output!");
|
|
|
|
for (uint32_t j = 0; j < stream->mListeners.Length(); ++j) {
|
|
|
|
MediaStreamListener* l = stream->mListeners[j];
|
|
|
|
l->NotifyOutput(this, mProcessedTime);
|
|
|
|
}
|
2015-09-04 08:01:01 +03:00
|
|
|
}
|
2015-09-08 07:18:15 +03:00
|
|
|
|
2015-09-04 15:45:14 +03:00
|
|
|
// The stream is fully finished when all of its track data has been played
|
|
|
|
// out.
|
|
|
|
if (stream->mFinished && !stream->mNotifiedFinished &&
|
|
|
|
mProcessedTime >=
|
2016-01-26 05:49:01 +03:00
|
|
|
stream->StreamTimeToGraphTime(stream->GetStreamTracks().GetAllTracksEnd())) {
|
2015-09-04 15:45:14 +03:00
|
|
|
stream->mNotifiedFinished = true;
|
|
|
|
SetStreamOrderDirty();
|
|
|
|
for (uint32_t j = 0; j < stream->mListeners.Length(); ++j) {
|
|
|
|
MediaStreamListener* l = stream->mListeners[j];
|
|
|
|
l->NotifyEvent(this, MediaStreamListener::EVENT_FINISHED);
|
|
|
|
}
|
2015-04-09 14:44:27 +03:00
|
|
|
}
|
2013-10-02 06:28:49 +04:00
|
|
|
}
|
2012-04-30 07:11:26 +04:00
|
|
|
}
|
|
|
|
|
2016-01-26 11:45:25 +03:00
|
|
|
template<typename C, typename Chunk>
|
|
|
|
void
|
|
|
|
MediaStreamGraphImpl::ProcessChunkMetadataForInterval(MediaStream* aStream,
|
|
|
|
TrackID aTrackID,
|
|
|
|
C& aSegment,
|
|
|
|
StreamTime aStart,
|
|
|
|
StreamTime aEnd)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aStream);
|
|
|
|
MOZ_ASSERT(IsTrackIDExplicit(aTrackID));
|
|
|
|
|
|
|
|
StreamTime offset = 0;
|
|
|
|
for (typename C::ConstChunkIterator chunk(aSegment);
|
|
|
|
!chunk.IsEnded(); chunk.Next()) {
|
|
|
|
if (offset >= aEnd) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
offset += chunk->GetDuration();
|
|
|
|
if (chunk->IsNull() || offset < aStart) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
PrincipalHandle principalHandle = chunk->GetPrincipalHandle();
|
|
|
|
if (principalHandle != aSegment.GetLastPrincipalHandle()) {
|
|
|
|
aSegment.SetLastPrincipalHandle(principalHandle);
|
|
|
|
STREAM_LOG(LogLevel::Debug, ("MediaStream %p track %d, principalHandle "
|
|
|
|
"changed in %sChunk with duration %lld",
|
|
|
|
aStream, aTrackID,
|
|
|
|
aSegment.GetType() == MediaSegment::AUDIO
|
|
|
|
? "Audio" : "Video",
|
|
|
|
(long long) chunk->GetDuration()));
|
|
|
|
for (const TrackBound<MediaStreamTrackListener>& listener : aStream->mTrackListeners) {
|
|
|
|
if (listener.mTrackID == aTrackID) {
|
|
|
|
listener.mListener->NotifyPrincipalHandleChanged(this, principalHandle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStreamGraphImpl::ProcessChunkMetadata(GraphTime aPrevCurrentTime)
|
|
|
|
{
|
|
|
|
for (MediaStream* stream : AllStreams()) {
|
|
|
|
StreamTime iterationStart = stream->GraphTimeToStreamTime(aPrevCurrentTime);
|
|
|
|
StreamTime iterationEnd = stream->GraphTimeToStreamTime(mProcessedTime);
|
2016-01-26 05:49:01 +03:00
|
|
|
for (StreamTracks::TrackIter tracks(stream->mTracks);
|
2016-01-26 11:45:25 +03:00
|
|
|
!tracks.IsEnded(); tracks.Next()) {
|
|
|
|
MediaSegment* segment = tracks->GetSegment();
|
|
|
|
if (!segment) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (tracks->GetType() == MediaSegment::AUDIO) {
|
|
|
|
AudioSegment* audio = static_cast<AudioSegment*>(segment);
|
|
|
|
ProcessChunkMetadataForInterval<AudioSegment, AudioChunk>(
|
|
|
|
stream, tracks->GetID(), *audio, iterationStart, iterationEnd);
|
|
|
|
} else if (tracks->GetType() == MediaSegment::VIDEO) {
|
|
|
|
VideoSegment* video = static_cast<VideoSegment*>(segment);
|
|
|
|
ProcessChunkMetadataForInterval<VideoSegment, VideoChunk>(
|
|
|
|
stream, tracks->GetID(), *video, iterationStart, iterationEnd);
|
|
|
|
} else {
|
|
|
|
MOZ_CRASH("Unknown track type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-16 07:17:30 +03:00
|
|
|
GraphTime
|
|
|
|
MediaStreamGraphImpl::WillUnderrun(MediaStream* aStream,
|
|
|
|
GraphTime aEndBlockingDecisions)
|
2012-04-30 07:11:26 +04:00
|
|
|
{
|
2012-07-31 16:17:21 +04:00
|
|
|
// Finished streams can't underrun. ProcessedMediaStreams also can't cause
|
|
|
|
// underrun currently, since we'll always be able to produce data for them
|
|
|
|
// unless they block on some other stream.
|
|
|
|
if (aStream->mFinished || aStream->AsProcessedStream()) {
|
2015-09-16 07:17:30 +03:00
|
|
|
return aEndBlockingDecisions;
|
2012-04-30 07:11:26 +04:00
|
|
|
}
|
2015-09-04 09:44:43 +03:00
|
|
|
// This stream isn't finished or suspended. We don't need to call
|
|
|
|
// StreamTimeToGraphTime since an underrun is the only thing that can block
|
|
|
|
// it.
|
2016-01-26 05:49:01 +03:00
|
|
|
GraphTime bufferEnd = aStream->GetTracksEnd() + aStream->mTracksStartTime;
|
2013-03-07 12:53:45 +04:00
|
|
|
#ifdef DEBUG
|
2015-07-29 10:32:10 +03:00
|
|
|
if (bufferEnd < mProcessedTime) {
|
2015-06-04 01:25:57 +03:00
|
|
|
STREAM_LOG(LogLevel::Error, ("MediaStream %p underrun, "
|
2015-07-29 10:32:10 +03:00
|
|
|
"bufferEnd %f < mProcessedTime %f (%lld < %lld), Streamtime %lld",
|
|
|
|
aStream, MediaTimeToSeconds(bufferEnd), MediaTimeToSeconds(mProcessedTime),
|
2016-01-26 05:49:01 +03:00
|
|
|
bufferEnd, mProcessedTime, aStream->GetTracksEnd()));
|
2013-03-07 12:53:45 +04:00
|
|
|
aStream->DumpTrackInfo();
|
2015-07-29 10:32:10 +03:00
|
|
|
NS_ASSERTION(bufferEnd >= mProcessedTime, "Buffer underran");
|
2013-03-07 12:53:45 +04:00
|
|
|
}
|
|
|
|
#endif
|
2015-09-16 07:17:30 +03:00
|
|
|
return std::min(bufferEnd, aEndBlockingDecisions);
|
2012-04-30 07:11:26 +04:00
|
|
|
}
|
|
|
|
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
namespace {
|
|
|
|
// Value of mCycleMarker for unvisited streams in cycle detection.
|
|
|
|
const uint32_t NOT_VISITED = UINT32_MAX;
|
|
|
|
// Value of mCycleMarker for ordered streams in muted cycles.
|
|
|
|
const uint32_t IN_MUTED_CYCLE = 1;
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
|
2016-03-08 20:11:08 +03:00
|
|
|
bool
|
|
|
|
MediaStreamGraphImpl::AudioTrackPresent(bool& aNeedsAEC)
|
2012-07-31 16:17:21 +04:00
|
|
|
{
|
2016-04-13 21:31:35 +03:00
|
|
|
AssertOnGraphThreadOrNotRunning();
|
|
|
|
|
2014-08-25 17:25:49 +04:00
|
|
|
bool audioTrackPresent = false;
|
2016-03-08 20:11:08 +03:00
|
|
|
for (uint32_t i = 0; i < mStreams.Length() && audioTrackPresent == false; ++i) {
|
2014-07-17 04:55:55 +04:00
|
|
|
MediaStream* stream = mStreams[i];
|
2016-03-08 20:11:08 +03:00
|
|
|
SourceMediaStream* source = stream->AsSourceStream();
|
2014-09-09 20:23:01 +04:00
|
|
|
#ifdef MOZ_WEBRTC
|
2016-03-08 20:11:08 +03:00
|
|
|
if (source && source->NeedsMixing()) {
|
|
|
|
aNeedsAEC = true;
|
2014-03-24 14:06:06 +04:00
|
|
|
}
|
2014-09-09 20:23:01 +04:00
|
|
|
#endif
|
Bug 1060311 - Force the use of an AudioCallbackDriver when at least an AudioNodeStream is present in the graph. r=jesup
This prevent a bug where the graph would be using a SystemClockDriver even if it
was rendering Web Audio API content.
It went like this:
- An AudioContext was created.
- Some AudioNodeStream (Web Audio API MediaStreams) were created, but their
MediaStreamTrack was not added yet
- During the stream ordering, we would see that we were running an
AudioCallbackDriver (because the MSG was created using an AudioContext, and we
pass in hints regarding the type of MediaStreams that will be added in the
future, to open the audio stream as early as we can, because it can take some
time, the MSG was created directly using an AudioCallbackDriver)
- Also during the stream ordering, we see that none of our MediaStream have an
MediaStreamTrack with an audio track. This triggers a switch to a
SystemClockDriver, because the graph thinks there is no audio.
- During CreateAndDestroyAudioNode, we would not switch to an
AudioCallbackDriver on the first iteration (right after the UpdateStreamOrder
call), because we would be switching, and not during the iteration after,
because we thought we already switched (the first patch makes this more robust).
This basically forces an AudioCallbackDriver if there is an AudioNodeStream,
which prevents unnecessary GraphDriver switches (and save threads creation
destruction, audio stream create and destruction, and all other resources
associated with a GraphDriver).
2014-08-29 22:26:29 +04:00
|
|
|
// If this is a AudioNodeStream, force a AudioCallbackDriver.
|
|
|
|
if (stream->AsAudioNodeStream()) {
|
|
|
|
audioTrackPresent = true;
|
2015-02-24 17:59:29 +03:00
|
|
|
} else {
|
2016-01-26 05:49:01 +03:00
|
|
|
for (StreamTracks::TrackIter tracks(stream->GetStreamTracks(), MediaSegment::AUDIO);
|
2015-02-24 17:59:29 +03:00
|
|
|
!tracks.IsEnded(); tracks.Next()) {
|
|
|
|
audioTrackPresent = true;
|
|
|
|
}
|
2014-08-25 17:25:49 +04:00
|
|
|
}
|
2016-03-08 20:11:08 +03:00
|
|
|
if (source) {
|
|
|
|
for (auto& data : source->mPendingTracks) {
|
|
|
|
if (data.mData->GetType() == MediaSegment::AUDIO) {
|
|
|
|
audioTrackPresent = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-07-31 16:17:21 +04:00
|
|
|
}
|
2016-03-08 20:11:08 +03:00
|
|
|
|
2016-03-08 20:11:08 +03:00
|
|
|
// XXX For some reason, there are race conditions when starting an audio input where
|
|
|
|
// we find no active audio tracks. In any case, if we have an active audio input we
|
|
|
|
// should not allow a switch back to a SystemClockDriver
|
|
|
|
if (!audioTrackPresent && mInputDeviceUsers.Count() != 0) {
|
|
|
|
NS_WARNING("No audio tracks, but full-duplex audio is enabled!!!!!");
|
|
|
|
audioTrackPresent = true;
|
2016-03-15 20:03:43 +03:00
|
|
|
#ifdef MOZ_WEBRTC
|
2016-04-08 21:54:00 +03:00
|
|
|
aNeedsAEC = true;
|
2016-03-15 20:03:43 +03:00
|
|
|
#endif
|
2016-03-08 20:11:08 +03:00
|
|
|
}
|
|
|
|
|
2016-03-08 20:11:08 +03:00
|
|
|
return audioTrackPresent;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStreamGraphImpl::UpdateStreamOrder()
|
|
|
|
{
|
|
|
|
bool shouldAEC = false;
|
|
|
|
bool audioTrackPresent = AudioTrackPresent(shouldAEC);
|
|
|
|
|
2016-01-21 19:51:36 +03:00
|
|
|
// Note that this looks for any audio streams, input or output, and switches to a
|
2016-03-08 20:11:08 +03:00
|
|
|
// SystemClockDriver if there are none. However, if another is already pending, let that
|
|
|
|
// switch happen.
|
2012-07-31 16:17:21 +04:00
|
|
|
|
2015-11-09 11:32:00 +03:00
|
|
|
if (!audioTrackPresent && mRealtime &&
|
2014-08-26 19:02:07 +04:00
|
|
|
CurrentDriver()->AsAudioCallbackDriver()) {
|
2015-02-24 17:59:29 +03:00
|
|
|
MonitorAutoLock mon(mMonitor);
|
2016-03-08 20:11:08 +03:00
|
|
|
if (CurrentDriver()->AsAudioCallbackDriver()->IsStarted() &&
|
|
|
|
!(CurrentDriver()->Switching())) {
|
2014-09-03 17:52:43 +04:00
|
|
|
if (mLifecycleState == LIFECYCLE_RUNNING) {
|
|
|
|
SystemClockDriver* driver = new SystemClockDriver(this);
|
|
|
|
CurrentDriver()->SwitchAtNextIteration(driver);
|
|
|
|
}
|
2014-08-26 19:02:07 +04:00
|
|
|
}
|
2014-08-26 19:01:33 +04:00
|
|
|
}
|
|
|
|
|
2015-12-01 13:48:02 +03:00
|
|
|
bool switching = false;
|
|
|
|
{
|
|
|
|
MonitorAutoLock mon(mMonitor);
|
|
|
|
switching = CurrentDriver()->Switching();
|
|
|
|
}
|
|
|
|
|
2015-11-09 11:32:00 +03:00
|
|
|
if (audioTrackPresent && mRealtime &&
|
|
|
|
!CurrentDriver()->AsAudioCallbackDriver() &&
|
2015-12-01 13:48:02 +03:00
|
|
|
!switching) {
|
2015-11-09 11:32:00 +03:00
|
|
|
MonitorAutoLock mon(mMonitor);
|
|
|
|
if (mLifecycleState == LIFECYCLE_RUNNING) {
|
|
|
|
AudioCallbackDriver* driver = new AudioCallbackDriver(this);
|
|
|
|
CurrentDriver()->SwitchAtNextIteration(driver);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-09 20:23:01 +04:00
|
|
|
#ifdef MOZ_WEBRTC
|
2016-04-13 21:31:35 +03:00
|
|
|
// Whenever we change AEC state, notify the current driver, which also
|
|
|
|
// will sample the state when the driver inits
|
2014-08-26 19:02:31 +04:00
|
|
|
if (shouldAEC && !mFarendObserverRef && gFarendObserver) {
|
|
|
|
mFarendObserverRef = gFarendObserver;
|
|
|
|
mMixer.AddCallback(mFarendObserverRef);
|
2016-04-13 21:31:35 +03:00
|
|
|
if (CurrentDriver()->AsAudioCallbackDriver()) {
|
|
|
|
CurrentDriver()->AsAudioCallbackDriver()->SetMicrophoneActive(true);
|
|
|
|
}
|
2014-08-26 19:02:31 +04:00
|
|
|
} else if (!shouldAEC && mFarendObserverRef){
|
|
|
|
if (mMixer.FindCallback(mFarendObserverRef)) {
|
|
|
|
mMixer.RemoveCallback(mFarendObserverRef);
|
|
|
|
mFarendObserverRef = nullptr;
|
2016-04-13 21:31:35 +03:00
|
|
|
if (CurrentDriver()->AsAudioCallbackDriver()) {
|
|
|
|
CurrentDriver()->AsAudioCallbackDriver()->SetMicrophoneActive(false);
|
|
|
|
}
|
2014-07-29 20:45:03 +04:00
|
|
|
}
|
2014-03-24 14:06:06 +04:00
|
|
|
}
|
2014-09-09 20:23:01 +04:00
|
|
|
#endif
|
2014-03-24 14:06:06 +04:00
|
|
|
|
2015-02-24 17:59:29 +03:00
|
|
|
if (!mStreamOrderDirty) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mStreamOrderDirty = false;
|
|
|
|
|
2014-07-17 04:55:55 +04:00
|
|
|
// The algorithm for finding cycles is based on Tim Leslie's iterative
|
|
|
|
// implementation [1][2] of Pearce's variant [3] of Tarjan's strongly
|
|
|
|
// connected components (SCC) algorithm. There are variations (a) to
|
|
|
|
// distinguish whether streams in SCCs of size 1 are in a cycle and (b) to
|
|
|
|
// re-run the algorithm over SCCs with breaks at DelayNodes.
|
|
|
|
//
|
|
|
|
// [1] http://www.timl.id.au/?p=327
|
|
|
|
// [2] https://github.com/scipy/scipy/blob/e2c502fca/scipy/sparse/csgraph/_traversal.pyx#L582
|
|
|
|
// [3] http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.102.1707
|
|
|
|
//
|
|
|
|
// There are two stacks. One for the depth-first search (DFS),
|
|
|
|
mozilla::LinkedList<MediaStream> dfsStack;
|
|
|
|
// and another for streams popped from the DFS stack, but still being
|
|
|
|
// considered as part of SCCs involving streams on the stack.
|
|
|
|
mozilla::LinkedList<MediaStream> sccStack;
|
|
|
|
|
|
|
|
// An index into mStreams for the next stream found with no unsatisfied
|
|
|
|
// upstream dependencies.
|
|
|
|
uint32_t orderedStreamCount = 0;
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < mStreams.Length(); ++i) {
|
|
|
|
MediaStream* s = mStreams[i];
|
|
|
|
ProcessedMediaStream* ps = s->AsProcessedStream();
|
|
|
|
if (ps) {
|
|
|
|
// The dfsStack initially contains a list of all processed streams in
|
|
|
|
// unchanged order.
|
|
|
|
dfsStack.insertBack(s);
|
|
|
|
ps->mCycleMarker = NOT_VISITED;
|
|
|
|
} else {
|
|
|
|
// SourceMediaStreams have no inputs and so can be ordered now.
|
|
|
|
mStreams[orderedStreamCount] = s;
|
|
|
|
++orderedStreamCount;
|
2012-07-31 16:17:21 +04:00
|
|
|
}
|
|
|
|
}
|
2014-07-17 04:55:55 +04:00
|
|
|
|
|
|
|
// mNextStackMarker corresponds to "index" in Tarjan's algorithm. It is a
|
|
|
|
// counter to label mCycleMarker on the next visited stream in the DFS
|
|
|
|
// uniquely in the set of visited streams that are still being considered.
|
|
|
|
//
|
|
|
|
// In this implementation, the counter descends so that the values are
|
|
|
|
// strictly greater than the values that mCycleMarker takes when the stream
|
|
|
|
// has been ordered (0 or IN_MUTED_CYCLE).
|
|
|
|
//
|
|
|
|
// Each new stream labelled, as the DFS searches upstream, receives a value
|
|
|
|
// less than those used for all other streams being considered.
|
|
|
|
uint32_t nextStackMarker = NOT_VISITED - 1;
|
|
|
|
// Reset list of DelayNodes in cycles stored at the tail of mStreams.
|
|
|
|
mFirstCycleBreaker = mStreams.Length();
|
|
|
|
|
|
|
|
// Rearrange dfsStack order as required to DFS upstream and pop streams
|
|
|
|
// in processing order to place in mStreams.
|
|
|
|
while (auto ps = static_cast<ProcessedMediaStream*>(dfsStack.getFirst())) {
|
|
|
|
const auto& inputs = ps->mInputs;
|
|
|
|
MOZ_ASSERT(ps->AsProcessedStream());
|
|
|
|
if (ps->mCycleMarker == NOT_VISITED) {
|
|
|
|
// Record the position on the visited stack, so that any searches
|
|
|
|
// finding this stream again know how much of the stack is in the cycle.
|
|
|
|
ps->mCycleMarker = nextStackMarker;
|
|
|
|
--nextStackMarker;
|
|
|
|
// Not-visited input streams should be processed first.
|
|
|
|
// SourceMediaStreams have already been ordered.
|
|
|
|
for (uint32_t i = inputs.Length(); i--; ) {
|
2015-09-16 07:15:21 +03:00
|
|
|
if (inputs[i]->mSource->IsSuspended()) {
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
continue;
|
|
|
|
}
|
2014-07-17 04:55:55 +04:00
|
|
|
auto input = inputs[i]->mSource->AsProcessedStream();
|
|
|
|
if (input && input->mCycleMarker == NOT_VISITED) {
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
// It can be that this stream has an input which is from a suspended
|
|
|
|
// AudioContext.
|
|
|
|
if (input->isInList()) {
|
|
|
|
input->remove();
|
|
|
|
dfsStack.insertFront(input);
|
|
|
|
}
|
2014-07-17 04:55:55 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returning from DFS. Pop from dfsStack.
|
|
|
|
ps->remove();
|
|
|
|
|
|
|
|
// cycleStackMarker keeps track of the highest marker value on any
|
|
|
|
// upstream stream, if any, found receiving input, directly or indirectly,
|
|
|
|
// from the visited stack (and so from |ps|, making a cycle). In a
|
|
|
|
// variation from Tarjan's SCC algorithm, this does not include |ps|
|
|
|
|
// unless it is part of the cycle.
|
|
|
|
uint32_t cycleStackMarker = 0;
|
|
|
|
for (uint32_t i = inputs.Length(); i--; ) {
|
2015-09-16 07:15:21 +03:00
|
|
|
if (inputs[i]->mSource->IsSuspended()) {
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
continue;
|
|
|
|
}
|
2014-07-17 04:55:55 +04:00
|
|
|
auto input = inputs[i]->mSource->AsProcessedStream();
|
|
|
|
if (input) {
|
|
|
|
cycleStackMarker = std::max(cycleStackMarker, input->mCycleMarker);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cycleStackMarker <= IN_MUTED_CYCLE) {
|
|
|
|
// All inputs have been ordered and their stack markers have been removed.
|
|
|
|
// This stream is not part of a cycle. It can be processed next.
|
|
|
|
ps->mCycleMarker = 0;
|
|
|
|
mStreams[orderedStreamCount] = ps;
|
|
|
|
++orderedStreamCount;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// A cycle has been found. Record this stream for ordering when all
|
|
|
|
// streams in this SCC have been popped from the DFS stack.
|
|
|
|
sccStack.insertFront(ps);
|
|
|
|
|
|
|
|
if (cycleStackMarker > ps->mCycleMarker) {
|
|
|
|
// Cycles have been found that involve streams that remain on the stack.
|
|
|
|
// Leave mCycleMarker indicating the most downstream (last) stream on
|
|
|
|
// the stack known to be part of this SCC. In this way, any searches on
|
|
|
|
// other paths that find |ps| will know (without having to traverse from
|
|
|
|
// this stream again) that they are part of this SCC (i.e. part of an
|
|
|
|
// intersecting cycle).
|
|
|
|
ps->mCycleMarker = cycleStackMarker;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// |ps| is the root of an SCC involving no other streams on dfsStack, the
|
|
|
|
// complete SCC has been recorded, and streams in this SCC are part of at
|
|
|
|
// least one cycle.
|
|
|
|
MOZ_ASSERT(cycleStackMarker == ps->mCycleMarker);
|
|
|
|
// If there are DelayNodes in this SCC, then they may break the cycles.
|
|
|
|
bool haveDelayNode = false;
|
2014-07-24 13:58:01 +04:00
|
|
|
auto next = sccStack.getFirst();
|
2014-07-17 04:55:55 +04:00
|
|
|
// Streams in this SCC are identified by mCycleMarker <= cycleStackMarker.
|
|
|
|
// (There may be other streams later in sccStack from other incompletely
|
|
|
|
// searched SCCs, involving streams still on dfsStack.)
|
|
|
|
//
|
|
|
|
// DelayNodes in cycles must behave differently from those not in cycles,
|
|
|
|
// so all DelayNodes in the SCC must be identified.
|
2014-07-24 13:58:01 +04:00
|
|
|
while (next && static_cast<ProcessedMediaStream*>(next)->
|
|
|
|
mCycleMarker <= cycleStackMarker) {
|
2014-07-17 04:55:55 +04:00
|
|
|
auto ns = next->AsAudioNodeStream();
|
|
|
|
// Get next before perhaps removing from list below.
|
2014-07-24 13:58:01 +04:00
|
|
|
next = next->getNext();
|
2014-07-17 04:55:55 +04:00
|
|
|
if (ns && ns->Engine()->AsDelayNodeEngine()) {
|
|
|
|
haveDelayNode = true;
|
|
|
|
// DelayNodes break cycles by producing their output in a
|
|
|
|
// preprocessing phase; they do not need to be ordered before their
|
|
|
|
// consumers. Order them at the tail of mStreams so that they can be
|
|
|
|
// handled specially. Do so now, so that DFS ignores them.
|
|
|
|
ns->remove();
|
|
|
|
ns->mCycleMarker = 0;
|
|
|
|
--mFirstCycleBreaker;
|
|
|
|
mStreams[mFirstCycleBreaker] = ns;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
auto after_scc = next;
|
2014-07-24 13:58:01 +04:00
|
|
|
while ((next = sccStack.getFirst()) != after_scc) {
|
|
|
|
next->remove();
|
|
|
|
auto removed = static_cast<ProcessedMediaStream*>(next);
|
2014-07-17 04:55:55 +04:00
|
|
|
if (haveDelayNode) {
|
|
|
|
// Return streams to the DFS stack again (to order and detect cycles
|
|
|
|
// without delayNodes). Any of these streams that are still inputs
|
|
|
|
// for streams on the visited stack must be returned to the front of
|
|
|
|
// the stack to be ordered before their dependents. We know that none
|
|
|
|
// of these streams need input from streams on the visited stack, so
|
|
|
|
// they can all be searched and ordered before the current stack head
|
|
|
|
// is popped.
|
2014-07-24 13:58:01 +04:00
|
|
|
removed->mCycleMarker = NOT_VISITED;
|
|
|
|
dfsStack.insertFront(removed);
|
2014-07-17 04:55:55 +04:00
|
|
|
} else {
|
|
|
|
// Streams in cycles without any DelayNodes must be muted, and so do
|
|
|
|
// not need input and can be ordered now. They must be ordered before
|
|
|
|
// their consumers so that their muted output is available.
|
2014-07-24 13:58:01 +04:00
|
|
|
removed->mCycleMarker = IN_MUTED_CYCLE;
|
|
|
|
mStreams[orderedStreamCount] = removed;
|
2014-07-17 04:55:55 +04:00
|
|
|
++orderedStreamCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(orderedStreamCount == mFirstCycleBreaker);
|
2012-07-31 16:17:21 +04:00
|
|
|
}
|
|
|
|
|
2012-09-20 04:47:51 +04:00
|
|
|
void
|
|
|
|
MediaStreamGraphImpl::NotifyHasCurrentData(MediaStream* aStream)
|
|
|
|
{
|
2013-03-20 15:19:39 +04:00
|
|
|
if (!aStream->mNotifiedHasCurrentData && aStream->mHasCurrentData) {
|
|
|
|
for (uint32_t j = 0; j < aStream->mListeners.Length(); ++j) {
|
|
|
|
MediaStreamListener* l = aStream->mListeners[j];
|
|
|
|
l->NotifyHasCurrentData(this);
|
|
|
|
}
|
|
|
|
aStream->mNotifiedHasCurrentData = true;
|
2012-09-20 04:47:51 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-30 07:11:26 +04:00
|
|
|
void
|
2015-09-16 07:23:14 +03:00
|
|
|
MediaStreamGraphImpl::CreateOrDestroyAudioStreams(MediaStream* aStream)
|
2012-04-30 07:11:26 +04:00
|
|
|
{
|
2013-05-08 15:44:07 +04:00
|
|
|
MOZ_ASSERT(mRealtime, "Should only attempt to create audio streams in real-time mode");
|
|
|
|
|
2015-05-13 16:34:56 +03:00
|
|
|
if (aStream->mAudioOutputs.IsEmpty()) {
|
|
|
|
aStream->mAudioOutputStreams.Clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-26 05:49:01 +03:00
|
|
|
if (!aStream->GetStreamTracks().GetAndResetTracksDirty() &&
|
2015-10-29 08:18:38 +03:00
|
|
|
!aStream->mAudioOutputStreams.IsEmpty()) {
|
2015-05-13 16:35:10 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-29 08:19:51 +03:00
|
|
|
STREAM_LOG(LogLevel::Debug, ("Updating AudioOutputStreams for MediaStream %p", aStream));
|
|
|
|
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<bool,2> audioOutputStreamsFound;
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < aStream->mAudioOutputStreams.Length(); ++i) {
|
2012-07-31 16:17:22 +04:00
|
|
|
audioOutputStreamsFound.AppendElement(false);
|
|
|
|
}
|
|
|
|
|
2016-01-26 05:49:01 +03:00
|
|
|
for (StreamTracks::TrackIter tracks(aStream->GetStreamTracks(), MediaSegment::AUDIO);
|
2015-05-13 16:35:10 +03:00
|
|
|
!tracks.IsEnded(); tracks.Next()) {
|
|
|
|
uint32_t i;
|
|
|
|
for (i = 0; i < audioOutputStreamsFound.Length(); ++i) {
|
|
|
|
if (aStream->mAudioOutputStreams[i].mTrackID == tracks->GetID()) {
|
|
|
|
break;
|
2012-07-31 16:17:22 +04:00
|
|
|
}
|
2015-05-13 16:35:10 +03:00
|
|
|
}
|
|
|
|
if (i < audioOutputStreamsFound.Length()) {
|
|
|
|
audioOutputStreamsFound[i] = true;
|
|
|
|
} else {
|
|
|
|
MediaStream::AudioOutputStream* audioOutputStream =
|
|
|
|
aStream->mAudioOutputStreams.AppendElement();
|
2015-09-16 07:23:14 +03:00
|
|
|
audioOutputStream->mAudioPlaybackStartTime = mProcessedTime;
|
2015-05-13 16:35:10 +03:00
|
|
|
audioOutputStream->mBlockedAudioTime = 0;
|
|
|
|
audioOutputStream->mLastTickWritten = 0;
|
|
|
|
audioOutputStream->mTrackID = tracks->GetID();
|
|
|
|
|
2015-12-01 13:48:02 +03:00
|
|
|
bool switching = false;
|
|
|
|
|
|
|
|
{
|
|
|
|
MonitorAutoLock lock(mMonitor);
|
|
|
|
switching = CurrentDriver()->Switching();
|
|
|
|
}
|
|
|
|
|
2015-05-13 16:35:10 +03:00
|
|
|
if (!CurrentDriver()->AsAudioCallbackDriver() &&
|
2015-12-01 13:48:02 +03:00
|
|
|
!switching) {
|
2015-05-13 16:35:10 +03:00
|
|
|
MonitorAutoLock mon(mMonitor);
|
|
|
|
if (mLifecycleState == LIFECYCLE_RUNNING) {
|
|
|
|
AudioCallbackDriver* driver = new AudioCallbackDriver(this);
|
|
|
|
CurrentDriver()->SwitchAtNextIteration(driver);
|
2014-08-25 17:25:49 +04:00
|
|
|
}
|
2012-07-31 16:17:22 +04:00
|
|
|
}
|
2012-04-30 07:11:26 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (int32_t i = audioOutputStreamsFound.Length() - 1; i >= 0; --i) {
|
2012-07-31 16:17:22 +04:00
|
|
|
if (!audioOutputStreamsFound[i]) {
|
|
|
|
aStream->mAudioOutputStreams.RemoveElementAt(i);
|
|
|
|
}
|
2012-04-30 07:11:26 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-18 09:20:43 +04:00
|
|
|
StreamTime
|
2015-09-08 07:58:19 +03:00
|
|
|
MediaStreamGraphImpl::PlayAudio(MediaStream* aStream)
|
2012-04-30 07:11:26 +04:00
|
|
|
{
|
2013-05-08 15:44:07 +04:00
|
|
|
MOZ_ASSERT(mRealtime, "Should only attempt to play audio in realtime mode");
|
|
|
|
|
2012-04-30 07:11:26 +04:00
|
|
|
float volume = 0.0f;
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < aStream->mAudioOutputs.Length(); ++i) {
|
2012-04-30 07:11:26 +04:00
|
|
|
volume += aStream->mAudioOutputs[i].mVolume;
|
|
|
|
}
|
|
|
|
|
2015-09-04 08:58:26 +03:00
|
|
|
StreamTime ticksWritten = 0;
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < aStream->mAudioOutputStreams.Length(); ++i) {
|
2015-09-04 08:58:26 +03:00
|
|
|
ticksWritten = 0;
|
|
|
|
|
2012-07-31 16:17:22 +04:00
|
|
|
MediaStream::AudioOutputStream& audioOutput = aStream->mAudioOutputStreams[i];
|
2016-01-26 05:49:01 +03:00
|
|
|
StreamTracks::Track* track = aStream->mTracks.FindTrack(audioOutput.mTrackID);
|
2012-07-31 16:17:22 +04:00
|
|
|
AudioSegment* audio = track->Get<AudioSegment>();
|
2014-03-24 14:06:06 +04:00
|
|
|
AudioSegment output;
|
|
|
|
|
2015-09-08 08:19:03 +03:00
|
|
|
StreamTime offset = aStream->GraphTimeToStreamTime(mProcessedTime);
|
2012-04-30 07:11:26 +04:00
|
|
|
|
2016-01-26 05:49:01 +03:00
|
|
|
// We don't update aStream->mTracksStartTime here to account for time spent
|
2014-04-25 20:04:23 +04:00
|
|
|
// blocked. Instead, we'll update it in UpdateCurrentTimeForStreams after
|
|
|
|
// the blocked period has completed. But we do need to make sure we play
|
|
|
|
// from the right offsets in the stream buffer, even if we've already
|
|
|
|
// written silence for some amount of blocked time after the current time.
|
2015-09-16 07:23:14 +03:00
|
|
|
GraphTime t = mProcessedTime;
|
2015-09-08 07:58:19 +03:00
|
|
|
while (t < mStateComputedTime) {
|
2015-09-04 09:44:43 +03:00
|
|
|
bool blocked = t >= aStream->mStartBlocking;
|
2015-09-08 07:58:19 +03:00
|
|
|
GraphTime end = blocked ? mStateComputedTime : aStream->mStartBlocking;
|
|
|
|
NS_ASSERTION(end <= mStateComputedTime, "mStartBlocking is wrong!");
|
2012-04-30 07:11:26 +04:00
|
|
|
|
2014-03-24 14:06:06 +04:00
|
|
|
// Check how many ticks of sound we can provide if we are blocked some
|
|
|
|
// time in the middle of this cycle.
|
2015-09-04 09:04:09 +03:00
|
|
|
StreamTime toWrite = end - t;
|
2014-03-24 14:06:06 +04:00
|
|
|
|
2012-07-31 16:17:22 +04:00
|
|
|
if (blocked) {
|
2014-03-24 14:06:06 +04:00
|
|
|
output.InsertNullDataAtStart(toWrite);
|
2014-08-26 19:01:33 +04:00
|
|
|
ticksWritten += toWrite;
|
2015-06-04 01:25:57 +03:00
|
|
|
STREAM_LOG(LogLevel::Verbose, ("MediaStream %p writing %ld blocking-silence samples for %f to %f (%ld to %ld)\n",
|
2014-03-24 14:06:06 +04:00
|
|
|
aStream, toWrite, MediaTimeToSeconds(t), MediaTimeToSeconds(end),
|
|
|
|
offset, offset + toWrite));
|
2012-07-31 16:17:22 +04:00
|
|
|
} else {
|
2014-09-18 09:20:43 +04:00
|
|
|
StreamTime endTicksNeeded = offset + toWrite;
|
|
|
|
StreamTime endTicksAvailable = audio->GetDuration();
|
2014-06-12 08:40:51 +04:00
|
|
|
|
2014-03-24 14:06:06 +04:00
|
|
|
if (endTicksNeeded <= endTicksAvailable) {
|
2015-06-04 01:25:57 +03:00
|
|
|
STREAM_LOG(LogLevel::Verbose,
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
("MediaStream %p writing %ld samples for %f to %f "
|
|
|
|
"(samples %ld to %ld)\n",
|
|
|
|
aStream, toWrite, MediaTimeToSeconds(t),
|
|
|
|
MediaTimeToSeconds(end), offset, endTicksNeeded));
|
2014-03-24 14:06:06 +04:00
|
|
|
output.AppendSlice(*audio, offset, endTicksNeeded);
|
2014-08-26 19:01:33 +04:00
|
|
|
ticksWritten += toWrite;
|
2014-06-12 08:40:51 +04:00
|
|
|
offset = endTicksNeeded;
|
2014-03-24 14:06:06 +04:00
|
|
|
} else {
|
2014-08-26 19:01:33 +04:00
|
|
|
// MOZ_ASSERT(track->IsEnded(), "Not enough data, and track not ended.");
|
2014-03-24 14:06:06 +04:00
|
|
|
// If we are at the end of the track, maybe write the remaining
|
|
|
|
// samples, and pad with/output silence.
|
|
|
|
if (endTicksNeeded > endTicksAvailable &&
|
|
|
|
offset < endTicksAvailable) {
|
|
|
|
output.AppendSlice(*audio, offset, endTicksAvailable);
|
2015-06-04 01:25:57 +03:00
|
|
|
STREAM_LOG(LogLevel::Verbose,
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
("MediaStream %p writing %ld samples for %f to %f "
|
|
|
|
"(samples %ld to %ld)\n",
|
|
|
|
aStream, toWrite, MediaTimeToSeconds(t),
|
|
|
|
MediaTimeToSeconds(end), offset, endTicksNeeded));
|
2014-12-09 16:11:32 +03:00
|
|
|
uint32_t available = endTicksAvailable - offset;
|
|
|
|
ticksWritten += available;
|
|
|
|
toWrite -= available;
|
2014-06-12 08:40:51 +04:00
|
|
|
offset = endTicksAvailable;
|
2014-03-24 14:06:06 +04:00
|
|
|
}
|
|
|
|
output.AppendNullData(toWrite);
|
2015-06-04 01:25:57 +03:00
|
|
|
STREAM_LOG(LogLevel::Verbose,
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
("MediaStream %p writing %ld padding slsamples for %f to "
|
|
|
|
"%f (samples %ld to %ld)\n",
|
|
|
|
aStream, toWrite, MediaTimeToSeconds(t),
|
|
|
|
MediaTimeToSeconds(end), offset, endTicksNeeded));
|
2014-12-09 16:11:32 +03:00
|
|
|
ticksWritten += toWrite;
|
2012-07-31 16:17:22 +04:00
|
|
|
}
|
|
|
|
output.ApplyVolume(volume);
|
|
|
|
}
|
|
|
|
t = end;
|
2012-04-30 07:11:26 +04:00
|
|
|
}
|
2014-06-12 08:40:51 +04:00
|
|
|
audioOutput.mLastTickWritten = offset;
|
2014-03-24 14:06:06 +04:00
|
|
|
|
|
|
|
// Need unique id for stream & track - and we want it to match the inserter
|
|
|
|
output.WriteTo(LATENCY_STREAM_ID(aStream, track->GetID()),
|
2014-08-25 17:25:49 +04:00
|
|
|
mMixer, AudioChannelCount(),
|
|
|
|
mSampleRate);
|
2012-04-30 07:11:26 +04:00
|
|
|
}
|
2014-03-24 14:06:06 +04:00
|
|
|
return ticksWritten;
|
2012-04-30 07:11:26 +04:00
|
|
|
}
|
|
|
|
|
2013-05-30 08:44:43 +04:00
|
|
|
static void
|
|
|
|
SetImageToBlackPixel(PlanarYCbCrImage* aImage)
|
|
|
|
{
|
|
|
|
uint8_t blackPixel[] = { 0x10, 0x80, 0x80 };
|
|
|
|
|
2013-10-02 04:57:50 +04:00
|
|
|
PlanarYCbCrData data;
|
2013-05-30 08:44:43 +04:00
|
|
|
data.mYChannel = blackPixel;
|
|
|
|
data.mCbChannel = blackPixel + 1;
|
|
|
|
data.mCrChannel = blackPixel + 2;
|
|
|
|
data.mYStride = data.mCbCrStride = 1;
|
2013-12-31 13:06:12 +04:00
|
|
|
data.mPicSize = data.mYSize = data.mCbCrSize = IntSize(1, 1);
|
2016-04-19 06:12:41 +03:00
|
|
|
aImage->CopyData(data);
|
2013-05-30 08:44:43 +04:00
|
|
|
}
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class VideoFrameContainerInvalidateRunnable : public Runnable {
|
2015-03-16 20:12:36 +03:00
|
|
|
public:
|
|
|
|
explicit VideoFrameContainerInvalidateRunnable(VideoFrameContainer* aVideoFrameContainer)
|
|
|
|
: mVideoFrameContainer(aVideoFrameContainer)
|
|
|
|
{}
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
mVideoFrameContainer->Invalidate();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
private:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<VideoFrameContainer> mVideoFrameContainer;
|
2015-03-16 20:12:36 +03:00
|
|
|
};
|
|
|
|
|
2012-04-30 07:11:26 +04:00
|
|
|
void
|
|
|
|
MediaStreamGraphImpl::PlayVideo(MediaStream* aStream)
|
|
|
|
{
|
2013-05-08 15:44:07 +04:00
|
|
|
MOZ_ASSERT(mRealtime, "Should only attempt to play video in realtime mode");
|
|
|
|
|
2012-04-30 07:11:26 +04:00
|
|
|
if (aStream->mVideoOutputs.IsEmpty())
|
|
|
|
return;
|
|
|
|
|
2015-08-14 05:16:57 +03:00
|
|
|
TimeStamp currentTimeStamp = CurrentDriver()->GetCurrentTimeStamp();
|
2012-04-30 07:11:26 +04:00
|
|
|
|
2015-08-14 05:16:57 +03:00
|
|
|
// Collect any new frames produced in this iteration.
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<ImageContainer::NonOwningImage,4> newImages;
|
2016-02-04 04:27:09 +03:00
|
|
|
PrincipalHandle lastPrincipalHandle = PRINCIPAL_HANDLE_NONE;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Image> blackImage;
|
2015-08-14 05:16:57 +03:00
|
|
|
|
2016-01-26 05:49:01 +03:00
|
|
|
MOZ_ASSERT(mProcessedTime >= aStream->mTracksStartTime, "frame position before buffer?");
|
2015-09-08 08:18:01 +03:00
|
|
|
// We only look at the non-blocking interval
|
|
|
|
StreamTime frameBufferTime = aStream->GraphTimeToStreamTime(mProcessedTime);
|
|
|
|
StreamTime bufferEndTime = aStream->GraphTimeToStreamTime(aStream->mStartBlocking);
|
2014-09-18 09:20:43 +04:00
|
|
|
StreamTime start;
|
2015-08-14 05:16:57 +03:00
|
|
|
const VideoChunk* chunk;
|
|
|
|
for ( ;
|
|
|
|
frameBufferTime < bufferEndTime;
|
|
|
|
frameBufferTime = start + chunk->GetDuration()) {
|
|
|
|
// Pick the last track that has a video chunk for the time, and
|
|
|
|
// schedule its frame.
|
|
|
|
chunk = nullptr;
|
2016-01-26 05:49:01 +03:00
|
|
|
for (StreamTracks::TrackIter tracks(aStream->GetStreamTracks(),
|
2015-08-14 05:16:57 +03:00
|
|
|
MediaSegment::VIDEO);
|
|
|
|
!tracks.IsEnded();
|
|
|
|
tracks.Next()) {
|
|
|
|
VideoSegment* segment = tracks->Get<VideoSegment>();
|
|
|
|
StreamTime thisStart;
|
|
|
|
const VideoChunk* thisChunk =
|
|
|
|
segment->FindChunkContaining(frameBufferTime, &thisStart);
|
|
|
|
if (thisChunk && thisChunk->mFrame.GetImage()) {
|
|
|
|
start = thisStart;
|
|
|
|
chunk = thisChunk;
|
|
|
|
}
|
2012-04-30 07:11:26 +04:00
|
|
|
}
|
2015-08-14 05:16:57 +03:00
|
|
|
if (!chunk)
|
|
|
|
break;
|
|
|
|
|
|
|
|
const VideoFrame* frame = &chunk->mFrame;
|
|
|
|
if (*frame == aStream->mLastPlayedVideoFrame) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Image* image = frame->GetImage();
|
|
|
|
STREAM_LOG(LogLevel::Verbose,
|
|
|
|
("MediaStream %p writing video frame %p (%dx%d)",
|
|
|
|
aStream, image, frame->GetIntrinsicSize().width,
|
|
|
|
frame->GetIntrinsicSize().height));
|
|
|
|
// Schedule this frame after the previous frame finishes, instead of at
|
|
|
|
// its start time. These times only differ in the case of multiple
|
|
|
|
// tracks.
|
2015-09-08 08:18:01 +03:00
|
|
|
// frameBufferTime is in the non-blocking interval.
|
|
|
|
GraphTime frameTime = aStream->StreamTimeToGraphTime(frameBufferTime);
|
2015-08-14 05:16:57 +03:00
|
|
|
TimeStamp targetTime = currentTimeStamp +
|
|
|
|
TimeDuration::FromSeconds(MediaTimeToSeconds(frameTime - IterationEnd()));
|
|
|
|
|
|
|
|
if (frame->GetForceBlack()) {
|
|
|
|
if (!blackImage) {
|
2015-11-17 11:09:01 +03:00
|
|
|
blackImage = aStream->mVideoOutputs[0]->GetImageContainer()->CreatePlanarYCbCrImage();
|
2015-08-14 05:16:57 +03:00
|
|
|
if (blackImage) {
|
|
|
|
// Sets the image to a single black pixel, which will be scaled to
|
|
|
|
// fill the rendered size.
|
2015-11-17 11:09:01 +03:00
|
|
|
SetImageToBlackPixel(blackImage->AsPlanarYCbCrImage());
|
2015-08-14 05:16:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (blackImage) {
|
|
|
|
image = blackImage;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
newImages.AppendElement(ImageContainer::NonOwningImage(image, targetTime));
|
|
|
|
|
2016-02-04 04:27:09 +03:00
|
|
|
lastPrincipalHandle = chunk->GetPrincipalHandle();
|
|
|
|
|
2015-08-14 05:16:57 +03:00
|
|
|
aStream->mLastPlayedVideoFrame = *frame;
|
2012-04-30 07:11:26 +04:00
|
|
|
}
|
2015-08-14 05:16:57 +03:00
|
|
|
|
|
|
|
if (!aStream->mLastPlayedVideoFrame.GetImage())
|
2012-07-31 16:17:22 +04:00
|
|
|
return;
|
|
|
|
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<ImageContainer::NonOwningImage,4> images;
|
2015-08-14 05:16:57 +03:00
|
|
|
bool haveMultipleImages = false;
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < aStream->mVideoOutputs.Length(); ++i) {
|
2012-07-31 16:17:22 +04:00
|
|
|
VideoFrameContainer* output = aStream->mVideoOutputs[i];
|
2013-05-30 08:44:43 +04:00
|
|
|
|
2016-02-04 04:27:09 +03:00
|
|
|
bool principalHandleChanged =
|
|
|
|
lastPrincipalHandle != PRINCIPAL_HANDLE_NONE &&
|
|
|
|
lastPrincipalHandle != output->GetLastPrincipalHandle();
|
|
|
|
|
2015-08-14 05:16:57 +03:00
|
|
|
// Find previous frames that may still be valid.
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<ImageContainer::OwningImage,4> previousImages;
|
2015-08-14 05:16:57 +03:00
|
|
|
output->GetImageContainer()->GetCurrentImages(&previousImages);
|
|
|
|
uint32_t j = previousImages.Length();
|
|
|
|
if (j) {
|
|
|
|
// Re-use the most recent frame before currentTimeStamp and subsequent,
|
|
|
|
// always keeping at least one frame.
|
|
|
|
do {
|
|
|
|
--j;
|
|
|
|
} while (j > 0 && previousImages[j].mTimeStamp > currentTimeStamp);
|
|
|
|
}
|
|
|
|
if (previousImages.Length() - j + newImages.Length() > 1) {
|
|
|
|
haveMultipleImages = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't update if there are no changes.
|
|
|
|
if (j == 0 && newImages.IsEmpty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for ( ; j < previousImages.Length(); ++j) {
|
|
|
|
const auto& image = previousImages[j];
|
|
|
|
// Cope with potential clock skew with AudioCallbackDriver.
|
|
|
|
if (newImages.Length() && image.mTimeStamp > newImages[0].mTimeStamp) {
|
|
|
|
STREAM_LOG(LogLevel::Warning,
|
|
|
|
("Dropping %u video frames due to clock skew",
|
|
|
|
unsigned(previousImages.Length() - j)));
|
|
|
|
break;
|
2013-05-30 08:44:43 +04:00
|
|
|
}
|
2015-08-14 05:16:57 +03:00
|
|
|
|
|
|
|
images.AppendElement(ImageContainer::
|
|
|
|
NonOwningImage(image.mImage,
|
|
|
|
image.mTimeStamp, image.mFrameID));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the frames from this iteration.
|
|
|
|
for (auto& image : newImages) {
|
|
|
|
image.mFrameID = output->NewFrameID();
|
|
|
|
images.AppendElement(image);
|
2013-05-30 08:44:43 +04:00
|
|
|
}
|
2016-02-04 04:27:09 +03:00
|
|
|
|
|
|
|
if (principalHandleChanged) {
|
|
|
|
output->UpdatePrincipalHandleForFrameID(lastPrincipalHandle,
|
|
|
|
newImages.LastElement().mFrameID);
|
|
|
|
}
|
|
|
|
|
2015-08-14 05:16:57 +03:00
|
|
|
output->SetCurrentFrames(aStream->mLastPlayedVideoFrame.GetIntrinsicSize(),
|
|
|
|
images);
|
2013-05-30 08:44:43 +04:00
|
|
|
|
2012-07-31 16:17:22 +04:00
|
|
|
nsCOMPtr<nsIRunnable> event =
|
2015-03-16 20:12:36 +03:00
|
|
|
new VideoFrameContainerInvalidateRunnable(output);
|
2014-11-20 21:10:00 +03:00
|
|
|
DispatchToMainThreadAfterStreamStateUpdate(event.forget());
|
2015-08-14 05:16:57 +03:00
|
|
|
|
|
|
|
images.ClearAndRetainStorage();
|
2012-07-31 16:17:22 +04:00
|
|
|
}
|
2015-08-14 05:16:57 +03:00
|
|
|
|
|
|
|
// If the stream has finished and the timestamps of all frames have expired
|
|
|
|
// then no more updates are required.
|
|
|
|
if (aStream->mFinished && !haveMultipleImages) {
|
|
|
|
aStream->mLastPlayedVideoFrame.SetNull();
|
2012-11-21 05:32:06 +04:00
|
|
|
}
|
2012-04-30 07:11:26 +04:00
|
|
|
}
|
|
|
|
|
2016-01-21 19:51:35 +03:00
|
|
|
void
|
2016-03-08 20:11:09 +03:00
|
|
|
MediaStreamGraphImpl::OpenAudioInputImpl(int aID,
|
2016-01-21 19:51:36 +03:00
|
|
|
AudioDataListener *aListener)
|
2016-01-21 19:51:35 +03:00
|
|
|
{
|
2016-02-04 05:12:51 +03:00
|
|
|
// Bug 1238038 Need support for multiple mics at once
|
|
|
|
if (mInputDeviceUsers.Count() > 0 &&
|
|
|
|
!mInputDeviceUsers.Get(aListener, nullptr)) {
|
|
|
|
NS_ASSERTION(false, "Input from multiple mics not yet supported; bug 1238038");
|
2016-01-21 19:51:36 +03:00
|
|
|
// Need to support separate input-only AudioCallback drivers; they'll
|
|
|
|
// call us back on "other" threads. We will need to echo-cancel them, though.
|
|
|
|
return;
|
|
|
|
}
|
2016-01-21 19:51:36 +03:00
|
|
|
mInputWanted = true;
|
2016-02-04 05:12:51 +03:00
|
|
|
|
|
|
|
// Add to count of users for this ID.
|
|
|
|
// XXX Since we can't rely on IDs staying valid (ugh), use the listener as
|
|
|
|
// a stand-in for the ID. Fix as part of support for multiple-captures
|
|
|
|
// (Bug 1238038)
|
|
|
|
uint32_t count = 0;
|
|
|
|
mInputDeviceUsers.Get(aListener, &count); // ok if this fails
|
|
|
|
count++;
|
|
|
|
mInputDeviceUsers.Put(aListener, count); // creates a new entry in the hash if needed
|
|
|
|
|
|
|
|
if (count == 1) { // first open for this listener
|
2016-03-08 20:11:08 +03:00
|
|
|
// aID is a cubeb_devid, and we assume that opaque ptr is valid until
|
|
|
|
// we close cubeb.
|
|
|
|
mInputDeviceID = aID;
|
2016-02-04 05:12:51 +03:00
|
|
|
mAudioInputs.AppendElement(aListener); // always monitor speaker data
|
2016-01-21 19:51:36 +03:00
|
|
|
|
2016-03-08 20:11:08 +03:00
|
|
|
// Switch Drivers since we're adding input (to input-only or full-duplex)
|
|
|
|
MonitorAutoLock mon(mMonitor);
|
|
|
|
if (mLifecycleState == LIFECYCLE_RUNNING) {
|
|
|
|
AudioCallbackDriver* driver = new AudioCallbackDriver(this);
|
|
|
|
STREAM_LOG(LogLevel::Debug, ("OpenAudioInput: starting new AudioCallbackDriver(input) %p", driver));
|
|
|
|
LIFECYCLE_LOG("OpenAudioInput: starting new AudioCallbackDriver(input) %p", driver);
|
|
|
|
driver->SetInputListener(aListener);
|
|
|
|
CurrentDriver()->SwitchAtNextIteration(driver);
|
|
|
|
} else {
|
|
|
|
STREAM_LOG(LogLevel::Error, ("OpenAudioInput in shutdown!"));
|
|
|
|
LIFECYCLE_LOG("OpenAudioInput in shutdown!");
|
|
|
|
NS_ASSERTION(false, "Can't open cubeb inputs in shutdown");
|
|
|
|
}
|
2016-01-21 19:51:36 +03:00
|
|
|
}
|
2016-01-21 19:51:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2016-03-08 20:11:09 +03:00
|
|
|
MediaStreamGraphImpl::OpenAudioInput(int aID,
|
2016-01-21 19:51:36 +03:00
|
|
|
AudioDataListener *aListener)
|
2016-01-21 19:51:35 +03:00
|
|
|
{
|
2016-01-21 19:51:36 +03:00
|
|
|
// So, so, so annoying. Can't AppendMessage except on Mainthread
|
2016-01-21 19:51:35 +03:00
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
NS_DispatchToMainThread(WrapRunnable(this,
|
|
|
|
&MediaStreamGraphImpl::OpenAudioInput,
|
2016-01-21 19:51:36 +03:00
|
|
|
aID, aListener));
|
2016-01-21 19:51:35 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
class Message : public ControlMessage {
|
|
|
|
public:
|
2016-03-08 20:11:09 +03:00
|
|
|
Message(MediaStreamGraphImpl *aGraph, int aID,
|
2016-01-21 19:51:36 +03:00
|
|
|
AudioDataListener *aListener) :
|
|
|
|
ControlMessage(nullptr), mGraph(aGraph), mID(aID), mListener(aListener) {}
|
2016-01-21 19:51:35 +03:00
|
|
|
virtual void Run()
|
|
|
|
{
|
2016-01-21 19:51:36 +03:00
|
|
|
mGraph->OpenAudioInputImpl(mID, mListener);
|
2016-01-21 19:51:35 +03:00
|
|
|
}
|
|
|
|
MediaStreamGraphImpl *mGraph;
|
2016-03-08 20:11:09 +03:00
|
|
|
int mID;
|
2016-01-21 19:51:36 +03:00
|
|
|
RefPtr<AudioDataListener> mListener;
|
2016-01-21 19:51:35 +03:00
|
|
|
};
|
2016-02-04 05:12:51 +03:00
|
|
|
// XXX Check not destroyed!
|
2016-01-21 00:14:33 +03:00
|
|
|
this->AppendMessage(MakeUnique<Message>(this, aID, aListener));
|
2016-01-21 19:51:35 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-01-21 19:51:36 +03:00
|
|
|
MediaStreamGraphImpl::CloseAudioInputImpl(AudioDataListener *aListener)
|
2016-01-21 19:51:35 +03:00
|
|
|
{
|
2016-02-04 05:12:51 +03:00
|
|
|
uint32_t count;
|
|
|
|
DebugOnly<bool> result = mInputDeviceUsers.Get(aListener, &count);
|
|
|
|
MOZ_ASSERT(result);
|
|
|
|
if (--count > 0) {
|
|
|
|
mInputDeviceUsers.Put(aListener, count);
|
|
|
|
return; // still in use
|
|
|
|
}
|
|
|
|
mInputDeviceUsers.Remove(aListener);
|
2016-03-08 20:11:09 +03:00
|
|
|
mInputDeviceID = -1;
|
2016-01-21 19:51:36 +03:00
|
|
|
mInputWanted = false;
|
2016-02-17 21:19:02 +03:00
|
|
|
AudioCallbackDriver *driver = CurrentDriver()->AsAudioCallbackDriver();
|
|
|
|
if (driver) {
|
|
|
|
driver->RemoveInputListener(aListener);
|
|
|
|
}
|
2016-01-21 19:51:35 +03:00
|
|
|
mAudioInputs.RemoveElement(aListener);
|
2016-01-21 19:51:36 +03:00
|
|
|
|
|
|
|
// Switch Drivers since we're adding or removing an input (to nothing/system or output only)
|
2016-03-08 20:11:08 +03:00
|
|
|
bool shouldAEC = false;
|
|
|
|
bool audioTrackPresent = AudioTrackPresent(shouldAEC);
|
2016-01-21 19:51:36 +03:00
|
|
|
|
|
|
|
MonitorAutoLock mon(mMonitor);
|
|
|
|
if (mLifecycleState == LIFECYCLE_RUNNING) {
|
|
|
|
GraphDriver* driver;
|
|
|
|
if (audioTrackPresent) {
|
|
|
|
// We still have audio output
|
|
|
|
STREAM_LOG(LogLevel::Debug, ("CloseInput: output present (AudioCallback)"));
|
|
|
|
|
|
|
|
driver = new AudioCallbackDriver(this);
|
|
|
|
CurrentDriver()->SwitchAtNextIteration(driver);
|
|
|
|
} else if (CurrentDriver()->AsAudioCallbackDriver()) {
|
|
|
|
STREAM_LOG(LogLevel::Debug, ("CloseInput: no output present (SystemClockCallback)"));
|
|
|
|
|
|
|
|
driver = new SystemClockDriver(this);
|
|
|
|
CurrentDriver()->SwitchAtNextIteration(driver);
|
|
|
|
} // else SystemClockDriver->SystemClockDriver, no switch
|
|
|
|
}
|
2016-01-21 19:51:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-01-21 19:51:36 +03:00
|
|
|
MediaStreamGraphImpl::CloseAudioInput(AudioDataListener *aListener)
|
2016-01-21 19:51:35 +03:00
|
|
|
{
|
2016-01-21 19:51:36 +03:00
|
|
|
// So, so, so annoying. Can't AppendMessage except on Mainthread
|
2016-01-21 19:51:35 +03:00
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
NS_DispatchToMainThread(WrapRunnable(this,
|
|
|
|
&MediaStreamGraphImpl::CloseAudioInput,
|
|
|
|
aListener));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
class Message : public ControlMessage {
|
|
|
|
public:
|
2016-01-21 19:51:36 +03:00
|
|
|
Message(MediaStreamGraphImpl *aGraph, AudioDataListener *aListener) :
|
2016-01-21 19:51:35 +03:00
|
|
|
ControlMessage(nullptr), mGraph(aGraph), mListener(aListener) {}
|
|
|
|
virtual void Run()
|
|
|
|
{
|
|
|
|
mGraph->CloseAudioInputImpl(mListener);
|
|
|
|
}
|
|
|
|
MediaStreamGraphImpl *mGraph;
|
2016-01-21 19:51:36 +03:00
|
|
|
RefPtr<AudioDataListener> mListener;
|
2016-01-21 19:51:35 +03:00
|
|
|
};
|
2016-01-21 00:14:33 +03:00
|
|
|
this->AppendMessage(MakeUnique<Message>(this, aListener));
|
2016-01-21 19:51:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// All AudioInput listeners get the same speaker data (at least for now).
|
|
|
|
void
|
2016-01-21 19:51:36 +03:00
|
|
|
MediaStreamGraph::NotifyOutputData(AudioDataValue* aBuffer, size_t aFrames,
|
2016-02-17 21:19:01 +03:00
|
|
|
TrackRate aRate, uint32_t aChannels)
|
2016-01-21 19:51:35 +03:00
|
|
|
{
|
|
|
|
for (auto& listener : mAudioInputs) {
|
2016-02-17 21:19:01 +03:00
|
|
|
listener->NotifyOutputData(this, aBuffer, aFrames, aRate, aChannels);
|
2016-01-21 19:51:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-19 18:40:57 +04:00
|
|
|
bool
|
|
|
|
MediaStreamGraphImpl::ShouldUpdateMainThread()
|
|
|
|
{
|
|
|
|
if (mRealtime) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
TimeStamp now = TimeStamp::Now();
|
2014-08-26 19:01:33 +04:00
|
|
|
if ((now - mLastMainThreadUpdate).ToMilliseconds() > CurrentDriver()->IterationDuration()) {
|
2013-07-19 18:40:57 +04:00
|
|
|
mLastMainThreadUpdate = now;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-04-30 07:11:26 +04:00
|
|
|
void
|
2013-06-17 17:06:34 +04:00
|
|
|
MediaStreamGraphImpl::PrepareUpdatesToMainThreadState(bool aFinalUpdate)
|
2012-04-30 07:11:26 +04:00
|
|
|
{
|
2014-08-26 19:01:33 +04:00
|
|
|
mMonitor.AssertCurrentThreadOwns();
|
2012-04-30 07:11:26 +04:00
|
|
|
|
2013-10-23 22:21:33 +04:00
|
|
|
// We don't want to frequently update the main thread about timing update
|
|
|
|
// when we are not running in realtime.
|
|
|
|
if (aFinalUpdate || ShouldUpdateMainThread()) {
|
2015-09-04 08:01:01 +03:00
|
|
|
mStreamUpdates.SetCapacity(mStreamUpdates.Length() + mStreams.Length() +
|
|
|
|
mSuspendedStreams.Length());
|
|
|
|
for (MediaStream* stream : AllStreams()) {
|
2015-06-11 22:10:06 +03:00
|
|
|
if (!stream->MainThreadNeedsUpdates()) {
|
2013-07-19 18:40:57 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
StreamUpdate* update = mStreamUpdates.AppendElement();
|
|
|
|
update->mStream = stream;
|
2015-09-08 08:12:01 +03:00
|
|
|
// No blocking to worry about here, since we've passed
|
|
|
|
// UpdateCurrentTimeForStreams.
|
2013-07-19 18:40:57 +04:00
|
|
|
update->mNextMainThreadCurrentTime =
|
2015-09-08 08:12:01 +03:00
|
|
|
stream->GraphTimeToStreamTime(mProcessedTime);
|
2013-12-09 09:08:02 +04:00
|
|
|
update->mNextMainThreadFinished = stream->mNotifiedFinished;
|
2013-07-19 18:40:57 +04:00
|
|
|
}
|
|
|
|
if (!mPendingUpdateRunnables.IsEmpty()) {
|
2015-08-11 18:29:46 +03:00
|
|
|
mUpdateRunnables.AppendElements(Move(mPendingUpdateRunnables));
|
2013-06-19 07:09:44 +04:00
|
|
|
}
|
2013-06-19 07:10:04 +04:00
|
|
|
}
|
2012-04-30 07:11:26 +04:00
|
|
|
|
2013-06-17 17:06:34 +04:00
|
|
|
// Don't send the message to the main thread if it's not going to have
|
|
|
|
// any work to do.
|
|
|
|
if (aFinalUpdate ||
|
|
|
|
!mUpdateRunnables.IsEmpty() ||
|
|
|
|
!mStreamUpdates.IsEmpty()) {
|
|
|
|
EnsureStableStateEventPosted();
|
|
|
|
}
|
2012-04-30 07:11:26 +04:00
|
|
|
}
|
|
|
|
|
2014-04-25 20:04:23 +04:00
|
|
|
GraphTime
|
|
|
|
MediaStreamGraphImpl::RoundUpToNextAudioBlock(GraphTime aTime)
|
2013-01-14 02:46:57 +04:00
|
|
|
{
|
2014-09-18 09:20:43 +04:00
|
|
|
StreamTime ticks = aTime;
|
2013-08-02 13:27:58 +04:00
|
|
|
uint64_t block = ticks >> WEBAUDIO_BLOCK_SIZE_BITS;
|
|
|
|
uint64_t nextBlock = block + 1;
|
2014-09-18 09:20:43 +04:00
|
|
|
StreamTime nextTicks = nextBlock << WEBAUDIO_BLOCK_SIZE_BITS;
|
2014-06-12 08:45:00 +04:00
|
|
|
return nextTicks;
|
2013-01-14 02:46:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStreamGraphImpl::ProduceDataForStreamsBlockByBlock(uint32_t aStreamIndex,
|
2015-09-16 07:24:10 +03:00
|
|
|
TrackRate aSampleRate)
|
2013-01-14 02:46:57 +04:00
|
|
|
{
|
2014-07-17 04:55:55 +04:00
|
|
|
MOZ_ASSERT(aStreamIndex <= mFirstCycleBreaker,
|
|
|
|
"Cycle breaker is not AudioNodeStream?");
|
2015-09-16 07:24:10 +03:00
|
|
|
GraphTime t = mProcessedTime;
|
|
|
|
while (t < mStateComputedTime) {
|
2014-04-25 20:04:23 +04:00
|
|
|
GraphTime next = RoundUpToNextAudioBlock(t);
|
2014-07-17 04:55:55 +04:00
|
|
|
for (uint32_t i = mFirstCycleBreaker; i < mStreams.Length(); ++i) {
|
|
|
|
auto ns = static_cast<AudioNodeStream*>(mStreams[i]);
|
|
|
|
MOZ_ASSERT(ns->AsAudioNodeStream());
|
|
|
|
ns->ProduceOutputBeforeInput(t);
|
|
|
|
}
|
2013-01-14 02:46:57 +04:00
|
|
|
for (uint32_t i = aStreamIndex; i < mStreams.Length(); ++i) {
|
2014-01-07 03:53:49 +04:00
|
|
|
ProcessedMediaStream* ps = mStreams[i]->AsProcessedStream();
|
2013-01-14 02:46:57 +04:00
|
|
|
if (ps) {
|
2015-09-16 07:24:10 +03:00
|
|
|
ps->ProcessInput(t, next,
|
|
|
|
(next == mStateComputedTime) ? ProcessedMediaStream::ALLOW_FINISH : 0);
|
2013-01-14 02:46:57 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
t = next;
|
|
|
|
}
|
2015-09-16 07:24:10 +03:00
|
|
|
NS_ASSERTION(t == mStateComputedTime,
|
|
|
|
"Something went wrong with rounding to block boundaries");
|
2013-01-14 02:46:57 +04:00
|
|
|
}
|
|
|
|
|
2013-12-09 09:08:02 +04:00
|
|
|
bool
|
|
|
|
MediaStreamGraphImpl::AllFinishedStreamsNotified()
|
|
|
|
{
|
2015-09-04 08:01:01 +03:00
|
|
|
for (MediaStream* stream : AllStreams()) {
|
|
|
|
if (stream->mFinished && !stream->mNotifiedFinished) {
|
2013-12-09 09:08:02 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-04-30 07:11:26 +04:00
|
|
|
void
|
2016-01-21 00:14:33 +03:00
|
|
|
MediaStreamGraphImpl::RunMessageAfterProcessing(UniquePtr<ControlMessage> aMessage)
|
2015-10-22 08:47:57 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(CurrentDriver()->OnThread());
|
|
|
|
|
|
|
|
if (mFrontMessageQueue.IsEmpty()) {
|
|
|
|
mFrontMessageQueue.AppendElement();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only one block is used for messages from the graph thread.
|
|
|
|
MOZ_ASSERT(mFrontMessageQueue.Length() == 1);
|
|
|
|
mFrontMessageQueue[0].mMessages.AppendElement(Move(aMessage));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStreamGraphImpl::RunMessagesInQueue()
|
2012-04-30 07:11:26 +04:00
|
|
|
{
|
2014-04-25 18:09:30 +04:00
|
|
|
// Calculate independent action times for each batch of messages (each
|
|
|
|
// batch corresponding to an event loop task). This isolates the performance
|
|
|
|
// of different scripts to some extent.
|
2014-08-25 17:26:21 +04:00
|
|
|
for (uint32_t i = 0; i < mFrontMessageQueue.Length(); ++i) {
|
2016-01-21 00:14:33 +03:00
|
|
|
nsTArray<UniquePtr<ControlMessage>>& messages = mFrontMessageQueue[i].mMessages;
|
2014-04-25 18:09:30 +04:00
|
|
|
|
|
|
|
for (uint32_t j = 0; j < messages.Length(); ++j) {
|
|
|
|
messages[j]->Run();
|
|
|
|
}
|
2012-04-30 07:11:26 +04:00
|
|
|
}
|
2014-08-25 17:26:21 +04:00
|
|
|
mFrontMessageQueue.Clear();
|
2015-10-22 08:47:57 +03:00
|
|
|
}
|
2012-04-30 07:11:26 +04:00
|
|
|
|
2015-10-22 08:47:57 +03:00
|
|
|
void
|
|
|
|
MediaStreamGraphImpl::UpdateGraph(GraphTime aEndBlockingDecisions)
|
|
|
|
{
|
2015-09-04 10:42:11 +03:00
|
|
|
MOZ_ASSERT(aEndBlockingDecisions >= mProcessedTime);
|
|
|
|
// The next state computed time can be the same as the previous: it
|
|
|
|
// means the driver would be have been blocking indefinitly, but the graph has
|
|
|
|
// been woken up right after having been to sleep.
|
|
|
|
MOZ_ASSERT(aEndBlockingDecisions >= mStateComputedTime);
|
|
|
|
|
2015-02-24 17:59:29 +03:00
|
|
|
UpdateStreamOrder();
|
2013-05-17 03:30:41 +04:00
|
|
|
|
2014-04-25 18:09:30 +04:00
|
|
|
bool ensureNextIteration = false;
|
|
|
|
|
2015-09-04 10:42:11 +03:00
|
|
|
// Grab pending stream input and compute blocking time
|
|
|
|
for (MediaStream* stream : mStreams) {
|
2015-09-08 16:23:31 +03:00
|
|
|
if (SourceMediaStream* is = stream->AsSourceStream()) {
|
2015-09-04 10:42:11 +03:00
|
|
|
ExtractPendingInput(is, aEndBlockingDecisions, &ensureNextIteration);
|
2014-04-25 18:09:30 +04:00
|
|
|
}
|
2015-09-04 10:42:11 +03:00
|
|
|
|
2015-09-08 16:23:31 +03:00
|
|
|
if (stream->mFinished) {
|
|
|
|
// The stream's not suspended, and since it's finished, underruns won't
|
|
|
|
// stop it playing out. So there's no blocking other than what we impose
|
|
|
|
// here.
|
2016-01-26 05:49:01 +03:00
|
|
|
GraphTime endTime = stream->GetStreamTracks().GetAllTracksEnd() +
|
|
|
|
stream->mTracksStartTime;
|
2015-09-08 16:23:31 +03:00
|
|
|
if (endTime <= mStateComputedTime) {
|
|
|
|
STREAM_LOG(LogLevel::Verbose, ("MediaStream %p is blocked due to being finished", stream));
|
|
|
|
stream->mStartBlocking = mStateComputedTime;
|
|
|
|
} else {
|
|
|
|
STREAM_LOG(LogLevel::Verbose, ("MediaStream %p is finished, but not blocked yet (end at %f, with blocking at %f)",
|
2016-01-26 05:49:01 +03:00
|
|
|
stream, MediaTimeToSeconds(stream->GetTracksEnd()),
|
2015-09-08 16:23:31 +03:00
|
|
|
MediaTimeToSeconds(endTime)));
|
|
|
|
// Data can't be added to a finished stream, so underruns are irrelevant.
|
|
|
|
stream->mStartBlocking = std::min(endTime, aEndBlockingDecisions);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
stream->mStartBlocking = WillUnderrun(stream, aEndBlockingDecisions);
|
|
|
|
}
|
2015-09-04 10:42:11 +03:00
|
|
|
}
|
2015-09-08 16:23:31 +03:00
|
|
|
|
2015-09-04 10:42:11 +03:00
|
|
|
for (MediaStream* stream : mSuspendedStreams) {
|
2015-09-08 16:23:31 +03:00
|
|
|
stream->mStartBlocking = mStateComputedTime;
|
2014-04-25 18:09:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// The loop is woken up so soon that IterationEnd() barely advances and we
|
2015-08-13 07:23:17 +03:00
|
|
|
// end up having aEndBlockingDecision == mStateComputedTime.
|
2014-04-25 18:09:30 +04:00
|
|
|
// Since stream blocking is computed in the interval of
|
2015-08-13 07:23:17 +03:00
|
|
|
// [mStateComputedTime, aEndBlockingDecision), it won't be computed at all.
|
2014-04-25 18:09:30 +04:00
|
|
|
// We should ensure next iteration so that pending blocking changes will be
|
|
|
|
// computed in next loop.
|
2014-04-25 20:04:23 +04:00
|
|
|
if (ensureNextIteration ||
|
2015-09-04 10:42:11 +03:00
|
|
|
aEndBlockingDecisions == mStateComputedTime) {
|
2014-09-28 20:07:25 +04:00
|
|
|
EnsureNextIteration();
|
2014-04-25 18:09:30 +04:00
|
|
|
}
|
2014-04-25 20:04:23 +04:00
|
|
|
}
|
2014-04-25 18:09:30 +04:00
|
|
|
|
2014-04-25 20:04:23 +04:00
|
|
|
void
|
2015-09-08 07:58:19 +03:00
|
|
|
MediaStreamGraphImpl::Process()
|
2014-04-25 20:04:23 +04:00
|
|
|
{
|
2014-04-25 18:09:30 +04:00
|
|
|
// Play stream contents.
|
|
|
|
bool allBlockedForever = true;
|
|
|
|
// True when we've done ProcessInput for all processed streams.
|
|
|
|
bool doneAllProducing = false;
|
|
|
|
// This is the number of frame that are written to the AudioStreams, for
|
|
|
|
// this cycle.
|
2014-09-18 09:20:43 +04:00
|
|
|
StreamTime ticksPlayed = 0;
|
2014-08-25 17:25:49 +04:00
|
|
|
|
|
|
|
mMixer.StartMixing();
|
|
|
|
|
2014-04-25 18:09:30 +04:00
|
|
|
// Figure out what each stream wants to do
|
|
|
|
for (uint32_t i = 0; i < mStreams.Length(); ++i) {
|
|
|
|
MediaStream* stream = mStreams[i];
|
|
|
|
if (!doneAllProducing) {
|
|
|
|
ProcessedMediaStream* ps = stream->AsProcessedStream();
|
|
|
|
if (ps) {
|
|
|
|
AudioNodeStream* n = stream->AsAudioNodeStream();
|
|
|
|
if (n) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
// Verify that the sampling rate for all of the following streams is the same
|
|
|
|
for (uint32_t j = i + 1; j < mStreams.Length(); ++j) {
|
|
|
|
AudioNodeStream* nextStream = mStreams[j]->AsAudioNodeStream();
|
|
|
|
if (nextStream) {
|
|
|
|
MOZ_ASSERT(n->SampleRate() == nextStream->SampleRate(),
|
|
|
|
"All AudioNodeStreams in the graph must have the same sampling rate");
|
|
|
|
}
|
2014-04-13 22:08:10 +04:00
|
|
|
}
|
2014-04-25 18:09:30 +04:00
|
|
|
#endif
|
|
|
|
// Since an AudioNodeStream is present, go ahead and
|
|
|
|
// produce audio block by block for all the rest of the streams.
|
2015-09-16 07:24:10 +03:00
|
|
|
ProduceDataForStreamsBlockByBlock(i, n->SampleRate());
|
2014-04-25 18:09:30 +04:00
|
|
|
doneAllProducing = true;
|
|
|
|
} else {
|
2015-09-08 07:58:19 +03:00
|
|
|
ps->ProcessInput(mProcessedTime, mStateComputedTime,
|
|
|
|
ProcessedMediaStream::ALLOW_FINISH);
|
2016-04-21 12:44:35 +03:00
|
|
|
NS_ASSERTION(stream->mTracks.GetEnd() >=
|
|
|
|
GraphTimeToStreamTimeWithBlocking(stream, mStateComputedTime),
|
|
|
|
"Stream did not produce enough data");
|
2014-04-13 22:08:10 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-04-25 18:09:30 +04:00
|
|
|
NotifyHasCurrentData(stream);
|
2014-08-25 17:25:49 +04:00
|
|
|
// Only playback audio and video in real-time mode
|
2014-04-25 18:09:30 +04:00
|
|
|
if (mRealtime) {
|
2015-09-16 07:23:14 +03:00
|
|
|
CreateOrDestroyAudioStreams(stream);
|
2014-08-27 21:13:15 +04:00
|
|
|
if (CurrentDriver()->AsAudioCallbackDriver()) {
|
2015-09-08 07:58:19 +03:00
|
|
|
StreamTime ticksPlayedForThisStream = PlayAudio(stream);
|
2014-08-27 21:13:15 +04:00
|
|
|
if (!ticksPlayed) {
|
|
|
|
ticksPlayed = ticksPlayedForThisStream;
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(!ticksPlayedForThisStream || ticksPlayedForThisStream == ticksPlayed,
|
|
|
|
"Each stream should have the same number of frame.");
|
|
|
|
}
|
2012-04-30 07:11:26 +04:00
|
|
|
}
|
2014-04-25 18:09:30 +04:00
|
|
|
PlayVideo(stream);
|
2012-04-30 07:11:26 +04:00
|
|
|
}
|
2015-09-16 07:23:14 +03:00
|
|
|
if (stream->mStartBlocking > mProcessedTime) {
|
2014-04-25 18:09:30 +04:00
|
|
|
allBlockedForever = false;
|
2012-04-30 07:11:40 +04:00
|
|
|
}
|
2014-04-25 18:09:30 +04:00
|
|
|
}
|
2012-04-30 07:11:40 +04:00
|
|
|
|
2014-08-26 19:01:33 +04:00
|
|
|
if (CurrentDriver()->AsAudioCallbackDriver() && ticksPlayed) {
|
2014-08-25 17:25:49 +04:00
|
|
|
mMixer.FinishMixing();
|
2014-04-25 18:09:30 +04:00
|
|
|
}
|
2014-03-05 03:15:41 +04:00
|
|
|
|
2014-04-25 20:04:23 +04:00
|
|
|
if (!allBlockedForever) {
|
2014-09-28 20:07:25 +04:00
|
|
|
EnsureNextIteration();
|
2014-04-25 18:09:30 +04:00
|
|
|
}
|
|
|
|
}
|
2012-04-30 07:11:26 +04:00
|
|
|
|
2015-09-04 15:26:48 +03:00
|
|
|
void
|
|
|
|
MediaStreamGraphImpl::MaybeProduceMemoryReport()
|
2014-04-25 18:09:30 +04:00
|
|
|
{
|
2015-09-04 15:26:48 +03:00
|
|
|
MonitorAutoLock lock(mMemoryReportMonitor);
|
|
|
|
if (mNeedsMemoryReport) {
|
|
|
|
mNeedsMemoryReport = false;
|
|
|
|
|
|
|
|
for (MediaStream* s : AllStreams()) {
|
|
|
|
AudioNodeStream* stream = s->AsAudioNodeStream();
|
|
|
|
if (stream) {
|
|
|
|
AudioNodeSizes usage;
|
|
|
|
stream->SizeOfAudioNodesIncludingThis(MallocSizeOf, usage);
|
|
|
|
mAudioStreamSizes.AppendElement(usage);
|
2014-08-26 19:01:33 +04:00
|
|
|
}
|
|
|
|
}
|
2015-09-04 15:26:48 +03:00
|
|
|
|
|
|
|
lock.Notify();
|
2014-08-26 19:01:33 +04:00
|
|
|
}
|
2015-09-04 15:26:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
MediaStreamGraphImpl::UpdateMainThreadState()
|
|
|
|
{
|
|
|
|
MonitorAutoLock lock(mMonitor);
|
|
|
|
bool finalUpdate = mForceShutDown ||
|
|
|
|
(mProcessedTime >= mEndTime && AllFinishedStreamsNotified()) ||
|
|
|
|
(IsEmpty() && mBackMessageQueue.IsEmpty());
|
|
|
|
PrepareUpdatesToMainThreadState(finalUpdate);
|
|
|
|
if (finalUpdate) {
|
|
|
|
// Enter shutdown mode. The stable-state handler will detect this
|
|
|
|
// and complete shutdown. Destroy any streams immediately.
|
|
|
|
STREAM_LOG(LogLevel::Debug, ("MediaStreamGraph %p waiting for main thread cleanup", this));
|
|
|
|
// We'll shut down this graph object if it does not get restarted.
|
|
|
|
mLifecycleState = LIFECYCLE_WAITING_FOR_MAIN_THREAD_CLEANUP;
|
|
|
|
// No need to Destroy streams here. The main-thread owner of each
|
|
|
|
// stream is responsible for calling Destroy on them.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CurrentDriver()->WaitForNextIteration();
|
|
|
|
|
|
|
|
SwapMessageQueues();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
MediaStreamGraphImpl::OneIteration(GraphTime aStateEnd)
|
|
|
|
{
|
2015-10-22 08:47:57 +03:00
|
|
|
// Process graph message from the main thread for this iteration.
|
|
|
|
RunMessagesInQueue();
|
|
|
|
|
2015-11-13 08:58:16 +03:00
|
|
|
MaybeProduceMemoryReport();
|
|
|
|
|
2015-07-31 14:43:55 +03:00
|
|
|
GraphTime stateEnd = std::min(aStateEnd, mEndTime);
|
|
|
|
UpdateGraph(stateEnd);
|
2014-08-25 16:13:08 +04:00
|
|
|
|
2015-09-04 15:41:15 +03:00
|
|
|
mStateComputedTime = stateEnd;
|
|
|
|
|
2015-09-08 07:58:19 +03:00
|
|
|
Process();
|
2015-09-04 15:41:15 +03:00
|
|
|
|
2015-09-16 07:23:14 +03:00
|
|
|
GraphTime oldProcessedTime = mProcessedTime;
|
2015-07-31 12:28:29 +03:00
|
|
|
mProcessedTime = stateEnd;
|
2014-08-25 16:13:08 +04:00
|
|
|
|
2015-09-16 07:23:14 +03:00
|
|
|
UpdateCurrentTimeForStreams(oldProcessedTime);
|
2015-07-29 10:32:10 +03:00
|
|
|
|
2016-01-26 11:45:25 +03:00
|
|
|
ProcessChunkMetadata(oldProcessedTime);
|
|
|
|
|
2015-10-22 08:47:57 +03:00
|
|
|
// Process graph messages queued from RunMessageAfterProcessing() on this
|
|
|
|
// thread during the iteration.
|
|
|
|
RunMessagesInQueue();
|
|
|
|
|
2015-09-04 15:26:48 +03:00
|
|
|
return UpdateMainThreadState();
|
2014-04-25 18:09:30 +04:00
|
|
|
}
|
2012-04-30 07:11:26 +04:00
|
|
|
|
|
|
|
void
|
|
|
|
MediaStreamGraphImpl::ApplyStreamUpdate(StreamUpdate* aUpdate)
|
|
|
|
{
|
2014-08-26 19:01:33 +04:00
|
|
|
mMonitor.AssertCurrentThreadOwns();
|
2012-04-30 07:11:26 +04:00
|
|
|
|
|
|
|
MediaStream* stream = aUpdate->mStream;
|
2015-06-11 22:10:06 +03:00
|
|
|
if (!stream)
|
2012-04-30 07:11:26 +04:00
|
|
|
return;
|
|
|
|
stream->mMainThreadCurrentTime = aUpdate->mNextMainThreadCurrentTime;
|
|
|
|
stream->mMainThreadFinished = aUpdate->mNextMainThreadFinished;
|
2012-08-20 08:20:44 +04:00
|
|
|
|
2015-05-11 17:07:24 +03:00
|
|
|
if (stream->ShouldNotifyStreamFinished()) {
|
|
|
|
if (stream->mWrapper) {
|
|
|
|
stream->mWrapper->NotifyStreamFinished();
|
|
|
|
}
|
|
|
|
|
2015-05-11 17:07:38 +03:00
|
|
|
stream->NotifyMainThreadListeners();
|
2012-08-20 08:20:44 +04:00
|
|
|
}
|
2012-04-30 07:11:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-01-22 21:49:54 +03:00
|
|
|
MediaStreamGraphImpl::ForceShutDown(ShutdownTicket* aShutdownTicket)
|
2012-04-30 07:11:26 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Must be called on main thread");
|
2015-06-04 01:25:57 +03:00
|
|
|
STREAM_LOG(LogLevel::Debug, ("MediaStreamGraph %p ForceShutdown", this));
|
2012-04-30 07:11:26 +04:00
|
|
|
{
|
2014-08-26 19:01:33 +04:00
|
|
|
MonitorAutoLock lock(mMonitor);
|
2012-04-30 07:11:26 +04:00
|
|
|
mForceShutDown = true;
|
2016-01-22 21:49:54 +03:00
|
|
|
mForceShutdownTicket = aShutdownTicket;
|
2016-03-15 18:46:01 +03:00
|
|
|
if (mLifecycleState == LIFECYCLE_THREAD_NOT_STARTED) {
|
|
|
|
// We *could* have just sent this a message to start up, so don't
|
|
|
|
// yank the rug out from under it. Tell it to startup and let it
|
|
|
|
// shut down.
|
|
|
|
RefPtr<GraphDriver> driver = CurrentDriver();
|
|
|
|
MonitorAutoUnlock unlock(mMonitor);
|
|
|
|
driver->Start();
|
|
|
|
}
|
2014-09-28 20:07:25 +04:00
|
|
|
EnsureNextIterationLocked();
|
2012-04-30 07:11:26 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-22 21:49:54 +03:00
|
|
|
/* static */ StaticRefPtr<nsIAsyncShutdownBlocker> gMediaStreamGraphShutdownBlocker;
|
|
|
|
|
2012-04-30 07:11:26 +04:00
|
|
|
namespace {
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class MediaStreamGraphShutDownRunnable : public Runnable {
|
2012-04-30 07:11:26 +04:00
|
|
|
public:
|
2014-09-01 07:50:23 +04:00
|
|
|
explicit MediaStreamGraphShutDownRunnable(MediaStreamGraphImpl* aGraph)
|
2014-08-26 19:01:33 +04:00
|
|
|
: mGraph(aGraph)
|
2014-04-25 20:03:04 +04:00
|
|
|
{}
|
2012-04-30 07:11:26 +04:00
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mGraph->mDetectedNotRunning,
|
|
|
|
"We should know the graph thread control loop isn't running!");
|
2013-05-03 09:02:55 +04:00
|
|
|
|
2014-08-31 16:19:48 +04:00
|
|
|
LIFECYCLE_LOG("Shutting down graph %p", mGraph.get());
|
2014-08-26 19:04:38 +04:00
|
|
|
|
2014-09-28 20:07:24 +04:00
|
|
|
// We've asserted the graph isn't running. Use mDriver instead of CurrentDriver
|
|
|
|
// to avoid thread-safety checks
|
|
|
|
#if 0 // AudioCallbackDrivers are released asynchronously anyways
|
|
|
|
// XXX a better test would be have setting mDetectedNotRunning make sure
|
|
|
|
// any current callback has finished and block future ones -- or just
|
|
|
|
// handle it all in Shutdown()!
|
|
|
|
if (mGraph->mDriver->AsAudioCallbackDriver()) {
|
|
|
|
MOZ_ASSERT(!mGraph->mDriver->AsAudioCallbackDriver()->InCallback());
|
2014-08-26 19:01:35 +04:00
|
|
|
}
|
2014-09-28 20:07:24 +04:00
|
|
|
#endif
|
2014-08-26 19:01:35 +04:00
|
|
|
|
2016-01-22 10:39:42 +03:00
|
|
|
mGraph->mDriver->Shutdown(); // This will wait until it's shutdown since
|
|
|
|
// we'll start tearing down the graph after this
|
2013-05-08 09:16:35 +04:00
|
|
|
|
2016-01-22 21:49:54 +03:00
|
|
|
// We may be one of several graphs. Drop ticket to eventually unblock shutdown.
|
|
|
|
mGraph->mForceShutdownTicket = nullptr;
|
|
|
|
|
|
|
|
// We can't block past the final LIFECYCLE_WAITING_FOR_STREAM_DESTRUCTION
|
|
|
|
// stage, since completion of that stage requires all streams to be freed,
|
|
|
|
// which requires shutdown to proceed.
|
|
|
|
|
2012-04-30 07:11:26 +04:00
|
|
|
// mGraph's thread is not running so it's OK to do whatever here
|
|
|
|
if (mGraph->IsEmpty()) {
|
2014-02-11 04:04:58 +04:00
|
|
|
// mGraph is no longer needed, so delete it.
|
2014-04-13 22:08:10 +04:00
|
|
|
mGraph->Destroy();
|
2012-04-30 07:11:26 +04:00
|
|
|
} else {
|
2014-02-11 04:04:58 +04:00
|
|
|
// The graph is not empty. We must be in a forced shutdown, or a
|
|
|
|
// non-realtime graph that has finished processing. Some later
|
|
|
|
// AppendMessage will detect that the manager has been emptied, and
|
|
|
|
// delete it.
|
|
|
|
NS_ASSERTION(mGraph->mForceShutDown || !mGraph->mRealtime,
|
|
|
|
"Not in forced shutdown?");
|
2015-09-04 08:01:01 +03:00
|
|
|
for (MediaStream* stream : mGraph->AllStreams()) {
|
|
|
|
DOMMediaStream* s = stream->GetWrapper();
|
2013-05-03 09:02:55 +04:00
|
|
|
if (s) {
|
|
|
|
s->NotifyMediaStreamGraphShutdown();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-30 07:11:26 +04:00
|
|
|
mGraph->mLifecycleState =
|
|
|
|
MediaStreamGraphImpl::LIFECYCLE_WAITING_FOR_STREAM_DESTRUCTION;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
private:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaStreamGraphImpl> mGraph;
|
2012-04-30 07:11:26 +04:00
|
|
|
};
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class MediaStreamGraphStableStateRunnable : public Runnable {
|
2012-04-30 07:11:26 +04:00
|
|
|
public:
|
2014-08-25 16:13:14 +04:00
|
|
|
explicit MediaStreamGraphStableStateRunnable(MediaStreamGraphImpl* aGraph,
|
|
|
|
bool aSourceIsMSG)
|
2013-02-02 00:20:32 +04:00
|
|
|
: mGraph(aGraph)
|
2014-08-25 16:13:14 +04:00
|
|
|
, mSourceIsMSG(aSourceIsMSG)
|
2013-02-02 00:20:32 +04:00
|
|
|
{
|
|
|
|
}
|
2012-04-30 07:11:26 +04:00
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
2013-02-02 00:20:32 +04:00
|
|
|
if (mGraph) {
|
2014-08-25 16:13:14 +04:00
|
|
|
mGraph->RunInStableState(mSourceIsMSG);
|
2012-04-30 07:11:26 +04:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-02-02 00:20:32 +04:00
|
|
|
private:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaStreamGraphImpl> mGraph;
|
2014-08-25 16:13:14 +04:00
|
|
|
bool mSourceIsMSG;
|
2012-04-30 07:11:26 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Control messages forwarded from main thread to graph manager thread
|
|
|
|
*/
|
|
|
|
class CreateMessage : public ControlMessage {
|
|
|
|
public:
|
2014-09-01 07:50:23 +04:00
|
|
|
explicit CreateMessage(MediaStream* aStream) : ControlMessage(aStream) {}
|
2016-01-18 06:50:29 +03:00
|
|
|
void Run() override
|
2012-04-30 07:11:26 +04:00
|
|
|
{
|
2015-08-12 02:29:35 +03:00
|
|
|
mStream->GraphImpl()->AddStreamGraphThread(mStream);
|
2012-04-30 07:11:26 +04:00
|
|
|
}
|
2016-01-18 06:50:29 +03:00
|
|
|
void RunDuringShutdown() override
|
2013-05-29 19:38:39 +04:00
|
|
|
{
|
|
|
|
// Make sure to run this message during shutdown too, to make sure
|
|
|
|
// that we balance the number of streams registered with the graph
|
|
|
|
// as they're destroyed during shutdown.
|
|
|
|
Run();
|
|
|
|
}
|
2012-04-30 07:11:26 +04:00
|
|
|
};
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2012-04-30 07:11:26 +04:00
|
|
|
|
|
|
|
void
|
2014-08-25 16:13:14 +04:00
|
|
|
MediaStreamGraphImpl::RunInStableState(bool aSourceIsMSG)
|
2012-04-30 07:11:26 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Must be called on main thread");
|
|
|
|
|
|
|
|
nsTArray<nsCOMPtr<nsIRunnable> > runnables;
|
2012-07-31 16:17:22 +04:00
|
|
|
// When we're doing a forced shutdown, pending control messages may be
|
|
|
|
// run on the main thread via RunDuringShutdown. Those messages must
|
|
|
|
// run without the graph monitor being held. So, we collect them here.
|
2016-01-21 00:14:33 +03:00
|
|
|
nsTArray<UniquePtr<ControlMessage>> controlMessagesToRunDuringShutdown;
|
2012-04-30 07:11:26 +04:00
|
|
|
|
|
|
|
{
|
2014-08-26 19:01:33 +04:00
|
|
|
MonitorAutoLock lock(mMonitor);
|
2014-08-25 16:13:14 +04:00
|
|
|
if (aSourceIsMSG) {
|
|
|
|
MOZ_ASSERT(mPostedRunInStableStateEvent);
|
|
|
|
mPostedRunInStableStateEvent = false;
|
|
|
|
}
|
2012-04-30 07:11:26 +04:00
|
|
|
|
2014-08-31 16:19:48 +04:00
|
|
|
#ifdef ENABLE_LIFECYCLE_LOG
|
|
|
|
// This should be kept in sync with the LifecycleState enum in
|
|
|
|
// MediaStreamGraphImpl.h
|
|
|
|
const char * LifecycleState_str[] = {
|
|
|
|
"LIFECYCLE_THREAD_NOT_STARTED",
|
|
|
|
"LIFECYCLE_RUNNING",
|
|
|
|
"LIFECYCLE_WAITING_FOR_MAIN_THREAD_CLEANUP",
|
|
|
|
"LIFECYCLE_WAITING_FOR_THREAD_SHUTDOWN",
|
|
|
|
"LIFECYCLE_WAITING_FOR_STREAM_DESTRUCTION"
|
|
|
|
};
|
|
|
|
|
|
|
|
if (mLifecycleState != LIFECYCLE_RUNNING) {
|
|
|
|
LIFECYCLE_LOG("Running %p in stable state. Current state: %s\n",
|
|
|
|
this, LifecycleState_str[mLifecycleState]);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-04-30 07:11:26 +04:00
|
|
|
runnables.SwapElements(mUpdateRunnables);
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < mStreamUpdates.Length(); ++i) {
|
2012-04-30 07:11:26 +04:00
|
|
|
StreamUpdate* update = &mStreamUpdates[i];
|
|
|
|
if (update->mStream) {
|
|
|
|
ApplyStreamUpdate(update);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mStreamUpdates.Clear();
|
|
|
|
|
|
|
|
if (mCurrentTaskMessageQueue.IsEmpty()) {
|
|
|
|
if (mLifecycleState == LIFECYCLE_WAITING_FOR_MAIN_THREAD_CLEANUP && IsEmpty()) {
|
|
|
|
// Complete shutdown. First, ensure that this graph is no longer used.
|
|
|
|
// A new graph graph will be created if one is needed.
|
|
|
|
// Asynchronously clean up old graph. We don't want to do this
|
|
|
|
// synchronously because it spins the event loop waiting for threads
|
|
|
|
// to shut down, and we don't want to do that in a stable state handler.
|
|
|
|
mLifecycleState = LIFECYCLE_WAITING_FOR_THREAD_SHUTDOWN;
|
2014-08-31 16:19:48 +04:00
|
|
|
LIFECYCLE_LOG("Sending MediaStreamGraphShutDownRunnable %p", this);
|
2014-08-26 19:01:33 +04:00
|
|
|
nsCOMPtr<nsIRunnable> event = new MediaStreamGraphShutDownRunnable(this );
|
2015-08-20 00:52:56 +03:00
|
|
|
NS_DispatchToMainThread(event.forget());
|
2014-09-03 17:52:43 +04:00
|
|
|
|
|
|
|
LIFECYCLE_LOG("Disconnecting MediaStreamGraph %p", this);
|
2014-11-17 19:07:55 +03:00
|
|
|
MediaStreamGraphImpl* graph;
|
2015-10-23 06:43:15 +03:00
|
|
|
if (gGraphs.Get(uint32_t(mAudioChannel), &graph) && graph == this) {
|
2014-09-03 17:52:43 +04:00
|
|
|
// null out gGraph if that's the graph being shut down
|
2015-10-23 06:43:15 +03:00
|
|
|
gGraphs.Remove(uint32_t(mAudioChannel));
|
2014-09-03 17:52:43 +04:00
|
|
|
}
|
2012-04-30 07:11:26 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (mLifecycleState <= LIFECYCLE_WAITING_FOR_MAIN_THREAD_CLEANUP) {
|
2014-08-25 17:26:21 +04:00
|
|
|
MessageBlock* block = mBackMessageQueue.AppendElement();
|
2012-04-30 07:11:26 +04:00
|
|
|
block->mMessages.SwapElements(mCurrentTaskMessageQueue);
|
2014-09-28 20:07:25 +04:00
|
|
|
EnsureNextIterationLocked();
|
2012-04-30 07:11:26 +04:00
|
|
|
}
|
|
|
|
|
2014-01-07 00:09:29 +04:00
|
|
|
// If the MediaStreamGraph has more messages going to it, try to revive
|
|
|
|
// it to process those messages. Don't do this if we're in a forced
|
|
|
|
// shutdown or it's a non-realtime graph that has already terminated
|
|
|
|
// processing.
|
|
|
|
if (mLifecycleState == LIFECYCLE_WAITING_FOR_MAIN_THREAD_CLEANUP &&
|
|
|
|
mRealtime && !mForceShutDown) {
|
2012-04-30 07:11:26 +04:00
|
|
|
mLifecycleState = LIFECYCLE_RUNNING;
|
|
|
|
// Revive the MediaStreamGraph since we have more messages going to it.
|
|
|
|
// Note that we need to put messages into its queue before reviving it,
|
|
|
|
// or it might exit immediately.
|
2014-08-26 19:01:33 +04:00
|
|
|
{
|
2014-08-31 16:19:48 +04:00
|
|
|
LIFECYCLE_LOG("Reviving a graph (%p) ! %s\n",
|
|
|
|
this, CurrentDriver()->AsAudioCallbackDriver() ? "AudioDriver" :
|
|
|
|
"SystemDriver");
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<GraphDriver> driver = CurrentDriver();
|
2014-09-28 20:07:24 +04:00
|
|
|
MonitorAutoUnlock unlock(mMonitor);
|
|
|
|
driver->Revive();
|
2014-08-26 19:01:33 +04:00
|
|
|
}
|
2012-04-30 07:11:26 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-26 19:02:09 +04:00
|
|
|
// Don't start the thread for a non-realtime graph until it has been
|
|
|
|
// explicitly started by StartNonRealtimeProcessing.
|
|
|
|
if (mLifecycleState == LIFECYCLE_THREAD_NOT_STARTED &&
|
|
|
|
(mRealtime || mNonRealtimeProcessing)) {
|
|
|
|
mLifecycleState = LIFECYCLE_RUNNING;
|
|
|
|
// Start the thread now. We couldn't start it earlier because
|
|
|
|
// the graph might exit immediately on finding it has no streams. The
|
|
|
|
// first message for a new graph must create a stream.
|
|
|
|
{
|
|
|
|
// We should exit the monitor for now, because starting a stream might
|
|
|
|
// take locks, and we don't want to deadlock.
|
2014-08-31 16:19:48 +04:00
|
|
|
LIFECYCLE_LOG("Starting a graph (%p) ! %s\n",
|
|
|
|
this,
|
|
|
|
CurrentDriver()->AsAudioCallbackDriver() ? "AudioDriver" :
|
|
|
|
"SystemDriver");
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<GraphDriver> driver = CurrentDriver();
|
2014-09-28 20:07:24 +04:00
|
|
|
MonitorAutoUnlock unlock(mMonitor);
|
|
|
|
driver->Start();
|
2014-08-26 19:02:09 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-11 04:04:58 +04:00
|
|
|
if ((mForceShutDown || !mRealtime) &&
|
2014-01-30 09:50:17 +04:00
|
|
|
mLifecycleState == LIFECYCLE_WAITING_FOR_MAIN_THREAD_CLEANUP) {
|
2014-01-07 00:09:29 +04:00
|
|
|
// Defer calls to RunDuringShutdown() to happen while mMonitor is not held.
|
2014-08-25 17:26:21 +04:00
|
|
|
for (uint32_t i = 0; i < mBackMessageQueue.Length(); ++i) {
|
|
|
|
MessageBlock& mb = mBackMessageQueue[i];
|
2015-08-11 18:29:46 +03:00
|
|
|
controlMessagesToRunDuringShutdown.AppendElements(Move(mb.mMessages));
|
2014-01-07 00:09:29 +04:00
|
|
|
}
|
2014-08-25 17:26:21 +04:00
|
|
|
mBackMessageQueue.Clear();
|
2014-01-08 07:58:14 +04:00
|
|
|
MOZ_ASSERT(mCurrentTaskMessageQueue.IsEmpty());
|
2014-01-07 00:09:29 +04:00
|
|
|
// Stop MediaStreamGraph threads. Do not clear gGraph since
|
|
|
|
// we have outstanding DOM objects that may need it.
|
|
|
|
mLifecycleState = LIFECYCLE_WAITING_FOR_THREAD_SHUTDOWN;
|
2014-08-26 19:01:33 +04:00
|
|
|
nsCOMPtr<nsIRunnable> event = new MediaStreamGraphShutDownRunnable(this);
|
2015-08-20 00:52:56 +03:00
|
|
|
NS_DispatchToMainThread(event.forget());
|
2014-01-07 00:09:29 +04:00
|
|
|
}
|
|
|
|
|
2012-04-30 07:11:26 +04:00
|
|
|
mDetectedNotRunning = mLifecycleState > LIFECYCLE_RUNNING;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we get a new current time in the next event loop task
|
2014-08-25 16:13:14 +04:00
|
|
|
if (!aSourceIsMSG) {
|
|
|
|
MOZ_ASSERT(mPostedRunInStableState);
|
|
|
|
mPostedRunInStableState = false;
|
|
|
|
}
|
2012-04-30 07:11:26 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < controlMessagesToRunDuringShutdown.Length(); ++i) {
|
2012-07-31 16:17:22 +04:00
|
|
|
controlMessagesToRunDuringShutdown[i]->RunDuringShutdown();
|
|
|
|
}
|
2014-07-02 10:04:54 +04:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
mCanRunMessagesSynchronously = mDetectedNotRunning &&
|
|
|
|
mLifecycleState >= LIFECYCLE_WAITING_FOR_THREAD_SHUTDOWN;
|
|
|
|
#endif
|
2015-01-08 08:21:00 +03:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < runnables.Length(); ++i) {
|
|
|
|
runnables[i]->Run();
|
2015-04-16 22:24:54 +03:00
|
|
|
// "Direct" tail dispatcher are supposed to run immediately following the
|
|
|
|
// execution of the current task. So the meta-tasking that we do here in
|
|
|
|
// RunInStableState() breaks that abstraction a bit unless we handle it here.
|
|
|
|
//
|
|
|
|
// This is particularly important because we can end up with a "stream
|
|
|
|
// ended" notification immediately following a "stream available" notification,
|
|
|
|
// and we need to make sure that the watcher responding to "stream available"
|
|
|
|
// has a chance to run before the second notification starts tearing things
|
|
|
|
// down.
|
2015-10-03 01:42:15 +03:00
|
|
|
AbstractThread::MainThread()->TailDispatcher().DrainDirectTasks();
|
2015-01-08 08:21:00 +03:00
|
|
|
}
|
2012-04-30 07:11:26 +04:00
|
|
|
}
|
|
|
|
|
2014-09-28 20:07:24 +04:00
|
|
|
|
2012-04-30 07:11:26 +04:00
|
|
|
void
|
|
|
|
MediaStreamGraphImpl::EnsureRunInStableState()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "main thread only");
|
|
|
|
|
|
|
|
if (mPostedRunInStableState)
|
|
|
|
return;
|
|
|
|
mPostedRunInStableState = true;
|
2014-08-25 16:13:14 +04:00
|
|
|
nsCOMPtr<nsIRunnable> event = new MediaStreamGraphStableStateRunnable(this, false);
|
2015-06-11 05:36:12 +03:00
|
|
|
nsContentUtils::RunInStableState(event.forget());
|
2012-04-30 07:11:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStreamGraphImpl::EnsureStableStateEventPosted()
|
|
|
|
{
|
2014-08-26 19:01:33 +04:00
|
|
|
mMonitor.AssertCurrentThreadOwns();
|
2012-04-30 07:11:26 +04:00
|
|
|
|
|
|
|
if (mPostedRunInStableStateEvent)
|
|
|
|
return;
|
|
|
|
mPostedRunInStableStateEvent = true;
|
2014-08-25 16:13:14 +04:00
|
|
|
nsCOMPtr<nsIRunnable> event = new MediaStreamGraphStableStateRunnable(this, true);
|
2015-08-20 00:52:56 +03:00
|
|
|
NS_DispatchToMainThread(event.forget());
|
2012-04-30 07:11:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-01-21 00:14:33 +03:00
|
|
|
MediaStreamGraphImpl::AppendMessage(UniquePtr<ControlMessage> aMessage)
|
2012-04-30 07:11:26 +04:00
|
|
|
{
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "main thread only");
|
2015-04-29 12:02:55 +03:00
|
|
|
MOZ_ASSERT(!aMessage->GetStream() ||
|
|
|
|
!aMessage->GetStream()->IsDestroyed(),
|
|
|
|
"Stream already destroyed");
|
2012-04-30 07:11:26 +04:00
|
|
|
|
|
|
|
if (mDetectedNotRunning &&
|
|
|
|
mLifecycleState > LIFECYCLE_WAITING_FOR_MAIN_THREAD_CLEANUP) {
|
|
|
|
// The graph control loop is not running and main thread cleanup has
|
|
|
|
// happened. From now on we can't append messages to mCurrentTaskMessageQueue,
|
2012-07-31 16:17:21 +04:00
|
|
|
// because that will never be processed again, so just RunDuringShutdown
|
2012-04-30 07:11:26 +04:00
|
|
|
// this message.
|
2014-02-11 04:04:58 +04:00
|
|
|
// This should only happen during forced shutdown, or after a non-realtime
|
|
|
|
// graph has finished processing.
|
2014-07-02 10:04:54 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
MOZ_ASSERT(mCanRunMessagesSynchronously);
|
|
|
|
mCanRunMessagesSynchronously = false;
|
|
|
|
#endif
|
2012-07-31 16:17:21 +04:00
|
|
|
aMessage->RunDuringShutdown();
|
2014-07-02 10:04:54 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
mCanRunMessagesSynchronously = true;
|
|
|
|
#endif
|
2013-05-31 04:53:51 +04:00
|
|
|
if (IsEmpty() &&
|
|
|
|
mLifecycleState >= LIFECYCLE_WAITING_FOR_STREAM_DESTRUCTION) {
|
2014-11-17 19:07:55 +03:00
|
|
|
|
|
|
|
MediaStreamGraphImpl* graph;
|
2015-10-23 06:43:15 +03:00
|
|
|
if (gGraphs.Get(uint32_t(mAudioChannel), &graph) && graph == this) {
|
|
|
|
gGraphs.Remove(uint32_t(mAudioChannel));
|
2013-02-04 21:29:14 +04:00
|
|
|
}
|
2014-11-17 19:07:55 +03:00
|
|
|
|
2014-04-13 22:08:10 +04:00
|
|
|
Destroy();
|
2012-04-30 07:11:26 +04:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-21 00:14:33 +03:00
|
|
|
mCurrentTaskMessageQueue.AppendElement(Move(aMessage));
|
2014-01-07 00:09:29 +04:00
|
|
|
EnsureRunInStableState();
|
2012-04-30 07:11:26 +04:00
|
|
|
}
|
|
|
|
|
2013-08-07 02:14:35 +04:00
|
|
|
MediaStream::MediaStream(DOMMediaStream* aWrapper)
|
2016-01-26 05:49:01 +03:00
|
|
|
: mTracksStartTime(0)
|
2015-09-04 09:44:43 +03:00
|
|
|
, mStartBlocking(GRAPH_TIME_MAX)
|
2015-09-16 07:15:21 +03:00
|
|
|
, mSuspendedCount(0)
|
2013-08-07 02:14:35 +04:00
|
|
|
, mFinished(false)
|
|
|
|
, mNotifiedFinished(false)
|
|
|
|
, mNotifiedBlocked(false)
|
|
|
|
, mHasCurrentData(false)
|
|
|
|
, mNotifiedHasCurrentData(false)
|
|
|
|
, mWrapper(aWrapper)
|
|
|
|
, mMainThreadCurrentTime(0)
|
|
|
|
, mMainThreadFinished(false)
|
2015-05-11 17:07:24 +03:00
|
|
|
, mFinishedNotificationSent(false)
|
2013-08-07 02:14:35 +04:00
|
|
|
, mMainThreadDestroyed(false)
|
2016-01-05 05:16:22 +03:00
|
|
|
, mNrOfMainThreadUsers(0)
|
2013-08-07 02:14:35 +04:00
|
|
|
, mGraph(nullptr)
|
2014-04-18 13:23:36 +04:00
|
|
|
, mAudioChannelType(dom::AudioChannel::Normal)
|
2013-08-07 02:14:35 +04:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(MediaStream);
|
|
|
|
// aWrapper should not already be connected to a MediaStream! It needs
|
|
|
|
// to be hooked up to this stream, and since this stream is only just
|
|
|
|
// being created now, aWrapper must not be connected to anything.
|
2015-09-30 04:31:54 +03:00
|
|
|
NS_ASSERTION(!aWrapper || !aWrapper->GetPlaybackStream(),
|
2013-08-07 02:14:35 +04:00
|
|
|
"Wrapper already has another media stream hooked up to it!");
|
|
|
|
}
|
|
|
|
|
2014-04-13 22:08:10 +04:00
|
|
|
size_t
|
|
|
|
MediaStream::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
|
|
|
|
{
|
|
|
|
size_t amount = 0;
|
|
|
|
|
|
|
|
// Not owned:
|
|
|
|
// - mGraph - Not reported here
|
|
|
|
// - mConsumers - elements
|
|
|
|
// Future:
|
|
|
|
// - mWrapper
|
|
|
|
// - mVideoOutputs - elements
|
|
|
|
// - mLastPlayedVideoFrame
|
|
|
|
// - mListeners - elements
|
2014-08-25 17:25:49 +04:00
|
|
|
// - mAudioOutputStream - elements
|
2014-04-13 22:08:10 +04:00
|
|
|
|
2016-01-26 05:49:01 +03:00
|
|
|
amount += mTracks.SizeOfExcludingThis(aMallocSizeOf);
|
2015-07-29 09:24:24 +03:00
|
|
|
amount += mAudioOutputs.ShallowSizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
amount += mVideoOutputs.ShallowSizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
amount += mListeners.ShallowSizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
amount += mMainThreadListeners.ShallowSizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
amount += mDisabledTrackIDs.ShallowSizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
amount += mConsumers.ShallowSizeOfExcludingThis(aMallocSizeOf);
|
2014-04-13 22:08:10 +04:00
|
|
|
|
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
MediaStream::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
|
|
|
|
{
|
|
|
|
return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
2012-04-30 07:11:26 +04:00
|
|
|
MediaStreamGraphImpl*
|
|
|
|
MediaStream::GraphImpl()
|
|
|
|
{
|
2013-02-01 23:43:36 +04:00
|
|
|
return mGraph;
|
2012-04-30 07:11:26 +04:00
|
|
|
}
|
|
|
|
|
2012-07-31 16:17:21 +04:00
|
|
|
MediaStreamGraph*
|
|
|
|
MediaStream::Graph()
|
|
|
|
{
|
2013-02-01 23:43:36 +04:00
|
|
|
return mGraph;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStream::SetGraphImpl(MediaStreamGraphImpl* aGraph)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mGraph, "Should only be called once");
|
|
|
|
mGraph = aGraph;
|
2015-10-23 06:43:15 +03:00
|
|
|
mAudioChannelType = aGraph->AudioChannel();
|
2016-01-26 05:49:01 +03:00
|
|
|
mTracks.InitGraphRate(aGraph->GraphRate());
|
2012-07-31 16:17:21 +04:00
|
|
|
}
|
|
|
|
|
2013-06-10 23:01:19 +04:00
|
|
|
void
|
|
|
|
MediaStream::SetGraphImpl(MediaStreamGraph* aGraph)
|
|
|
|
{
|
|
|
|
MediaStreamGraphImpl* graph = static_cast<MediaStreamGraphImpl*>(aGraph);
|
|
|
|
SetGraphImpl(graph);
|
|
|
|
}
|
|
|
|
|
2012-07-31 16:17:21 +04:00
|
|
|
StreamTime
|
2015-09-16 07:35:16 +03:00
|
|
|
MediaStream::GraphTimeToStreamTime(GraphTime aTime)
|
2012-07-31 16:17:21 +04:00
|
|
|
{
|
2015-09-08 08:14:43 +03:00
|
|
|
NS_ASSERTION(mStartBlocking == GraphImpl()->mStateComputedTime ||
|
|
|
|
aTime <= mStartBlocking,
|
|
|
|
"Incorrectly ignoring blocking!");
|
2016-01-26 05:49:01 +03:00
|
|
|
return aTime - mTracksStartTime;
|
2015-09-16 07:35:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
GraphTime
|
|
|
|
MediaStream::StreamTimeToGraphTime(StreamTime aTime)
|
|
|
|
{
|
2015-09-08 08:14:43 +03:00
|
|
|
NS_ASSERTION(mStartBlocking == GraphImpl()->mStateComputedTime ||
|
2016-01-26 05:49:01 +03:00
|
|
|
aTime + mTracksStartTime <= mStartBlocking,
|
2015-09-08 08:14:43 +03:00
|
|
|
"Incorrectly ignoring blocking!");
|
2016-01-26 05:49:01 +03:00
|
|
|
return aTime + mTracksStartTime;
|
2012-07-31 16:17:21 +04:00
|
|
|
}
|
|
|
|
|
2013-02-04 14:04:26 +04:00
|
|
|
StreamTime
|
2015-09-16 07:35:16 +03:00
|
|
|
MediaStream::GraphTimeToStreamTimeWithBlocking(GraphTime aTime)
|
2013-02-04 14:04:26 +04:00
|
|
|
{
|
2015-09-16 07:35:16 +03:00
|
|
|
return GraphImpl()->GraphTimeToStreamTimeWithBlocking(this, aTime);
|
2013-02-04 14:04:26 +04:00
|
|
|
}
|
|
|
|
|
2012-07-31 16:17:21 +04:00
|
|
|
void
|
|
|
|
MediaStream::FinishOnGraphThread()
|
|
|
|
{
|
|
|
|
GraphImpl()->FinishStream(this);
|
|
|
|
}
|
|
|
|
|
2016-01-26 05:49:01 +03:00
|
|
|
StreamTracks::Track*
|
2016-01-21 14:25:25 +03:00
|
|
|
MediaStream::FindTrack(TrackID aID)
|
|
|
|
{
|
2016-01-26 05:49:01 +03:00
|
|
|
return mTracks.FindTrack(aID);
|
2016-01-21 14:25:25 +03:00
|
|
|
}
|
|
|
|
|
2016-01-26 05:49:01 +03:00
|
|
|
StreamTracks::Track*
|
2014-09-18 09:13:16 +04:00
|
|
|
MediaStream::EnsureTrack(TrackID aTrackId)
|
2013-05-21 23:17:47 +04:00
|
|
|
{
|
2016-01-26 05:49:01 +03:00
|
|
|
StreamTracks::Track* track = mTracks.FindTrack(aTrackId);
|
2013-05-21 23:17:47 +04:00
|
|
|
if (!track) {
|
|
|
|
nsAutoPtr<MediaSegment> segment(new AudioSegment());
|
|
|
|
for (uint32_t j = 0; j < mListeners.Length(); ++j) {
|
|
|
|
MediaStreamListener* l = mListeners[j];
|
2014-09-18 03:51:13 +04:00
|
|
|
l->NotifyQueuedTrackChanges(Graph(), aTrackId, 0,
|
2013-05-21 23:17:47 +04:00
|
|
|
MediaStreamListener::TRACK_EVENT_CREATED,
|
|
|
|
*segment);
|
2015-02-06 12:38:11 +03:00
|
|
|
// TODO If we ever need to ensure several tracks at once, we will have to
|
|
|
|
// change this.
|
|
|
|
l->NotifyFinishedTrackCreation(Graph());
|
2013-05-21 23:17:47 +04:00
|
|
|
}
|
2016-01-26 05:49:01 +03:00
|
|
|
track = &mTracks.AddTrack(aTrackId, 0, segment.forget());
|
2013-05-21 23:17:47 +04:00
|
|
|
}
|
|
|
|
return track;
|
|
|
|
}
|
|
|
|
|
2013-01-07 06:31:30 +04:00
|
|
|
void
|
|
|
|
MediaStream::RemoveAllListenersImpl()
|
|
|
|
{
|
|
|
|
for (int32_t i = mListeners.Length() - 1; i >= 0; --i) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaStreamListener> listener = mListeners[i].forget();
|
2014-07-14 09:47:56 +04:00
|
|
|
listener->NotifyEvent(GraphImpl(), MediaStreamListener::EVENT_REMOVED);
|
2013-01-07 06:31:30 +04:00
|
|
|
}
|
|
|
|
mListeners.Clear();
|
|
|
|
}
|
|
|
|
|
2012-04-30 07:11:26 +04:00
|
|
|
void
|
|
|
|
MediaStream::DestroyImpl()
|
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
for (int32_t i = mConsumers.Length() - 1; i >= 0; --i) {
|
2012-07-31 16:17:21 +04:00
|
|
|
mConsumers[i]->Disconnect();
|
|
|
|
}
|
2014-07-25 01:09:22 +04:00
|
|
|
mGraph = nullptr;
|
2012-04-30 07:11:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStream::Destroy()
|
|
|
|
{
|
2016-01-05 05:16:22 +03:00
|
|
|
NS_ASSERTION(mNrOfMainThreadUsers == 0,
|
|
|
|
"Do not mix Destroy() and RegisterUser()/UnregisterUser()");
|
2012-10-26 05:39:05 +04:00
|
|
|
// Keep this stream alive until we leave this method
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaStream> kungFuDeathGrip = this;
|
2012-10-26 05:39:05 +04:00
|
|
|
|
2012-04-30 07:11:26 +04:00
|
|
|
class Message : public ControlMessage {
|
|
|
|
public:
|
2014-09-01 07:50:23 +04:00
|
|
|
explicit Message(MediaStream* aStream) : ControlMessage(aStream) {}
|
2016-01-18 06:50:29 +03:00
|
|
|
void Run() override
|
2012-04-30 07:11:26 +04:00
|
|
|
{
|
2014-07-25 01:09:22 +04:00
|
|
|
mStream->RemoveAllListenersImpl();
|
|
|
|
auto graph = mStream->GraphImpl();
|
2012-04-30 07:11:26 +04:00
|
|
|
mStream->DestroyImpl();
|
2015-08-12 02:29:35 +03:00
|
|
|
graph->RemoveStreamGraphThread(mStream);
|
2012-04-30 07:11:26 +04:00
|
|
|
}
|
2016-01-18 06:50:29 +03:00
|
|
|
void RunDuringShutdown() override
|
2015-06-11 22:10:06 +03:00
|
|
|
{ Run(); }
|
2012-04-30 07:11:26 +04:00
|
|
|
};
|
2012-07-30 18:20:58 +04:00
|
|
|
mWrapper = nullptr;
|
2016-01-21 00:14:33 +03:00
|
|
|
GraphImpl()->AppendMessage(MakeUnique<Message>(this));
|
2012-10-26 05:39:05 +04:00
|
|
|
// Message::RunDuringShutdown may have removed this stream from the graph,
|
|
|
|
// but our kungFuDeathGrip above will have kept this stream alive if
|
|
|
|
// necessary.
|
2012-10-26 03:08:38 +04:00
|
|
|
mMainThreadDestroyed = true;
|
2012-04-30 07:11:26 +04:00
|
|
|
}
|
|
|
|
|
2016-01-05 05:16:22 +03:00
|
|
|
void
|
|
|
|
MediaStream::RegisterUser()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
++mNrOfMainThreadUsers;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStream::UnregisterUser()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
--mNrOfMainThreadUsers;
|
|
|
|
NS_ASSERTION(mNrOfMainThreadUsers >= 0, "Double-removal of main thread user");
|
|
|
|
NS_ASSERTION(!IsDestroyed(), "Do not mix Destroy() and RegisterUser()/UnregisterUser()");
|
|
|
|
if (mNrOfMainThreadUsers == 0) {
|
|
|
|
Destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-30 07:11:26 +04:00
|
|
|
void
|
|
|
|
MediaStream::AddAudioOutput(void* aKey)
|
|
|
|
{
|
|
|
|
class Message : public ControlMessage {
|
|
|
|
public:
|
|
|
|
Message(MediaStream* aStream, void* aKey) : ControlMessage(aStream), mKey(aKey) {}
|
2016-01-18 06:50:29 +03:00
|
|
|
void Run() override
|
2012-04-30 07:11:26 +04:00
|
|
|
{
|
|
|
|
mStream->AddAudioOutputImpl(mKey);
|
|
|
|
}
|
|
|
|
void* mKey;
|
|
|
|
};
|
2016-01-21 00:14:33 +03:00
|
|
|
GraphImpl()->AppendMessage(MakeUnique<Message>(this, aKey));
|
2012-04-30 07:11:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStream::SetAudioOutputVolumeImpl(void* aKey, float aVolume)
|
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < mAudioOutputs.Length(); ++i) {
|
2012-04-30 07:11:26 +04:00
|
|
|
if (mAudioOutputs[i].mKey == aKey) {
|
|
|
|
mAudioOutputs[i].mVolume = aVolume;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
NS_ERROR("Audio output key not found");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStream::SetAudioOutputVolume(void* aKey, float aVolume)
|
|
|
|
{
|
|
|
|
class Message : public ControlMessage {
|
|
|
|
public:
|
|
|
|
Message(MediaStream* aStream, void* aKey, float aVolume) :
|
|
|
|
ControlMessage(aStream), mKey(aKey), mVolume(aVolume) {}
|
2016-01-18 06:50:29 +03:00
|
|
|
void Run() override
|
2012-04-30 07:11:26 +04:00
|
|
|
{
|
|
|
|
mStream->SetAudioOutputVolumeImpl(mKey, mVolume);
|
|
|
|
}
|
|
|
|
void* mKey;
|
|
|
|
float mVolume;
|
|
|
|
};
|
2016-01-21 00:14:33 +03:00
|
|
|
GraphImpl()->AppendMessage(MakeUnique<Message>(this, aKey, aVolume));
|
2012-04-30 07:11:26 +04:00
|
|
|
}
|
|
|
|
|
2015-10-29 08:19:51 +03:00
|
|
|
void
|
|
|
|
MediaStream::AddAudioOutputImpl(void* aKey)
|
|
|
|
{
|
|
|
|
STREAM_LOG(LogLevel::Info, ("MediaStream %p Adding AudioOutput for key %p",
|
|
|
|
this, aKey));
|
|
|
|
mAudioOutputs.AppendElement(AudioOutput(aKey));
|
|
|
|
}
|
|
|
|
|
2012-04-30 07:11:26 +04:00
|
|
|
void
|
|
|
|
MediaStream::RemoveAudioOutputImpl(void* aKey)
|
|
|
|
{
|
2015-10-29 08:19:51 +03:00
|
|
|
STREAM_LOG(LogLevel::Info, ("MediaStream %p Removing AudioOutput for key %p",
|
|
|
|
this, aKey));
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < mAudioOutputs.Length(); ++i) {
|
2012-04-30 07:11:26 +04:00
|
|
|
if (mAudioOutputs[i].mKey == aKey) {
|
|
|
|
mAudioOutputs.RemoveElementAt(i);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
NS_ERROR("Audio output key not found");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStream::RemoveAudioOutput(void* aKey)
|
|
|
|
{
|
|
|
|
class Message : public ControlMessage {
|
|
|
|
public:
|
|
|
|
Message(MediaStream* aStream, void* aKey) :
|
|
|
|
ControlMessage(aStream), mKey(aKey) {}
|
2016-01-18 06:50:29 +03:00
|
|
|
void Run() override
|
2012-04-30 07:11:26 +04:00
|
|
|
{
|
|
|
|
mStream->RemoveAudioOutputImpl(mKey);
|
|
|
|
}
|
|
|
|
void* mKey;
|
|
|
|
};
|
2016-01-21 00:14:33 +03:00
|
|
|
GraphImpl()->AppendMessage(MakeUnique<Message>(this, aKey));
|
2012-04-30 07:11:26 +04:00
|
|
|
}
|
|
|
|
|
2015-10-29 08:19:51 +03:00
|
|
|
void
|
|
|
|
MediaStream::AddVideoOutputImpl(already_AddRefed<VideoFrameContainer> aContainer)
|
|
|
|
{
|
|
|
|
RefPtr<VideoFrameContainer> container = aContainer;
|
|
|
|
STREAM_LOG(LogLevel::Info, ("MediaStream %p Adding VideoFrameContainer %p as output",
|
|
|
|
this, container.get()));
|
|
|
|
*mVideoOutputs.AppendElement() = container.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStream::RemoveVideoOutputImpl(VideoFrameContainer* aContainer)
|
|
|
|
{
|
|
|
|
STREAM_LOG(LogLevel::Info, ("MediaStream %p Removing VideoFrameContainer %p as output",
|
|
|
|
this, aContainer));
|
|
|
|
// Ensure that any frames currently queued for playback by the compositor
|
|
|
|
// are removed.
|
|
|
|
aContainer->ClearFutureFrames();
|
|
|
|
mVideoOutputs.RemoveElement(aContainer);
|
|
|
|
}
|
|
|
|
|
2012-04-30 07:11:26 +04:00
|
|
|
void
|
|
|
|
MediaStream::AddVideoOutput(VideoFrameContainer* aContainer)
|
|
|
|
{
|
|
|
|
class Message : public ControlMessage {
|
|
|
|
public:
|
|
|
|
Message(MediaStream* aStream, VideoFrameContainer* aContainer) :
|
|
|
|
ControlMessage(aStream), mContainer(aContainer) {}
|
2016-01-18 06:50:29 +03:00
|
|
|
void Run() override
|
2012-04-30 07:11:26 +04:00
|
|
|
{
|
|
|
|
mStream->AddVideoOutputImpl(mContainer.forget());
|
|
|
|
}
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<VideoFrameContainer> mContainer;
|
2012-04-30 07:11:26 +04:00
|
|
|
};
|
2016-01-21 00:14:33 +03:00
|
|
|
GraphImpl()->AppendMessage(MakeUnique<Message>(this, aContainer));
|
2012-04-30 07:11:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStream::RemoveVideoOutput(VideoFrameContainer* aContainer)
|
|
|
|
{
|
|
|
|
class Message : public ControlMessage {
|
|
|
|
public:
|
|
|
|
Message(MediaStream* aStream, VideoFrameContainer* aContainer) :
|
|
|
|
ControlMessage(aStream), mContainer(aContainer) {}
|
2016-01-18 06:50:29 +03:00
|
|
|
void Run() override
|
2012-04-30 07:11:26 +04:00
|
|
|
{
|
|
|
|
mStream->RemoveVideoOutputImpl(mContainer);
|
|
|
|
}
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<VideoFrameContainer> mContainer;
|
2012-04-30 07:11:26 +04:00
|
|
|
};
|
2016-01-21 00:14:33 +03:00
|
|
|
GraphImpl()->AppendMessage(MakeUnique<Message>(this, aContainer));
|
2012-04-30 07:11:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-09-10 16:45:36 +03:00
|
|
|
MediaStream::Suspend()
|
2012-04-30 07:11:26 +04:00
|
|
|
{
|
|
|
|
class Message : public ControlMessage {
|
|
|
|
public:
|
2015-09-10 16:45:36 +03:00
|
|
|
explicit Message(MediaStream* aStream) :
|
|
|
|
ControlMessage(aStream) {}
|
2016-01-18 06:50:29 +03:00
|
|
|
void Run() override
|
2012-04-30 07:11:26 +04:00
|
|
|
{
|
2015-09-10 16:45:36 +03:00
|
|
|
mStream->GraphImpl()->IncrementSuspendCount(mStream);
|
2012-04-30 07:11:26 +04:00
|
|
|
}
|
|
|
|
};
|
2014-01-13 20:38:30 +04:00
|
|
|
|
|
|
|
// This can happen if this method has been called asynchronously, and the
|
|
|
|
// stream has been destroyed since then.
|
|
|
|
if (mMainThreadDestroyed) {
|
|
|
|
return;
|
|
|
|
}
|
2016-01-21 00:14:33 +03:00
|
|
|
GraphImpl()->AppendMessage(MakeUnique<Message>(this));
|
2015-09-10 16:45:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStream::Resume()
|
|
|
|
{
|
|
|
|
class Message : public ControlMessage {
|
|
|
|
public:
|
|
|
|
explicit Message(MediaStream* aStream) :
|
|
|
|
ControlMessage(aStream) {}
|
2016-01-18 06:50:29 +03:00
|
|
|
void Run() override
|
2015-09-10 16:45:36 +03:00
|
|
|
{
|
|
|
|
mStream->GraphImpl()->DecrementSuspendCount(mStream);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// This can happen if this method has been called asynchronously, and the
|
|
|
|
// stream has been destroyed since then.
|
|
|
|
if (mMainThreadDestroyed) {
|
|
|
|
return;
|
|
|
|
}
|
2016-01-21 00:14:33 +03:00
|
|
|
GraphImpl()->AppendMessage(MakeUnique<Message>(this));
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
}
|
|
|
|
|
2012-05-24 14:37:14 +04:00
|
|
|
void
|
|
|
|
MediaStream::AddListenerImpl(already_AddRefed<MediaStreamListener> aListener)
|
|
|
|
{
|
|
|
|
MediaStreamListener* listener = *mListeners.AppendElement() = aListener;
|
|
|
|
listener->NotifyBlockingChanged(GraphImpl(),
|
2013-01-02 17:49:18 +04:00
|
|
|
mNotifiedBlocked ? MediaStreamListener::BLOCKED : MediaStreamListener::UNBLOCKED);
|
2012-05-24 14:37:14 +04:00
|
|
|
if (mNotifiedFinished) {
|
2014-07-14 09:47:56 +04:00
|
|
|
listener->NotifyEvent(GraphImpl(), MediaStreamListener::EVENT_FINISHED);
|
2012-05-24 14:37:14 +04:00
|
|
|
}
|
2013-03-20 15:19:39 +04:00
|
|
|
if (mNotifiedHasCurrentData) {
|
|
|
|
listener->NotifyHasCurrentData(GraphImpl());
|
|
|
|
}
|
2012-05-24 14:37:14 +04:00
|
|
|
}
|
|
|
|
|
2012-04-30 07:11:26 +04:00
|
|
|
void
|
|
|
|
MediaStream::AddListener(MediaStreamListener* aListener)
|
|
|
|
{
|
|
|
|
class Message : public ControlMessage {
|
|
|
|
public:
|
|
|
|
Message(MediaStream* aStream, MediaStreamListener* aListener) :
|
|
|
|
ControlMessage(aStream), mListener(aListener) {}
|
2016-01-18 06:50:29 +03:00
|
|
|
void Run() override
|
2012-04-30 07:11:26 +04:00
|
|
|
{
|
|
|
|
mStream->AddListenerImpl(mListener.forget());
|
|
|
|
}
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaStreamListener> mListener;
|
2012-04-30 07:11:26 +04:00
|
|
|
};
|
2016-01-21 00:14:33 +03:00
|
|
|
GraphImpl()->AppendMessage(MakeUnique<Message>(this, aListener));
|
2012-04-30 07:11:26 +04:00
|
|
|
}
|
|
|
|
|
2013-01-07 06:31:30 +04:00
|
|
|
void
|
|
|
|
MediaStream::RemoveListenerImpl(MediaStreamListener* aListener)
|
2014-06-22 22:21:00 +04:00
|
|
|
{
|
2013-01-07 06:31:30 +04:00
|
|
|
// wouldn't need this if we could do it in the opposite order
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaStreamListener> listener(aListener);
|
2013-01-07 06:31:30 +04:00
|
|
|
mListeners.RemoveElement(aListener);
|
2014-07-14 09:47:56 +04:00
|
|
|
listener->NotifyEvent(GraphImpl(), MediaStreamListener::EVENT_REMOVED);
|
2013-01-07 06:31:30 +04:00
|
|
|
}
|
|
|
|
|
2012-04-30 07:11:26 +04:00
|
|
|
void
|
|
|
|
MediaStream::RemoveListener(MediaStreamListener* aListener)
|
|
|
|
{
|
|
|
|
class Message : public ControlMessage {
|
|
|
|
public:
|
|
|
|
Message(MediaStream* aStream, MediaStreamListener* aListener) :
|
|
|
|
ControlMessage(aStream), mListener(aListener) {}
|
2016-01-18 06:50:29 +03:00
|
|
|
void Run() override
|
2012-04-30 07:11:26 +04:00
|
|
|
{
|
|
|
|
mStream->RemoveListenerImpl(mListener);
|
|
|
|
}
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaStreamListener> mListener;
|
2012-04-30 07:11:26 +04:00
|
|
|
};
|
2013-02-28 23:53:38 +04:00
|
|
|
// If the stream is destroyed the Listeners have or will be
|
|
|
|
// removed.
|
|
|
|
if (!IsDestroyed()) {
|
2016-01-21 00:14:33 +03:00
|
|
|
GraphImpl()->AppendMessage(MakeUnique<Message>(this, aListener));
|
2013-02-28 23:53:38 +04:00
|
|
|
}
|
2012-04-30 07:11:26 +04:00
|
|
|
}
|
2013-10-25 03:07:29 +04:00
|
|
|
|
2016-03-03 19:27:59 +03:00
|
|
|
void
|
|
|
|
MediaStream::AddTrackListenerImpl(already_AddRefed<MediaStreamTrackListener> aListener,
|
|
|
|
TrackID aTrackID)
|
|
|
|
{
|
|
|
|
TrackBound<MediaStreamTrackListener>* l = mTrackListeners.AppendElement();
|
|
|
|
l->mListener = aListener;
|
|
|
|
l->mTrackID = aTrackID;
|
2016-01-26 11:45:25 +03:00
|
|
|
|
2016-01-26 05:49:01 +03:00
|
|
|
StreamTracks::Track* track = FindTrack(aTrackID);
|
2016-01-26 11:45:25 +03:00
|
|
|
if (!track) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
PrincipalHandle lastPrincipalHandle =
|
|
|
|
track->GetSegment()->GetLastPrincipalHandle();
|
|
|
|
l->mListener->NotifyPrincipalHandleChanged(Graph(), lastPrincipalHandle);
|
2016-03-03 19:27:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStream::AddTrackListener(MediaStreamTrackListener* aListener,
|
|
|
|
TrackID aTrackID)
|
|
|
|
{
|
|
|
|
class Message : public ControlMessage {
|
|
|
|
public:
|
|
|
|
Message(MediaStream* aStream, MediaStreamTrackListener* aListener,
|
|
|
|
TrackID aTrackID) :
|
|
|
|
ControlMessage(aStream), mListener(aListener), mTrackID(aTrackID) {}
|
|
|
|
virtual void Run()
|
|
|
|
{
|
|
|
|
mStream->AddTrackListenerImpl(mListener.forget(), mTrackID);
|
|
|
|
}
|
|
|
|
RefPtr<MediaStreamTrackListener> mListener;
|
|
|
|
TrackID mTrackID;
|
|
|
|
};
|
|
|
|
GraphImpl()->AppendMessage(MakeUnique<Message>(this, aListener, aTrackID));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStream::RemoveTrackListenerImpl(MediaStreamTrackListener* aListener,
|
|
|
|
TrackID aTrackID)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < mTrackListeners.Length(); ++i) {
|
|
|
|
if (mTrackListeners[i].mListener == aListener &&
|
|
|
|
mTrackListeners[i].mTrackID == aTrackID) {
|
|
|
|
mTrackListeners[i].mListener->NotifyRemoved();
|
|
|
|
mTrackListeners.RemoveElementAt(i);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStream::RemoveTrackListener(MediaStreamTrackListener* aListener,
|
|
|
|
TrackID aTrackID)
|
|
|
|
{
|
|
|
|
class Message : public ControlMessage {
|
|
|
|
public:
|
|
|
|
Message(MediaStream* aStream, MediaStreamTrackListener* aListener,
|
|
|
|
TrackID aTrackID) :
|
|
|
|
ControlMessage(aStream), mListener(aListener), mTrackID(aTrackID) {}
|
|
|
|
virtual void Run()
|
|
|
|
{
|
|
|
|
mStream->RemoveTrackListenerImpl(mListener, mTrackID);
|
|
|
|
}
|
|
|
|
RefPtr<MediaStreamTrackListener> mListener;
|
|
|
|
TrackID mTrackID;
|
|
|
|
};
|
|
|
|
GraphImpl()->AppendMessage(MakeUnique<Message>(this, aListener, aTrackID));
|
|
|
|
}
|
|
|
|
|
2016-03-03 19:28:37 +03:00
|
|
|
void
|
|
|
|
MediaStream::AddDirectTrackListenerImpl(already_AddRefed<MediaStreamTrackDirectListener> aListener,
|
|
|
|
TrackID aTrackID)
|
|
|
|
{
|
|
|
|
// Base implementation, for streams that don't support direct track listeners.
|
|
|
|
RefPtr<MediaStreamTrackDirectListener> listener = aListener;
|
|
|
|
listener->NotifyDirectListenerInstalled(
|
|
|
|
MediaStreamTrackDirectListener::InstallationResult::STREAM_NOT_SUPPORTED);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStream::AddDirectTrackListener(MediaStreamTrackDirectListener* aListener,
|
|
|
|
TrackID aTrackID)
|
|
|
|
{
|
|
|
|
class Message : public ControlMessage {
|
|
|
|
public:
|
|
|
|
Message(MediaStream* aStream, MediaStreamTrackDirectListener* aListener,
|
|
|
|
TrackID aTrackID) :
|
|
|
|
ControlMessage(aStream), mListener(aListener), mTrackID(aTrackID) {}
|
|
|
|
virtual void Run()
|
|
|
|
{
|
|
|
|
mStream->AddDirectTrackListenerImpl(mListener.forget(), mTrackID);
|
|
|
|
}
|
|
|
|
RefPtr<MediaStreamTrackDirectListener> mListener;
|
|
|
|
TrackID mTrackID;
|
|
|
|
};
|
|
|
|
GraphImpl()->AppendMessage(MakeUnique<Message>(this, aListener, aTrackID));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStream::RemoveDirectTrackListenerImpl(MediaStreamTrackDirectListener* aListener,
|
|
|
|
TrackID aTrackID)
|
|
|
|
{
|
|
|
|
// Base implementation, the listener was never added so nothing to do.
|
|
|
|
RefPtr<MediaStreamTrackDirectListener> listener = aListener;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStream::RemoveDirectTrackListener(MediaStreamTrackDirectListener* aListener,
|
|
|
|
TrackID aTrackID)
|
|
|
|
{
|
|
|
|
class Message : public ControlMessage {
|
|
|
|
public:
|
|
|
|
Message(MediaStream* aStream, MediaStreamTrackDirectListener* aListener,
|
|
|
|
TrackID aTrackID) :
|
|
|
|
ControlMessage(aStream), mListener(aListener), mTrackID(aTrackID) {}
|
|
|
|
virtual void Run()
|
|
|
|
{
|
|
|
|
mStream->RemoveDirectTrackListenerImpl(mListener, mTrackID);
|
|
|
|
}
|
|
|
|
RefPtr<MediaStreamTrackDirectListener> mListener;
|
|
|
|
TrackID mTrackID;
|
|
|
|
};
|
|
|
|
GraphImpl()->AppendMessage(MakeUnique<Message>(this, aListener, aTrackID));
|
|
|
|
}
|
|
|
|
|
2013-10-25 03:07:29 +04:00
|
|
|
void
|
2015-03-17 19:29:17 +03:00
|
|
|
MediaStream::RunAfterPendingUpdates(already_AddRefed<nsIRunnable> aRunnable)
|
2013-10-25 03:07:29 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MediaStreamGraphImpl* graph = GraphImpl();
|
2015-03-17 19:29:17 +03:00
|
|
|
nsCOMPtr<nsIRunnable> runnable(aRunnable);
|
2013-10-25 03:07:29 +04:00
|
|
|
|
|
|
|
// Special case when a non-realtime graph has not started, to ensure the
|
|
|
|
// runnable will run in finite time.
|
|
|
|
if (!(graph->mRealtime || graph->mNonRealtimeProcessing)) {
|
2015-03-17 19:29:17 +03:00
|
|
|
runnable->Run();
|
2015-03-17 23:29:33 +03:00
|
|
|
return;
|
2013-10-25 03:07:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
class Message : public ControlMessage {
|
|
|
|
public:
|
|
|
|
explicit Message(MediaStream* aStream,
|
|
|
|
already_AddRefed<nsIRunnable> aRunnable)
|
|
|
|
: ControlMessage(aStream)
|
|
|
|
, mRunnable(aRunnable) {}
|
2016-01-18 06:50:29 +03:00
|
|
|
void Run() override
|
2013-10-25 03:07:29 +04:00
|
|
|
{
|
|
|
|
mStream->Graph()->
|
|
|
|
DispatchToMainThreadAfterStreamStateUpdate(mRunnable.forget());
|
|
|
|
}
|
2016-01-18 06:50:29 +03:00
|
|
|
void RunDuringShutdown() override
|
2013-10-25 03:07:29 +04:00
|
|
|
{
|
2014-07-02 10:04:54 +04:00
|
|
|
// Don't run mRunnable now as it may call AppendMessage() which would
|
|
|
|
// assume that there are no remaining controlMessagesToRunDuringShutdown.
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
NS_DispatchToCurrentThread(mRunnable);
|
2013-10-25 03:07:29 +04:00
|
|
|
}
|
|
|
|
private:
|
2015-03-17 19:29:17 +03:00
|
|
|
nsCOMPtr<nsIRunnable> mRunnable;
|
2013-10-25 03:07:29 +04:00
|
|
|
};
|
|
|
|
|
2016-01-21 00:14:33 +03:00
|
|
|
graph->AppendMessage(MakeUnique<Message>(this, runnable.forget()));
|
2013-10-25 03:07:29 +04:00
|
|
|
}
|
2012-04-30 07:11:26 +04:00
|
|
|
|
2013-05-30 08:44:43 +04:00
|
|
|
void
|
|
|
|
MediaStream::SetTrackEnabledImpl(TrackID aTrackID, bool aEnabled)
|
|
|
|
{
|
|
|
|
if (aEnabled) {
|
|
|
|
mDisabledTrackIDs.RemoveElement(aTrackID);
|
|
|
|
} else {
|
|
|
|
if (!mDisabledTrackIDs.Contains(aTrackID)) {
|
|
|
|
mDisabledTrackIDs.AppendElement(aTrackID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStream::SetTrackEnabled(TrackID aTrackID, bool aEnabled)
|
|
|
|
{
|
|
|
|
class Message : public ControlMessage {
|
|
|
|
public:
|
|
|
|
Message(MediaStream* aStream, TrackID aTrackID, bool aEnabled) :
|
|
|
|
ControlMessage(aStream), mTrackID(aTrackID), mEnabled(aEnabled) {}
|
2016-01-18 06:50:29 +03:00
|
|
|
void Run() override
|
2013-05-30 08:44:43 +04:00
|
|
|
{
|
|
|
|
mStream->SetTrackEnabledImpl(mTrackID, mEnabled);
|
|
|
|
}
|
|
|
|
TrackID mTrackID;
|
|
|
|
bool mEnabled;
|
|
|
|
};
|
2016-01-21 00:14:33 +03:00
|
|
|
GraphImpl()->AppendMessage(MakeUnique<Message>(this, aTrackID, aEnabled));
|
2013-05-30 08:44:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-08-26 10:07:17 +04:00
|
|
|
MediaStream::ApplyTrackDisabling(TrackID aTrackID, MediaSegment* aSegment, MediaSegment* aRawSegment)
|
2013-05-30 08:44:43 +04:00
|
|
|
{
|
|
|
|
if (!mDisabledTrackIDs.Contains(aTrackID)) {
|
|
|
|
return;
|
|
|
|
}
|
2013-08-26 10:07:17 +04:00
|
|
|
aSegment->ReplaceWithDisabled();
|
|
|
|
if (aRawSegment) {
|
|
|
|
aRawSegment->ReplaceWithDisabled();
|
2013-05-30 08:44:43 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-11 17:07:38 +03:00
|
|
|
void
|
|
|
|
MediaStream::AddMainThreadListener(MainThreadMediaStreamListener* aListener)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aListener);
|
|
|
|
MOZ_ASSERT(!mMainThreadListeners.Contains(aListener));
|
|
|
|
|
|
|
|
mMainThreadListeners.AppendElement(aListener);
|
|
|
|
|
2015-08-24 02:54:24 +03:00
|
|
|
// If it is not yet time to send the notification, then finish here.
|
|
|
|
if (!mFinishedNotificationSent) {
|
2015-05-11 17:07:38 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class NotifyRunnable final : public Runnable
|
2015-05-11 17:07:38 +03:00
|
|
|
{
|
|
|
|
public:
|
2015-05-11 17:25:28 +03:00
|
|
|
explicit NotifyRunnable(MediaStream* aStream)
|
2015-05-11 17:07:38 +03:00
|
|
|
: mStream(aStream)
|
|
|
|
{}
|
|
|
|
|
|
|
|
NS_IMETHOD Run() override
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
mStream->NotifyMainThreadListeners();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
~NotifyRunnable() {}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaStream> mStream;
|
2015-05-11 17:07:38 +03:00
|
|
|
};
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
nsCOMPtr<nsIRunnable> runnable = new NotifyRunnable(this);
|
2015-08-24 02:54:24 +03:00
|
|
|
NS_WARN_IF(NS_FAILED(NS_DispatchToMainThread(runnable.forget())));
|
2015-05-11 17:07:38 +03:00
|
|
|
}
|
|
|
|
|
2016-02-04 05:12:51 +03:00
|
|
|
nsresult
|
2016-03-08 20:11:09 +03:00
|
|
|
SourceMediaStream::OpenAudioInput(int aID,
|
2016-02-04 05:12:51 +03:00
|
|
|
AudioDataListener *aListener)
|
|
|
|
{
|
|
|
|
if (GraphImpl()) {
|
|
|
|
mInputListener = aListener;
|
|
|
|
return GraphImpl()->OpenAudioInput(aID, aListener);
|
|
|
|
}
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SourceMediaStream::CloseAudioInput()
|
|
|
|
{
|
|
|
|
// Destroy() may have run already and cleared this
|
|
|
|
if (GraphImpl() && mInputListener) {
|
|
|
|
GraphImpl()->CloseAudioInput(mInputListener);
|
|
|
|
}
|
|
|
|
mInputListener = nullptr;
|
|
|
|
}
|
|
|
|
|
2012-04-30 07:11:40 +04:00
|
|
|
void
|
2012-05-23 10:01:15 +04:00
|
|
|
SourceMediaStream::DestroyImpl()
|
2012-04-30 07:11:40 +04:00
|
|
|
{
|
2016-02-04 05:12:51 +03:00
|
|
|
CloseAudioInput();
|
|
|
|
|
2014-07-25 01:09:22 +04:00
|
|
|
// Hold mMutex while mGraph is reset so that other threads holding mMutex
|
|
|
|
// can null-check know that the graph will not destroyed.
|
|
|
|
MutexAutoLock lock(mMutex);
|
2012-05-23 10:01:15 +04:00
|
|
|
MediaStream::DestroyImpl();
|
|
|
|
}
|
|
|
|
|
2012-07-20 23:36:03 +04:00
|
|
|
void
|
|
|
|
SourceMediaStream::SetPullEnabled(bool aEnabled)
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
mPullEnabled = aEnabled;
|
2014-07-25 01:09:22 +04:00
|
|
|
if (mPullEnabled && GraphImpl()) {
|
2014-09-28 20:07:24 +04:00
|
|
|
GraphImpl()->EnsureNextIteration();
|
2012-07-20 23:36:03 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-23 10:01:15 +04:00
|
|
|
void
|
2014-09-18 09:20:43 +04:00
|
|
|
SourceMediaStream::AddTrackInternal(TrackID aID, TrackRate aRate, StreamTime aStart,
|
2015-02-19 20:04:26 +03:00
|
|
|
MediaSegment* aSegment, uint32_t aFlags)
|
2012-05-23 10:01:15 +04:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
2015-02-19 20:04:26 +03:00
|
|
|
nsTArray<TrackData> *track_data = (aFlags & ADDTRACK_QUEUED) ?
|
|
|
|
&mPendingTracks : &mUpdateTracks;
|
|
|
|
TrackData* data = track_data->AppendElement();
|
2016-03-08 20:11:09 +03:00
|
|
|
LIFECYCLE_LOG("AddTrackInternal: %lu/%lu", mPendingTracks.Length(), mUpdateTracks.Length());
|
2012-05-23 10:01:15 +04:00
|
|
|
data->mID = aID;
|
2014-03-24 14:06:05 +04:00
|
|
|
data->mInputRate = aRate;
|
2015-10-20 13:01:08 +03:00
|
|
|
data->mResamplerChannelCount = 0;
|
2012-05-23 10:01:15 +04:00
|
|
|
data->mStart = aStart;
|
2014-12-30 04:54:01 +03:00
|
|
|
data->mEndOfFlushedData = aStart;
|
2012-05-23 10:01:15 +04:00
|
|
|
data->mCommands = TRACK_CREATE;
|
|
|
|
data->mData = aSegment;
|
2016-04-15 16:52:59 +03:00
|
|
|
ResampleAudioToGraphSampleRate(data, aSegment);
|
2015-02-19 20:04:26 +03:00
|
|
|
if (!(aFlags & ADDTRACK_QUEUED) && GraphImpl()) {
|
|
|
|
GraphImpl()->EnsureNextIteration();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SourceMediaStream::FinishAddTracks()
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
2015-08-11 18:29:46 +03:00
|
|
|
mUpdateTracks.AppendElements(Move(mPendingTracks));
|
2016-03-08 20:11:09 +03:00
|
|
|
LIFECYCLE_LOG("FinishAddTracks: %lu/%lu", mPendingTracks.Length(), mUpdateTracks.Length());
|
2014-09-18 03:50:02 +04:00
|
|
|
if (GraphImpl()) {
|
|
|
|
GraphImpl()->EnsureNextIteration();
|
2012-05-23 10:01:15 +04:00
|
|
|
}
|
2012-04-30 07:11:40 +04:00
|
|
|
}
|
|
|
|
|
2014-03-24 14:06:05 +04:00
|
|
|
void
|
|
|
|
SourceMediaStream::ResampleAudioToGraphSampleRate(TrackData* aTrackData, MediaSegment* aSegment)
|
|
|
|
{
|
|
|
|
if (aSegment->GetType() != MediaSegment::AUDIO ||
|
2014-09-18 03:50:01 +04:00
|
|
|
aTrackData->mInputRate == GraphImpl()->GraphRate()) {
|
2014-03-24 14:06:05 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
AudioSegment* segment = static_cast<AudioSegment*>(aSegment);
|
2014-06-09 04:11:41 +04:00
|
|
|
int channels = segment->ChannelCount();
|
2014-04-25 17:15:21 +04:00
|
|
|
|
2015-09-01 15:25:48 +03:00
|
|
|
// If this segment is just silence, we delay instanciating the resampler. We
|
|
|
|
// also need to recreate the resampler if the channel count changes.
|
|
|
|
if (channels && aTrackData->mResamplerChannelCount != channels) {
|
|
|
|
SpeexResamplerState* state = speex_resampler_init(channels,
|
|
|
|
aTrackData->mInputRate,
|
|
|
|
GraphImpl()->GraphRate(),
|
|
|
|
SPEEX_RESAMPLER_QUALITY_MIN,
|
|
|
|
nullptr);
|
|
|
|
if (!state) {
|
|
|
|
return;
|
2014-03-24 14:06:05 +04:00
|
|
|
}
|
2015-09-01 15:25:48 +03:00
|
|
|
aTrackData->mResampler.own(state);
|
|
|
|
aTrackData->mResamplerChannelCount = channels;
|
2014-03-24 14:06:05 +04:00
|
|
|
}
|
2014-09-18 03:50:01 +04:00
|
|
|
segment->ResampleChunks(aTrackData->mResampler, aTrackData->mInputRate, GraphImpl()->GraphRate());
|
2014-03-24 14:06:05 +04:00
|
|
|
}
|
|
|
|
|
2013-02-25 13:25:07 +04:00
|
|
|
bool
|
2013-08-24 17:53:11 +04:00
|
|
|
SourceMediaStream::AppendToTrack(TrackID aID, MediaSegment* aSegment, MediaSegment *aRawSegment)
|
2012-04-30 07:11:40 +04:00
|
|
|
{
|
2012-05-23 10:01:15 +04:00
|
|
|
MutexAutoLock lock(mMutex);
|
2012-10-25 03:21:32 +04:00
|
|
|
// ::EndAllTrackAndFinished() can end these before the sources notice
|
2013-02-25 13:25:07 +04:00
|
|
|
bool appended = false;
|
2014-07-25 01:23:59 +04:00
|
|
|
auto graph = GraphImpl();
|
|
|
|
if (!mFinished && graph) {
|
2012-10-25 03:21:32 +04:00
|
|
|
TrackData *track = FindDataForTrack(aID);
|
|
|
|
if (track) {
|
2013-08-24 17:53:01 +04:00
|
|
|
// Data goes into mData, and on the next iteration of the MSG moves
|
|
|
|
// into the track's segment after NotifyQueuedTrackChanges(). This adds
|
|
|
|
// 0-10ms of delay before data gets to direct listeners.
|
|
|
|
// Indirect listeners (via subsequent TrackUnion nodes) are synced to
|
|
|
|
// playout time, and so can be delayed by buffering.
|
|
|
|
|
2013-08-26 10:07:17 +04:00
|
|
|
// Apply track disabling before notifying any consumers directly
|
|
|
|
// or inserting into the graph
|
|
|
|
ApplyTrackDisabling(aID, aSegment, aRawSegment);
|
|
|
|
|
2014-03-24 14:06:05 +04:00
|
|
|
ResampleAudioToGraphSampleRate(track, aSegment);
|
|
|
|
|
2013-08-24 17:53:11 +04:00
|
|
|
// Must notify first, since AppendFrom() will empty out aSegment
|
|
|
|
NotifyDirectConsumers(track, aRawSegment ? aRawSegment : aSegment);
|
|
|
|
track->mData->AppendFrom(aSegment); // note: aSegment is now dead
|
2013-02-25 13:25:07 +04:00
|
|
|
appended = true;
|
2014-09-28 20:07:24 +04:00
|
|
|
GraphImpl()->EnsureNextIteration();
|
2012-10-25 03:21:32 +04:00
|
|
|
} else {
|
2013-02-25 13:25:07 +04:00
|
|
|
aSegment->Clear();
|
2013-02-27 16:49:26 +04:00
|
|
|
}
|
2012-05-23 10:01:15 +04:00
|
|
|
}
|
2013-02-25 13:25:07 +04:00
|
|
|
return appended;
|
2012-04-30 07:11:40 +04:00
|
|
|
}
|
|
|
|
|
2013-08-24 17:53:11 +04:00
|
|
|
void
|
|
|
|
SourceMediaStream::NotifyDirectConsumers(TrackData *aTrack,
|
|
|
|
MediaSegment *aSegment)
|
|
|
|
{
|
2016-03-03 19:28:37 +03:00
|
|
|
mMutex.AssertCurrentThreadOwns();
|
2013-08-24 17:53:11 +04:00
|
|
|
MOZ_ASSERT(aTrack);
|
|
|
|
|
|
|
|
for (uint32_t j = 0; j < mDirectListeners.Length(); ++j) {
|
|
|
|
MediaStreamDirectListener* l = mDirectListeners[j];
|
2014-09-18 09:20:43 +04:00
|
|
|
StreamTime offset = 0; // FIX! need a separate StreamTime.... or the end of the internal buffer
|
2014-09-18 03:50:02 +04:00
|
|
|
l->NotifyRealtimeData(static_cast<MediaStreamGraph*>(GraphImpl()), aTrack->mID,
|
2013-08-24 17:53:11 +04:00
|
|
|
offset, aTrack->mCommands, *aSegment);
|
|
|
|
}
|
2016-03-03 19:28:37 +03:00
|
|
|
|
|
|
|
for (const TrackBound<MediaStreamTrackDirectListener>& source
|
|
|
|
: mDirectTrackListeners) {
|
|
|
|
if (aTrack->mID != source.mTrackID) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
StreamTime offset = 0; // FIX! need a separate StreamTime.... or the end of the internal buffer
|
2016-03-18 16:21:51 +03:00
|
|
|
source.mListener->NotifyRealtimeTrackDataAndApplyTrackDisabling(Graph(), offset, *aSegment);
|
2016-03-03 19:28:37 +03:00
|
|
|
}
|
2013-08-24 17:53:11 +04:00
|
|
|
}
|
|
|
|
|
2014-08-17 10:09:21 +04:00
|
|
|
// These handle notifying all the listeners of an event
|
|
|
|
void
|
|
|
|
SourceMediaStream::NotifyListenersEventImpl(MediaStreamListener::MediaStreamGraphEvent aEvent)
|
|
|
|
{
|
|
|
|
for (uint32_t j = 0; j < mListeners.Length(); ++j) {
|
|
|
|
MediaStreamListener* l = mListeners[j];
|
|
|
|
l->NotifyEvent(GraphImpl(), aEvent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SourceMediaStream::NotifyListenersEvent(MediaStreamListener::MediaStreamGraphEvent aNewEvent)
|
|
|
|
{
|
|
|
|
class Message : public ControlMessage {
|
|
|
|
public:
|
|
|
|
Message(SourceMediaStream* aStream, MediaStreamListener::MediaStreamGraphEvent aEvent) :
|
|
|
|
ControlMessage(aStream), mEvent(aEvent) {}
|
2016-01-18 06:50:29 +03:00
|
|
|
void Run() override
|
2014-08-17 10:09:21 +04:00
|
|
|
{
|
|
|
|
mStream->AsSourceStream()->NotifyListenersEventImpl(mEvent);
|
|
|
|
}
|
|
|
|
MediaStreamListener::MediaStreamGraphEvent mEvent;
|
|
|
|
};
|
2016-01-21 00:14:33 +03:00
|
|
|
GraphImpl()->AppendMessage(MakeUnique<Message>(this, aNewEvent));
|
2014-08-17 10:09:21 +04:00
|
|
|
}
|
|
|
|
|
2013-08-24 17:53:11 +04:00
|
|
|
void
|
|
|
|
SourceMediaStream::AddDirectListener(MediaStreamDirectListener* aListener)
|
|
|
|
{
|
2014-07-14 09:47:56 +04:00
|
|
|
bool wasEmpty;
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
wasEmpty = mDirectListeners.IsEmpty();
|
|
|
|
mDirectListeners.AppendElement(aListener);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wasEmpty) {
|
2014-08-17 10:09:21 +04:00
|
|
|
// Async
|
|
|
|
NotifyListenersEvent(MediaStreamListener::EVENT_HAS_DIRECT_LISTENERS);
|
2014-07-14 09:47:56 +04:00
|
|
|
}
|
2013-08-24 17:53:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SourceMediaStream::RemoveDirectListener(MediaStreamDirectListener* aListener)
|
|
|
|
{
|
2014-07-14 09:47:56 +04:00
|
|
|
bool isEmpty;
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
mDirectListeners.RemoveElement(aListener);
|
|
|
|
isEmpty = mDirectListeners.IsEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isEmpty) {
|
2014-08-17 10:09:21 +04:00
|
|
|
// Async
|
|
|
|
NotifyListenersEvent(MediaStreamListener::EVENT_HAS_NO_DIRECT_LISTENERS);
|
2014-07-14 09:47:56 +04:00
|
|
|
}
|
2013-08-24 17:53:11 +04:00
|
|
|
}
|
|
|
|
|
2016-03-03 19:28:37 +03:00
|
|
|
void
|
|
|
|
SourceMediaStream::AddDirectTrackListenerImpl(already_AddRefed<MediaStreamTrackDirectListener> aListener,
|
|
|
|
TrackID aTrackID)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(IsTrackIDExplicit(aTrackID));
|
|
|
|
TrackData* data;
|
|
|
|
bool found;
|
|
|
|
bool isAudio;
|
|
|
|
RefPtr<MediaStreamTrackDirectListener> listener = aListener;
|
|
|
|
STREAM_LOG(LogLevel::Debug, ("Adding direct track listener %p bound to track %d to source stream %p",
|
|
|
|
listener.get(), aTrackID, this));
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
data = FindDataForTrack(aTrackID);
|
|
|
|
found = !!data;
|
|
|
|
isAudio = found && data->mData->GetType() == MediaSegment::AUDIO;
|
|
|
|
if (found && isAudio) {
|
|
|
|
TrackBound<MediaStreamTrackDirectListener>* sourceListener =
|
|
|
|
mDirectTrackListeners.AppendElement();
|
|
|
|
sourceListener->mListener = listener;
|
|
|
|
sourceListener->mTrackID = aTrackID;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found) {
|
|
|
|
STREAM_LOG(LogLevel::Warning, ("Couldn't find source track for direct track listener %p",
|
|
|
|
listener.get()));
|
|
|
|
listener->NotifyDirectListenerInstalled(
|
|
|
|
MediaStreamTrackDirectListener::InstallationResult::TRACK_NOT_FOUND_AT_SOURCE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!isAudio) {
|
|
|
|
STREAM_LOG(LogLevel::Warning, ("Source track for direct track listener %p is not audio",
|
|
|
|
listener.get()));
|
|
|
|
listener->NotifyDirectListenerInstalled(
|
|
|
|
MediaStreamTrackDirectListener::InstallationResult::TRACK_TYPE_NOT_SUPPORTED);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
STREAM_LOG(LogLevel::Debug, ("Added direct track listener %p", listener.get()));
|
|
|
|
listener->NotifyDirectListenerInstalled(
|
|
|
|
MediaStreamTrackDirectListener::InstallationResult::SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SourceMediaStream::RemoveDirectTrackListenerImpl(MediaStreamTrackDirectListener* aListener,
|
|
|
|
TrackID aTrackID)
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
for (int32_t i = mDirectTrackListeners.Length() - 1; i >= 0; --i) {
|
|
|
|
const TrackBound<MediaStreamTrackDirectListener>& source =
|
|
|
|
mDirectTrackListeners[i];
|
|
|
|
if (source.mListener == aListener && source.mTrackID == aTrackID) {
|
|
|
|
aListener->NotifyDirectListenerUninstalled();
|
|
|
|
mDirectTrackListeners.RemoveElementAt(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-30 04:54:01 +03:00
|
|
|
StreamTime
|
|
|
|
SourceMediaStream::GetEndOfAppendedData(TrackID aID)
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
TrackData *track = FindDataForTrack(aID);
|
|
|
|
if (track) {
|
|
|
|
return track->mEndOfFlushedData + track->mData->GetDuration();
|
|
|
|
}
|
|
|
|
NS_ERROR("Track not found");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-30 07:11:40 +04:00
|
|
|
void
|
|
|
|
SourceMediaStream::EndTrack(TrackID aID)
|
|
|
|
{
|
2012-05-23 10:01:15 +04:00
|
|
|
MutexAutoLock lock(mMutex);
|
2015-01-28 07:40:51 +03:00
|
|
|
TrackData *track = FindDataForTrack(aID);
|
|
|
|
if (track) {
|
|
|
|
track->mCommands |= TRACK_END;
|
2012-05-23 10:01:15 +04:00
|
|
|
}
|
2014-07-25 01:09:22 +04:00
|
|
|
if (auto graph = GraphImpl()) {
|
2014-09-28 20:07:24 +04:00
|
|
|
graph->EnsureNextIteration();
|
2012-04-30 07:11:40 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SourceMediaStream::AdvanceKnownTracksTime(StreamTime aKnownTime)
|
|
|
|
{
|
2012-05-23 10:01:15 +04:00
|
|
|
MutexAutoLock lock(mMutex);
|
2014-05-19 00:26:54 +04:00
|
|
|
MOZ_ASSERT(aKnownTime >= mUpdateKnownTracksTime);
|
2012-05-23 10:01:15 +04:00
|
|
|
mUpdateKnownTracksTime = aKnownTime;
|
2014-07-25 01:09:22 +04:00
|
|
|
if (auto graph = GraphImpl()) {
|
2014-09-28 20:07:24 +04:00
|
|
|
graph->EnsureNextIteration();
|
2012-04-30 07:11:40 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-10-25 03:21:32 +04:00
|
|
|
SourceMediaStream::FinishWithLockHeld()
|
2012-04-30 07:11:40 +04:00
|
|
|
{
|
2013-04-08 16:03:33 +04:00
|
|
|
mMutex.AssertCurrentThreadOwns();
|
2012-05-23 10:01:15 +04:00
|
|
|
mUpdateFinished = true;
|
2014-07-25 01:09:22 +04:00
|
|
|
if (auto graph = GraphImpl()) {
|
2014-09-28 20:07:24 +04:00
|
|
|
graph->EnsureNextIteration();
|
2012-04-30 07:11:40 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-18 16:21:51 +03:00
|
|
|
void
|
|
|
|
SourceMediaStream::SetTrackEnabledImpl(TrackID aTrackID, bool aEnabled)
|
|
|
|
{
|
2016-04-25 06:59:39 +03:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
for (TrackBound<MediaStreamTrackDirectListener>& l: mDirectTrackListeners) {
|
|
|
|
if (l.mTrackID == aTrackID) {
|
|
|
|
bool oldEnabled = !mDisabledTrackIDs.Contains(aTrackID);
|
|
|
|
if (!oldEnabled && aEnabled) {
|
|
|
|
STREAM_LOG(LogLevel::Debug, ("SourceMediaStream %p track %d setting "
|
|
|
|
"direct listener enabled",
|
|
|
|
this, aTrackID));
|
|
|
|
l.mListener->DecreaseDisabled();
|
|
|
|
} else if (oldEnabled && !aEnabled) {
|
|
|
|
STREAM_LOG(LogLevel::Debug, ("SourceMediaStream %p track %d setting "
|
|
|
|
"direct listener disabled",
|
|
|
|
this, aTrackID));
|
|
|
|
l.mListener->IncreaseDisabled();
|
|
|
|
}
|
2016-03-18 16:21:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MediaStream::SetTrackEnabledImpl(aTrackID, aEnabled);
|
|
|
|
}
|
|
|
|
|
2012-10-25 03:21:32 +04:00
|
|
|
void
|
|
|
|
SourceMediaStream::EndAllTrackAndFinish()
|
|
|
|
{
|
2013-04-08 16:03:33 +04:00
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
for (uint32_t i = 0; i < mUpdateTracks.Length(); ++i) {
|
|
|
|
SourceMediaStream::TrackData* data = &mUpdateTracks[i];
|
|
|
|
data->mCommands |= TRACK_END;
|
2012-10-25 03:21:32 +04:00
|
|
|
}
|
2015-02-19 20:04:26 +03:00
|
|
|
mPendingTracks.Clear();
|
2012-10-25 03:21:32 +04:00
|
|
|
FinishWithLockHeld();
|
2014-07-14 09:47:56 +04:00
|
|
|
// we will call NotifyEvent() to let GetUserMedia know
|
2012-10-25 03:21:32 +04:00
|
|
|
}
|
|
|
|
|
2014-03-24 14:06:06 +04:00
|
|
|
void
|
|
|
|
SourceMediaStream::RegisterForAudioMixing()
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
mNeedsMixing = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
SourceMediaStream::NeedsMixing()
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
return mNeedsMixing;
|
|
|
|
}
|
|
|
|
|
2012-07-31 16:17:21 +04:00
|
|
|
void
|
|
|
|
MediaInputPort::Init()
|
|
|
|
{
|
2015-06-04 01:25:57 +03:00
|
|
|
STREAM_LOG(LogLevel::Debug, ("Adding MediaInputPort %p (from %p to %p) to the graph",
|
2013-11-21 07:02:42 +04:00
|
|
|
this, mSource, mDest));
|
2012-07-31 16:17:21 +04:00
|
|
|
mSource->AddConsumer(this);
|
|
|
|
mDest->AddInput(this);
|
2012-08-23 16:46:20 +04:00
|
|
|
// mPortCount decremented via MediaInputPort::Destroy's message
|
2012-07-31 16:17:21 +04:00
|
|
|
++mDest->GraphImpl()->mPortCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaInputPort::Disconnect()
|
|
|
|
{
|
2015-11-05 13:28:00 +03:00
|
|
|
GraphImpl()->AssertOnGraphThreadOrNotRunning();
|
2012-07-31 16:17:21 +04:00
|
|
|
NS_ASSERTION(!mSource == !mDest,
|
|
|
|
"mSource must either both be null or both non-null");
|
|
|
|
if (!mSource)
|
|
|
|
return;
|
|
|
|
|
|
|
|
mSource->RemoveConsumer(this);
|
2015-09-18 17:36:33 +03:00
|
|
|
mDest->RemoveInput(this);
|
2015-09-09 05:19:05 +03:00
|
|
|
mSource = nullptr;
|
2012-07-31 16:17:21 +04:00
|
|
|
mDest = nullptr;
|
2014-01-29 17:34:35 +04:00
|
|
|
|
|
|
|
GraphImpl()->SetStreamOrderDirty();
|
2012-07-31 16:17:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
MediaInputPort::InputInterval
|
|
|
|
MediaInputPort::GetNextInputInterval(GraphTime aTime)
|
|
|
|
{
|
|
|
|
InputInterval result = { GRAPH_TIME_MAX, GRAPH_TIME_MAX, false };
|
2015-09-04 09:44:43 +03:00
|
|
|
if (aTime >= mDest->mStartBlocking) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
result.mStart = aTime;
|
|
|
|
result.mEnd = mDest->mStartBlocking;
|
|
|
|
result.mInputIsBlocked = aTime >= mSource->mStartBlocking;
|
|
|
|
if (!result.mInputIsBlocked) {
|
|
|
|
result.mEnd = std::min(result.mEnd, mSource->mStartBlocking);
|
2012-07-31 16:17:21 +04:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaInputPort::Destroy()
|
|
|
|
{
|
|
|
|
class Message : public ControlMessage {
|
|
|
|
public:
|
2014-09-01 07:50:23 +04:00
|
|
|
explicit Message(MediaInputPort* aPort)
|
2012-08-23 16:46:20 +04:00
|
|
|
: ControlMessage(nullptr), mPort(aPort) {}
|
2016-01-18 06:50:29 +03:00
|
|
|
void Run() override
|
2012-07-31 16:17:21 +04:00
|
|
|
{
|
|
|
|
mPort->Disconnect();
|
2012-08-23 16:46:20 +04:00
|
|
|
--mPort->GraphImpl()->mPortCount;
|
2014-07-25 01:18:38 +04:00
|
|
|
mPort->SetGraphImpl(nullptr);
|
2012-07-31 16:17:21 +04:00
|
|
|
NS_RELEASE(mPort);
|
|
|
|
}
|
2016-01-18 06:50:29 +03:00
|
|
|
void RunDuringShutdown() override
|
2012-07-31 16:17:21 +04:00
|
|
|
{
|
|
|
|
Run();
|
|
|
|
}
|
|
|
|
MediaInputPort* mPort;
|
|
|
|
};
|
2016-01-21 00:14:33 +03:00
|
|
|
GraphImpl()->AppendMessage(MakeUnique<Message>(this));
|
2012-08-23 16:46:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
MediaStreamGraphImpl*
|
|
|
|
MediaInputPort::GraphImpl()
|
|
|
|
{
|
2013-02-01 23:49:58 +04:00
|
|
|
return mGraph;
|
2012-08-23 16:46:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
MediaStreamGraph*
|
|
|
|
MediaInputPort::Graph()
|
|
|
|
{
|
2013-02-01 23:49:58 +04:00
|
|
|
return mGraph;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaInputPort::SetGraphImpl(MediaStreamGraphImpl* aGraph)
|
|
|
|
{
|
2014-07-25 01:18:38 +04:00
|
|
|
MOZ_ASSERT(!mGraph || !aGraph, "Should only be set once");
|
2013-02-01 23:49:58 +04:00
|
|
|
mGraph = aGraph;
|
2012-07-31 16:17:21 +04:00
|
|
|
}
|
|
|
|
|
2015-09-30 04:31:53 +03:00
|
|
|
void
|
2016-04-22 18:14:26 +03:00
|
|
|
MediaInputPort::BlockSourceTrackIdImpl(TrackID aTrackId)
|
2015-09-30 04:31:53 +03:00
|
|
|
{
|
|
|
|
mBlockedTracks.AppendElement(aTrackId);
|
|
|
|
}
|
|
|
|
|
2016-03-16 18:00:34 +03:00
|
|
|
already_AddRefed<Pledge<bool>>
|
2016-04-22 18:14:26 +03:00
|
|
|
MediaInputPort::BlockSourceTrackId(TrackID aTrackId)
|
2015-09-30 04:31:53 +03:00
|
|
|
{
|
|
|
|
class Message : public ControlMessage {
|
|
|
|
public:
|
2016-03-16 18:00:34 +03:00
|
|
|
explicit Message(MediaInputPort* aPort,
|
|
|
|
TrackID aTrackId,
|
|
|
|
already_AddRefed<nsIRunnable> aRunnable)
|
2015-09-30 04:31:53 +03:00
|
|
|
: ControlMessage(aPort->GetDestination()),
|
2016-03-16 18:00:34 +03:00
|
|
|
mPort(aPort), mTrackId(aTrackId), mRunnable(aRunnable) {}
|
2016-01-18 06:50:29 +03:00
|
|
|
void Run() override
|
2015-09-30 04:31:53 +03:00
|
|
|
{
|
2016-04-22 18:14:26 +03:00
|
|
|
mPort->BlockSourceTrackIdImpl(mTrackId);
|
2016-03-16 18:00:34 +03:00
|
|
|
if (mRunnable) {
|
|
|
|
mStream->Graph()->DispatchToMainThreadAfterStreamStateUpdate(mRunnable.forget());
|
|
|
|
}
|
2015-09-30 04:31:53 +03:00
|
|
|
}
|
2016-01-18 06:50:29 +03:00
|
|
|
void RunDuringShutdown() override
|
2015-09-30 04:31:53 +03:00
|
|
|
{
|
|
|
|
Run();
|
|
|
|
}
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaInputPort> mPort;
|
2015-09-30 04:31:53 +03:00
|
|
|
TrackID mTrackId;
|
2016-03-16 18:00:34 +03:00
|
|
|
nsCOMPtr<nsIRunnable> mRunnable;
|
2015-09-30 04:31:53 +03:00
|
|
|
};
|
|
|
|
|
2016-01-05 05:16:22 +03:00
|
|
|
MOZ_ASSERT(IsTrackIDExplicit(aTrackId),
|
2015-09-30 04:31:53 +03:00
|
|
|
"Only explicit TrackID is allowed");
|
2016-03-16 18:00:34 +03:00
|
|
|
|
|
|
|
RefPtr<Pledge<bool>> pledge = new Pledge<bool>();
|
|
|
|
nsCOMPtr<nsIRunnable> runnable = NewRunnableFrom([pledge]() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
pledge->Resolve(true);
|
|
|
|
return NS_OK;
|
|
|
|
});
|
|
|
|
GraphImpl()->AppendMessage(MakeUnique<Message>(this, aTrackId, runnable.forget()));
|
|
|
|
return pledge.forget();
|
2015-09-30 04:31:53 +03:00
|
|
|
}
|
|
|
|
|
2012-11-23 02:25:05 +04:00
|
|
|
already_AddRefed<MediaInputPort>
|
2015-09-30 04:31:53 +03:00
|
|
|
ProcessedMediaStream::AllocateInputPort(MediaStream* aStream, TrackID aTrackID,
|
2016-01-05 05:16:22 +03:00
|
|
|
TrackID aDestTrackID,
|
2016-01-05 05:16:25 +03:00
|
|
|
uint16_t aInputNumber, uint16_t aOutputNumber,
|
|
|
|
nsTArray<TrackID>* aBlockedTracks)
|
2012-07-31 16:17:21 +04:00
|
|
|
{
|
2012-11-23 02:25:05 +04:00
|
|
|
// This method creates two references to the MediaInputPort: one for
|
|
|
|
// the main thread, and one for the MediaStreamGraph.
|
2012-07-31 16:17:21 +04:00
|
|
|
class Message : public ControlMessage {
|
|
|
|
public:
|
2014-09-01 07:50:23 +04:00
|
|
|
explicit Message(MediaInputPort* aPort)
|
2012-07-31 16:17:21 +04:00
|
|
|
: ControlMessage(aPort->GetDestination()),
|
|
|
|
mPort(aPort) {}
|
2016-01-18 06:50:29 +03:00
|
|
|
void Run() override
|
2012-07-31 16:17:21 +04:00
|
|
|
{
|
|
|
|
mPort->Init();
|
2012-11-23 02:25:05 +04:00
|
|
|
// The graph holds its reference implicitly
|
2014-01-29 17:34:35 +04:00
|
|
|
mPort->GraphImpl()->SetStreamOrderDirty();
|
2015-11-02 08:53:26 +03:00
|
|
|
Unused << mPort.forget();
|
2012-07-31 16:17:21 +04:00
|
|
|
}
|
2016-01-18 06:50:29 +03:00
|
|
|
void RunDuringShutdown() override
|
2013-05-25 18:01:08 +04:00
|
|
|
{
|
|
|
|
Run();
|
|
|
|
}
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaInputPort> mPort;
|
2012-07-31 16:17:21 +04:00
|
|
|
};
|
2015-09-30 04:31:53 +03:00
|
|
|
|
2015-12-03 07:38:34 +03:00
|
|
|
MOZ_ASSERT(aStream->GraphImpl() == GraphImpl());
|
2016-01-05 05:16:22 +03:00
|
|
|
MOZ_ASSERT(aTrackID == TRACK_ANY || IsTrackIDExplicit(aTrackID),
|
|
|
|
"Only TRACK_ANY and explicit ID are allowed for source track");
|
2016-01-05 05:16:22 +03:00
|
|
|
MOZ_ASSERT(aDestTrackID == TRACK_ANY || IsTrackIDExplicit(aDestTrackID),
|
|
|
|
"Only TRACK_ANY and explicit ID are allowed for destination track");
|
|
|
|
MOZ_ASSERT(aTrackID != TRACK_ANY || aDestTrackID == TRACK_ANY,
|
|
|
|
"Generic MediaInputPort cannot produce a single destination track");
|
|
|
|
RefPtr<MediaInputPort> port =
|
|
|
|
new MediaInputPort(aStream, aTrackID, this, aDestTrackID,
|
|
|
|
aInputNumber, aOutputNumber);
|
2016-01-05 05:16:25 +03:00
|
|
|
if (aBlockedTracks) {
|
|
|
|
for (TrackID trackID : *aBlockedTracks) {
|
2016-04-22 18:14:26 +03:00
|
|
|
port->BlockSourceTrackIdImpl(trackID);
|
2016-01-05 05:16:25 +03:00
|
|
|
}
|
|
|
|
}
|
2013-02-01 23:49:58 +04:00
|
|
|
port->SetGraphImpl(GraphImpl());
|
2016-01-21 00:14:33 +03:00
|
|
|
GraphImpl()->AppendMessage(MakeUnique<Message>(port));
|
2012-11-23 02:25:05 +04:00
|
|
|
return port.forget();
|
2012-07-31 16:17:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ProcessedMediaStream::Finish()
|
|
|
|
{
|
|
|
|
class Message : public ControlMessage {
|
|
|
|
public:
|
2014-09-01 07:50:23 +04:00
|
|
|
explicit Message(ProcessedMediaStream* aStream)
|
2012-07-31 16:17:21 +04:00
|
|
|
: ControlMessage(aStream) {}
|
2016-01-18 06:50:29 +03:00
|
|
|
void Run() override
|
2012-07-31 16:17:21 +04:00
|
|
|
{
|
|
|
|
mStream->GraphImpl()->FinishStream(mStream);
|
|
|
|
}
|
|
|
|
};
|
2016-01-21 00:14:33 +03:00
|
|
|
GraphImpl()->AppendMessage(MakeUnique<Message>(this));
|
2012-07-31 16:17:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ProcessedMediaStream::SetAutofinish(bool aAutofinish)
|
|
|
|
{
|
|
|
|
class Message : public ControlMessage {
|
|
|
|
public:
|
|
|
|
Message(ProcessedMediaStream* aStream, bool aAutofinish)
|
|
|
|
: ControlMessage(aStream), mAutofinish(aAutofinish) {}
|
2016-01-18 06:50:29 +03:00
|
|
|
void Run() override
|
2012-07-31 16:17:21 +04:00
|
|
|
{
|
2013-02-04 14:04:26 +04:00
|
|
|
static_cast<ProcessedMediaStream*>(mStream)->SetAutofinishImpl(mAutofinish);
|
2012-07-31 16:17:21 +04:00
|
|
|
}
|
|
|
|
bool mAutofinish;
|
|
|
|
};
|
2016-01-21 00:14:33 +03:00
|
|
|
GraphImpl()->AppendMessage(MakeUnique<Message>(this, aAutofinish));
|
2012-07-31 16:17:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ProcessedMediaStream::DestroyImpl()
|
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
for (int32_t i = mInputs.Length() - 1; i >= 0; --i) {
|
2012-07-31 16:17:21 +04:00
|
|
|
mInputs[i]->Disconnect();
|
|
|
|
}
|
|
|
|
MediaStream::DestroyImpl();
|
2014-07-25 00:36:07 +04:00
|
|
|
// The stream order is only important if there are connections, in which
|
|
|
|
// case MediaInputPort::Disconnect() called SetStreamOrderDirty().
|
2015-08-12 02:29:35 +03:00
|
|
|
// MediaStreamGraphImpl::RemoveStreamGraphThread() will also call
|
2014-07-25 00:36:07 +04:00
|
|
|
// SetStreamOrderDirty(), for other reasons.
|
2012-07-31 16:17:21 +04:00
|
|
|
}
|
2012-04-30 07:11:26 +04:00
|
|
|
|
2015-08-25 11:17:31 +03:00
|
|
|
MediaStreamGraphImpl::MediaStreamGraphImpl(GraphDriverType aDriverRequested,
|
2014-08-25 17:27:25 +04:00
|
|
|
TrackRate aSampleRate,
|
2014-08-26 19:02:08 +04:00
|
|
|
dom::AudioChannel aChannel)
|
2014-09-18 03:50:01 +04:00
|
|
|
: MediaStreamGraph(aSampleRate)
|
2012-07-31 16:17:21 +04:00
|
|
|
, mPortCount(0)
|
2016-01-21 19:51:36 +03:00
|
|
|
, mInputWanted(false)
|
2016-03-08 20:11:09 +03:00
|
|
|
, mInputDeviceID(-1)
|
2016-01-21 19:51:36 +03:00
|
|
|
, mOutputWanted(true)
|
2016-03-08 20:11:09 +03:00
|
|
|
, mOutputDeviceID(-1)
|
2014-09-28 20:07:25 +04:00
|
|
|
, mNeedAnotherIteration(false)
|
|
|
|
, mGraphDriverAsleep(false)
|
2014-08-26 19:01:33 +04:00
|
|
|
, mMonitor("MediaStreamGraphImpl")
|
2012-04-30 07:11:26 +04:00
|
|
|
, mLifecycleState(LIFECYCLE_THREAD_NOT_STARTED)
|
2013-12-09 09:08:02 +04:00
|
|
|
, mEndTime(GRAPH_TIME_MAX)
|
2012-04-30 07:11:26 +04:00
|
|
|
, mForceShutDown(false)
|
|
|
|
, mPostedRunInStableStateEvent(false)
|
|
|
|
, mDetectedNotRunning(false)
|
|
|
|
, mPostedRunInStableState(false)
|
2015-08-25 11:17:31 +03:00
|
|
|
, mRealtime(aDriverRequested != OFFLINE_THREAD_DRIVER)
|
2013-05-17 03:30:41 +04:00
|
|
|
, mNonRealtimeProcessing(false)
|
2013-09-13 20:12:07 +04:00
|
|
|
, mStreamOrderDirty(false)
|
2013-09-25 06:10:24 +04:00
|
|
|
, mLatencyLog(AsyncLatencyLogger::Get())
|
2014-09-29 01:27:00 +04:00
|
|
|
#ifdef MOZ_WEBRTC
|
2014-08-26 19:02:31 +04:00
|
|
|
, mFarendObserverRef(nullptr)
|
2014-09-09 20:23:01 +04:00
|
|
|
#endif
|
2014-04-13 22:08:10 +04:00
|
|
|
, mMemoryReportMonitor("MSGIMemory")
|
2015-01-07 08:39:46 +03:00
|
|
|
, mSelfRef(this)
|
2014-04-13 22:08:10 +04:00
|
|
|
, mAudioStreamSizes()
|
|
|
|
, mNeedsMemoryReport(false)
|
2014-07-02 10:04:54 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
, mCanRunMessagesSynchronously(false)
|
|
|
|
#endif
|
2015-10-23 06:43:15 +03:00
|
|
|
, mAudioChannel(aChannel)
|
2012-04-30 07:11:26 +04:00
|
|
|
{
|
2014-04-25 18:09:30 +04:00
|
|
|
if (mRealtime) {
|
2015-08-25 11:17:31 +03:00
|
|
|
if (aDriverRequested == AUDIO_THREAD_DRIVER) {
|
2015-10-23 06:43:15 +03:00
|
|
|
AudioCallbackDriver* driver = new AudioCallbackDriver(this);
|
2014-08-26 19:02:28 +04:00
|
|
|
mDriver = driver;
|
2014-08-26 19:01:33 +04:00
|
|
|
} else {
|
2014-08-26 19:02:28 +04:00
|
|
|
mDriver = new SystemClockDriver(this);
|
2014-08-26 19:01:33 +04:00
|
|
|
}
|
2014-04-25 18:09:30 +04:00
|
|
|
} else {
|
2015-02-09 10:23:34 +03:00
|
|
|
mDriver = new OfflineClockDriver(this, MEDIA_GRAPH_TARGET_PERIOD_MS);
|
2014-04-25 18:09:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
mLastMainThreadUpdate = TimeStamp::Now();
|
2014-04-13 22:08:10 +04:00
|
|
|
|
|
|
|
RegisterWeakMemoryReporter(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStreamGraphImpl::Destroy()
|
|
|
|
{
|
|
|
|
// First unregister from memory reporting.
|
|
|
|
UnregisterWeakMemoryReporter(this);
|
|
|
|
|
|
|
|
// Clear the self reference which will destroy this instance.
|
|
|
|
mSelfRef = nullptr;
|
2012-04-30 07:11:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
MediaStreamGraph*
|
2015-08-25 11:17:31 +03:00
|
|
|
MediaStreamGraph::GetInstance(MediaStreamGraph::GraphDriverType aGraphDriverRequested,
|
2015-02-09 10:23:34 +03:00
|
|
|
dom::AudioChannel aChannel)
|
2012-04-30 07:11:26 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Main thread only");
|
|
|
|
|
2014-11-17 19:07:55 +03:00
|
|
|
uint32_t channel = static_cast<uint32_t>(aChannel);
|
|
|
|
MediaStreamGraphImpl* graph = nullptr;
|
|
|
|
|
|
|
|
if (!gGraphs.Get(channel, &graph)) {
|
2016-01-22 21:49:54 +03:00
|
|
|
if (!gMediaStreamGraphShutdownBlocker) {
|
|
|
|
|
|
|
|
class Blocker : public media::ShutdownBlocker
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Blocker()
|
|
|
|
: media::ShutdownBlocker(NS_LITERAL_STRING(
|
|
|
|
"MediaStreamGraph shutdown: blocking on msg thread")) {}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
BlockShutdown(nsIAsyncShutdownClient* aProfileBeforeChange) override
|
|
|
|
{
|
|
|
|
// Distribute the global async shutdown blocker in a ticket. If there
|
|
|
|
// are zero graphs then shutdown is unblocked when we go out of scope.
|
|
|
|
RefPtr<MediaStreamGraphImpl::ShutdownTicket> ticket =
|
|
|
|
new MediaStreamGraphImpl::ShutdownTicket(gMediaStreamGraphShutdownBlocker.get());
|
|
|
|
gMediaStreamGraphShutdownBlocker = nullptr;
|
|
|
|
|
|
|
|
for (auto iter = gGraphs.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
iter.UserData()->ForceShutDown(ticket);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
gMediaStreamGraphShutdownBlocker = new Blocker();
|
|
|
|
nsCOMPtr<nsIAsyncShutdownClient> barrier = MediaStreamGraphImpl::GetShutdownBarrier();
|
|
|
|
nsresult rv = barrier->
|
|
|
|
AddBlocker(gMediaStreamGraphShutdownBlocker,
|
|
|
|
NS_LITERAL_STRING(__FILE__), __LINE__,
|
|
|
|
NS_LITERAL_STRING("MediaStreamGraph shutdown"));
|
|
|
|
MOZ_RELEASE_ASSERT(NS_SUCCEEDED(rv));
|
2012-04-30 07:11:26 +04:00
|
|
|
}
|
|
|
|
|
2014-08-25 17:26:09 +04:00
|
|
|
CubebUtils::InitPreferredSampleRate();
|
2014-04-13 22:08:10 +04:00
|
|
|
|
2015-08-25 11:17:31 +03:00
|
|
|
graph = new MediaStreamGraphImpl(aGraphDriverRequested,
|
|
|
|
CubebUtils::PreferredSampleRate(),
|
|
|
|
aChannel);
|
|
|
|
|
2014-11-17 19:07:55 +03:00
|
|
|
gGraphs.Put(channel, graph);
|
2014-03-24 14:06:05 +04:00
|
|
|
|
2015-08-25 11:17:31 +03:00
|
|
|
STREAM_LOG(LogLevel::Debug,
|
|
|
|
("Starting up MediaStreamGraph %p for channel %s",
|
2015-12-23 05:44:31 +03:00
|
|
|
graph, AudioChannelValues::strings[channel].value));
|
2012-04-30 07:11:26 +04:00
|
|
|
}
|
|
|
|
|
2014-11-17 19:07:55 +03:00
|
|
|
return graph;
|
2012-04-30 07:11:26 +04:00
|
|
|
}
|
|
|
|
|
2013-05-08 15:44:07 +04:00
|
|
|
MediaStreamGraph*
|
2014-04-23 13:20:56 +04:00
|
|
|
MediaStreamGraph::CreateNonRealtimeInstance(TrackRate aSampleRate)
|
2013-05-08 15:44:07 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Main thread only");
|
|
|
|
|
2015-08-25 11:17:31 +03:00
|
|
|
MediaStreamGraphImpl* graph =
|
|
|
|
new MediaStreamGraphImpl(OFFLINE_THREAD_DRIVER,
|
|
|
|
aSampleRate,
|
|
|
|
AudioChannel::Normal);
|
2014-04-13 22:08:10 +04:00
|
|
|
|
2015-06-04 01:25:57 +03:00
|
|
|
STREAM_LOG(LogLevel::Debug, ("Starting up Offline MediaStreamGraph %p", graph));
|
2014-08-26 19:01:33 +04:00
|
|
|
|
2013-05-08 15:44:07 +04:00
|
|
|
return graph;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStreamGraph::DestroyNonRealtimeInstance(MediaStreamGraph* aGraph)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Main thread only");
|
2013-09-10 09:05:22 +04:00
|
|
|
MOZ_ASSERT(aGraph->IsNonRealtime(), "Should not destroy the global graph here");
|
2013-05-08 15:44:07 +04:00
|
|
|
|
|
|
|
MediaStreamGraphImpl* graph = static_cast<MediaStreamGraphImpl*>(aGraph);
|
2013-09-10 09:05:22 +04:00
|
|
|
if (graph->mForceShutDown)
|
|
|
|
return; // already done
|
|
|
|
|
2013-05-25 17:59:59 +04:00
|
|
|
if (!graph->mNonRealtimeProcessing) {
|
|
|
|
// Start the graph, but don't produce anything
|
2014-11-19 13:21:38 +03:00
|
|
|
graph->StartNonRealtimeProcessing(0);
|
2013-05-25 17:59:59 +04:00
|
|
|
}
|
2016-01-22 21:49:54 +03:00
|
|
|
graph->ForceShutDown(nullptr);
|
2013-05-08 15:44:07 +04:00
|
|
|
}
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(MediaStreamGraphImpl, nsIMemoryReporter)
|
2014-04-13 22:08:10 +04:00
|
|
|
|
|
|
|
struct ArrayClearer
|
|
|
|
{
|
2014-09-01 07:50:23 +04:00
|
|
|
explicit ArrayClearer(nsTArray<AudioNodeSizes>& aArray) : mArray(aArray) {}
|
2014-04-13 22:08:10 +04:00
|
|
|
~ArrayClearer() { mArray.Clear(); }
|
|
|
|
nsTArray<AudioNodeSizes>& mArray;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MediaStreamGraphImpl::CollectReports(nsIHandleReportCallback* aHandleReport,
|
2014-05-21 10:06:54 +04:00
|
|
|
nsISupports* aData, bool aAnonymize)
|
2014-04-13 22:08:10 +04:00
|
|
|
{
|
|
|
|
// Clears out the report array after we're done with it.
|
|
|
|
ArrayClearer reportCleanup(mAudioStreamSizes);
|
|
|
|
|
|
|
|
{
|
|
|
|
MonitorAutoLock memoryReportLock(mMemoryReportMonitor);
|
|
|
|
mNeedsMemoryReport = true;
|
|
|
|
|
2014-09-29 22:46:29 +04:00
|
|
|
{
|
2014-10-20 20:06:10 +04:00
|
|
|
// Wake up the MSG thread if it's real time (Offline graphs can't be
|
|
|
|
// sleeping).
|
2014-09-29 22:46:29 +04:00
|
|
|
MonitorAutoLock monitorLock(mMonitor);
|
2014-10-20 20:06:10 +04:00
|
|
|
if (!CurrentDriver()->AsOfflineClockDriver()) {
|
|
|
|
CurrentDriver()->WakeUp();
|
|
|
|
}
|
2014-09-29 22:46:29 +04:00
|
|
|
}
|
2014-04-13 22:08:10 +04:00
|
|
|
|
2014-07-10 04:22:37 +04:00
|
|
|
if (mLifecycleState >= LIFECYCLE_WAITING_FOR_THREAD_SHUTDOWN) {
|
|
|
|
// Shutting down, nothing to report.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for up to one second for the report to complete.
|
2014-04-13 22:08:10 +04:00
|
|
|
nsresult rv;
|
2014-07-10 04:22:37 +04:00
|
|
|
const PRIntervalTime kMaxWait = PR_SecondsToInterval(1);
|
|
|
|
while ((rv = memoryReportLock.Wait(kMaxWait)) != NS_OK) {
|
2014-04-13 22:08:10 +04:00
|
|
|
if (PR_GetError() != PR_PENDING_INTERRUPT_ERROR) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define REPORT(_path, _amount, _desc) \
|
|
|
|
do { \
|
|
|
|
nsresult rv; \
|
|
|
|
rv = aHandleReport->Callback(EmptyCString(), _path, \
|
|
|
|
KIND_HEAP, UNITS_BYTES, _amount, \
|
|
|
|
NS_LITERAL_CSTRING(_desc), aData); \
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
for (size_t i = 0; i < mAudioStreamSizes.Length(); i++) {
|
|
|
|
const AudioNodeSizes& usage = mAudioStreamSizes[i];
|
2014-06-20 21:29:10 +04:00
|
|
|
const char* const nodeType = usage.mNodeType.IsEmpty() ?
|
|
|
|
"<unknown>" : usage.mNodeType.get();
|
2014-04-13 22:08:10 +04:00
|
|
|
|
|
|
|
nsPrintfCString domNodePath("explicit/webaudio/audio-node/%s/dom-nodes",
|
2014-05-21 10:06:54 +04:00
|
|
|
nodeType);
|
2014-04-13 22:08:10 +04:00
|
|
|
REPORT(domNodePath, usage.mDomNode,
|
|
|
|
"Memory used by AudioNode DOM objects (Web Audio).");
|
|
|
|
|
|
|
|
nsPrintfCString enginePath("explicit/webaudio/audio-node/%s/engine-objects",
|
2014-05-21 10:06:54 +04:00
|
|
|
nodeType);
|
2014-04-13 22:08:10 +04:00
|
|
|
REPORT(enginePath, usage.mEngine,
|
|
|
|
"Memory used by AudioNode engine objects (Web Audio).");
|
|
|
|
|
|
|
|
nsPrintfCString streamPath("explicit/webaudio/audio-node/%s/stream-objects",
|
2014-05-21 10:06:54 +04:00
|
|
|
nodeType);
|
2014-04-13 22:08:10 +04:00
|
|
|
REPORT(streamPath, usage.mStream,
|
|
|
|
"Memory used by AudioNode stream objects (Web Audio).");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-10-10 06:30:28 +04:00
|
|
|
size_t hrtfLoaders = WebCore::HRTFDatabaseLoader::sizeOfLoaders(MallocSizeOf);
|
|
|
|
if (hrtfLoaders) {
|
|
|
|
|
|
|
|
REPORT(NS_LITERAL_CSTRING(
|
|
|
|
"explicit/webaudio/audio-node/PannerNode/hrtf-databases"),
|
|
|
|
hrtfLoaders,
|
|
|
|
"Memory used by PannerNode databases (Web Audio).");
|
|
|
|
}
|
|
|
|
|
2014-04-13 22:08:10 +04:00
|
|
|
#undef REPORT
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-04-30 07:11:40 +04:00
|
|
|
SourceMediaStream*
|
2013-02-15 12:01:58 +04:00
|
|
|
MediaStreamGraph::CreateSourceStream(DOMMediaStream* aWrapper)
|
2012-04-30 07:11:40 +04:00
|
|
|
{
|
|
|
|
SourceMediaStream* stream = new SourceMediaStream(aWrapper);
|
2015-08-12 02:46:56 +03:00
|
|
|
AddStream(stream);
|
2012-04-30 07:11:40 +04:00
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
2012-07-31 16:17:21 +04:00
|
|
|
ProcessedMediaStream*
|
2013-02-15 12:01:58 +04:00
|
|
|
MediaStreamGraph::CreateTrackUnionStream(DOMMediaStream* aWrapper)
|
2012-07-31 16:17:21 +04:00
|
|
|
{
|
|
|
|
TrackUnionStream* stream = new TrackUnionStream(aWrapper);
|
2015-08-12 02:46:56 +03:00
|
|
|
AddStream(stream);
|
2012-07-31 16:17:21 +04:00
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
2015-07-24 15:28:16 +03:00
|
|
|
ProcessedMediaStream*
|
2015-09-30 04:31:53 +03:00
|
|
|
MediaStreamGraph::CreateAudioCaptureStream(DOMMediaStream* aWrapper,
|
|
|
|
TrackID aTrackId)
|
2015-07-24 15:28:16 +03:00
|
|
|
{
|
2015-09-30 04:31:53 +03:00
|
|
|
AudioCaptureStream* stream = new AudioCaptureStream(aWrapper, aTrackId);
|
2015-08-12 02:46:56 +03:00
|
|
|
AddStream(stream);
|
2015-07-24 15:28:16 +03:00
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
2015-08-12 02:46:56 +03:00
|
|
|
void
|
2015-10-22 12:14:46 +03:00
|
|
|
MediaStreamGraph::AddStream(MediaStream* aStream)
|
2015-08-12 02:46:56 +03:00
|
|
|
{
|
|
|
|
NS_ADDREF(aStream);
|
|
|
|
MediaStreamGraphImpl* graph = static_cast<MediaStreamGraphImpl*>(this);
|
|
|
|
aStream->SetGraphImpl(graph);
|
2016-01-21 00:14:33 +03:00
|
|
|
graph->AppendMessage(MakeUnique<CreateMessage>(aStream));
|
2015-08-12 02:46:56 +03:00
|
|
|
}
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class GraphStartedRunnable final : public Runnable
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
GraphStartedRunnable(AudioNodeStream* aStream, MediaStreamGraph* aGraph)
|
|
|
|
: mStream(aStream)
|
|
|
|
, mGraph(aGraph)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
NS_IMETHOD Run() {
|
|
|
|
mGraph->NotifyWhenGraphStarted(mStream);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<AudioNodeStream> mStream;
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
MediaStreamGraph* mGraph;
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStreamGraph::NotifyWhenGraphStarted(AudioNodeStream* aStream)
|
|
|
|
{
|
2015-04-29 12:02:55 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
class GraphStartedNotificationControlMessage : public ControlMessage
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit GraphStartedNotificationControlMessage(AudioNodeStream* aStream)
|
|
|
|
: ControlMessage(aStream)
|
|
|
|
{
|
|
|
|
}
|
2016-01-18 06:50:29 +03:00
|
|
|
void Run() override
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
{
|
|
|
|
// This runs on the graph thread, so when this runs, and the current
|
|
|
|
// driver is an AudioCallbackDriver, we know the audio hardware is
|
|
|
|
// started. If not, we are going to switch soon, keep reposting this
|
|
|
|
// ControlMessage.
|
|
|
|
MediaStreamGraphImpl* graphImpl = mStream->GraphImpl();
|
|
|
|
if (graphImpl->CurrentDriver()->AsAudioCallbackDriver()) {
|
|
|
|
nsCOMPtr<nsIRunnable> event = new dom::StateChangeTask(
|
|
|
|
mStream->AsAudioNodeStream(), nullptr, AudioContextState::Running);
|
2015-08-20 00:52:56 +03:00
|
|
|
NS_DispatchToMainThread(event.forget());
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
} else {
|
|
|
|
nsCOMPtr<nsIRunnable> event = new GraphStartedRunnable(
|
|
|
|
mStream->AsAudioNodeStream(), mStream->Graph());
|
2015-08-20 00:52:56 +03:00
|
|
|
NS_DispatchToMainThread(event.forget());
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
}
|
|
|
|
}
|
2016-01-18 06:50:29 +03:00
|
|
|
void RunDuringShutdown() override
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-04-29 12:02:55 +03:00
|
|
|
if (!aStream->IsDestroyed()) {
|
|
|
|
MediaStreamGraphImpl* graphImpl = static_cast<MediaStreamGraphImpl*>(this);
|
2016-01-21 00:14:33 +03:00
|
|
|
graphImpl->AppendMessage(MakeUnique<GraphStartedNotificationControlMessage>(aStream));
|
2015-04-29 12:02:55 +03:00
|
|
|
}
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-09-03 14:54:00 +03:00
|
|
|
MediaStreamGraphImpl::IncrementSuspendCount(MediaStream* aStream)
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
{
|
2015-09-03 14:54:00 +03:00
|
|
|
if (!aStream->IsSuspended()) {
|
|
|
|
MOZ_ASSERT(mStreams.Contains(aStream));
|
|
|
|
mStreams.RemoveElement(aStream);
|
|
|
|
mSuspendedStreams.AppendElement(aStream);
|
|
|
|
SetStreamOrderDirty();
|
|
|
|
}
|
|
|
|
aStream->IncrementSuspendCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStreamGraphImpl::DecrementSuspendCount(MediaStream* aStream)
|
|
|
|
{
|
|
|
|
bool wasSuspended = aStream->IsSuspended();
|
|
|
|
aStream->DecrementSuspendCount();
|
|
|
|
if (wasSuspended && !aStream->IsSuspended()) {
|
|
|
|
MOZ_ASSERT(mSuspendedStreams.Contains(aStream));
|
|
|
|
mSuspendedStreams.RemoveElement(aStream);
|
|
|
|
mStreams.AppendElement(aStream);
|
|
|
|
ProcessedMediaStream* ps = aStream->AsProcessedStream();
|
2015-09-08 07:18:15 +03:00
|
|
|
if (ps) {
|
|
|
|
ps->mCycleMarker = NOT_VISITED;
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
}
|
2015-09-03 14:54:00 +03:00
|
|
|
SetStreamOrderDirty();
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-09-16 07:15:21 +03:00
|
|
|
MediaStreamGraphImpl::SuspendOrResumeStreams(AudioContextOperation aAudioContextOperation,
|
|
|
|
const nsTArray<MediaStream*>& aStreamSet)
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
{
|
|
|
|
// For our purpose, Suspend and Close are equivalent: we want to remove the
|
|
|
|
// streams from the set of streams that are going to be processed.
|
2015-09-16 07:15:21 +03:00
|
|
|
for (MediaStream* stream : aStreamSet) {
|
|
|
|
if (aAudioContextOperation == AudioContextOperation::Resume) {
|
2015-09-03 14:54:00 +03:00
|
|
|
DecrementSuspendCount(stream);
|
2015-09-16 07:15:21 +03:00
|
|
|
} else {
|
2015-09-03 14:54:00 +03:00
|
|
|
IncrementSuspendCount(stream);
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
}
|
|
|
|
}
|
2015-06-04 01:25:57 +03:00
|
|
|
STREAM_LOG(LogLevel::Debug, ("Moving streams between suspended and running"
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
"state: mStreams: %d, mSuspendedStreams: %d\n", mStreams.Length(),
|
|
|
|
mSuspendedStreams.Length()));
|
|
|
|
#ifdef DEBUG
|
|
|
|
// The intersection of the two arrays should be null.
|
|
|
|
for (uint32_t i = 0; i < mStreams.Length(); i++) {
|
|
|
|
for (uint32_t j = 0; j < mSuspendedStreams.Length(); j++) {
|
|
|
|
MOZ_ASSERT(
|
|
|
|
mStreams[i] != mSuspendedStreams[j],
|
|
|
|
"The suspended stream set and running stream set are not disjoint.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStreamGraphImpl::AudioContextOperationCompleted(MediaStream* aStream,
|
|
|
|
void* aPromise,
|
|
|
|
AudioContextOperation aOperation)
|
|
|
|
{
|
|
|
|
// This can be called from the thread created to do cubeb operation, or the
|
|
|
|
// MSG thread. The pointers passed back here are refcounted, so are still
|
|
|
|
// alive.
|
|
|
|
MonitorAutoLock lock(mMonitor);
|
|
|
|
|
|
|
|
AudioContextState state;
|
|
|
|
switch (aOperation) {
|
2015-09-24 23:49:03 +03:00
|
|
|
case AudioContextOperation::Suspend:
|
|
|
|
state = AudioContextState::Suspended;
|
|
|
|
break;
|
|
|
|
case AudioContextOperation::Resume:
|
|
|
|
state = AudioContextState::Running;
|
|
|
|
break;
|
|
|
|
case AudioContextOperation::Close:
|
|
|
|
state = AudioContextState::Closed;
|
|
|
|
break;
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
default: MOZ_CRASH("Not handled.");
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> event = new dom::StateChangeTask(
|
|
|
|
aStream->AsAudioNodeStream(), aPromise, state);
|
2015-08-20 00:52:56 +03:00
|
|
|
NS_DispatchToMainThread(event.forget());
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-09-16 07:15:21 +03:00
|
|
|
MediaStreamGraphImpl::ApplyAudioContextOperationImpl(
|
|
|
|
MediaStream* aDestinationStream, const nsTArray<MediaStream*>& aStreams,
|
|
|
|
AudioContextOperation aOperation, void* aPromise)
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(CurrentDriver()->OnThread());
|
|
|
|
|
2015-09-16 07:15:21 +03:00
|
|
|
SuspendOrResumeStreams(aOperation, aStreams);
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
|
2015-12-01 13:48:02 +03:00
|
|
|
bool switching = false;
|
|
|
|
GraphDriver* nextDriver = nullptr;
|
|
|
|
{
|
|
|
|
MonitorAutoLock lock(mMonitor);
|
|
|
|
switching = CurrentDriver()->Switching();
|
|
|
|
if (switching) {
|
|
|
|
nextDriver = CurrentDriver()->NextDriver();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
// If we have suspended the last AudioContext, and we don't have other
|
|
|
|
// streams that have audio, this graph will automatically switch to a
|
|
|
|
// SystemCallbackDriver, because it can't find a MediaStream that has an audio
|
2015-07-13 20:16:53 +03:00
|
|
|
// track. When resuming, force switching to an AudioCallbackDriver (if we're
|
|
|
|
// not already switching). It would have happened at the next iteration
|
|
|
|
// anyways, but doing this now save some time.
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
if (aOperation == AudioContextOperation::Resume) {
|
|
|
|
if (!CurrentDriver()->AsAudioCallbackDriver()) {
|
2015-07-13 20:16:53 +03:00
|
|
|
AudioCallbackDriver* driver;
|
2015-12-01 13:48:02 +03:00
|
|
|
if (switching) {
|
|
|
|
MOZ_ASSERT(nextDriver->AsAudioCallbackDriver());
|
|
|
|
driver = nextDriver->AsAudioCallbackDriver();
|
2015-07-13 20:16:53 +03:00
|
|
|
} else {
|
|
|
|
driver = new AudioCallbackDriver(this);
|
2015-12-01 13:48:02 +03:00
|
|
|
MonitorAutoLock lock(mMonitor);
|
2015-07-13 20:16:53 +03:00
|
|
|
CurrentDriver()->SwitchAtNextIteration(driver);
|
|
|
|
}
|
2015-09-16 07:15:21 +03:00
|
|
|
driver->EnqueueStreamAndPromiseForOperation(aDestinationStream,
|
|
|
|
aPromise, aOperation);
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
} else {
|
|
|
|
// We are resuming a context, but we are already using an
|
|
|
|
// AudioCallbackDriver, we can resolve the promise now.
|
2015-09-16 07:15:21 +03:00
|
|
|
AudioContextOperationCompleted(aDestinationStream, aPromise, aOperation);
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Close, suspend: check if we are going to switch to a
|
|
|
|
// SystemAudioCallbackDriver, and pass the promise to the AudioCallbackDriver
|
|
|
|
// if that's the case, so it can notify the content.
|
|
|
|
// This is the same logic as in UpdateStreamOrder, but it's simpler to have it
|
|
|
|
// here as well so we don't have to store the Promise(s) on the Graph.
|
|
|
|
if (aOperation != AudioContextOperation::Resume) {
|
2016-03-08 20:11:08 +03:00
|
|
|
bool shouldAEC = false;
|
|
|
|
bool audioTrackPresent = AudioTrackPresent(shouldAEC);
|
|
|
|
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
if (!audioTrackPresent && CurrentDriver()->AsAudioCallbackDriver()) {
|
|
|
|
CurrentDriver()->AsAudioCallbackDriver()->
|
2015-09-16 07:15:21 +03:00
|
|
|
EnqueueStreamAndPromiseForOperation(aDestinationStream, aPromise,
|
|
|
|
aOperation);
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
|
2015-07-13 20:16:53 +03:00
|
|
|
SystemClockDriver* driver;
|
2015-12-01 13:48:02 +03:00
|
|
|
if (nextDriver) {
|
|
|
|
MOZ_ASSERT(!nextDriver->AsAudioCallbackDriver());
|
2015-07-13 20:16:53 +03:00
|
|
|
} else {
|
|
|
|
driver = new SystemClockDriver(this);
|
2015-12-01 13:48:02 +03:00
|
|
|
MonitorAutoLock lock(mMonitor);
|
2015-07-13 20:16:53 +03:00
|
|
|
CurrentDriver()->SwitchAtNextIteration(driver);
|
|
|
|
}
|
2015-07-24 16:09:21 +03:00
|
|
|
// We are closing or suspending an AudioContext, but we just got resumed.
|
|
|
|
// Queue the operation on the next driver so that the ordering is
|
|
|
|
// preserved.
|
2015-12-01 13:48:02 +03:00
|
|
|
} else if (!audioTrackPresent && switching) {
|
|
|
|
MOZ_ASSERT(nextDriver->AsAudioCallbackDriver());
|
|
|
|
nextDriver->AsAudioCallbackDriver()->
|
2015-09-16 07:15:21 +03:00
|
|
|
EnqueueStreamAndPromiseForOperation(aDestinationStream, aPromise,
|
|
|
|
aOperation);
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
} else {
|
|
|
|
// We are closing or suspending an AudioContext, but something else is
|
|
|
|
// using the audio stream, we can resolve the promise now.
|
2015-09-16 07:15:21 +03:00
|
|
|
AudioContextOperationCompleted(aDestinationStream, aPromise, aOperation);
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-09-16 07:15:21 +03:00
|
|
|
MediaStreamGraph::ApplyAudioContextOperation(MediaStream* aDestinationStream,
|
|
|
|
const nsTArray<MediaStream*>& aStreams,
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
AudioContextOperation aOperation,
|
|
|
|
void* aPromise)
|
|
|
|
{
|
|
|
|
class AudioContextOperationControlMessage : public ControlMessage
|
|
|
|
{
|
|
|
|
public:
|
2015-09-16 07:15:21 +03:00
|
|
|
AudioContextOperationControlMessage(MediaStream* aDestinationStream,
|
|
|
|
const nsTArray<MediaStream*>& aStreams,
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
AudioContextOperation aOperation,
|
|
|
|
void* aPromise)
|
2015-09-16 07:15:21 +03:00
|
|
|
: ControlMessage(aDestinationStream)
|
|
|
|
, mStreams(aStreams)
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
, mAudioContextOperation(aOperation)
|
|
|
|
, mPromise(aPromise)
|
|
|
|
{
|
|
|
|
}
|
2016-01-18 06:50:29 +03:00
|
|
|
void Run() override
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
{
|
2015-09-16 07:15:21 +03:00
|
|
|
mStream->GraphImpl()->ApplyAudioContextOperationImpl(mStream,
|
|
|
|
mStreams, mAudioContextOperation, mPromise);
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
}
|
2016-01-18 06:50:29 +03:00
|
|
|
void RunDuringShutdown() override
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(false, "We should be reviving the graph?");
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-09-16 07:15:21 +03:00
|
|
|
// We don't need strong references here for the same reason ControlMessage
|
|
|
|
// doesn't.
|
|
|
|
nsTArray<MediaStream*> mStreams;
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
AudioContextOperation mAudioContextOperation;
|
|
|
|
void* mPromise;
|
|
|
|
};
|
|
|
|
|
|
|
|
MediaStreamGraphImpl* graphImpl = static_cast<MediaStreamGraphImpl*>(this);
|
|
|
|
graphImpl->AppendMessage(
|
2016-01-21 00:14:33 +03:00
|
|
|
MakeUnique<AudioContextOperationControlMessage>(aDestinationStream, aStreams,
|
|
|
|
aOperation, aPromise));
|
Bug 1094764 - Implement AudioContext.suspend and friends. r=roc,ehsan
- Relevant spec text:
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-suspend-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-resume-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-close-Promise
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-state
- http://webaudio.github.io/web-audio-api/#widl-AudioContext-onstatechange
- In a couple words, the behavior we want:
- Closed context cannot have new nodes created, but can do decodeAudioData,
and create buffers, and such.
- OfflineAudioContexts don't support those methods, transitions happen at
startRendering and at the end of processing. onstatechange is used to make
this observable.
- (regular) AudioContexts support those methods. The promises and
onstatechange should be resolved/called when the operation has actually
completed on the rendering thread. Once a context has been closed, it
cannot transition back to "running". An AudioContext switches to "running"
when the audio callback start running, this allow authors to know how long
the audio stack takes to start running.
- MediaStreams that feed in/go out of a suspended graph should respectively
not buffer at the graph input, and output silence
- suspended context should not be doing much on the CPU, and we should try
to pause audio streams if we can (this behaviour is the main reason we need
this in the first place, for saving battery on mobile, and CPU on all
platforms)
- Now, the implementation:
- AudioNodeStreams are now tagged with a context id, to be able to operate
on all the streams of a given AudioContext on the Graph thread without
having to go and lock everytime to touch the AudioContext. This happens in
the AudioNodeStream ctor. IDs are of course constant for the lifetime of the
node.
- When an AudioContext goes into suspended mode, streams for this
AudioContext are moved out of the mStreams array to a second array,
mSuspendedStreams. Streams in mSuspendedStream are not ordered, and are not
processed.
- The MSG will automatically switch to a SystemClockDriver when it finds
that there are no more AudioNodeStream/Stream with an audio track. This is
how pausing the audio subsystem and saving battery works. Subsequently, when
the MSG finds that there are only streams in mSuspendedStreams, it will go
to sleep (block on a monitor), so we save CPU, but it does not shut itself
down. This is mostly not a new behaviour (this is what the MSG does since
the refactoring), but is important to note.
- Promises are gripped (addref-ed) on the main thread, and then shepherd
down other threads and to the GraphDriver, if needed (sometimes we can
resolve them right away). They move between threads as void* to prevent
calling methods on them, as they are not thread safe. Then, the driver
executes the operation, and when it's done (initializing and closing audio
streams can take some time), we send the promise back to the main thread,
and resolve it, casting back to Promise* after asserting we're back on the
main thread. This way, we can send them back on the main thread once an
operation has complete (suspending an audio stream, starting it again on
resume(), etc.), without having to do bookkeeping between suspend calls and
their result. Promises are not thread safe, so we can't move them around
AddRef-ed.
- The stream destruction logic now takes into account that a stream can be
destroyed while not being in mStreams.
- A graph can now switch GraphDriver twice or more per iteration, for
example if an author goes suspend()/resume()/suspend() in the same script.
- Some operation have to be done on suspended stream, so we now use double
for-loop around mSuspendedStreams and mStreams in some places in
MediaStreamGraph.cpp.
- A tricky part was making sure everything worked at AudioContext
boundaries. TrackUnionStream that have one of their input stream suspended
append null ticks instead.
- The graph ordering algorithm had to be altered to not include suspended
streams.
- There are some edge cases (adding a stream on a suspended graph, calling
suspend/resume when a graph has just been close()d).
2015-02-27 20:22:05 +03:00
|
|
|
}
|
|
|
|
|
2013-09-10 09:05:22 +04:00
|
|
|
bool
|
|
|
|
MediaStreamGraph::IsNonRealtime() const
|
|
|
|
{
|
2014-11-17 19:07:55 +03:00
|
|
|
const MediaStreamGraphImpl* impl = static_cast<const MediaStreamGraphImpl*>(this);
|
|
|
|
MediaStreamGraphImpl* graph;
|
|
|
|
|
2015-10-23 06:43:15 +03:00
|
|
|
return !gGraphs.Get(uint32_t(impl->AudioChannel()), &graph) || graph != impl;
|
2013-09-10 09:05:22 +04:00
|
|
|
}
|
|
|
|
|
2013-05-17 03:30:41 +04:00
|
|
|
void
|
2014-11-19 13:21:38 +03:00
|
|
|
MediaStreamGraph::StartNonRealtimeProcessing(uint32_t aTicksToProcess)
|
2013-05-17 03:30:41 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "main thread only");
|
|
|
|
|
|
|
|
MediaStreamGraphImpl* graph = static_cast<MediaStreamGraphImpl*>(this);
|
|
|
|
NS_ASSERTION(!graph->mRealtime, "non-realtime only");
|
|
|
|
|
|
|
|
if (graph->mNonRealtimeProcessing)
|
|
|
|
return;
|
2014-04-25 18:09:30 +04:00
|
|
|
|
2015-07-31 14:43:55 +03:00
|
|
|
graph->mEndTime =
|
2015-08-13 07:23:17 +03:00
|
|
|
graph->RoundUpToNextAudioBlock(graph->mStateComputedTime +
|
2015-07-31 14:43:55 +03:00
|
|
|
aTicksToProcess - 1);
|
2013-05-17 03:30:41 +04:00
|
|
|
graph->mNonRealtimeProcessing = true;
|
|
|
|
graph->EnsureRunInStableState();
|
|
|
|
}
|
|
|
|
|
2013-09-13 20:12:07 +04:00
|
|
|
void
|
|
|
|
ProcessedMediaStream::AddInput(MediaInputPort* aPort)
|
|
|
|
{
|
|
|
|
mInputs.AppendElement(aPort);
|
|
|
|
GraphImpl()->SetStreamOrderDirty();
|
|
|
|
}
|
|
|
|
|
2015-07-24 15:28:16 +03:00
|
|
|
void
|
|
|
|
MediaStreamGraph::RegisterCaptureStreamForWindow(
|
|
|
|
uint64_t aWindowId, ProcessedMediaStream* aCaptureStream)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MediaStreamGraphImpl* graphImpl = static_cast<MediaStreamGraphImpl*>(this);
|
|
|
|
graphImpl->RegisterCaptureStreamForWindow(aWindowId, aCaptureStream);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStreamGraphImpl::RegisterCaptureStreamForWindow(
|
|
|
|
uint64_t aWindowId, ProcessedMediaStream* aCaptureStream)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
WindowAndStream winAndStream;
|
|
|
|
winAndStream.mWindowId = aWindowId;
|
|
|
|
winAndStream.mCaptureStreamSink = aCaptureStream;
|
|
|
|
mWindowCaptureStreams.AppendElement(winAndStream);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStreamGraph::UnregisterCaptureStreamForWindow(uint64_t aWindowId)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MediaStreamGraphImpl* graphImpl = static_cast<MediaStreamGraphImpl*>(this);
|
|
|
|
graphImpl->UnregisterCaptureStreamForWindow(aWindowId);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaStreamGraphImpl::UnregisterCaptureStreamForWindow(uint64_t aWindowId)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
for (uint32_t i = 0; i < mWindowCaptureStreams.Length(); i++) {
|
|
|
|
if (mWindowCaptureStreams[i].mWindowId == aWindowId) {
|
|
|
|
mWindowCaptureStreams.RemoveElementAt(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<MediaInputPort>
|
|
|
|
MediaStreamGraph::ConnectToCaptureStream(uint64_t aWindowId,
|
|
|
|
MediaStream* aMediaStream)
|
|
|
|
{
|
|
|
|
return aMediaStream->GraphImpl()->ConnectToCaptureStream(aWindowId,
|
|
|
|
aMediaStream);
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<MediaInputPort>
|
|
|
|
MediaStreamGraphImpl::ConnectToCaptureStream(uint64_t aWindowId,
|
|
|
|
MediaStream* aMediaStream)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
for (uint32_t i = 0; i < mWindowCaptureStreams.Length(); i++) {
|
|
|
|
if (mWindowCaptureStreams[i].mWindowId == aWindowId) {
|
|
|
|
ProcessedMediaStream* sink = mWindowCaptureStreams[i].mCaptureStreamSink;
|
2015-09-04 07:42:42 +03:00
|
|
|
return sink->AllocateInputPort(aMediaStream);
|
2015-07-24 15:28:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace mozilla
|