Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 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_MEDIAPLAYER2_H |
| 18 | #define ANDROID_MEDIAPLAYER2_H |
| 19 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 20 | #include <media/AVSyncSettings.h> |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 21 | #include <media/AudioResamplerPublic.h> |
| 22 | #include <media/BufferingSettings.h> |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 23 | #include <media/mediaplayer_common.h> |
| 24 | #include <mediaplayer2/MediaPlayer2Interface.h> |
| 25 | #include <mediaplayer2/MediaPlayer2Types.h> |
Dichen Zhang | c2465c5 | 2018-11-12 11:56:05 -0800 | [diff] [blame] | 26 | #include <mediaplayer2/JObjectHolder.h> |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 27 | |
Dichen Zhang | f872691 | 2018-10-17 13:31:26 -0700 | [diff] [blame] | 28 | #include <jni.h> |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 29 | #include <utils/Errors.h> |
| 30 | #include <utils/Mutex.h> |
| 31 | #include <utils/RefBase.h> |
| 32 | #include <utils/String16.h> |
| 33 | #include <utils/Vector.h> |
| 34 | #include <system/audio-base.h> |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 35 | |
Dongwon Kang | 0d7042d | 2018-10-12 16:52:14 -0700 | [diff] [blame] | 36 | #include "jni.h" |
| 37 | |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 38 | namespace android { |
| 39 | |
Wei Jia | 28288fb | 2017-12-15 13:45:29 -0800 | [diff] [blame] | 40 | struct ANativeWindowWrapper; |
Wei Jia | c263603 | 2018-02-01 09:15:25 -0800 | [diff] [blame] | 41 | struct DataSourceDesc; |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 42 | class MediaPlayer2AudioOutput; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 43 | |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 44 | // ref-counted object for callbacks |
| 45 | class MediaPlayer2Listener: virtual public RefBase |
| 46 | { |
| 47 | public: |
Dongwon Kang | 41929fb | 2018-09-09 08:29:56 -0700 | [diff] [blame] | 48 | virtual void notify(int64_t srcId, int msg, int ext1, int ext2, |
| 49 | const PlayerMessage *obj = NULL) = 0; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 50 | }; |
| 51 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 52 | class MediaPlayer2 : public MediaPlayer2InterfaceListener |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 53 | { |
| 54 | public: |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 55 | ~MediaPlayer2(); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 56 | |
Dichen Zhang | fc9f40f | 2019-01-04 14:15:28 -0800 | [diff] [blame] | 57 | static sp<MediaPlayer2> Create(int32_t sessionId, jobject context); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 58 | static status_t DumpAll(int fd, const Vector<String16>& args); |
| 59 | |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 60 | void disconnect(); |
| 61 | |
Wei Jia | d2bb1bd | 2018-02-08 09:47:37 -0800 | [diff] [blame] | 62 | status_t getSrcId(int64_t *srcId); |
Wei Jia | c263603 | 2018-02-01 09:15:25 -0800 | [diff] [blame] | 63 | status_t setDataSource(const sp<DataSourceDesc> &dsd); |
Wei Jia | 57aeffd | 2018-02-15 16:01:14 -0800 | [diff] [blame] | 64 | status_t prepareNextDataSource(const sp<DataSourceDesc> &dsd); |
| 65 | status_t playNextDataSource(int64_t srcId); |
Wei Jia | 28288fb | 2017-12-15 13:45:29 -0800 | [diff] [blame] | 66 | status_t setVideoSurfaceTexture(const sp<ANativeWindowWrapper>& nww); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 67 | status_t setListener(const sp<MediaPlayer2Listener>& listener); |
| 68 | status_t getBufferingSettings(BufferingSettings* buffering /* nonnull */); |
| 69 | status_t setBufferingSettings(const BufferingSettings& buffering); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 70 | status_t prepareAsync(); |
| 71 | status_t start(); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 72 | status_t pause(); |
| 73 | bool isPlaying(); |
Wei Jia | 1f043e4 | 2018-06-20 16:52:50 -0700 | [diff] [blame] | 74 | mediaplayer2_states getState(); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 75 | status_t setPlaybackSettings(const AudioPlaybackRate& rate); |
| 76 | status_t getPlaybackSettings(AudioPlaybackRate* rate /* nonnull */); |
| 77 | status_t setSyncSettings(const AVSyncSettings& sync, float videoFpsHint); |
| 78 | status_t getSyncSettings( |
| 79 | AVSyncSettings* sync /* nonnull */, |
| 80 | float* videoFps /* nonnull */); |
| 81 | status_t getVideoWidth(int *w); |
| 82 | status_t getVideoHeight(int *h); |
| 83 | status_t seekTo( |
Wei Jia | 800fe37 | 2018-02-20 15:00:45 -0800 | [diff] [blame] | 84 | int64_t msec, |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 85 | MediaPlayer2SeekMode mode = MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC); |
| 86 | status_t notifyAt(int64_t mediaTimeUs); |
Wei Jia | 800fe37 | 2018-02-20 15:00:45 -0800 | [diff] [blame] | 87 | status_t getCurrentPosition(int64_t *msec); |
Wei Jia | 17944af | 2018-12-13 18:13:10 -0800 | [diff] [blame] | 88 | status_t getDuration(int64_t srcId, int64_t *msec); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 89 | status_t reset(); |
| 90 | status_t setAudioStreamType(audio_stream_type_t type); |
| 91 | status_t getAudioStreamType(audio_stream_type_t *type); |
| 92 | status_t setLooping(int loop); |
| 93 | bool isLooping(); |
Dichen Zhang | 7398ca0 | 2018-10-15 10:25:12 -0700 | [diff] [blame] | 94 | status_t setVolume(float volume); |
Wei Jia | d2bb1bd | 2018-02-08 09:47:37 -0800 | [diff] [blame] | 95 | void notify(int64_t srcId, int msg, int ext1, int ext2, |
Dongwon Kang | 41929fb | 2018-09-09 08:29:56 -0700 | [diff] [blame] | 96 | const PlayerMessage *obj = NULL); |
Dongwon Kang | 9f63198 | 2018-07-10 12:34:41 -0700 | [diff] [blame] | 97 | status_t invoke(const PlayerMessage &request, PlayerMessage *reply); |
Dichen Zhang | 2365864 | 2018-11-15 10:26:16 -0800 | [diff] [blame] | 98 | status_t setAudioSessionId(int32_t sessionId); |
| 99 | int32_t getAudioSessionId(); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 100 | status_t setAuxEffectSendLevel(float level); |
| 101 | status_t attachAuxEffect(int effectId); |
Dichen Zhang | c2465c5 | 2018-11-12 11:56:05 -0800 | [diff] [blame] | 102 | status_t setAudioAttributes(const jobject attributes); |
| 103 | jobject getAudioAttributes(); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 104 | status_t getParameter(int key, Parcel* reply); |
Ray Essick | 89269d6 | 2019-01-20 14:46:19 -0800 | [diff] [blame] | 105 | status_t getMetrics(char **buffer, size_t *length); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 106 | |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 107 | // Modular DRM |
Robert Shih | 3c3728d | 2018-12-04 17:06:36 -0800 | [diff] [blame] | 108 | status_t prepareDrm(int64_t srcId, |
| 109 | const uint8_t uuid[16], |
| 110 | const Vector<uint8_t>& drmSessionId); |
| 111 | status_t releaseDrm(int64_t srcId); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 112 | // AudioRouting |
Dongwon Kang | 0d7042d | 2018-10-12 16:52:14 -0700 | [diff] [blame] | 113 | status_t setPreferredDevice(jobject device); |
| 114 | jobject getRoutedDevice(); |
Dichen Zhang | f872691 | 2018-10-17 13:31:26 -0700 | [diff] [blame] | 115 | status_t addAudioDeviceCallback(jobject routingDelegate); |
| 116 | status_t removeAudioDeviceCallback(jobject listener); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 117 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 118 | status_t dump(int fd, const Vector<String16>& args); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 119 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 120 | private: |
Dichen Zhang | fc9f40f | 2019-01-04 14:15:28 -0800 | [diff] [blame] | 121 | MediaPlayer2(int32_t sessionId, jobject context); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 122 | bool init(); |
| 123 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 124 | // Disconnect from the currently connected ANativeWindow. |
| 125 | void disconnectNativeWindow_l(); |
| 126 | |
Dichen Zhang | c2465c5 | 2018-11-12 11:56:05 -0800 | [diff] [blame] | 127 | status_t setAudioAttributes_l(const jobject attributes); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 128 | |
| 129 | void clear_l(); |
Wei Jia | 800fe37 | 2018-02-20 15:00:45 -0800 | [diff] [blame] | 130 | status_t seekTo_l(int64_t msec, MediaPlayer2SeekMode mode); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 131 | status_t prepareAsync_l(); |
Wei Jia | 800fe37 | 2018-02-20 15:00:45 -0800 | [diff] [blame] | 132 | status_t getDuration_l(int64_t *msec); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 133 | status_t reset_l(); |
Dichen Zhang | c2465c5 | 2018-11-12 11:56:05 -0800 | [diff] [blame] | 134 | status_t checkState_l(); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 135 | |
| 136 | pid_t mPid; |
| 137 | uid_t mUid; |
| 138 | sp<MediaPlayer2Interface> mPlayer; |
| 139 | sp<MediaPlayer2AudioOutput> mAudioOutput; |
Wei Jia | d2bb1bd | 2018-02-08 09:47:37 -0800 | [diff] [blame] | 140 | int64_t mSrcId; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 141 | thread_id_t mLockThreadId; |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 142 | mutable Mutex mLock; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 143 | Mutex mNotifyLock; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 144 | sp<MediaPlayer2Listener> mListener; |
Wei Jia | 98787a7 | 2018-03-02 14:33:06 -0800 | [diff] [blame] | 145 | media_player2_internal_states mCurrentState; |
Wei Jia | 800fe37 | 2018-02-20 15:00:45 -0800 | [diff] [blame] | 146 | int64_t mCurrentPosition; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 147 | MediaPlayer2SeekMode mCurrentSeekMode; |
Wei Jia | 800fe37 | 2018-02-20 15:00:45 -0800 | [diff] [blame] | 148 | int64_t mSeekPosition; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 149 | MediaPlayer2SeekMode mSeekMode; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 150 | audio_stream_type_t mStreamType; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 151 | bool mLoop; |
Dichen Zhang | 7398ca0 | 2018-10-15 10:25:12 -0700 | [diff] [blame] | 152 | float mVolume; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 153 | int mVideoWidth; |
| 154 | int mVideoHeight; |
Dichen Zhang | 2365864 | 2018-11-15 10:26:16 -0800 | [diff] [blame] | 155 | int32_t mAudioSessionId; |
Dichen Zhang | c2465c5 | 2018-11-12 11:56:05 -0800 | [diff] [blame] | 156 | sp<JObjectHolder> mAudioAttributes; |
Dichen Zhang | fc9f40f | 2019-01-04 14:15:28 -0800 | [diff] [blame] | 157 | sp<JObjectHolder> mContext; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 158 | float mSendLevel; |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 159 | sp<ANativeWindowWrapper> mConnectedWindow; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 160 | }; |
| 161 | |
| 162 | }; // namespace android |
| 163 | |
| 164 | #endif // ANDROID_MEDIAPLAYER2_H |