/work/obj-fuzz/dist/include/mozilla/dom/AudioBuffer.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim:set ts=2 sw=2 sts=2 et cindent: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | #ifndef AudioBuffer_h_ |
8 | | #define AudioBuffer_h_ |
9 | | |
10 | | #include "AudioSegment.h" |
11 | | #include "nsWrapperCache.h" |
12 | | #include "nsCycleCollectionParticipant.h" |
13 | | #include "mozilla/Attributes.h" |
14 | | #include "mozilla/StaticPtr.h" |
15 | | #include "mozilla/StaticMutex.h" |
16 | | #include "nsTArray.h" |
17 | | #include "js/TypeDecls.h" |
18 | | #include "mozilla/MemoryReporting.h" |
19 | | #include "mozilla/dom/TypedArray.h" |
20 | | #include "nsPIDOMWindow.h" |
21 | | #include "nsIWeakReferenceUtils.h" |
22 | | |
23 | | namespace mozilla { |
24 | | |
25 | | class ErrorResult; |
26 | | class ThreadSharedFloatArrayBufferList; |
27 | | |
28 | | namespace dom { |
29 | | |
30 | | struct AudioBufferOptions; |
31 | | |
32 | | /** |
33 | | * An AudioBuffer keeps its data either in the mJSChannels objects, which |
34 | | * are Float32Arrays, or in mSharedChannels if the mJSChannels objects' buffers |
35 | | * are detached. |
36 | | */ |
37 | | class AudioBuffer final : public nsWrapperCache |
38 | | { |
39 | | public: |
40 | | // If non-null, aInitialContents must have number of channels equal to |
41 | | // aNumberOfChannels and their lengths must be at least aLength. |
42 | | static already_AddRefed<AudioBuffer> |
43 | | Create(nsPIDOMWindowInner* aWindow, uint32_t aNumberOfChannels, |
44 | | uint32_t aLength, float aSampleRate, |
45 | | already_AddRefed<ThreadSharedFloatArrayBufferList> aInitialContents, |
46 | | ErrorResult& aRv); |
47 | | |
48 | | static already_AddRefed<AudioBuffer> |
49 | | Create(nsPIDOMWindowInner* aWindow, uint32_t aNumberOfChannels, |
50 | | uint32_t aLength, float aSampleRate, |
51 | | ErrorResult& aRv) |
52 | 0 | { |
53 | 0 | return Create(aWindow, aNumberOfChannels, aLength, aSampleRate, |
54 | 0 | nullptr, aRv); |
55 | 0 | } |
56 | | |
57 | | // Non-unit AudioChunk::mVolume is not supported |
58 | | static already_AddRefed<AudioBuffer> |
59 | | Create(nsPIDOMWindowInner* aWindow, float aSampleRate, |
60 | | AudioChunk&& aInitialContents); |
61 | | |
62 | | size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const; |
63 | | |
64 | | NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(AudioBuffer) |
65 | | NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(AudioBuffer) |
66 | | |
67 | | static already_AddRefed<AudioBuffer> |
68 | | Constructor(const GlobalObject& aGlobal, |
69 | | const AudioBufferOptions& aOptions, ErrorResult& aRv); |
70 | | |
71 | | nsPIDOMWindowInner* GetParentObject() const |
72 | 0 | { |
73 | 0 | nsCOMPtr<nsPIDOMWindowInner> parentObject = do_QueryReferent(mOwnerWindow); |
74 | 0 | return parentObject; |
75 | 0 | } |
76 | | |
77 | | JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override; |
78 | | |
79 | | float SampleRate() const |
80 | 0 | { |
81 | 0 | return mSampleRate; |
82 | 0 | } |
83 | | |
84 | | uint32_t Length() const |
85 | 0 | { |
86 | 0 | return mSharedChannels.mDuration; |
87 | 0 | } |
88 | | |
89 | | double Duration() const |
90 | 0 | { |
91 | 0 | return Length() / static_cast<double> (mSampleRate); |
92 | 0 | } |
93 | | |
94 | | uint32_t NumberOfChannels() const |
95 | 0 | { |
96 | 0 | return mJSChannels.Length(); |
97 | 0 | } |
98 | | |
99 | | /** |
100 | | * If mSharedChannels is non-null, copies its contents to |
101 | | * new Float32Arrays in mJSChannels. Returns a Float32Array. |
102 | | */ |
103 | | void GetChannelData(JSContext* aJSContext, uint32_t aChannel, |
104 | | JS::MutableHandle<JSObject*> aRetval, |
105 | | ErrorResult& aRv); |
106 | | |
107 | | void CopyFromChannel(const Float32Array& aDestination, uint32_t aChannelNumber, |
108 | | uint32_t aStartInChannel, ErrorResult& aRv); |
109 | | void CopyToChannel(JSContext* aJSContext, const Float32Array& aSource, |
110 | | uint32_t aChannelNumber, uint32_t aStartInChannel, |
111 | | ErrorResult& aRv); |
112 | | |
113 | | /** |
114 | | * Returns a reference to an AudioChunk containing the sample data. |
115 | | * The AudioChunk can have a null buffer if there is no data. |
116 | | */ |
117 | | const AudioChunk& GetThreadSharedChannelsForRate(JSContext* aContext); |
118 | | |
119 | | protected: |
120 | | AudioBuffer(nsPIDOMWindowInner* aWindow, uint32_t aNumberOfChannels, |
121 | | uint32_t aLength, float aSampleRate, ErrorResult& aRv); |
122 | | ~AudioBuffer(); |
123 | | |
124 | | void |
125 | | SetSharedChannels(already_AddRefed<ThreadSharedFloatArrayBufferList> aBuffer); |
126 | | |
127 | | bool RestoreJSChannelData(JSContext* aJSContext); |
128 | | |
129 | | already_AddRefed<ThreadSharedFloatArrayBufferList> |
130 | | StealJSArrayDataIntoSharedChannels(JSContext* aJSContext); |
131 | | |
132 | | void ClearJSChannels(); |
133 | | |
134 | | // Float32Arrays |
135 | | AutoTArray<JS::Heap<JSObject*>, 2> mJSChannels; |
136 | | // mSharedChannels aggregates the data from mJSChannels. This is non-null |
137 | | // if and only if the mJSChannels' buffers are detached, but its mDuration |
138 | | // member keeps the buffer length regardless of whether the buffer is |
139 | | // provided by mJSChannels or mSharedChannels. |
140 | | AudioChunk mSharedChannels; |
141 | | |
142 | | nsWeakPtr mOwnerWindow; |
143 | | float mSampleRate; |
144 | | }; |
145 | | |
146 | | } // namespace dom |
147 | | } // namespace mozilla |
148 | | |
149 | | #endif |