blob: 26b054bd4c2468a3ae86a39adb851eb28c3e7e06 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
Mathias Agopian07952722009-05-19 19:08:10 -070020#include <binder/IMemory.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021#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,
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -070037 MEDIA_INFO = 200,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038};
39
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -070040// 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.)
Nicolas Catania32f82772009-06-11 16:33:49 -070085//
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -070086enum 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,
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700100 // New media metadata is available.
101 MEDIA_INFO_METADATA_UPDATE = 802,
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700102};
103
104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105
106enum media_player_states {
107 MEDIA_PLAYER_STATE_ERROR = 0,
108 MEDIA_PLAYER_IDLE = 1 << 0,
109 MEDIA_PLAYER_INITIALIZED = 1 << 1,
110 MEDIA_PLAYER_PREPARING = 1 << 2,
111 MEDIA_PLAYER_PREPARED = 1 << 3,
112 MEDIA_PLAYER_STARTED = 1 << 4,
113 MEDIA_PLAYER_PAUSED = 1 << 5,
114 MEDIA_PLAYER_STOPPED = 1 << 6,
115 MEDIA_PLAYER_PLAYBACK_COMPLETE = 1 << 7
116};
117
118// ----------------------------------------------------------------------------
119// ref-counted object for callbacks
120class MediaPlayerListener: virtual public RefBase
121{
122public:
123 virtual void notify(int msg, int ext1, int ext2) = 0;
124};
125
126class MediaPlayer : public BnMediaPlayerClient
127{
128public:
129 MediaPlayer();
130 ~MediaPlayer();
131 void onFirstRef();
132 void disconnect();
133 status_t setDataSource(const char *url);
134 status_t setDataSource(int fd, int64_t offset, int64_t length);
135 status_t setVideoSurface(const sp<Surface>& surface);
136 status_t setListener(const sp<MediaPlayerListener>& listener);
137 status_t prepare();
138 status_t prepareAsync();
139 status_t start();
140 status_t stop();
141 status_t pause();
142 bool isPlaying();
143 status_t getVideoWidth(int *w);
144 status_t getVideoHeight(int *h);
145 status_t seekTo(int msec);
146 status_t getCurrentPosition(int *msec);
147 status_t getDuration(int *msec);
148 status_t reset();
149 status_t setAudioStreamType(int type);
150 status_t setLooping(int loop);
151 bool isLooping();
152 status_t setVolume(float leftVolume, float rightVolume);
153 void notify(int msg, int ext1, int ext2);
154 static sp<IMemory> decode(const char* url, uint32_t *pSampleRate, int* pNumChannels, int* pFormat);
155 static sp<IMemory> decode(int fd, int64_t offset, int64_t length, uint32_t *pSampleRate, int* pNumChannels, int* pFormat);
Nicolas Catania20cb94e2009-05-12 23:25:55 -0700156 status_t invoke(const Parcel& request, Parcel *reply);
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700157 status_t setMetadataFilter(const Parcel& filter);
Nicolas Catania5d55c712009-07-09 09:21:33 -0700158 status_t getMetadata(bool update_only, bool apply_filter, Parcel *metadata);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159private:
160 void clear_l();
161 status_t seekTo_l(int msec);
162 status_t prepareAsync_l();
163 status_t getDuration_l(int *msec);
164 status_t setDataSource(const sp<IMediaPlayer>& player);
165
166 static const sp<IMediaPlayerService>& getMediaPlayerService();
167 static void addObitRecipient(const wp<MediaPlayer>& recipient);
168 static void removeObitRecipient(const wp<MediaPlayer>& recipient);
169
170 class DeathNotifier: public IBinder::DeathRecipient
171 {
172 public:
173 DeathNotifier() {}
174 virtual ~DeathNotifier();
175
176 virtual void binderDied(const wp<IBinder>& who);
177 };
178
179 sp<IMediaPlayer> mPlayer;
Jason Samsebb020a2009-03-24 18:45:22 -0700180 thread_id_t mLockThreadId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 Mutex mLock;
182 Mutex mNotifyLock;
183 Condition mSignal;
184 sp<MediaPlayerListener> mListener;
185 void* mCookie;
186 media_player_states mCurrentState;
187 int mDuration;
188 int mCurrentPosition;
189 int mSeekPosition;
190 bool mPrepareSync;
191 status_t mPrepareStatus;
192 int mStreamType;
193 bool mLoop;
194 float mLeftVolume;
195 float mRightVolume;
196 int mVideoWidth;
197 int mVideoHeight;
198
199 friend class DeathNotifier;
200
201 static Mutex sServiceLock;
202 static sp<IMediaPlayerService> sMediaPlayerService;
203 static sp<DeathNotifier> sDeathNotifier;
204 static SortedVector< wp<MediaPlayer> > sObitRecipients;
205};
206
207}; // namespace android
208
209#endif // ANDROID_MEDIAPLAYER_H