content/media/webaudio/AudioListener.cpp

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

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.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include "AudioListener.h"
michael@0 8 #include "AudioContext.h"
michael@0 9 #include "mozilla/dom/AudioListenerBinding.h"
michael@0 10
michael@0 11 namespace mozilla {
michael@0 12 namespace dom {
michael@0 13
michael@0 14 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_1(AudioListener, mContext)
michael@0 15
michael@0 16 NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(AudioListener, AddRef)
michael@0 17 NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(AudioListener, Release)
michael@0 18
michael@0 19 AudioListener::AudioListener(AudioContext* aContext)
michael@0 20 : mContext(aContext)
michael@0 21 , mPosition()
michael@0 22 , mFrontVector(0., 0., -1.)
michael@0 23 , mRightVector(1., 0., 0.)
michael@0 24 , mVelocity()
michael@0 25 , mDopplerFactor(1.)
michael@0 26 , mSpeedOfSound(343.3) // meters/second
michael@0 27 {
michael@0 28 MOZ_ASSERT(aContext);
michael@0 29 SetIsDOMBinding();
michael@0 30 }
michael@0 31
michael@0 32 JSObject*
michael@0 33 AudioListener::WrapObject(JSContext* aCx)
michael@0 34 {
michael@0 35 return AudioListenerBinding::Wrap(aCx, this);
michael@0 36 }
michael@0 37
michael@0 38 void
michael@0 39 AudioListener::SetOrientation(double aX, double aY, double aZ,
michael@0 40 double aXUp, double aYUp, double aZUp)
michael@0 41 {
michael@0 42 ThreeDPoint front(aX, aY, aZ);
michael@0 43 // The panning effect and the azimuth and elevation calculation in the Web
michael@0 44 // Audio spec becomes undefined with linearly dependent vectors, so keep
michael@0 45 // existing state in these situations.
michael@0 46 if (front.IsZero()) {
michael@0 47 return;
michael@0 48 }
michael@0 49 // Normalize before using CrossProduct() to avoid overflow.
michael@0 50 front.Normalize();
michael@0 51 ThreeDPoint up(aXUp, aYUp, aZUp);
michael@0 52 if (up.IsZero()) {
michael@0 53 return;
michael@0 54 }
michael@0 55 up.Normalize();
michael@0 56 ThreeDPoint right = front.CrossProduct(up);
michael@0 57 if (right.IsZero()) {
michael@0 58 return;
michael@0 59 }
michael@0 60 right.Normalize();
michael@0 61
michael@0 62 if (!mFrontVector.FuzzyEqual(front)) {
michael@0 63 mFrontVector = front;
michael@0 64 SendThreeDPointParameterToStream(PannerNode::LISTENER_FRONT_VECTOR, front);
michael@0 65 }
michael@0 66 if (!mRightVector.FuzzyEqual(right)) {
michael@0 67 mRightVector = right;
michael@0 68 SendThreeDPointParameterToStream(PannerNode::LISTENER_RIGHT_VECTOR, right);
michael@0 69 }
michael@0 70 }
michael@0 71
michael@0 72 void
michael@0 73 AudioListener::RegisterPannerNode(PannerNode* aPannerNode)
michael@0 74 {
michael@0 75 mPanners.AppendElement(aPannerNode->asWeakPtr());
michael@0 76
michael@0 77 // Let the panner node know about our parameters
michael@0 78 aPannerNode->SendThreeDPointParameterToStream(PannerNode::LISTENER_POSITION, mPosition);
michael@0 79 aPannerNode->SendThreeDPointParameterToStream(PannerNode::LISTENER_FRONT_VECTOR, mFrontVector);
michael@0 80 aPannerNode->SendThreeDPointParameterToStream(PannerNode::LISTENER_RIGHT_VECTOR, mRightVector);
michael@0 81 aPannerNode->SendThreeDPointParameterToStream(PannerNode::LISTENER_VELOCITY, mVelocity);
michael@0 82 aPannerNode->SendDoubleParameterToStream(PannerNode::LISTENER_DOPPLER_FACTOR, mDopplerFactor);
michael@0 83 aPannerNode->SendDoubleParameterToStream(PannerNode::LISTENER_SPEED_OF_SOUND, mSpeedOfSound);
michael@0 84 UpdatePannersVelocity();
michael@0 85 }
michael@0 86
michael@0 87 void AudioListener::UnregisterPannerNode(PannerNode* aPannerNode)
michael@0 88 {
michael@0 89 mPanners.RemoveElement(aPannerNode);
michael@0 90 }
michael@0 91
michael@0 92 void
michael@0 93 AudioListener::SendDoubleParameterToStream(uint32_t aIndex, double aValue)
michael@0 94 {
michael@0 95 for (uint32_t i = 0; i < mPanners.Length(); ++i) {
michael@0 96 if (mPanners[i]) {
michael@0 97 mPanners[i]->SendDoubleParameterToStream(aIndex, aValue);
michael@0 98 }
michael@0 99 }
michael@0 100 }
michael@0 101
michael@0 102 void
michael@0 103 AudioListener::SendThreeDPointParameterToStream(uint32_t aIndex, const ThreeDPoint& aValue)
michael@0 104 {
michael@0 105 for (uint32_t i = 0; i < mPanners.Length(); ++i) {
michael@0 106 if (mPanners[i]) {
michael@0 107 mPanners[i]->SendThreeDPointParameterToStream(aIndex, aValue);
michael@0 108 }
michael@0 109 }
michael@0 110 }
michael@0 111
michael@0 112 void AudioListener::UpdatePannersVelocity()
michael@0 113 {
michael@0 114 for (uint32_t i = 0; i < mPanners.Length(); ++i) {
michael@0 115 if (mPanners[i]) {
michael@0 116 mPanners[i]->SendDopplerToSourcesIfNeeded();
michael@0 117 }
michael@0 118 }
michael@0 119 }
michael@0 120
michael@0 121 size_t
michael@0 122 AudioListener::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
michael@0 123 {
michael@0 124 size_t amount = aMallocSizeOf(this);
michael@0 125 // AudioNodes are tracked separately
michael@0 126 amount += mPanners.SizeOfExcludingThis(aMallocSizeOf);
michael@0 127 return amount;
michael@0 128 }
michael@0 129
michael@0 130 }
michael@0 131 }
michael@0 132

mercurial