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