Tue, 06 Jan 2015 21:39:09 +0100
Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
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 WebAudioUtils_h_
8 #define WebAudioUtils_h_
10 #include <cmath>
11 #include <limits>
12 #include "mozilla/TypeTraits.h"
13 #include "mozilla/FloatingPoint.h"
14 #include "MediaSegment.h"
16 // Forward declaration
17 typedef struct SpeexResamplerState_ SpeexResamplerState;
19 namespace mozilla {
21 class AudioNodeStream;
23 namespace dom {
25 class AudioParamTimeline;
27 namespace WebAudioUtils {
28 // 32 is the minimum required by the spec for createBuffer() and
29 // createScriptProcessor() and matches what is used by Blink. The limit
30 // protects against large memory allocations.
31 const uint32_t MaxChannelCount = 32;
32 // AudioContext::CreateBuffer() "must support sample-rates in at least the
33 // range 22050 to 96000."
34 const uint32_t MinSampleRate = 8000;
35 const uint32_t MaxSampleRate = 192000;
37 inline bool FuzzyEqual(float v1, float v2)
38 {
39 using namespace std;
40 return fabsf(v1 - v2) < 1e-7f;
41 }
42 inline bool FuzzyEqual(double v1, double v2)
43 {
44 using namespace std;
45 return fabs(v1 - v2) < 1e-7;
46 }
48 /**
49 * Computes an exponential smoothing rate for a time based variable
50 * over aDuration seconds.
51 */
52 inline double ComputeSmoothingRate(double aDuration, double aSampleRate)
53 {
54 return 1.0 - std::exp(-1.0 / (aDuration * aSampleRate));
55 }
57 /**
58 * Converts AudioParamTimeline floating point time values to tick values
59 * with respect to a source and a destination AudioNodeStream.
60 *
61 * This needs to be called for each AudioParamTimeline that gets sent to an
62 * AudioNodeEngine on the engine side where the AudioParamTimeline is
63 * received. This means that such engines need to be aware of their source
64 * and destination streams as well.
65 */
66 void ConvertAudioParamToTicks(AudioParamTimeline& aParam,
67 AudioNodeStream* aSource,
68 AudioNodeStream* aDest);
70 /**
71 * Converts a linear value to decibels. Returns aMinDecibels if the linear
72 * value is 0.
73 */
74 inline float ConvertLinearToDecibels(float aLinearValue, float aMinDecibels)
75 {
76 return aLinearValue ? 20.0f * std::log10(aLinearValue) : aMinDecibels;
77 }
79 /**
80 * Converts a decibel value to a linear value.
81 */
82 inline float ConvertDecibelsToLinear(float aDecibels)
83 {
84 return std::pow(10.0f, 0.05f * aDecibels);
85 }
87 /**
88 * Converts a decibel to a linear value.
89 */
90 inline float ConvertDecibelToLinear(float aDecibel)
91 {
92 return std::pow(10.0f, 0.05f * aDecibel);
93 }
95 inline void FixNaN(double& aDouble)
96 {
97 if (IsNaN(aDouble) || IsInfinite(aDouble)) {
98 aDouble = 0.0;
99 }
100 }
102 inline double DiscreteTimeConstantForSampleRate(double timeConstant, double sampleRate)
103 {
104 return 1.0 - std::exp(-1.0 / (sampleRate * timeConstant));
105 }
107 inline bool IsTimeValid(double aTime)
108 {
109 return aTime >= 0 && aTime <= (MEDIA_TIME_MAX >> MEDIA_TIME_FRAC_BITS);
110 }
112 /**
113 * Converts a floating point value to an integral type in a safe and
114 * platform agnostic way. The following program demonstrates the kinds
115 * of ways things can go wrong depending on the CPU architecture you're
116 * compiling for:
117 *
118 * #include <stdio.h>
119 * volatile float r;
120 * int main()
121 * {
122 * unsigned int q;
123 * r = 1e100;
124 * q = r;
125 * printf("%f %d\n", r, q);
126 * r = -1e100;
127 * q = r;
128 * printf("%f %d\n", r, q);
129 * r = 1e15;
130 * q = r;
131 * printf("%f %x\n", r, q);
132 * r = 0/0.;
133 * q = r;
134 * printf("%f %d\n", r, q);
135 * }
136 *
137 * This program, when compiled for unsigned int, generates the following
138 * results depending on the architecture:
139 *
140 * x86 and x86-64
141 * ---
142 * inf 0
143 * -inf 0
144 * 999999995904.000000 -727384064 d4a50000
145 * nan 0
146 *
147 * ARM
148 * ---
149 * inf -1
150 * -inf 0
151 * 999999995904.000000 -1
152 * nan 0
153 *
154 * When compiled for int, this program generates the following results:
155 *
156 * x86 and x86-64
157 * ---
158 * inf -2147483648
159 * -inf -2147483648
160 * 999999995904.000000 -2147483648
161 * nan -2147483648
162 *
163 * ARM
164 * ---
165 * inf 2147483647
166 * -inf -2147483648
167 * 999999995904.000000 2147483647
168 * nan 0
169 *
170 * Note that the caller is responsible to make sure that the value
171 * passed to this function is not a NaN. This function will abort if
172 * it sees a NaN.
173 */
174 template <typename IntType, typename FloatType>
175 IntType TruncateFloatToInt(FloatType f)
176 {
177 using namespace std;
179 static_assert(mozilla::IsIntegral<IntType>::value == true,
180 "IntType must be an integral type");
181 static_assert(mozilla::IsFloatingPoint<FloatType>::value == true,
182 "FloatType must be a floating point type");
184 if (f != f) {
185 // It is the responsibility of the caller to deal with NaN values.
186 // If we ever get to this point, we have a serious bug to fix.
187 NS_RUNTIMEABORT("We should never see a NaN here");
188 }
190 if (f > FloatType(numeric_limits<IntType>::max())) {
191 // If the floating point value is outside of the range of maximum
192 // integral value for this type, just clamp to the maximum value.
193 return numeric_limits<IntType>::max();
194 }
196 if (f < FloatType(numeric_limits<IntType>::min())) {
197 // If the floating point value is outside of the range of minimum
198 // integral value for this type, just clamp to the minimum value.
199 return numeric_limits<IntType>::min();
200 }
202 // Otherwise, this conversion must be well defined.
203 return IntType(f);
204 }
206 void Shutdown();
208 int
209 SpeexResamplerProcess(SpeexResamplerState* aResampler,
210 uint32_t aChannel,
211 const float* aIn, uint32_t* aInLen,
212 float* aOut, uint32_t* aOutLen);
214 int
215 SpeexResamplerProcess(SpeexResamplerState* aResampler,
216 uint32_t aChannel,
217 const int16_t* aIn, uint32_t* aInLen,
218 float* aOut, uint32_t* aOutLen);
220 int
221 SpeexResamplerProcess(SpeexResamplerState* aResampler,
222 uint32_t aChannel,
223 const int16_t* aIn, uint32_t* aInLen,
224 int16_t* aOut, uint32_t* aOutLen);
225 }
227 }
228 }
230 #endif