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_MEDIAPLAYERINTERFACE_H |
| 18 | #define ANDROID_MEDIAPLAYERINTERFACE_H |
| 19 | |
| 20 | #include <pthread.h> |
| 21 | #include <signal.h> |
| 22 | |
| 23 | #ifdef __cplusplus |
| 24 | |
| 25 | #include <ui/ISurface.h> |
| 26 | #include <utils/RefBase.h> |
| 27 | |
| 28 | #include <media/mediaplayer.h> |
| 29 | |
| 30 | namespace android { |
| 31 | |
| 32 | enum player_type { |
| 33 | PV_PLAYER = 1, |
| 34 | SONIVOX_PLAYER = 2, |
| 35 | VORBIS_PLAYER = 3 |
| 36 | }; |
| 37 | |
| 38 | #define DEFAULT_AUDIOSINK_BUFFERCOUNT 4 |
| 39 | |
| 40 | // callback mechanism for passing messages to MediaPlayer object |
| 41 | typedef void (*notify_callback_f)(void* cookie, int msg, int ext1, int ext2); |
| 42 | |
| 43 | // abstract base class - use MediaPlayerInterface |
| 44 | class MediaPlayerBase : public RefBase |
| 45 | { |
| 46 | public: |
| 47 | |
| 48 | // AudioSink: abstraction layer for audio output |
| 49 | class AudioSink : public RefBase { |
| 50 | public: |
| 51 | virtual ~AudioSink() {} |
| 52 | virtual bool ready() const = 0; // audio output is open and ready |
| 53 | virtual bool realtime() const = 0; // audio output is real-time output |
| 54 | virtual ssize_t bufferSize() const = 0; |
| 55 | virtual ssize_t frameCount() const = 0; |
| 56 | virtual ssize_t channelCount() const = 0; |
| 57 | virtual ssize_t frameSize() const = 0; |
| 58 | virtual uint32_t latency() const = 0; |
| 59 | virtual float msecsPerFrame() const = 0; |
| 60 | virtual status_t open(uint32_t sampleRate, int channelCount, int bufferCount=DEFAULT_AUDIOSINK_BUFFERCOUNT) = 0; |
| 61 | virtual void start() = 0; |
| 62 | virtual ssize_t write(const void* buffer, size_t size) = 0; |
| 63 | virtual void stop() = 0; |
| 64 | virtual void flush() = 0; |
| 65 | virtual void pause() = 0; |
| 66 | virtual void close() = 0; |
| 67 | }; |
| 68 | |
| 69 | MediaPlayerBase() : mCookie(0), mNotify(0) {} |
| 70 | virtual ~MediaPlayerBase() {} |
| 71 | virtual status_t initCheck() = 0; |
| 72 | virtual bool hardwareOutput() = 0; |
| 73 | virtual status_t setSigBusHandlerStructTLSKey(pthread_key_t key) { return 0; } |
| 74 | virtual status_t setDataSource(const char *url) = 0; |
| 75 | virtual status_t setDataSource(int fd, int64_t offset, int64_t length) = 0; |
| 76 | virtual status_t setVideoSurface(const sp<ISurface>& surface) = 0; |
| 77 | virtual status_t prepare() = 0; |
| 78 | virtual status_t prepareAsync() = 0; |
| 79 | virtual status_t start() = 0; |
| 80 | virtual status_t stop() = 0; |
| 81 | virtual status_t pause() = 0; |
| 82 | virtual bool isPlaying() = 0; |
| 83 | virtual status_t getVideoWidth(int *w) {return 0;} |
| 84 | virtual status_t getVideoHeight(int *h) {return 0;} |
| 85 | virtual status_t seekTo(int msec) = 0; |
| 86 | virtual status_t getCurrentPosition(int *msec) = 0; |
| 87 | virtual status_t getDuration(int *msec) = 0; |
| 88 | virtual status_t reset() = 0; |
| 89 | virtual status_t setLooping(int loop) = 0; |
| 90 | virtual player_type playerType() = 0; |
| 91 | virtual void setNotifyCallback(void* cookie, notify_callback_f notifyFunc) { |
| 92 | mCookie = cookie; mNotify = notifyFunc; } |
| 93 | |
| 94 | protected: |
| 95 | virtual void sendEvent(int msg, int ext1=0, int ext2=0) { if (mNotify) mNotify(mCookie, msg, ext1, ext2); } |
| 96 | |
| 97 | void* mCookie; |
| 98 | notify_callback_f mNotify; |
| 99 | }; |
| 100 | |
| 101 | // Implement this class for media players that use the AudioFlinger software mixer |
| 102 | class MediaPlayerInterface : public MediaPlayerBase |
| 103 | { |
| 104 | public: |
| 105 | virtual ~MediaPlayerInterface() { } |
| 106 | virtual bool hardwareOutput() { return false; } |
| 107 | virtual void setAudioSink(const sp<AudioSink>& audioSink) { mAudioSink = audioSink; } |
| 108 | protected: |
| 109 | sp<AudioSink> mAudioSink; |
| 110 | }; |
| 111 | |
| 112 | // Implement this class for media players that output directo to hardware |
| 113 | class MediaPlayerHWInterface : public MediaPlayerBase |
| 114 | { |
| 115 | public: |
| 116 | virtual ~MediaPlayerHWInterface() {} |
| 117 | virtual bool hardwareOutput() { return true; } |
| 118 | virtual status_t setVolume(float leftVolume, float rightVolume) = 0; |
| 119 | virtual status_t setAudioStreamType(int streamType) = 0; |
| 120 | }; |
| 121 | |
| 122 | }; // namespace android |
| 123 | |
| 124 | #endif // __cplusplus |
| 125 | |
| 126 | // A thread can set the thread local variable identified by the pthread_key_t |
| 127 | // that was passed to the player using the setSigBusHandlerStructTLSKey() |
| 128 | // method to the address of the following structure. |
| 129 | // If 'handlesigbus' is non-NULL, the function it points to will be called, |
| 130 | // and if it returns 0, the signal will be assumed to have been handled, |
| 131 | // and no other action will be taken. If it returns non-zero, the old SIGBUS |
| 132 | // handler will be called. |
| 133 | // If 'handlesigbus is NULL, then sigbusvar must be non NULL. The system's |
| 134 | // SIGBUS handler will map an accessible page filled with zeroes at the |
| 135 | // location that caused the original fault, set the variable pointed to by |
| 136 | // sigbusvar to a non-zero value, and exit (which causes the operation to |
| 137 | // be retried, which should now succeed). |
| 138 | // If base and len are non zero, which is strongly recommended, they will |
| 139 | // be used as additional constraints on the signal handler. That is, when |
| 140 | // specified, the fault address must be in the range specified by base and |
| 141 | // len in order for handlesigbus() to be called or sigbusvar to be set. |
| 142 | // If the fault address is outside of the range, the old SIGBUS handler |
| 143 | // will be called. |
| 144 | struct mediasigbushandler { |
| 145 | int (*handlesigbus)(siginfo_t *, struct mediasigbushandler *); |
| 146 | int *sigbusvar; |
| 147 | char *base; |
| 148 | int len; |
| 149 | // these next two are free for application use |
| 150 | struct mediasigbushandler *next; |
| 151 | void *data; |
| 152 | }; |
| 153 | |
| 154 | |
| 155 | #endif // ANDROID_MEDIAPLAYERINTERFACE_H |
| 156 | |