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