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