Backed out changeset 06f41c22f3a6 (bug 1888460) for causing linux xpcshell failures...
[gecko.git] / dom / media / webaudio / AudioBufferSourceNode.h
blob9a3861555c0774ffacf74ad339a068b8b1b7263e
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef AudioBufferSourceNode_h_
8 #define AudioBufferSourceNode_h_
10 #include "AudioScheduledSourceNode.h"
11 #include "AudioBuffer.h"
13 namespace mozilla::dom {
15 struct AudioBufferSourceOptions;
16 class AudioParam;
18 class AudioBufferSourceNode final : public AudioScheduledSourceNode,
19 public MainThreadMediaTrackListener {
20 public:
21 static already_AddRefed<AudioBufferSourceNode> Create(
22 JSContext* aCx, AudioContext& aAudioContext,
23 const AudioBufferSourceOptions& aOptions);
25 void DestroyMediaTrack() override;
27 uint16_t NumberOfInputs() const final { return 0; }
28 AudioBufferSourceNode* AsAudioBufferSourceNode() override { return this; }
29 NS_DECL_ISUPPORTS_INHERITED
30 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(AudioBufferSourceNode,
31 AudioScheduledSourceNode)
33 static already_AddRefed<AudioBufferSourceNode> Constructor(
34 const GlobalObject& aGlobal, AudioContext& aAudioContext,
35 const AudioBufferSourceOptions& aOptions) {
36 return Create(aGlobal.Context(), aAudioContext, aOptions);
39 JSObject* WrapObject(JSContext* aCx,
40 JS::Handle<JSObject*> aGivenProto) override;
42 void Start(double aWhen, double aOffset, const Optional<double>& aDuration,
43 ErrorResult& aRv);
45 void Start(double aWhen, ErrorResult& aRv) override;
46 void Stop(double aWhen, ErrorResult& aRv) override;
48 AudioBuffer* GetBuffer(JSContext* aCx) const { return mBuffer; }
49 void SetBuffer(JSContext* aCx, AudioBuffer* aBuffer, ErrorResult& aRv) {
50 if (aBuffer && mBufferSet) {
51 aRv.ThrowInvalidStateError(
52 "Cannot set the buffer attribute of an AudioBufferSourceNode "
53 "with an AudioBuffer more than once");
54 return;
56 if (aBuffer) {
57 mBufferSet = true;
59 mBuffer = aBuffer;
60 SendBufferParameterToTrack(aCx);
61 SendLoopParametersToTrack();
63 AudioParam* PlaybackRate() const { return mPlaybackRate; }
64 AudioParam* Detune() const { return mDetune; }
65 bool Loop() const { return mLoop; }
66 void SetLoop(bool aLoop) {
67 mLoop = aLoop;
68 SendLoopParametersToTrack();
70 double LoopStart() const { return mLoopStart; }
71 void SetLoopStart(double aStart) {
72 mLoopStart = aStart;
73 SendLoopParametersToTrack();
75 double LoopEnd() const { return mLoopEnd; }
76 void SetLoopEnd(double aEnd) {
77 mLoopEnd = aEnd;
78 SendLoopParametersToTrack();
80 void NotifyMainThreadTrackEnded() override;
82 const char* NodeType() const override { return "AudioBufferSourceNode"; }
84 size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const override;
85 size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const override;
87 private:
88 explicit AudioBufferSourceNode(AudioContext* aContext);
89 ~AudioBufferSourceNode() = default;
91 friend class AudioBufferSourceNodeEngine;
92 // START is sent during Start().
93 // STOP is sent during Stop().
94 // BUFFERSTART and DURATION are sent when SetBuffer() and Start() have
95 // been called (along with sending the buffer).
96 enum EngineParameters {
97 SAMPLE_RATE,
98 START,
99 STOP,
100 // BUFFERSTART is the "offset" passed to start(), multiplied by
101 // buffer.sampleRate.
102 BUFFERSTART,
103 DURATION,
104 LOOP,
105 LOOPSTART,
106 LOOPEND,
107 PLAYBACKRATE,
108 DETUNE
111 void SendLoopParametersToTrack();
112 void SendBufferParameterToTrack(JSContext* aCx);
113 void SendOffsetAndDurationParametersToTrack(AudioNodeTrack* aTrack);
115 double mLoopStart;
116 double mLoopEnd;
117 double mOffset;
118 double mDuration;
119 RefPtr<AudioBuffer> mBuffer;
120 RefPtr<AudioParam> mPlaybackRate;
121 RefPtr<AudioParam> mDetune;
122 bool mLoop;
123 bool mStartCalled;
124 bool mBufferSet;
127 } // namespace mozilla::dom
129 #endif