2010-08-25 06:10:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2010-08-25 06:10:00 -07:00
|
|
|
|
|
|
|
#include "nsDOMNotifyAudioAvailableEvent.h"
|
2013-09-10 08:29:43 -07:00
|
|
|
|
|
|
|
#include "nsError.h"
|
|
|
|
#include "mozilla/HoldDropJSObjects.h"
|
2012-01-14 09:43:00 -08:00
|
|
|
#include "jsfriendapi.h"
|
2010-08-25 06:10:00 -07:00
|
|
|
|
2013-05-02 10:11:12 -07:00
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
|
|
|
nsDOMNotifyAudioAvailableEvent::nsDOMNotifyAudioAvailableEvent(EventTarget* aOwner,
|
2013-03-09 03:34:29 -08:00
|
|
|
nsPresContext* aPresContext,
|
2013-10-01 20:46:04 -07:00
|
|
|
WidgetEvent* aEvent,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aEventType,
|
2010-08-25 06:10:00 -07:00
|
|
|
float* aFrameBuffer,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aFrameBufferLength,
|
2010-08-25 06:10:00 -07:00
|
|
|
float aTime)
|
2013-03-09 03:34:29 -08:00
|
|
|
: nsDOMEvent(aOwner, aPresContext, aEvent),
|
2010-08-25 06:10:00 -07:00
|
|
|
mFrameBuffer(aFrameBuffer),
|
|
|
|
mFrameBufferLength(aFrameBufferLength),
|
|
|
|
mTime(aTime),
|
2012-07-30 07:20:58 -07:00
|
|
|
mCachedArray(nullptr),
|
2011-10-17 07:59:28 -07:00
|
|
|
mAllowAudioData(false)
|
2010-08-25 06:10:00 -07:00
|
|
|
{
|
2010-09-05 19:14:50 -07:00
|
|
|
MOZ_COUNT_CTOR(nsDOMNotifyAudioAvailableEvent);
|
2010-08-25 06:10:00 -07:00
|
|
|
if (mEvent) {
|
|
|
|
mEvent->message = aEventType;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(nsDOMNotifyAudioAvailableEvent, nsDOMEvent)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(nsDOMNotifyAudioAvailableEvent, nsDOMEvent)
|
|
|
|
|
2013-08-01 18:29:05 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(nsDOMNotifyAudioAvailableEvent)
|
|
|
|
|
2010-11-08 07:02:49 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(nsDOMNotifyAudioAvailableEvent, nsDOMEvent)
|
2010-08-25 06:10:00 -07:00
|
|
|
if (tmp->mCachedArray) {
|
2012-07-30 07:20:58 -07:00
|
|
|
tmp->mCachedArray = nullptr;
|
2013-08-16 13:10:17 -07:00
|
|
|
mozilla::DropJSObjects(tmp);
|
2010-08-25 06:10:00 -07:00
|
|
|
}
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(nsDOMNotifyAudioAvailableEvent, nsDOMEvent)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
2013-03-09 03:34:29 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(nsDOMNotifyAudioAvailableEvent, nsDOMEvent)
|
2010-08-25 06:10:00 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mCachedArray)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(nsDOMNotifyAudioAvailableEvent)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMNotifyAudioAvailableEvent)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(nsDOMEvent)
|
|
|
|
|
|
|
|
nsDOMNotifyAudioAvailableEvent::~nsDOMNotifyAudioAvailableEvent()
|
|
|
|
{
|
2010-09-05 19:14:50 -07:00
|
|
|
MOZ_COUNT_DTOR(nsDOMNotifyAudioAvailableEvent);
|
2010-08-25 06:10:00 -07:00
|
|
|
if (mCachedArray) {
|
2012-07-30 07:20:58 -07:00
|
|
|
mCachedArray = nullptr;
|
2013-08-16 13:10:17 -07:00
|
|
|
mozilla::DropJSObjects(this);
|
2010-08-25 06:10:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-01-09 09:39:36 -08:00
|
|
|
nsDOMNotifyAudioAvailableEvent::GetFrameBuffer(JSContext* aCx, JS::MutableHandle<JS::Value> aResult)
|
2010-08-25 06:10:00 -07:00
|
|
|
{
|
|
|
|
if (!mAllowAudioData) {
|
|
|
|
// Media is not same-origin, don't allow the data out.
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mCachedArray) {
|
2014-01-09 09:39:36 -08:00
|
|
|
aResult.setObject(*mCachedArray);
|
2010-08-25 06:10:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cache this array so we don't recreate on next call.
|
2013-08-16 13:10:17 -07:00
|
|
|
mozilla::HoldJSObjects(this);
|
2010-08-25 06:10:00 -07:00
|
|
|
|
2012-01-14 09:43:00 -08:00
|
|
|
mCachedArray = JS_NewFloat32Array(aCx, mFrameBufferLength);
|
2010-08-25 06:10:00 -07:00
|
|
|
if (!mCachedArray) {
|
2013-08-16 13:10:17 -07:00
|
|
|
mozilla::DropJSObjects(this);
|
2010-08-25 06:10:00 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2012-11-14 09:56:26 -08:00
|
|
|
memcpy(JS_GetFloat32ArrayData(mCachedArray), mFrameBuffer.get(), mFrameBufferLength * sizeof(float));
|
2010-08-25 06:10:00 -07:00
|
|
|
|
2014-01-09 09:39:36 -08:00
|
|
|
aResult.setObject(*mCachedArray);
|
2010-08-25 06:10:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDOMNotifyAudioAvailableEvent::GetTime(float *aRetVal)
|
|
|
|
{
|
2013-05-02 10:11:12 -07:00
|
|
|
*aRetVal = Time();
|
2010-08-25 06:10:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDOMNotifyAudioAvailableEvent::InitAudioAvailableEvent(const nsAString& aType,
|
2012-08-16 21:52:08 -07:00
|
|
|
bool aCanBubble,
|
|
|
|
bool aCancelable,
|
2010-08-25 06:10:00 -07:00
|
|
|
float* aFrameBuffer,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aFrameBufferLength,
|
2010-08-25 06:10:00 -07:00
|
|
|
float aTime,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aAllowAudioData)
|
2010-08-25 06:10:00 -07:00
|
|
|
{
|
2010-09-05 19:14:50 -07:00
|
|
|
// Auto manage the memory which stores the frame buffer. This ensures
|
|
|
|
// that if we exit due to some error, the memory will be freed. Otherwise,
|
|
|
|
// the framebuffer's memory will be freed when this event is destroyed.
|
|
|
|
nsAutoArrayPtr<float> frameBuffer(aFrameBuffer);
|
2012-08-16 21:52:08 -07:00
|
|
|
nsresult rv = nsDOMEvent::InitEvent(aType, aCanBubble, aCancelable);
|
2010-08-25 06:10:00 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2010-09-05 19:14:50 -07:00
|
|
|
mFrameBuffer = frameBuffer.forget();
|
2010-08-25 06:10:00 -07:00
|
|
|
mFrameBufferLength = aFrameBufferLength;
|
|
|
|
mTime = aTime;
|
|
|
|
mAllowAudioData = aAllowAudioData;
|
2013-05-02 10:11:12 -07:00
|
|
|
mCachedArray = nullptr;
|
2010-08-25 06:10:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-05-02 10:11:12 -07:00
|
|
|
void
|
|
|
|
nsDOMNotifyAudioAvailableEvent::InitAudioAvailableEvent(const nsAString& aType,
|
|
|
|
bool aCanBubble,
|
|
|
|
bool aCancelable,
|
|
|
|
const Nullable<Sequence<float> >& aFrameBuffer,
|
|
|
|
uint32_t aFrameBufferLength,
|
|
|
|
float aTime,
|
|
|
|
bool aAllowAudioData,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
if ((aFrameBuffer.IsNull() && aFrameBufferLength > 0) ||
|
|
|
|
(!aFrameBuffer.IsNull() &&
|
|
|
|
aFrameBuffer.Value().Length() < aFrameBufferLength)) {
|
|
|
|
aRv = NS_ERROR_UNEXPECTED;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoArrayPtr<float> buffer;
|
|
|
|
if (!aFrameBuffer.IsNull()) {
|
|
|
|
buffer = new float[aFrameBufferLength];
|
|
|
|
memcpy(buffer.get(), aFrameBuffer.Value().Elements(),
|
|
|
|
aFrameBufferLength * sizeof(float));
|
|
|
|
}
|
|
|
|
|
|
|
|
aRv = InitAudioAvailableEvent(aType, aCanBubble, aCancelable,
|
|
|
|
buffer.forget(),
|
|
|
|
aFrameBufferLength,
|
|
|
|
aTime, aAllowAudioData);
|
|
|
|
}
|
|
|
|
|
2010-08-25 06:10:00 -07:00
|
|
|
nsresult NS_NewDOMAudioAvailableEvent(nsIDOMEvent** aInstancePtrResult,
|
2013-05-02 10:11:12 -07:00
|
|
|
EventTarget* aOwner,
|
2010-08-25 06:10:00 -07:00
|
|
|
nsPresContext* aPresContext,
|
2013-10-01 20:46:04 -07:00
|
|
|
WidgetEvent* aEvent,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aEventType,
|
2010-08-25 06:10:00 -07:00
|
|
|
float* aFrameBuffer,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aFrameBufferLength,
|
2010-08-25 06:10:00 -07:00
|
|
|
float aTime)
|
|
|
|
{
|
|
|
|
nsDOMNotifyAudioAvailableEvent* it =
|
2013-03-09 03:34:29 -08:00
|
|
|
new nsDOMNotifyAudioAvailableEvent(aOwner, aPresContext, aEvent, aEventType,
|
2010-08-25 06:10:00 -07:00
|
|
|
aFrameBuffer, aFrameBufferLength, aTime);
|
|
|
|
return CallQueryInterface(it, aInstancePtrResult);
|
|
|
|
}
|