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