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