Line data Source code
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 "nsWrapperCache.h"
11 : #include "nsCycleCollectionParticipant.h"
12 : #include "mozilla/Attributes.h"
13 : #include "mozilla/StaticPtr.h"
14 : #include "mozilla/StaticMutex.h"
15 : #include "nsTArray.h"
16 : #include "js/TypeDecls.h"
17 : #include "mozilla/MemoryReporting.h"
18 : #include "mozilla/dom/TypedArray.h"
19 :
20 : namespace mozilla {
21 :
22 : class ErrorResult;
23 : class ThreadSharedFloatArrayBufferList;
24 :
25 : namespace dom {
26 :
27 : struct AudioBufferOptions;
28 :
29 : /**
30 : * An AudioBuffer keeps its data either in the mJSChannels objects, which
31 : * are Float32Arrays, or in mSharedChannels if the mJSChannels objects' buffers
32 : * are detached.
33 : */
34 : class AudioBuffer final : public nsWrapperCache
35 : {
36 : public:
37 : // If non-null, aInitialContents must have number of channels equal to
38 : // aNumberOfChannels and their lengths must be at least aLength.
39 : static already_AddRefed<AudioBuffer>
40 : Create(nsPIDOMWindowInner* aWindow, uint32_t aNumberOfChannels,
41 : uint32_t aLength, float aSampleRate,
42 : already_AddRefed<ThreadSharedFloatArrayBufferList> aInitialContents,
43 : ErrorResult& aRv);
44 :
45 : static already_AddRefed<AudioBuffer>
46 0 : Create(nsPIDOMWindowInner* aWindow, uint32_t aNumberOfChannels,
47 : uint32_t aLength, float aSampleRate,
48 : ErrorResult& aRv)
49 : {
50 : return Create(aWindow, aNumberOfChannels, aLength, aSampleRate,
51 0 : nullptr, aRv);
52 : }
53 :
54 : size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
55 :
56 0 : NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(AudioBuffer)
57 0 : NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(AudioBuffer)
58 :
59 : static already_AddRefed<AudioBuffer>
60 : Constructor(const GlobalObject& aGlobal,
61 : const AudioBufferOptions& aOptions, ErrorResult& aRv);
62 :
63 0 : nsPIDOMWindowInner* GetParentObject() const
64 : {
65 0 : nsCOMPtr<nsPIDOMWindowInner> parentObject = do_QueryReferent(mOwnerWindow);
66 0 : return parentObject;
67 : }
68 :
69 : JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
70 :
71 0 : float SampleRate() const
72 : {
73 0 : return mSampleRate;
74 : }
75 :
76 0 : uint32_t Length() const
77 : {
78 0 : return mLength;
79 : }
80 :
81 0 : double Duration() const
82 : {
83 0 : return mLength / static_cast<double> (mSampleRate);
84 : }
85 :
86 0 : uint32_t NumberOfChannels() const
87 : {
88 0 : return mJSChannels.Length();
89 : }
90 :
91 : /**
92 : * If mSharedChannels is non-null, copies its contents to
93 : * new Float32Arrays in mJSChannels. Returns a Float32Array.
94 : */
95 : void GetChannelData(JSContext* aJSContext, uint32_t aChannel,
96 : JS::MutableHandle<JSObject*> aRetval,
97 : ErrorResult& aRv);
98 :
99 : void CopyFromChannel(const Float32Array& aDestination, uint32_t aChannelNumber,
100 : uint32_t aStartInChannel, ErrorResult& aRv);
101 : void CopyToChannel(JSContext* aJSContext, const Float32Array& aSource,
102 : uint32_t aChannelNumber, uint32_t aStartInChannel,
103 : ErrorResult& aRv);
104 :
105 : /**
106 : * Returns a ThreadSharedFloatArrayBufferList containing the sample data.
107 : * Can return null if there is no data.
108 : */
109 : ThreadSharedFloatArrayBufferList* GetThreadSharedChannelsForRate(JSContext* aContext);
110 :
111 : protected:
112 : AudioBuffer(nsPIDOMWindowInner* aWindow, uint32_t aNumberOfChannels,
113 : uint32_t aLength, float aSampleRate,
114 : already_AddRefed<ThreadSharedFloatArrayBufferList>
115 : aInitialContents);
116 : ~AudioBuffer();
117 :
118 : bool RestoreJSChannelData(JSContext* aJSContext);
119 :
120 : already_AddRefed<ThreadSharedFloatArrayBufferList>
121 : StealJSArrayDataIntoSharedChannels(JSContext* aJSContext);
122 :
123 : void ClearJSChannels();
124 :
125 : nsWeakPtr mOwnerWindow;
126 : // Float32Arrays
127 : AutoTArray<JS::Heap<JSObject*>, 2> mJSChannels;
128 :
129 : // mSharedChannels aggregates the data from mJSChannels. This is non-null
130 : // if and only if the mJSChannels' buffers are detached.
131 : RefPtr<ThreadSharedFloatArrayBufferList> mSharedChannels;
132 :
133 : uint32_t mLength;
134 : float mSampleRate;
135 : };
136 :
137 : } // namespace dom
138 : } // namespace mozilla
139 :
140 : #endif
|