blob: f723cfdc8e860b02a6e1c6384341cc73d3b12cf1 [file] [log] [blame]
The Android Open Source Project9066cfe2009-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
Nicolas Catania8f5fcab2009-07-13 14:37:49 -070022#include <sys/types.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023#include <ui/ISurface.h>
24#include <utils/RefBase.h>
Nicolas Catania8f5fcab2009-07-13 14:37:49 -070025#include <utils/Errors.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026
27#include <media/mediaplayer.h>
28#include <media/AudioSystem.h>
nikobc726922009-07-20 15:07:26 -070029#include <media/Metadata.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030
31namespace android {
32
Nicolas Catania20cb94e2009-05-12 23:25:55 -070033class Parcel;
niko89948372009-07-16 16:39:53 -070034template<typename T> class SortedVector;
Nicolas Catania20cb94e2009-05-12 23:25:55 -070035
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036enum player_type {
37 PV_PLAYER = 1,
38 SONIVOX_PLAYER = 2,
Andreas Hubere46b7be2009-07-14 16:56:47 -070039 VORBIS_PLAYER = 3,
Nicolas Catania8f5fcab2009-07-13 14:37:49 -070040 STAGEFRIGHT_PLAYER = 4,
41 // Test players are available only in the 'test' and 'eng' builds.
42 // The shared library with the test player is passed passed as an
43 // argument to the 'test:' url in the setDataSource call.
44 TEST_PLAYER = 5,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045};
46
Nicolas Catania20cb94e2009-05-12 23:25:55 -070047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048#define DEFAULT_AUDIOSINK_BUFFERCOUNT 4
49#define DEFAULT_AUDIOSINK_BUFFERSIZE 1200
50#define DEFAULT_AUDIOSINK_SAMPLERATE 44100
51
52
53// callback mechanism for passing messages to MediaPlayer object
54typedef void (*notify_callback_f)(void* cookie, int msg, int ext1, int ext2);
55
56// abstract base class - use MediaPlayerInterface
57class MediaPlayerBase : public RefBase
58{
59public:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060 // AudioSink: abstraction layer for audio output
61 class AudioSink : public RefBase {
62 public:
Andreas Hubere46b7be2009-07-14 16:56:47 -070063 typedef void (*AudioCallback)(
64 AudioSink *audioSink, void *buffer, size_t size, void *cookie);
65
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066 virtual ~AudioSink() {}
67 virtual bool ready() const = 0; // audio output is open and ready
68 virtual bool realtime() const = 0; // audio output is real-time output
69 virtual ssize_t bufferSize() const = 0;
70 virtual ssize_t frameCount() const = 0;
71 virtual ssize_t channelCount() const = 0;
72 virtual ssize_t frameSize() const = 0;
73 virtual uint32_t latency() const = 0;
74 virtual float msecsPerFrame() const = 0;
Andreas Hubere46b7be2009-07-14 16:56:47 -070075
76 // If no callback is specified, use the "write" API below to submit
77 // audio data. Otherwise return a full buffer of audio data on each
78 // callback.
79 virtual status_t open(
80 uint32_t sampleRate, int channelCount,
81 int format=AudioSystem::PCM_16_BIT,
82 int bufferCount=DEFAULT_AUDIOSINK_BUFFERCOUNT,
83 AudioCallback cb = NULL,
84 void *cookie = NULL) = 0;
85
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 virtual void start() = 0;
87 virtual ssize_t write(const void* buffer, size_t size) = 0;
88 virtual void stop() = 0;
89 virtual void flush() = 0;
90 virtual void pause() = 0;
91 virtual void close() = 0;
92 };
93
94 MediaPlayerBase() : mCookie(0), mNotify(0) {}
95 virtual ~MediaPlayerBase() {}
96 virtual status_t initCheck() = 0;
97 virtual bool hardwareOutput() = 0;
98 virtual status_t setDataSource(const char *url) = 0;
99 virtual status_t setDataSource(int fd, int64_t offset, int64_t length) = 0;
100 virtual status_t setVideoSurface(const sp<ISurface>& surface) = 0;
101 virtual status_t prepare() = 0;
102 virtual status_t prepareAsync() = 0;
103 virtual status_t start() = 0;
104 virtual status_t stop() = 0;
105 virtual status_t pause() = 0;
106 virtual bool isPlaying() = 0;
107 virtual status_t seekTo(int msec) = 0;
108 virtual status_t getCurrentPosition(int *msec) = 0;
109 virtual status_t getDuration(int *msec) = 0;
110 virtual status_t reset() = 0;
111 virtual status_t setLooping(int loop) = 0;
112 virtual player_type playerType() = 0;
113 virtual void setNotifyCallback(void* cookie, notify_callback_f notifyFunc) {
114 mCookie = cookie; mNotify = notifyFunc; }
Nicolas Catania20cb94e2009-05-12 23:25:55 -0700115 // Invoke a generic method on the player by using opaque parcels
116 // for the request and reply.
niko89948372009-07-16 16:39:53 -0700117 //
Nicolas Catania20cb94e2009-05-12 23:25:55 -0700118 // @param request Parcel that is positioned at the start of the
119 // data sent by the java layer.
120 // @param[out] reply Parcel to hold the reply data. Cannot be null.
niko89948372009-07-16 16:39:53 -0700121 // @return OK if the call was successful.
Nicolas Catania20cb94e2009-05-12 23:25:55 -0700122 virtual status_t invoke(const Parcel& request, Parcel *reply) = 0;
niko89948372009-07-16 16:39:53 -0700123
124 // The Client in the MetadataPlayerService calls this method on
125 // the native player to retrieve all or a subset of metadata.
126 //
127 // @param ids SortedList of metadata ID to be fetch. If empty, all
128 // the known metadata should be returned.
129 // @param[inout] records Parcel where the player appends its metadata.
130 // @return OK if the call was successful.
nikobc726922009-07-20 15:07:26 -0700131 virtual status_t getMetadata(const media::Metadata::Filter& ids,
132 Parcel *records) {
133 return INVALID_OPERATION;
134 };
niko89948372009-07-16 16:39:53 -0700135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136protected:
137 virtual void sendEvent(int msg, int ext1=0, int ext2=0) { if (mNotify) mNotify(mCookie, msg, ext1, ext2); }
138
139 void* mCookie;
140 notify_callback_f mNotify;
141};
142
143// Implement this class for media players that use the AudioFlinger software mixer
144class MediaPlayerInterface : public MediaPlayerBase
145{
146public:
147 virtual ~MediaPlayerInterface() { }
148 virtual bool hardwareOutput() { return false; }
149 virtual void setAudioSink(const sp<AudioSink>& audioSink) { mAudioSink = audioSink; }
150protected:
151 sp<AudioSink> mAudioSink;
152};
153
154// Implement this class for media players that output directo to hardware
155class MediaPlayerHWInterface : public MediaPlayerBase
156{
157public:
158 virtual ~MediaPlayerHWInterface() {}
159 virtual bool hardwareOutput() { return true; }
160 virtual status_t setVolume(float leftVolume, float rightVolume) = 0;
161 virtual status_t setAudioStreamType(int streamType) = 0;
162};
163
164}; // namespace android
165
166#endif // __cplusplus
167
168
169#endif // ANDROID_MEDIAPLAYERINTERFACE_H