1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/camera/CameraRecorderProfiles.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,195 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#include "CameraRecorderProfiles.h" 1.9 +#include "jsapi.h" 1.10 +#include "CameraCommon.h" 1.11 + 1.12 +using namespace mozilla; 1.13 + 1.14 +/** 1.15 + * Video profile implementation. 1.16 + */ 1.17 +RecorderVideoProfile::RecorderVideoProfile(uint32_t aCameraId, uint32_t aQualityIndex) 1.18 + : mCameraId(aCameraId) 1.19 + , mQualityIndex(aQualityIndex) 1.20 +{ 1.21 + DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this); 1.22 +} 1.23 + 1.24 +RecorderVideoProfile::~RecorderVideoProfile() 1.25 +{ 1.26 + DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this); 1.27 +} 1.28 + 1.29 +nsresult 1.30 +RecorderVideoProfile::GetJsObject(JSContext* aCx, JSObject** aObject) 1.31 +{ 1.32 + NS_ENSURE_TRUE(aObject, NS_ERROR_INVALID_ARG); 1.33 + 1.34 + JS::Rooted<JSObject*> o(aCx, JS_NewObject(aCx, nullptr, JS::NullPtr(), JS::NullPtr())); 1.35 + NS_ENSURE_TRUE(o, NS_ERROR_OUT_OF_MEMORY); 1.36 + 1.37 + const char* codec = GetCodecName(); 1.38 + NS_ENSURE_TRUE(codec, NS_ERROR_FAILURE); 1.39 + 1.40 + JS::Rooted<JSString*> s(aCx, JS_NewStringCopyZ(aCx, codec)); 1.41 + JS::Rooted<JS::Value> v(aCx, STRING_TO_JSVAL(s)); 1.42 + if (!JS_SetProperty(aCx, o, "codec", v)) { 1.43 + return NS_ERROR_FAILURE; 1.44 + } 1.45 + 1.46 + if (mBitrate != -1) { 1.47 + v = INT_TO_JSVAL(mBitrate); 1.48 + if (!JS_SetProperty(aCx, o, "bitrate", v)) { 1.49 + return NS_ERROR_FAILURE; 1.50 + } 1.51 + } 1.52 + if (mFramerate != -1) { 1.53 + v = INT_TO_JSVAL(mFramerate); 1.54 + if (!JS_SetProperty(aCx, o, "framerate", v)) { 1.55 + return NS_ERROR_FAILURE; 1.56 + } 1.57 + } 1.58 + if (mWidth != -1) { 1.59 + v = INT_TO_JSVAL(mWidth); 1.60 + if (!JS_SetProperty(aCx, o, "width", v)) { 1.61 + return NS_ERROR_FAILURE; 1.62 + } 1.63 + } 1.64 + if (mHeight != -1) { 1.65 + v = INT_TO_JSVAL(mHeight); 1.66 + if (!JS_SetProperty(aCx, o, "height", v)) { 1.67 + return NS_ERROR_FAILURE; 1.68 + } 1.69 + } 1.70 + 1.71 + *aObject = o; 1.72 + return NS_OK; 1.73 +} 1.74 + 1.75 +/** 1.76 + * Audio profile implementation. 1.77 + */ 1.78 +RecorderAudioProfile::RecorderAudioProfile(uint32_t aCameraId, uint32_t aQualityIndex) 1.79 + : mCameraId(aCameraId) 1.80 + , mQualityIndex(aQualityIndex) 1.81 +{ 1.82 + DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this); 1.83 +} 1.84 + 1.85 +RecorderAudioProfile::~RecorderAudioProfile() 1.86 +{ 1.87 + DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this); 1.88 +} 1.89 + 1.90 +nsresult 1.91 +RecorderAudioProfile::GetJsObject(JSContext* aCx, JSObject** aObject) 1.92 +{ 1.93 + NS_ENSURE_TRUE(aObject, NS_ERROR_INVALID_ARG); 1.94 + 1.95 + JS::Rooted<JSObject*> o(aCx, JS_NewObject(aCx, nullptr, JS::NullPtr(), JS::NullPtr())); 1.96 + NS_ENSURE_TRUE(o, NS_ERROR_OUT_OF_MEMORY); 1.97 + 1.98 + const char* codec = GetCodecName(); 1.99 + NS_ENSURE_TRUE(codec, NS_ERROR_FAILURE); 1.100 + 1.101 + JS::Rooted<JSString*> s(aCx, JS_NewStringCopyZ(aCx, codec)); 1.102 + JS::Rooted<JS::Value> v(aCx, STRING_TO_JSVAL(s)); 1.103 + if (!JS_SetProperty(aCx, o, "codec", v)) { 1.104 + return NS_ERROR_FAILURE; 1.105 + } 1.106 + 1.107 + if (mBitrate != -1) { 1.108 + v = INT_TO_JSVAL(mBitrate); 1.109 + if (!JS_SetProperty(aCx, o, "bitrate", v)) { 1.110 + return NS_ERROR_FAILURE; 1.111 + } 1.112 + } 1.113 + if (mSamplerate != -1) { 1.114 + v = INT_TO_JSVAL(mSamplerate); 1.115 + if (!JS_SetProperty(aCx, o, "samplerate", v)) { 1.116 + return NS_ERROR_FAILURE; 1.117 + } 1.118 + } 1.119 + if (mChannels != -1) { 1.120 + v = INT_TO_JSVAL(mChannels); 1.121 + if (!JS_SetProperty(aCx, o, "channels", v)) { 1.122 + return NS_ERROR_FAILURE; 1.123 + } 1.124 + } 1.125 + 1.126 + *aObject = o; 1.127 + return NS_OK; 1.128 +} 1.129 + 1.130 +/** 1.131 + * Recorder Profile 1.132 + */ 1.133 +RecorderProfile::RecorderProfile(uint32_t aCameraId, uint32_t aQualityIndex) 1.134 + : mCameraId(aCameraId) 1.135 + , mQualityIndex(aQualityIndex) 1.136 + , mName(nullptr) 1.137 +{ 1.138 + DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this); 1.139 +} 1.140 + 1.141 +RecorderProfile::~RecorderProfile() 1.142 +{ 1.143 + DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this); 1.144 +} 1.145 + 1.146 +/** 1.147 + * Recorder profile manager implementation. 1.148 + */ 1.149 +RecorderProfileManager::RecorderProfileManager(uint32_t aCameraId) 1.150 + : mCameraId(aCameraId) 1.151 +{ 1.152 + DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this); 1.153 +} 1.154 + 1.155 +RecorderProfileManager::~RecorderProfileManager() 1.156 +{ 1.157 + DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this); 1.158 +} 1.159 + 1.160 +nsresult 1.161 +RecorderProfileManager::GetJsObject(JSContext* aCx, JSObject** aObject) const 1.162 +{ 1.163 + NS_ENSURE_TRUE(aObject, NS_ERROR_INVALID_ARG); 1.164 + 1.165 + JS::Rooted<JSObject*> o(aCx, JS_NewObject(aCx, nullptr, JS::NullPtr(), JS::NullPtr())); 1.166 + if (!o) { 1.167 + return NS_ERROR_OUT_OF_MEMORY; 1.168 + } 1.169 + 1.170 + for (uint32_t q = 0; q < GetMaxQualityIndex(); ++q) { 1.171 + if (!IsSupported(q)) { 1.172 + continue; 1.173 + } 1.174 + 1.175 + nsRefPtr<RecorderProfile> profile = Get(q); 1.176 + if (!profile) { 1.177 + return NS_ERROR_OUT_OF_MEMORY; 1.178 + } 1.179 + 1.180 + const char* profileName = profile->GetName(); 1.181 + if (!profileName) { 1.182 + // don't allow anonymous recorder profiles 1.183 + continue; 1.184 + } 1.185 + 1.186 + JS::Rooted<JSObject*> p(aCx); 1.187 + nsresult rv = profile->GetJsObject(aCx, p.address()); 1.188 + NS_ENSURE_SUCCESS(rv, rv); 1.189 + JS::Rooted<JS::Value> v(aCx, OBJECT_TO_JSVAL(p)); 1.190 + 1.191 + if (!JS_SetProperty(aCx, o, profileName, v)) { 1.192 + return NS_ERROR_FAILURE; 1.193 + } 1.194 + } 1.195 + 1.196 + *aObject = o; 1.197 + return NS_OK; 1.198 +}