This repository has been archived by the owner on Nov 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCameraHardware.cpp
2110 lines (1725 loc) · 66.3 KB
/
CameraHardware.cpp
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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
**
** Copyright 2008, The Android Open Source Project
**
** 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.
*/
#define LOG_TAG "CameraHardware"
#include <utils/Log.h>
#include <cutils/properties.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h> /* for mode definitions */
#include <ui/Rect.h>
#include <ui/GraphicBufferMapper.h>
#include "CameraHardware.h"
#include "Converter.h"
#define MIN_WIDTH 320
#define MIN_HEIGHT 240
#ifndef PIXEL_FORMAT_RGB_888
#define PIXEL_FORMAT_RGB_888 3 /* */
#endif
#ifndef PIXEL_FORMAT_RGBA_8888
#define PIXEL_FORMAT_RGBA_8888 1 /* [ov] */
#endif
#ifndef PIXEL_FORMAT_RGBX_8888
#define PIXEL_FORMAT_RGBX_8888 2
#endif
#ifndef PIXEL_FORMAT_BGRA_8888
#define PIXEL_FORMAT_BGRA_8888 5 /* [ov] */
#endif
#ifndef PIXEL_FORMAT_RGB_565
#define PIXEL_FORMAT_RGB_565 4 /* [ov] */
#endif
// We need this format to allow special preview modes
#ifndef PIXEL_FORMAT_YCrCb_422_I
#define PIXEL_FORMAT_YCrCb_422_I 100
#endif
#ifndef PIXEL_FORMAT_YCbCr_422_SP
#define PIXEL_FORMAT_YCbCr_422_SP 0x10 /* NV16 [ov] */
#endif
#ifndef PIXEL_FORMAT_YCbCr_420_SP
#define PIXEL_FORMAT_YCbCr_420_SP 0x21 /* NV12 */
#endif
#ifndef PIXEL_FORMAT_UNKNOWN
#define PIXEL_FORMAT_UNKNOWN 0
#endif
/*
* Android YUV format:
*
* This format is exposed outside of the HAL to software
* decoders and applications.
* EGLImageKHR must support it in conjunction with the
* OES_EGL_image_external extension.
*
* YV12 is 4:2:0 YCrCb planar format comprised of a WxH Y plane followed
* by (W/2) x (H/2) Cr and Cb planes.
*
* This format assumes
* - an even width
* - an even height
* - a horizontal stride multiple of 16 pixels
* - a vertical stride equal to the height
*
* y_size = stride * height
* c_size = ALIGN(stride/2, 16) * height/2
* size = y_size + c_size * 2
* cr_offset = y_size
* cb_offset = y_size + c_size
*
*/
#ifndef PIXEL_FORMAT_YV12
#define PIXEL_FORMAT_YV12 0x32315659 /* YCrCb 4:2:0 Planar */
#endif
#ifndef PIXEL_FORMAT_YV16
#define PIXEL_FORMAT_YV16 0x36315659 /* YCrCb 4:2:2 Planar */
#endif
// File to control camera power
#define CAMERA_POWER_FILE "camera.power_file"
namespace android {
bool CameraHardware::PowerOn()
{
ALOGD("CameraHardware::PowerOn: Power ON camera.");
mCameraPowerFile = new char[PROPERTY_VALUE_MAX];
if (!property_get(CAMERA_POWER_FILE, mCameraPowerFile, "")) {
ALOGD("CameraHardware::PowerOn: no power_file set");
delete [] mCameraPowerFile;
mCameraPowerFile = 0;
return true;
}
// power on camera
int handle = ::open(mCameraPowerFile,O_RDWR);
if (handle >= 0) {
::write(handle,"1\n",2);
::close(handle);
} else {
ALOGE("Could not open %s for writing.", mCameraPowerFile);
return false;
}
// Wait until the camera is recognized or timed out
int timeOut = 500;
do {
// Try to open the video capture device
handle = ::open(mVideoDevice,O_RDWR);
if (handle >= 0)
break;
// Wait a bit
::usleep(10000);
} while (--timeOut > 0);
if (handle >= 0) {
ALOGD("Camera powered on");
::close(handle);
return true;
} else {
ALOGE("Unable to power camera");
}
return false;
}
bool CameraHardware::PowerOff()
{
ALOGD("CameraHardware::PowerOff: Power OFF camera.");
if (!mCameraPowerFile)
return true;
// power on camera
int handle = ::open(mCameraPowerFile,O_RDWR);
if (handle >= 0) {
::write(handle,"0\n",2);
::close(handle);
} else {
ALOGE("Could not open %s for writing.", mCameraPowerFile);
return false;
}
delete [] mCameraPowerFile;
mCameraPowerFile = 0;
return true;
}
void CameraHardware::ResetRuntimeData(){
mWin = 0;
mPreviewWinFmt = PIXEL_FORMAT_UNKNOWN;
mPreviewWinWidth = 0;
mPreviewWinHeight = 0;
mRawPreviewFrameSize = 0;
mRawPreviewWidth = 0;
mRawPreviewHeight = 0;
mPreviewFrameSize = 0;
mPreviewFmt = PIXEL_FORMAT_UNKNOWN;
mRawPictureBufferSize = 0;
mRecordingFrameSize = 0;
mRecFmt = PIXEL_FORMAT_UNKNOWN;
mJpegPictureBufferSize = 0;
mRecordingEnabled = 0;
mMsgEnabled = 0;
mCurrentPreviewFrame = 0;
mCurrentRecordingFrame = 0;
}
CameraHardware::CameraHardware(const hw_module_t* module, char* devLocation) :
mWin(0),
mPreviewWinFmt(PIXEL_FORMAT_UNKNOWN),
mPreviewWinWidth(0),
mPreviewWinHeight(0),
mParameters(),
mRawPreviewHeap(0),
mRawPreviewFrameSize(0),
mRawPreviewWidth(0),
mRawPreviewHeight(0),
mPreviewHeap(0),
mPreviewFrameSize(0),
mPreviewFmt(PIXEL_FORMAT_UNKNOWN),
mRawPictureHeap(0),
mRawPictureBufferSize(0),
mRecordingHeap(0),
mRecordingFrameSize(0),
mRecFmt(PIXEL_FORMAT_UNKNOWN),
mJpegPictureHeap(0),
mJpegPictureBufferSize(0),
mRecordingEnabled(0),
mNotifyCb(0),
mDataCb(0),
mDataCbTimestamp(0),
mRequestMemory(0),
mCallbackCookie(0),
mMsgEnabled(0),
mCurrentPreviewFrame(0),
mCurrentRecordingFrame(0),
mCameraPowerFile(0)
{
//Store the video device location
mVideoDevice = devLocation;
/*
* Initialize camera_device descriptor for this object.
*/
/* Common header */
common.tag = HARDWARE_DEVICE_TAG;
common.version = 0;
common.module = const_cast<hw_module_t*>(module);
common.close = CameraHardware::close;
/* camera_device fields. */
ops = &mDeviceOps;
priv = this;
// Power on camera
PowerOn();
// Init default parameters
initDefaultParameters();
}
void CameraHardware::ReleaseAllHeap()
{
// Release all memory heaps
if (mRawPreviewHeap) {
mRawPreviewHeap->release(mRawPreviewHeap);
mRawPreviewHeap = NULL;
}
if (mPreviewHeap) {
mPreviewHeap->release(mPreviewHeap);
mPreviewHeap = NULL;
}
if (mRawPictureHeap) {
mRawPictureHeap->release(mRawPictureHeap);
mRawPictureHeap = NULL;
}
if (mRecordingHeap) {
mRecordingHeap->release(mRecordingHeap);
mRecordingHeap = NULL;
}
if (mJpegPictureHeap) {
mJpegPictureHeap->release(mJpegPictureHeap);
mJpegPictureHeap = NULL;
}
}
CameraHardware::~CameraHardware()
{
ALOGD("CameraHardware::destruct");
if (mPreviewThread != 0) {
stopPreview();
}
ReleaseAllHeap();
// Power off camera
PowerOff();
}
bool CameraHardware::NegotiatePreviewFormat(struct preview_stream_ops* win)
{
ALOGD("CameraHardware::NegotiatePreviewFormat");
// Get the preview size... If we are recording, use the recording video size instead of the preview size
int pw, ph;
if (mRecordingEnabled && mMsgEnabled & CAMERA_MSG_VIDEO_FRAME) {
mParameters.getVideoSize(&pw, &ph);
} else {
mParameters.getPreviewSize(&pw, &ph);
}
ALOGD("Trying to set preview window geometry to %dx%d",pw,ph);
mPreviewWinFmt = PIXEL_FORMAT_UNKNOWN;
mPreviewWinWidth = 0;
mPreviewWinHeight = 0;
// Set the buffer geometry of the surface and YV12 as the preview format
if (win->set_buffers_geometry(win,pw,ph,PIXEL_FORMAT_RGBA_8888) != NO_ERROR) {
ALOGE("Unable to set buffer geometry");
return false;
}
// Store the preview window format
mPreviewWinFmt = PIXEL_FORMAT_RGBA_8888;
mPreviewWinWidth = pw;
mPreviewWinHeight = ph;
return true;
}
/****************************************************************************
* Camera API implementation.
***************************************************************************/
status_t CameraHardware::connectCamera(hw_device_t** device)
{
ALOGD("CameraHardware::connectCamera: %s", mVideoDevice);
*device = &common;
return NO_ERROR;
}
status_t CameraHardware::closeCamera()
{
ALOGD("CameraHardware::closeCamera");
releaseCamera();
return NO_ERROR;
}
status_t CameraHardware::getCameraInfo(struct camera_info* info, int facing,
int orientation)
{
ALOGV("CameraHardware::getCameraInfo");
info->facing = facing;
info->orientation = orientation;
return NO_ERROR;
}
status_t CameraHardware::setPreviewWindow(struct preview_stream_ops* window)
{
ALOGD("CameraHardware::setPreviewWindow: preview_stream_ops: %p", window);
{
Mutex::Autolock lock(mLock);
if (window != NULL) {
/* The CPU will write each frame to the preview window buffer.
* Note that we delay setting preview window buffer geometry until
* frames start to come in. */
status_t res = window->set_usage(window, GRALLOC_USAGE_SW_WRITE_OFTEN);
if (res != NO_ERROR) {
res = -res; // set_usage returns a negative errno.
ALOGE("%s: Error setting preview window usage %d -> %s",
__FUNCTION__, res, strerror(res));
return res;
}
}
mWin = window;
// setup the preview window geometry to be able to use the full preview window
if (mPreviewThread != 0 && mWin != 0) {
ALOGD("CameraHardware::setPreviewWindow - Negotiating preview format");
NegotiatePreviewFormat(mWin);
}
}
return NO_ERROR;
}
void CameraHardware::setCallbacks(camera_notify_callback notify_cb,
camera_data_callback data_cb,
camera_data_timestamp_callback data_cb_timestamp,
camera_request_memory get_memory,
void* user)
{
ALOGD("CameraHardware::setCallbacks");
{
Mutex::Autolock lock(mLock);
mNotifyCb = notify_cb;
mDataCb = data_cb;
mDataCbTimestamp = data_cb_timestamp;
mRequestMemory = get_memory;
mCallbackCookie = user;
}
}
void CameraHardware::enableMsgType(int32_t msgType)
{
ALOGD("CameraHardware::enableMsgType: %d", msgType);
{
Mutex::Autolock lock(mLock);
int32_t old = mMsgEnabled;
mMsgEnabled |= msgType;
// If something changed related to the starting or stopping of
// the recording process...
if ((msgType & CAMERA_MSG_VIDEO_FRAME) &&
(mMsgEnabled ^ old) & CAMERA_MSG_VIDEO_FRAME && mRecordingEnabled) {
// Recreate the heaps if toggling recording changes the raw preview size
// and also restart the preview so we use the new size if needed
initHeapLocked();
}
}
}
void CameraHardware::disableMsgType(int32_t msgType)
{
ALOGD("CameraHardware::disableMsgType: %d", msgType);
{
Mutex::Autolock lock(mLock);
int32_t old = mMsgEnabled;
mMsgEnabled &= ~msgType;
// If something changed related to the starting or stopping of
// the recording process...
if ((msgType & CAMERA_MSG_VIDEO_FRAME) &&
(mMsgEnabled ^ old) & CAMERA_MSG_VIDEO_FRAME && mRecordingEnabled) {
// Recreate the heaps if toggling recording changes the raw preview size
// and also restart the preview so we use the new size if needed
initHeapLocked();
}
}
}
/**
* Query whether a message, or a set of messages, is enabled.
* Note that this is operates as an AND, if any of the messages
* queried are off, this will return false.
*/
int CameraHardware::isMsgTypeEnabled(int32_t msgType)
{
Mutex::Autolock lock(mLock);
// All messages queried must be enabled to return true
int enabled = (mMsgEnabled & msgType) == msgType;
ALOGD("CameraHardware::isMsgTypeEnabled(%d): %d", msgType, enabled);
return enabled;
}
CameraHardware::PreviewThread::PreviewThread(CameraHardware* hw) :
Thread(false),
mHardware(hw)
{
}
void CameraHardware::PreviewThread::onFirstRef()
{
run("CameraPreviewThread", PRIORITY_URGENT_DISPLAY);
}
bool CameraHardware::PreviewThread::threadLoop()
{
mHardware->previewThread();
// loop until we need to quit
return true;
}
status_t CameraHardware::startPreviewLocked()
{
ALOGD("CameraHardware::startPreviewLocked");
if (mPreviewThread != 0) {
ALOGD("CameraHardware::startPreviewLocked: preview already running");
return NO_ERROR;
}
int width, height;
// If we are recording, use the recording video size instead of the preview size
if (mRecordingEnabled && mMsgEnabled & CAMERA_MSG_VIDEO_FRAME) {
mParameters.getVideoSize(&width, &height);
} else {
mParameters.getPreviewSize(&width, &height);
}
int fps = getPreviewFrameRate(mParameters);
ALOGD("CameraHardware::startPreviewLocked: Open, %dx%d", width, height);
status_t ret = camera.Open(mVideoDevice);
if (ret != NO_ERROR) {
ALOGE("Failed to initialize Camera");
return ret;
}
ALOGD("CameraHardware::startPreviewLocked: Init");
ret = camera.Init(width, height, fps);
if (ret != NO_ERROR) {
ALOGE("Failed to setup streaming");
return ret;
}
/* Retrieve the real size being used */
camera.getSize(width, height);
ALOGD("CameraHardware::startPreviewLocked: effective size: %dx%d",width, height);
// If we are recording, use the recording video size instead of the preview size
if (mRecordingEnabled && mMsgEnabled & CAMERA_MSG_VIDEO_FRAME) {
/* Store it as the video size to use */
mParameters.setVideoSize(width, height);
} else {
/* Store it as the preview size to use */
mParameters.setPreviewSize(width, height);
}
/* And reinit the memory heaps to reflect the real used size if needed */
initHeapLocked();
ALOGD("CameraHardware::startPreviewLocked: StartStreaming");
ret = camera.StartStreaming();
if (ret != NO_ERROR) {
ALOGE("Failed to start streaming");
return ret;
}
// setup the preview window geometry in order to use it to zoom the image
if (mWin != 0) {
ALOGD("CameraHardware::setPreviewWindow - Negotiating preview format");
NegotiatePreviewFormat(mWin);
}
ALOGD("CameraHardware::startPreviewLocked: starting PreviewThread");
mPreviewThread = new PreviewThread(this);
ALOGD("CameraHardware::startPreviewLocked: O - this:0x%p",this);
return NO_ERROR;
}
status_t CameraHardware::startPreview()
{
ALOGD("CameraHardware::startPreview");
Mutex::Autolock lock(mLock);
return startPreviewLocked();
}
void CameraHardware::stopPreviewLocked()
{
ALOGD("CameraHardware::stopPreviewLocked");
if (mPreviewThread != 0) {
ALOGD("CameraHardware::stopPreviewLocked: stopping PreviewThread");
mPreviewThread->requestExitAndWait();
mPreviewThread.clear();
ALOGD("CameraHardware::stopPreviewLocked: Uninit");
camera.Uninit();
ALOGD("CameraHardware::stopPreviewLocked: StopStreaming");
camera.StopStreaming();
ALOGD("CameraHardware::stopPreviewLocked: Close");
camera.Close();
}
ALOGD("CameraHardware::stopPreviewLocked: OK");
}
void CameraHardware::stopPreview()
{
ALOGD("CameraHardware::stopPreview");
Mutex::Autolock lock(mLock);
stopPreviewLocked();
}
int CameraHardware::isPreviewEnabled()
{
int enabled = 0;
{
Mutex::Autolock lock(mLock);
enabled = (mPreviewThread != 0);
}
ALOGD("CameraHardware::isPreviewEnabled: %d", enabled);
return enabled;
}
status_t CameraHardware::storeMetaDataInBuffers(int value)
{
ALOGD("CameraHardware::storeMetaDataInBuffers: %d", value);
// Do not accept to store metadata in buffers - We will always store
// YUV data on video buffers. Metadata, in the case of Nvidia Tegra2
// is a descriptor of an OpenMax endpoint that was filled with the
// data.
return (value) ? INVALID_OPERATION : NO_ERROR;
}
status_t CameraHardware::startRecording()
{
ALOGD("CameraHardware::startRecording");
{
Mutex::Autolock lock(mLock);
if (!mRecordingEnabled) {
mRecordingEnabled = true;
// If something changed related to the starting or stopping of
// the recording process...
if (mMsgEnabled & CAMERA_MSG_VIDEO_FRAME) {
// Recreate the heaps if toggling recording changes the raw preview size
// and also restart the preview so we use the new size if needed
initHeapLocked();
}
}
}
return NO_ERROR;
}
void CameraHardware::stopRecording()
{
ALOGD("CameraHardware::stopRecording");
{
Mutex::Autolock lock(mLock);
if (mRecordingEnabled) {
mRecordingEnabled = false;
// If something changed related to the starting or stopping of
// the recording process...
if (mMsgEnabled & CAMERA_MSG_VIDEO_FRAME) {
// Recreate the heaps if toggling recording changes the raw preview size
// and also restart the preview so we use the new size if needed
initHeapLocked();
}
}
}
}
int CameraHardware::isRecordingEnabled()
{
int enabled;
{
Mutex::Autolock lock(mLock);
enabled = mRecordingEnabled;
}
ALOGD("CameraHardware::isRecordingEnabled: %d", mRecordingEnabled);
return enabled;
}
void CameraHardware::releaseRecordingFrame(const void* mem)
{
ALOGD("CameraHardware::releaseRecordingFrame: %p", mem);
}
status_t CameraHardware::setAutoFocus()
{
ALOGD("CameraHardware::setAutoFocus");
Mutex::Autolock lock(mLock);
if (createThread(beginAutoFocusThread, this) == false)
return UNKNOWN_ERROR;
return NO_ERROR;
}
status_t CameraHardware::cancelAutoFocus()
{
ALOGD("CameraHardware::cancelAutoFocus");
return NO_ERROR;
}
status_t CameraHardware::takePicture()
{
ALOGD("CameraHardware::takePicture");
if (createThread(beginPictureThread, this) == false)
return UNKNOWN_ERROR;
return NO_ERROR;
}
status_t CameraHardware::cancelPicture()
{
ALOGD("CameraHardware::cancelPicture");
return NO_ERROR;
}
status_t CameraHardware::setParameters(const char* parms)
{
ALOGV("CameraHardware::setParameters");
CameraParameters params;
String8 str8_param(parms);
params.unflatten(str8_param);
Mutex::Autolock lock(mLock);
// If no changes, trivially accept it!
if (params.flatten() == mParameters.flatten()) {
ALOGD("Trivially accept it. No changes detected");
return NO_ERROR;
}
if (strcmp(params.getPreviewFormat(),"yuv422i-yuyv") &&
strcmp(params.getPreviewFormat(),"yuv422sp") &&
strcmp(params.getPreviewFormat(),"yuv420sp") &&
strcmp(params.getPreviewFormat(),"yuv420p")) {
ALOGE("CameraHardware::setParameters: Unsupported format '%s' for preview",params.getPreviewFormat());
return BAD_VALUE;
}
if (strcmp(params.getPictureFormat(), CameraParameters::PIXEL_FORMAT_JPEG)) {
ALOGE("CameraHardware::setParameters: Only jpeg still pictures are supported");
return BAD_VALUE;
}
if (strcmp(params.get(CameraParameters::KEY_VIDEO_FRAME_FORMAT),"yuv422i-yuyv") &&
strcmp(params.get(CameraParameters::KEY_VIDEO_FRAME_FORMAT),"yuv422sp") &&
strcmp(params.get(CameraParameters::KEY_VIDEO_FRAME_FORMAT),"yuv420sp") &&
strcmp(params.get(CameraParameters::KEY_VIDEO_FRAME_FORMAT),"yuv420p")) {
ALOGE("CameraHardware::setParameters: Unsupported format '%s' for recording",params.get(CameraParameters::KEY_VIDEO_FRAME_FORMAT));
return BAD_VALUE;
}
int w, h;
params.getPreviewSize(&w, &h);
ALOGD("CameraHardware::setParameters: PREVIEW: Size %dx%d, %d fps, format: %s", w, h, getPreviewFrameRate(params), params.getPreviewFormat());
params.getPictureSize(&w, &h);
ALOGD("CameraHardware::setParameters: PICTURE: Size %dx%d, format: %s", w, h, params.getPictureFormat());
params.getVideoSize(&w, &h);
ALOGD("CameraHardware::setParameters: VIDEO: Size %dx%d, format: %s", w, h, params.get(CameraParameters::KEY_VIDEO_FRAME_FORMAT));
// Store the new parameters
mParameters = params;
// Recreate the heaps if toggling recording changes the raw preview size
// and also restart the preview so we use the new size if needed
initHeapLocked();
ALOGD("CameraHardware::setParameters: OK");
return NO_ERROR;
}
/* A dumb variable indicating "no params" / error on the exit from
* EmulatedCamera::getParameters(). */
static char lNoParam = '\0';
char* CameraHardware::getParameters()
{
ALOGD("CameraHardware::getParameters");
String8 params;
{
Mutex::Autolock lock(mLock);
params = mParameters.flatten();
}
char* ret_str = reinterpret_cast<char*>(malloc(sizeof(char) * (params.length()+1)));
memset(ret_str, 0, params.length()+1);
if (ret_str != NULL) {
strncpy(ret_str, params.string(), params.length()+1);
return ret_str;
}
ALOGE("%s: Unable to allocate string for %s", __FUNCTION__, params.string());
/* Apparently, we can't return NULL fron this routine. */
return &lNoParam;
}
void CameraHardware::putParameters(char* params)
{
ALOGD("CameraHardware::putParameters");
/* This method simply frees parameters allocated in getParameters(). */
if (params != NULL && params != &lNoParam) {
free(params);
}
}
status_t CameraHardware::sendCommand(int32_t command, int32_t arg1, int32_t arg2)
{
ALOGD("CameraHardware::sendCommand: %d (%d, %d)", command, arg1, arg2);
return 0;
}
void CameraHardware::releaseCamera()
{
ALOGD("CameraHardware::releaseCamera");
if (mPreviewThread != 0) {
stopPreview();
}
ReleaseAllHeap();
ResetRuntimeData();
}
status_t CameraHardware::dumpCamera(int fd)
{
ALOGV("%s: %d", __FUNCTION__, fd);
return -EINVAL;
}
// ---------------------------------------------------------------------------
void CameraHardware::initDefaultParameters()
{
ALOGD("CameraHardware::initDefaultParameters");
CameraParameters p;
unsigned int i;
int pw = MIN_WIDTH;
int ph = MIN_HEIGHT;
int pfps = 30;
int fw = MIN_WIDTH;
int fh = MIN_HEIGHT;
SortedVector<SurfaceSize> avSizes;
SortedVector<int> avFps;
if (camera.Open(mVideoDevice) != NO_ERROR) {
ALOGE("cannot open device.");
} else {
// Get the default preview format
pw = camera.getBestPreviewFmt().getWidth();
ph = camera.getBestPreviewFmt().getHeight();
pfps = camera.getBestPreviewFmt().getFps();
// Get the default picture format
fw = camera.getBestPictureFmt().getWidth();
fh = camera.getBestPictureFmt().getHeight();
// Get all the available sizes
avSizes = camera.getAvailableSizes();
// Add some sizes that some specific apps expect to find:
// GTalk expects 320x200
// Fring expects 240x160
// And also add standard resolutions found in low end cameras, as
// android apps could be expecting to find them
// The V4LCamera handles those resolutions by choosing the next
// larger one and cropping the captured frames to the requested size
avSizes.add(SurfaceSize(480,320)); // HVGA
avSizes.add(SurfaceSize(432,320)); // 1.35-to-1, for photos. (Rounded up from 1.3333 to 1)
avSizes.add(SurfaceSize(352,288)); // CIF
avSizes.add(SurfaceSize(320,240)); // QVGA
avSizes.add(SurfaceSize(320,200));
avSizes.add(SurfaceSize(240,160)); // SQVGA
avSizes.add(SurfaceSize(176,144)); // QCIF
// Get all the available Fps
avFps = camera.getAvailableFps();
}
// Convert the sizes to text
String8 szs("");
for (i = 0; i < avSizes.size(); i++) {
char descr[32];
SurfaceSize ss = avSizes[i];
sprintf(descr,"%dx%d",ss.getWidth(),ss.getHeight());
szs.append(descr);
if (i < avSizes.size() - 1) {
szs.append(",");
}
}
// Convert the fps to ranges in text
String8 fpsranges("");
for (i = 0; i < avFps.size(); i++) {
char descr[32];
int ss = avFps[i] * 1000;
sprintf(descr, "(%d,%d)", ss, ss);
fpsranges.append(descr);
if (i < avFps.size() - 1) {
fpsranges.append(",");
}
}
// Convert the fps to text
String8 fps("");
for (i = 0; i < avFps.size(); i++) {
char descr[32];
int ss = avFps[i];
sprintf(descr,"%d",ss);
fps.append(descr);
if (i < avFps.size() - 1) {
fps.append(",");
}
}
ALOGI("Default preview size: (%d x %d), fps:%d\n",pw,ph,pfps);
ALOGI("All available formats: %s",(const char*)szs);
ALOGI("All available fps: %s",(const char*)fpsranges);
ALOGI("Default picture size: (%d x %d)\n",fw,fh);
// Now store the data
// Antibanding
p.set(CameraParameters::KEY_SUPPORTED_ANTIBANDING,"auto");
p.set(CameraParameters::KEY_ANTIBANDING,"auto");
// Effects
p.set(CameraParameters::KEY_SUPPORTED_EFFECTS,"none"); // "none,mono,sepia,negative,solarize"
p.set(CameraParameters::KEY_EFFECT,"none");
// Flash modes
p.set(CameraParameters::KEY_SUPPORTED_FLASH_MODES,"off");
p.set(CameraParameters::KEY_FLASH_MODE,"off");
// Focus modes
p.set(CameraParameters::KEY_SUPPORTED_FOCUS_MODES,"fixed");
p.set(CameraParameters::KEY_FOCUS_MODE,"fixed");
#if 0
p.set(CameraParameters::KEY_JPEG_THUMBNAIL_HEIGHT,0);
p.set(CameraParameters::KEY_JPEG_THUMBNAIL_QUALITY,75);
p.set(CameraParameters::KEY_SUPPORTED_JPEG_THUMBNAIL_SIZES,"0x0");
p.set("jpeg-thumbnail-size","0x0");
p.set(CameraParameters::KEY_JPEG_THUMBNAIL_WIDTH,0);
#endif
// Picture - Only JPEG supported
p.set(CameraParameters::KEY_SUPPORTED_PICTURE_FORMATS,CameraParameters::PIXEL_FORMAT_JPEG); // ONLY jpeg
p.setPictureFormat(CameraParameters::PIXEL_FORMAT_JPEG);
p.set(CameraParameters::KEY_SUPPORTED_PICTURE_SIZES, szs);
p.setPictureSize(fw,fh);
p.set(CameraParameters::KEY_JPEG_QUALITY, 85);
// Preview - Supporting yuv422i-yuyv,yuv422sp,yuv420sp, defaulting to yuv420sp, as that is the android Defacto default
p.set(CameraParameters::KEY_SUPPORTED_PREVIEW_FORMATS,"yuv422i-yuyv,yuv422sp,yuv420sp,yuv420p"); // All supported preview formats
p.setPreviewFormat(CameraParameters::PIXEL_FORMAT_YUV422SP); // For compatibility sake ... Default to the android standard
p.set(CameraParameters::KEY_SUPPORTED_PREVIEW_FPS_RANGE, fpsranges);
p.set(CameraParameters::KEY_SUPPORTED_PREVIEW_FRAME_RATES, fps);
p.setPreviewFrameRate( pfps );
p.set(CameraParameters::KEY_SUPPORTED_PREVIEW_SIZES, szs);
p.setPreviewSize(pw,ph);
// Video - Supporting yuv422i-yuyv,yuv422sp,yuv420sp and defaulting to yuv420p
p.set("video-size-values"/*CameraParameters::KEY_SUPPORTED_VIDEO_SIZES*/, szs);
p.setVideoSize(pw,ph);
p.set(CameraParameters::KEY_VIDEO_FRAME_FORMAT, CameraParameters::PIXEL_FORMAT_YUV420P);
p.set("preferred-preview-size-for-video", "640x480");
// supported rotations
p.set("rotation-values","0");
p.set(CameraParameters::KEY_ROTATION,"0");
// scenes modes
p.set(CameraParameters::KEY_SUPPORTED_SCENE_MODES,"auto");
p.set(CameraParameters::KEY_SCENE_MODE,"auto");
// white balance
p.set(CameraParameters::KEY_SUPPORTED_WHITE_BALANCE,"auto");
p.set(CameraParameters::KEY_WHITE_BALANCE,"auto");
// zoom
p.set(CameraParameters::KEY_SMOOTH_ZOOM_SUPPORTED,"false");
p.set("max-video-continuous-zoom", 0 );
p.set(CameraParameters::KEY_ZOOM, "0");
p.set(CameraParameters::KEY_MAX_ZOOM, "100");
p.set(CameraParameters::KEY_ZOOM_RATIOS, "100");
p.set(CameraParameters::KEY_ZOOM_SUPPORTED, "false");
// missing parameters for Camera2
p.setFloat(CameraParameters::KEY_FOCAL_LENGTH, 4.31);
p.setFloat(CameraParameters::KEY_HORIZONTAL_VIEW_ANGLE, 90);
p.setFloat(CameraParameters::KEY_VERTICAL_VIEW_ANGLE, 90);
p.set(CameraParameters::KEY_SUPPORTED_JPEG_THUMBNAIL_SIZES, "640x480,0x0");
p.set(CameraParameters::KEY_EXPOSURE_COMPENSATION, "6");
p.set(CameraParameters::KEY_EXPOSURE_COMPENSATION_STEP, "0.5");
if (setParameters(p.flatten()) != NO_ERROR) {
ALOGE("CameraHardware::initDefaultParameters: Failed to set default parameters.");
}
}