MediaPlayer2Interface.h 10.0 KB

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