MediaPlayerInterface.h 13 KB

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