116ce39d96d41884c7b0d1676553ab8167baaab74Jean-Michel Trivi/*
268d56b8ebaf60184a3aef988e3d2b09ed8b88c05Jean-Michel Trivi * Copyright (C) 2011 The Android Open Source Project
316ce39d96d41884c7b0d1676553ab8167baaab74Jean-Michel Trivi *
416ce39d96d41884c7b0d1676553ab8167baaab74Jean-Michel Trivi * Licensed under the Apache License, Version 2.0 (the "License");
516ce39d96d41884c7b0d1676553ab8167baaab74Jean-Michel Trivi * you may not use this file except in compliance with the License.
616ce39d96d41884c7b0d1676553ab8167baaab74Jean-Michel Trivi * You may obtain a copy of the License at
716ce39d96d41884c7b0d1676553ab8167baaab74Jean-Michel Trivi *
816ce39d96d41884c7b0d1676553ab8167baaab74Jean-Michel Trivi *      http://www.apache.org/licenses/LICENSE-2.0
916ce39d96d41884c7b0d1676553ab8167baaab74Jean-Michel Trivi *
1016ce39d96d41884c7b0d1676553ab8167baaab74Jean-Michel Trivi * Unless required by applicable law or agreed to in writing, software
1116ce39d96d41884c7b0d1676553ab8167baaab74Jean-Michel Trivi * distributed under the License is distributed on an "AS IS" BASIS,
1216ce39d96d41884c7b0d1676553ab8167baaab74Jean-Michel Trivi * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1316ce39d96d41884c7b0d1676553ab8167baaab74Jean-Michel Trivi * See the License for the specific language governing permissions and
1416ce39d96d41884c7b0d1676553ab8167baaab74Jean-Michel Trivi * limitations under the License.
1516ce39d96d41884c7b0d1676553ab8167baaab74Jean-Michel Trivi */
1616ce39d96d41884c7b0d1676553ab8167baaab74Jean-Michel Trivi
172b06e20ae32388f6e1dfd088d9773c34e6b1cb45Jean-Michel Trivi#ifndef __ANDROID_GENERICMEDIAPLAYER_H__
182b06e20ae32388f6e1dfd088d9773c34e6b1cb45Jean-Michel Trivi#define __ANDROID_GENERICMEDIAPLAYER_H__
1916ce39d96d41884c7b0d1676553ab8167baaab74Jean-Michel Trivi
2035a5a30fdad179ccf38d8d756590411326159a89Jean-Michel Trivi#include "android_GenericPlayer.h"
214ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi
2216ce39d96d41884c7b0d1676553ab8167baaab74Jean-Michel Trivi#include <binder/IServiceManager.h>
23e21f91df7afb1175ddb5b0045ebb0d5338df2535Andy McFadden#include <gui/IGraphicBufferProducer.h>
2416ce39d96d41884c7b0d1676553ab8167baaab74Jean-Michel Trivi
2516ce39d96d41884c7b0d1676553ab8167baaab74Jean-Michel Trivi
2616ce39d96d41884c7b0d1676553ab8167baaab74Jean-Michel Trivi//--------------------------------------------------------------------------------------------------
2716ce39d96d41884c7b0d1676553ab8167baaab74Jean-Michel Trivinamespace android {
2816ce39d96d41884c7b0d1676553ab8167baaab74Jean-Michel Trivi
2937dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Triviclass GenericMediaPlayer;
3016ce39d96d41884c7b0d1676553ab8167baaab74Jean-Michel Triviclass MediaPlayerNotificationClient : public BnMediaPlayerClient
3116ce39d96d41884c7b0d1676553ab8167baaab74Jean-Michel Trivi{
3216ce39d96d41884c7b0d1676553ab8167baaab74Jean-Michel Trivipublic:
339bf773df2b8a0cf788f394c326960bc3a5af7c60Chih-Hung Hsieh    explicit MediaPlayerNotificationClient(GenericMediaPlayer* gmp);
3416ce39d96d41884c7b0d1676553ab8167baaab74Jean-Michel Trivi
3516ce39d96d41884c7b0d1676553ab8167baaab74Jean-Michel Trivi    // IMediaPlayerClient implementation
362f6d462d89356cdaa77299ca27b60c5cca198d98Gloria Wang    virtual void notify(int msg, int ext1, int ext2, const Parcel *obj);
3716ce39d96d41884c7b0d1676553ab8167baaab74Jean-Michel Trivi
3849935c51fddcd0caa0030e2aac0c3a7ba3339e3dGlenn Kasten    // Call before enqueuing a prepare event
3949935c51fddcd0caa0030e2aac0c3a7ba3339e3dGlenn Kasten    void beforePrepare();
4049935c51fddcd0caa0030e2aac0c3a7ba3339e3dGlenn Kasten
4149935c51fddcd0caa0030e2aac0c3a7ba3339e3dGlenn Kasten    // Call after enqueueing the prepare event; returns true if the prepare
4249935c51fddcd0caa0030e2aac0c3a7ba3339e3dGlenn Kasten    // completed successfully, or false if it completed unsuccessfully
4349935c51fddcd0caa0030e2aac0c3a7ba3339e3dGlenn Kasten    bool blockUntilPlayerPrepared();
4416ce39d96d41884c7b0d1676553ab8167baaab74Jean-Michel Trivi
45513222822545c3e954176476b263df52a47f43a4Glenn Kastenprotected:
46513222822545c3e954176476b263df52a47f43a4Glenn Kasten    virtual ~MediaPlayerNotificationClient();
47513222822545c3e954176476b263df52a47f43a4Glenn Kasten
4816ce39d96d41884c7b0d1676553ab8167baaab74Jean-Michel Triviprivate:
4991ff087fb814063f9faa23ab37a61e8fe4e38f45Glenn Kasten    const wp<GenericMediaPlayer> mGenericMediaPlayer;
5091ff087fb814063f9faa23ab37a61e8fe4e38f45Glenn Kasten    Mutex mLock;                        // protects mPlayerPrepared
5191ff087fb814063f9faa23ab37a61e8fe4e38f45Glenn Kasten    Condition mPlayerPreparedCondition; // signalled when mPlayerPrepared is changed
5249935c51fddcd0caa0030e2aac0c3a7ba3339e3dGlenn Kasten    enum {
5349935c51fddcd0caa0030e2aac0c3a7ba3339e3dGlenn Kasten        PREPARE_NOT_STARTED,
5449935c51fddcd0caa0030e2aac0c3a7ba3339e3dGlenn Kasten        PREPARE_IN_PROGRESS,
5549935c51fddcd0caa0030e2aac0c3a7ba3339e3dGlenn Kasten        PREPARE_COMPLETED_SUCCESSFULLY,
5649935c51fddcd0caa0030e2aac0c3a7ba3339e3dGlenn Kasten        PREPARE_COMPLETED_UNSUCCESSFULLY
5749935c51fddcd0caa0030e2aac0c3a7ba3339e3dGlenn Kasten    } mPlayerPrepared;
5816ce39d96d41884c7b0d1676553ab8167baaab74Jean-Michel Trivi};
5916ce39d96d41884c7b0d1676553ab8167baaab74Jean-Michel Trivi
6068d56b8ebaf60184a3aef988e3d2b09ed8b88c05Jean-Michel Trivi
61513222822545c3e954176476b263df52a47f43a4Glenn Kastenclass MediaPlayerDeathNotifier : public IMediaDeathNotifier {
62513222822545c3e954176476b263df52a47f43a4Glenn Kastenpublic:
639bf773df2b8a0cf788f394c326960bc3a5af7c60Chih-Hung Hsieh    explicit MediaPlayerDeathNotifier(const sp<MediaPlayerNotificationClient>& playerClient) :
64513222822545c3e954176476b263df52a47f43a4Glenn Kasten        mPlayerClient(playerClient) {
65513222822545c3e954176476b263df52a47f43a4Glenn Kasten    }
66513222822545c3e954176476b263df52a47f43a4Glenn Kasten
67513222822545c3e954176476b263df52a47f43a4Glenn Kasten    void died() {
68513222822545c3e954176476b263df52a47f43a4Glenn Kasten        mPlayerClient->notify(MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED, 0, NULL);
69513222822545c3e954176476b263df52a47f43a4Glenn Kasten    }
70513222822545c3e954176476b263df52a47f43a4Glenn Kasten
71513222822545c3e954176476b263df52a47f43a4Glenn Kastenprotected:
72513222822545c3e954176476b263df52a47f43a4Glenn Kasten    virtual ~MediaPlayerDeathNotifier() { }
73513222822545c3e954176476b263df52a47f43a4Glenn Kasten
74513222822545c3e954176476b263df52a47f43a4Glenn Kastenprivate:
75513222822545c3e954176476b263df52a47f43a4Glenn Kasten    const sp<MediaPlayerNotificationClient> mPlayerClient;
76513222822545c3e954176476b263df52a47f43a4Glenn Kasten};
77513222822545c3e954176476b263df52a47f43a4Glenn Kasten
78513222822545c3e954176476b263df52a47f43a4Glenn Kasten
7916ce39d96d41884c7b0d1676553ab8167baaab74Jean-Michel Trivi//--------------------------------------------------------------------------------------------------
8068d56b8ebaf60184a3aef988e3d2b09ed8b88c05Jean-Michel Triviclass GenericMediaPlayer : public GenericPlayer
8116ce39d96d41884c7b0d1676553ab8167baaab74Jean-Michel Trivi{
8216ce39d96d41884c7b0d1676553ab8167baaab74Jean-Michel Trivipublic:
83581a0f550f15f6fc22199cb85775a220f668b480Jean-Michel Trivi
8468d56b8ebaf60184a3aef988e3d2b09ed8b88c05Jean-Michel Trivi    GenericMediaPlayer(const AudioPlayback_Parameters* params, bool hasVideo);
8568d56b8ebaf60184a3aef988e3d2b09ed8b88c05Jean-Michel Trivi    virtual ~GenericMediaPlayer();
8616ce39d96d41884c7b0d1676553ab8167baaab74Jean-Michel Trivi
874b0e0b2860ffd5e246b42c8a434833cca2f068b3Jean-Michel Trivi    virtual void preDestroy();
884b0e0b2860ffd5e246b42c8a434833cca2f068b3Jean-Michel Trivi
894b0e0b2860ffd5e246b42c8a434833cca2f068b3Jean-Michel Trivi    // overridden from GenericPlayer
904b0e0b2860ffd5e246b42c8a434833cca2f068b3Jean-Michel Trivi    virtual void getPositionMsec(int* msec); // ANDROID_UNKNOWN_TIME if unknown
914b0e0b2860ffd5e246b42c8a434833cca2f068b3Jean-Michel Trivi
92e21f91df7afb1175ddb5b0045ebb0d5338df2535Andy McFadden    virtual void setVideoSurfaceTexture(const sp<IGraphicBufferProducer> &bufferProducer);
9316ce39d96d41884c7b0d1676553ab8167baaab74Jean-Michel Trivi
9491145ef159d3e165a461cbd76341ff8ed3d72baeJean-Michel Trivi    virtual void setPlaybackRate(int32_t ratePermille);
9591145ef159d3e165a461cbd76341ff8ed3d72baeJean-Michel Trivi
9616ce39d96d41884c7b0d1676553ab8167baaab74Jean-Michel Triviprotected:
9737dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi    friend class MediaPlayerNotificationClient;
9816ce39d96d41884c7b0d1676553ab8167baaab74Jean-Michel Trivi
9968d56b8ebaf60184a3aef988e3d2b09ed8b88c05Jean-Michel Trivi    // Async event handlers (called from GenericPlayer's event loop)
100581a0f550f15f6fc22199cb85775a220f668b480Jean-Michel Trivi    virtual void onPrepare();
101581a0f550f15f6fc22199cb85775a220f668b480Jean-Michel Trivi    virtual void onPlay();
102581a0f550f15f6fc22199cb85775a220f668b480Jean-Michel Trivi    virtual void onPause();
1034ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi    virtual void onSeek(const sp<AMessage> &msg);
1044ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi    virtual void onLoop(const sp<AMessage> &msg);
105f6445d330c05ccc57d1adcc6ee05735a33f78881Glenn Kasten    virtual void onSeekComplete();
10637dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi    virtual void onVolumeUpdate();
1074ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi    virtual void onBufferingUpdate(const sp<AMessage> &msg);
1083610785fa93586ce84a27a27530feb77b8035229Glenn Kasten    virtual void onAttachAuxEffect(const sp<AMessage> &msg);
1093610785fa93586ce84a27a27530feb77b8035229Glenn Kasten    virtual void onSetAuxEffectSendLevel(const sp<AMessage> &msg);
110581a0f550f15f6fc22199cb85775a220f668b480Jean-Michel Trivi
11191ff087fb814063f9faa23ab37a61e8fe4e38f45Glenn Kasten    const bool mHasVideo;   // const allows MediaPlayerNotificationClient::notify to safely access
1127ef5526a7bd12eccfa777cc8bc167794634f405aJean-Michel Trivi    int32_t mSeekTimeMsec;
113581a0f550f15f6fc22199cb85775a220f668b480Jean-Michel Trivi
114e21f91df7afb1175ddb5b0045ebb0d5338df2535Andy McFadden    sp<IGraphicBufferProducer> mVideoSurfaceTexture;
11516ce39d96d41884c7b0d1676553ab8167baaab74Jean-Michel Trivi
116a0fa47f72f47fffb80ab2ae791739ce73de1e8f4Glenn Kasten    // only safe to access from within Realize and looper
11716ce39d96d41884c7b0d1676553ab8167baaab74Jean-Michel Trivi    sp<IMediaPlayer> mPlayer;
118581a0f550f15f6fc22199cb85775a220f668b480Jean-Michel Trivi    // Receives Android MediaPlayer events from mPlayer
11991ff087fb814063f9faa23ab37a61e8fe4e38f45Glenn Kasten    const sp<MediaPlayerNotificationClient> mPlayerClient;
12016ce39d96d41884c7b0d1676553ab8167baaab74Jean-Michel Trivi
121513222822545c3e954176476b263df52a47f43a4Glenn Kasten    // Receives notifications about death of media.player service
122513222822545c3e954176476b263df52a47f43a4Glenn Kasten    const sp<MediaPlayerDeathNotifier> mPlayerDeathNotifier;
123513222822545c3e954176476b263df52a47f43a4Glenn Kasten
124b47c7beddecd38ae565d84b72da526843bfdd43eSteve Block    // Return a reference to the media player service, or ALOGE and return NULL after retries fail
12585edd878a30caa535b0267d8d6e61b4ccc0d5fd0Glenn Kasten    static const sp<IMediaPlayerService> getMediaPlayerService() {
12685edd878a30caa535b0267d8d6e61b4ccc0d5fd0Glenn Kasten        return IMediaDeathNotifier::getMediaPlayerService();
12785edd878a30caa535b0267d8d6e61b4ccc0d5fd0Glenn Kasten    }
1284ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi
12968d56b8ebaf60184a3aef988e3d2b09ed8b88c05Jean-Michel Triviprivate:
13068d56b8ebaf60184a3aef988e3d2b09ed8b88c05Jean-Michel Trivi    DISALLOW_EVIL_CONSTRUCTORS(GenericMediaPlayer);
13106059e5ee1eaf907589c7f8d1320253f92211348Glenn Kasten    void afterMediaPlayerPreparedSuccessfully();
1325933f3d5e532aaac31ce0e6551c59f0197c0ae3cGlenn Kasten
133a0fa47f72f47fffb80ab2ae791739ce73de1e8f4Glenn Kastenprotected:  // FIXME temporary
134a0fa47f72f47fffb80ab2ae791739ce73de1e8f4Glenn Kasten    Mutex mPreparedPlayerLock;          // protects mPreparedPlayer
135a0fa47f72f47fffb80ab2ae791739ce73de1e8f4Glenn Kasten    sp<IMediaPlayer> mPreparedPlayer;   // non-NULL if MediaPlayer exists and prepared, write once
136a0fa47f72f47fffb80ab2ae791739ce73de1e8f4Glenn Kastenprivate:
137a0fa47f72f47fffb80ab2ae791739ce73de1e8f4Glenn Kasten    void getPreparedPlayer(sp<IMediaPlayer> &preparedPlayer);   // safely read mPreparedPlayer
1385933f3d5e532aaac31ce0e6551c59f0197c0ae3cGlenn Kasten
13916ce39d96d41884c7b0d1676553ab8167baaab74Jean-Michel Trivi};
14016ce39d96d41884c7b0d1676553ab8167baaab74Jean-Michel Trivi
14116ce39d96d41884c7b0d1676553ab8167baaab74Jean-Michel Trivi} // namespace android
1422b06e20ae32388f6e1dfd088d9773c34e6b1cb45Jean-Michel Trivi
143833251ab9e5e59a6ea5ac325122cf3abdf7cd944Glenn Kasten// is the specified URI a known distant protocol?
144833251ab9e5e59a6ea5ac325122cf3abdf7cd944Glenn Kastenbool isDistantProtocol(const char *uri);
145833251ab9e5e59a6ea5ac325122cf3abdf7cd944Glenn Kasten
1462b06e20ae32388f6e1dfd088d9773c34e6b1cb45Jean-Michel Trivi#endif /* __ANDROID_GENERICMEDIAPLAYER_H__ */
147