mediaplayer.h revision 89fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5
1/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_MEDIAPLAYER_H
18#define ANDROID_MEDIAPLAYER_H
19
20#include <utils/IMemory.h>
21#include <ui/Surface.h>
22#include <media/IMediaPlayerClient.h>
23#include <media/IMediaPlayer.h>
24#include <media/IMediaPlayerService.h>
25#include <utils/SortedVector.h>
26
27namespace android {
28
29enum media_event_type {
30    MEDIA_NOP               = 0, // interface test message
31    MEDIA_PREPARED          = 1,
32    MEDIA_PLAYBACK_COMPLETE = 2,
33    MEDIA_BUFFERING_UPDATE  = 3,
34    MEDIA_SEEK_COMPLETE     = 4,
35    MEDIA_SET_VIDEO_SIZE    = 5,
36    MEDIA_ERROR             = 100,
37};
38
39typedef int media_error_type;
40const media_error_type MEDIA_ERROR_UNKNOWN = 1;
41const media_error_type MEDIA_ERROR_SERVER_DIED = 100;
42
43enum media_player_states {
44    MEDIA_PLAYER_STATE_ERROR        = 0,
45    MEDIA_PLAYER_IDLE               = 1 << 0,
46    MEDIA_PLAYER_INITIALIZED        = 1 << 1,
47    MEDIA_PLAYER_PREPARING          = 1 << 2,
48    MEDIA_PLAYER_PREPARED           = 1 << 3,
49    MEDIA_PLAYER_STARTED            = 1 << 4,
50    MEDIA_PLAYER_PAUSED             = 1 << 5,
51    MEDIA_PLAYER_STOPPED            = 1 << 6,
52    MEDIA_PLAYER_PLAYBACK_COMPLETE  = 1 << 7
53};
54
55// ----------------------------------------------------------------------------
56// ref-counted object for callbacks
57class MediaPlayerListener: virtual public RefBase
58{
59public:
60    virtual void notify(int msg, int ext1, int ext2) = 0;
61};
62
63class MediaPlayer : public BnMediaPlayerClient
64{
65public:
66    MediaPlayer();
67    ~MediaPlayer();
68            void            onFirstRef();
69            void            disconnect();
70            status_t        setDataSource(const char *url);
71            status_t        setDataSource(int fd, int64_t offset, int64_t length);
72            status_t        setVideoSurface(const sp<Surface>& surface);
73            status_t        setListener(const sp<MediaPlayerListener>& listener);
74            status_t        prepare();
75            status_t        prepareAsync();
76            status_t        start();
77            status_t        stop();
78            status_t        pause();
79            bool            isPlaying();
80            status_t        getVideoWidth(int *w);
81            status_t        getVideoHeight(int *h);
82            status_t        seekTo(int msec);
83            status_t        getCurrentPosition(int *msec);
84            status_t        getDuration(int *msec);
85            status_t        reset();
86            status_t        setAudioStreamType(int type);
87            status_t        setLooping(int loop);
88            bool            isLooping();
89            status_t        setVolume(float leftVolume, float rightVolume);
90            void            notify(int msg, int ext1, int ext2);
91    static  sp<IMemory>     decode(const char* url, uint32_t *pSampleRate, int* pNumChannels, int* pFormat);
92    static  sp<IMemory>     decode(int fd, int64_t offset, int64_t length, uint32_t *pSampleRate, int* pNumChannels, int* pFormat);
93
94private:
95            void            clear_l();
96            status_t        seekTo_l(int msec);
97            status_t        prepareAsync_l();
98            status_t        getDuration_l(int *msec);
99            status_t        setDataSource(const sp<IMediaPlayer>& player);
100
101    static const sp<IMediaPlayerService>& getMediaPlayerService();
102    static void addObitRecipient(const wp<MediaPlayer>& recipient);
103    static void removeObitRecipient(const wp<MediaPlayer>& recipient);
104
105    class DeathNotifier: public IBinder::DeathRecipient
106    {
107    public:
108                DeathNotifier() {}
109        virtual ~DeathNotifier();
110
111        virtual void binderDied(const wp<IBinder>& who);
112    };
113
114    sp<IMediaPlayer>            mPlayer;
115    Mutex                       mLock;
116    Mutex                       mNotifyLock;
117    Condition                   mSignal;
118    sp<MediaPlayerListener>     mListener;
119    void*                       mCookie;
120    media_player_states         mCurrentState;
121    int                         mDuration;
122    int                         mCurrentPosition;
123    int                         mSeekPosition;
124    bool                        mPrepareSync;
125    status_t                    mPrepareStatus;
126    int                         mStreamType;
127    bool                        mLoop;
128    float                       mLeftVolume;
129    float                       mRightVolume;
130    int                         mVideoWidth;
131    int                         mVideoHeight;
132
133    friend class DeathNotifier;
134
135    static  Mutex                           sServiceLock;
136    static  sp<IMediaPlayerService>         sMediaPlayerService;
137    static  sp<DeathNotifier>               sDeathNotifier;
138    static  SortedVector< wp<MediaPlayer> > sObitRecipients;
139};
140
141}; // namespace android
142
143#endif // ANDROID_MEDIAPLAYER_H
144
145