2013-07-24 15:29:39 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#ifndef MediaStreamAudioSourceNode_h_
|
|
|
|
#define MediaStreamAudioSourceNode_h_
|
|
|
|
|
|
|
|
#include "AudioNode.h"
|
2014-04-25 18:32:00 +04:00
|
|
|
#include "DOMMediaStream.h"
|
|
|
|
#include "AudioNodeEngine.h"
|
2013-07-24 15:29:39 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
2013-07-25 06:07:34 +04:00
|
|
|
|
2013-07-24 15:29:39 +04:00
|
|
|
namespace dom {
|
|
|
|
|
2016-12-15 21:24:43 +03:00
|
|
|
class AudioContext;
|
|
|
|
struct MediaStreamAudioSourceOptions;
|
|
|
|
|
2015-04-28 09:42:00 +03:00
|
|
|
class MediaStreamAudioSourceNodeEngine final : public AudioNodeEngine {
|
2014-04-25 18:32:00 +04:00
|
|
|
public:
|
2014-09-02 02:26:43 +04:00
|
|
|
explicit MediaStreamAudioSourceNodeEngine(AudioNode* aNode)
|
2014-04-25 18:32:00 +04:00
|
|
|
: AudioNodeEngine(aNode), mEnabled(false) {}
|
|
|
|
|
|
|
|
bool IsEnabled() const { return mEnabled; }
|
|
|
|
enum Parameters { ENABLE };
|
2016-01-18 06:22:51 +03:00
|
|
|
void SetInt32Parameter(uint32_t aIndex, int32_t aValue) override {
|
2014-04-25 18:32:00 +04:00
|
|
|
switch (aIndex) {
|
|
|
|
case ENABLE:
|
|
|
|
mEnabled = !!aValue;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NS_ERROR("MediaStreamAudioSourceNodeEngine bad parameter index");
|
|
|
|
}
|
|
|
|
}
|
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-04-25 18:32:00 +04:00
|
|
|
private:
|
|
|
|
bool mEnabled;
|
|
|
|
};
|
|
|
|
|
|
|
|
class MediaStreamAudioSourceNode
|
|
|
|
: public AudioNode,
|
2016-01-22 06:29:40 +03:00
|
|
|
public DOMMediaStream::TrackListener,
|
|
|
|
public PrincipalChangeObserver<MediaStreamTrack> {
|
2013-07-24 15:29:39 +04:00
|
|
|
public:
|
2015-12-10 19:25:54 +03:00
|
|
|
static already_AddRefed<MediaStreamAudioSourceNode> Create(
|
2016-12-15 21:24:43 +03:00
|
|
|
AudioContext& aContext, const MediaStreamAudioSourceOptions& aOptions,
|
|
|
|
ErrorResult& aRv);
|
2013-07-25 06:07:34 +04:00
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(MediaStreamAudioSourceNode,
|
|
|
|
AudioNode)
|
2013-07-24 15:29:39 +04:00
|
|
|
|
2016-12-15 21:24:43 +03:00
|
|
|
static already_AddRefed<MediaStreamAudioSourceNode> Constructor(
|
|
|
|
const GlobalObject& aGlobal, AudioContext& aAudioContext,
|
|
|
|
const MediaStreamAudioSourceOptions& aOptions, ErrorResult& aRv) {
|
|
|
|
return Create(aAudioContext, aOptions, aRv);
|
|
|
|
}
|
|
|
|
|
2016-01-18 06:22:51 +03:00
|
|
|
JSObject* WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) override;
|
2013-07-24 15:29:39 +04:00
|
|
|
|
2016-01-18 06:22:51 +03:00
|
|
|
void DestroyMediaStream() override;
|
2013-07-24 15:29:39 +04:00
|
|
|
|
2016-01-18 06:22:51 +03:00
|
|
|
uint16_t NumberOfInputs() const override { return 0; }
|
2013-07-24 15:29:39 +04:00
|
|
|
|
2016-01-18 06:22:51 +03:00
|
|
|
const char* NodeType() const override { return "MediaStreamAudioSourceNode"; }
|
2014-04-13 22:08:10 +04:00
|
|
|
|
2016-04-26 18:46:31 +03:00
|
|
|
virtual const char* CrossOriginErrorString() const {
|
|
|
|
return "MediaStreamAudioSourceNodeCrossOrigin";
|
|
|
|
}
|
|
|
|
|
2016-01-18 06:22:51 +03:00
|
|
|
size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const override;
|
|
|
|
size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const override;
|
2014-04-13 22:08:10 +04:00
|
|
|
|
2016-01-22 06:29:40 +03:00
|
|
|
// Attaches to aTrack so that its audio content will be used as input.
|
|
|
|
void AttachToTrack(const RefPtr<MediaStreamTrack>& aTrack);
|
|
|
|
|
|
|
|
// Detaches from the currently attached track if there is one.
|
|
|
|
void DetachFromTrack();
|
|
|
|
|
2019-06-20 02:06:33 +03:00
|
|
|
// Attaches to the first audio track in the MediaStream, when the tracks are
|
|
|
|
// ordered by id.
|
|
|
|
void AttachToRightTrack(const RefPtr<DOMMediaStream>& aMediaStream,
|
|
|
|
ErrorResult& aRv);
|
2016-01-22 06:29:40 +03:00
|
|
|
|
|
|
|
// From DOMMediaStream::TrackListener.
|
|
|
|
void NotifyTrackAdded(const RefPtr<MediaStreamTrack>& aTrack) override;
|
|
|
|
void NotifyTrackRemoved(const RefPtr<MediaStreamTrack>& aTrack) override;
|
2019-02-07 02:14:09 +03:00
|
|
|
void NotifyActive() override;
|
2016-01-22 06:29:40 +03:00
|
|
|
|
|
|
|
// From PrincipalChangeObserver<MediaStreamTrack>.
|
|
|
|
void PrincipalChanged(MediaStreamTrack* aMediaStreamTrack) override;
|
2014-04-25 18:32:00 +04:00
|
|
|
|
2019-06-20 02:06:33 +03:00
|
|
|
// This allows implementing the correct behaviour for both
|
|
|
|
// MediaElementAudioSourceNode and MediaStreamAudioSourceNode, that have most
|
|
|
|
// of their behaviour shared.
|
|
|
|
enum TrackChangeBehavior {
|
|
|
|
// MediaStreamAudioSourceNode locks on the track it picked, and never
|
|
|
|
// changes.
|
|
|
|
LockOnTrackPicked,
|
|
|
|
// MediaElementAudioSourceNode can change track, depending on what the
|
|
|
|
// HTMLMediaElement does.
|
|
|
|
FollowChanges
|
|
|
|
};
|
|
|
|
|
2014-07-09 01:23:17 +04:00
|
|
|
protected:
|
2019-06-20 02:06:33 +03:00
|
|
|
MediaStreamAudioSourceNode(AudioContext* aContext,
|
|
|
|
TrackChangeBehavior aBehavior);
|
2015-12-10 19:25:54 +03:00
|
|
|
void Init(DOMMediaStream* aMediaStream, ErrorResult& aRv);
|
2019-01-11 23:43:01 +03:00
|
|
|
virtual void Destroy();
|
2014-07-09 01:23:17 +04:00
|
|
|
virtual ~MediaStreamAudioSourceNode();
|
|
|
|
|
2013-07-24 15:29:39 +04:00
|
|
|
private:
|
2019-06-20 02:06:33 +03:00
|
|
|
const TrackChangeBehavior mBehavior;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaInputPort> mInputPort;
|
|
|
|
RefPtr<DOMMediaStream> mInputStream;
|
2016-01-22 06:29:40 +03:00
|
|
|
|
|
|
|
// On construction we set this to the first audio track of mInputStream.
|
|
|
|
RefPtr<MediaStreamTrack> mInputTrack;
|
2013-07-24 15:29:39 +04:00
|
|
|
};
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
2013-07-24 15:29:39 +04:00
|
|
|
|
|
|
|
#endif
|