Call process from Android camera AP to camera hal

Source: Internet
Author: User

1. First, call the takepicture method of camera. Java (frameworks \ base \ core \ Java \ Android \ hardware) in the framework layer.

2. The android_hardware_camera.cpp (frameworks \ base \ core \ JNI) android_hardware_camera_takepicture method on the JNI layer is called. The camera object is obtained through get_native_camera.

3. The preceding camera object is defined in camera. H (frameworks \ AV \ include \ camera), and camera. H also defines camerainfo. The camera object inherits from publicbncameraclient and deathrecipient. The real implementation of the camera object is in camera. cpp (frameworks \ AV \ camera)
. Bncameraclient exists as a cameraservice callback. The camera object contains a mcameraservice variable used to save the reference of cameraservice.

Class camera:
Public bncameraclient, public ibinder: deathrecipient

Constsp <icameraservice> & camera: getcameraservice ()

{

Mutex: autolock _ L (mlock );

If (mcameraservice. Get () = 0 ){

Sp <iservicemanager> Sm = defaultservicemanager ();

Sp <ibinder> binder;

Do {

Binder = Sm-> getservice (string16 ("media. Camera "));

If (binder! = 0)

Break;

Alogw ("cameraservice notpublished, waiting ...");

Usleep (500000); // 0.5 s

} While (true );

If (mdeathnotifier = NULL ){

Mdeathnotifier = newdeathnotifier ();

}

Binder-> linktodeath (mdeathnotifier );

Mcameraservice = interface_cast <icameraservice> (binder );

}

4. In the icameraclient. h and icameraservice. H files, both icameraclient and icameraservice inherit from iinterface, which is the basis of binder communication. In addition, icamera. H also inherits from iinterface

Class icameraclient: Public iinterface

Class icameraservice: Public iinterface

Class icamera: publiciinterface

Then it is found that bncameraclient inherits from bninterface <icameraclient> and serves as the server of icameraclient:

Class bncameraclient: publicbninterface <icameraclient>

Bncameraservice inherits from bninterface <icameraservice> and serves as the server of icameraservice:

Class bncameraservice: publicbninterface <icameraservice>

Bncamera inherits from bninterface <icamera> and serves as the server of icamera:

Class bncamera: Public bninterface <icamera>

The preceding three classes exist respectively as the binder client, which is BP ***:

Class bpcameraclient: Public bpinterface <icameraclient>

Class bpcameraservice: Public bpinterface <icameraservice>

Class bpcamera: publicbpinterface <icamera>

The above three implementations are in icameraclient. CPP, icameraservice. CPP, icamera. CPP, which implements the server and client of three binder groups respectively in these three files. Common Methods of camera are implemented in icamera.

5. camera. the CPP File Connect method is called when setupcamera is removed from the JNI layer. The JNI layer is called when the upper-layer opencamera is located in camera. obtain an icamera object in the Connect Method of CPP to complete the real operations on camera.

Sp <camera> camera: connect (INT cameraid)

{

Alogv ("Connect ");

Sp <camera> C = new camera ();

Const sp <icameraservice> & cs = getcameraservice ();

If (CS! = 0 ){

C-> mcamera = cs-> connect (C, cameraid );

}

If (c-> mcamera! = 0 ){

C-> mcamera-> asbinder ()-> linktodeath (C );

C-> mstatus = no_error;

} Else {

C. Clear ();

}

Return C;

}

6. in cameraservice. in the H file, we can see that cameraservice defines the internal class client. cameraclient inherits this class and holds an array of mclient objects in cameraservice. This array is also camera. the binder reference object to be returned after the CPP client calls the Connect Method

Class client: Public bncamera

Class cameraclient: Public cameraservice: Client

WP <client> mclient [max_cameras];

Switch (deviceversion ){

Case camera_device_api_version_1_0:

Client = new cameraclient (this, cameraclient, cameraid,

Info. Facing, callingpid, getpid ());

Break;

Case camera_device_api_version_2_0:

Client = new camera2client (this, cameraclient, cameraid,

Info. Facing, callingpid, getpid ());

In addition, Google seems to have developed a new api2.0. In this case, the binder references objects of different cameraclient instances by judging different versions. Currently, the camera2 folder has the burst implementation.

7. in the definition of the cameraclient object in the cameraservice layer, the reference mhardware of the camerahardwareinterface object is held. this object is created in the initilize method of cameraclient, And the ID of the camera to be opened is passed in the object constructor.

Sp <camerahardwareinterface> mhardware;

Status_t cameraclient: Initialize (camera_module_t * module ){

Int callingpid = getcallingpid ();

Log1 ("cameraclient: Initialize E (PID % d, Id % d)", callingpid, mcameraid );

 

Char camera_device_name [10];

Status_t res;

Snprintf (camera_device_name, sizeof (camera_device_name), "% d ",
Mcameraid );

Mhardware = new camerahardwareinterface (camera_device_name );

Res = mhardware-> initialize (& module-> common );

If (res! = OK ){

Aloge ("% s: Camera % d: unable toinitialize device: % s (% d )",

_ FUNCTION __, mcameraid, strerror (-res), Res );

Mhardware. Clear ();

Return no_init;

}

Mhardware-> setcallbacks (notifycallback,

Datacallback,

Datacallbacktimestamp,

(Void *) mcameraid );

// Enable zoom, error, focus, and metadata messages by default

Enablemsgtype (camera_msg_error | camera_msg_zoom | camera_msg_focus |

Camera_msg_preview_metadata | camera_msg_focus_move );

//! ++

# Ifdef mtk_camera_bsp_support

// Enable mtk-extended messages by default

Enablemsgtype (mtk_camera_msg_ext_policy | mtk_camera_msg_ext_data );

# Endif

//! --

Log1 ("cameraclient: Initialize X (PID % d, Id % d)", callingpid, mcameraid );

Return OK;

}

8. In the onfirstref method of cameraservice, The camerahal module is loaded.

Void cameraservice: onfirstref ()

{

Bncameraservice: onfirstref ();

 

If (hw_get_module (camera_hardware_module_id,

(Const hw_module_t **) & mmodule) <0 ){

Aloge ("cocould not load camera halmodule ");

Mnumberofcameras = 0;

}

Else {

Mnumberofcameras = mmodule-> get_number_of_cameras ();

If (mnumberofcameras> max_cameras ){

Aloge ("number of cameras (% d)> max_cameras (% d ).",

Mnumberofcameras, max_cameras );

Mnumberofcameras = max_cameras;

}

For (INT I = 0; I <mnumberofcameras; I ++ ){

Setcamerafree (I );

}

}

}

The hw_get_module method is implemented in the hardware/libhardware/hardware. c file.

Int hw_get_module (constchar * ID, const struct hw_module_t ** module)

{

Return hw_get_module_by_class (ID, null, module );

}

The struct of camera_module_t is defined in the camera_common.h file.

Typedef structcamera_module {

Hw_module_t common;

INT (* get_number_of_cameras) (void );

INT (* get_camera_info) (INT camera_id, struct camera_info * info );

} Camera_module_t;

Hw_module_t is defined in the hardware. h file. hw_module_t holds a reference to hw_module_methods_t, which defines an open pointer.

Typedef structhw_module_methods_t {

/** Open a specific device */

INT (* open) (const struct hw_module_t * module, const char * ID,

Struct hw_device_t ** device );

 

} Hw_module_methods_t;

The camdevice loading operation calls open in the init method of camerahardwareinterface.

Int rc = module-> methods-> open (module, mname. String (),

(Hw_device_t **) & mdevice );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.