Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 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 | |
| 18 | #ifndef ANDROID_MEDIAPLAYER2AUDIOOUTPUT_H |
| 19 | #define ANDROID_MEDIAPLAYER2AUDIOOUTPUT_H |
| 20 | |
| 21 | #include <mediaplayer2/MediaPlayer2Interface.h> |
Dichen Zhang | f872691 | 2018-10-17 13:31:26 -0700 | [diff] [blame] | 22 | #include <mediaplayer2/JAudioTrack.h> |
Dongwon Kang | 0d7042d | 2018-10-12 16:52:14 -0700 | [diff] [blame] | 23 | #include <mediaplayer2/JObjectHolder.h> |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 24 | |
Dichen Zhang | f872691 | 2018-10-17 13:31:26 -0700 | [diff] [blame] | 25 | #include <vector> |
| 26 | #include <utility> |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 27 | #include <utils/String16.h> |
| 28 | #include <utils/Vector.h> |
| 29 | |
Dongwon Kang | 0d7042d | 2018-10-12 16:52:14 -0700 | [diff] [blame] | 30 | #include "jni.h" |
| 31 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 32 | namespace android { |
| 33 | |
| 34 | class AudioTrack; |
| 35 | |
| 36 | class MediaPlayer2AudioOutput : public MediaPlayer2Interface::AudioSink |
| 37 | { |
| 38 | class CallbackData; |
| 39 | |
| 40 | public: |
Dichen Zhang | 2365864 | 2018-11-15 10:26:16 -0800 | [diff] [blame] | 41 | MediaPlayer2AudioOutput(int32_t sessionId, |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 42 | uid_t uid, |
| 43 | int pid, |
Dichen Zhang | a60eaa8 | 2018-11-13 17:14:34 +0000 | [diff] [blame] | 44 | const jobject attributes); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 45 | virtual ~MediaPlayer2AudioOutput(); |
| 46 | |
| 47 | virtual bool ready() const { |
Dichen Zhang | f872691 | 2018-10-17 13:31:26 -0700 | [diff] [blame] | 48 | return mJAudioTrack != nullptr; |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 49 | } |
| 50 | virtual ssize_t bufferSize() const; |
| 51 | virtual ssize_t frameCount() const; |
| 52 | virtual ssize_t channelCount() const; |
| 53 | virtual ssize_t frameSize() const; |
| 54 | virtual uint32_t latency() const; |
| 55 | virtual float msecsPerFrame() const; |
| 56 | virtual status_t getPosition(uint32_t *position) const; |
| 57 | virtual status_t getTimestamp(AudioTimestamp &ts) const; |
| 58 | virtual int64_t getPlayedOutDurationUs(int64_t nowUs) const; |
| 59 | virtual status_t getFramesWritten(uint32_t *frameswritten) const; |
Dichen Zhang | 2365864 | 2018-11-15 10:26:16 -0800 | [diff] [blame] | 60 | virtual int32_t getSessionId() const; |
| 61 | virtual void setSessionId(const int32_t id); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 62 | virtual uint32_t getSampleRate() const; |
| 63 | virtual int64_t getBufferDurationInUs() const; |
| 64 | |
| 65 | virtual status_t open( |
| 66 | uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask, |
Dichen Zhang | 303bd2d | 2018-09-28 12:52:49 -0700 | [diff] [blame] | 67 | audio_format_t format, |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 68 | AudioCallback cb, void *cookie, |
| 69 | audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE, |
| 70 | const audio_offload_info_t *offloadInfo = NULL, |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 71 | uint32_t suggestedFrameCount = 0); |
| 72 | |
| 73 | virtual status_t start(); |
| 74 | virtual ssize_t write(const void* buffer, size_t size, bool blocking = true); |
| 75 | virtual void stop(); |
| 76 | virtual void flush(); |
| 77 | virtual void pause(); |
| 78 | virtual void close(); |
Dichen Zhang | c2465c5 | 2018-11-12 11:56:05 -0800 | [diff] [blame] | 79 | void setAudioAttributes(const jobject attributes); |
Dichen Zhang | f872691 | 2018-10-17 13:31:26 -0700 | [diff] [blame] | 80 | virtual audio_stream_type_t getAudioStreamType() const; |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 81 | |
Dichen Zhang | 7398ca0 | 2018-10-15 10:25:12 -0700 | [diff] [blame] | 82 | void setVolume(float volume); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 83 | virtual status_t setPlaybackRate(const AudioPlaybackRate& rate); |
| 84 | virtual status_t getPlaybackRate(AudioPlaybackRate* rate /* nonnull */); |
| 85 | |
| 86 | status_t setAuxEffectSendLevel(float level); |
| 87 | status_t attachAuxEffect(int effectId); |
| 88 | virtual status_t dump(int fd, const Vector<String16>& args) const; |
| 89 | |
| 90 | static bool isOnEmulator(); |
| 91 | static int getMinBufferCount(); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 92 | virtual bool needsTrailingPadding() { |
| 93 | return true; |
| 94 | // TODO: return correct value. |
| 95 | //return mNextOutput == NULL; |
| 96 | } |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 97 | // AudioRouting |
Dongwon Kang | 0d7042d | 2018-10-12 16:52:14 -0700 | [diff] [blame] | 98 | virtual status_t setPreferredDevice(jobject device); |
| 99 | virtual jobject getRoutedDevice(); |
Dichen Zhang | f872691 | 2018-10-17 13:31:26 -0700 | [diff] [blame] | 100 | virtual status_t addAudioDeviceCallback(jobject routingDelegate); |
| 101 | virtual status_t removeAudioDeviceCallback(jobject listener); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 102 | |
| 103 | private: |
| 104 | static void setMinBufferCount(); |
| 105 | static void CallbackWrapper(int event, void *me, void *info); |
| 106 | void deleteRecycledTrack_l(); |
| 107 | void close_l(); |
| 108 | status_t updateTrack_l(); |
| 109 | |
Dichen Zhang | f872691 | 2018-10-17 13:31:26 -0700 | [diff] [blame] | 110 | sp<JAudioTrack> mJAudioTrack; |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 111 | AudioCallback mCallback; |
| 112 | void * mCallbackCookie; |
| 113 | CallbackData * mCallbackData; |
Dichen Zhang | c2465c5 | 2018-11-12 11:56:05 -0800 | [diff] [blame] | 114 | sp<JObjectHolder> mAttributes; |
Dichen Zhang | 7398ca0 | 2018-10-15 10:25:12 -0700 | [diff] [blame] | 115 | float mVolume; |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 116 | AudioPlaybackRate mPlaybackRate; |
| 117 | uint32_t mSampleRateHz; // sample rate of the content, as set in open() |
| 118 | float mMsecsPerFrame; |
| 119 | size_t mFrameSize; |
Dichen Zhang | 2365864 | 2018-11-15 10:26:16 -0800 | [diff] [blame] | 120 | int32_t mSessionId; |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 121 | uid_t mUid; |
| 122 | int mPid; |
| 123 | float mSendLevel; |
| 124 | int mAuxEffectId; |
| 125 | audio_output_flags_t mFlags; |
Dongwon Kang | 0d7042d | 2018-10-12 16:52:14 -0700 | [diff] [blame] | 126 | sp<JObjectHolder> mPreferredDevice; |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 127 | mutable Mutex mLock; |
Dichen Zhang | f872691 | 2018-10-17 13:31:26 -0700 | [diff] [blame] | 128 | std::vector<std::pair<jobject, jobject>> mRoutingDelegates; // <listener, routingDelegate> |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 129 | |
| 130 | // static variables below not protected by mutex |
| 131 | static bool mIsOnEmulator; |
| 132 | static int mMinBufferCount; // 12 for emulator; otherwise 4 |
| 133 | |
| 134 | // CallbackData is what is passed to the AudioTrack as the "user" data. |
| 135 | // We need to be able to target this to a different Output on the fly, |
| 136 | // so we can't use the Output itself for this. |
| 137 | class CallbackData { |
| 138 | friend MediaPlayer2AudioOutput; |
| 139 | public: |
| 140 | explicit CallbackData(MediaPlayer2AudioOutput *cookie) { |
| 141 | mData = cookie; |
| 142 | mSwitching = false; |
| 143 | } |
| 144 | MediaPlayer2AudioOutput *getOutput() const { |
| 145 | return mData; |
| 146 | } |
| 147 | void setOutput(MediaPlayer2AudioOutput* newcookie) { |
| 148 | mData = newcookie; |
| 149 | } |
| 150 | // lock/unlock are used by the callback before accessing the payload of this object |
| 151 | void lock() const { |
| 152 | mLock.lock(); |
| 153 | } |
| 154 | void unlock() const { |
| 155 | mLock.unlock(); |
| 156 | } |
| 157 | |
| 158 | // tryBeginTrackSwitch/endTrackSwitch are used when the CallbackData is handed over |
| 159 | // to the next sink. |
| 160 | |
| 161 | // tryBeginTrackSwitch() returns true only if it obtains the lock. |
| 162 | bool tryBeginTrackSwitch() { |
| 163 | LOG_ALWAYS_FATAL_IF(mSwitching, "tryBeginTrackSwitch() already called"); |
| 164 | if (mLock.tryLock() != OK) { |
| 165 | return false; |
| 166 | } |
| 167 | mSwitching = true; |
| 168 | return true; |
| 169 | } |
| 170 | void endTrackSwitch() { |
| 171 | if (mSwitching) { |
| 172 | mLock.unlock(); |
| 173 | } |
| 174 | mSwitching = false; |
| 175 | } |
| 176 | |
| 177 | private: |
| 178 | MediaPlayer2AudioOutput *mData; |
| 179 | mutable Mutex mLock; // a recursive mutex might make this unnecessary. |
| 180 | bool mSwitching; |
| 181 | DISALLOW_EVIL_CONSTRUCTORS(CallbackData); |
| 182 | }; |
| 183 | }; |
| 184 | |
| 185 | }; // namespace android |
| 186 | |
| 187 | #endif // ANDROID_MEDIAPLAYER2AUDIOOUTPUT_H |