Android MediaPlayerService解析

frameworks/av/media/libmediaplayerservice/MediaPlayerService.h

MediaPlayerService 类的继承关系如下:
Android MediaPlayerService解析_第1张图片

Client 和 AudioOutput 是 MediaPlayerService的内部类
IMediaPlayerService是对为提供的接口类, 定义如下:

frameworks/av/include/media/IMediaPlayerService.h
43class IMediaPlayerService: public IInterface
44{
45public:
46    DECLARE_META_INTERFACE(MediaPlayerService);
47
48    virtual sp createMediaRecorder(const String16 &opPackageName) = 0;
49    virtual sp createMetadataRetriever() = 0;
50    virtual sp create(const sp& client,
51            audio_session_t audioSessionId = AUDIO_SESSION_ALLOCATE) = 0;
52    virtual sp            getOMX() = 0;
53    virtual sp           makeHDCP(bool createEncryptionModule) = 0;
54    virtual sp getCodecList() const = 0;
55
56    // Connects to a remote display.
57    // 'iface' specifies the address of the local interface on which to listen for
58    // a connection from the remote display as an ip address and port number
59    // of the form "x.x.x.x:y".  The media server should call back into the provided remote
60    // display client when display connection, disconnection or errors occur.
61    // The assumption is that at most one remote display will be connected to the
62    // provided interface at a time.
63    virtual sp listenForRemoteDisplay(const String16 &opPackageName,
64            const sp& client, const String8& iface) = 0;
65
66    // codecs and audio devices usage tracking for the battery app
67    enum BatteryDataBits {
68        // tracking audio codec
69        kBatteryDataTrackAudio          = 0x1,
70        // tracking video codec
71        kBatteryDataTrackVideo          = 0x2,
72        // codec is started, otherwise codec is paused
73        kBatteryDataCodecStarted        = 0x4,
74        // tracking decoder (for media player),
75        // otherwise tracking encoder (for media recorder)
76        kBatteryDataTrackDecoder        = 0x8,
77        // start to play an audio on an audio device
78        kBatteryDataAudioFlingerStart   = 0x10,
79        // stop/pause the audio playback
80        kBatteryDataAudioFlingerStop    = 0x20,
81        // audio is rounted to speaker
82        kBatteryDataSpeakerOn           = 0x40,
83        // audio is rounted to devices other than speaker
84        kBatteryDataOtherAudioDeviceOn  = 0x80,
85    };
86
87    virtual void addBatteryData(uint32_t params) = 0;
88    virtual status_t pullBatteryData(Parcel* reply) = 0;
89};

MediaPlayerService 是native系统服务, 通过ServiceManager注册到系统中

frameworks/av/media/mediaserver/main_mediaserver.cpp
.....
38    sp<ProcessState> proc(ProcessState::self());
39    sp<IServiceManager> sm(defaultServiceManager());
40    ALOGI("ServiceManager: %p", sm.get());
41    InitializeIcuOrDie();
42    MediaPlayerService::instantiate();

instantiate() 创建MediaPlayerService并注册到系统服务中

frameworks/av/media/libmediaplayerservice/MediaPlayerService.cpp
267void MediaPlayerService::instantiate() {
268    defaultServiceManager()->addService(
269            String16("media.player"), new MediaPlayerService());
270}

MediaPlayerService构造函数如下:

272MediaPlayerService::MediaPlayerService()
273{
274    ALOGV("MediaPlayerService created");
275    mNextConnId = 1;
276
277    mBatteryAudio.refCount = 0;
278    for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
279        mBatteryAudio.deviceOn[i] = 0;
280        mBatteryAudio.lastTime[i] = 0;
281        mBatteryAudio.totalTime[i] = 0;
282    }
283    // speaker is on by default
284    mBatteryAudio.deviceOn[SPEAKER] = 1;
285
286    // reset battery stats
287    // if the mediaserver has crashed, battery stats could be left
288    // in bad state, reset the state upon service start.
289    BatteryNotifier::getInstance().noteResetVideo();
290    //register built-in player
291    MediaPlayerFactory::registerBuiltinFactories();
292}

registerBuiltinFactories() register the NU_PLAYER and TEST_PLAYER

243void MediaPlayerFactory::registerBuiltinFactories() {
244    Mutex::Autolock lock_(&sLock);
245
246    if (sInitComplete)
247        return;
248
249    registerFactory_l(new NuPlayerFactory(), NU_PLAYER);
250    registerFactory_l(new TestPlayerFactory(), TEST_PLAYER);
251
252    sInitComplete = true;
253}

createMediaRecorder() 创建Recorder用于录音

299sp MediaPlayerService::createMediaRecorder(const String16 &opPackageName)
300{
301    pid_t pid = IPCThreadState::self()->getCallingPid();
302    sp recorder = new MediaRecorderClient(this, pid, opPackageName);
303    wp w = recorder;
304    Mutex::Autolock lock(mLock);
305    mMediaRecorderClients.add(w);
306    ALOGV("Create new media recorder client from pid %d", pid);
307    return recorder;
308}

创建Client主要用于播放

325sp MediaPlayerService::create(const sp& client,
326        audio_session_t audioSessionId)
327{
328    pid_t pid = IPCThreadState::self()->getCallingPid();
329    int32_t connId = android_atomic_inc(&mNextConnId);
330
331    sp c = new Client(
332            this, pid, connId, client, audioSessionId,
333            IPCThreadState::self()->getCallingUid());
334
335    ALOGV("Create new client(%d) from pid %d, uid %d, ", connId, pid,
336         IPCThreadState::self()->getCallingUid());
337
338    wp w = c;
339    {
340        Mutex::Autolock lock(mLock);
341        mClients.add(w);
342    }
343    return c;
344}
567MediaPlayerService::Client::Client(
568        const sp& service, pid_t pid,
569        int32_t connId, const sp& client,
570        audio_session_t audioSessionId, uid_t uid)
571{
572    ALOGV("Client(%d) constructor", connId);
573    mPid = pid;
574    mConnId = connId;
575    mService = service;
576    mClient = client;
577    mLoop = false;
578    mStatus = NO_INIT;
579    mAudioSessionId = audioSessionId;
580    mUID = uid;
581    mRetransmitEndpointValid = false;
582    mAudioAttributes = NULL;
583
584#if CALLBACK_ANTAGONIZER
585    ALOGD("create Antagonizer");
586    mAntagonizer = new Antagonizer(notify, this);
587#endif
588}

你可能感兴趣的:(video)