in this case, media source is camera. (Learning platform is android sdk6.0)
Call Graph:
Startrecording:
Recorder = New Mediarecorder (String16 ());
Classmediarecorder:public Bnmediarecorderclient,
Public Virtualimediadeathnotifier
When we create the Mediarecorder, we get the Imediaplayerservice pointer before we create the Imediarecorder
( declaration of sp<imediarecorder> Mmediarecorder;)
Mmediarecorder =service->createmediarecorder (oppackagename);
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);
Finally, the local corresponding object is created according to the remote reply.
Interface_cast<imediarecorder> (Reply.readstrongbinder ());
The following methods are passed Bpmediarecorder(remote) to send to the bnmediarecorder operation.
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);
};
-
in this method , each operation has a corresponding processing branch
-
c) is a virtual function, Specific work by mediarecorderclient to complete: mediarecorderclient:public bnmediarecorder
-
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);
Recorder->setparameters (...)
Recorder->prepare ()
Recorder->start ()
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);
Therefore, the specific work is done by Stagefrightrecorder to finish.
in the in AVMediaServiceFactory.cpp
Stagefrightrecorder*avmediaservicefactory::createstagefrightrecorder (
Const STRING16 &oppackagename) {
Return Newstagefrightrecorder (Oppackagename);
}
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
Setupmpeg4orwebmrecording ()
Here you create a Mpeg4writer
writer = Mp4writer =avfactory::get ()->creatempeg4writer (MOUTPUTFD);
Setupmediasource specifying the camera as the input source for encoder
Sp<mediasource> encoder;
Err = Setupvideoencoder (MediaSource, &encoder); Create Encoder
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);
Sp<mediacodecsource> Encoder
=mediacodecsource::create (
Mlooper,
Format
Camerasource,
Mpersistentsurface,
;
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;
}
Mediacodecsource::init ()
status_t err = Initencoder ();
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 ();
}
Mediacodec::createbytype (
Mcodeclooper, Outputmime.c_str (), TRUE/* Encoder */) {
Sp<mediacodec> codec = new Mediacodec (looper, PID);
Const STATUS_TRET = Codec->init (MIME, True/* Nameistype */, encoder);
}
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