DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (d1ed7de67f5a)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
/*
 * Copyright (C) 2012 Mozilla Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "GonkRecorderProfiles.h"
#include <media/MediaProfiles.h>
#include "GonkRecorder.h"
#include "CameraControlImpl.h"
#include "CameraCommon.h"

using namespace mozilla;
using namespace android;

#define DEF_GONK_RECORDER_PROFILE(e, n) e##_INDEX,
enum {
  #include "GonkRecorderProfiles.def"
  PROFILE_COUNT
};

#define DEF_GONK_RECORDER_PROFILE(e, n) { n, e },
static struct {
  const char* name;
  int quality;
} ProfileList[] = {
  #include "GonkRecorderProfiles.def"
  { nullptr, 0 }
};

static MediaProfiles* sMediaProfiles = nullptr;

static bool
IsQualitySupported(uint32_t aCameraId, uint32_t aQualityIndex)
{
  if (!sMediaProfiles) {
    sMediaProfiles = MediaProfiles::getInstance();
  }
  camcorder_quality q = static_cast<camcorder_quality>(ProfileList[aQualityIndex].quality);
  return sMediaProfiles->hasCamcorderProfile(static_cast<int>(aCameraId), q);
}

static int
GetProfileParam(uint32_t aCameraId, uint32_t aQualityIndex, const char* aParam)
{
  if (!sMediaProfiles) {
    sMediaProfiles = MediaProfiles::getInstance();
  }
  camcorder_quality q = static_cast<camcorder_quality>(ProfileList[aQualityIndex].quality);
  return sMediaProfiles->getCamcorderProfileParamByName(aParam, static_cast<int>(aCameraId), q);
}

/**
 * Recorder profile.
 */
static RecorderProfile::FileFormat
TranslateFileFormat(output_format aFileFormat)
{
  switch (aFileFormat) {
    case OUTPUT_FORMAT_THREE_GPP: return RecorderProfile::THREE_GPP;
    case OUTPUT_FORMAT_MPEG_4:    return RecorderProfile::MPEG4;
    default:                      return RecorderProfile::UNKNOWN;
  }
}

GonkRecorderProfile::GonkRecorderProfile(uint32_t aCameraId, uint32_t aQualityIndex)
  : RecorderProfileBase<GonkRecorderAudioProfile, GonkRecorderVideoProfile>(aCameraId, aQualityIndex)
{
  DOM_CAMERA_LOGT("%s:%d : this=%p, mCameraId=%d, mQualityIndex=%d\n", __func__, __LINE__, this, mCameraId, mQualityIndex);
  mPlatformOutputFormat = static_cast<output_format>(GetProfileParam(mCameraId, mQualityIndex, "file.format"));
  mFileFormat = TranslateFileFormat(mPlatformOutputFormat);
  if (aQualityIndex < PROFILE_COUNT) {
    mName = ProfileList[aQualityIndex].name;
    DOM_CAMERA_LOGI("Created camera %d profile index %d: '%s'\n", mCameraId, mQualityIndex, mName);
  }
}

GonkRecorderProfile::~GonkRecorderProfile()
{
  DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this);
}

nsresult
GonkRecorderProfile::ConfigureRecorder(GonkRecorder* aRecorder)
{
  if (!aRecorder) {
    DOM_CAMERA_LOGW("ConfigureRecorder() called with null aRecorder\n");
    return NS_ERROR_INVALID_ARG;
  }

  static const size_t SIZE = 256;
  char buffer[SIZE];

  // set all the params
  CHECK_SETARG(aRecorder->setAudioSource(AUDIO_SOURCE_CAMCORDER));
  CHECK_SETARG(aRecorder->setVideoSource(VIDEO_SOURCE_CAMERA));
  CHECK_SETARG(aRecorder->setOutputFormat(GetOutputFormat()));
  CHECK_SETARG(aRecorder->setVideoFrameRate(mVideo.GetFramerate()));
  CHECK_SETARG(aRecorder->setVideoSize(mVideo.GetWidth(), mVideo.GetHeight()));
  CHECK_SETARG(aRecorder->setVideoEncoder(mVideo.GetPlatformCodec()));
  CHECK_SETARG(aRecorder->setAudioEncoder(mAudio.GetPlatformCodec()));

  snprintf(buffer, SIZE, "video-param-encoding-bitrate=%d", mVideo.GetBitrate());
  CHECK_SETARG(aRecorder->setParameters(String8(buffer)));

  snprintf(buffer, SIZE, "audio-param-encoding-bitrate=%d", mAudio.GetBitrate());
  CHECK_SETARG(aRecorder->setParameters(String8(buffer)));

  snprintf(buffer, SIZE, "audio-param-number-of-channels=%d", mAudio.GetChannels());
  CHECK_SETARG(aRecorder->setParameters(String8(buffer)));

  snprintf(buffer, SIZE, "audio-param-sampling-rate=%d", mAudio.GetSamplerate());
  CHECK_SETARG(aRecorder->setParameters(String8(buffer)));

  return NS_OK;
}

/**
 * Recorder audio profile.
 */
static RecorderAudioProfile::Codec
TranslateAudioCodec(audio_encoder aCodec)
{
  switch (aCodec) {
    case AUDIO_ENCODER_AMR_NB:  return RecorderAudioProfile::AMRNB;
    case AUDIO_ENCODER_AMR_WB:  return RecorderAudioProfile::AMRWB;
    case AUDIO_ENCODER_AAC:     return RecorderAudioProfile::AAC;
    default:                    return RecorderAudioProfile::UNKNOWN;
  }
}

GonkRecorderAudioProfile::GonkRecorderAudioProfile(uint32_t aCameraId, uint32_t aQualityIndex)
  : RecorderAudioProfile(aCameraId, aQualityIndex)
{
  DOM_CAMERA_LOGT("%s:%d : this=%p, mCameraId=%d, mQualityIndex=%d\n", __func__, __LINE__, this, mCameraId, mQualityIndex);
  mPlatformCodec = static_cast<audio_encoder>(GetProfileParam(mCameraId, mQualityIndex, "aud.codec"));
  mCodec = TranslateAudioCodec(mPlatformCodec);
  mBitrate = GetProfileParam(mCameraId, mQualityIndex, "aud.bps");
  mSamplerate = GetProfileParam(mCameraId, mQualityIndex, "aud.hz");
  mChannels = GetProfileParam(mCameraId, mQualityIndex, "aud.ch");
}

GonkRecorderAudioProfile::~GonkRecorderAudioProfile()
{
  DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this);
}

/**
 * Recorder video profile.
 */
static RecorderVideoProfile::Codec
TranslateVideoCodec(video_encoder aCodec)
{
  switch (aCodec) {
    case VIDEO_ENCODER_H263:      return RecorderVideoProfile::H263;
    case VIDEO_ENCODER_H264:      return RecorderVideoProfile::H264;
    case VIDEO_ENCODER_MPEG_4_SP: return RecorderVideoProfile::MPEG4SP;
    default:                      return RecorderVideoProfile::UNKNOWN;
  }
}

GonkRecorderVideoProfile::GonkRecorderVideoProfile(uint32_t aCameraId, uint32_t aQualityIndex)
  : RecorderVideoProfile(aCameraId, aQualityIndex)
{
  DOM_CAMERA_LOGT("%s:%d : this=%p, mCameraId=%d, mQualityIndex=%d\n", __func__, __LINE__, this, mCameraId, mQualityIndex);
  mPlatformCodec = static_cast<video_encoder>(GetProfileParam(mCameraId, mQualityIndex, "vid.codec"));
  mCodec = TranslateVideoCodec(mPlatformCodec);
  mBitrate = GetProfileParam(mCameraId, mQualityIndex, "vid.bps");
  mFramerate = GetProfileParam(mCameraId, mQualityIndex, "vid.fps");
  mWidth = GetProfileParam(mCameraId, mQualityIndex, "vid.width");
  mHeight = GetProfileParam(mCameraId, mQualityIndex, "vid.height");
}

GonkRecorderVideoProfile::~GonkRecorderVideoProfile()
{
  DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this);
}

GonkRecorderProfileManager::GonkRecorderProfileManager(uint32_t aCameraId)
  : RecorderProfileManager(aCameraId)
{
  DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this);
  mMaxQualityIndex = sizeof(ProfileList) / sizeof(ProfileList[0]) - 1;
}

GonkRecorderProfileManager::~GonkRecorderProfileManager()
{
  DOM_CAMERA_LOGT("%s:%d : this=%p\n", __func__, __LINE__, this);
}

bool
GonkRecorderProfileManager::IsSupported(uint32_t aQualityIndex) const
{
  if (!IsQualitySupported(mCameraId, aQualityIndex)) {
    // This profile is not supported
    return false;
  }

  int width = GetProfileParam(mCameraId, aQualityIndex, "vid.width");
  int height = GetProfileParam(mCameraId, aQualityIndex, "vid.height");
  if (width == -1 || height == -1) {
    // This would be unexpected, but we handle it just in case
    DOM_CAMERA_LOGE("Camera %d recorder profile %d has width=%d, height=%d\n", mCameraId, aQualityIndex, width, height);
    return false;
  }

  for (uint32_t i = 0; i < mSupportedSizes.Length(); ++i) {
    if (static_cast<uint32_t>(width) == mSupportedSizes[i].width &&
      static_cast<uint32_t>(height) == mSupportedSizes[i].height)
    {
      return true;
    }
  }
  return false;
}

already_AddRefed<RecorderProfile>
GonkRecorderProfileManager::Get(uint32_t aQualityIndex) const
{
  // This overrides virtual RecorderProfileManager::Get(...)
  DOM_CAMERA_LOGT("%s:%d : aQualityIndex=%d\n", __func__, __LINE__, aQualityIndex);
  nsRefPtr<RecorderProfile> profile = new GonkRecorderProfile(mCameraId, aQualityIndex);
  return profile.forget();
}

already_AddRefed<GonkRecorderProfile>
GonkRecorderProfileManager::Get(const char* aProfileName) const
{
  DOM_CAMERA_LOGT("%s:%d : aProfileName='%s'\n", __func__, __LINE__, aProfileName);
  for (uint32_t i = 0; i < mMaxQualityIndex; ++i) {
    if (strcmp(ProfileList[i].name, aProfileName) == 0) {
      nsRefPtr<GonkRecorderProfile> profile = nullptr;
      if (IsSupported(i)) {
        profile = new GonkRecorderProfile(mCameraId, i);
        return profile.forget();
      }
      return nullptr;
    }
  }

  DOM_CAMERA_LOGW("Couldn't file recorder profile named '%s'\n", aProfileName);
  return nullptr;
}