media server分析

media server是android的一个重要服务,主要是与多媒体相关的一些系统服务。在 ServiceManager相关中我们已经分析了,media server是在系统init.rc脚本中启动的,并且在service manager之后。由于media server中包含了media player、audio flinger、camera等多个service相关的东西,本文则只取其中的media player service分析media server和service manager之间的交互。

1、main_mediaserver.cpp
main_mediaserver.cpp文件位于/av/media/mediaserver/目录。该目录下的Android.mk文件中可以清楚看出,media server中包含了多个部分,并且最终编译成了一个名为mediaserver的可执行文件。可执行文件存放在系统的/system/bin/目录下,和init.rc中配置的media进程的可执行文件路径一致。

LOCAL_C_INCLUDES := \
    frameworks/av/media/libmediaplayerservice \
    frameworks/av/services/audioflinger \
    frameworks/av/services/camera/libcameraservice \
    frameworks/native/services/audioflinger

LOCAL_MODULE:= mediaserver    // 编译模块名

include $(BUILD_EXECUTABLE)   // 编译成可执行文件

main_mediaserver.cpp的代码很简单。主要是创建binder通讯的环境,初始化多个service,然后进入循环talk with binder driver状态。

int main(int argc, char** argv)
{
    signal(SIGPIPE, SIG_IGN);
    sp<ProcessState> proc(ProcessState::self());  // 初始化一个ProcessState对象
    sp<IServiceManager> sm = defaultServiceManager(); // 初始化BpServiceManager对象
    ALOGI("ServiceManager: %p", sm.get());
    AudioFlinger::instantiate();
    MediaPlayerService::instantiate(); // media player service初始化
    CameraService::instantiate();
    AudioPolicyService::instantiate();
    ProcessState::self()->startThreadPool();  // 启动PooledThread
    IPCThreadState::self()->joinThreadPool(); // talk with binder driver循环
}

2、MediaPlayerService.cpp
文件位于/av/media/libmediaplayerservice/目录。

void MediaPlayerService::instantiate() {
    defaultServiceManager()->addService(
            String16("media.player"), new MediaPlayerService());
}

// 创建一个MediaPlayer对象
sp<IMediaPlayer> MediaPlayerService::create(pid_t pid, const sp<IMediaPlayerClient>& client,
        int audioSessionId)
{
    int32_t connId = android_atomic_inc(&mNextConnId);

    sp<Client> c = new Client(
            this, pid, connId, client, audioSessionId,
            IPCThreadState::self()->getCallingUid());

    ALOGV("Create new client(%d) from pid %d, uid %d, ", connId, pid, 
         IPCThreadState::self()->getCallingUid());

    wp<Client> w = c; 
    {    
        Mutex::Autolock lock(mLock);
        mClients.add(w);
    }    
    return c;
}

在init时,直接获取到一个BpServiceManager对象,并通过其addService()接口,添加了一个MediaPlayerService对象到ServiceManager。defaultServiceManager()和addService()接口我们在native binder相关类中已经分析过了,前者将返回一个由一个handle == 0的BpBinder构造出来的BpServiceManager,而addService()方法将会调用到service_manager.c中的do_add_service()方法。需要注意的是service manager的Bn端实现,和普通的binder接口的Bn端实现不一样。普通的binder接口的Bn端,都有一个onTransact()方法,在该方法里对binder命令解析,并分发到接口方法的实现里。service manager的Bn端没有onTransact()接口,而是在一个binder_loop()循环中,不断的解析binder命令,并返回数据。具体分析可以参考ServiceManager启动流程

发布了30 篇原创文章 · 获赞 3 · 访问量 3万+
展开阅读全文

ICamera died,MediaMetadataRetriever server died!

10-19

使用视频录像功能报错,请大神们帮忙看一下 10-19 11:44:10.875: V/Camera-JNI(24054): get_native_camera: context=0x744aaeb0, camera=0x40ddbc38 10-19 11:44:10.895: I/MediaRecorderJNI(24054): prepare: surface=0x742b8a18 (identity=156) 10-19 11:44:11.515: W/Camera(24054): ICamera died 10-19 11:44:11.515: W/MediaMetadataRetriever(24054): MediaMetadataRetriever server died! 10-19 11:44:11.515: V/Camera-JNI(24054): notify 10-19 11:44:11.515: W/Camera(24054): Camera server died! 10-19 11:44:11.515: W/IMediaDeathNotifier(24054): media server died 10-19 11:44:11.515: E/Camera(24054): Error 100 10-19 11:44:13.077: V/Camera-JNI(24054): setHasPreviewCallback: installed:0, manualBuffer:0 10-19 11:44:13.077: V/Camera-JNI(24054): get_native_camera: context=0x744aaeb0, camera=0x40ddbc38 10-19 11:44:13.077: V/Camera-JNI(24054): Clearing callback buffers, 0 remained 10-19 11:44:13.077: V/Camera-JNI(24054): stopPreview 10-19 11:44:13.077: V/Camera-JNI(24054): get_native_camera: context=0x744aaeb0, camera=0x40ddbc38 10-19 11:44:13.077: V/Camera-JNI(24054): lock 10-19 11:44:13.077: V/Camera-JNI(24054): get_native_camera: context=0x744aaeb0, camera=0x40ddbc38 10-19 11:44:13.077: V/Camera-JNI(24054): release camera 10-19 11:44:13.077: V/Camera-JNI(24054): release 10-19 11:44:13.077: V/Camera-JNI(24054): Clearing callback buffers, 0 remained 10-19 11:44:13.077: V/Camera-JNI(24054): Clearing callback buffers, 0 remained 10-19 11:44:13.077: V/Camera-JNI(24054): native_release: context=0x744aaeb0 camera=0x40ddbc38 10-19 11:44:13.077: V/Camera-JNI(24054): release 10-19 11:44:13.077: V/Camera-JNI(24054): Clearing callback buffers, 0 remained 10-19 11:44:13.077: V/Camera-JNI(24054): Clearing callback buffers, 0 remained 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览