Using Android Mediarecorder C + + class for video encoding

Source: Internet
Author: User
Tags prepare

in this case, media source is camera. (Learning platform is android sdk6.0)

Call Graph:


Startrecording:

    1. Recorder = New Mediarecorder (String16 ());

Classmediarecorder:public Bnmediarecorderclient,

Public Virtualimediadeathnotifier

    1. When we create the Mediarecorder, we get the Imediaplayerservice pointer before we create the Imediarecorder

( declaration of sp<imediarecorder> Mmediarecorder;)

Mmediarecorder =service->createmediarecorder (oppackagename);

    1. in Service->createmediarecorder, the object is imediaplayerservice sequence session (using data packaging), and then the request Create_media_recorder sent to remote(that is , bpbinder) processing

Remote ()->transact (Create_media_recorder, data,&reply);

    1. Finally, the local corresponding object is created according to the remote reply.

Interface_cast<imediarecorder> (Reply.readstrongbinder ());

    1. The following methods are passed Bpmediarecorder(remote) to send to the bnmediarecorder operation.

    2. A) bnmediarecorder inherited the bninterface<imediarecorder>and realized the ontransact

Classbnmediarecorder: publicbninterface<imediarecorder>

{

Public

Virtual status_t ontransact (uint32_t code,

constparcel& data,

Parcel*reply,

uint32_tflags = 0);

};

    1. in this method , each operation has a corresponding processing branch

    2. c) is a virtual function, Specific work by mediarecorderclient to complete: mediarecorderclient:public bnmediarecorder

    3. Class Bpmediarecorder: public Bpinterface<imediarecorder>

in the corresponding method, the last is passed Remote Give Bn processing, such as:

Parceldata, reply;

Data.writeinterfacetoken (Imediarecorder::getinterfacedescriptor ());

Data.writestring8 (params);

Remote ()->transact (Set_parameters,data, &reply);

    1. Recorder->setparameters (...)

    2. Recorder->prepare ()

    3. Recorder->start ()

    4. 3. the Mediarecorderbase class is encapsulated in mediarecorderclient , and each task has to be completed

in the you can see in the Mediarecorderclient constructor

Mrecorder = Avmediaservicefactory::get (),createstagefrightrecorder(oppackagename);

    1. Therefore, the specific work is done by Stagefrightrecorder to finish.

    2. in the in AVMediaServiceFactory.cpp

Stagefrightrecorder*avmediaservicefactory::createstagefrightrecorder (

Const STRING16 &oppackagename) {

Return Newstagefrightrecorder (Oppackagename);

}

    1. Stagefrightrecorder Recorder->start()

Call Stagefrightrecorder in prepare or start::p repareinternal ()

In this case, according to the output encoding settings, select the corresponding encoder to initialize.

us to Output_format_mpeg_4 As an example for analysis

Enter setupmpeg4orwebmrecording () according to the output format

    1. Setupmpeg4orwebmrecording ()

Here you create a Mpeg4writer

    1. writer = Mp4writer =avfactory::get ()->creatempeg4writer (MOUTPUTFD);

    2. Setupmediasource specifying the camera as the input source for encoder

    3. Sp<mediasource> encoder;

Err = Setupvideoencoder (MediaSource, &encoder); Create Encoder

    1. Setupvideoencoder (Mediasource,&encoder);

first , the various parameter settings in the camera are read out and set into the encoder (The default value is used for camera null )

Sp<metadata>meta = Camerasource->getformat ();

int32_t width, height, stride,sliceheight, ColorFormat;

CHECK (Meta->findint32 (kkeywidth,&width));

CHECK (Meta->findint32 (kkeyheight,&height));

CHECK (Meta->findint32 (kkeystride,&stride));

CHECK (Meta->findint32 (Kkeysliceheight, &sliceheight));

CHECK (Meta->findint32 (Kkeycolorformat, &colorformat));

Format->setint32 ("width", width);

Format->setint32 ("height", height);

Format->setint32 ("Stride", stride);

Format->setint32 ("Slice-height", sliceheight);

Format->setint32 ("Color-format", ColorFormat);

The custom parameter setting functionis reserved here , which is not implemented by default in Android, is an empty function

then create the encoder.

Sp<mediacodecsource> Encoder =mediacodecsource::create (

Mlooper, format, camerasource,mpersistentsurface, flags);

    1. Sp<mediacodecsource> Encoder

=mediacodecsource::create (

Mlooper,

Format

Camerasource,

Mpersistentsurface,

    1. ;

in the create function , start with a new object,

Sp<mediacodecsource> MediaSource =

New Mediacodecsource (looper, format, source, consumer, flags);

if (mediasource->init () = = OK) {

return mediaSource;

}

    1. Mediacodecsource::init ()

status_t err = Initencoder ();

    1. Initencoder (){

Mreflector = new Ahandlerreflector<mediacodecsource> (this);

Mlooper->registerhandler (Mreflector);

Mcodeclooper = new Alooper;

Mcodeclooper->setname ("Codec_looper");

Mcodeclooper->start ();

.................

MEncoder =mediacodec::createbytype (

Mcodeclooper,outputmime.c_str (), TRUE/* encoder */);

....

Mencoderactivitynotify =new amessage (kwhatencoderactivity, mreflector);

Mencoder->setcallback (mencoderactivitynotify);

status_t Err =mencoder->configure (

Moutputformat,

NULL/*nativewindow */,

NULL/* crypto*/,

Mediacodec::configure_flag_encode);

..............................

Err = Mencoder->start ();

    1. Mediacodec::createbytype (

Mcodeclooper, Outputmime.c_str (), TRUE/* Encoder */) {

Sp<mediacodec> codec = new Mediacodec (looper, PID);

Const STATUS_TRET = Codec->init (MIME, True/* Nameistype */, encoder);

}

    1. Mediacodec::init (const astring&name, BOOL Nameistype, BOOL encoder) {

...........

Name contains video, Nameistype is True

So

Mcodec =avfactory::get ()->createacodec ();

..............

Misvideo =true;

..............

Mcodeclooper->registerhandler (MCODEC);

Mcodec->setnotificationmessage (Newamessage (kwhatcodecnotify, this));

sp<amessage> msg = Newamessage (Kwhatinit, this);

Msg->setstring ("name", name);

Msg->setint32 ("Nameistype", Nameistype);

if (Nameistype) {

Msg->setint32 ("encoder", encoder);

}

status_t err;

vector<mediaresource> resources;

const char *type = Securecodec? Kresourcesecurecodec:kresourcenonsecurecodec;

const char *subtype = misvideo? Kresourcevideocodec:kresourceaudiocodec;

Resources.push_back (Mediaresource (String8 (type), String8 (subtype), 1));

for (int i = 0; I <= kmaxretry; ++i) {

if (i > 0) {

Don ' t try to reclaim resourcefor the first time.

if (!mresourcemanagerservice->reclaimresource (resources)) {

Break

}

}

sp<amessage> response;

Err = Postandawaitresponse (msg,&response);

if (!isresourceerror (err)) {

Break

}

}

}


This article is from the "Dragon Dance" blog, please be sure to keep this source http://westfisher.blog.51cto.com/3396434/1945121

Using Android Mediarecorder C + + class for video encoding

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.