MediaPlayerInterface.h revision 20111aa043c5f404472bc63b90bc5aad906b1101
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#ifdef __cplusplus 21 22#include <ui/ISurface.h> 23#include <utils/RefBase.h> 24 25#include <media/mediaplayer.h> 26#include <media/AudioSystem.h> 27 28namespace android { 29 30class Parcel; 31 32enum player_type { 33 PV_PLAYER = 1, 34 SONIVOX_PLAYER = 2, 35 VORBIS_PLAYER = 3, 36 STAGEFRIGHT_PLAYER = 4 37}; 38 39 40#define DEFAULT_AUDIOSINK_BUFFERCOUNT 4 41#define DEFAULT_AUDIOSINK_BUFFERSIZE 1200 42#define DEFAULT_AUDIOSINK_SAMPLERATE 44100 43 44 45// callback mechanism for passing messages to MediaPlayer object 46typedef void (*notify_callback_f)(void* cookie, int msg, int ext1, int ext2); 47 48// abstract base class - use MediaPlayerInterface 49class MediaPlayerBase : public RefBase 50{ 51public: 52 // AudioSink: abstraction layer for audio output 53 class AudioSink : public RefBase { 54 public: 55 typedef void (*AudioCallback)( 56 AudioSink *audioSink, void *buffer, size_t size, void *cookie); 57 58 virtual ~AudioSink() {} 59 virtual bool ready() const = 0; // audio output is open and ready 60 virtual bool realtime() const = 0; // audio output is real-time output 61 virtual ssize_t bufferSize() const = 0; 62 virtual ssize_t frameCount() const = 0; 63 virtual ssize_t channelCount() const = 0; 64 virtual ssize_t frameSize() const = 0; 65 virtual uint32_t latency() const = 0; 66 virtual float msecsPerFrame() const = 0; 67 68 // If no callback is specified, use the "write" API below to submit 69 // audio data. Otherwise return a full buffer of audio data on each 70 // callback. 71 virtual status_t open( 72 uint32_t sampleRate, int channelCount, 73 int format=AudioSystem::PCM_16_BIT, 74 int bufferCount=DEFAULT_AUDIOSINK_BUFFERCOUNT, 75 AudioCallback cb = NULL, 76 void *cookie = NULL) = 0; 77 78 virtual void start() = 0; 79 virtual ssize_t write(const void* buffer, size_t size) = 0; 80 virtual void stop() = 0; 81 virtual void flush() = 0; 82 virtual void pause() = 0; 83 virtual void close() = 0; 84 }; 85 86 MediaPlayerBase() : mCookie(0), mNotify(0) {} 87 virtual ~MediaPlayerBase() {} 88 virtual status_t initCheck() = 0; 89 virtual bool hardwareOutput() = 0; 90 virtual status_t setDataSource(const char *url) = 0; 91 virtual status_t setDataSource(int fd, int64_t offset, int64_t length) = 0; 92 virtual status_t setVideoSurface(const sp<ISurface>& surface) = 0; 93 virtual status_t prepare() = 0; 94 virtual status_t prepareAsync() = 0; 95 virtual status_t start() = 0; 96 virtual status_t stop() = 0; 97 virtual status_t pause() = 0; 98 virtual bool isPlaying() = 0; 99 virtual status_t seekTo(int msec) = 0; 100 virtual status_t getCurrentPosition(int *msec) = 0; 101 virtual status_t getDuration(int *msec) = 0; 102 virtual status_t reset() = 0; 103 virtual status_t setLooping(int loop) = 0; 104 virtual player_type playerType() = 0; 105 virtual void setNotifyCallback(void* cookie, notify_callback_f notifyFunc) { 106 mCookie = cookie; mNotify = notifyFunc; } 107 // Invoke a generic method on the player by using opaque parcels 108 // for the request and reply. 109 // @param request Parcel that is positioned at the start of the 110 // data sent by the java layer. 111 // @param[out] reply Parcel to hold the reply data. Cannot be null. 112 // @return OK if the invocation was made successfully. A player 113 // not supporting the direct API should return INVALID_OPERATION. 114 virtual status_t invoke(const Parcel& request, Parcel *reply) = 0; 115protected: 116 virtual void sendEvent(int msg, int ext1=0, int ext2=0) { if (mNotify) mNotify(mCookie, msg, ext1, ext2); } 117 118 void* mCookie; 119 notify_callback_f mNotify; 120}; 121 122// Implement this class for media players that use the AudioFlinger software mixer 123class MediaPlayerInterface : public MediaPlayerBase 124{ 125public: 126 virtual ~MediaPlayerInterface() { } 127 virtual bool hardwareOutput() { return false; } 128 virtual void setAudioSink(const sp<AudioSink>& audioSink) { mAudioSink = audioSink; } 129protected: 130 sp<AudioSink> mAudioSink; 131}; 132 133// Implement this class for media players that output directo to hardware 134class MediaPlayerHWInterface : public MediaPlayerBase 135{ 136public: 137 virtual ~MediaPlayerHWInterface() {} 138 virtual bool hardwareOutput() { return true; } 139 virtual status_t setVolume(float leftVolume, float rightVolume) = 0; 140 virtual status_t setAudioStreamType(int streamType) = 0; 141}; 142 143}; // namespace android 144 145#endif // __cplusplus 146 147 148#endif // ANDROID_MEDIAPLAYERINTERFACE_H 149