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> |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 27 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 28 | #include <utils/Errors.h> |
| 29 | #include <utils/Mutex.h> |
| 30 | #include <utils/RefBase.h> |
| 31 | #include <utils/String16.h> |
| 32 | #include <utils/Vector.h> |
| 33 | #include <system/audio-base.h> |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 34 | |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 35 | namespace android { |
| 36 | |
Wei Jia | 28288fb | 2017-12-15 13:45:29 -0800 | [diff] [blame] | 37 | struct ANativeWindowWrapper; |
Wei Jia | c263603 | 2018-02-01 09:15:25 -0800 | [diff] [blame] | 38 | struct DataSourceDesc; |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 39 | class MediaPlayer2AudioOutput; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 40 | |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 41 | // ref-counted object for callbacks |
| 42 | class MediaPlayer2Listener: virtual public RefBase |
| 43 | { |
| 44 | public: |
Wei Jia | d2bb1bd | 2018-02-08 09:47:37 -0800 | [diff] [blame] | 45 | virtual void notify(int64_t srcId, int msg, int ext1, int ext2, const Parcel *obj) = 0; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 46 | }; |
| 47 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 48 | class MediaPlayer2 : public MediaPlayer2InterfaceListener |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 49 | { |
| 50 | public: |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 51 | ~MediaPlayer2(); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 52 | |
| 53 | static sp<MediaPlayer2> Create(); |
| 54 | static status_t DumpAll(int fd, const Vector<String16>& args); |
| 55 | |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 56 | void disconnect(); |
| 57 | |
Wei Jia | d2bb1bd | 2018-02-08 09:47:37 -0800 | [diff] [blame] | 58 | status_t getSrcId(int64_t *srcId); |
Wei Jia | c263603 | 2018-02-01 09:15:25 -0800 | [diff] [blame] | 59 | status_t setDataSource(const sp<DataSourceDesc> &dsd); |
Wei Jia | 57aeffd | 2018-02-15 16:01:14 -0800 | [diff] [blame] | 60 | status_t prepareNextDataSource(const sp<DataSourceDesc> &dsd); |
| 61 | status_t playNextDataSource(int64_t srcId); |
Wei Jia | 28288fb | 2017-12-15 13:45:29 -0800 | [diff] [blame] | 62 | status_t setVideoSurfaceTexture(const sp<ANativeWindowWrapper>& nww); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 63 | status_t setListener(const sp<MediaPlayer2Listener>& listener); |
| 64 | status_t getBufferingSettings(BufferingSettings* buffering /* nonnull */); |
| 65 | status_t setBufferingSettings(const BufferingSettings& buffering); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 66 | status_t prepareAsync(); |
| 67 | status_t start(); |
| 68 | status_t stop(); |
| 69 | status_t pause(); |
| 70 | bool isPlaying(); |
| 71 | status_t setPlaybackSettings(const AudioPlaybackRate& rate); |
| 72 | status_t getPlaybackSettings(AudioPlaybackRate* rate /* nonnull */); |
| 73 | status_t setSyncSettings(const AVSyncSettings& sync, float videoFpsHint); |
| 74 | status_t getSyncSettings( |
| 75 | AVSyncSettings* sync /* nonnull */, |
| 76 | float* videoFps /* nonnull */); |
| 77 | status_t getVideoWidth(int *w); |
| 78 | status_t getVideoHeight(int *h); |
| 79 | status_t seekTo( |
Wei Jia | 800fe37 | 2018-02-20 15:00:45 -0800 | [diff] [blame^] | 80 | int64_t msec, |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 81 | MediaPlayer2SeekMode mode = MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC); |
| 82 | status_t notifyAt(int64_t mediaTimeUs); |
Wei Jia | 800fe37 | 2018-02-20 15:00:45 -0800 | [diff] [blame^] | 83 | status_t getCurrentPosition(int64_t *msec); |
| 84 | status_t getDuration(int64_t *msec); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 85 | status_t reset(); |
| 86 | status_t setAudioStreamType(audio_stream_type_t type); |
| 87 | status_t getAudioStreamType(audio_stream_type_t *type); |
| 88 | status_t setLooping(int loop); |
| 89 | bool isLooping(); |
| 90 | status_t setVolume(float leftVolume, float rightVolume); |
Wei Jia | d2bb1bd | 2018-02-08 09:47:37 -0800 | [diff] [blame] | 91 | void notify(int64_t srcId, int msg, int ext1, int ext2, |
| 92 | const Parcel *obj = NULL); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 93 | status_t invoke(const Parcel& request, Parcel *reply); |
| 94 | status_t setMetadataFilter(const Parcel& filter); |
| 95 | status_t getMetadata(bool update_only, bool apply_filter, Parcel *metadata); |
| 96 | status_t setAudioSessionId(audio_session_t sessionId); |
| 97 | audio_session_t getAudioSessionId(); |
| 98 | status_t setAuxEffectSendLevel(float level); |
| 99 | status_t attachAuxEffect(int effectId); |
| 100 | status_t setParameter(int key, const Parcel& request); |
| 101 | status_t getParameter(int key, Parcel* reply); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 102 | |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 103 | // Modular DRM |
| 104 | status_t prepareDrm(const uint8_t uuid[16], const Vector<uint8_t>& drmSessionId); |
| 105 | status_t releaseDrm(); |
| 106 | // AudioRouting |
| 107 | status_t setOutputDevice(audio_port_handle_t deviceId); |
| 108 | audio_port_handle_t getRoutedDeviceId(); |
| 109 | status_t enableAudioDeviceCallback(bool enabled); |
| 110 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 111 | status_t dump(int fd, const Vector<String16>& args); |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 112 | |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 113 | private: |
| 114 | MediaPlayer2(); |
| 115 | bool init(); |
| 116 | |
| 117 | // @param type Of the metadata to be tested. |
| 118 | // @return true if the metadata should be dropped according to |
| 119 | // the filters. |
| 120 | bool shouldDropMetadata(media::Metadata::Type type) const; |
| 121 | |
| 122 | // Add a new element to the set of metadata updated. Noop if |
| 123 | // the element exists already. |
| 124 | // @param type Of the metadata to be recorded. |
| 125 | void addNewMetadataUpdate(media::Metadata::Type type); |
| 126 | |
| 127 | // Disconnect from the currently connected ANativeWindow. |
| 128 | void disconnectNativeWindow_l(); |
| 129 | |
| 130 | status_t setAudioAttributes_l(const Parcel &request); |
| 131 | |
| 132 | void clear_l(); |
Wei Jia | 800fe37 | 2018-02-20 15:00:45 -0800 | [diff] [blame^] | 133 | status_t seekTo_l(int64_t msec, MediaPlayer2SeekMode mode); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 134 | status_t prepareAsync_l(); |
Wei Jia | 800fe37 | 2018-02-20 15:00:45 -0800 | [diff] [blame^] | 135 | status_t getDuration_l(int64_t *msec); |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 136 | status_t reset_l(); |
| 137 | status_t checkStateForKeySet_l(int key); |
| 138 | |
| 139 | pid_t mPid; |
| 140 | uid_t mUid; |
| 141 | sp<MediaPlayer2Interface> mPlayer; |
| 142 | sp<MediaPlayer2AudioOutput> mAudioOutput; |
Wei Jia | d2bb1bd | 2018-02-08 09:47:37 -0800 | [diff] [blame] | 143 | int64_t mSrcId; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 144 | thread_id_t mLockThreadId; |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 145 | mutable Mutex mLock; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 146 | Mutex mNotifyLock; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 147 | sp<MediaPlayer2Listener> mListener; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 148 | media_player2_states mCurrentState; |
Wei Jia | 800fe37 | 2018-02-20 15:00:45 -0800 | [diff] [blame^] | 149 | int64_t mCurrentPosition; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 150 | MediaPlayer2SeekMode mCurrentSeekMode; |
Wei Jia | 800fe37 | 2018-02-20 15:00:45 -0800 | [diff] [blame^] | 151 | int64_t mSeekPosition; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 152 | MediaPlayer2SeekMode mSeekMode; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 153 | audio_stream_type_t mStreamType; |
| 154 | Parcel* mAudioAttributesParcel; |
| 155 | bool mLoop; |
| 156 | float mLeftVolume; |
| 157 | float mRightVolume; |
| 158 | int mVideoWidth; |
| 159 | int mVideoHeight; |
| 160 | audio_session_t mAudioSessionId; |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 161 | audio_attributes_t * mAudioAttributes; |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 162 | float mSendLevel; |
Wei Jia | ec044b0 | 2018-02-19 12:41:23 -0800 | [diff] [blame] | 163 | |
| 164 | sp<ANativeWindowWrapper> mConnectedWindow; |
| 165 | |
| 166 | // Metadata filters. |
| 167 | media::Metadata::Filter mMetadataAllow; // protected by mLock |
| 168 | media::Metadata::Filter mMetadataDrop; // protected by mLock |
| 169 | |
| 170 | // Metadata updated. For each MEDIA_INFO_METADATA_UPDATE |
| 171 | // notification we try to update mMetadataUpdated which is a |
| 172 | // set: no duplicate. |
| 173 | // getMetadata clears this set. |
| 174 | media::Metadata::Filter mMetadataUpdated; // protected by mLock |
Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame] | 175 | }; |
| 176 | |
| 177 | }; // namespace android |
| 178 | |
| 179 | #endif // ANDROID_MEDIAPLAYER2_H |