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