mediaplayer.h revision ebb020aebc157ff127458dc20e1d0066b86bb99d
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    MEDIA_INFO              = 200,
38};
39
40// Generic error codes for the media player framework.  Errors are fatal, the
41// playback must abort.
42//
43// Errors are communicated back to the client using the
44// MediaPlayerListener::notify method defined below.
45// In this situation, 'notify' is invoked with the following:
46//   'msg' is set to MEDIA_ERROR.
47//   'ext1' should be a value from the enum media_error_type.
48//   'ext2' contains an implementation dependant error code to provide
49//          more details. Should default to 0 when not used.
50//
51// The codes are distributed as follow:
52//   0xx: Reserved
53//   1xx: Android Player errors. Something went wrong inside the MediaPlayer.
54//   2xx: Media errors (e.g Codec not supported). There is a problem with the
55//        media itself.
56//   3xx: Runtime errors. Some extraordinary condition arose making the playback
57//        impossible.
58//
59enum media_error_type {
60    // 0xx
61    MEDIA_ERROR_UNKNOWN = 1,
62    // 1xx
63    MEDIA_ERROR_SERVER_DIED = 100,
64    // 2xx
65    MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK = 200,
66    // 3xx
67};
68
69
70// Info and warning codes for the media player framework.  These are non fatal,
71// the playback is going on but there might be some user visible issues.
72//
73// Info and warning messages are communicated back to the client using the
74// MediaPlayerListener::notify method defined below.  In this situation,
75// 'notify' is invoked with the following:
76//   'msg' is set to MEDIA_INFO.
77//   'ext1' should be a value from the enum media_info_type.
78//   'ext2' contains an implementation dependant error code to provide
79//          more details. Should default to 0 when not used.
80//
81// The codes are distributed as follow:
82//   0xx: Reserved
83//   7xx: Android Player info/warning (e.g player lagging behind.)
84//   8xx: Media info/warning (e.g media badly interleaved.)
85//
86enum media_info_type {
87    // 0xx
88    MEDIA_INFO_UNKNOWN = 1,
89    // 7xx
90    // The video is too complex for the decoder: it can't decode frames fast
91    // enough. Possibly only the audio plays fine at this stage.
92    MEDIA_INFO_VIDEO_TRACK_LAGGING = 700,
93    // 8xx
94    // Bad interleaving means that a media has been improperly interleaved or not
95    // interleaved at all, e.g has all the video samples first then all the audio
96    // ones. Video is playing but a lot of disk seek may be happening.
97    MEDIA_INFO_BAD_INTERLEAVING = 800,
98    // The media is not seekable (e.g live stream).
99    MEDIA_INFO_NOT_SEEKABLE = 801,
100};
101
102
103
104enum media_player_states {
105    MEDIA_PLAYER_STATE_ERROR        = 0,
106    MEDIA_PLAYER_IDLE               = 1 << 0,
107    MEDIA_PLAYER_INITIALIZED        = 1 << 1,
108    MEDIA_PLAYER_PREPARING          = 1 << 2,
109    MEDIA_PLAYER_PREPARED           = 1 << 3,
110    MEDIA_PLAYER_STARTED            = 1 << 4,
111    MEDIA_PLAYER_PAUSED             = 1 << 5,
112    MEDIA_PLAYER_STOPPED            = 1 << 6,
113    MEDIA_PLAYER_PLAYBACK_COMPLETE  = 1 << 7
114};
115
116// ----------------------------------------------------------------------------
117// ref-counted object for callbacks
118class MediaPlayerListener: virtual public RefBase
119{
120public:
121    virtual void notify(int msg, int ext1, int ext2) = 0;
122};
123
124class MediaPlayer : public BnMediaPlayerClient
125{
126public:
127    MediaPlayer();
128    ~MediaPlayer();
129            void            onFirstRef();
130            void            disconnect();
131            status_t        setDataSource(const char *url);
132            status_t        setDataSource(int fd, int64_t offset, int64_t length);
133            status_t        setVideoSurface(const sp<Surface>& surface);
134            status_t        setListener(const sp<MediaPlayerListener>& listener);
135            status_t        prepare();
136            status_t        prepareAsync();
137            status_t        start();
138            status_t        stop();
139            status_t        pause();
140            bool            isPlaying();
141            status_t        getVideoWidth(int *w);
142            status_t        getVideoHeight(int *h);
143            status_t        seekTo(int msec);
144            status_t        getCurrentPosition(int *msec);
145            status_t        getDuration(int *msec);
146            status_t        reset();
147            status_t        setAudioStreamType(int type);
148            status_t        setLooping(int loop);
149            bool            isLooping();
150            status_t        setVolume(float leftVolume, float rightVolume);
151            void            notify(int msg, int ext1, int ext2);
152    static  sp<IMemory>     decode(const char* url, uint32_t *pSampleRate, int* pNumChannels, int* pFormat);
153    static  sp<IMemory>     decode(int fd, int64_t offset, int64_t length, uint32_t *pSampleRate, int* pNumChannels, int* pFormat);
154
155private:
156            void            clear_l();
157            status_t        seekTo_l(int msec);
158            status_t        prepareAsync_l();
159            status_t        getDuration_l(int *msec);
160            status_t        setDataSource(const sp<IMediaPlayer>& player);
161
162    static const sp<IMediaPlayerService>& getMediaPlayerService();
163    static void addObitRecipient(const wp<MediaPlayer>& recipient);
164    static void removeObitRecipient(const wp<MediaPlayer>& recipient);
165
166    class DeathNotifier: public IBinder::DeathRecipient
167    {
168    public:
169                DeathNotifier() {}
170        virtual ~DeathNotifier();
171
172        virtual void binderDied(const wp<IBinder>& who);
173    };
174
175    sp<IMediaPlayer>            mPlayer;
176    thread_id_t                 mLockThreadId;
177    Mutex                       mLock;
178    Mutex                       mNotifyLock;
179    Condition                   mSignal;
180    sp<MediaPlayerListener>     mListener;
181    void*                       mCookie;
182    media_player_states         mCurrentState;
183    int                         mDuration;
184    int                         mCurrentPosition;
185    int                         mSeekPosition;
186    bool                        mPrepareSync;
187    status_t                    mPrepareStatus;
188    int                         mStreamType;
189    bool                        mLoop;
190    float                       mLeftVolume;
191    float                       mRightVolume;
192    int                         mVideoWidth;
193    int                         mVideoHeight;
194
195    friend class DeathNotifier;
196
197    static  Mutex                           sServiceLock;
198    static  sp<IMediaPlayerService>         sMediaPlayerService;
199    static  sp<DeathNotifier>               sDeathNotifier;
200    static  SortedVector< wp<MediaPlayer> > sObitRecipients;
201};
202
203}; // namespace android
204
205#endif // ANDROID_MEDIAPLAYER_H
206