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