summaryrefslogtreecommitdiff
path: root/dom/camera/TestGonkCameraHardware.cpp
blob: 2e17c96082225590492b3d8cd42e39f8fdaec3ec (plain)
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
/*
 * Copyright (C) 2013-2014 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 "TestGonkCameraHardware.h"

#include "CameraPreferences.h"
#include "nsThreadUtils.h"

using namespace android;
using namespace mozilla;

TestGonkCameraHardware::TestGonkCameraHardware(nsGonkCameraControl* aTarget,
                                               uint32_t aCameraId,
                                               const sp<Camera>& aCamera)
  : GonkCameraHardware(aTarget, aCameraId, aCamera)
{
  DOM_CAMERA_LOGA("v===== Created TestGonkCameraHardware =====v\n");
  DOM_CAMERA_LOGT("%s:%d : this=%p (aTarget=%p)\n",
    __func__, __LINE__, this, aTarget);
  MOZ_COUNT_CTOR(TestGonkCameraHardware);
}

TestGonkCameraHardware::~TestGonkCameraHardware()
{
  MOZ_COUNT_DTOR(TestGonkCameraHardware);
  DOM_CAMERA_LOGA("^===== Destroyed TestGonkCameraHardware =====^\n");
}

void
TestGonkCameraHardware::InjectFakeSystemFailure()
{
  DOM_CAMERA_LOGA("====== Fake Camera Hardware Failure ======\n");
  // The values '100' and '0' below seem to be what the AOSP layer
  // throws back when the mediaserver process fails.
  OnSystemError(mTarget, CameraControlListener::kSystemService, 100, 0);
}

nsresult
TestGonkCameraHardware::Init()
{
  class DeferredSystemFailure : public nsRunnable
  {
  public:
    DeferredSystemFailure(TestGonkCameraHardware* aCameraHw)
      : mCameraHw(aCameraHw)
    { }

    NS_IMETHODIMP
    Run()
    {
      mCameraHw->InjectFakeSystemFailure();
      return NS_OK;
    }

  protected:
    android::sp<TestGonkCameraHardware> mCameraHw;
  };

  if (IsTestCase("init-failure")) {
    return NS_ERROR_NOT_INITIALIZED;
  }
  if (IsTestCase("post-init-system-failure")) {
    nsCOMPtr<nsIThread> me = NS_GetCurrentThread();
    if (me) {
      me->Dispatch(new DeferredSystemFailure(this), NS_DISPATCH_NORMAL);
    }
  }

  return GonkCameraHardware::Init();
}

const nsCString
TestGonkCameraHardware::TestCase()
{
  nsCString test;
  CameraPreferences::GetPref("camera.control.test.hardware", test);
  return test;
}

const nsCString
TestGonkCameraHardware::GetExtraParameters()
{
  /**
   * The contents of this pref are appended to the flattened string of
   * parameters stuffed into GonkCameraParameters by the camera library.
   * It consists of semicolon-delimited key=value pairs, e.g.
   *
   *   focus-mode=auto;flash-mode=auto;preview-size=1024x768
   *
   * The unflattening process breaks this string up on semicolon boundaries
   * and sets an entry in a hashtable of strings with the token before
   * the equals sign as the key, and the token after as the value. Because
   * the string is parsed in order, key=value pairs occuring later in the
   * string will replace value pairs appearing earlier, making it easy to
   * inject fake, testable values into the parameters table.
   *
   * One constraint of this approach is that neither the key nor the value
   * may contain equals signs or semicolons. We don't enforce that here
   * so that we can also test correct handling of improperly-formatted values.
   */
  nsCString parameters;
  CameraPreferences::GetPref("camera.control.test.hardware.gonk.parameters", parameters);
  DOM_CAMERA_LOGA("TestGonkCameraHardware : extra-parameters '%s'\n",
    parameters.get());
  return parameters;
}

bool
TestGonkCameraHardware::IsTestCaseInternal(const char* aTest, const char* aFile, int aLine)
{
  if (TestCase().EqualsASCII(aTest)) {
    DOM_CAMERA_LOGA("TestGonkCameraHardware : test-case '%s' (%s:%d)\n",
      aTest, aFile, aLine);
    return true;
  }

  return false;
}

int
TestGonkCameraHardware::TestCaseError(int aDefaultError)
{
  // for now, just return the default error
  return aDefaultError;
}

int
TestGonkCameraHardware::AutoFocus()
{
  class AutoFocusFailure : public nsRunnable
  {
  public:
    AutoFocusFailure(nsGonkCameraControl* aTarget)
      : mTarget(aTarget)
    { }

    NS_IMETHODIMP
    Run()
    {
      OnAutoFocusComplete(mTarget, false);
      return NS_OK;
    }

  protected:
    nsGonkCameraControl* mTarget;
  };

  if (IsTestCase("auto-focus-failure")) {
    return TestCaseError(UNKNOWN_ERROR);
  }
  if (IsTestCase("auto-focus-process-failure")) {
    nsresult rv = NS_DispatchToCurrentThread(new AutoFocusFailure(mTarget));
    if (NS_SUCCEEDED(rv)) {
      return OK;
    }
    DOM_CAMERA_LOGE("Failed to dispatch AutoFocusFailure runnable (0x%08x)\n", rv);
    return UNKNOWN_ERROR;
  }

  return GonkCameraHardware::AutoFocus();
}

// These classes have to be external to StartFaceDetection(), at least
// until we pick up gcc 4.5, which supports local classes as template
// arguments.
class FaceDetected : public nsRunnable
{
public:
  FaceDetected(nsGonkCameraControl* aTarget)
    : mTarget(aTarget)
  { }

  ~FaceDetected()
  {
    ReleaseFacesArray();
  }

  NS_IMETHODIMP
  Run()
  {
    InitMetaData();
    OnFacesDetected(mTarget, &mMetaData);
    return NS_OK;
  }

protected:
  virtual nsresult InitMetaData() = 0;

  nsresult
  AllocateFacesArray(uint32_t num)
  {
    mMetaData.faces = new camera_face_t[num];
    return NS_OK;
  }

  nsresult
  ReleaseFacesArray()
  {
    delete [] mMetaData.faces;
    mMetaData.faces = nullptr;
    return NS_OK;
  }

  nsRefPtr<nsGonkCameraControl> mTarget;
  camera_frame_metadata_t mMetaData;
};

class OneFaceDetected : public FaceDetected
{
public:
  OneFaceDetected(nsGonkCameraControl* aTarget)
    : FaceDetected(aTarget)
  { }

  nsresult
  InitMetaData() override
  {
    mMetaData.number_of_faces = 1;
    AllocateFacesArray(1);
    mMetaData.faces[0].id = 1;
    mMetaData.faces[0].score = 2;
    mMetaData.faces[0].rect[0] = 3;
    mMetaData.faces[0].rect[1] = 4;
    mMetaData.faces[0].rect[2] = 5;
    mMetaData.faces[0].rect[3] = 6;
    mMetaData.faces[0].left_eye[0] = 7;
    mMetaData.faces[0].left_eye[1] = 8;
    mMetaData.faces[0].right_eye[0] = 9;
    mMetaData.faces[0].right_eye[1] = 10;
    mMetaData.faces[0].mouth[0] = 11;
    mMetaData.faces[0].mouth[1] = 12;

    return NS_OK;
  }
};

class TwoFacesDetected : public FaceDetected
{
public:
  TwoFacesDetected(nsGonkCameraControl* aTarget)
    : FaceDetected(aTarget)
  { }

  nsresult
  InitMetaData() override
  {
    mMetaData.number_of_faces = 2;
    AllocateFacesArray(2);
    mMetaData.faces[0].id = 1;
    mMetaData.faces[0].score = 2;
    mMetaData.faces[0].rect[0] = 3;
    mMetaData.faces[0].rect[1] = 4;
    mMetaData.faces[0].rect[2] = 5;
    mMetaData.faces[0].rect[3] = 6;
    mMetaData.faces[0].left_eye[0] = 7;
    mMetaData.faces[0].left_eye[1] = 8;
    mMetaData.faces[0].right_eye[0] = 9;
    mMetaData.faces[0].right_eye[1] = 10;
    mMetaData.faces[0].mouth[0] = 11;
    mMetaData.faces[0].mouth[1] = 12;
    mMetaData.faces[1].id = 13;
    mMetaData.faces[1].score = 14;
    mMetaData.faces[1].rect[0] = 15;
    mMetaData.faces[1].rect[1] = 16;
    mMetaData.faces[1].rect[2] = 17;
    mMetaData.faces[1].rect[3] = 18;
    mMetaData.faces[1].left_eye[0] = 19;
    mMetaData.faces[1].left_eye[1] = 20;
    mMetaData.faces[1].right_eye[0] = 21;
    mMetaData.faces[1].right_eye[1] = 22;
    mMetaData.faces[1].mouth[0] = 23;
    mMetaData.faces[1].mouth[1] = 24;

    return NS_OK;
  }
};

class OneFaceNoFeaturesDetected : public FaceDetected
{
public:
  OneFaceNoFeaturesDetected(nsGonkCameraControl* aTarget)
    : FaceDetected(aTarget)
  { }

  nsresult
  InitMetaData() override
  {
    mMetaData.number_of_faces = 1;
    AllocateFacesArray(1);
    mMetaData.faces[0].id = 1;
    // Test clamping 'score' to 100.
    mMetaData.faces[0].score = 1000;
    mMetaData.faces[0].rect[0] = 3;
    mMetaData.faces[0].rect[1] = 4;
    mMetaData.faces[0].rect[2] = 5;
    mMetaData.faces[0].rect[3] = 6;
    // Nullable values set to 'not-supported' specific values
    mMetaData.faces[0].left_eye[0] = -2000;
    mMetaData.faces[0].left_eye[1] = -2000;
    // Test other 'not-supported' values as well. We treat
    // anything outside the range [-1000, 1000] as invalid.
    mMetaData.faces[0].right_eye[0] = 1001;
    mMetaData.faces[0].right_eye[1] = -1001;
    mMetaData.faces[0].mouth[0] = -2000;
    mMetaData.faces[0].mouth[1] = 2000;

    return NS_OK;
  }
};

class NoFacesDetected : public FaceDetected
{
public:
  NoFacesDetected(nsGonkCameraControl* aTarget)
    : FaceDetected(aTarget)
  { }

  nsresult
  InitMetaData() override
  {
    mMetaData.number_of_faces = 0;
    mMetaData.faces = nullptr;

    return NS_OK;
  }
};

int
TestGonkCameraHardware::StartFaceDetection()
{
  nsRefPtr<FaceDetected> faceDetected;

  if (IsTestCase("face-detection-detected-one-face")) {
    faceDetected = new OneFaceDetected(mTarget);
  } else if (IsTestCase("face-detection-detected-two-faces")) {
    faceDetected = new TwoFacesDetected(mTarget);
  } else if (IsTestCase("face-detection-detected-one-face-no-features")) {
    faceDetected = new OneFaceNoFeaturesDetected(mTarget);
  } else if (IsTestCase("face-detection-no-faces-detected")) {
    faceDetected = new NoFacesDetected(mTarget);
  }

  if (!faceDetected) {
    return GonkCameraHardware::StartFaceDetection();
  }

  nsresult rv = NS_DispatchToCurrentThread(faceDetected);
  if (NS_FAILED(rv)) {
    DOM_CAMERA_LOGE("Failed to dispatch FaceDetected runnable (0x%08x)\n", rv);
    return UNKNOWN_ERROR;
  }

  return OK;
}

int
TestGonkCameraHardware::StopFaceDetection()
{
  if (IsTestCase("face-detection-detected-one-face") ||
      IsTestCase("face-detection-detected-two-faces") ||
      IsTestCase("face-detection-detected-one-face-no-features") ||
      IsTestCase("face-detection-no-faces-detected"))
  {
    return OK;
  }

  return GonkCameraHardware::StopFaceDetection();
}

int
TestGonkCameraHardware::TakePicture()
{
  class TakePictureFailure : public nsRunnable
  {
  public:
    TakePictureFailure(nsGonkCameraControl* aTarget)
      : mTarget(aTarget)
    { }

    NS_IMETHODIMP
    Run()
    {
      OnTakePictureError(mTarget);
      return NS_OK;
    }

  protected:
    nsGonkCameraControl* mTarget;
  };

  if (IsTestCase("take-picture-failure")) {
    return TestCaseError(UNKNOWN_ERROR);
  }
  if (IsTestCase("take-picture-process-failure")) {
    nsresult rv = NS_DispatchToCurrentThread(new TakePictureFailure(mTarget));
    if (NS_SUCCEEDED(rv)) {
      return OK;
    }
    DOM_CAMERA_LOGE("Failed to dispatch TakePictureFailure runnable (0x%08x)\n", rv);
    return UNKNOWN_ERROR;
  }

  return GonkCameraHardware::TakePicture();
}

int
TestGonkCameraHardware::StartPreview()
{
  if (IsTestCase("start-preview-failure")) {
    return TestCaseError(UNKNOWN_ERROR);
  }

  return GonkCameraHardware::StartPreview();
}

int
TestGonkCameraHardware::StartAutoFocusMoving(bool aIsMoving)
{
  class AutoFocusMoving : public nsRunnable
  {
  public:
    AutoFocusMoving(nsGonkCameraControl* aTarget, bool aIsMoving)
      : mTarget(aTarget)
      , mIsMoving(aIsMoving)
    { }

    NS_IMETHODIMP
    Run()
    {
      OnAutoFocusMoving(mTarget, mIsMoving);
      return NS_OK;
    }

  protected:
    nsGonkCameraControl* mTarget;
    bool mIsMoving;
  };

  nsresult rv = NS_DispatchToCurrentThread(new AutoFocusMoving(mTarget, aIsMoving));
  if (NS_SUCCEEDED(rv)) {
    return OK;
  }
  DOM_CAMERA_LOGE("Failed to dispatch AutoFocusMoving runnable (0x%08x)\n", rv);
  return UNKNOWN_ERROR;
}

int
TestGonkCameraHardware::PushParameters(const GonkCameraParameters& aParams)
{
  if (IsTestCase("push-parameters-failure")) {
    return TestCaseError(UNKNOWN_ERROR);
  }

  nsString focusMode;
  GonkCameraParameters& params = const_cast<GonkCameraParameters&>(aParams);
  params.Get(CAMERA_PARAM_FOCUSMODE, focusMode);
  if (focusMode.EqualsASCII("continuous-picture") ||
      focusMode.EqualsASCII("continuous-video"))
  {
    if (IsTestCase("autofocus-moving-true")) {
      return StartAutoFocusMoving(true);
    } else if (IsTestCase("autofocus-moving-false")) {
      return StartAutoFocusMoving(false);
    }
  }

  return GonkCameraHardware::PushParameters(aParams);
}

nsresult
TestGonkCameraHardware::PullParameters(GonkCameraParameters& aParams)
{
  if (IsTestCase("pull-parameters-failure")) {
    return static_cast<nsresult>(TestCaseError(UNKNOWN_ERROR));
  }

  String8 s = mCamera->getParameters();
  nsCString extra = GetExtraParameters();
  if (!extra.IsEmpty()) {
    s += ";";
    s += extra.get();
  }

  return aParams.Unflatten(s);
}

int
TestGonkCameraHardware::StartRecording()
{
  if (IsTestCase("start-recording-failure")) {
    return TestCaseError(UNKNOWN_ERROR);
  }

  return GonkCameraHardware::StartRecording();
}

int
TestGonkCameraHardware::StopRecording()
{
  if (IsTestCase("stop-recording-failure")) {
    return TestCaseError(UNKNOWN_ERROR);
  }

  return GonkCameraHardware::StopRecording();
}

int
TestGonkCameraHardware::SetListener(const sp<GonkCameraListener>& aListener)
{
  if (IsTestCase("set-listener-failure")) {
    return TestCaseError(UNKNOWN_ERROR);
  }

  return GonkCameraHardware::SetListener(aListener);
}

int
TestGonkCameraHardware::StoreMetaDataInBuffers(bool aEnabled)
{
  if (IsTestCase("store-metadata-in-buffers-failure")) {
    return TestCaseError(UNKNOWN_ERROR);
  }

  return GonkCameraHardware::StoreMetaDataInBuffers(aEnabled);
}