MediaPlayer2AudioOutput.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. **
  3. ** Copyright 2018, The Android Open Source Project
  4. **
  5. ** Licensed under the Apache License, Version 2.0 (the "License");
  6. ** you may not use this file except in compliance with the License.
  7. ** You may obtain a copy of the License at
  8. **
  9. ** http://www.apache.org/licenses/LICENSE-2.0
  10. **
  11. ** Unless required by applicable law or agreed to in writing, software
  12. ** distributed under the License is distributed on an "AS IS" BASIS,
  13. ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. ** See the License for the specific language governing permissions and
  15. ** limitations under the License.
  16. */
  17. #ifndef ANDROID_MEDIAPLAYER2AUDIOOUTPUT_H
  18. #define ANDROID_MEDIAPLAYER2AUDIOOUTPUT_H
  19. #include <mediaplayer2/MediaPlayer2Interface.h>
  20. #include <mediaplayer2/JAudioTrack.h>
  21. #include <mediaplayer2/JObjectHolder.h>
  22. #include <utility>
  23. #include <utils/String16.h>
  24. #include <utils/Vector.h>
  25. #include "jni.h"
  26. namespace android {
  27. class AudioTrack;
  28. class MediaPlayer2AudioOutput : public MediaPlayer2Interface::AudioSink
  29. {
  30. class CallbackData;
  31. public:
  32. MediaPlayer2AudioOutput(int32_t sessionId,
  33. uid_t uid,
  34. int pid,
  35. const jobject attributes);
  36. virtual ~MediaPlayer2AudioOutput();
  37. virtual bool ready() const {
  38. return mJAudioTrack != nullptr;
  39. }
  40. virtual ssize_t bufferSize() const;
  41. virtual ssize_t frameCount() const;
  42. virtual ssize_t channelCount() const;
  43. virtual ssize_t frameSize() const;
  44. virtual uint32_t latency() const;
  45. virtual float msecsPerFrame() const;
  46. virtual status_t getPosition(uint32_t *position) const;
  47. virtual status_t getTimestamp(AudioTimestamp &ts) const;
  48. virtual int64_t getPlayedOutDurationUs(int64_t nowUs) const;
  49. virtual status_t getFramesWritten(uint32_t *frameswritten) const;
  50. virtual int32_t getSessionId() const;
  51. virtual void setSessionId(const int32_t id);
  52. virtual uint32_t getSampleRate() const;
  53. virtual int64_t getBufferDurationInUs() const;
  54. virtual status_t open(
  55. uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
  56. audio_format_t format,
  57. AudioCallback cb, void *cookie,
  58. audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE,
  59. const audio_offload_info_t *offloadInfo = NULL,
  60. uint32_t suggestedFrameCount = 0);
  61. virtual status_t start();
  62. virtual ssize_t write(const void* buffer, size_t size, bool blocking = true);
  63. virtual void stop();
  64. virtual void flush();
  65. virtual void pause();
  66. virtual void close();
  67. void setAudioAttributes(const jobject attributes);
  68. virtual audio_stream_type_t getAudioStreamType() const;
  69. void setVolume(float volume);
  70. virtual status_t setPlaybackRate(const AudioPlaybackRate& rate);
  71. virtual status_t getPlaybackRate(AudioPlaybackRate* rate /* nonnull */);
  72. status_t setAuxEffectSendLevel(float level);
  73. status_t attachAuxEffect(int effectId);
  74. virtual status_t dump(int fd, const Vector<String16>& args) const;
  75. static bool isOnEmulator();
  76. static int getMinBufferCount();
  77. virtual bool needsTrailingPadding() {
  78. return true;
  79. // TODO: return correct value.
  80. //return mNextOutput == NULL;
  81. }
  82. // AudioRouting
  83. virtual status_t setPreferredDevice(jobject device);
  84. virtual jobject getRoutedDevice();
  85. virtual status_t addAudioDeviceCallback(jobject routingDelegate);
  86. virtual status_t removeAudioDeviceCallback(jobject listener);
  87. private:
  88. static void setMinBufferCount();
  89. static void CallbackWrapper(int event, void *me, void *info);
  90. void deleteRecycledTrack_l();
  91. void close_l();
  92. status_t updateTrack_l();
  93. sp<JAudioTrack> mJAudioTrack;
  94. AudioCallback mCallback;
  95. void * mCallbackCookie;
  96. CallbackData * mCallbackData;
  97. sp<JObjectHolder> mAttributes;
  98. float mVolume;
  99. AudioPlaybackRate mPlaybackRate;
  100. uint32_t mSampleRateHz; // sample rate of the content, as set in open()
  101. float mMsecsPerFrame;
  102. size_t mFrameSize;
  103. int32_t mSessionId;
  104. uid_t mUid;
  105. int mPid;
  106. float mSendLevel;
  107. int mAuxEffectId;
  108. audio_output_flags_t mFlags;
  109. sp<JObjectHolder> mPreferredDevice;
  110. mutable Mutex mLock;
  111. // <listener, routingDelegate>
  112. Vector<std::pair<sp<JObjectHolder>, sp<JObjectHolder>>> mRoutingDelegates;
  113. // static variables below not protected by mutex
  114. static bool mIsOnEmulator;
  115. static int mMinBufferCount; // 12 for emulator; otherwise 4
  116. // CallbackData is what is passed to the AudioTrack as the "user" data.
  117. // We need to be able to target this to a different Output on the fly,
  118. // so we can't use the Output itself for this.
  119. class CallbackData {
  120. friend MediaPlayer2AudioOutput;
  121. public:
  122. explicit CallbackData(MediaPlayer2AudioOutput *cookie) {
  123. mData = cookie;
  124. mSwitching = false;
  125. }
  126. MediaPlayer2AudioOutput *getOutput() const {
  127. return mData;
  128. }
  129. void setOutput(MediaPlayer2AudioOutput* newcookie) {
  130. mData = newcookie;
  131. }
  132. // lock/unlock are used by the callback before accessing the payload of this object
  133. void lock() const {
  134. mLock.lock();
  135. }
  136. void unlock() const {
  137. mLock.unlock();
  138. }
  139. // tryBeginTrackSwitch/endTrackSwitch are used when the CallbackData is handed over
  140. // to the next sink.
  141. // tryBeginTrackSwitch() returns true only if it obtains the lock.
  142. bool tryBeginTrackSwitch() {
  143. LOG_ALWAYS_FATAL_IF(mSwitching, "tryBeginTrackSwitch() already called");
  144. if (mLock.tryLock() != OK) {
  145. return false;
  146. }
  147. mSwitching = true;
  148. return true;
  149. }
  150. void endTrackSwitch() {
  151. if (mSwitching) {
  152. mLock.unlock();
  153. }
  154. mSwitching = false;
  155. }
  156. private:
  157. MediaPlayer2AudioOutput *mData;
  158. mutable Mutex mLock; // a recursive mutex might make this unnecessary.
  159. bool mSwitching;
  160. DISALLOW_EVIL_CONSTRUCTORS(CallbackData);
  161. };
  162. };
  163. }; // namespace android
  164. #endif // ANDROID_MEDIAPLAYER2AUDIOOUTPUT_H