blob: c0c490bb3e2d9b07d411256ff676cc4b82951ff2 [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,
Wei Jiad2bb1bd2018-02-08 09:47:37 -080067 int64_t srcId, int msg, int ext1, int ext2, const Parcel *obj);
Pawin Vongmasa50963852017-12-12 06:24:42 -080068
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 Jia57aeffd2018-02-15 16:01:14 -0800153 virtual status_t setDataSource(const sp<DataSourceDesc> &dsd) = 0;
154
155 virtual status_t prepareNextDataSource(const sp<DataSourceDesc> &dsd) = 0;
156
157 virtual status_t playNextDataSource(int64_t srcId) = 0;
Wei Jia53692fa2017-12-11 10:33:46 -0800158
Wei Jia28288fb2017-12-15 13:45:29 -0800159 // pass the buffered native window to the media player service
160 virtual status_t setVideoSurfaceTexture(const sp<ANativeWindowWrapper>& nww) = 0;
Wei Jia53692fa2017-12-11 10:33:46 -0800161
162 virtual status_t getBufferingSettings(
163 BufferingSettings* buffering /* nonnull */) {
164 *buffering = BufferingSettings();
165 return OK;
166 }
167 virtual status_t setBufferingSettings(const BufferingSettings& /* buffering */) {
168 return OK;
169 }
170
Wei Jia53692fa2017-12-11 10:33:46 -0800171 virtual status_t prepareAsync() = 0;
172 virtual status_t start() = 0;
173 virtual status_t stop() = 0;
174 virtual status_t pause() = 0;
175 virtual bool isPlaying() = 0;
176 virtual status_t setPlaybackSettings(const AudioPlaybackRate& rate) {
177 // by default, players only support setting rate to the default
178 if (!isAudioPlaybackRateEqual(rate, AUDIO_PLAYBACK_RATE_DEFAULT)) {
179 return BAD_VALUE;
180 }
181 return OK;
182 }
183 virtual status_t getPlaybackSettings(AudioPlaybackRate* rate /* nonnull */) {
184 *rate = AUDIO_PLAYBACK_RATE_DEFAULT;
185 return OK;
186 }
187 virtual status_t setSyncSettings(const AVSyncSettings& sync, float /* videoFps */) {
188 // By default, players only support setting sync source to default; all other sync
189 // settings are ignored. There is no requirement for getters to return set values.
190 if (sync.mSource != AVSYNC_SOURCE_DEFAULT) {
191 return BAD_VALUE;
192 }
193 return OK;
194 }
195 virtual status_t getSyncSettings(
196 AVSyncSettings* sync /* nonnull */, float* videoFps /* nonnull */) {
197 *sync = AVSyncSettings();
198 *videoFps = -1.f;
199 return OK;
200 }
201 virtual status_t seekTo(
202 int msec, MediaPlayer2SeekMode mode = MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC) = 0;
203 virtual status_t getCurrentPosition(int *msec) = 0;
204 virtual status_t getDuration(int *msec) = 0;
205 virtual status_t reset() = 0;
206 virtual status_t notifyAt(int64_t /* mediaTimeUs */) {
207 return INVALID_OPERATION;
208 }
209 virtual status_t setLooping(int loop) = 0;
Wei Jia53692fa2017-12-11 10:33:46 -0800210 virtual status_t setParameter(int key, const Parcel &request) = 0;
211 virtual status_t getParameter(int key, Parcel *reply) = 0;
212
Wei Jia33abcc72018-01-30 09:47:38 -0800213 virtual status_t setNextPlayer(const sp<MediaPlayer2Interface>& /* next */) {
Wei Jia53692fa2017-12-11 10:33:46 -0800214 return OK;
215 }
216
217 // Invoke a generic method on the player by using opaque parcels
218 // for the request and reply.
219 //
220 // @param request Parcel that is positioned at the start of the
221 // data sent by the java layer.
222 // @param[out] reply Parcel to hold the reply data. Cannot be null.
223 // @return OK if the call was successful.
224 virtual status_t invoke(const Parcel& request, Parcel *reply) = 0;
225
226 // The Client in the MetadataPlayerService calls this method on
227 // the native player to retrieve all or a subset of metadata.
228 //
229 // @param ids SortedList of metadata ID to be fetch. If empty, all
230 // the known metadata should be returned.
231 // @param[inout] records Parcel where the player appends its metadata.
232 // @return OK if the call was successful.
233 virtual status_t getMetadata(const media::Metadata::Filter& /* ids */,
234 Parcel* /* records */) {
235 return INVALID_OPERATION;
236 };
237
238 void setNotifyCallback(
Pawin Vongmasa50963852017-12-12 06:24:42 -0800239 const wp<MediaPlayer2Engine> &client, NotifyCallback notifyFunc) {
Wei Jia53692fa2017-12-11 10:33:46 -0800240 Mutex::Autolock autoLock(mNotifyLock);
Pawin Vongmasa50963852017-12-12 06:24:42 -0800241 mClient = client; mNotify = notifyFunc;
Wei Jia53692fa2017-12-11 10:33:46 -0800242 }
243
Wei Jiad2bb1bd2018-02-08 09:47:37 -0800244 void sendEvent(int64_t srcId, int msg, int ext1=0, int ext2=0,
Wei Jia53692fa2017-12-11 10:33:46 -0800245 const Parcel *obj=NULL) {
Pawin Vongmasa50963852017-12-12 06:24:42 -0800246 NotifyCallback notifyCB;
247 wp<MediaPlayer2Engine> client;
Wei Jia53692fa2017-12-11 10:33:46 -0800248 {
249 Mutex::Autolock autoLock(mNotifyLock);
250 notifyCB = mNotify;
Pawin Vongmasa50963852017-12-12 06:24:42 -0800251 client = mClient;
Wei Jia53692fa2017-12-11 10:33:46 -0800252 }
253
Wei Jiad2bb1bd2018-02-08 09:47:37 -0800254 if (notifyCB) notifyCB(client, srcId, msg, ext1, ext2, obj);
Wei Jia53692fa2017-12-11 10:33:46 -0800255 }
256
257 virtual status_t dump(int /* fd */, const Vector<String16>& /* args */) const {
258 return INVALID_OPERATION;
259 }
260
Wei Jia12b9f4a2017-12-13 15:24:13 -0800261 virtual void onMessageReceived(const sp<AMessage> & /* msg */) override { }
262
Wei Jia53692fa2017-12-11 10:33:46 -0800263 // Modular DRM
264 virtual status_t prepareDrm(const uint8_t /* uuid */[16], const Vector<uint8_t>& /* drmSessionId */) {
265 return INVALID_OPERATION;
266 }
267 virtual status_t releaseDrm() {
268 return INVALID_OPERATION;
269 }
270
Wei Jia33abcc72018-01-30 09:47:38 -0800271protected:
272 sp<AudioSink> mAudioSink;
273
Wei Jia53692fa2017-12-11 10:33:46 -0800274private:
275 friend class MediaPlayer2Manager;
276
Pawin Vongmasa50963852017-12-12 06:24:42 -0800277 Mutex mNotifyLock;
278 wp<MediaPlayer2Engine> mClient;
279 NotifyCallback mNotify;
Wei Jia53692fa2017-12-11 10:33:46 -0800280};
281
Wei Jia53692fa2017-12-11 10:33:46 -0800282}; // namespace android
283
284#endif // __cplusplus
285
286
287#endif // ANDROID_MEDIAPLAYER2INTERFACE_H