blob: d1933f66aa4513de559b07a2782fc2ecf6a804b7 [file] [log] [blame]
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001/*
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
Nicolas Catania1d187f12009-05-12 23:25:55 -070030class Parcel;
31
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080032enum player_type {
33 PV_PLAYER = 1,
34 SONIVOX_PLAYER = 2,
Andreas Huber20111aa2009-07-14 16:56:47 -070035 VORBIS_PLAYER = 3,
36 STAGEFRIGHT_PLAYER = 4
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080037};
38
Nicolas Catania1d187f12009-05-12 23:25:55 -070039
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080040#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:
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080052 // AudioSink: abstraction layer for audio output
53 class AudioSink : public RefBase {
54 public:
Andreas Huber20111aa2009-07-14 16:56:47 -070055 typedef void (*AudioCallback)(
56 AudioSink *audioSink, void *buffer, size_t size, void *cookie);
57
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080058 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;
Andreas Huber20111aa2009-07-14 16:56:47 -070067
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
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080078 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; }
Nicolas Catania1d187f12009-05-12 23:25:55 -0700107 // 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;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800115protected:
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