利用:
mediaplayer和SurfaceComposerClient
在native层创建surface,然后指定url进行播放。
环境: Android 8.1 rk3399pro
例如:
TestMediaserver http://172.18.10.76/1.ts
目录结构:
frameworks/av/media/test_player
├── Android.mk
└── TestMediaPlayer.cpp
TestMediaPlayer.cpp:
#define LOG_TAG "test_mediaserver"
#define LOG_NDEBUG 0
#include <media/mediaplayer.h>
#include <media/IMediaHTTPService.h>
#include <gui/Surface.h>
#include <gui/SurfaceComposerClient.h>
#include <gui/ISurfaceComposer.h>
#include <ui/DisplayInfo.h>
#include <ui/Rect.h>
#include <ui/Region.h>
#include <android/native_window.h>
using namespace android;
bool gIsEnd;
class TestMediaPlayerListener : public MediaPlayerListener
{
public:
TestMediaPlayerListener(sp<MediaPlayer>& player):mp(player) {}
void notify(int msg, int ext1, int ext2, const Parcel *obj)
{
(void)msg;
(void)ext1;
(void)ext2;
(void)obj;
switch (msg)
{
case MEDIA_INFO:
{
ALOGE("MEDIA_INFO, ext1 : %d, ext2 : %d ", ext1, ext2);
if (MEDIA_INFO_RENDERING_START == ext1) {
ALOGE("begin to rendering frame now!!!!!!!!!");
}
}
break;
case MEDIA_PREPARED:
{
ALOGE("MEDIA_PREPARED, ext1 : %d, ext2 : %d ", ext1, ext2);
if (mp.get())
{
gIsEnd = false;
mp->start();
}
}
break;
case MEDIA_ERROR:
ALOGE("MEDIA_ERROR, ext1 : %d, ext2 : %d ", ext1, ext2);
break;
case MEDIA_PLAYBACK_COMPLETE:
{
ALOGE("MEDIA_PLAYBACK_COMPLETE, ext1 : %d, ext2 : %d ", ext1, ext2);
gIsEnd = true;
}
break;
case MEDIA_STOPPED:
{
ALOGE("MEDIA_STOPPED, ext1 : %d, ext2 : %d ", ext1, ext2);
}
break;
default:
break;
}
}
private:
sp<MediaPlayer> mp;
};
int main (int argc __unused, char **argv __unused) {
if (argc < 2)
{
printf("Use TestMediaserver http://xxxxx \n");
return 0;
}
int argv_index = 0;
for(; argv_index<argc; argv_index++)
{
printf("argv[%d]: %s \n", argv_index, argv[argv_index]);
}
String8 url(argv[1]);
bool isStreaming = false;
if (url.contains("http://") || url.contains("https://"))
{
printf("This is a streaming playback url: %s\n", url.string());
isStreaming = true;
}
else
{
printf("local file \n");
}
sp<MediaPlayer> mp = new MediaPlayer();
if (true == isStreaming)
{
sp<IMediaHTTPService> httpService = NULL;
mp->setDataSource(
httpService,
url.string(),
NULL);
}
else
{
// open fd in here
}
// create a client to surfaceflinger
sp<SurfaceComposerClient> client = new SurfaceComposerClient();
sp<SurfaceControl> surfaceControl =
client->createSurface( String8("resize"),
800,
600,
PIXEL_FORMAT_RGB_565, 0);
sp<Surface> surface = surfaceControl->getSurface();
SurfaceComposerClient::openGlobalTransaction();
surfaceControl->setLayer(100000);
SurfaceComposerClient::closeGlobalTransaction();
mp->setVideoSurfaceTexture( surface->getIGraphicBufferProducer() );
sp<TestMediaPlayerListener> listener = new TestMediaPlayerListener(mp);
mp->setListener(listener);
mp->prepareAsync();
while(1)
{
if (true == gIsEnd)
{
ALOGE("Exit!!!!!!!!!!!!!!!");
break;
}
usleep(1000);
}
SurfaceComposerClient::openGlobalTransaction();
surfaceControl->clear();
SurfaceComposerClient::closeGlobalTransaction();
mp->reset();
ALOGE(" End !!!!!!!!!!");
return 0;
}
Android.mk
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:= \
TestMediaPlayer.cpp
LOCAL_SHARED_LIBRARIES := \
liblog \
libmediaplayerservice \
libmedia \
libutils \
libbinder \
libgui \
LOCAL_C_INCLUDES := \
frameworks/av/media/libmediaplayerservice \
frameworks/av/include \
frameworks/native/libs/gui/include \
frameworks/native/libs/nativewindow/include \
LOCAL_MODULE:= TestMediaserver
LOCAL_32_BIT_ONLY := true
LOCAL_CFLAGS := -Werror -Wall
include $(BUILD_EXECUTABLE)