1 /* 2 * Copyright (C) 2007 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_AUDIOTRACK_H 18 #define ANDROID_AUDIOTRACK_H 19 20 #include <audiomanager/IAudioManager.h> 21 #include <binder/IMemory.h> 22 #include <cutils/sched_policy.h> 23 #include <media/AudioSystem.h> 24 #include <media/AudioTimestamp.h> 25 #include <media/AudioResamplerPublic.h> 26 #include <media/MediaMetricsItem.h> 27 #include <media/Modulo.h> 28 #include <media/VolumeShaper.h> 29 #include <utils/threads.h> 30 #include <android/content/AttributionSourceState.h> 31 32 #include <chrono> 33 #include <string> 34 35 #include "android/media/BnAudioTrackCallback.h" 36 #include "android/media/IAudioTrack.h" 37 #include "android/media/IAudioTrackCallback.h" 38 39 namespace android { 40 41 using content::AttributionSourceState; 42 43 // ---------------------------------------------------------------------------- 44 45 struct audio_track_cblk_t; 46 class AudioTrackClientProxy; 47 class StaticAudioTrackClientProxy; 48 49 // ---------------------------------------------------------------------------- 50 51 class AudioTrack : public AudioSystem::AudioDeviceCallback 52 { 53 public: 54 55 /* Events used by AudioTrack callback function (callback_t). 56 * Keep in sync with frameworks/base/media/java/android/media/AudioTrack.java NATIVE_EVENT_*. 57 */ 58 enum event_type { 59 EVENT_MORE_DATA = 0, // Request to write more data to buffer. 60 // This event only occurs for TRANSFER_CALLBACK. 61 // If this event is delivered but the callback handler 62 // does not want to write more data, the handler must 63 // ignore the event by setting frameCount to zero. 64 // This might occur, for example, if the application is 65 // waiting for source data or is at the end of stream. 66 // 67 // For data filling, it is preferred that the callback 68 // does not block and instead returns a short count on 69 // the amount of data actually delivered 70 // (or 0, if no data is currently available). 71 EVENT_UNDERRUN = 1, // Buffer underrun occurred. This will not occur for 72 // static tracks. 73 EVENT_LOOP_END = 2, // Sample loop end was reached; playback restarted from 74 // loop start if loop count was not 0 for a static track. 75 EVENT_MARKER = 3, // Playback head is at the specified marker position 76 // (See setMarkerPosition()). 77 EVENT_NEW_POS = 4, // Playback head is at a new position 78 // (See setPositionUpdatePeriod()). 79 EVENT_BUFFER_END = 5, // Playback has completed for a static track. 80 EVENT_NEW_IAUDIOTRACK = 6, // IAudioTrack was re-created, either due to re-routing and 81 // voluntary invalidation by mediaserver, or mediaserver crash. 82 EVENT_STREAM_END = 7, // Sent after all the buffers queued in AF and HW are played 83 // back (after stop is called) for an offloaded track. 84 #if 0 // FIXME not yet implemented 85 EVENT_NEW_TIMESTAMP = 8, // Delivered periodically and when there's a significant change 86 // in the mapping from frame position to presentation time. 87 // See AudioTimestamp for the information included with event. 88 #endif 89 EVENT_CAN_WRITE_MORE_DATA = 9,// Notification that more data can be given by write() 90 // This event only occurs for TRANSFER_SYNC_NOTIF_CALLBACK. 91 }; 92 93 /* Client should declare a Buffer and pass the address to obtainBuffer() 94 * and releaseBuffer(). See also callback_t for EVENT_MORE_DATA. 95 */ 96 97 class Buffer 98 { 99 friend AudioTrack; 100 public: size()101 size_t size() const { return mSize; } getFrameCount()102 size_t getFrameCount() const { return frameCount; } data()103 uint8_t * data() const { return ui8; } 104 // Leaving public for now to ease refactoring. This class will be 105 // replaced 106 size_t frameCount; // number of sample frames corresponding to size; 107 // on input to obtainBuffer() it is the number of frames desired, 108 // on output from obtainBuffer() it is the number of available 109 // [empty slots for] frames to be filled 110 // on input to releaseBuffer() it is currently ignored 111 private: 112 size_t mSize; // input/output in bytes == frameCount * frameSize 113 // on input to obtainBuffer() it is ignored 114 // on output from obtainBuffer() it is the number of available 115 // [empty slots for] bytes to be filled, 116 // which is frameCount * frameSize 117 // on input to releaseBuffer() it is the number of bytes to 118 // release 119 120 union { 121 void* raw; 122 int16_t* i16; // signed 16-bit 123 uint8_t* ui8; // unsigned 8-bit, offset by 0x80 124 }; // input to obtainBuffer(): unused, output: pointer to buffer 125 126 uint32_t sequence; // IAudioTrack instance sequence number, as of obtainBuffer(). 127 // It is set by obtainBuffer() and confirmed by releaseBuffer(). 128 // Not "user-serviceable". 129 }; 130 131 /* As a convenience, if a callback is supplied, a handler thread 132 * is automatically created with the appropriate priority. This thread 133 * invokes the callback when a new buffer becomes available or various conditions occur. 134 * Parameters: 135 * 136 * event: type of event notified (see enum AudioTrack::event_type). 137 * user: Pointer to context for use by the callback receiver. 138 * info: Pointer to optional parameter according to event type: 139 * - EVENT_MORE_DATA: pointer to AudioTrack::Buffer struct. The callback must not write 140 * more bytes than indicated by 'size' field and update 'size' if fewer bytes are 141 * written. 142 * - EVENT_UNDERRUN: unused. 143 * - EVENT_LOOP_END: pointer to an int indicating the number of loops remaining. 144 * - EVENT_MARKER: pointer to const uint32_t containing the marker position in frames. 145 * - EVENT_NEW_POS: pointer to const uint32_t containing the new position in frames. 146 * - EVENT_BUFFER_END: unused. 147 * - EVENT_NEW_IAUDIOTRACK: unused. 148 * - EVENT_STREAM_END: unused. 149 * - EVENT_NEW_TIMESTAMP: pointer to const AudioTimestamp. 150 */ 151 152 class IAudioTrackCallback : public virtual RefBase { 153 friend AudioTrack; 154 protected: 155 /* Request to write more data to buffer. 156 * This event only occurs for TRANSFER_CALLBACK. 157 * If this event is delivered but the callback handler does not want to write more data, 158 * the handler must ignore the event by returning zero. 159 * This might occur, for example, if the application is waiting for source data or is at 160 * the end of stream. 161 * For data filling, it is preferred that the callback does not block and instead returns 162 * a short count of the amount of data actually delivered. 163 * Parameters: 164 * - buffer: Buffer to fill 165 * Returns: 166 * Amount of data actually written in bytes. 167 */ onMoreData(const AudioTrack::Buffer & buffer)168 virtual size_t onMoreData([[maybe_unused]] const AudioTrack::Buffer& buffer) { return 0; } 169 170 // Buffer underrun occurred. This will not occur for static tracks. onUnderrun()171 virtual void onUnderrun() {} 172 173 /* Sample loop end was reached; playback restarted from loop start if loop count was not 0 174 * for a static track. 175 * Parameters: 176 * - loopsRemaining: Number of loops remaining to be played. -1 if infinite looping. 177 */ onLoopEnd(int32_t loopsRemaining)178 virtual void onLoopEnd([[maybe_unused]] int32_t loopsRemaining) {} 179 180 /* Playback head is at the specified marker (See setMarkerPosition()). 181 * Parameters: 182 * - onMarker: Marker position in frames 183 */ onMarker(uint32_t markerPosition)184 virtual void onMarker([[maybe_unused]] uint32_t markerPosition) {} 185 186 /* Playback head is at a new position (See setPositionUpdatePeriod()). 187 * Parameters: 188 * - newPos: New position in frames 189 */ onNewPos(uint32_t newPos)190 virtual void onNewPos([[maybe_unused]] uint32_t newPos) {} 191 192 // Playback has completed for a static track. onBufferEnd()193 virtual void onBufferEnd() {} 194 195 // IAudioTrack was re-created, either due to re-routing and voluntary invalidation 196 // by mediaserver, or mediaserver crash. onNewIAudioTrack()197 virtual void onNewIAudioTrack() {} 198 199 // Sent after all the buffers queued in AF and HW are played back (after stop is called) 200 // for an offloaded track. onStreamEnd()201 virtual void onStreamEnd() {} 202 203 /* Delivered periodically and when there's a significant change 204 * in the mapping from frame position to presentation time. 205 * See AudioTimestamp for the information included with event. 206 * TODO not yet implemented. 207 * Parameters: 208 * - timestamp: New frame position and presentation time mapping. 209 */ onNewTimestamp(AudioTimestamp timestamp)210 virtual void onNewTimestamp([[maybe_unused]] AudioTimestamp timestamp) {} 211 212 /* Notification that more data can be given by write() 213 * This event only occurs for TRANSFER_SYNC_NOTIF_CALLBACK. 214 * Similar to onMoreData(), return the number of frames actually written 215 * Parameters: 216 * - buffer: Buffer to fill 217 * Returns: 218 * Amount of data actually written in bytes. 219 */ onCanWriteMoreData(const AudioTrack::Buffer & buffer)220 virtual size_t onCanWriteMoreData([[maybe_unused]] const AudioTrack::Buffer& buffer) { 221 return 0; 222 } 223 }; 224 225 /* Returns the minimum frame count required for the successful creation of 226 * an AudioTrack object. 227 * Returned status (from utils/Errors.h) can be: 228 * - NO_ERROR: successful operation 229 * - NO_INIT: audio server or audio hardware not initialized 230 * - BAD_VALUE: unsupported configuration 231 * frameCount is guaranteed to be non-zero if status is NO_ERROR, 232 * and is undefined otherwise. 233 * FIXME This API assumes a route, and so should be deprecated. 234 */ 235 236 static status_t getMinFrameCount(size_t* frameCount, audio_stream_type_t streamType, 237 uint32_t sampleRate); 238 239 /* Check if direct playback is possible for the given audio configuration and attributes. 240 * Return true if output is possible for the given parameters. Otherwise returns false. 241 */ 242 static bool isDirectOutputSupported(const audio_config_base_t& config, 243 const audio_attributes_t& attributes); 244 245 /* Checks for erroneous status, logs the error message. 246 * Updates and returns mStatus. 247 */ 248 status_t logIfErrorAndReturnStatus(status_t status, const std::string& errorMessage); 249 250 /* How data is transferred to AudioTrack 251 */ 252 enum transfer_type { 253 TRANSFER_DEFAULT, // not specified explicitly; determine from the other parameters 254 TRANSFER_CALLBACK, // callback EVENT_MORE_DATA 255 TRANSFER_OBTAIN, // call obtainBuffer() and releaseBuffer() 256 TRANSFER_SYNC, // synchronous write() 257 TRANSFER_SHARED, // shared memory 258 TRANSFER_SYNC_NOTIF_CALLBACK, // synchronous write(), notif EVENT_CAN_WRITE_MORE_DATA 259 }; 260 261 /* Constructs an uninitialized AudioTrack. No connection with 262 * AudioFlinger takes place. Use set() after this. 263 */ 264 explicit AudioTrack(const AttributionSourceState& attributionSourceState = {}); 265 266 /* Creates an AudioTrack object and registers it with AudioFlinger. 267 * Once created, the track needs to be started before it can be used. 268 * Unspecified values are set to appropriate default values. 269 * 270 * Parameters: 271 * 272 * streamType: Select the type of audio stream this track is attached to 273 * (e.g. AUDIO_STREAM_MUSIC). 274 * sampleRate: Data source sampling rate in Hz. Zero means to use the sink sample rate. 275 * A non-zero value must be specified if AUDIO_OUTPUT_FLAG_DIRECT is set. 276 * 0 will not work with current policy implementation for direct output 277 * selection where an exact match is needed for sampling rate. 278 * format: Audio format. For mixed tracks, any PCM format supported by server is OK. 279 * For direct and offloaded tracks, the possible format(s) depends on the 280 * output sink. 281 * channelMask: Channel mask, such that audio_is_output_channel(channelMask) is true. 282 * frameCount: Minimum size of track PCM buffer in frames. This defines the 283 * application's contribution to the 284 * latency of the track. The actual size selected by the AudioTrack could be 285 * larger if the requested size is not compatible with current audio HAL 286 * configuration. Zero means to use a default value. 287 * flags: See comments on audio_output_flags_t in <system/audio.h>. 288 * cbf: Callback function. If not null, this function is called periodically 289 * to provide new data in TRANSFER_CALLBACK mode 290 * and inform of marker, position updates, etc. 291 * user: Context for use by the callback receiver. 292 * notificationFrames: The callback function is called each time notificationFrames PCM 293 * frames have been consumed from track input buffer by server. 294 * Zero means to use a default value, which is typically: 295 * - fast tracks: HAL buffer size, even if track frameCount is larger 296 * - normal tracks: 1/2 of track frameCount 297 * A positive value means that many frames at initial source sample rate. 298 * A negative value for this parameter specifies the negative of the 299 * requested number of notifications (sub-buffers) in the entire buffer. 300 * For fast tracks, the FastMixer will process one sub-buffer at a time. 301 * The size of each sub-buffer is determined by the HAL. 302 * To get "double buffering", for example, one should pass -2. 303 * The minimum number of sub-buffers is 1 (expressed as -1), 304 * and the maximum number of sub-buffers is 8 (expressed as -8). 305 * Negative is only permitted for fast tracks, and if frameCount is zero. 306 * TODO It is ugly to overload a parameter in this way depending on 307 * whether it is positive, negative, or zero. Consider splitting apart. 308 * sessionId: Specific session ID, or zero to use default. 309 * transferType: How data is transferred to AudioTrack. 310 * offloadInfo: If not NULL, provides offload parameters for 311 * AudioSystem::getOutputForAttr(). 312 * attributionSource: The attribution source of the app which initially requested this 313 * AudioTrack. 314 * Includes the UID and PID for power management tracking, or -1 for 315 * current user/process ID, plus the package name. 316 * pAttributes: If not NULL, supersedes streamType for use case selection. 317 * doNotReconnect: If set to true, AudioTrack won't automatically recreate the IAudioTrack 318 binder to AudioFlinger. 319 It will return an error instead. The application will recreate 320 the track based on offloading or different channel configuration, etc. 321 * maxRequiredSpeed: For PCM tracks, this creates an appropriate buffer size that will allow 322 * maxRequiredSpeed playback. Values less than 1.0f and greater than 323 * AUDIO_TIMESTRETCH_SPEED_MAX will be clamped. For non-PCM tracks 324 * and direct or offloaded tracks, this parameter is ignored. 325 * selectedDeviceId: Selected device id of the app which initially requested the AudioTrack 326 * to open with a specific device. 327 * threadCanCallJava: Not present in parameter list, and so is fixed at false. 328 */ 329 330 AudioTrack( audio_stream_type_t streamType, 331 uint32_t sampleRate, 332 audio_format_t format, 333 audio_channel_mask_t channelMask, 334 size_t frameCount = 0, 335 audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE, 336 const wp<IAudioTrackCallback>& callback = nullptr, 337 int32_t notificationFrames = 0, 338 audio_session_t sessionId = AUDIO_SESSION_ALLOCATE, 339 transfer_type transferType = TRANSFER_DEFAULT, 340 const audio_offload_info_t *offloadInfo = nullptr, 341 const AttributionSourceState& attributionSource = 342 AttributionSourceState(), 343 const audio_attributes_t* pAttributes = nullptr, 344 bool doNotReconnect = false, 345 float maxRequiredSpeed = 1.0f, 346 audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE); 347 348 /* Creates an audio track and registers it with AudioFlinger. 349 * With this constructor, the track is configured for static buffer mode. 350 * Data to be rendered is passed in a shared memory buffer 351 * identified by the argument sharedBuffer, which should be non-0. 352 * If sharedBuffer is zero, this constructor is equivalent to the previous constructor 353 * but without the ability to specify a non-zero value for the frameCount parameter. 354 * The memory should be initialized to the desired data before calling start(). 355 * The write() method is not supported in this case. 356 * It is recommended to pass a callback function to be notified of playback end by an 357 * EVENT_UNDERRUN event. 358 */ 359 AudioTrack( audio_stream_type_t streamType, 360 uint32_t sampleRate, 361 audio_format_t format, 362 audio_channel_mask_t channelMask, 363 const sp<IMemory>& sharedBuffer, 364 audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE, 365 const wp<IAudioTrackCallback>& callback = nullptr, 366 int32_t notificationFrames = 0, 367 audio_session_t sessionId = AUDIO_SESSION_ALLOCATE, 368 transfer_type transferType = TRANSFER_DEFAULT, 369 const audio_offload_info_t *offloadInfo = nullptr, 370 const AttributionSourceState& attributionSource = 371 AttributionSourceState(), 372 const audio_attributes_t* pAttributes = nullptr, 373 bool doNotReconnect = false, 374 float maxRequiredSpeed = 1.0f); 375 376 /* Terminates the AudioTrack and unregisters it from AudioFlinger. 377 * Also destroys all resources associated with the AudioTrack. 378 */ 379 protected: 380 virtual ~AudioTrack(); 381 public: 382 383 /* Initialize an AudioTrack that was created using the AudioTrack() constructor. 384 * Don't call set() more than once, or after the AudioTrack() constructors that take parameters. 385 * set() is not multi-thread safe. 386 * Returned status (from utils/Errors.h) can be: 387 * - NO_ERROR: successful initialization 388 * - INVALID_OPERATION: AudioTrack is already initialized 389 * - BAD_VALUE: invalid parameter (channelMask, format, sampleRate...) 390 * - NO_INIT: audio server or audio hardware not initialized 391 * If status is not equal to NO_ERROR, don't call any other APIs on this AudioTrack. 392 * If sharedBuffer is non-0, the frameCount parameter is ignored and 393 * replaced by the shared buffer's total allocated size in frame units. 394 * 395 * Parameters not listed in the AudioTrack constructors above: 396 * 397 * threadCanCallJava: Whether callbacks are made from an attached thread and thus can call JNI. 398 * Only set to true when AudioTrack object is used for a java android.media.AudioTrack 399 * in its JNI code. 400 * 401 * Internal state post condition: 402 * (mStreamType == AUDIO_STREAM_DEFAULT) implies this AudioTrack has valid attributes 403 */ 404 status_t set(audio_stream_type_t streamType, 405 uint32_t sampleRate, 406 audio_format_t format, 407 audio_channel_mask_t channelMask, 408 size_t frameCount = 0, 409 audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE, 410 const wp<IAudioTrackCallback>& callback = nullptr, 411 int32_t notificationFrames = 0, 412 const sp<IMemory>& sharedBuffer = 0, 413 bool threadCanCallJava = false, 414 audio_session_t sessionId = AUDIO_SESSION_ALLOCATE, 415 transfer_type transferType = TRANSFER_DEFAULT, 416 const audio_offload_info_t *offloadInfo = nullptr, 417 const AttributionSourceState& attributionSource = 418 AttributionSourceState(), 419 const audio_attributes_t* pAttributes = nullptr, 420 bool doNotReconnect = false, 421 float maxRequiredSpeed = 1.0f, 422 audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE); 423 424 struct SetParams { 425 audio_stream_type_t streamType; 426 uint32_t sampleRate; 427 audio_format_t format; 428 audio_channel_mask_t channelMask; 429 size_t frameCount; 430 audio_output_flags_t flags; 431 wp<IAudioTrackCallback> callback; 432 int32_t notificationFrames; 433 sp<IMemory> sharedBuffer; 434 bool threadCanCallJava; 435 audio_session_t sessionId; 436 transfer_type transferType; 437 // TODO don't take pointers here 438 const audio_offload_info_t *offloadInfo; 439 AttributionSourceState attributionSource; 440 const audio_attributes_t* pAttributes; 441 bool doNotReconnect; 442 float maxRequiredSpeed; 443 audio_port_handle_t selectedDeviceId; 444 }; 445 private: 446 // Note: Consumes parameters set(SetParams & s)447 void set(SetParams& s) { 448 (void)set(s.streamType, s.sampleRate, s.format, s.channelMask, s.frameCount, 449 s.flags, std::move(s.callback), s.notificationFrames, 450 std::move(s.sharedBuffer), s.threadCanCallJava, s.sessionId, 451 s.transferType, s.offloadInfo, std::move(s.attributionSource), 452 s.pAttributes, s.doNotReconnect, s.maxRequiredSpeed, s.selectedDeviceId); 453 } 454 void onFirstRef() override; 455 public: 456 typedef void (*legacy_callback_t)(int event, void* user, void* info); 457 // FIXME(b/169889714): Vendor code depends on the old method signature at link time 458 status_t set(audio_stream_type_t streamType, 459 uint32_t sampleRate, 460 audio_format_t format, 461 uint32_t channelMask, 462 size_t frameCount = 0, 463 audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE, 464 legacy_callback_t cbf = nullptr, 465 void* user = nullptr, 466 int32_t notificationFrames = 0, 467 const sp<IMemory>& sharedBuffer = 0, 468 bool threadCanCallJava = false, 469 audio_session_t sessionId = AUDIO_SESSION_ALLOCATE, 470 transfer_type transferType = TRANSFER_DEFAULT, 471 const audio_offload_info_t *offloadInfo = nullptr, 472 uid_t uid = AUDIO_UID_INVALID, 473 pid_t pid = -1, 474 const audio_attributes_t* pAttributes = nullptr, 475 bool doNotReconnect = false, 476 float maxRequiredSpeed = 1.0f, 477 audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE); 478 479 /* Result of constructing the AudioTrack. This must be checked for successful initialization 480 * before using any AudioTrack API (except for set()), because using 481 * an uninitialized AudioTrack produces undefined results. 482 * See set() method above for possible return codes. 483 */ initCheck()484 status_t initCheck() const { return mStatus; } 485 486 /* Returns this track's estimated latency in milliseconds. 487 * This includes the latency due to AudioTrack buffer size, AudioMixer (if any) 488 * and audio hardware driver. 489 */ 490 uint32_t latency(); 491 492 /* Returns the number of application-level buffer underruns 493 * since the AudioTrack was created. 494 */ 495 uint32_t getUnderrunCount() const; 496 497 /* getters, see constructors and set() */ 498 499 audio_stream_type_t streamType() const; format()500 audio_format_t format() const { return mFormat; } 501 502 /* Return frame size in bytes, which for linear PCM is 503 * channelCount * (bit depth per channel / 8). 504 * channelCount is determined from channelMask, and bit depth comes from format. 505 * For non-linear formats, the frame size is typically 1 byte. 506 */ frameSize()507 size_t frameSize() const { return mFrameSize; } 508 channelCount()509 uint32_t channelCount() const { return mChannelCount; } frameCount()510 size_t frameCount() const { return mFrameCount; } channelMask()511 audio_channel_mask_t channelMask() const { return mChannelMask; } 512 513 /* 514 * Return the period of the notification callback in frames. 515 * This value is set when the AudioTrack is constructed. 516 * It can be modified if the AudioTrack is rerouted. 517 */ getNotificationPeriodInFrames()518 uint32_t getNotificationPeriodInFrames() const { return mNotificationFramesAct; } 519 520 /* Return effective size of audio buffer that an application writes to 521 * or a negative error if the track is uninitialized. 522 */ 523 ssize_t getBufferSizeInFrames(); 524 525 /* Returns the buffer duration in microseconds at current playback rate. 526 */ 527 status_t getBufferDurationInUs(int64_t *duration); 528 529 /* Set the effective size of audio buffer that an application writes to. 530 * This is used to determine the amount of available room in the buffer, 531 * which determines when a write will block. 532 * This allows an application to raise and lower the audio latency. 533 * The requested size may be adjusted so that it is 534 * greater or equal to the absolute minimum and 535 * less than or equal to the getBufferCapacityInFrames(). 536 * It may also be adjusted slightly for internal reasons. 537 * 538 * Return the final size or a negative value (NO_INIT) if the track is uninitialized. 539 */ 540 ssize_t setBufferSizeInFrames(size_t size); 541 542 /* Returns the start threshold on the buffer for audio streaming 543 * or a negative value if the AudioTrack is not initialized. 544 */ 545 ssize_t getStartThresholdInFrames() const; 546 547 /* Sets the start threshold in frames on the buffer for audio streaming. 548 * 549 * May be clamped internally. Returns the actual value set, or a negative 550 * value if the AudioTrack is not initialized or if the input 551 * is zero or greater than INT_MAX. 552 */ 553 ssize_t setStartThresholdInFrames(size_t startThresholdInFrames); 554 555 /* Return the static buffer specified in constructor or set(), or 0 for streaming mode */ sharedBuffer()556 sp<IMemory> sharedBuffer() const { return mSharedBuffer; } 557 558 /* 559 * return metrics information for the current track. 560 */ 561 status_t getMetrics(mediametrics::Item * &item); 562 563 /* 564 * Set name of API that is using this object. 565 * For example "aaudio" or "opensles". 566 * This may be logged or reported as part of MediaMetrics. 567 */ setCallerName(const std::string & name)568 void setCallerName(const std::string &name) { 569 mCallerName = name; 570 } 571 getCallerName()572 std::string getCallerName() const { 573 return mCallerName; 574 }; 575 576 /* After it's created the track is not active. Call start() to 577 * make it active. If set, the callback will start being called. 578 * If the track was previously paused, volume is ramped up over the first mix buffer. 579 */ 580 status_t start(); 581 582 /* Stop a track. 583 * In static buffer mode, the track is stopped immediately. 584 * In streaming mode, the callback will cease being called. Note that obtainBuffer() still 585 * works and will fill up buffers until the pool is exhausted, and then will return WOULD_BLOCK. 586 * In streaming mode the stop does not occur immediately: any data remaining in the buffer 587 * is first drained, mixed, and output, and only then is the track marked as stopped. 588 */ 589 void stop(); 590 bool stopped() const; 591 592 /* Call stop() and then wait for all of the callbacks to return. 593 * It is safe to call this if stop() or pause() has already been called. 594 * 595 * This function is called from the destructor. But since AudioTrack 596 * is ref counted, the destructor may be called later than desired. 597 * This can be called explicitly as part of closing an AudioTrack 598 * if you want to be certain that callbacks have completely finished. 599 * 600 * This is not thread safe and should only be called from one thread, 601 * ideally as the AudioTrack is being closed. 602 */ 603 void stopAndJoinCallbacks(); 604 605 /* Flush a stopped or paused track. All previously buffered data is discarded immediately. 606 * This has the effect of draining the buffers without mixing or output. 607 * Flush is intended for streaming mode, for example before switching to non-contiguous content. 608 * This function is a no-op if the track is not stopped or paused, or uses a static buffer. 609 */ 610 void flush(); 611 612 /* Pause a track. After pause, the callback will cease being called and 613 * obtainBuffer returns WOULD_BLOCK. Note that obtainBuffer() still works 614 * and will fill up buffers until the pool is exhausted. 615 * Volume is ramped down over the next mix buffer following the pause request, 616 * and then the track is marked as paused. It can be resumed with ramp up by start(). 617 */ 618 void pause(); 619 620 /* Pause and wait (with timeout) for the audio track to ramp to silence. 621 * 622 * \param timeout is the time limit to wait before returning. 623 * A negative number is treated as 0. 624 * \return true if the track is ramped to silence, false if the timeout occurred. 625 */ 626 bool pauseAndWait(const std::chrono::milliseconds& timeout); 627 628 /* Set volume for this track, mostly used for games' sound effects 629 * left and right volumes. Levels must be >= 0.0 and <= 1.0. 630 * This is the older API. New applications should use setVolume(float) when possible. 631 */ 632 status_t setVolume(float left, float right); 633 634 /* Set volume for all channels. This is the preferred API for new applications, 635 * especially for multi-channel content. 636 */ 637 status_t setVolume(float volume); 638 639 /* Set the send level for this track. An auxiliary effect should be attached 640 * to the track with attachEffect(). Level must be >= 0.0 and <= 1.0. 641 */ 642 status_t setAuxEffectSendLevel(float level); 643 void getAuxEffectSendLevel(float* level) const; 644 645 /* Set source sample rate for this track in Hz, mostly used for games' sound effects. 646 * Zero is not permitted. 647 */ 648 status_t setSampleRate(uint32_t sampleRate); 649 650 /* Return current source sample rate in Hz. 651 * If specified as zero in constructor or set(), this will be the sink sample rate. 652 */ 653 uint32_t getSampleRate() const; 654 655 /* Return the original source sample rate in Hz. This corresponds to the sample rate 656 * if playback rate had normal speed and pitch. 657 */ 658 uint32_t getOriginalSampleRate() const; 659 660 /* Return the sample rate from the AudioFlinger output thread. */ 661 uint32_t getHalSampleRate() const; 662 663 /* Return the channel count from the AudioFlinger output thread. */ 664 uint32_t getHalChannelCount() const; 665 666 /* Return the HAL format from the AudioFlinger output thread. */ 667 audio_format_t getHalFormat() const; 668 669 /* Sets the Dual Mono mode presentation on the output device. */ 670 status_t setDualMonoMode(audio_dual_mono_mode_t mode); 671 672 /* Returns the Dual Mono mode presentation setting. */ 673 status_t getDualMonoMode(audio_dual_mono_mode_t* mode) const; 674 675 /* Sets the Audio Description Mix level in dB. */ 676 status_t setAudioDescriptionMixLevel(float leveldB); 677 678 /* Returns the Audio Description Mix level in dB. */ 679 status_t getAudioDescriptionMixLevel(float* leveldB) const; 680 681 /* Set source playback rate for timestretch 682 * 1.0 is normal speed: < 1.0 is slower, > 1.0 is faster 683 * 1.0 is normal pitch: < 1.0 is lower pitch, > 1.0 is higher pitch 684 * 685 * AUDIO_TIMESTRETCH_SPEED_MIN <= speed <= AUDIO_TIMESTRETCH_SPEED_MAX 686 * AUDIO_TIMESTRETCH_PITCH_MIN <= pitch <= AUDIO_TIMESTRETCH_PITCH_MAX 687 * 688 * Speed increases the playback rate of media, but does not alter pitch. 689 * Pitch increases the "tonal frequency" of media, but does not affect the playback rate. 690 */ 691 status_t setPlaybackRate(const AudioPlaybackRate &playbackRate); 692 693 /* Return current playback rate */ 694 const AudioPlaybackRate& getPlaybackRate(); 695 696 /* Enables looping and sets the start and end points of looping. 697 * Only supported for static buffer mode. 698 * 699 * Parameters: 700 * 701 * loopStart: loop start in frames relative to start of buffer. 702 * loopEnd: loop end in frames relative to start of buffer. 703 * loopCount: number of loops to execute. Calling setLoop() with loopCount == 0 cancels any 704 * pending or active loop. loopCount == -1 means infinite looping. 705 * 706 * For proper operation the following condition must be respected: 707 * loopCount != 0 implies 0 <= loopStart < loopEnd <= frameCount(). 708 * 709 * If the loop period (loopEnd - loopStart) is too small for the implementation to support, 710 * setLoop() will return BAD_VALUE. loopCount must be >= -1. 711 * 712 */ 713 status_t setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount); 714 715 /* Sets marker position. When playback reaches the number of frames specified, a callback with 716 * event type EVENT_MARKER is called. Calling setMarkerPosition with marker == 0 cancels marker 717 * notification callback. To set a marker at a position which would compute as 0, 718 * a workaround is to set the marker at a nearby position such as ~0 or 1. 719 * If the AudioTrack has been opened with no callback function associated, the operation will 720 * fail. 721 * 722 * Parameters: 723 * 724 * marker: marker position expressed in wrapping (overflow) frame units, 725 * like the return value of getPosition(). 726 * 727 * Returned status (from utils/Errors.h) can be: 728 * - NO_ERROR: successful operation 729 * - INVALID_OPERATION: the AudioTrack has no callback installed. 730 */ 731 status_t setMarkerPosition(uint32_t marker); 732 status_t getMarkerPosition(uint32_t *marker) const; 733 734 /* Sets position update period. Every time the number of frames specified has been played, 735 * a callback with event type EVENT_NEW_POS is called. 736 * Calling setPositionUpdatePeriod with updatePeriod == 0 cancels new position notification 737 * callback. 738 * If the AudioTrack has been opened with no callback function associated, the operation will 739 * fail. 740 * Extremely small values may be rounded up to a value the implementation can support. 741 * 742 * Parameters: 743 * 744 * updatePeriod: position update notification period expressed in frames. 745 * 746 * Returned status (from utils/Errors.h) can be: 747 * - NO_ERROR: successful operation 748 * - INVALID_OPERATION: the AudioTrack has no callback installed. 749 */ 750 status_t setPositionUpdatePeriod(uint32_t updatePeriod); 751 status_t getPositionUpdatePeriod(uint32_t *updatePeriod) const; 752 753 /* Sets playback head position. 754 * Only supported for static buffer mode. 755 * 756 * Parameters: 757 * 758 * position: New playback head position in frames relative to start of buffer. 759 * 0 <= position <= frameCount(). Note that end of buffer is permitted, 760 * but will result in an immediate underrun if started. 761 * 762 * Returned status (from utils/Errors.h) can be: 763 * - NO_ERROR: successful operation 764 * - INVALID_OPERATION: the AudioTrack is not stopped or paused, or is streaming mode. 765 * - BAD_VALUE: The specified position is beyond the number of frames present in AudioTrack 766 * buffer 767 */ 768 status_t setPosition(uint32_t position); 769 770 /* Return the total number of frames played since playback start. 771 * The counter will wrap (overflow) periodically, e.g. every ~27 hours at 44.1 kHz. 772 * It is reset to zero by flush(), reload(), and stop(). 773 * 774 * Parameters: 775 * 776 * position: Address where to return play head position. 777 * 778 * Returned status (from utils/Errors.h) can be: 779 * - NO_ERROR: successful operation 780 * - BAD_VALUE: position is NULL 781 */ 782 status_t getPosition(uint32_t *position); 783 784 /* For static buffer mode only, this returns the current playback position in frames 785 * relative to start of buffer. It is analogous to the position units used by 786 * setLoop() and setPosition(). After underrun, the position will be at end of buffer. 787 */ 788 status_t getBufferPosition(uint32_t *position); 789 790 /* Forces AudioTrack buffer full condition. When playing a static buffer, this method avoids 791 * rewriting the buffer before restarting playback after a stop. 792 * This method must be called with the AudioTrack in paused or stopped state. 793 * Not allowed in streaming mode. 794 * 795 * Returned status (from utils/Errors.h) can be: 796 * - NO_ERROR: successful operation 797 * - INVALID_OPERATION: the AudioTrack is not stopped or paused, or is streaming mode. 798 */ 799 status_t reload(); 800 801 /** 802 * @param transferType 803 * @return text string that matches the enum name 804 */ 805 static const char * convertTransferToText(transfer_type transferType); 806 807 /* Returns a handle on the audio output used by this AudioTrack. 808 * 809 * Parameters: 810 * none. 811 * 812 * Returned value: 813 * handle on audio hardware output, or AUDIO_IO_HANDLE_NONE if the 814 * track needed to be re-created but that failed 815 */ 816 audio_io_handle_t getOutput() const; 817 818 /* Selects the audio device to use for output of this AudioTrack. A value of 819 * AUDIO_PORT_HANDLE_NONE indicates default (AudioPolicyManager) routing. 820 * 821 * Parameters: 822 * The device ID of the selected device (as returned by the AudioDevicesManager API). 823 * 824 * Returned value: 825 * - NO_ERROR: successful operation 826 * TODO: what else can happen here? 827 */ 828 status_t setOutputDevice(audio_port_handle_t deviceId); 829 830 /* Returns the ID of the audio device selected for this AudioTrack. 831 * A value of AUDIO_PORT_HANDLE_NONE indicates default (AudioPolicyManager) routing. 832 * 833 * Parameters: 834 * none. 835 */ 836 audio_port_handle_t getOutputDevice(); 837 838 /* Returns the IDs of the audio devices actually used by the output to which this AudioTrack is 839 * attached. 840 * When the AudioTrack is inactive, the device ID returned can be either: 841 * - An empty vector if the AudioTrack is not attached to any output. 842 * - The device IDs used before paused or stopped. 843 * - The device ID selected by audio policy manager of setOutputDevice() if the AudioTrack 844 * has not been started yet. 845 * 846 * Parameters: 847 * none. 848 */ 849 DeviceIdVector getRoutedDeviceIds(); 850 851 /* Returns the unique session ID associated with this track. 852 * 853 * Parameters: 854 * none. 855 * 856 * Returned value: 857 * AudioTrack session ID. 858 */ getSessionId()859 audio_session_t getSessionId() const { return mSessionId; } 860 861 /* Attach track auxiliary output to specified effect. Use effectId = 0 862 * to detach track from effect. 863 * 864 * Parameters: 865 * 866 * effectId: effectId obtained from AudioEffect::id(). 867 * 868 * Returned status (from utils/Errors.h) can be: 869 * - NO_ERROR: successful operation 870 * - INVALID_OPERATION: the effect is not an auxiliary effect. 871 * - BAD_VALUE: The specified effect ID is invalid 872 */ 873 status_t attachAuxEffect(int effectId); 874 875 /* Public API for TRANSFER_OBTAIN mode. 876 * Obtains a buffer of up to "audioBuffer->frameCount" empty slots for frames. 877 * After filling these slots with data, the caller should release them with releaseBuffer(). 878 * If the track buffer is not full, obtainBuffer() returns as many contiguous 879 * [empty slots for] frames as are available immediately. 880 * 881 * If nonContig is non-NULL, it is an output parameter that will be set to the number of 882 * additional non-contiguous frames that are predicted to be available immediately, 883 * if the client were to release the first frames and then call obtainBuffer() again. 884 * This value is only a prediction, and needs to be confirmed. 885 * It will be set to zero for an error return. 886 * 887 * If the track buffer is full and track is stopped, obtainBuffer() returns WOULD_BLOCK 888 * regardless of the value of waitCount. 889 * If the track buffer is full and track is not stopped, obtainBuffer() blocks with a 890 * maximum timeout based on waitCount; see chart below. 891 * Buffers will be returned until the pool 892 * is exhausted, at which point obtainBuffer() will either block 893 * or return WOULD_BLOCK depending on the value of the "waitCount" 894 * parameter. 895 * 896 * Interpretation of waitCount: 897 * +n limits wait time to n * WAIT_PERIOD_MS, 898 * -1 causes an (almost) infinite wait time, 899 * 0 non-blocking. 900 * 901 * Buffer fields 902 * On entry: 903 * frameCount number of [empty slots for] frames requested 904 * size ignored 905 * raw ignored 906 * sequence ignored 907 * After error return: 908 * frameCount 0 909 * size 0 910 * raw undefined 911 * sequence undefined 912 * After successful return: 913 * frameCount actual number of [empty slots for] frames available, <= number requested 914 * size actual number of bytes available 915 * raw pointer to the buffer 916 * sequence IAudioTrack instance sequence number, as of obtainBuffer() 917 */ 918 status_t obtainBuffer(Buffer* audioBuffer, int32_t waitCount, 919 size_t *nonContig = NULL); 920 921 private: 922 /* If nonContig is non-NULL, it is an output parameter that will be set to the number of 923 * additional non-contiguous frames that are predicted to be available immediately, 924 * if the client were to release the first frames and then call obtainBuffer() again. 925 * This value is only a prediction, and needs to be confirmed. 926 * It will be set to zero for an error return. 927 * FIXME We could pass an array of Buffers instead of only one Buffer to obtainBuffer(), 928 * in case the requested amount of frames is in two or more non-contiguous regions. 929 * FIXME requested and elapsed are both relative times. Consider changing to absolute time. 930 */ 931 status_t obtainBuffer(Buffer* audioBuffer, const struct timespec *requested, 932 struct timespec *elapsed = NULL, size_t *nonContig = NULL); 933 public: 934 935 /* Public API for TRANSFER_OBTAIN mode. 936 * Release a filled buffer of frames for AudioFlinger to process. 937 * 938 * Buffer fields: 939 * frameCount currently ignored but recommend to set to actual number of frames filled 940 * size actual number of bytes filled, must be multiple of frameSize 941 * raw ignored 942 */ 943 void releaseBuffer(const Buffer* audioBuffer); 944 945 /* As a convenience we provide a write() interface to the audio buffer. 946 * Input parameter 'size' is in byte units. 947 * This is implemented on top of obtainBuffer/releaseBuffer. For best 948 * performance use callbacks. Returns actual number of bytes written >= 0, 949 * or one of the following negative status codes: 950 * INVALID_OPERATION AudioTrack is configured for static buffer or streaming mode 951 * BAD_VALUE size is invalid 952 * WOULD_BLOCK when obtainBuffer() returns same, or 953 * AudioTrack was stopped during the write 954 * DEAD_OBJECT when AudioFlinger dies or the output device changes and 955 * the track cannot be automatically restored. 956 * The application needs to recreate the AudioTrack 957 * because the audio device changed or AudioFlinger died. 958 * This typically occurs for direct or offload tracks 959 * or if mDoNotReconnect is true. 960 * or any other error code returned by IAudioTrack::start() or restoreTrack_l(). 961 * Default behavior is to only return when all data has been transferred. Set 'blocking' to 962 * false for the method to return immediately without waiting to try multiple times to write 963 * the full content of the buffer. 964 */ 965 ssize_t write(const void* buffer, size_t size, bool blocking = true); 966 967 /* 968 * Dumps the state of an audio track. 969 * Not a general-purpose API; intended only for use by media player service to dump its tracks. 970 */ 971 status_t dump(int fd, const Vector<String16>& args) const; 972 973 /* 974 * Return the total number of frames which AudioFlinger desired but were unavailable, 975 * and thus which resulted in an underrun. Reset to zero by stop(). 976 */ 977 uint32_t getUnderrunFrames() const; 978 979 /* Get the flags */ getFlags()980 audio_output_flags_t getFlags() const { AutoMutex _l(mLock); return mFlags; } 981 982 /* Set parameters - only possible when using direct output */ 983 status_t setParameters(const String8& keyValuePairs); 984 985 /* Sets the volume shaper object */ 986 media::VolumeShaper::Status applyVolumeShaper( 987 const sp<media::VolumeShaper::Configuration>& configuration, 988 const sp<media::VolumeShaper::Operation>& operation); 989 990 /* Gets the volume shaper state */ 991 sp<media::VolumeShaper::State> getVolumeShaperState(int id); 992 993 /* Selects the presentation (if available) */ 994 status_t selectPresentation(int presentationId, int programId); 995 996 /* Get parameters */ 997 String8 getParameters(const String8& keys); 998 999 /* Poll for a timestamp on demand. 1000 * Use if EVENT_NEW_TIMESTAMP is not delivered often enough for your needs, 1001 * or if you need to get the most recent timestamp outside of the event callback handler. 1002 * Caution: calling this method too often may be inefficient; 1003 * if you need a high resolution mapping between frame position and presentation time, 1004 * consider implementing that at application level, based on the low resolution timestamps. 1005 * Returns NO_ERROR if timestamp is valid. 1006 * WOULD_BLOCK if called in STOPPED or FLUSHED state, or if called immediately after 1007 * start/ACTIVE, when the number of frames consumed is less than the 1008 * overall hardware latency to physical output. In WOULD_BLOCK cases, 1009 * one might poll again, or use getPosition(), or use 0 position and 1010 * current time for the timestamp. 1011 * DEAD_OBJECT if AudioFlinger dies or the output device changes and 1012 * the track cannot be automatically restored. 1013 * The application needs to recreate the AudioTrack 1014 * because the audio device changed or AudioFlinger died. 1015 * This typically occurs for direct or offload tracks 1016 * or if mDoNotReconnect is true. 1017 * INVALID_OPERATION wrong state, or some other error. 1018 * 1019 * The timestamp parameter is undefined on return, if status is not NO_ERROR. 1020 */ 1021 status_t getTimestamp(AudioTimestamp& timestamp); 1022 private: 1023 status_t getTimestamp_l(AudioTimestamp& timestamp); 1024 public: 1025 1026 /* Return the extended timestamp, with additional timebase info and improved drain behavior. 1027 * 1028 * This is similar to the AudioTrack.java API: 1029 * getTimestamp(@NonNull AudioTimestamp timestamp, @AudioTimestamp.Timebase int timebase) 1030 * 1031 * Some differences between this method and the getTimestamp(AudioTimestamp& timestamp) method 1032 * 1033 * 1. stop() by itself does not reset the frame position. 1034 * A following start() resets the frame position to 0. 1035 * 2. flush() by itself does not reset the frame position. 1036 * The frame position advances by the number of frames flushed, 1037 * when the first frame after flush reaches the audio sink. 1038 * 3. BOOTTIME clock offsets are provided to help synchronize with 1039 * non-audio streams, e.g. sensor data. 1040 * 4. Position is returned with 64 bits of resolution. 1041 * 1042 * Parameters: 1043 * timestamp: A pointer to the caller allocated ExtendedTimestamp. 1044 * 1045 * Returns NO_ERROR on success; timestamp is filled with valid data. 1046 * BAD_VALUE if timestamp is NULL. 1047 * WOULD_BLOCK if called immediately after start() when the number 1048 * of frames consumed is less than the 1049 * overall hardware latency to physical output. In WOULD_BLOCK cases, 1050 * one might poll again, or use getPosition(), or use 0 position and 1051 * current time for the timestamp. 1052 * If WOULD_BLOCK is returned, the timestamp is still 1053 * modified with the LOCATION_CLIENT portion filled. 1054 * DEAD_OBJECT if AudioFlinger dies or the output device changes and 1055 * the track cannot be automatically restored. 1056 * The application needs to recreate the AudioTrack 1057 * because the audio device changed or AudioFlinger died. 1058 * This typically occurs for direct or offloaded tracks 1059 * or if mDoNotReconnect is true. 1060 * INVALID_OPERATION if called on a offloaded or direct track. 1061 * Use getTimestamp(AudioTimestamp& timestamp) instead. 1062 */ 1063 status_t getTimestamp(ExtendedTimestamp *timestamp); 1064 private: 1065 status_t getTimestamp_l(ExtendedTimestamp *timestamp); 1066 public: 1067 1068 /* Add an AudioDeviceCallback. The caller will be notified when the audio device to which this 1069 * AudioTrack is routed is updated. 1070 * Replaces any previously installed callback. 1071 * Parameters: 1072 * callback: The callback interface 1073 * Returns NO_ERROR if successful. 1074 * INVALID_OPERATION if the same callback is already installed. 1075 * NO_INIT or PREMISSION_DENIED if AudioFlinger service is not reachable 1076 * BAD_VALUE if the callback is NULL 1077 */ 1078 status_t addAudioDeviceCallback(const sp<AudioSystem::AudioDeviceCallback>& callback); 1079 1080 /* remove an AudioDeviceCallback. 1081 * Parameters: 1082 * callback: The callback interface 1083 * Returns NO_ERROR if successful. 1084 * INVALID_OPERATION if the callback is not installed 1085 * BAD_VALUE if the callback is NULL 1086 */ 1087 status_t removeAudioDeviceCallback( 1088 const sp<AudioSystem::AudioDeviceCallback>& callback); 1089 1090 // AudioSystem::AudioDeviceCallback> virtuals 1091 virtual void onAudioDeviceUpdate(audio_io_handle_t audioIo, 1092 const DeviceIdVector& deviceIds); 1093 1094 /* Obtain the pending duration in milliseconds for playback of pure PCM 1095 * (mixable without embedded timing) data remaining in AudioTrack. 1096 * 1097 * This is used to estimate the drain time for the client-server buffer 1098 * so the choice of ExtendedTimestamp::LOCATION_SERVER is default. 1099 * One may optionally request to find the duration to play through the HAL 1100 * by specifying a location ExtendedTimestamp::LOCATION_KERNEL; however, 1101 * INVALID_OPERATION may be returned if the kernel location is unavailable. 1102 * 1103 * Returns NO_ERROR if successful. 1104 * INVALID_OPERATION if ExtendedTimestamp::LOCATION_KERNEL cannot be obtained 1105 * or the AudioTrack does not contain pure PCM data. 1106 * BAD_VALUE if msec is nullptr or location is invalid. 1107 */ 1108 status_t pendingDuration(int32_t *msec, 1109 ExtendedTimestamp::Location location = ExtendedTimestamp::LOCATION_SERVER); 1110 1111 /* hasStarted() is used to determine if audio is now audible at the device after 1112 * a start() command. The underlying implementation checks a nonzero timestamp position 1113 * or increment for the audible assumption. 1114 * 1115 * hasStarted() returns true if the track has been started() and audio is audible 1116 * and no subsequent pause() or flush() has been called. Immediately after pause() or 1117 * flush() hasStarted() will return false. 1118 * 1119 * If stop() has been called, hasStarted() will return true if audio is still being 1120 * delivered or has finished delivery (even if no audio was written) for both offloaded 1121 * and normal tracks. This property removes a race condition in checking hasStarted() 1122 * for very short clips, where stop() must be called to finish drain. 1123 * 1124 * In all cases, hasStarted() may turn false briefly after a subsequent start() is called 1125 * until audio becomes audible again. 1126 */ 1127 bool hasStarted(); // not const 1128 isPlaying()1129 bool isPlaying() { 1130 AutoMutex lock(mLock); 1131 return isPlaying_l(); 1132 } isPlaying_l()1133 bool isPlaying_l() { 1134 return mState == STATE_ACTIVE || mState == STATE_STOPPING; 1135 } 1136 1137 /* Get the unique port ID assigned to this AudioTrack instance by audio policy manager. 1138 * The ID is unique across all audioserver clients and can change during the life cycle 1139 * of a given AudioTrack instance if the connection to audioserver is restored. 1140 */ getPortId()1141 audio_port_handle_t getPortId() const { return mPortId; }; 1142 1143 /* Sets the LogSessionId field which is used for metrics association of 1144 * this object with other objects. A nullptr or empty string clears 1145 * the logSessionId. 1146 */ 1147 void setLogSessionId(const char *logSessionId); 1148 1149 /* Sets the playerIId field to associate the AudioTrack with an interface managed by 1150 * AudioService. 1151 * 1152 * If this value is not set, then the playerIId is reported as -1 1153 * (not associated with an AudioService player interface). 1154 * 1155 * For metrics purposes, we keep the playerIId association in the native 1156 * client AudioTrack to improve the robustness under track restoration. 1157 */ 1158 void setPlayerIId(int playerIId); 1159 setAudioTrackCallback(const sp<media::IAudioTrackCallback> & callback)1160 void setAudioTrackCallback(const sp<media::IAudioTrackCallback>& callback) { 1161 mAudioTrackCallback->setAudioTrackCallback(callback); 1162 } 1163 private: 1164 void triggerPortIdUpdate_l(); 1165 1166 protected: 1167 /* copying audio tracks is not allowed */ 1168 AudioTrack(const AudioTrack& other); 1169 AudioTrack& operator = (const AudioTrack& other); 1170 1171 /* a small internal class to handle the callback */ 1172 class AudioTrackThread : public Thread 1173 { 1174 public: 1175 explicit AudioTrackThread(AudioTrack& receiver); 1176 1177 // Do not call Thread::requestExitAndWait() without first calling requestExit(). 1178 // Thread::requestExitAndWait() is not virtual, and the implementation doesn't do enough. 1179 virtual void requestExit(); 1180 1181 void pause(); // suspend thread from execution at next loop boundary 1182 void resume(); // allow thread to execute, if not requested to exit 1183 void wake(); // wake to handle changed notification conditions. 1184 1185 private: 1186 void pauseInternal(nsecs_t ns = 0LL); 1187 // like pause(), but only used internally within thread 1188 1189 friend class AudioTrack; 1190 virtual bool threadLoop(); 1191 AudioTrack& mReceiver; 1192 virtual ~AudioTrackThread(); 1193 Mutex mMyLock; // Thread::mLock is private 1194 Condition mMyCond; // Thread::mThreadExitedCondition is private 1195 bool mPaused; // whether thread is requested to pause at next loop entry 1196 bool mPausedInt; // whether thread internally requests pause 1197 nsecs_t mPausedNs; // if mPausedInt then associated timeout, otherwise ignored 1198 bool mIgnoreNextPausedInt; // skip any internal pause and go immediately 1199 // to processAudioBuffer() as state may have changed 1200 // since pause time calculated. 1201 }; 1202 1203 // body of AudioTrackThread::threadLoop() 1204 // returns the maximum amount of time before we would like to run again, where: 1205 // 0 immediately 1206 // > 0 no later than this many nanoseconds from now 1207 // NS_WHENEVER still active but no particular deadline 1208 // NS_INACTIVE inactive so don't run again until re-started 1209 // NS_NEVER never again 1210 static const nsecs_t NS_WHENEVER = -1, NS_INACTIVE = -2, NS_NEVER = -3; 1211 nsecs_t processAudioBuffer(); 1212 1213 // caller must hold lock on mLock for all _l methods 1214 1215 void updateLatency_l(); // updates mAfLatency and mLatency from AudioSystem cache 1216 1217 status_t createTrack_l(); 1218 1219 // can only be called when mState != STATE_ACTIVE 1220 void flush_l(); 1221 1222 void setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount); 1223 1224 // FIXME enum is faster than strcmp() for parameter 'from' 1225 status_t restoreTrack_l(const char *from, bool forceRestore = false); 1226 1227 uint32_t getUnderrunCount_l() const; 1228 1229 bool isOffloaded() const; 1230 bool isDirect() const; 1231 bool isOffloadedOrDirect() const; 1232 isOffloaded_l()1233 bool isOffloaded_l() const 1234 { return (mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0; } 1235 isOffloadedOrDirect_l()1236 bool isOffloadedOrDirect_l() const 1237 { return (mFlags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD| 1238 AUDIO_OUTPUT_FLAG_DIRECT)) != 0; } 1239 isDirect_l()1240 bool isDirect_l() const 1241 { return (mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0; } 1242 isAfTrackOffloadedOrDirect_l()1243 bool isAfTrackOffloadedOrDirect_l() const 1244 { return isOffloadedOrDirect_l() || 1245 (mAfTrackFlags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD| 1246 AUDIO_OUTPUT_FLAG_DIRECT)) != 0; } 1247 1248 // pure pcm data is mixable (which excludes HW_AV_SYNC, with embedded timing) isPurePcmData_l()1249 bool isPurePcmData_l() const 1250 { return audio_is_linear_pcm(mFormat) 1251 && (mAttributes.flags & AUDIO_FLAG_HW_AV_SYNC) == 0; } 1252 1253 // increment mPosition by the delta of mServer, and return new value of mPosition 1254 Modulo<uint32_t> updateAndGetPosition_l(); 1255 1256 // check sample rate and speed is compatible with AudioTrack 1257 bool isSampleRateSpeedAllowed_l(uint32_t sampleRate, float speed); 1258 1259 void restartIfDisabled(); 1260 1261 void updateRoutedDeviceIds_l(); 1262 1263 /* Sets the Dual Mono mode presentation on the output device. */ 1264 status_t setDualMonoMode_l(audio_dual_mono_mode_t mode); 1265 1266 /* Sets the Audio Description Mix level in dB. */ 1267 status_t setAudioDescriptionMixLevel_l(float leveldB); 1268 1269 // Next 4 fields may be changed if IAudioTrack is re-created, but always != 0 1270 sp<media::IAudioTrack> mAudioTrack; 1271 sp<IMemory> mCblkMemory; 1272 audio_track_cblk_t* mCblk; // re-load after mLock.unlock() 1273 audio_io_handle_t mOutput = AUDIO_IO_HANDLE_NONE; // from AudioSystem::getOutputForAttr() 1274 1275 // A copy of shared memory and proxy between obtainBuffer and releaseBuffer to keep the 1276 // shared memory valid when processing data. 1277 sp<IMemory> mCblkMemoryObtainBufferRef GUARDED_BY(mLock); 1278 sp<AudioTrackClientProxy> mProxyObtainBufferRef GUARDED_BY(mLock); 1279 1280 sp<AudioTrackThread> mAudioTrackThread; 1281 bool mThreadCanCallJava; 1282 1283 float mVolume[2]; 1284 float mSendLevel; 1285 mutable uint32_t mSampleRate; // mutable because getSampleRate() can update it 1286 uint32_t mOriginalSampleRate; 1287 AudioPlaybackRate mPlaybackRate; 1288 float mMaxRequiredSpeed; // use PCM buffer size to allow this speed 1289 1290 // Corresponds to current IAudioTrack, value is reported back by AudioFlinger to the client. 1291 // This allocated buffer size is maintained by the proxy. 1292 size_t mFrameCount; // maximum size of buffer 1293 1294 size_t mReqFrameCount; // frame count to request the first or next time 1295 // a new IAudioTrack is needed, non-decreasing 1296 1297 // The following AudioFlinger server-side values are cached in createTrack_l(). 1298 // These values can be used for informational purposes until the track is invalidated, 1299 // whereupon restoreTrack_l() calls createTrack_l() to update the values. 1300 uint32_t mAfLatency; // AudioFlinger latency in ms 1301 size_t mAfFrameCount; // AudioFlinger frame count 1302 uint32_t mAfSampleRate; // AudioFlinger sample rate 1303 uint32_t mAfChannelCount; // AudioFlinger channel count 1304 audio_format_t mAfFormat; // AudioFlinger format 1305 audio_output_flags_t mAfTrackFlags; // AudioFlinger track flags 1306 1307 // constant after constructor or set() 1308 audio_format_t mFormat; // as requested by client, not forced to 16-bit 1309 // mOriginalStreamType == AUDIO_STREAM_DEFAULT implies this AudioTrack has valid attributes 1310 audio_stream_type_t mOriginalStreamType = AUDIO_STREAM_DEFAULT; 1311 audio_stream_type_t mStreamType = AUDIO_STREAM_DEFAULT; 1312 uint32_t mChannelCount; 1313 audio_channel_mask_t mChannelMask; 1314 sp<IMemory> mSharedBuffer; 1315 transfer_type mTransfer; 1316 audio_offload_info_t mOffloadInfoCopy; 1317 audio_attributes_t mAttributes = AUDIO_ATTRIBUTES_INITIALIZER; 1318 1319 size_t mFrameSize; // frame size in bytes 1320 1321 status_t mStatus = NO_INIT; 1322 1323 // can change dynamically when IAudioTrack invalidated 1324 uint32_t mLatency; // in ms 1325 1326 // Indicates the current track state. Protected by mLock. 1327 enum State { 1328 STATE_ACTIVE, 1329 STATE_STOPPED, 1330 STATE_PAUSED, 1331 STATE_PAUSED_STOPPING, 1332 STATE_FLUSHED, 1333 STATE_STOPPING, 1334 } mState = STATE_STOPPED; 1335 stateToString(State state)1336 static constexpr const char *stateToString(State state) 1337 { 1338 switch (state) { 1339 case STATE_ACTIVE: return "STATE_ACTIVE"; 1340 case STATE_STOPPED: return "STATE_STOPPED"; 1341 case STATE_PAUSED: return "STATE_PAUSED"; 1342 case STATE_PAUSED_STOPPING: return "STATE_PAUSED_STOPPING"; 1343 case STATE_FLUSHED: return "STATE_FLUSHED"; 1344 case STATE_STOPPING: return "STATE_STOPPING"; 1345 default: return "UNKNOWN"; 1346 } 1347 } 1348 1349 // for client callback handler 1350 wp<IAudioTrackCallback> mCallback; // callback handler for events, or NULL 1351 sp<IAudioTrackCallback> mLegacyCallbackWrapper; // wrapper for legacy callback interface 1352 // for notification APIs 1353 std::unique_ptr<SetParams> mSetParams; // Temporary copy of ctor params to allow for 1354 // deferred set after first reference. 1355 1356 bool mInitialized = false; // Set after track is initialized 1357 // next 2 fields are const after constructor or set() 1358 uint32_t mNotificationFramesReq; // requested number of frames between each 1359 // notification callback, 1360 // at initial source sample rate 1361 uint32_t mNotificationsPerBufferReq; 1362 // requested number of notifications per buffer, 1363 // currently only used for fast tracks with 1364 // default track buffer size 1365 1366 uint32_t mNotificationFramesAct; // actual number of frames between each 1367 // notification callback, 1368 // at initial source sample rate 1369 bool mRefreshRemaining; // processAudioBuffer() should refresh 1370 // mRemainingFrames and mRetryOnPartialBuffer 1371 1372 // used for static track cbf and restoration 1373 int32_t mLoopCount; // last setLoop loopCount; zero means disabled 1374 uint32_t mLoopStart; // last setLoop loopStart 1375 uint32_t mLoopEnd; // last setLoop loopEnd 1376 int32_t mLoopCountNotified; // the last loopCount notified by callback. 1377 // mLoopCountNotified counts down, matching 1378 // the remaining loop count for static track 1379 // playback. 1380 1381 // These are private to processAudioBuffer(), and are not protected by a lock 1382 uint32_t mRemainingFrames; // number of frames to request in obtainBuffer() 1383 bool mRetryOnPartialBuffer; // sleep and retry after partial obtainBuffer() 1384 uint32_t mObservedSequence; // last observed value of mSequence 1385 1386 Modulo<uint32_t> mMarkerPosition; // in wrapping (overflow) frame units 1387 bool mMarkerReached; 1388 Modulo<uint32_t> mNewPosition; // in frames 1389 uint32_t mUpdatePeriod; // in frames, zero means no EVENT_NEW_POS 1390 1391 Modulo<uint32_t> mServer; // in frames, last known mProxy->getPosition() 1392 // which is count of frames consumed by server, 1393 // reset by new IAudioTrack, 1394 // whether it is reset by stop() is TBD 1395 Modulo<uint32_t> mPosition; // in frames, like mServer except continues 1396 // monotonically after new IAudioTrack, 1397 // and could be easily widened to uint64_t 1398 Modulo<uint32_t> mReleased; // count of frames released to server 1399 // but not necessarily consumed by server, 1400 // reset by stop() but continues monotonically 1401 // after new IAudioTrack to restore mPosition, 1402 // and could be easily widened to uint64_t 1403 int64_t mStartFromZeroUs; // the start time after flush or stop, 1404 // when position should be 0. 1405 // only used for offloaded and direct tracks. 1406 int64_t mStartNs; // the time when start() is called. 1407 ExtendedTimestamp mStartEts; // Extended timestamp at start for normal 1408 // AudioTracks. 1409 AudioTimestamp mStartTs; // Timestamp at start for offloaded or direct 1410 // AudioTracks. 1411 1412 bool mPreviousTimestampValid;// true if mPreviousTimestamp is valid 1413 bool mTimestampStartupGlitchReported; // reduce log spam 1414 bool mTimestampRetrogradePositionReported; // reduce log spam 1415 bool mTimestampRetrogradeTimeReported; // reduce log spam 1416 bool mTimestampStallReported; // reduce log spam 1417 bool mTimestampStaleTimeReported; // reduce log spam 1418 AudioTimestamp mPreviousTimestamp; // used to detect retrograde motion 1419 ExtendedTimestamp::Location mPreviousLocation; // location used for previous timestamp 1420 1421 uint32_t mUnderrunCountOffset; // updated when restoring tracks 1422 1423 int64_t mFramesWritten; // total frames written. reset to zero after 1424 // the start() following stop(). It is not 1425 // changed after restoring the track or 1426 // after flush. 1427 int64_t mFramesWrittenServerOffset; // An offset to server frames due to 1428 // restoring AudioTrack, or stop/start. 1429 // This offset is also used for static tracks. 1430 int64_t mFramesWrittenAtRestore; // Frames written at restore point (or frames 1431 // delivered for static tracks). 1432 // -1 indicates no previous restore point. 1433 1434 audio_output_flags_t mFlags; // same as mOrigFlags, except for bits that may 1435 // be denied by client or server, such as 1436 // AUDIO_OUTPUT_FLAG_FAST. mLock must be 1437 // held to read or write those bits reliably. 1438 audio_output_flags_t mOrigFlags; // as specified in constructor or set(), const 1439 1440 bool mDoNotReconnect; 1441 1442 audio_session_t mSessionId; 1443 int mAuxEffectId; 1444 audio_port_handle_t mPortId = AUDIO_PORT_HANDLE_NONE; // Id from Audio Policy Manager 1445 1446 /** 1447 * mPlayerIId is the player id of the AudioTrack used by AudioManager. 1448 * For an AudioTrack created by the Java interface, this is generally set once. 1449 */ 1450 int mPlayerIId = -1; // AudioManager.h PLAYER_PIID_INVALID 1451 1452 /** Interface for interacting with the AudioService. */ 1453 sp<IAudioManager> mAudioManager; 1454 1455 /** 1456 * mLogSessionId is a string identifying this AudioTrack for the metrics service. 1457 * It may be unique or shared with other objects. An empty string means the 1458 * logSessionId is not set. 1459 */ 1460 std::string mLogSessionId{}; 1461 1462 mutable Mutex mLock; 1463 1464 int mPreviousPriority = ANDROID_PRIORITY_NORMAL; // before start() 1465 SchedPolicy mPreviousSchedulingGroup = SP_DEFAULT; 1466 bool mAwaitBoost; // thread should wait for priority boost before running 1467 1468 // The proxy should only be referenced while a lock is held because the proxy isn't 1469 // multi-thread safe, especially the SingleStateQueue part of the proxy. 1470 // An exception is that a blocking ClientProxy::obtainBuffer() may be called without a lock, 1471 // provided that the caller also holds an extra reference to the proxy and shared memory to keep 1472 // them around in case they are replaced during the obtainBuffer(). 1473 sp<StaticAudioTrackClientProxy> mStaticProxy; // for type safety only 1474 sp<AudioTrackClientProxy> mProxy; // primary owner of the memory 1475 1476 bool mInUnderrun; // whether track is currently in underrun state 1477 uint32_t mPausedPosition = 0; 1478 1479 // For Device Selection API 1480 // a value of AUDIO_PORT_HANDLE_NONE indicated default (AudioPolicyManager) routing. 1481 1482 // Device requested by the application. 1483 audio_port_handle_t mSelectedDeviceId = AUDIO_PORT_HANDLE_NONE; 1484 1485 // Devices actually selected by AudioPolicyManager: This may not match the app 1486 // selection depending on other activity and connected devices. 1487 DeviceIdVector mRoutedDeviceIds; 1488 1489 sp<media::VolumeHandler> mVolumeHandler; 1490 1491 private: 1492 class DeathNotifier : public IBinder::DeathRecipient { 1493 public: DeathNotifier(AudioTrack * audioTrack)1494 explicit DeathNotifier(AudioTrack* audioTrack) : mAudioTrack(audioTrack) { } 1495 protected: 1496 virtual void binderDied(const wp<IBinder>& who); 1497 private: 1498 const wp<AudioTrack> mAudioTrack; 1499 }; 1500 1501 sp<DeathNotifier> mDeathNotifier; 1502 uint32_t mSequence; // incremented for each new IAudioTrack attempt 1503 AttributionSourceState mClientAttributionSource; 1504 1505 wp<AudioSystem::AudioDeviceCallback> mDeviceCallback; 1506 1507 // Cached values to restore along with the AudioTrack. 1508 audio_dual_mono_mode_t mDualMonoMode = AUDIO_DUAL_MONO_MODE_OFF; 1509 float mAudioDescriptionMixLeveldB = -std::numeric_limits<float>::infinity(); 1510 1511 private: 1512 class MediaMetrics { 1513 public: MediaMetrics()1514 MediaMetrics() : mMetricsItem(mediametrics::Item::create("audiotrack")) { 1515 } ~MediaMetrics()1516 ~MediaMetrics() { 1517 // mMetricsItem alloc failure will be flagged in the constructor 1518 // don't log empty records 1519 if (mMetricsItem->count() > 0) { 1520 mMetricsItem->selfrecord(); 1521 } 1522 } 1523 void gather(const AudioTrack *track); dup()1524 mediametrics::Item *dup() { return mMetricsItem->dup(); } 1525 private: 1526 std::unique_ptr<mediametrics::Item> mMetricsItem; 1527 }; 1528 MediaMetrics mMediaMetrics; 1529 std::string mMetricsId; // GUARDED_BY(mLock), could change in createTrack_l(). 1530 std::string mCallerName; // for example "aaudio" 1531 1532 // report error to mediametrics. 1533 void reportError(status_t status, const char *event, const char *message) const; 1534 1535 private: 1536 class AudioTrackCallback : public media::BnAudioTrackCallback { 1537 public: 1538 binder::Status onCodecFormatChanged(const std::vector<uint8_t>& audioMetadata) override; 1539 1540 void setAudioTrackCallback(const sp<media::IAudioTrackCallback>& callback); 1541 private: 1542 Mutex mAudioTrackCbLock; 1543 wp<media::IAudioTrackCallback> mCallback; 1544 }; 1545 sp<AudioTrackCallback> mAudioTrackCallback = sp<AudioTrackCallback>::make(); 1546 }; 1547 1548 }; // namespace android 1549 1550 #endif // ANDROID_AUDIOTRACK_H 1551