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 <binder/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 // New media metadata is available. 101 MEDIA_INFO_METADATA_UPDATE = 802, 102}; 103 104 105 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); 156 static int snoop(short *data, int len, int kind); 157 status_t invoke(const Parcel& request, Parcel *reply); 158 status_t setMetadataFilter(const Parcel& filter); 159 status_t getMetadata(bool update_only, bool apply_filter, Parcel *metadata); 160private: 161 void clear_l(); 162 status_t seekTo_l(int msec); 163 status_t prepareAsync_l(); 164 status_t getDuration_l(int *msec); 165 status_t setDataSource(const sp<IMediaPlayer>& player); 166 167 static const sp<IMediaPlayerService>& getMediaPlayerService(); 168 static void addObitRecipient(const wp<MediaPlayer>& recipient); 169 static void removeObitRecipient(const wp<MediaPlayer>& recipient); 170 171 class DeathNotifier: public IBinder::DeathRecipient 172 { 173 public: 174 DeathNotifier() {} 175 virtual ~DeathNotifier(); 176 177 virtual void binderDied(const wp<IBinder>& who); 178 }; 179 180 sp<IMediaPlayer> mPlayer; 181 thread_id_t mLockThreadId; 182 Mutex mLock; 183 Mutex mNotifyLock; 184 Condition mSignal; 185 sp<MediaPlayerListener> mListener; 186 void* mCookie; 187 media_player_states mCurrentState; 188 int mDuration; 189 int mCurrentPosition; 190 int mSeekPosition; 191 bool mPrepareSync; 192 status_t mPrepareStatus; 193 int mStreamType; 194 bool mLoop; 195 float mLeftVolume; 196 float mRightVolume; 197 int mVideoWidth; 198 int mVideoHeight; 199 200 friend class DeathNotifier; 201 202 static Mutex sServiceLock; 203 static sp<IMediaPlayerService> sMediaPlayerService; 204 static sp<DeathNotifier> sDeathNotifier; 205 static SortedVector< wp<MediaPlayer> > sObitRecipients; 206}; 207 208}; // namespace android 209 210#endif // ANDROID_MEDIAPLAYER_H 211