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