1 /* 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/String8.h> 25 #include <utils/RefBase.h> 26 #include <jni.h> 27 28 #include <media/AVSyncSettings.h> 29 #include <media/AudioResamplerPublic.h> 30 #include <media/AudioSystem.h> 31 #include <media/AudioTimestamp.h> 32 #include <media/BufferingSettings.h> 33 #include <media/stagefright/foundation/AHandler.h> 34 #include <mediaplayer2/MediaPlayer2Types.h> 35 36 #include "jni.h" 37 #include "mediaplayer2.pb.h" 38 39 using android::media::MediaPlayer2Proto::PlayerMessage; 40 41 // Fwd decl to make sure everyone agrees that the scope of struct sockaddr_in is 42 // global, and not in android:: 43 struct sockaddr_in; 44 45 namespace android { 46 47 struct DataSourceDesc; 48 class Parcel; 49 struct ANativeWindowWrapper; 50 51 #define DEFAULT_AUDIOSINK_BUFFERSIZE 1200 52 #define DEFAULT_AUDIOSINK_SAMPLERATE 44100 53 54 // when the channel mask isn't known, use the channel count to derive a mask in AudioSink::open() 55 #define CHANNEL_MASK_USE_CHANNEL_ORDER 0 56 57 // duration below which we do not allow deep audio buffering 58 #define AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US 5000000 59 60 class MediaPlayer2InterfaceListener: public RefBase 61 { 62 public: 63 virtual void notify(int64_t srcId, int msg, int ext1, int ext2, 64 const PlayerMessage *obj) = 0; 65 }; 66 67 class MediaPlayer2Interface : public AHandler { 68 public: 69 // 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, cb_event_t event); 83 ~AudioSink()84 virtual ~AudioSink() {} 85 virtual bool ready() const = 0; // audio output is open and ready 86 virtual ssize_t bufferSize() const = 0; 87 virtual ssize_t frameCount() const = 0; 88 virtual ssize_t channelCount() const = 0; 89 virtual ssize_t frameSize() const = 0; 90 virtual uint32_t latency() const = 0; 91 virtual float msecsPerFrame() const = 0; 92 virtual status_t getPosition(uint32_t *position) const = 0; 93 virtual status_t getTimestamp(AudioTimestamp &ts) const = 0; 94 virtual int64_t getPlayedOutDurationUs(int64_t nowUs) const = 0; 95 virtual status_t getFramesWritten(uint32_t *frameswritten) const = 0; 96 virtual int32_t getSessionId() const = 0; 97 virtual audio_stream_type_t getAudioStreamType() const = 0; 98 virtual uint32_t getSampleRate() const = 0; 99 virtual int64_t getBufferDurationInUs() const = 0; 100 101 // If no callback is specified, use the "write" API below to submit 102 // audio data. 103 virtual status_t open( 104 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask, 105 audio_format_t format=AUDIO_FORMAT_PCM_16_BIT, 106 AudioCallback cb = NULL, 107 void *cookie = NULL, 108 audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE, 109 const audio_offload_info_t *offloadInfo = NULL, 110 uint32_t suggestedFrameCount = 0) = 0; 111 112 virtual status_t start() = 0; 113 114 /* Input parameter |size| is in byte units stored in |buffer|. 115 * Data is copied over and actual number of bytes written (>= 0) 116 * is returned, or no data is copied and a negative status code 117 * is returned (even when |blocking| is true). 118 * When |blocking| is false, AudioSink will immediately return after 119 * part of or full |buffer| is copied over. 120 * When |blocking| is true, AudioSink will wait to copy the entire 121 * buffer, unless an error occurs or the copy operation is 122 * prematurely stopped. 123 */ 124 virtual ssize_t write(const void* buffer, size_t size, bool blocking = true) = 0; 125 126 virtual void stop() = 0; 127 virtual void flush() = 0; 128 virtual void pause() = 0; 129 virtual void close() = 0; 130 131 virtual status_t setPlaybackRate(const AudioPlaybackRate& rate) = 0; 132 virtual status_t getPlaybackRate(AudioPlaybackRate* rate /* nonnull */) = 0; needsTrailingPadding()133 virtual bool needsTrailingPadding() { 134 return true; 135 } 136 setParameters(const String8 &)137 virtual status_t setParameters(const String8& /* keyValuePairs */) { 138 return NO_ERROR; 139 } getParameters(const String8 &)140 virtual String8 getParameters(const String8& /* keys */) { 141 return String8::empty(); 142 } 143 144 // AudioRouting 145 virtual status_t setPreferredDevice(jobject device); 146 virtual jobject getRoutedDevice(); 147 virtual status_t addAudioDeviceCallback(jobject routingDelegate); 148 virtual status_t removeAudioDeviceCallback(jobject listener); 149 }; 150 MediaPlayer2Interface()151 MediaPlayer2Interface() : mListener(NULL) { } ~MediaPlayer2Interface()152 virtual ~MediaPlayer2Interface() { } 153 virtual status_t initCheck() = 0; 154 setAudioSink(const sp<AudioSink> & audioSink)155 virtual void setAudioSink(const sp<AudioSink>& audioSink) { 156 mAudioSink = audioSink; 157 } 158 159 virtual status_t setDataSource(const sp<DataSourceDesc> &dsd) = 0; 160 161 virtual status_t prepareNextDataSource(const sp<DataSourceDesc> &dsd) = 0; 162 163 virtual status_t playNextDataSource(int64_t srcId) = 0; 164 165 // pass the buffered native window to the media player service 166 virtual status_t setVideoSurfaceTexture(const sp<ANativeWindowWrapper>& nww) = 0; 167 getBufferingSettings(BufferingSettings * buffering)168 virtual status_t getBufferingSettings(BufferingSettings* buffering /* nonnull */) { 169 *buffering = BufferingSettings(); 170 return OK; 171 } setBufferingSettings(const BufferingSettings &)172 virtual status_t setBufferingSettings(const BufferingSettings& /* buffering */) { 173 return OK; 174 } 175 176 virtual status_t prepareAsync() = 0; 177 virtual status_t start() = 0; 178 virtual status_t pause() = 0; 179 virtual bool isPlaying() = 0; setPlaybackSettings(const AudioPlaybackRate & rate)180 virtual status_t setPlaybackSettings(const AudioPlaybackRate& rate) { 181 // by default, players only support setting rate to the default 182 if (!isAudioPlaybackRateEqual(rate, AUDIO_PLAYBACK_RATE_DEFAULT)) { 183 return BAD_VALUE; 184 } 185 return OK; 186 } getPlaybackSettings(AudioPlaybackRate * rate)187 virtual status_t getPlaybackSettings(AudioPlaybackRate* rate /* nonnull */) { 188 *rate = AUDIO_PLAYBACK_RATE_DEFAULT; 189 return OK; 190 } setSyncSettings(const AVSyncSettings & sync,float)191 virtual status_t setSyncSettings(const AVSyncSettings& sync, float /* videoFps */) { 192 // By default, players only support setting sync source to default; all other sync 193 // settings are ignored. There is no requirement for getters to return set values. 194 if (sync.mSource != AVSYNC_SOURCE_DEFAULT) { 195 return BAD_VALUE; 196 } 197 return OK; 198 } getSyncSettings(AVSyncSettings * sync,float * videoFps)199 virtual status_t getSyncSettings( 200 AVSyncSettings* sync /* nonnull */, float* videoFps /* nonnull */) { 201 *sync = AVSyncSettings(); 202 *videoFps = -1.f; 203 return OK; 204 } 205 virtual status_t seekTo( 206 int64_t msec, MediaPlayer2SeekMode mode = MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC) = 0; 207 virtual status_t getCurrentPosition(int64_t *msec) = 0; 208 virtual status_t getDuration(int64_t *msec) = 0; 209 virtual status_t reset() = 0; notifyAt(int64_t)210 virtual status_t notifyAt(int64_t /* mediaTimeUs */) { 211 return INVALID_OPERATION; 212 } 213 virtual status_t setLooping(int loop) = 0; 214 virtual status_t setParameter(int key, const Parcel &request) = 0; 215 virtual status_t getParameter(int key, Parcel *reply) = 0; 216 217 virtual status_t getMetrics(char **buffer, size_t *length) = 0; 218 219 // Invoke a generic method on the player by using opaque parcels 220 // for the request and reply. 221 // 222 // @param request Parcel that is positioned at the start of the 223 // data sent by the java layer. 224 // @param[out] reply Parcel to hold the reply data. Cannot be null. 225 // @return OK if the call was successful. 226 virtual status_t invoke(const PlayerMessage &request, PlayerMessage *reply) = 0; 227 setListener(const sp<MediaPlayer2InterfaceListener> & listener)228 void setListener(const sp<MediaPlayer2InterfaceListener> &listener) { 229 Mutex::Autolock autoLock(mListenerLock); 230 mListener = listener; 231 } 232 233 void sendEvent(int64_t srcId, int msg, int ext1=0, int ext2=0, const PlayerMessage *obj=NULL) { 234 sp<MediaPlayer2InterfaceListener> listener; 235 { 236 Mutex::Autolock autoLock(mListenerLock); 237 listener = mListener; 238 } 239 240 if (listener) { 241 listener->notify(srcId, msg, ext1, ext2, obj); 242 } 243 } 244 dump(int,const Vector<String16> &)245 virtual status_t dump(int /* fd */, const Vector<String16>& /* args */) const { 246 return INVALID_OPERATION; 247 } 248 onMessageReceived(const sp<AMessage> &)249 virtual void onMessageReceived(const sp<AMessage> & /* msg */) override { } 250 251 // Modular DRM prepareDrm(int64_t,const uint8_t[16],const Vector<uint8_t> &)252 virtual status_t prepareDrm(int64_t /*srcId*/, const uint8_t /* uuid */[16], 253 const Vector<uint8_t>& /* drmSessionId */) { 254 return INVALID_OPERATION; 255 } releaseDrm(int64_t)256 virtual status_t releaseDrm(int64_t /*srcId*/) { 257 return INVALID_OPERATION; 258 } 259 260 protected: 261 sp<AudioSink> mAudioSink; 262 263 private: 264 Mutex mListenerLock; 265 sp<MediaPlayer2InterfaceListener> mListener; 266 }; 267 268 }; // namespace android 269 270 #endif // __cplusplus 271 272 273 #endif // ANDROID_MEDIAPLAYER2INTERFACE_H 274