The Android Open Source Project | 2729ea9 | 2008-10-21 07:00:00 -0700 | [diff] [blame^] | 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 <ui/Surface.h> |
| 21 | #include <media/AudioTrack.h> |
| 22 | #include <media/IMediaPlayerClient.h> |
| 23 | #include <media/IMediaPlayer.h> |
| 24 | #include <media/IMediaPlayerService.h> |
| 25 | |
| 26 | namespace android { |
| 27 | |
| 28 | enum media_event_type { |
| 29 | MEDIA_NOP = 0, // interface test message |
| 30 | MEDIA_PREPARED = 1, |
| 31 | MEDIA_PLAYBACK_COMPLETE = 2, |
| 32 | MEDIA_BUFFERING_UPDATE = 3, |
| 33 | MEDIA_SEEK_COMPLETE = 4, |
| 34 | MEDIA_ERROR = 100, |
| 35 | }; |
| 36 | |
| 37 | typedef int media_error_type; |
| 38 | const media_error_type MEDIA_ERROR_UNKNOWN = 1; |
| 39 | const media_error_type MEDIA_ERROR_SERVER_DIED = 100; |
| 40 | |
| 41 | enum media_player_states { |
| 42 | MEDIA_PLAYER_STATE_ERROR = 0, |
| 43 | MEDIA_PLAYER_IDLE = 1 << 0, |
| 44 | MEDIA_PLAYER_INITIALIZED = 1 << 1, |
| 45 | MEDIA_PLAYER_PREPARING = 1 << 2, |
| 46 | MEDIA_PLAYER_PREPARED = 1 << 3, |
| 47 | MEDIA_PLAYER_STARTED = 1 << 4, |
| 48 | MEDIA_PLAYER_PAUSED = 1 << 5, |
| 49 | MEDIA_PLAYER_STOPPED = 1 << 6, |
| 50 | MEDIA_PLAYER_PLAYBACK_COMPLETE = 1 << 7 |
| 51 | }; |
| 52 | |
| 53 | // ---------------------------------------------------------------------------- |
| 54 | // ref-counted object for callbacks |
| 55 | class MediaPlayerListener: public RefBase |
| 56 | { |
| 57 | public: |
| 58 | virtual void notify(int msg, int ext1, int ext2) = 0; |
| 59 | }; |
| 60 | |
| 61 | class MediaPlayer : public BnMediaPlayerClient, public IBinder::DeathRecipient |
| 62 | { |
| 63 | public: |
| 64 | MediaPlayer(); |
| 65 | ~MediaPlayer(); |
| 66 | |
| 67 | void disconnect(); |
| 68 | status_t setDataSource(const char *url); |
| 69 | status_t setDataSource(int fd, int64_t offset, int64_t length); |
| 70 | status_t setVideoSurface(const sp<Surface>& surface); |
| 71 | status_t setListener(const sp<MediaPlayerListener>& listener); |
| 72 | status_t prepare(); |
| 73 | status_t prepareAsync(); |
| 74 | status_t start(); |
| 75 | status_t stop(); |
| 76 | status_t pause(); |
| 77 | bool isPlaying(); |
| 78 | status_t getVideoWidth(int *w); |
| 79 | status_t getVideoHeight(int *h); |
| 80 | status_t seekTo(int msec); |
| 81 | status_t getCurrentPosition(int *msec); |
| 82 | status_t getDuration(int *msec); |
| 83 | status_t reset(); |
| 84 | status_t setAudioStreamType(int type); |
| 85 | status_t setLooping(int loop); |
| 86 | status_t setVolume(float leftVolume, float rightVolume); |
| 87 | void notify(int msg, int ext1, int ext2); |
| 88 | static sp<IMemory> decode(const char* url, uint32_t *pSampleRate, int* pNumChannels); |
| 89 | static sp<IMemory> decode(int fd, int64_t offset, int64_t length, uint32_t *pSampleRate, int* pNumChannels); |
| 90 | |
| 91 | private: |
| 92 | void clear_l(); |
| 93 | status_t seekTo_l(int msec); |
| 94 | status_t prepareAsync_l(); |
| 95 | status_t getDuration_l(int *msec); |
| 96 | status_t setDataSource(const sp<IMediaPlayer>& player); |
| 97 | |
| 98 | static const sp<IMediaPlayerService>& getMediaPlayerService(); |
| 99 | virtual void binderDied(const wp<IBinder>& who); |
| 100 | |
| 101 | class DeathNotifier: public IBinder::DeathRecipient |
| 102 | { |
| 103 | public: |
| 104 | DeathNotifier() {} |
| 105 | virtual ~DeathNotifier(); |
| 106 | |
| 107 | virtual void binderDied(const wp<IBinder>& who); |
| 108 | }; |
| 109 | |
| 110 | static sp<DeathNotifier> mDeathNotifier; |
| 111 | |
| 112 | sp<IMediaPlayer> mPlayer; |
| 113 | Mutex mLock; |
| 114 | Mutex mNotifyLock; |
| 115 | Condition mSignal; |
| 116 | sp<MediaPlayerListener> mListener; |
| 117 | void* mCookie; |
| 118 | media_player_states mCurrentState; |
| 119 | int mDuration; |
| 120 | int mCurrentPosition; |
| 121 | int mSeekPosition; |
| 122 | bool mPrepareSync; |
| 123 | status_t mPrepareStatus; |
| 124 | int mStreamType; |
| 125 | bool mLoop; |
| 126 | float mLeftVolume; |
| 127 | float mRightVolume; |
| 128 | |
| 129 | friend class DeathNotifier; |
| 130 | |
| 131 | static Mutex mServiceLock; |
| 132 | static sp<IMediaPlayerService> mMediaPlayerService; |
| 133 | }; |
| 134 | |
| 135 | }; // namespace android |
| 136 | |
| 137 | #endif // ANDROID_MEDIAPLAYER_H |
| 138 | |