michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "CameraRecorderProfiles.h" michael@0: #include "jsapi.h" michael@0: #include "CameraCommon.h" michael@0: michael@0: using namespace mozilla; michael@0: michael@0: /** michael@0: * Video profile implementation. michael@0: */ michael@0: RecorderVideoProfile::RecorderVideoProfile(uint32_t aCameraId, uint32_t aQualityIndex) michael@0: : mCameraId(aCameraId) michael@0: , mQualityIndex(aQualityIndex) michael@0: { michael@0: DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this); michael@0: } michael@0: michael@0: RecorderVideoProfile::~RecorderVideoProfile() michael@0: { michael@0: DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this); michael@0: } michael@0: michael@0: nsresult michael@0: RecorderVideoProfile::GetJsObject(JSContext* aCx, JSObject** aObject) michael@0: { michael@0: NS_ENSURE_TRUE(aObject, NS_ERROR_INVALID_ARG); michael@0: michael@0: JS::Rooted o(aCx, JS_NewObject(aCx, nullptr, JS::NullPtr(), JS::NullPtr())); michael@0: NS_ENSURE_TRUE(o, NS_ERROR_OUT_OF_MEMORY); michael@0: michael@0: const char* codec = GetCodecName(); michael@0: NS_ENSURE_TRUE(codec, NS_ERROR_FAILURE); michael@0: michael@0: JS::Rooted s(aCx, JS_NewStringCopyZ(aCx, codec)); michael@0: JS::Rooted v(aCx, STRING_TO_JSVAL(s)); michael@0: if (!JS_SetProperty(aCx, o, "codec", v)) { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: if (mBitrate != -1) { michael@0: v = INT_TO_JSVAL(mBitrate); michael@0: if (!JS_SetProperty(aCx, o, "bitrate", v)) { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: } michael@0: if (mFramerate != -1) { michael@0: v = INT_TO_JSVAL(mFramerate); michael@0: if (!JS_SetProperty(aCx, o, "framerate", v)) { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: } michael@0: if (mWidth != -1) { michael@0: v = INT_TO_JSVAL(mWidth); michael@0: if (!JS_SetProperty(aCx, o, "width", v)) { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: } michael@0: if (mHeight != -1) { michael@0: v = INT_TO_JSVAL(mHeight); michael@0: if (!JS_SetProperty(aCx, o, "height", v)) { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: } michael@0: michael@0: *aObject = o; michael@0: return NS_OK; michael@0: } michael@0: michael@0: /** michael@0: * Audio profile implementation. michael@0: */ michael@0: RecorderAudioProfile::RecorderAudioProfile(uint32_t aCameraId, uint32_t aQualityIndex) michael@0: : mCameraId(aCameraId) michael@0: , mQualityIndex(aQualityIndex) michael@0: { michael@0: DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this); michael@0: } michael@0: michael@0: RecorderAudioProfile::~RecorderAudioProfile() michael@0: { michael@0: DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this); michael@0: } michael@0: michael@0: nsresult michael@0: RecorderAudioProfile::GetJsObject(JSContext* aCx, JSObject** aObject) michael@0: { michael@0: NS_ENSURE_TRUE(aObject, NS_ERROR_INVALID_ARG); michael@0: michael@0: JS::Rooted o(aCx, JS_NewObject(aCx, nullptr, JS::NullPtr(), JS::NullPtr())); michael@0: NS_ENSURE_TRUE(o, NS_ERROR_OUT_OF_MEMORY); michael@0: michael@0: const char* codec = GetCodecName(); michael@0: NS_ENSURE_TRUE(codec, NS_ERROR_FAILURE); michael@0: michael@0: JS::Rooted s(aCx, JS_NewStringCopyZ(aCx, codec)); michael@0: JS::Rooted v(aCx, STRING_TO_JSVAL(s)); michael@0: if (!JS_SetProperty(aCx, o, "codec", v)) { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: if (mBitrate != -1) { michael@0: v = INT_TO_JSVAL(mBitrate); michael@0: if (!JS_SetProperty(aCx, o, "bitrate", v)) { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: } michael@0: if (mSamplerate != -1) { michael@0: v = INT_TO_JSVAL(mSamplerate); michael@0: if (!JS_SetProperty(aCx, o, "samplerate", v)) { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: } michael@0: if (mChannels != -1) { michael@0: v = INT_TO_JSVAL(mChannels); michael@0: if (!JS_SetProperty(aCx, o, "channels", v)) { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: } michael@0: michael@0: *aObject = o; michael@0: return NS_OK; michael@0: } michael@0: michael@0: /** michael@0: * Recorder Profile michael@0: */ michael@0: RecorderProfile::RecorderProfile(uint32_t aCameraId, uint32_t aQualityIndex) michael@0: : mCameraId(aCameraId) michael@0: , mQualityIndex(aQualityIndex) michael@0: , mName(nullptr) michael@0: { michael@0: DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this); michael@0: } michael@0: michael@0: RecorderProfile::~RecorderProfile() michael@0: { michael@0: DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this); michael@0: } michael@0: michael@0: /** michael@0: * Recorder profile manager implementation. michael@0: */ michael@0: RecorderProfileManager::RecorderProfileManager(uint32_t aCameraId) michael@0: : mCameraId(aCameraId) michael@0: { michael@0: DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this); michael@0: } michael@0: michael@0: RecorderProfileManager::~RecorderProfileManager() michael@0: { michael@0: DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this); michael@0: } michael@0: michael@0: nsresult michael@0: RecorderProfileManager::GetJsObject(JSContext* aCx, JSObject** aObject) const michael@0: { michael@0: NS_ENSURE_TRUE(aObject, NS_ERROR_INVALID_ARG); michael@0: michael@0: JS::Rooted o(aCx, JS_NewObject(aCx, nullptr, JS::NullPtr(), JS::NullPtr())); michael@0: if (!o) { michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: } michael@0: michael@0: for (uint32_t q = 0; q < GetMaxQualityIndex(); ++q) { michael@0: if (!IsSupported(q)) { michael@0: continue; michael@0: } michael@0: michael@0: nsRefPtr profile = Get(q); michael@0: if (!profile) { michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: } michael@0: michael@0: const char* profileName = profile->GetName(); michael@0: if (!profileName) { michael@0: // don't allow anonymous recorder profiles michael@0: continue; michael@0: } michael@0: michael@0: JS::Rooted p(aCx); michael@0: nsresult rv = profile->GetJsObject(aCx, p.address()); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: JS::Rooted v(aCx, OBJECT_TO_JSVAL(p)); michael@0: michael@0: if (!JS_SetProperty(aCx, o, profileName, v)) { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: } michael@0: michael@0: *aObject = o; michael@0: return NS_OK; michael@0: }