Tuesday, February 24, 2009

1. Native console app for video playback in Android

I spent some time for writing a native C++  app to test video in console, actually it seems to be much easier than I thought:) Below is the code snippet:

PS. I thought I should paint a hole above video surface as SurfaceView does, but it appears not necessary.


int main(int argc, char** argv)
{
LOGI("entering main...");
sp<ProcessState> proc = ProcessState::self();
proc->startThreadPool();
MediaPlayer mediaplayer;
if(argc > 1)
{
LOGI("set datasource: %s", argv[1]);
mediaplayer.setDataSource(argv[1]);
}
else
{
LOGI("set datasource: /data/test.mp4");
mediaplayer.setDataSource("/data/test.mp4");
}

LOGI("create SurfaceComposerClient");
int pid = getpid();
int nState = 0;
sp<SurfaceComposerClient> videoClient = new SurfaceComposerClient;

LOGI("create video surface");
sp<surface> videoSurface(videoClient->createSurface(pid, 0, 176, 144, PIXEL_FORMAT_OPAQUE, ISurfaceComposer::eFXSurfaceNormal|ISurfaceComposer::ePushBuffers));
videoClient->openTransaction();
//nState = videoSurface->setSize(176, 144);
//LOGI("videosurface->setSize, %d", nState);
//nState = videoSurface->setPosition(0, 0);
//LOGI("videosurface->setPosition, %d", nState);

//set toppest z-order
nState = videoSurface->setLayer(INT_MAX);
LOGI("videosurface->setLayer, %d", nState);
nState = videoSurface->show();
LOGI("videosurface->show, %d", nState);
videoClient->closeTransaction();

LOGI("set video surface to player");
mediaplayer.setVideoSurface(videoSurface);

status_t retCode = mediaplayer.prepare();
if(retCode < 0)
{
LOGE("prepare failed: %d\n", retCode);
IPCThreadState::self()->stopProcess();
return -1;
};

mediaplayer.start();
for(int i=0; i < 10; i++)
{
sleep(1);
LOGI("playing, i=%d\n", i);
}
mediaplayer.reset();
LOGI("quiting...");

//close binder fd, still need waiting for all binder threads exit?
IPCThreadState::self()->stopProcess();
return 0;
}

4 comments:

Unknown said...

hi
How did u get this to work. I encounter the error "W/ServiceManager( 1321): Permission failure: android.permission.ACCESS_SURFACE_FLINGER from uid=10037 pid=3058"
Any idea of hoe to get rid of this?

Anonymous said...

hi,it's amazing!
Can you tell me how do you figure out how to write native code for android API? do you find out the necessary functions to call based on the java code of the app?

vignesh said...

Hi,
I tried to complie tis code but its showing error 'sp' was not declared in this scope and many more.
Then i added the header file "utils/ProcessState.h" and linking the library -lutils but no improvement this time it showing utils/ProcessState.h: No such file or directory compilation terminated. So what should i do comple and run properly

with anticipation
VWS

Li Chen said...

Hi Vignesh,

Sorry the code is a bit obsolete as the native header files keep changing with new Android releases:)
I've uploaded it to github(https://github.com/freepine/MediaTest/tree/master/ConsoleApp) some time ago and I'll try to have it compliant with JB4.2.2 in a couple of days.