xref: /aosp_15_r20/frameworks/av/media/libmediaplayerservice/MediaPlayerService.h (revision ec779b8e0859a360c3d303172224686826e6e0e1)
1 /*
2 **
3 ** Copyright 2008, 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_MEDIAPLAYERSERVICE_H
19 #define ANDROID_MEDIAPLAYERSERVICE_H
20 
21 #include <arpa/inet.h>
22 #include <string>
23 
24 #include <utils/threads.h>
25 #include <utils/Errors.h>
26 #include <utils/KeyedVector.h>
27 #include <utils/String8.h>
28 #include <utils/Vector.h>
29 
30 #include <media/AidlConversion.h>
31 #include <media/AudioContainers.h>
32 #include <media/AudioResamplerPublic.h>
33 #include <media/AudioSystem.h>
34 #include <media/AudioTrack.h>
35 #include <media/MediaPlayerInterface.h>
36 #include <media/Metadata.h>
37 #include <media/stagefright/foundation/ABase.h>
38 #include <mediautils/Synchronization.h>
39 #include <android/content/AttributionSourceState.h>
40 
41 #include <system/audio.h>
42 
43 namespace android {
44 
45 using content::AttributionSourceState;
46 
47 struct AVSyncSettings;
48 class DeathNotifier;
49 class IDataSource;
50 class IMediaRecorder;
51 class IMediaMetadataRetriever;
52 class IRemoteDisplay;
53 class IRemoteDisplayClient;
54 class MediaRecorderClient;
55 
56 #define CALLBACK_ANTAGONIZER 0
57 #if CALLBACK_ANTAGONIZER
58 class Antagonizer {
59 public:
60     Antagonizer(const sp<MediaPlayerBase::Listener> &listener);
start()61     void start() { mActive = true; }
stop()62     void stop() { mActive = false; }
63     void kill();
64 private:
65     static const int interval;
66     Antagonizer();
67     static int callbackThread(void* cookie);
68     Mutex                         mLock;
69     Condition                     mCondition;
70     bool                          mExit;
71     bool                          mActive;
72     sp<MediaPlayerBase::Listener> mListener;
73 };
74 #endif
75 
76 class MediaPlayerService : public BnMediaPlayerService
77 {
78     class Client;
79 
80     class AudioOutput : public MediaPlayerBase::AudioSink
81     {
82         class CallbackData;
83 
84      public:
85                                 AudioOutput(
86                                         audio_session_t sessionId,
87                                         const AttributionSourceState& attributionSource,
88                                         const audio_attributes_t * attr,
89                                         const sp<AudioSystem::AudioDeviceCallback>& deviceCallback);
90         virtual                 ~AudioOutput();
91 
ready()92         virtual bool            ready() const { return mTrack != 0; }
93         virtual ssize_t         bufferSize() const;
94         virtual ssize_t         frameCount() const;
95         virtual ssize_t         channelCount() const;
96         virtual ssize_t         frameSize() const;
97         virtual uint32_t        latency() const;
98         virtual float           msecsPerFrame() const;
99         virtual status_t        getPosition(uint32_t *position) const;
100         virtual status_t        getTimestamp(AudioTimestamp &ts) const;
101         virtual int64_t         getPlayedOutDurationUs(int64_t nowUs) const;
102         virtual status_t        getFramesWritten(uint32_t *frameswritten) const;
103         virtual audio_session_t getSessionId() const;
104         virtual uint32_t        getSampleRate() const;
105         virtual int64_t         getBufferDurationInUs() const;
getFlags()106         virtual audio_output_flags_t getFlags() const { return mFlags; }
107 
108         status_t open(
109                 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
110                 audio_format_t format, int bufferCount,
111                 AudioCallback cb, const wp<RefBase>& cookie,
112                 audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE,
113                 const audio_offload_info_t *offloadInfo = NULL,
114                 bool doNotReconnect = false,
115                 uint32_t suggestedFrameCount = 0) override;
116 
117         virtual void            setPlayerIId(int32_t playerIId);
118 
119         virtual status_t        start();
120         virtual ssize_t         write(const void* buffer, size_t size, bool blocking = true);
121         virtual void            stop();
122         virtual void            flush();
123         virtual void            pause();
124         virtual void            close();
125                 void            setAudioStreamType(audio_stream_type_t streamType);
getAudioStreamType()126         virtual audio_stream_type_t getAudioStreamType() const { return mStreamType; }
127                 void            setAudioAttributes(const audio_attributes_t * attributes);
128 
129                 void            setVolume(float left, float right);
130         virtual status_t        setPlaybackRate(const AudioPlaybackRate& rate);
131         virtual status_t        getPlaybackRate(AudioPlaybackRate* rate /* nonnull */);
132 
133                 status_t        setAuxEffectSendLevel(float level);
134                 status_t        attachAuxEffect(int effectId);
135         virtual status_t        dump(int fd, const Vector<String16>& args) const;
136 
137         static bool             isOnEmulator();
138         static int              getMinBufferCount();
139                 void            setNextOutput(const sp<AudioOutput>& nextOutput);
140                 void            switchToNextOutput();
needsTrailingPadding()141         virtual bool            needsTrailingPadding() { return mNextOutput == NULL; }
142         virtual status_t        setParameters(const String8& keyValuePairs);
143         virtual String8         getParameters(const String8& keys);
144 
145         virtual media::VolumeShaper::Status applyVolumeShaper(
146                                         const sp<media::VolumeShaper::Configuration>& configuration,
147                                         const sp<media::VolumeShaper::Operation>& operation) override;
148         virtual sp<media::VolumeShaper::State> getVolumeShaperState(int id) override;
149 
150         // AudioRouting
151         virtual status_t        setOutputDevice(audio_port_handle_t deviceId);
152         virtual status_t        getRoutedDeviceIds(DeviceIdVector& deviceIds);
153         virtual status_t        enableAudioDeviceCallback(bool enabled);
154 
155     private:
156         static void             setMinBufferCount();
157         static void             CallbackWrapper(
158                 int event, void *me, void *info);
159                void             deleteRecycledTrack_l();
160                void             close_l();
161            status_t             updateTrack();
162 
163         sp<AudioTrack>          mTrack;
164         sp<AudioTrack>          mRecycledTrack;
165         sp<AudioOutput>         mNextOutput;
166         int                     mCachedPlayerIId;
167         AudioCallback           mCallback;
168         wp<RefBase>             mCallbackCookie;
169         sp<CallbackData>        mCallbackData;
170         audio_stream_type_t     mStreamType;
171         audio_attributes_t *    mAttributes;
172         float                   mLeftVolume;
173         float                   mRightVolume;
174         AudioPlaybackRate       mPlaybackRate;
175         uint32_t                mSampleRateHz; // sample rate of the content, as set in open()
176         float                   mMsecsPerFrame;
177         size_t                  mFrameSize;
178         audio_session_t         mSessionId;
179         AttributionSourceState  mAttributionSource;
180         float                   mSendLevel;
181         int                     mAuxEffectId;
182         audio_output_flags_t    mFlags;
183         sp<media::VolumeHandler>       mVolumeHandler;
184         audio_port_handle_t     mSelectedDeviceId;
185         DeviceIdVector          mRoutedDeviceIds;
186         bool                    mDeviceCallbackEnabled;
187         wp<AudioSystem::AudioDeviceCallback>        mDeviceCallback;
188         mutable Mutex           mLock;
189 
190         // static variables below not protected by mutex
191         static bool             mIsOnEmulator;
192         static int              mMinBufferCount;  // 12 for emulator; otherwise 4
193 
194         // CallbackData is what is passed to the AudioTrack as the "user" data.
195         // We need to be able to target this to a different Output on the fly,
196         // so we can't use the Output itself for this.
197         class CallbackData : public AudioTrack::IAudioTrackCallback {
198             friend AudioOutput;
199         public:
CallbackData(const wp<AudioOutput> & cookie)200             explicit CallbackData(const wp<AudioOutput>& cookie) {
201                 mData = cookie;
202                 mSwitching = false;
203             }
getOutput()204             sp<AudioOutput> getOutput() const { return mData.load().promote(); }
setOutput(const wp<AudioOutput> & newcookie)205             void            setOutput(const wp<AudioOutput>& newcookie) { mData.store(newcookie); }
206             // lock/unlock are used by the callback before accessing the payload of this object
lock()207             void            lock() const { mLock.lock(); }
unlock()208             void            unlock() const { mLock.unlock(); }
209 
210             // tryBeginTrackSwitch/endTrackSwitch are used when the CallbackData is handed over
211             // to the next sink.
212 
213             // tryBeginTrackSwitch() returns true only if it obtains the lock.
tryBeginTrackSwitch()214             bool            tryBeginTrackSwitch() {
215                 LOG_ALWAYS_FATAL_IF(mSwitching, "tryBeginTrackSwitch() already called");
216                 if (mLock.tryLock() != OK) {
217                     return false;
218                 }
219                 mSwitching = true;
220                 return true;
221             }
endTrackSwitch()222             void            endTrackSwitch() {
223                 if (mSwitching) {
224                     mLock.unlock();
225                 }
226                 mSwitching = false;
227             }
228         protected:
229             size_t onMoreData(const AudioTrack::Buffer& buffer) override;
230             void onUnderrun() override;
231             void onStreamEnd() override;
232             void onNewIAudioTrack() override;
233         private:
234             mediautils::atomic_wp<AudioOutput> mData;
235             mutable Mutex   mLock; // a recursive mutex might make this unnecessary.
236             bool            mSwitching;
237             DISALLOW_EVIL_CONSTRUCTORS(CallbackData);
238         };
239 
240     }; // AudioOutput
241 
242 public:
243     static  void                instantiate();
244 
245     // IMediaPlayerService interface
246     virtual sp<IMediaRecorder> createMediaRecorder(const AttributionSourceState &attributionSource);
247     void    removeMediaRecorderClient(const wp<MediaRecorderClient>& client);
248     virtual sp<IMediaMetadataRetriever> createMetadataRetriever();
249 
250     virtual sp<IMediaPlayer>    create(const sp<IMediaPlayerClient>& client,
251                                        audio_session_t audioSessionId,
252                                        const AttributionSourceState& attributionSource);
253 
254     virtual sp<IMediaCodecList> getCodecList() const;
255 
256     virtual sp<IRemoteDisplay> listenForRemoteDisplay(const String16 &opPackageName,
257             const sp<IRemoteDisplayClient>& client, const String8& iface);
258     virtual status_t            dump(int fd, const Vector<String16>& args);
259 
260             void                removeClient(const wp<Client>& client);
261             bool                hasClient(wp<Client> client);
262 
263     enum {
264         MEDIASERVER_PROCESS_DEATH = 0,
265         MEDIAEXTRACTOR_PROCESS_DEATH = 1,
266         MEDIACODEC_PROCESS_DEATH = 2,
267         AUDIO_PROCESS_DEATH = 3,   // currently no need to track this
268         CAMERA_PROCESS_DEATH = 4
269     };
270 
271     // Collect info of the codec usage from media player and media recorder
272     virtual void                addBatteryData(uint32_t params);
273     // API for the Battery app to pull the data of codecs usage
274     virtual status_t            pullBatteryData(Parcel* reply);
275 private:
276     struct BatteryTracker {
277         BatteryTracker();
278         // Collect info of the codec usage from media player and media recorder
279         void addBatteryData(uint32_t params);
280         // API for the Battery app to pull the data of codecs usage
281         status_t pullBatteryData(Parcel* reply);
282 
283     private:
284         // For battery usage tracking purpose
285         struct BatteryUsageInfo {
286             // how many streams are being played by one UID
287             int     refCount;
288             // a temp variable to store the duration(ms) of audio codecs
289             // when we start a audio codec, we minus the system time from audioLastTime
290             // when we pause it, we add the system time back to the audioLastTime
291             // so after the pause, audioLastTime = pause time - start time
292             // if multiple audio streams are played (or recorded), then audioLastTime
293             // = the total playing time of all the streams
294             int32_t audioLastTime;
295             // when all the audio streams are being paused, we assign audioLastTime to
296             // this variable, so this value could be provided to the battery app
297             // in the next pullBatteryData call
298             int32_t audioTotalTime;
299 
300             int32_t videoLastTime;
301             int32_t videoTotalTime;
302         };
303         KeyedVector<int, BatteryUsageInfo>    mBatteryData;
304 
305         enum {
306             SPEAKER,
307             OTHER_AUDIO_DEVICE,
308             SPEAKER_AND_OTHER,
309             NUM_AUDIO_DEVICES
310         };
311 
312         struct BatteryAudioFlingerUsageInfo {
313             int refCount; // how many audio streams are being played
314             int deviceOn[NUM_AUDIO_DEVICES]; // whether the device is currently used
315             int32_t lastTime[NUM_AUDIO_DEVICES]; // in ms
316             // totalTime[]: total time of audio output devices usage
317             int32_t totalTime[NUM_AUDIO_DEVICES]; // in ms
318         };
319 
320         // This varialble is used to record the usage of audio output device
321         // for battery app
322         BatteryAudioFlingerUsageInfo mBatteryAudio;
323 
324         mutable Mutex mLock;
325     };
326     BatteryTracker mBatteryTracker;
327 
328     class Client : public BnMediaPlayer {
329         // IMediaPlayer interface
330         virtual void            disconnect();
331         virtual status_t        setVideoSurfaceTexture(
332                                         const sp<IGraphicBufferProducer>& bufferProducer);
333         virtual status_t        setBufferingSettings(const BufferingSettings& buffering) override;
334         virtual status_t        getBufferingSettings(
335                                         BufferingSettings* buffering /* nonnull */) override;
336         virtual status_t        prepareAsync();
337         virtual status_t        start();
338         virtual status_t        stop();
339         virtual status_t        pause();
340         virtual status_t        isPlaying(bool* state);
341         virtual status_t        setPlaybackSettings(const AudioPlaybackRate& rate);
342         virtual status_t        getPlaybackSettings(AudioPlaybackRate* rate /* nonnull */);
343         virtual status_t        setSyncSettings(const AVSyncSettings& rate, float videoFpsHint);
344         virtual status_t        getSyncSettings(AVSyncSettings* rate /* nonnull */,
345                                                 float* videoFps /* nonnull */);
346         virtual status_t        seekTo(
347                 int msec,
348                 MediaPlayerSeekMode mode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC);
349         virtual status_t        getCurrentPosition(int* msec);
350         virtual status_t        getDuration(int* msec);
351         virtual status_t        reset();
352         virtual status_t        notifyAt(int64_t mediaTimeUs);
353         virtual status_t        setAudioStreamType(audio_stream_type_t type);
354         virtual status_t        setLooping(int loop);
355         virtual status_t        setVolume(float leftVolume, float rightVolume);
356         virtual status_t        invoke(const Parcel& request, Parcel *reply);
357         virtual status_t        setMetadataFilter(const Parcel& filter);
358         virtual status_t        getMetadata(bool update_only,
359                                             bool apply_filter,
360                                             Parcel *reply);
361         virtual status_t        setAuxEffectSendLevel(float level);
362         virtual status_t        attachAuxEffect(int effectId);
363         virtual status_t        setParameter(int key, const Parcel &request);
364         virtual status_t        getParameter(int key, Parcel *reply);
365         virtual status_t        setRetransmitEndpoint(const struct sockaddr_in* endpoint);
366         virtual status_t        getRetransmitEndpoint(struct sockaddr_in* endpoint);
367         virtual status_t        setNextPlayer(const sp<IMediaPlayer>& player);
368 
369         virtual media::VolumeShaper::Status applyVolumeShaper(
370                                         const sp<media::VolumeShaper::Configuration>& configuration,
371                                         const sp<media::VolumeShaper::Operation>& operation) override;
372         virtual sp<media::VolumeShaper::State> getVolumeShaperState(int id) override;
373 
374         sp<MediaPlayerBase>     createPlayer(player_type playerType);
375 
376         virtual status_t        setDataSource(
377                         const sp<IMediaHTTPService> &httpService,
378                         const char *url,
379                         const KeyedVector<String8, String8> *headers);
380 
381         virtual status_t        setDataSource(int fd, int64_t offset, int64_t length);
382 
383         virtual status_t        setDataSource(const sp<IStreamSource> &source);
384         virtual status_t        setDataSource(const sp<IDataSource> &source);
385         virtual status_t        setDataSource(const String8& rtpParams);
386 
387 
388         sp<MediaPlayerBase>     setDataSource_pre(player_type playerType);
389         status_t                setDataSource_post(const sp<MediaPlayerBase>& p,
390                                                    status_t status);
391 
392                 void            notify(int msg, int ext1, int ext2, const Parcel *obj);
393 
pid()394                 pid_t           pid() const {
395                     return VALUE_OR_FATAL(aidl2legacy_int32_t_pid_t(mAttributionSource.pid));
396                 }
397         virtual status_t        dump(int fd, const Vector<String16>& args);
398 
getAudioSessionId()399                 audio_session_t getAudioSessionId() { return mAudioSessionId; }
400         // Modular DRM
401         virtual status_t prepareDrm(const uint8_t uuid[16], const Vector<uint8_t>& drmSessionId);
402         virtual status_t releaseDrm();
403         // AudioRouting
404         virtual status_t setOutputDevice(audio_port_handle_t deviceId);
405         virtual status_t getRoutedDeviceIds(DeviceIdVector& deviceIds);
406         virtual status_t enableAudioDeviceCallback(bool enabled);
407 
408     private:
409         class AudioDeviceUpdatedNotifier: public AudioSystem::AudioDeviceCallback
410         {
411         public:
AudioDeviceUpdatedNotifier(const sp<MediaPlayerBase> & listener)412             AudioDeviceUpdatedNotifier(const sp<MediaPlayerBase>& listener) {
413                 mListener = listener;
414             }
~AudioDeviceUpdatedNotifier()415             ~AudioDeviceUpdatedNotifier() {}
416 
417             virtual void onAudioDeviceUpdate(audio_io_handle_t audioIo,
418                                              const DeviceIdVector& deviceIds);
419 
420         private:
421             wp<MediaPlayerBase> mListener;
422         };
423 
424         friend class MediaPlayerService;
425                                 Client( const sp<MediaPlayerService>& service,
426                                         const AttributionSourceState& attributionSource,
427                                         int32_t connId,
428                                         const sp<IMediaPlayerClient>& client,
429                                         audio_session_t audioSessionId);
430                                 Client();
431         virtual                 ~Client();
432 
433                 void            deletePlayer();
434 
getPlayer()435         sp<MediaPlayerBase>     getPlayer() const { Mutex::Autolock lock(mLock); return mPlayer; }
436 
437 
438 
439         // @param type Of the metadata to be tested.
440         // @return true if the metadata should be dropped according to
441         //              the filters.
442         bool shouldDropMetadata(media::Metadata::Type type) const;
443 
444         // Add a new element to the set of metadata updated. Noop if
445         // the element exists already.
446         // @param type Of the metadata to be recorded.
447         void addNewMetadataUpdate(media::Metadata::Type type);
448 
449         // Disconnect from the currently connected ANativeWindow.
450         void disconnectNativeWindow_l();
451 
452         status_t setAudioAttributes_l(const Parcel &request);
453 
454         class Listener : public MediaPlayerBase::Listener {
455         public:
Listener(const wp<Client> & client)456             Listener(const wp<Client> &client) : mClient(client) {}
~Listener()457             virtual ~Listener() {}
notify(int msg,int ext1,int ext2,const Parcel * obj)458             virtual void notify(int msg, int ext1, int ext2, const Parcel *obj) {
459                 sp<Client> client = mClient.promote();
460                 if (client != NULL) {
461                     client->notify(msg, ext1, ext2, obj);
462                 }
463             }
464         private:
465             wp<Client> mClient;
466         };
467 
468         mutable     Mutex                         mLock;
469                     sp<MediaPlayerBase>           mPlayer;
470                     sp<MediaPlayerService>        mService;
471                     sp<IMediaPlayerClient>        mClient;
472                     sp<AudioOutput>               mAudioOutput;
473                     const AttributionSourceState  mAttributionSource;
474                     status_t                      mStatus;
475                     bool                          mLoop;
476                     int32_t                       mConnId;
477                     audio_session_t               mAudioSessionId;
478                     audio_attributes_t *          mAudioAttributes;
479                     sp<ANativeWindow>             mConnectedWindow;
480                     sp<IBinder>                   mConnectedWindowBinder;
481                     struct sockaddr_in            mRetransmitEndpoint;
482                     bool                          mRetransmitEndpointValid;
483                     sp<Client>                    mNextClient;
484                     sp<MediaPlayerBase::Listener> mListener;
485 
486         // Metadata filters.
487         media::Metadata::Filter mMetadataAllow;  // protected by mLock
488         media::Metadata::Filter mMetadataDrop;  // protected by mLock
489 
490         // Metadata updated. For each MEDIA_INFO_METADATA_UPDATE
491         // notification we try to update mMetadataUpdated which is a
492         // set: no duplicate.
493         // getMetadata clears this set.
494         media::Metadata::Filter mMetadataUpdated;  // protected by mLock
495 
496         std::vector<DeathNotifier> mDeathNotifiers;
497         sp<AudioDeviceUpdatedNotifier> mAudioDeviceUpdatedListener;
498 #if CALLBACK_ANTAGONIZER
499                     Antagonizer*                  mAntagonizer;
500 #endif
501     }; // Client
502 
503 // ----------------------------------------------------------------------------
504 
505                             MediaPlayerService();
506     virtual                 ~MediaPlayerService();
507 
508     mutable     Mutex                       mLock;
509                 SortedVector< wp<Client> >  mClients;
510                 SortedVector< wp<MediaRecorderClient> > mMediaRecorderClients;
511                 int32_t                     mNextConnId;
512 
513 #ifdef FUZZ_MODE_MEDIA_PLAYER_SERVICE
514 public:
515     friend class sp<MediaPlayerService>;
516     static sp<MediaPlayerService> createForFuzzTesting();
517 #endif // FUZZ_MODE_MEDIA_PLAYER_SERVICE
518 };
519 
520 // ----------------------------------------------------------------------------
521 
522 }; // namespace android
523 
524 #endif // ANDROID_MEDIAPLAYERSERVICE_H
525