1 /*
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 <sys/types.h>
23 #include <utils/Errors.h>
24 #include <utils/KeyedVector.h>
25 #include <utils/String8.h>
26 #include <utils/RefBase.h>
27 
28 #include <media/mediaplayer.h>
29 #include <media/AudioContainers.h>
30 #include <media/AudioResamplerPublic.h>
31 #include <media/AudioTimestamp.h>
32 #include <media/AVSyncSettings.h>
33 #include <media/BufferingSettings.h>
34 #include <media/Metadata.h>
35 
36 // Fwd decl to make sure everyone agrees that the scope of struct sockaddr_in is
37 // global, and not in android::
38 struct sockaddr_in;
39 
40 namespace android {
41 
42 class DataSource;
43 class Parcel;
44 class Surface;
45 class IGraphicBufferProducer;
46 
47 template<typename T> class SortedVector;
48 
49 enum player_type {
50     STAGEFRIGHT_PLAYER = 3,
51     NU_PLAYER = 4,
52     // Test players are available only in the 'test' and 'eng' builds.
53     // The shared library with the test player is passed passed as an
54     // argument to the 'test:' url in the setDataSource call.
55     TEST_PLAYER = 5,
56 };
57 
58 
59 #define DEFAULT_AUDIOSINK_BUFFERCOUNT 4
60 #define DEFAULT_AUDIOSINK_BUFFERSIZE 1200
61 #define DEFAULT_AUDIOSINK_SAMPLERATE 44100
62 
63 // when the channel mask isn't known, use the channel count to derive a mask in AudioSink::open()
64 #define CHANNEL_MASK_USE_CHANNEL_ORDER AUDIO_CHANNEL_NONE
65 
66 // duration below which we do not allow deep audio buffering
67 #define AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US 5000000
68 
69 // abstract base class - use MediaPlayerInterface
70 class MediaPlayerBase : public RefBase
71 {
72 public:
73     // callback mechanism for passing messages to MediaPlayer object
74     class Listener : public RefBase {
75     public:
76         virtual void notify(int msg, int ext1, int ext2, const Parcel *obj) = 0;
~Listener()77         virtual ~Listener() {}
78     };
79 
80     // For the AudioCallback, we provide a WeakWrapper class
81     // to wrap a virtual RefBase derived object to pass into the AudioCallback.
82     // This is not used for NuPlayer::Renderer, only for legacy AudioPlayer implementation.
83     template <typename T>
84     class WeakWrapper : public RefBase {
85     public:
WeakWrapper(const sp<T> & object)86         explicit WeakWrapper(const sp<T>& object)
87                 : mObject(object) {}
88 
promote()89         sp<T> promote() const {
90             if (mObject == nullptr) return {};
91             return mObject.promote();
92         }
93 
promoteFromRefBase(const wp<RefBase> & weakWrapper)94         static sp<T> promoteFromRefBase(const wp<RefBase>& weakWrapper) {
95             if (weakWrapper == nullptr) return {};
96             const auto refBase = weakWrapper.promote();
97             if (!refBase) return {};
98             const auto wrapper = sp<WeakWrapper<T>>::fromExisting(
99                     static_cast<WeakWrapper<T>*>(refBase.get()));
100             return wrapper->promote();
101         }
102 
103     private:
104         const wp<T> mObject;
105     };
106 
107     // AudioSink: abstraction layer for audio output
108     class AudioSink : public RefBase {
109     public:
110         enum cb_event_t {
111             CB_EVENT_FILL_BUFFER,   // Request to write more data to buffer.
112             CB_EVENT_STREAM_END,    // Sent after all the buffers queued in AF and HW are played
113                                     // back (after stop is called)
114             CB_EVENT_TEAR_DOWN      // The AudioTrack was invalidated due to use case change:
115                                     // Need to re-evaluate offloading options
116         };
117 
118         // Callback returns the number of bytes actually written to the buffer.
119         typedef size_t (*AudioCallback)(
120                 const sp<AudioSink>& audioSink, void *buffer, size_t size,
121                 const wp<RefBase>& cookie, cb_event_t event);
122 
~AudioSink()123         virtual             ~AudioSink() {}
124         virtual bool        ready() const = 0; // audio output is open and ready
125         virtual ssize_t     bufferSize() const = 0;
126         virtual ssize_t     frameCount() const = 0;
127         virtual ssize_t     channelCount() const = 0;
128         virtual ssize_t     frameSize() const = 0;
129         virtual uint32_t    latency() const = 0;
130         virtual float       msecsPerFrame() const = 0;
131         virtual status_t    getPosition(uint32_t *position) const = 0;
132         virtual status_t    getTimestamp(AudioTimestamp &ts) const = 0;
133         virtual int64_t     getPlayedOutDurationUs(int64_t nowUs) const = 0;
134         virtual status_t    getFramesWritten(uint32_t *frameswritten) const = 0;
135         virtual audio_session_t getSessionId() const = 0;
136         virtual audio_stream_type_t getAudioStreamType() const = 0;
137         virtual uint32_t    getSampleRate() const = 0;
138         virtual int64_t     getBufferDurationInUs() const = 0;
139         virtual audio_output_flags_t getFlags() const = 0;
140 
141         // If no callback is specified, use the "write" API below to submit
142         // audio data.
143         virtual status_t    open(
144                 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
145                 audio_format_t format=AUDIO_FORMAT_PCM_16_BIT,
146                 int bufferCount=DEFAULT_AUDIOSINK_BUFFERCOUNT,
147                 AudioCallback cb = NULL,
148                 const wp<RefBase>& cookie = {},
149                 audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE,
150                 const audio_offload_info_t *offloadInfo = NULL,
151                 bool doNotReconnect = false,
152                 uint32_t suggestedFrameCount = 0) = 0;
153 
154         virtual void        setPlayerIId(int32_t playerIId) = 0;
155 
156         virtual status_t    start() = 0;
157 
158         /* Input parameter |size| is in byte units stored in |buffer|.
159          * Data is copied over and actual number of bytes written (>= 0)
160          * is returned, or no data is copied and a negative status code
161          * is returned (even when |blocking| is true).
162          * When |blocking| is false, AudioSink will immediately return after
163          * part of or full |buffer| is copied over.
164          * When |blocking| is true, AudioSink will wait to copy the entire
165          * buffer, unless an error occurs or the copy operation is
166          * prematurely stopped.
167          */
168         virtual ssize_t     write(const void* buffer, size_t size, bool blocking = true) = 0;
169 
170         virtual void        stop() = 0;
171         virtual void        flush() = 0;
172         virtual void        pause() = 0;
173         virtual void        close() = 0;
174 
175         virtual status_t    setPlaybackRate(const AudioPlaybackRate& rate) = 0;
176         virtual status_t    getPlaybackRate(AudioPlaybackRate* rate /* nonnull */) = 0;
needsTrailingPadding()177         virtual bool        needsTrailingPadding() { return true; }
178 
setParameters(const String8 &)179         virtual status_t    setParameters(const String8& /* keyValuePairs */) { return NO_ERROR; }
getParameters(const String8 &)180         virtual String8     getParameters(const String8& /* keys */) { return String8(); }
181 
182         virtual media::VolumeShaper::Status applyVolumeShaper(
183                                     const sp<media::VolumeShaper::Configuration>& configuration,
184                                     const sp<media::VolumeShaper::Operation>& operation) = 0;
185         virtual sp<media::VolumeShaper::State> getVolumeShaperState(int id) = 0;
186 
187         // AudioRouting
188         virtual status_t    setOutputDevice(audio_port_handle_t deviceId) = 0;
189         virtual status_t    getRoutedDeviceIds(DeviceIdVector& deviceIds) = 0;
190         virtual status_t    enableAudioDeviceCallback(bool enabled) = 0;
191     };
192 
MediaPlayerBase()193                         MediaPlayerBase() {}
~MediaPlayerBase()194     virtual             ~MediaPlayerBase() {}
195     virtual status_t    initCheck() = 0;
196     virtual bool        hardwareOutput() = 0;
197 
setUID(uid_t)198     virtual status_t    setUID(uid_t /* uid */) {
199         return INVALID_OPERATION;
200     }
201 
202     virtual status_t    setDataSource(
203             const sp<IMediaHTTPService> &httpService,
204             const char *url,
205             const KeyedVector<String8, String8> *headers = NULL) = 0;
206 
207     virtual status_t    setDataSource(int fd, int64_t offset, int64_t length) = 0;
208 
setDataSource(const sp<IStreamSource> &)209     virtual status_t    setDataSource(const sp<IStreamSource>& /* source */) {
210         return INVALID_OPERATION;
211     }
212 
setDataSource(const sp<DataSource> &)213     virtual status_t    setDataSource(const sp<DataSource>& /* source */) {
214         return INVALID_OPERATION;
215     }
216 
setDataSource(const String8 &)217     virtual status_t    setDataSource(const String8& /* rtpParams */) {
218         return INVALID_OPERATION;
219     }
220 
221     // pass the buffered IGraphicBufferProducer to the media player service
222     virtual status_t    setVideoSurfaceTexture(
223                                 const sp<IGraphicBufferProducer>& bufferProducer) = 0;
224 
getBufferingSettings(BufferingSettings * buffering)225     virtual status_t    getBufferingSettings(
226                                 BufferingSettings* buffering /* nonnull */) {
227         *buffering = BufferingSettings();
228         return OK;
229     }
setBufferingSettings(const BufferingSettings &)230     virtual status_t    setBufferingSettings(const BufferingSettings& /* buffering */) {
231         return OK;
232     }
233 
234     virtual status_t    prepare() = 0;
235     virtual status_t    prepareAsync() = 0;
236     virtual status_t    start() = 0;
237     virtual status_t    stop() = 0;
238     virtual status_t    pause() = 0;
239     virtual bool        isPlaying() = 0;
setPlaybackSettings(const AudioPlaybackRate & rate)240     virtual status_t    setPlaybackSettings(const AudioPlaybackRate& rate) {
241         // by default, players only support setting rate to the default
242         if (!isAudioPlaybackRateEqual(rate, AUDIO_PLAYBACK_RATE_DEFAULT)) {
243             return BAD_VALUE;
244         }
245         return OK;
246     }
getPlaybackSettings(AudioPlaybackRate * rate)247     virtual status_t    getPlaybackSettings(AudioPlaybackRate* rate /* nonnull */) {
248         *rate = AUDIO_PLAYBACK_RATE_DEFAULT;
249         return OK;
250     }
setSyncSettings(const AVSyncSettings & sync,float)251     virtual status_t    setSyncSettings(const AVSyncSettings& sync, float /* videoFps */) {
252         // By default, players only support setting sync source to default; all other sync
253         // settings are ignored. There is no requirement for getters to return set values.
254         if (sync.mSource != AVSYNC_SOURCE_DEFAULT) {
255             return BAD_VALUE;
256         }
257         return OK;
258     }
getSyncSettings(AVSyncSettings * sync,float * videoFps)259     virtual status_t    getSyncSettings(
260                                 AVSyncSettings* sync /* nonnull */, float* videoFps /* nonnull */) {
261         *sync = AVSyncSettings();
262         *videoFps = -1.f;
263         return OK;
264     }
265     virtual status_t    seekTo(
266             int msec, MediaPlayerSeekMode mode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC) = 0;
267     virtual status_t    getCurrentPosition(int *msec) = 0;
268     virtual status_t    getDuration(int *msec) = 0;
269     virtual status_t    reset() = 0;
notifyAt(int64_t)270     virtual status_t    notifyAt(int64_t /* mediaTimeUs */) {
271         return INVALID_OPERATION;
272     }
273     virtual status_t    setLooping(int loop) = 0;
274     virtual player_type playerType() = 0;
275     virtual status_t    setParameter(int key, const Parcel &request) = 0;
276     virtual status_t    getParameter(int key, Parcel *reply) = 0;
277 
278     // default no-op implementation of optional extensions
setRetransmitEndpoint(const struct sockaddr_in *)279     virtual status_t setRetransmitEndpoint(const struct sockaddr_in* /* endpoint */) {
280         return INVALID_OPERATION;
281     }
getRetransmitEndpoint(struct sockaddr_in *)282     virtual status_t getRetransmitEndpoint(struct sockaddr_in* /* endpoint */) {
283         return INVALID_OPERATION;
284     }
setNextPlayer(const sp<MediaPlayerBase> &)285     virtual status_t setNextPlayer(const sp<MediaPlayerBase>& /* next */) {
286         return OK;
287     }
288 
289     // Invoke a generic method on the player by using opaque parcels
290     // for the request and reply.
291     //
292     // @param request Parcel that is positioned at the start of the
293     //                data sent by the java layer.
294     // @param[out] reply Parcel to hold the reply data. Cannot be null.
295     // @return OK if the call was successful.
296     virtual status_t    invoke(const Parcel& request, Parcel *reply) = 0;
297 
298     // The Client in the MetadataPlayerService calls this method on
299     // the native player to retrieve all or a subset of metadata.
300     //
301     // @param ids SortedList of metadata ID to be fetch. If empty, all
302     //            the known metadata should be returned.
303     // @param[inout] records Parcel where the player appends its metadata.
304     // @return OK if the call was successful.
getMetadata(const media::Metadata::Filter &,Parcel *)305     virtual status_t    getMetadata(const media::Metadata::Filter& /* ids */,
306                                     Parcel* /* records */) {
307         return INVALID_OPERATION;
308     };
309 
setNotifyCallback(const sp<Listener> & listener)310     void        setNotifyCallback(
311             const sp<Listener> &listener) {
312         Mutex::Autolock autoLock(mNotifyLock);
313         mListener = listener;
314     }
315 
316     void        sendEvent(int msg, int ext1=0, int ext2=0,
317                           const Parcel *obj=NULL) {
318         sp<Listener> listener;
319         {
320             Mutex::Autolock autoLock(mNotifyLock);
321             listener = mListener;
322         }
323 
324         if (listener != NULL) {
325             listener->notify(msg, ext1, ext2, obj);
326         }
327     }
328 
dump(int,const Vector<String16> &)329     virtual status_t dump(int /* fd */, const Vector<String16>& /* args */) const {
330         return INVALID_OPERATION;
331     }
332 
333     // Modular DRM
prepareDrm(const uint8_t[16],const Vector<uint8_t> &)334     virtual status_t prepareDrm(const uint8_t /* uuid */[16], const Vector<uint8_t>& /* drmSessionId */) {
335         return INVALID_OPERATION;
336     }
releaseDrm()337     virtual status_t releaseDrm() {
338         return INVALID_OPERATION;
339     }
340 
341 private:
342     friend class MediaPlayerService;
343 
344     Mutex               mNotifyLock;
345     sp<Listener>        mListener;
346 };
347 
348 // Implement this class for media players that use the AudioFlinger software mixer
349 class MediaPlayerInterface : public MediaPlayerBase
350 {
351 public:
~MediaPlayerInterface()352     virtual             ~MediaPlayerInterface() { }
hardwareOutput()353     virtual bool        hardwareOutput() { return false; }
setAudioSink(const sp<AudioSink> & audioSink)354     virtual void        setAudioSink(const sp<AudioSink>& audioSink) { mAudioSink = audioSink; }
355 protected:
356     sp<AudioSink>       mAudioSink;
357 };
358 
359 // Implement this class for media players that output audio directly to hardware
360 class MediaPlayerHWInterface : public MediaPlayerBase
361 {
362 public:
~MediaPlayerHWInterface()363     virtual             ~MediaPlayerHWInterface() {}
hardwareOutput()364     virtual bool        hardwareOutput() { return true; }
365     virtual status_t    setVolume(float leftVolume, float rightVolume) = 0;
366     virtual status_t    setAudioStreamType(audio_stream_type_t streamType) = 0;
367 };
368 
369 }; // namespace android
370 
371 #endif // __cplusplus
372 
373 
374 #endif // ANDROID_MEDIAPLAYERINTERFACE_H
375