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/Metadata.h> |
| 24 | #include <media/mediaplayer_common.h> |
| 25 | #include <mediaplayer2/MediaPlayer2Interface.h> |
| 26 | #include <mediaplayer2/MediaPlayer2Types.h> |
Dichen Zhang | c2465c5 | 2018-11-12 11:56:05 -0800 | [diff] [blame] | 27 | #include <mediaplayer2/JObjectHolder.h> |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 28 | |
Dichen Zhang | f872691 | 2018-10-17 13:31:26 -0700 | [diff] [blame] | 29 | #include <jni.h> |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 30 | #include <utils/Errors.h> |
| 31 | #include <utils/Mutex.h> |
| 32 | #include <utils/RefBase.h> |
| 33 | #include <utils/String16.h> |
| 34 | #include <utils/Vector.h> |
| 35 | #include <system/audio-base.h> |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 36 | |
Dongwon Kang | 0d7042d | 2018-10-12 16:52:14 -0700 | [diff] [blame] | 37 | #include "jni.h" |
| 38 | |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 39 | namespace android { |
| 40 | |
Wei Jia | 28288fb | 2017-12-15 13:45:29 -0800 | [diff] [blame] | 41 | struct ANativeWindowWrapper; |
Wei Jia | c263603 | 2018-02-01 09:15:25 -0800 | [diff] [blame] | 42 | struct DataSourceDesc; |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 43 | class MediaPlayer2AudioOutput; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 44 | |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 45 | // ref-counted object for callbacks |
| 46 | class MediaPlayer2Listener: virtual public RefBase |
| 47 | { |
| 48 | public: |
Dongwon Kang | 41929fb | 2018-09-09 08:29:56 -0700 | [diff] [blame] | 49 | virtual void notify(int64_t srcId, int msg, int ext1, int ext2, |
| 50 | const PlayerMessage *obj = NULL) = 0; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 51 | }; |
| 52 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 53 | class MediaPlayer2 : public MediaPlayer2InterfaceListener |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 54 | { |
| 55 | public: |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 56 | ~MediaPlayer2(); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 57 | |
Dichen Zhang | 2365864 | 2018-11-15 10:26:16 -0800 | [diff] [blame] | 58 | static sp<MediaPlayer2> Create(int32_t sessionId); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 59 | static status_t DumpAll(int fd, const Vector<String16>& args); |
| 60 | |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 61 | void disconnect(); |
| 62 | |
Wei Jia | d2bb1bd | 2018-02-08 09:47:37 -0800 | [diff] [blame] | 63 | status_t getSrcId(int64_t *srcId); |
Wei Jia | c263603 | 2018-02-01 09:15:25 -0800 | [diff] [blame] | 64 | status_t setDataSource(const sp<DataSourceDesc> &dsd); |
Wei Jia | 57aeffd | 2018-02-15 16:01:14 -0800 | [diff] [blame] | 65 | status_t prepareNextDataSource(const sp<DataSourceDesc> &dsd); |
| 66 | status_t playNextDataSource(int64_t srcId); |
Wei Jia | 28288fb | 2017-12-15 13:45:29 -0800 | [diff] [blame] | 67 | status_t setVideoSurfaceTexture(const sp<ANativeWindowWrapper>& nww); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 68 | status_t setListener(const sp<MediaPlayer2Listener>& listener); |
| 69 | status_t getBufferingSettings(BufferingSettings* buffering /* nonnull */); |
| 70 | status_t setBufferingSettings(const BufferingSettings& buffering); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 71 | status_t prepareAsync(); |
| 72 | status_t start(); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 73 | status_t pause(); |
| 74 | bool isPlaying(); |
Wei Jia | 1f043e4 | 2018-06-20 16:52:50 -0700 | [diff] [blame] | 75 | mediaplayer2_states getState(); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 76 | status_t setPlaybackSettings(const AudioPlaybackRate& rate); |
| 77 | status_t getPlaybackSettings(AudioPlaybackRate* rate /* nonnull */); |
| 78 | status_t setSyncSettings(const AVSyncSettings& sync, float videoFpsHint); |
| 79 | status_t getSyncSettings( |
| 80 | AVSyncSettings* sync /* nonnull */, |
| 81 | float* videoFps /* nonnull */); |
| 82 | status_t getVideoWidth(int *w); |
| 83 | status_t getVideoHeight(int *h); |
| 84 | status_t seekTo( |
Wei Jia | 800fe37 | 2018-02-20 15:00:45 -0800 | [diff] [blame] | 85 | int64_t msec, |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 86 | MediaPlayer2SeekMode mode = MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC); |
| 87 | status_t notifyAt(int64_t mediaTimeUs); |
Wei Jia | 800fe37 | 2018-02-20 15:00:45 -0800 | [diff] [blame] | 88 | status_t getCurrentPosition(int64_t *msec); |
| 89 | status_t getDuration(int64_t *msec); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 90 | status_t reset(); |
| 91 | status_t setAudioStreamType(audio_stream_type_t type); |
| 92 | status_t getAudioStreamType(audio_stream_type_t *type); |
| 93 | status_t setLooping(int loop); |
| 94 | bool isLooping(); |
Dichen Zhang | 7398ca0 | 2018-10-15 10:25:12 -0700 | [diff] [blame] | 95 | status_t setVolume(float volume); |
Wei Jia | d2bb1bd | 2018-02-08 09:47:37 -0800 | [diff] [blame] | 96 | void notify(int64_t srcId, int msg, int ext1, int ext2, |
Dongwon Kang | 41929fb | 2018-09-09 08:29:56 -0700 | [diff] [blame] | 97 | const PlayerMessage *obj = NULL); |
Dongwon Kang | 9f63198 | 2018-07-10 12:34:41 -0700 | [diff] [blame] | 98 | status_t invoke(const PlayerMessage &request, PlayerMessage *reply); |
Dichen Zhang | 2365864 | 2018-11-15 10:26:16 -0800 | [diff] [blame] | 99 | status_t setAudioSessionId(int32_t sessionId); |
| 100 | int32_t getAudioSessionId(); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 101 | status_t setAuxEffectSendLevel(float level); |
| 102 | status_t attachAuxEffect(int effectId); |
Dichen Zhang | c2465c5 | 2018-11-12 11:56:05 -0800 | [diff] [blame] | 103 | status_t setAudioAttributes(const jobject attributes); |
| 104 | jobject getAudioAttributes(); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 105 | status_t getParameter(int key, Parcel* reply); |
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 | 2365864 | 2018-11-15 10:26:16 -0800 | [diff] [blame] | 121 | MediaPlayer2(int32_t sessionId); |
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; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 157 | float mSendLevel; |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 158 | sp<ANativeWindowWrapper> mConnectedWindow; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 159 | }; |
| 160 | |
| 161 | }; // namespace android |
| 162 | |
| 163 | #endif // ANDROID_MEDIAPLAYER2_H |