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