blob: 1de43486488ce4781796f4b3fca801034008197e [file] [log] [blame]
Wei Jia53692fa2017-12-11 10:33:46 -08001/*
2 * Copyright 2017 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_MEDIAPLAYER2INTERFACE_H
18#define ANDROID_MEDIAPLAYER2INTERFACE_H
19
20#ifdef __cplusplus
21
22#include <sys/types.h>
23#include <utils/Errors.h>
24#include <utils/KeyedVector.h>
25#include <utils/String8.h>
26#include <utils/RefBase.h>
27
Wei Jia53692fa2017-12-11 10:33:46 -080028#include <media/AudioResamplerPublic.h>
29#include <media/AudioSystem.h>
30#include <media/AudioTimestamp.h>
31#include <media/AVSyncSettings.h>
32#include <media/BufferingSettings.h>
33#include <media/Metadata.h>
Wei Jia12b9f4a2017-12-13 15:24:13 -080034#include <media/stagefright/foundation/AHandler.h>
Wei Jia51b69562018-02-05 16:17:13 -080035#include <mediaplayer2/mediaplayer2.h>
Wei Jia53692fa2017-12-11 10:33:46 -080036
37// Fwd decl to make sure everyone agrees that the scope of struct sockaddr_in is
38// global, and not in android::
39struct sockaddr_in;
40
41namespace android {
42
43class DataSource;
Wei Jiac2636032018-02-01 09:15:25 -080044struct DataSourceDesc;
Wei Jia53692fa2017-12-11 10:33:46 -080045struct MediaHTTPService;
46class Parcel;
Wei Jia28288fb2017-12-15 13:45:29 -080047struct ANativeWindowWrapper;
Wei Jia53692fa2017-12-11 10:33:46 -080048
49template<typename T> class SortedVector;
50
Wei Jia53692fa2017-12-11 10:33:46 -080051#define DEFAULT_AUDIOSINK_BUFFERCOUNT 4
52#define DEFAULT_AUDIOSINK_BUFFERSIZE 1200
53#define DEFAULT_AUDIOSINK_SAMPLERATE 44100
54
55// when the channel mask isn't known, use the channel count to derive a mask in AudioSink::open()
56#define CHANNEL_MASK_USE_CHANNEL_ORDER 0
57
58// duration below which we do not allow deep audio buffering
59#define AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US 5000000
60
Wei Jia53692fa2017-12-11 10:33:46 -080061// abstract base class - use MediaPlayer2Interface
Wei Jia33abcc72018-01-30 09:47:38 -080062class MediaPlayer2Interface : public AHandler
Wei Jia53692fa2017-12-11 10:33:46 -080063{
64public:
Pawin Vongmasa50963852017-12-12 06:24:42 -080065 // callback mechanism for passing messages to MediaPlayer2 object
66 typedef void (*NotifyCallback)(const wp<MediaPlayer2Engine> &listener,
67 int msg, int ext1, int ext2, const Parcel *obj);
68
Wei Jia53692fa2017-12-11 10:33:46 -080069 // AudioSink: abstraction layer for audio output
70 class AudioSink : public RefBase {
71 public:
72 enum cb_event_t {
73 CB_EVENT_FILL_BUFFER, // Request to write more data to buffer.
74 CB_EVENT_STREAM_END, // Sent after all the buffers queued in AF and HW are played
75 // back (after stop is called)
76 CB_EVENT_TEAR_DOWN // The AudioTrack was invalidated due to use case change:
77 // Need to re-evaluate offloading options
78 };
79
80 // Callback returns the number of bytes actually written to the buffer.
81 typedef size_t (*AudioCallback)(
82 AudioSink *audioSink, void *buffer, size_t size, void *cookie,
83 cb_event_t event);
84
85 virtual ~AudioSink() {}
86 virtual bool ready() const = 0; // audio output is open and ready
87 virtual ssize_t bufferSize() const = 0;
88 virtual ssize_t frameCount() const = 0;
89 virtual ssize_t channelCount() const = 0;
90 virtual ssize_t frameSize() const = 0;
91 virtual uint32_t latency() const = 0;
92 virtual float msecsPerFrame() const = 0;
93 virtual status_t getPosition(uint32_t *position) const = 0;
94 virtual status_t getTimestamp(AudioTimestamp &ts) const = 0;
95 virtual int64_t getPlayedOutDurationUs(int64_t nowUs) const = 0;
96 virtual status_t getFramesWritten(uint32_t *frameswritten) const = 0;
97 virtual audio_session_t getSessionId() const = 0;
98 virtual audio_stream_type_t getAudioStreamType() const = 0;
99 virtual uint32_t getSampleRate() const = 0;
100 virtual int64_t getBufferDurationInUs() const = 0;
101
102 // If no callback is specified, use the "write" API below to submit
103 // audio data.
104 virtual status_t open(
105 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
106 audio_format_t format=AUDIO_FORMAT_PCM_16_BIT,
107 int bufferCount=DEFAULT_AUDIOSINK_BUFFERCOUNT,
108 AudioCallback cb = NULL,
109 void *cookie = NULL,
110 audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE,
111 const audio_offload_info_t *offloadInfo = NULL,
112 bool doNotReconnect = false,
113 uint32_t suggestedFrameCount = 0) = 0;
114
115 virtual status_t start() = 0;
116
117 /* Input parameter |size| is in byte units stored in |buffer|.
118 * Data is copied over and actual number of bytes written (>= 0)
119 * is returned, or no data is copied and a negative status code
120 * is returned (even when |blocking| is true).
121 * When |blocking| is false, AudioSink will immediately return after
122 * part of or full |buffer| is copied over.
123 * When |blocking| is true, AudioSink will wait to copy the entire
124 * buffer, unless an error occurs or the copy operation is
125 * prematurely stopped.
126 */
127 virtual ssize_t write(const void* buffer, size_t size, bool blocking = true) = 0;
128
129 virtual void stop() = 0;
130 virtual void flush() = 0;
131 virtual void pause() = 0;
132 virtual void close() = 0;
133
134 virtual status_t setPlaybackRate(const AudioPlaybackRate& rate) = 0;
135 virtual status_t getPlaybackRate(AudioPlaybackRate* rate /* nonnull */) = 0;
136 virtual bool needsTrailingPadding() { return true; }
137
138 virtual status_t setParameters(const String8& /* keyValuePairs */) { return NO_ERROR; }
139 virtual String8 getParameters(const String8& /* keys */) { return String8::empty(); }
140
Wei Jia53692fa2017-12-11 10:33:46 -0800141 // AudioRouting
142 virtual status_t setOutputDevice(audio_port_handle_t deviceId);
143 virtual status_t getRoutedDeviceId(audio_port_handle_t* deviceId);
144 virtual status_t enableAudioDeviceCallback(bool enabled);
145 };
146
Wei Jia33abcc72018-01-30 09:47:38 -0800147 MediaPlayer2Interface() : mClient(0), mNotify(0) { }
148 virtual ~MediaPlayer2Interface() { }
Wei Jia53692fa2017-12-11 10:33:46 -0800149 virtual status_t initCheck() = 0;
Wei Jia53692fa2017-12-11 10:33:46 -0800150
Wei Jia33abcc72018-01-30 09:47:38 -0800151 virtual void setAudioSink(const sp<AudioSink>& audioSink) { mAudioSink = audioSink; }
152
Wei Jiac2636032018-02-01 09:15:25 -0800153 virtual status_t setDataSource(const sp<DataSourceDesc>& /* dsd */) {
Wei Jia53692fa2017-12-11 10:33:46 -0800154 return INVALID_OPERATION;
155 }
156
Wei Jia28288fb2017-12-15 13:45:29 -0800157 // pass the buffered native window to the media player service
158 virtual status_t setVideoSurfaceTexture(const sp<ANativeWindowWrapper>& nww) = 0;
Wei Jia53692fa2017-12-11 10:33:46 -0800159
160 virtual status_t getBufferingSettings(
161 BufferingSettings* buffering /* nonnull */) {
162 *buffering = BufferingSettings();
163 return OK;
164 }
165 virtual status_t setBufferingSettings(const BufferingSettings& /* buffering */) {
166 return OK;
167 }
168
169 virtual status_t prepare() = 0;
170 virtual status_t prepareAsync() = 0;
171 virtual status_t start() = 0;
172 virtual status_t stop() = 0;
173 virtual status_t pause() = 0;
174 virtual bool isPlaying() = 0;
175 virtual status_t setPlaybackSettings(const AudioPlaybackRate& rate) {
176 // by default, players only support setting rate to the default
177 if (!isAudioPlaybackRateEqual(rate, AUDIO_PLAYBACK_RATE_DEFAULT)) {
178 return BAD_VALUE;
179 }
180 return OK;
181 }
182 virtual status_t getPlaybackSettings(AudioPlaybackRate* rate /* nonnull */) {
183 *rate = AUDIO_PLAYBACK_RATE_DEFAULT;
184 return OK;
185 }
186 virtual status_t setSyncSettings(const AVSyncSettings& sync, float /* videoFps */) {
187 // By default, players only support setting sync source to default; all other sync
188 // settings are ignored. There is no requirement for getters to return set values.
189 if (sync.mSource != AVSYNC_SOURCE_DEFAULT) {
190 return BAD_VALUE;
191 }
192 return OK;
193 }
194 virtual status_t getSyncSettings(
195 AVSyncSettings* sync /* nonnull */, float* videoFps /* nonnull */) {
196 *sync = AVSyncSettings();
197 *videoFps = -1.f;
198 return OK;
199 }
200 virtual status_t seekTo(
201 int msec, MediaPlayer2SeekMode mode = MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC) = 0;
202 virtual status_t getCurrentPosition(int *msec) = 0;
203 virtual status_t getDuration(int *msec) = 0;
204 virtual status_t reset() = 0;
205 virtual status_t notifyAt(int64_t /* mediaTimeUs */) {
206 return INVALID_OPERATION;
207 }
208 virtual status_t setLooping(int loop) = 0;
Wei Jia53692fa2017-12-11 10:33:46 -0800209 virtual status_t setParameter(int key, const Parcel &request) = 0;
210 virtual status_t getParameter(int key, Parcel *reply) = 0;
211
Wei Jia33abcc72018-01-30 09:47:38 -0800212 virtual status_t setNextPlayer(const sp<MediaPlayer2Interface>& /* next */) {
Wei Jia53692fa2017-12-11 10:33:46 -0800213 return OK;
214 }
215
216 // Invoke a generic method on the player by using opaque parcels
217 // for the request and reply.
218 //
219 // @param request Parcel that is positioned at the start of the
220 // data sent by the java layer.
221 // @param[out] reply Parcel to hold the reply data. Cannot be null.
222 // @return OK if the call was successful.
223 virtual status_t invoke(const Parcel& request, Parcel *reply) = 0;
224
225 // The Client in the MetadataPlayerService calls this method on
226 // the native player to retrieve all or a subset of metadata.
227 //
228 // @param ids SortedList of metadata ID to be fetch. If empty, all
229 // the known metadata should be returned.
230 // @param[inout] records Parcel where the player appends its metadata.
231 // @return OK if the call was successful.
232 virtual status_t getMetadata(const media::Metadata::Filter& /* ids */,
233 Parcel* /* records */) {
234 return INVALID_OPERATION;
235 };
236
237 void setNotifyCallback(
Pawin Vongmasa50963852017-12-12 06:24:42 -0800238 const wp<MediaPlayer2Engine> &client, NotifyCallback notifyFunc) {
Wei Jia53692fa2017-12-11 10:33:46 -0800239 Mutex::Autolock autoLock(mNotifyLock);
Pawin Vongmasa50963852017-12-12 06:24:42 -0800240 mClient = client; mNotify = notifyFunc;
Wei Jia53692fa2017-12-11 10:33:46 -0800241 }
242
243 void sendEvent(int msg, int ext1=0, int ext2=0,
244 const Parcel *obj=NULL) {
Pawin Vongmasa50963852017-12-12 06:24:42 -0800245 NotifyCallback notifyCB;
246 wp<MediaPlayer2Engine> client;
Wei Jia53692fa2017-12-11 10:33:46 -0800247 {
248 Mutex::Autolock autoLock(mNotifyLock);
249 notifyCB = mNotify;
Pawin Vongmasa50963852017-12-12 06:24:42 -0800250 client = mClient;
Wei Jia53692fa2017-12-11 10:33:46 -0800251 }
252
Pawin Vongmasa50963852017-12-12 06:24:42 -0800253 if (notifyCB) notifyCB(client, msg, ext1, ext2, obj);
Wei Jia53692fa2017-12-11 10:33:46 -0800254 }
255
256 virtual status_t dump(int /* fd */, const Vector<String16>& /* args */) const {
257 return INVALID_OPERATION;
258 }
259
Wei Jia12b9f4a2017-12-13 15:24:13 -0800260 virtual void onMessageReceived(const sp<AMessage> & /* msg */) override { }
261
Wei Jia53692fa2017-12-11 10:33:46 -0800262 // Modular DRM
263 virtual status_t prepareDrm(const uint8_t /* uuid */[16], const Vector<uint8_t>& /* drmSessionId */) {
264 return INVALID_OPERATION;
265 }
266 virtual status_t releaseDrm() {
267 return INVALID_OPERATION;
268 }
269
Wei Jia33abcc72018-01-30 09:47:38 -0800270protected:
271 sp<AudioSink> mAudioSink;
272
Wei Jia53692fa2017-12-11 10:33:46 -0800273private:
274 friend class MediaPlayer2Manager;
275
Pawin Vongmasa50963852017-12-12 06:24:42 -0800276 Mutex mNotifyLock;
277 wp<MediaPlayer2Engine> mClient;
278 NotifyCallback mNotify;
Wei Jia53692fa2017-12-11 10:33:46 -0800279};
280
Wei Jia53692fa2017-12-11 10:33:46 -0800281}; // namespace android
282
283#endif // __cplusplus
284
285
286#endif // ANDROID_MEDIAPLAYER2INTERFACE_H