already_AddRefed<AudioBuffer> AudioContext::CreateBuffer(JSContext* aJSContext, const ArrayBuffer& aBuffer, bool aMixToMono, ErrorResult& aRv) { // Do not accept this method unless the legacy pref has been set. if (!Preferences::GetBool("media.webaudio.legacy.AudioContext")) { aRv.ThrowNotEnoughArgsError(); return nullptr; } // Sniff the content of the media. // Failed type sniffing will be handled by SyncDecodeMedia. nsAutoCString contentType; NS_SniffContent(NS_DATA_SNIFFER_CATEGORY, nullptr, aBuffer.Data(), aBuffer.Length(), contentType); nsRefPtr<WebAudioDecodeJob> job = new WebAudioDecodeJob(contentType, this, aBuffer); if (mDecoder.SyncDecodeMedia(contentType.get(), aBuffer.Data(), aBuffer.Length(), *job) && job->mOutput) { nsRefPtr<AudioBuffer> buffer = job->mOutput.forget(); if (aMixToMono) { buffer->MixToMono(aJSContext); } return buffer.forget(); } return nullptr; }
already_AddRefed<AudioBuffer> AudioContext::CreateBuffer(JSContext* aJSContext, ArrayBuffer& aBuffer, bool aMixToMono, ErrorResult& aRv) { // Sniff the content of the media. // Failed type sniffing will be handled by SyncDecodeMedia. nsAutoCString contentType; NS_SniffContent(NS_DATA_SNIFFER_CATEGORY, nullptr, aBuffer.Data(), aBuffer.Length(), contentType); WebAudioDecodeJob job(contentType, this); if (mDecoder.SyncDecodeMedia(contentType.get(), aBuffer.Data(), aBuffer.Length(), job) && job.mOutput) { nsRefPtr<AudioBuffer> buffer = job.mOutput.forget(); if (aMixToMono) { buffer->MixToMono(aJSContext); } return buffer.forget(); } return nullptr; }
/* static */ bool PushUtil::CopyArrayBufferToArray(const ArrayBuffer& aBuffer, nsTArray<uint8_t>& aArray) { aBuffer.ComputeLengthAndData(); return aArray.SetLength(aBuffer.Length(), fallible) && aArray.ReplaceElementsAt(0, aBuffer.Length(), aBuffer.Data(), aBuffer.Length(), fallible); }
already_AddRefed<Promise> BluetoothGattCharacteristic::WriteValue(const ArrayBuffer& aValue, ErrorResult& aRv) { nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(GetParentObject()); if (!global) { aRv.Throw(NS_ERROR_FAILURE); return nullptr; } RefPtr<Promise> promise = Promise::Create(global, aRv); NS_ENSURE_TRUE(!aRv.Failed(), nullptr); aValue.ComputeLengthAndData(); if (mAttRole == ATT_SERVER_ROLE) { mValue.Clear(); mValue.AppendElements(aValue.Data(), aValue.Length()); promise->MaybeResolve(JS::UndefinedHandleValue); return promise.forget(); } BT_ENSURE_TRUE_REJECT(mProperties & (GATT_CHAR_PROP_BIT_WRITE_NO_RESPONSE | GATT_CHAR_PROP_BIT_WRITE | GATT_CHAR_PROP_BIT_SIGNED_WRITE), promise, NS_ERROR_NOT_AVAILABLE); nsTArray<uint8_t> value; value.AppendElements(aValue.Data(), aValue.Length()); BluetoothService* bs = BluetoothService::Get(); BT_ENSURE_TRUE_REJECT(bs, promise, NS_ERROR_NOT_AVAILABLE); BluetoothUuid appUuid; BT_ENSURE_TRUE_REJECT(NS_SUCCEEDED(StringToUuid(mService->GetAppUuid(), appUuid)), promise, NS_ERROR_DOM_OPERATION_ERR); bs->GattClientWriteCharacteristicValueInternal( appUuid, mService->GetServiceId(), mCharId, mWriteType, value, new BluetoothVoidReplyRunnable(nullptr, promise)); return promise.forget(); }
// Constructor of BluetoothGattCharacteristic in ATT server role BluetoothGattCharacteristic::BluetoothGattCharacteristic( nsPIDOMWindow* aOwner, BluetoothGattService* aService, const nsAString& aCharacteristicUuid, const GattPermissions& aPermissions, const GattCharacteristicProperties& aProperties, const ArrayBuffer& aValue) : mOwner(aOwner) , mService(aService) , mUuidStr(aCharacteristicUuid) , mPermissions(BLUETOOTH_EMPTY_GATT_ATTR_PERM) , mProperties(BLUETOOTH_EMPTY_GATT_CHAR_PROP) , mWriteType(GATT_WRITE_TYPE_NORMAL) , mAttRole(ATT_SERVER_ROLE) , mActive(false) { MOZ_ASSERT(aOwner); MOZ_ASSERT(aService); // UUID memset(&mCharId, 0, sizeof(mCharId)); StringToUuid(aCharacteristicUuid, mCharId.mUuid); // permissions GattPermissionsToBits(aPermissions, mPermissions); // properties GattPropertiesToBits(aProperties, mProperties); // value aValue.ComputeLengthAndData(); mValue.AppendElements(aValue.Data(), aValue.Length()); }
void PresentationConnection::Send(const ArrayBuffer& aData, ErrorResult& aRv) { if (NS_WARN_IF(mState != PresentationConnectionState::Connected)) { aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR); return; } nsCOMPtr<nsIPresentationService> service = do_GetService(PRESENTATION_SERVICE_CONTRACTID); if(NS_WARN_IF(!service)) { AsyncCloseConnectionWithErrorMsg( NS_LITERAL_STRING("Unable to send message due to an internal error.")); return; } aData.ComputeLengthAndData(); static_assert(sizeof(*aData.Data()) == 1, "byte-sized data required"); uint32_t length = aData.Length(); char* data = reinterpret_cast<char*>(aData.Data()); nsDependentCSubstring msgString(data, length); nsresult rv = service->SendSessionBinaryMsg(mId, mRole, msgString); if(NS_WARN_IF(NS_FAILED(rv))) { AsyncCloseConnectionWithErrorMsg( NS_LITERAL_STRING("Unable to send binary message for ArrayBuffer message.")); } }
void AudioContext::DecodeAudioData(const ArrayBuffer& aBuffer, DecodeSuccessCallback& aSuccessCallback, const Optional<OwningNonNull<DecodeErrorCallback> >& aFailureCallback) { AutoJSAPI jsapi; jsapi.Init(); JSContext* cx = jsapi.cx(); JSAutoCompartment ac(cx, aBuffer.Obj()); aBuffer.ComputeLengthAndData(); // Neuter the array buffer size_t length = aBuffer.Length(); JS::RootedObject obj(cx, aBuffer.Obj()); uint8_t* data = static_cast<uint8_t*>(JS_StealArrayBufferContents(cx, obj)); // Sniff the content of the media. // Failed type sniffing will be handled by AsyncDecodeMedia. nsAutoCString contentType; NS_SniffContent(NS_DATA_SNIFFER_CATEGORY, nullptr, data, length, contentType); nsRefPtr<DecodeErrorCallback> failureCallback; if (aFailureCallback.WasPassed()) { failureCallback = &aFailureCallback.Value(); } nsRefPtr<WebAudioDecodeJob> job( new WebAudioDecodeJob(contentType, this, &aSuccessCallback, failureCallback)); mDecoder.AsyncDecodeMedia(contentType.get(), data, length, *job); // Transfer the ownership to mDecodeJobs mDecodeJobs.AppendElement(job); }
// Constructor of BluetoothGattDescriptor in ATT server role BluetoothGattDescriptor::BluetoothGattDescriptor( nsPIDOMWindow* aOwner, BluetoothGattCharacteristic* aCharacteristic, const nsAString& aDescriptorUuid, const GattPermissions& aPermissions, const ArrayBuffer& aValue) : mOwner(aOwner) , mCharacteristic(aCharacteristic) , mUuidStr(aDescriptorUuid) , mPermissions(BLUETOOTH_EMPTY_GATT_ATTR_PERM) , mAttRole(ATT_SERVER_ROLE) , mActive(false) { MOZ_ASSERT(aOwner); MOZ_ASSERT(aCharacteristic); // UUID memset(&mDescriptorId, 0, sizeof(mDescriptorId)); StringToUuid(aDescriptorUuid, mDescriptorId.mUuid); // permissions GattPermissionsToBits(aPermissions, mPermissions); // value aValue.ComputeLengthAndData(); mValue.AppendElements(aValue.Data(), aValue.Length()); }
void SourceBuffer::AppendBuffer(const ArrayBuffer& aData, ErrorResult& aRv) { MOZ_ASSERT(NS_IsMainThread()); MSE_API("AppendBuffer(ArrayBuffer)"); aData.ComputeLengthAndData(); AppendData(aData.Data(), aData.Length(), aRv); }
already_AddRefed<Promise> AudioContext::DecodeAudioData(const ArrayBuffer& aBuffer, const Optional<OwningNonNull<DecodeSuccessCallback> >& aSuccessCallback, const Optional<OwningNonNull<DecodeErrorCallback> >& aFailureCallback, ErrorResult& aRv) { nsCOMPtr<nsIGlobalObject> parentObject = do_QueryInterface(GetParentObject()); RefPtr<Promise> promise; AutoJSAPI jsapi; jsapi.Init(); JSContext* cx = jsapi.cx(); JSAutoCompartment ac(cx, aBuffer.Obj()); promise = Promise::Create(parentObject, aRv); if (aRv.Failed()) { return nullptr; } aBuffer.ComputeLengthAndData(); if (aBuffer.IsShared()) { // Throw if the object is mapping shared memory (must opt in). aRv.ThrowTypeError<MSG_TYPEDARRAY_IS_SHARED>(NS_LITERAL_STRING("Argument of AudioContext.decodeAudioData")); return nullptr; } // Detach the array buffer size_t length = aBuffer.Length(); JS::RootedObject obj(cx, aBuffer.Obj()); uint8_t* data = static_cast<uint8_t*>(JS_StealArrayBufferContents(cx, obj)); // Sniff the content of the media. // Failed type sniffing will be handled by AsyncDecodeWebAudio. nsAutoCString contentType; NS_SniffContent(NS_DATA_SNIFFER_CATEGORY, nullptr, data, length, contentType); RefPtr<DecodeErrorCallback> failureCallback; RefPtr<DecodeSuccessCallback> successCallback; if (aFailureCallback.WasPassed()) { failureCallback = &aFailureCallback.Value(); } if (aSuccessCallback.WasPassed()) { successCallback = &aSuccessCallback.Value(); } RefPtr<WebAudioDecodeJob> job( new WebAudioDecodeJob(contentType, this, promise, successCallback, failureCallback)); AsyncDecodeWebAudio(contentType.get(), data, length, *job); // Transfer the ownership to mDecodeJobs mDecodeJobs.AppendElement(job.forget()); return promise.forget(); }
void nsDOMDataChannel::Send(const ArrayBuffer& aData, ErrorResult& aRv) { NS_ABORT_IF_FALSE(NS_IsMainThread(), "Not running on main thread"); MOZ_ASSERT(sizeof(*aData.Data()) == 1); uint32_t len = aData.Length(); char* data = reinterpret_cast<char*>(aData.Data()); nsDependentCSubstring msgString(data, len); Send(nullptr, msgString, len, true, aRv); }
void AudioContext::DecodeAudioData(const ArrayBuffer& aBuffer, DecodeSuccessCallback& aSuccessCallback, const Optional<OwningNonNull<DecodeErrorCallback> >& aFailureCallback) { // Sniff the content of the media. // Failed type sniffing will be handled by AsyncDecodeMedia. nsAutoCString contentType; NS_SniffContent(NS_DATA_SNIFFER_CATEGORY, nullptr, aBuffer.Data(), aBuffer.Length(), contentType); nsCOMPtr<DecodeErrorCallback> failureCallback; if (aFailureCallback.WasPassed()) { failureCallback = &aFailureCallback.Value(); } nsRefPtr<WebAudioDecodeJob> job( new WebAudioDecodeJob(contentType, this, aBuffer, &aSuccessCallback, failureCallback)); mDecoder.AsyncDecodeMedia(contentType.get(), aBuffer.Data(), aBuffer.Length(), *job); // Transfer the ownership to mDecodeJobs mDecodeJobs.AppendElement(job.forget()); }
void nsDOMDataChannel::Send(const ArrayBuffer& aData, ErrorResult& aRv) { MOZ_ASSERT(NS_IsMainThread(), "Not running on main thread"); aData.ComputeLengthAndData(); static_assert(sizeof(*aData.Data()) == 1, "byte-sized data required"); uint32_t len = aData.Length(); char* data = reinterpret_cast<char*>(aData.Data()); nsDependentCSubstring msgString(data, len); Send(nullptr, msgString, len, true, aRv); }
already_AddRefed<Promise> AudioContext::DecodeAudioData(const ArrayBuffer& aBuffer, const Optional<OwningNonNull<DecodeSuccessCallback> >& aSuccessCallback, const Optional<OwningNonNull<DecodeErrorCallback> >& aFailureCallback) { ErrorResult rv; nsCOMPtr<nsIGlobalObject> parentObject = do_QueryInterface(GetParentObject()); nsRefPtr<Promise> promise; AutoJSAPI jsapi; jsapi.Init(); JSContext* cx = jsapi.cx(); JSAutoCompartment ac(cx, aBuffer.Obj()); promise = Promise::Create(parentObject, rv); if (rv.Failed()) { return nullptr; } aBuffer.ComputeLengthAndData(); // Neuter the array buffer size_t length = aBuffer.Length(); JS::RootedObject obj(cx, aBuffer.Obj()); uint8_t* data = static_cast<uint8_t*>(JS_StealArrayBufferContents(cx, obj)); // Sniff the content of the media. // Failed type sniffing will be handled by AsyncDecodeMedia. nsAutoCString contentType; NS_SniffContent(NS_DATA_SNIFFER_CATEGORY, nullptr, data, length, contentType); nsRefPtr<DecodeErrorCallback> failureCallback; nsRefPtr<DecodeSuccessCallback> successCallback; if (aFailureCallback.WasPassed()) { failureCallback = &aFailureCallback.Value(); } if (aSuccessCallback.WasPassed()) { successCallback = &aSuccessCallback.Value(); } nsRefPtr<WebAudioDecodeJob> job( new WebAudioDecodeJob(contentType, this, promise, successCallback, failureCallback)); mDecoder.AsyncDecodeMedia(contentType.get(), data, length, *job); // Transfer the ownership to mDecodeJobs mDecodeJobs.AppendElement(job.forget()); return promise.forget(); }
// static already_AddRefed<nsIInputStream> FileHandleBase::GetInputStream(const ArrayBuffer& aValue, uint64_t* aInputLength, ErrorResult& aRv) { aValue.ComputeLengthAndData(); const char* data = reinterpret_cast<const char*>(aValue.Data()); uint32_t length = aValue.Length(); nsCOMPtr<nsIInputStream> stream; aRv = NS_NewByteInputStream(getter_AddRefs(stream), data, length, NS_ASSIGNMENT_COPY); if (aRv.Failed()) { return nullptr; } *aInputLength = length; return stream.forget(); }
nsresult TCPSocketChild::SendSend(const ArrayBuffer& aData, uint32_t aByteOffset, uint32_t aByteLength, uint32_t aTrackingNumber) { uint32_t buflen = aData.Length(); uint32_t offset = std::min(buflen, aByteOffset); uint32_t nbytes = std::min(buflen - aByteOffset, aByteLength); FallibleTArray<uint8_t> fallibleArr; if (!fallibleArr.InsertElementsAt(0, aData.Data() + offset, nbytes, fallible)) { return NS_ERROR_OUT_OF_MEMORY; } InfallibleTArray<uint8_t> arr; arr.SwapElements(fallibleArr); SendData(arr, aTrackingNumber); return NS_OK; }
already_AddRefed<Promise> BluetoothGattCharacteristic::WriteValue(const ArrayBuffer& aValue, ErrorResult& aRv) { nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(GetParentObject()); if (!global) { aRv.Throw(NS_ERROR_FAILURE); return nullptr; } nsRefPtr<Promise> promise = Promise::Create(global, aRv); NS_ENSURE_TRUE(!aRv.Failed(), nullptr); BT_ENSURE_TRUE_REJECT(mProperties & (GATT_CHAR_PROP_BIT_WRITE_NO_RESPONSE | GATT_CHAR_PROP_BIT_WRITE | GATT_CHAR_PROP_BIT_SIGNED_WRITE), promise, NS_ERROR_NOT_AVAILABLE); aValue.ComputeLengthAndData(); nsTArray<uint8_t> value; value.AppendElements(aValue.Data(), aValue.Length()); BluetoothService* bs = BluetoothService::Get(); BT_ENSURE_TRUE_REJECT(bs, promise, NS_ERROR_NOT_AVAILABLE); nsRefPtr<BluetoothReplyRunnable> result = new BluetoothVoidReplyRunnable( nullptr, promise, NS_LITERAL_STRING("GattClientWriteCharacteristicValue")); bs->GattClientWriteCharacteristicValueInternal(mService->GetAppUuid(), mService->GetServiceId(), mCharId, mWriteType, value, result); return promise.forget(); }