dom/camera/GonkRecorderProfiles.cpp

Thu, 15 Jan 2015 15:55:04 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:55:04 +0100
branch
TOR_BUG_9701
changeset 9
a63d609f5ebe
permissions
-rw-r--r--

Back out 97036ab72558 which inappropriately compared turds to third parties.

michael@0 1 /*
michael@0 2 * Copyright (C) 2012 Mozilla Foundation
michael@0 3 *
michael@0 4 * Licensed under the Apache License, Version 2.0 (the "License");
michael@0 5 * you may not use this file except in compliance with the License.
michael@0 6 * You may obtain a copy of the License at
michael@0 7 *
michael@0 8 * http://www.apache.org/licenses/LICENSE-2.0
michael@0 9 *
michael@0 10 * Unless required by applicable law or agreed to in writing, software
michael@0 11 * distributed under the License is distributed on an "AS IS" BASIS,
michael@0 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
michael@0 13 * See the License for the specific language governing permissions and
michael@0 14 * limitations under the License.
michael@0 15 */
michael@0 16
michael@0 17 #include "GonkRecorderProfiles.h"
michael@0 18 #include <media/MediaProfiles.h>
michael@0 19 #include "GonkRecorder.h"
michael@0 20 #include "CameraControlImpl.h"
michael@0 21 #include "CameraCommon.h"
michael@0 22
michael@0 23 using namespace mozilla;
michael@0 24 using namespace android;
michael@0 25
michael@0 26 #define DEF_GONK_RECORDER_PROFILE(e, n) e##_INDEX,
michael@0 27 enum {
michael@0 28 #include "GonkRecorderProfiles.def"
michael@0 29 PROFILE_COUNT
michael@0 30 };
michael@0 31
michael@0 32 #define DEF_GONK_RECORDER_PROFILE(e, n) { n, e },
michael@0 33 static struct {
michael@0 34 const char* name;
michael@0 35 int quality;
michael@0 36 } ProfileList[] = {
michael@0 37 #include "GonkRecorderProfiles.def"
michael@0 38 { nullptr, 0 }
michael@0 39 };
michael@0 40
michael@0 41 static MediaProfiles* sMediaProfiles = nullptr;
michael@0 42
michael@0 43 static bool
michael@0 44 IsQualitySupported(uint32_t aCameraId, uint32_t aQualityIndex)
michael@0 45 {
michael@0 46 if (!sMediaProfiles) {
michael@0 47 sMediaProfiles = MediaProfiles::getInstance();
michael@0 48 }
michael@0 49 camcorder_quality q = static_cast<camcorder_quality>(ProfileList[aQualityIndex].quality);
michael@0 50 return sMediaProfiles->hasCamcorderProfile(static_cast<int>(aCameraId), q);
michael@0 51 }
michael@0 52
michael@0 53 static int
michael@0 54 GetProfileParam(uint32_t aCameraId, uint32_t aQualityIndex, const char* aParam)
michael@0 55 {
michael@0 56 if (!sMediaProfiles) {
michael@0 57 sMediaProfiles = MediaProfiles::getInstance();
michael@0 58 }
michael@0 59 camcorder_quality q = static_cast<camcorder_quality>(ProfileList[aQualityIndex].quality);
michael@0 60 return sMediaProfiles->getCamcorderProfileParamByName(aParam, static_cast<int>(aCameraId), q);
michael@0 61 }
michael@0 62
michael@0 63 /**
michael@0 64 * Recorder profile.
michael@0 65 */
michael@0 66 static RecorderProfile::FileFormat
michael@0 67 TranslateFileFormat(output_format aFileFormat)
michael@0 68 {
michael@0 69 switch (aFileFormat) {
michael@0 70 case OUTPUT_FORMAT_THREE_GPP: return RecorderProfile::THREE_GPP;
michael@0 71 case OUTPUT_FORMAT_MPEG_4: return RecorderProfile::MPEG4;
michael@0 72 default: return RecorderProfile::UNKNOWN;
michael@0 73 }
michael@0 74 }
michael@0 75
michael@0 76 GonkRecorderProfile::GonkRecorderProfile(uint32_t aCameraId, uint32_t aQualityIndex)
michael@0 77 : RecorderProfileBase<GonkRecorderAudioProfile, GonkRecorderVideoProfile>(aCameraId, aQualityIndex)
michael@0 78 {
michael@0 79 DOM_CAMERA_LOGT("%s:%d : this=%p, mCameraId=%d, mQualityIndex=%d\n", __func__, __LINE__, this, mCameraId, mQualityIndex);
michael@0 80 mPlatformOutputFormat = static_cast<output_format>(GetProfileParam(mCameraId, mQualityIndex, "file.format"));
michael@0 81 mFileFormat = TranslateFileFormat(mPlatformOutputFormat);
michael@0 82 if (aQualityIndex < PROFILE_COUNT) {
michael@0 83 mName = ProfileList[aQualityIndex].name;
michael@0 84 DOM_CAMERA_LOGI("Created camera %d profile index %d: '%s'\n", mCameraId, mQualityIndex, mName);
michael@0 85 }
michael@0 86 }
michael@0 87
michael@0 88 GonkRecorderProfile::~GonkRecorderProfile()
michael@0 89 {
michael@0 90 DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this);
michael@0 91 }
michael@0 92
michael@0 93 nsresult
michael@0 94 GonkRecorderProfile::ConfigureRecorder(GonkRecorder* aRecorder)
michael@0 95 {
michael@0 96 static const size_t SIZE = 256;
michael@0 97 char buffer[SIZE];
michael@0 98
michael@0 99 // set all the params
michael@0 100 CHECK_SETARG(aRecorder->setAudioSource(AUDIO_SOURCE_CAMCORDER));
michael@0 101 CHECK_SETARG(aRecorder->setVideoSource(VIDEO_SOURCE_CAMERA));
michael@0 102 CHECK_SETARG(aRecorder->setOutputFormat(GetOutputFormat()));
michael@0 103 CHECK_SETARG(aRecorder->setVideoFrameRate(mVideo.GetFramerate()));
michael@0 104 CHECK_SETARG(aRecorder->setVideoSize(mVideo.GetWidth(), mVideo.GetHeight()));
michael@0 105 CHECK_SETARG(aRecorder->setVideoEncoder(mVideo.GetPlatformCodec()));
michael@0 106 CHECK_SETARG(aRecorder->setAudioEncoder(mAudio.GetPlatformCodec()));
michael@0 107
michael@0 108 snprintf(buffer, SIZE, "video-param-encoding-bitrate=%d", mVideo.GetBitrate());
michael@0 109 CHECK_SETARG(aRecorder->setParameters(String8(buffer)));
michael@0 110
michael@0 111 snprintf(buffer, SIZE, "audio-param-encoding-bitrate=%d", mAudio.GetBitrate());
michael@0 112 CHECK_SETARG(aRecorder->setParameters(String8(buffer)));
michael@0 113
michael@0 114 snprintf(buffer, SIZE, "audio-param-number-of-channels=%d", mAudio.GetChannels());
michael@0 115 CHECK_SETARG(aRecorder->setParameters(String8(buffer)));
michael@0 116
michael@0 117 snprintf(buffer, SIZE, "audio-param-sampling-rate=%d", mAudio.GetSamplerate());
michael@0 118 CHECK_SETARG(aRecorder->setParameters(String8(buffer)));
michael@0 119
michael@0 120 return NS_OK;
michael@0 121 }
michael@0 122
michael@0 123 /**
michael@0 124 * Recorder audio profile.
michael@0 125 */
michael@0 126 static RecorderAudioProfile::Codec
michael@0 127 TranslateAudioCodec(audio_encoder aCodec)
michael@0 128 {
michael@0 129 switch (aCodec) {
michael@0 130 case AUDIO_ENCODER_AMR_NB: return RecorderAudioProfile::AMRNB;
michael@0 131 case AUDIO_ENCODER_AMR_WB: return RecorderAudioProfile::AMRWB;
michael@0 132 case AUDIO_ENCODER_AAC: return RecorderAudioProfile::AAC;
michael@0 133 default: return RecorderAudioProfile::UNKNOWN;
michael@0 134 }
michael@0 135 }
michael@0 136
michael@0 137 GonkRecorderAudioProfile::GonkRecorderAudioProfile(uint32_t aCameraId, uint32_t aQualityIndex)
michael@0 138 : RecorderAudioProfile(aCameraId, aQualityIndex)
michael@0 139 {
michael@0 140 DOM_CAMERA_LOGT("%s:%d : this=%p, mCameraId=%d, mQualityIndex=%d\n", __func__, __LINE__, this, mCameraId, mQualityIndex);
michael@0 141 mPlatformCodec = static_cast<audio_encoder>(GetProfileParam(mCameraId, mQualityIndex, "aud.codec"));
michael@0 142 mCodec = TranslateAudioCodec(mPlatformCodec);
michael@0 143 mBitrate = GetProfileParam(mCameraId, mQualityIndex, "aud.bps");
michael@0 144 mSamplerate = GetProfileParam(mCameraId, mQualityIndex, "aud.hz");
michael@0 145 mChannels = GetProfileParam(mCameraId, mQualityIndex, "aud.ch");
michael@0 146 }
michael@0 147
michael@0 148 GonkRecorderAudioProfile::~GonkRecorderAudioProfile()
michael@0 149 {
michael@0 150 DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this);
michael@0 151 }
michael@0 152
michael@0 153 /**
michael@0 154 * Recorder video profile.
michael@0 155 */
michael@0 156 static RecorderVideoProfile::Codec
michael@0 157 TranslateVideoCodec(video_encoder aCodec)
michael@0 158 {
michael@0 159 switch (aCodec) {
michael@0 160 case VIDEO_ENCODER_H263: return RecorderVideoProfile::H263;
michael@0 161 case VIDEO_ENCODER_H264: return RecorderVideoProfile::H264;
michael@0 162 case VIDEO_ENCODER_MPEG_4_SP: return RecorderVideoProfile::MPEG4SP;
michael@0 163 default: return RecorderVideoProfile::UNKNOWN;
michael@0 164 }
michael@0 165 }
michael@0 166
michael@0 167 GonkRecorderVideoProfile::GonkRecorderVideoProfile(uint32_t aCameraId, uint32_t aQualityIndex)
michael@0 168 : RecorderVideoProfile(aCameraId, aQualityIndex)
michael@0 169 {
michael@0 170 DOM_CAMERA_LOGT("%s:%d : this=%p, mCameraId=%d, mQualityIndex=%d\n", __func__, __LINE__, this, mCameraId, mQualityIndex);
michael@0 171 mPlatformCodec = static_cast<video_encoder>(GetProfileParam(mCameraId, mQualityIndex, "vid.codec"));
michael@0 172 mCodec = TranslateVideoCodec(mPlatformCodec);
michael@0 173 mBitrate = GetProfileParam(mCameraId, mQualityIndex, "vid.bps");
michael@0 174 mFramerate = GetProfileParam(mCameraId, mQualityIndex, "vid.fps");
michael@0 175 mWidth = GetProfileParam(mCameraId, mQualityIndex, "vid.width");
michael@0 176 mHeight = GetProfileParam(mCameraId, mQualityIndex, "vid.height");
michael@0 177 }
michael@0 178
michael@0 179 GonkRecorderVideoProfile::~GonkRecorderVideoProfile()
michael@0 180 {
michael@0 181 DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this);
michael@0 182 }
michael@0 183
michael@0 184 GonkRecorderProfileManager::GonkRecorderProfileManager(uint32_t aCameraId)
michael@0 185 : RecorderProfileManager(aCameraId)
michael@0 186 {
michael@0 187 DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this);
michael@0 188 mMaxQualityIndex = sizeof(ProfileList) / sizeof(ProfileList[0]) - 1;
michael@0 189 }
michael@0 190
michael@0 191 GonkRecorderProfileManager::~GonkRecorderProfileManager()
michael@0 192 {
michael@0 193 DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this);
michael@0 194 }
michael@0 195
michael@0 196 bool
michael@0 197 GonkRecorderProfileManager::IsSupported(uint32_t aQualityIndex) const
michael@0 198 {
michael@0 199 if (!IsQualitySupported(mCameraId, aQualityIndex)) {
michael@0 200 // This profile is not supported
michael@0 201 return false;
michael@0 202 }
michael@0 203
michael@0 204 int width = GetProfileParam(mCameraId, aQualityIndex, "vid.width");
michael@0 205 int height = GetProfileParam(mCameraId, aQualityIndex, "vid.height");
michael@0 206 if (width == -1 || height == -1) {
michael@0 207 // This would be unexpected, but we handle it just in case
michael@0 208 DOM_CAMERA_LOGE("Camera %d recorder profile %d has width=%d, height=%d\n", mCameraId, aQualityIndex, width, height);
michael@0 209 return false;
michael@0 210 }
michael@0 211
michael@0 212 for (uint32_t i = 0; i < mSupportedSizes.Length(); ++i) {
michael@0 213 if (static_cast<uint32_t>(width) == mSupportedSizes[i].width &&
michael@0 214 static_cast<uint32_t>(height) == mSupportedSizes[i].height)
michael@0 215 {
michael@0 216 return true;
michael@0 217 }
michael@0 218 }
michael@0 219 return false;
michael@0 220 }
michael@0 221
michael@0 222 already_AddRefed<RecorderProfile>
michael@0 223 GonkRecorderProfileManager::Get(uint32_t aQualityIndex) const
michael@0 224 {
michael@0 225 // This overrides virtual RecorderProfileManager::Get(...)
michael@0 226 DOM_CAMERA_LOGT("%s:%d : aQualityIndex=%d\n", __func__, __LINE__, aQualityIndex);
michael@0 227 nsRefPtr<RecorderProfile> profile = new GonkRecorderProfile(mCameraId, aQualityIndex);
michael@0 228 return profile.forget();
michael@0 229 }
michael@0 230
michael@0 231 already_AddRefed<GonkRecorderProfile>
michael@0 232 GonkRecorderProfileManager::Get(const char* aProfileName) const
michael@0 233 {
michael@0 234 DOM_CAMERA_LOGT("%s:%d : aProfileName='%s'\n", __func__, __LINE__, aProfileName);
michael@0 235 for (uint32_t i = 0; i < mMaxQualityIndex; ++i) {
michael@0 236 if (strcmp(ProfileList[i].name, aProfileName) == 0) {
michael@0 237 nsRefPtr<GonkRecorderProfile> profile = nullptr;
michael@0 238 if (IsSupported(i)) {
michael@0 239 profile = new GonkRecorderProfile(mCameraId, i);
michael@0 240 return profile.forget();
michael@0 241 }
michael@0 242 return nullptr;
michael@0 243 }
michael@0 244 }
michael@0 245
michael@0 246 DOM_CAMERA_LOGW("Couldn't file recorder profile named '%s'\n", aProfileName);
michael@0 247 return nullptr;
michael@0 248 }

mercurial