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