AudioTrack.java revision 8fd0928cdb6249882acb18274ff7ddaab6fa46d0
1/*
2 * Copyright (C) 2008 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
17package android.media;
18
19import java.lang.annotation.Retention;
20import java.lang.annotation.RetentionPolicy;
21import java.lang.ref.WeakReference;
22import java.lang.Math;
23import java.nio.ByteBuffer;
24import java.nio.ByteOrder;
25import java.nio.NioUtils;
26import java.util.Collection;
27
28import android.annotation.IntDef;
29import android.annotation.NonNull;
30import android.annotation.SystemApi;
31import android.app.ActivityThread;
32import android.app.AppOpsManager;
33import android.content.Context;
34import android.os.Handler;
35import android.os.IBinder;
36import android.os.Looper;
37import android.os.Message;
38import android.os.Process;
39import android.os.RemoteException;
40import android.os.ServiceManager;
41import android.util.ArrayMap;
42import android.util.Log;
43
44import com.android.internal.app.IAppOpsService;
45
46
47/**
48 * The AudioTrack class manages and plays a single audio resource for Java applications.
49 * It allows streaming of PCM audio buffers to the audio sink for playback. This is
50 * achieved by "pushing" the data to the AudioTrack object using one of the
51 *  {@link #write(byte[], int, int)}, {@link #write(short[], int, int)},
52 *  and {@link #write(float[], int, int, int)} methods.
53 *
54 * <p>An AudioTrack instance can operate under two modes: static or streaming.<br>
55 * In Streaming mode, the application writes a continuous stream of data to the AudioTrack, using
56 * one of the {@code write()} methods. These are blocking and return when the data has been
57 * transferred from the Java layer to the native layer and queued for playback. The streaming
58 * mode is most useful when playing blocks of audio data that for instance are:
59 *
60 * <ul>
61 *   <li>too big to fit in memory because of the duration of the sound to play,</li>
62 *   <li>too big to fit in memory because of the characteristics of the audio data
63 *         (high sampling rate, bits per sample ...)</li>
64 *   <li>received or generated while previously queued audio is playing.</li>
65 * </ul>
66 *
67 * The static mode should be chosen when dealing with short sounds that fit in memory and
68 * that need to be played with the smallest latency possible. The static mode will
69 * therefore be preferred for UI and game sounds that are played often, and with the
70 * smallest overhead possible.
71 *
72 * <p>Upon creation, an AudioTrack object initializes its associated audio buffer.
73 * The size of this buffer, specified during the construction, determines how long an AudioTrack
74 * can play before running out of data.<br>
75 * For an AudioTrack using the static mode, this size is the maximum size of the sound that can
76 * be played from it.<br>
77 * For the streaming mode, data will be written to the audio sink in chunks of
78 * sizes less than or equal to the total buffer size.
79 *
80 * AudioTrack is not final and thus permits subclasses, but such use is not recommended.
81 */
82public class AudioTrack
83{
84    //---------------------------------------------------------
85    // Constants
86    //--------------------
87    /** Minimum value for a linear gain or auxiliary effect level.
88     *  This value must be exactly equal to 0.0f; do not change it.
89     */
90    private static final float GAIN_MIN = 0.0f;
91    /** Maximum value for a linear gain or auxiliary effect level.
92     *  This value must be greater than or equal to 1.0f.
93     */
94    private static final float GAIN_MAX = 1.0f;
95
96    /** Minimum value for sample rate */
97    private static final int SAMPLE_RATE_HZ_MIN = 4000;
98    /** Maximum value for sample rate */
99    private static final int SAMPLE_RATE_HZ_MAX = 192000;
100
101    /** Maximum value for AudioTrack channel count */
102    private static final int CHANNEL_COUNT_MAX = 8;
103
104    /** indicates AudioTrack state is stopped */
105    public static final int PLAYSTATE_STOPPED = 1;  // matches SL_PLAYSTATE_STOPPED
106    /** indicates AudioTrack state is paused */
107    public static final int PLAYSTATE_PAUSED  = 2;  // matches SL_PLAYSTATE_PAUSED
108    /** indicates AudioTrack state is playing */
109    public static final int PLAYSTATE_PLAYING = 3;  // matches SL_PLAYSTATE_PLAYING
110
111    // keep these values in sync with android_media_AudioTrack.cpp
112    /**
113     * Creation mode where audio data is transferred from Java to the native layer
114     * only once before the audio starts playing.
115     */
116    public static final int MODE_STATIC = 0;
117    /**
118     * Creation mode where audio data is streamed from Java to the native layer
119     * as the audio is playing.
120     */
121    public static final int MODE_STREAM = 1;
122
123    /** @hide */
124    @IntDef({
125        MODE_STATIC,
126        MODE_STREAM
127    })
128    @Retention(RetentionPolicy.SOURCE)
129    public @interface TransferMode {}
130
131    /**
132     * State of an AudioTrack that was not successfully initialized upon creation.
133     */
134    public static final int STATE_UNINITIALIZED = 0;
135    /**
136     * State of an AudioTrack that is ready to be used.
137     */
138    public static final int STATE_INITIALIZED   = 1;
139    /**
140     * State of a successfully initialized AudioTrack that uses static data,
141     * but that hasn't received that data yet.
142     */
143    public static final int STATE_NO_STATIC_DATA = 2;
144
145    /**
146     * Denotes a successful operation.
147     */
148    public  static final int SUCCESS                               = AudioSystem.SUCCESS;
149    /**
150     * Denotes a generic operation failure.
151     */
152    public  static final int ERROR                                 = AudioSystem.ERROR;
153    /**
154     * Denotes a failure due to the use of an invalid value.
155     */
156    public  static final int ERROR_BAD_VALUE                       = AudioSystem.BAD_VALUE;
157    /**
158     * Denotes a failure due to the improper use of a method.
159     */
160    public  static final int ERROR_INVALID_OPERATION               = AudioSystem.INVALID_OPERATION;
161
162    // Error codes:
163    // to keep in sync with frameworks/base/core/jni/android_media_AudioTrack.cpp
164    private static final int ERROR_NATIVESETUP_AUDIOSYSTEM         = -16;
165    private static final int ERROR_NATIVESETUP_INVALIDCHANNELMASK  = -17;
166    private static final int ERROR_NATIVESETUP_INVALIDFORMAT       = -18;
167    private static final int ERROR_NATIVESETUP_INVALIDSTREAMTYPE   = -19;
168    private static final int ERROR_NATIVESETUP_NATIVEINITFAILED    = -20;
169
170    // Events:
171    // to keep in sync with frameworks/av/include/media/AudioTrack.h
172    /**
173     * Event id denotes when playback head has reached a previously set marker.
174     */
175    private static final int NATIVE_EVENT_MARKER  = 3;
176    /**
177     * Event id denotes when previously set update period has elapsed during playback.
178     */
179    private static final int NATIVE_EVENT_NEW_POS = 4;
180
181    /**
182     * Event id denotes when the routing changes.
183     */
184    private final static int NATIVE_EVENT_ROUTING_CHANGE = 1000;
185
186
187    private final static String TAG = "android.media.AudioTrack";
188
189
190    /** @hide */
191    @IntDef({
192        WRITE_BLOCKING,
193        WRITE_NON_BLOCKING
194    })
195    @Retention(RetentionPolicy.SOURCE)
196    public @interface WriteMode {}
197
198    /**
199     * The write mode indicating the write operation will block until all data has been written,
200     * to be used in {@link #write(ByteBuffer, int, int)}
201     */
202    public final static int WRITE_BLOCKING = 0;
203    /**
204     * The write mode indicating the write operation will return immediately after
205     * queuing as much audio data for playback as possible without blocking, to be used in
206     * {@link #write(ByteBuffer, int, int)}.
207     */
208    public final static int WRITE_NON_BLOCKING = 1;
209
210    //--------------------------------------------------------------------------
211    // Member variables
212    //--------------------
213    /**
214     * Indicates the state of the AudioTrack instance.
215     */
216    private int mState = STATE_UNINITIALIZED;
217    /**
218     * Indicates the play state of the AudioTrack instance.
219     */
220    private int mPlayState = PLAYSTATE_STOPPED;
221    /**
222     * Lock to make sure mPlayState updates are reflecting the actual state of the object.
223     */
224    private final Object mPlayStateLock = new Object();
225    /**
226     * Sizes of the native audio buffer.
227     * These values are set during construction and can be stale.
228     * To obtain the current native audio buffer frame count use {@link #getNativeFrameCount()}.
229     */
230    private int mNativeBufferSizeInBytes = 0;
231    private int mNativeBufferSizeInFrames = 0;
232    /**
233     * Handler for events coming from the native code.
234     */
235    private NativePositionEventHandlerDelegate mEventHandlerDelegate;
236    /**
237     * Looper associated with the thread that creates the AudioTrack instance.
238     */
239    private final Looper mInitializationLooper;
240    /**
241     * The audio data source sampling rate in Hz.
242     */
243    private int mSampleRate; // initialized by all constructors
244    /**
245     * The number of audio output channels (1 is mono, 2 is stereo).
246     */
247    private int mChannelCount = 1;
248    /**
249     * The audio channel mask used for calling native AudioTrack
250     */
251    private int mChannels = AudioFormat.CHANNEL_OUT_MONO;
252
253    /**
254     * The type of the audio stream to play. See
255     *   {@link AudioManager#STREAM_VOICE_CALL}, {@link AudioManager#STREAM_SYSTEM},
256     *   {@link AudioManager#STREAM_RING}, {@link AudioManager#STREAM_MUSIC},
257     *   {@link AudioManager#STREAM_ALARM}, {@link AudioManager#STREAM_NOTIFICATION}, and
258     *   {@link AudioManager#STREAM_DTMF}.
259     */
260    private int mStreamType = AudioManager.STREAM_MUSIC;
261
262    private final AudioAttributes mAttributes;
263    /**
264     * The way audio is consumed by the audio sink, streaming or static.
265     */
266    private int mDataLoadMode = MODE_STREAM;
267    /**
268     * The current channel position mask, as specified on AudioTrack creation.
269     * Can be set simultaneously with channel index mask {@link #mChannelIndexMask}.
270     * May be set to {@link AudioFormat#CHANNEL_INVALID} if a channel index mask is specified.
271     */
272    private int mChannelConfiguration = AudioFormat.CHANNEL_OUT_MONO;
273    /**
274     * The current audio channel index configuration (if specified).
275     */
276    private int mChannelIndexMask = 0;
277    /**
278     * The encoding of the audio samples.
279     * @see AudioFormat#ENCODING_PCM_8BIT
280     * @see AudioFormat#ENCODING_PCM_16BIT
281     * @see AudioFormat#ENCODING_PCM_FLOAT
282     */
283    private int mAudioFormat = AudioFormat.ENCODING_PCM_16BIT;
284    /**
285     * Audio session ID
286     */
287    private int mSessionId = AudioSystem.AUDIO_SESSION_ALLOCATE;
288    /**
289     * Reference to the app-ops service.
290     */
291    private final IAppOpsService mAppOps;
292    /**
293     * HW_AV_SYNC track AV Sync Header
294     */
295    private ByteBuffer mAvSyncHeader = null;
296    /**
297     * HW_AV_SYNC track audio data bytes remaining to write after current AV sync header
298     */
299    private int mAvSyncBytesRemaining = 0;
300
301    //--------------------------------
302    // Used exclusively by native code
303    //--------------------
304    /**
305     * Accessed by native methods: provides access to C++ AudioTrack object.
306     */
307    @SuppressWarnings("unused")
308    private long mNativeTrackInJavaObj;
309    /**
310     * Accessed by native methods: provides access to the JNI data (i.e. resources used by
311     * the native AudioTrack object, but not stored in it).
312     */
313    @SuppressWarnings("unused")
314    private long mJniData;
315
316
317    //--------------------------------------------------------------------------
318    // Constructor, Finalize
319    //--------------------
320    /**
321     * Class constructor.
322     * @param streamType the type of the audio stream. See
323     *   {@link AudioManager#STREAM_VOICE_CALL}, {@link AudioManager#STREAM_SYSTEM},
324     *   {@link AudioManager#STREAM_RING}, {@link AudioManager#STREAM_MUSIC},
325     *   {@link AudioManager#STREAM_ALARM}, and {@link AudioManager#STREAM_NOTIFICATION}.
326     * @param sampleRateInHz the initial source sample rate expressed in Hz.
327     * @param channelConfig describes the configuration of the audio channels.
328     *   See {@link AudioFormat#CHANNEL_OUT_MONO} and
329     *   {@link AudioFormat#CHANNEL_OUT_STEREO}
330     * @param audioFormat the format in which the audio data is represented.
331     *   See {@link AudioFormat#ENCODING_PCM_16BIT},
332     *   {@link AudioFormat#ENCODING_PCM_8BIT},
333     *   and {@link AudioFormat#ENCODING_PCM_FLOAT}.
334     * @param bufferSizeInBytes the total size (in bytes) of the internal buffer where audio data is
335     *   read from for playback. This should be a multiple of the frame size in bytes.
336     *   <p> If the track's creation mode is {@link #MODE_STATIC},
337     *   this is the maximum length sample, or audio clip, that can be played by this instance.
338     *   <p> If the track's creation mode is {@link #MODE_STREAM},
339     *   this should be the desired buffer size
340     *   for the <code>AudioTrack</code> to satisfy the application's
341     *   natural latency requirements.
342     *   If <code>bufferSizeInBytes</code> is less than the
343     *   minimum buffer size for the output sink, it is automatically increased to the minimum
344     *   buffer size.
345     *   The method {@link #getNativeFrameCount()} returns the
346     *   actual size in frames of the native buffer created, which
347     *   determines the frequency to write
348     *   to the streaming <code>AudioTrack</code> to avoid underrun.
349     * @param mode streaming or static buffer. See {@link #MODE_STATIC} and {@link #MODE_STREAM}
350     * @throws java.lang.IllegalArgumentException
351     */
352    public AudioTrack(int streamType, int sampleRateInHz, int channelConfig, int audioFormat,
353            int bufferSizeInBytes, int mode)
354    throws IllegalArgumentException {
355        this(streamType, sampleRateInHz, channelConfig, audioFormat,
356                bufferSizeInBytes, mode, AudioSystem.AUDIO_SESSION_ALLOCATE);
357    }
358
359    /**
360     * Class constructor with audio session. Use this constructor when the AudioTrack must be
361     * attached to a particular audio session. The primary use of the audio session ID is to
362     * associate audio effects to a particular instance of AudioTrack: if an audio session ID
363     * is provided when creating an AudioEffect, this effect will be applied only to audio tracks
364     * and media players in the same session and not to the output mix.
365     * When an AudioTrack is created without specifying a session, it will create its own session
366     * which can be retrieved by calling the {@link #getAudioSessionId()} method.
367     * If a non-zero session ID is provided, this AudioTrack will share effects attached to this
368     * session
369     * with all other media players or audio tracks in the same session, otherwise a new session
370     * will be created for this track if none is supplied.
371     * @param streamType the type of the audio stream. See
372     *   {@link AudioManager#STREAM_VOICE_CALL}, {@link AudioManager#STREAM_SYSTEM},
373     *   {@link AudioManager#STREAM_RING}, {@link AudioManager#STREAM_MUSIC},
374     *   {@link AudioManager#STREAM_ALARM}, and {@link AudioManager#STREAM_NOTIFICATION}.
375     * @param sampleRateInHz the initial source sample rate expressed in Hz.
376     * @param channelConfig describes the configuration of the audio channels.
377     *   See {@link AudioFormat#CHANNEL_OUT_MONO} and
378     *   {@link AudioFormat#CHANNEL_OUT_STEREO}
379     * @param audioFormat the format in which the audio data is represented.
380     *   See {@link AudioFormat#ENCODING_PCM_16BIT} and
381     *   {@link AudioFormat#ENCODING_PCM_8BIT},
382     *   and {@link AudioFormat#ENCODING_PCM_FLOAT}.
383     * @param bufferSizeInBytes the total size (in bytes) of the buffer where audio data is read
384     *   from for playback. If using the AudioTrack in streaming mode, you can write data into
385     *   this buffer in smaller chunks than this size. If using the AudioTrack in static mode,
386     *   this is the maximum size of the sound that will be played for this instance.
387     *   See {@link #getMinBufferSize(int, int, int)} to determine the minimum required buffer size
388     *   for the successful creation of an AudioTrack instance in streaming mode. Using values
389     *   smaller than getMinBufferSize() will result in an initialization failure.
390     * @param mode streaming or static buffer. See {@link #MODE_STATIC} and {@link #MODE_STREAM}
391     * @param sessionId Id of audio session the AudioTrack must be attached to
392     * @throws java.lang.IllegalArgumentException
393     */
394    public AudioTrack(int streamType, int sampleRateInHz, int channelConfig, int audioFormat,
395            int bufferSizeInBytes, int mode, int sessionId)
396    throws IllegalArgumentException {
397        // mState already == STATE_UNINITIALIZED
398        this((new AudioAttributes.Builder())
399                    .setLegacyStreamType(streamType)
400                    .build(),
401                (new AudioFormat.Builder())
402                    .setChannelMask(channelConfig)
403                    .setEncoding(audioFormat)
404                    .setSampleRate(sampleRateInHz)
405                    .build(),
406                bufferSizeInBytes,
407                mode, sessionId);
408    }
409
410    /**
411     * Class constructor with {@link AudioAttributes} and {@link AudioFormat}.
412     * @param attributes a non-null {@link AudioAttributes} instance.
413     * @param format a non-null {@link AudioFormat} instance describing the format of the data
414     *     that will be played through this AudioTrack. See {@link AudioFormat.Builder} for
415     *     configuring the audio format parameters such as encoding, channel mask and sample rate.
416     * @param bufferSizeInBytes the total size (in bytes) of the buffer where audio data is read
417     *   from for playback. If using the AudioTrack in streaming mode, you can write data into
418     *   this buffer in smaller chunks than this size. If using the AudioTrack in static mode,
419     *   this is the maximum size of the sound that will be played for this instance.
420     *   See {@link #getMinBufferSize(int, int, int)} to determine the minimum required buffer size
421     *   for the successful creation of an AudioTrack instance in streaming mode. Using values
422     *   smaller than getMinBufferSize() will result in an initialization failure.
423     * @param mode streaming or static buffer. See {@link #MODE_STATIC} and {@link #MODE_STREAM}.
424     * @param sessionId ID of audio session the AudioTrack must be attached to, or
425     *   {@link AudioManager#AUDIO_SESSION_ID_GENERATE} if the session isn't known at construction
426     *   time. See also {@link AudioManager#generateAudioSessionId()} to obtain a session ID before
427     *   construction.
428     * @throws IllegalArgumentException
429     */
430    public AudioTrack(AudioAttributes attributes, AudioFormat format, int bufferSizeInBytes,
431            int mode, int sessionId)
432                    throws IllegalArgumentException {
433        // mState already == STATE_UNINITIALIZED
434
435        if (attributes == null) {
436            throw new IllegalArgumentException("Illegal null AudioAttributes");
437        }
438        if (format == null) {
439            throw new IllegalArgumentException("Illegal null AudioFormat");
440        }
441
442        // remember which looper is associated with the AudioTrack instantiation
443        Looper looper;
444        if ((looper = Looper.myLooper()) == null) {
445            looper = Looper.getMainLooper();
446        }
447
448        int rate = 0;
449        if ((format.getPropertySetMask() & AudioFormat.AUDIO_FORMAT_HAS_PROPERTY_SAMPLE_RATE) != 0)
450        {
451            rate = format.getSampleRate();
452        } else {
453            rate = AudioSystem.getPrimaryOutputSamplingRate();
454            if (rate <= 0) {
455                rate = 44100;
456            }
457        }
458        int channelIndexMask = 0;
459        if ((format.getPropertySetMask()
460                & AudioFormat.AUDIO_FORMAT_HAS_PROPERTY_CHANNEL_INDEX_MASK) != 0) {
461            channelIndexMask = format.getChannelIndexMask();
462        }
463        int channelMask = 0;
464        if ((format.getPropertySetMask()
465                & AudioFormat.AUDIO_FORMAT_HAS_PROPERTY_CHANNEL_MASK) != 0) {
466            channelMask = format.getChannelMask();
467        } else if (channelIndexMask == 0) { // if no masks at all, use stereo
468            channelMask = AudioFormat.CHANNEL_OUT_FRONT_LEFT
469                    | AudioFormat.CHANNEL_OUT_FRONT_RIGHT;
470        }
471        int encoding = AudioFormat.ENCODING_DEFAULT;
472        if ((format.getPropertySetMask() & AudioFormat.AUDIO_FORMAT_HAS_PROPERTY_ENCODING) != 0) {
473            encoding = format.getEncoding();
474        }
475        audioParamCheck(rate, channelMask, channelIndexMask, encoding, mode);
476        mStreamType = AudioSystem.STREAM_DEFAULT;
477
478        audioBuffSizeCheck(bufferSizeInBytes);
479
480        mInitializationLooper = looper;
481        IBinder b = ServiceManager.getService(Context.APP_OPS_SERVICE);
482        mAppOps = IAppOpsService.Stub.asInterface(b);
483
484        mAttributes = (new AudioAttributes.Builder(attributes).build());
485
486        if (sessionId < 0) {
487            throw new IllegalArgumentException("Invalid audio session ID: "+sessionId);
488        }
489
490        int[] session = new int[1];
491        session[0] = sessionId;
492        // native initialization
493        int initResult = native_setup(new WeakReference<AudioTrack>(this), mAttributes,
494                mSampleRate, mChannels, mAudioFormat,
495                mNativeBufferSizeInBytes, mDataLoadMode, session);
496        if (initResult != SUCCESS) {
497            loge("Error code "+initResult+" when initializing AudioTrack.");
498            return; // with mState == STATE_UNINITIALIZED
499        }
500
501        mSessionId = session[0];
502
503        if (mDataLoadMode == MODE_STATIC) {
504            mState = STATE_NO_STATIC_DATA;
505        } else {
506            mState = STATE_INITIALIZED;
507        }
508    }
509
510    /**
511     * Builder class for {@link AudioTrack} objects.
512     * Use this class to configure and create an <code>AudioTrack</code> instance. By setting audio
513     * attributes and audio format parameters, you indicate which of those vary from the default
514     * behavior on the device.
515     * <p> Here is an example where <code>Builder</code> is used to specify all {@link AudioFormat}
516     * parameters, to be used by a new <code>AudioTrack</code> instance:
517     *
518     * <pre class="prettyprint">
519     * AudioTrack player = new AudioTrack.Builder()
520     *         .setAudioAttributes(new AudioAttributes.Builder()
521     *                  .setUsage(AudioAttributes.USAGE_ALARM)
522     *                  .setContentType(CONTENT_TYPE_MUSIC)
523     *                  .build())
524     *         .setAudioFormat(new AudioFormat.Builder()
525     *                 .setEncoding(AudioFormat.ENCODING_PCM_16BIT)
526     *                 .setSampleRate(441000)
527     *                 .setChannelMask(AudioFormat.CHANNEL_OUT_STEREO)
528     *                 .build())
529     *         .setBufferSize(minBuffSize)
530     *         .build();
531     * </pre>
532     * <p>
533     * If the audio attributes are not set with {@link #setAudioAttributes(AudioAttributes)},
534     * attributes comprising {@link AudioAttributes#USAGE_MEDIA} will be used.
535     * <br>If the audio format is not specified or is incomplete, its sample rate will be the
536     * default output sample rate of the device (see
537     * {@link AudioManager#PROPERTY_OUTPUT_SAMPLE_RATE}), its channel configuration will be
538     * {@link AudioFormat#CHANNEL_OUT_STEREO} and the encoding will be
539     * {@link AudioFormat#ENCODING_PCM_16BIT}.
540     * <br>If the buffer size is not specified with {@link #setBufferSizeInBytes(int)},
541     * and the mode is {@link AudioTrack#MODE_STREAM}, the minimum buffer size is used.
542     * <br>If the transfer mode is not specified with {@link #setTransferMode(int)},
543     * <code>MODE_STREAM</code> will be used.
544     * <br>If the session ID is not specified with {@link #setSessionId(int)}, a new one will
545     * be generated.
546     */
547    public static class Builder {
548        private AudioAttributes mAttributes;
549        private AudioFormat mFormat;
550        private int mBufferSizeInBytes;
551        private int mSessionId = AudioManager.AUDIO_SESSION_ID_GENERATE;
552        private int mMode = MODE_STREAM;
553
554        /**
555         * Constructs a new Builder with the default values as described above.
556         */
557        public Builder() {
558        }
559
560        /**
561         * Sets the {@link AudioAttributes}.
562         * @param attributes a non-null {@link AudioAttributes} instance that describes the audio
563         *     data to be played.
564         * @return the same Builder instance.
565         * @throws IllegalArgumentException
566         */
567        public @NonNull Builder setAudioAttributes(@NonNull AudioAttributes attributes)
568                throws IllegalArgumentException {
569            if (attributes == null) {
570                throw new IllegalArgumentException("Illegal null AudioAttributes argument");
571            }
572            // keep reference, we only copy the data when building
573            mAttributes = attributes;
574            return this;
575        }
576
577        /**
578         * Sets the format of the audio data to be played by the {@link AudioTrack}.
579         * See {@link AudioFormat.Builder} for configuring the audio format parameters such
580         * as encoding, channel mask and sample rate.
581         * @param format a non-null {@link AudioFormat} instance.
582         * @return the same Builder instance.
583         * @throws IllegalArgumentException
584         */
585        public @NonNull Builder setAudioFormat(@NonNull AudioFormat format)
586                throws IllegalArgumentException {
587            if (format == null) {
588                throw new IllegalArgumentException("Illegal null AudioFormat argument");
589            }
590            // keep reference, we only copy the data when building
591            mFormat = format;
592            return this;
593        }
594
595        /**
596         * Sets the total size (in bytes) of the buffer where audio data is read from for playback.
597         * If using the {@link AudioTrack} in streaming mode
598         * (see {@link AudioTrack#MODE_STREAM}, you can write data into this buffer in smaller
599         * chunks than this size. See {@link #getMinBufferSize(int, int, int)} to determine
600         * the minimum required buffer size for the successful creation of an AudioTrack instance
601         * in streaming mode. Using values smaller than <code>getMinBufferSize()</code> will result
602         * in an exception when trying to build the <code>AudioTrack</code>.
603         * <br>If using the <code>AudioTrack</code> in static mode (see
604         * {@link AudioTrack#MODE_STATIC}), this is the maximum size of the sound that will be
605         * played by this instance.
606         * @param bufferSizeInBytes
607         * @return the same Builder instance.
608         * @throws IllegalArgumentException
609         */
610        public @NonNull Builder setBufferSizeInBytes(int bufferSizeInBytes)
611                throws IllegalArgumentException {
612            if (bufferSizeInBytes <= 0) {
613                throw new IllegalArgumentException("Invalid buffer size " + bufferSizeInBytes);
614            }
615            mBufferSizeInBytes = bufferSizeInBytes;
616            return this;
617        }
618
619        /**
620         * Sets the mode under which buffers of audio data are transferred from the
621         * {@link AudioTrack} to the framework.
622         * @param mode one of {@link AudioTrack#MODE_STREAM}, {@link AudioTrack#MODE_STATIC}.
623         * @return the same Builder instance.
624         * @throws IllegalArgumentException
625         */
626        public @NonNull Builder setTransferMode(@TransferMode int mode)
627                throws IllegalArgumentException {
628            switch(mode) {
629                case MODE_STREAM:
630                case MODE_STATIC:
631                    mMode = mode;
632                    break;
633                default:
634                    throw new IllegalArgumentException("Invalid transfer mode " + mode);
635            }
636            return this;
637        }
638
639        /**
640         * Sets the session ID the {@link AudioTrack} will be attached to.
641         * @param sessionId a strictly positive ID number retrieved from another
642         *     <code>AudioTrack</code> via {@link AudioTrack#getAudioSessionId()} or allocated by
643         *     {@link AudioManager} via {@link AudioManager#generateAudioSessionId()}, or
644         *     {@link AudioManager#AUDIO_SESSION_ID_GENERATE}.
645         * @return the same Builder instance.
646         * @throws IllegalArgumentException
647         */
648        public @NonNull Builder setSessionId(int sessionId)
649                throws IllegalArgumentException {
650            if ((sessionId != AudioManager.AUDIO_SESSION_ID_GENERATE) && (sessionId < 1)) {
651                throw new IllegalArgumentException("Invalid audio session ID " + sessionId);
652            }
653            mSessionId = sessionId;
654            return this;
655        }
656
657        /**
658         * Builds an {@link AudioTrack} instance initialized with all the parameters set
659         * on this <code>Builder</code>.
660         * @return a new {@link AudioTrack} instance.
661         * @throws UnsupportedOperationException if the parameters set on the <code>Builder</code>
662         *     were incompatible, or if they are not supported by the device.
663         */
664        public @NonNull AudioTrack build() throws UnsupportedOperationException {
665            if (mAttributes == null) {
666                mAttributes = new AudioAttributes.Builder()
667                        .setUsage(AudioAttributes.USAGE_MEDIA)
668                        .build();
669            }
670            if (mFormat == null) {
671                mFormat = new AudioFormat.Builder()
672                        .setChannelMask(AudioFormat.CHANNEL_OUT_STEREO)
673                        .setSampleRate(AudioSystem.getPrimaryOutputSamplingRate())
674                        .setEncoding(AudioFormat.ENCODING_DEFAULT)
675                        .build();
676            }
677            try {
678                // If the buffer size is not specified in streaming mode,
679                // use a single frame for the buffer size and let the
680                // native code figure out the minimum buffer size.
681                if (mMode == MODE_STREAM && mBufferSizeInBytes == 0) {
682                    mBufferSizeInBytes = mFormat.getChannelCount()
683                            * mFormat.getBytesPerSample(mFormat.getEncoding());
684                }
685                return new AudioTrack(mAttributes, mFormat, mBufferSizeInBytes, mMode, mSessionId);
686            } catch (IllegalArgumentException e) {
687                throw new UnsupportedOperationException(e.getMessage());
688            }
689        }
690    }
691
692    // mask of all the channels supported by this implementation
693    private static final int SUPPORTED_OUT_CHANNELS =
694            AudioFormat.CHANNEL_OUT_FRONT_LEFT |
695            AudioFormat.CHANNEL_OUT_FRONT_RIGHT |
696            AudioFormat.CHANNEL_OUT_FRONT_CENTER |
697            AudioFormat.CHANNEL_OUT_LOW_FREQUENCY |
698            AudioFormat.CHANNEL_OUT_BACK_LEFT |
699            AudioFormat.CHANNEL_OUT_BACK_RIGHT |
700            AudioFormat.CHANNEL_OUT_BACK_CENTER |
701            AudioFormat.CHANNEL_OUT_SIDE_LEFT |
702            AudioFormat.CHANNEL_OUT_SIDE_RIGHT;
703
704    // Java channel mask definitions below match those
705    // in /system/core/include/system/audio.h in the JNI code of AudioTrack.
706
707    // internal maximum size for bits parameter, not part of public API
708    private static final int AUDIO_CHANNEL_BITS_LOG2 = 30;
709
710    // log(2) of maximum number of representations, not part of public API
711    private static final int AUDIO_CHANNEL_REPRESENTATION_LOG2 = 2;
712
713    // used to create a channel index mask or channel position mask
714    // with getChannelMaskFromRepresentationAndBits();
715    private static final int CHANNEL_OUT_REPRESENTATION_POSITION = 0;
716    private static final int CHANNEL_OUT_REPRESENTATION_INDEX = 2;
717
718    /**
719     * Return the channel mask from its representation and bits.
720     *
721     * This creates a channel mask for mChannels which combines a
722     * representation field and a bits field.  This is for internal
723     * communication to native code, not part of the public API.
724     *
725     * @param representation the type of channel mask,
726     *   either CHANNEL_OUT_REPRESENTATION_POSITION
727     *   or CHANNEL_OUT_REPRESENTATION_INDEX
728     * @param bits is the channel bits specifying occupancy
729     * @return the channel mask
730     * @throws java.lang.IllegalArgumentException if representation is not recognized or
731     *   the bits field is not acceptable for that representation
732     */
733    private static int getChannelMaskFromRepresentationAndBits(int representation, int bits) {
734        switch (representation) {
735        case CHANNEL_OUT_REPRESENTATION_POSITION:
736        case CHANNEL_OUT_REPRESENTATION_INDEX:
737            if ((bits & ~((1 << AUDIO_CHANNEL_BITS_LOG2) - 1)) != 0) {
738                throw new IllegalArgumentException("invalid bits " + bits);
739            }
740            return representation << AUDIO_CHANNEL_BITS_LOG2 | bits;
741        default:
742            throw new IllegalArgumentException("invalid representation " + representation);
743        }
744    }
745
746    // Convenience method for the constructor's parameter checks.
747    // This is where constructor IllegalArgumentException-s are thrown
748    // postconditions:
749    //    mChannelCount is valid
750    //    mChannels is valid
751    //    mAudioFormat is valid
752    //    mSampleRate is valid
753    //    mDataLoadMode is valid
754    private void audioParamCheck(int sampleRateInHz, int channelConfig, int channelIndexMask,
755                                 int audioFormat, int mode) {
756        //--------------
757        // sample rate, note these values are subject to change
758        if (sampleRateInHz < SAMPLE_RATE_HZ_MIN || sampleRateInHz > SAMPLE_RATE_HZ_MAX) {
759            throw new IllegalArgumentException(sampleRateInHz
760                    + "Hz is not a supported sample rate.");
761        }
762        mSampleRate = sampleRateInHz;
763
764        //--------------
765        // channel config
766        mChannelConfiguration = channelConfig;
767
768        switch (channelConfig) {
769        case AudioFormat.CHANNEL_OUT_DEFAULT: //AudioFormat.CHANNEL_CONFIGURATION_DEFAULT
770        case AudioFormat.CHANNEL_OUT_MONO:
771        case AudioFormat.CHANNEL_CONFIGURATION_MONO:
772            mChannelCount = 1;
773            mChannels = AudioFormat.CHANNEL_OUT_MONO;
774            break;
775        case AudioFormat.CHANNEL_OUT_STEREO:
776        case AudioFormat.CHANNEL_CONFIGURATION_STEREO:
777            mChannelCount = 2;
778            mChannels = AudioFormat.CHANNEL_OUT_STEREO;
779            break;
780        default:
781            if (channelConfig == AudioFormat.CHANNEL_INVALID && channelIndexMask != 0) {
782                mChannelCount = 0;
783                break; // channel index configuration only
784            }
785            if (!isMultichannelConfigSupported(channelConfig)) {
786                // input channel configuration features unsupported channels
787                throw new IllegalArgumentException("Unsupported channel configuration.");
788            }
789            mChannels = channelConfig;
790            mChannelCount = AudioFormat.channelCountFromOutChannelMask(channelConfig);
791        }
792        // check the channel index configuration (if present)
793        mChannelIndexMask = channelIndexMask;
794        if (mChannelIndexMask != 0) {
795            // restrictive: indexMask could allow up to AUDIO_CHANNEL_BITS_LOG2
796            final int indexMask = (1 << CHANNEL_COUNT_MAX) - 1;
797            if ((channelIndexMask & ~indexMask) != 0) {
798                throw new IllegalArgumentException("Unsupported channel index configuration "
799                        + channelIndexMask);
800            }
801            int channelIndexCount = Integer.bitCount(channelIndexMask);
802            if (mChannelCount == 0) {
803                 mChannelCount = channelIndexCount;
804            } else if (mChannelCount != channelIndexCount) {
805                throw new IllegalArgumentException("Channel count must match");
806            }
807
808            // AudioTrack prefers to use the channel index configuration
809            // over the channel position configuration if both are specified.
810            mChannels = getChannelMaskFromRepresentationAndBits(
811                    CHANNEL_OUT_REPRESENTATION_INDEX, mChannelIndexMask);
812        }
813
814        //--------------
815        // audio format
816        if (audioFormat == AudioFormat.ENCODING_DEFAULT) {
817            audioFormat = AudioFormat.ENCODING_PCM_16BIT;
818        }
819
820        if (!AudioFormat.isValidEncoding(audioFormat)) {
821            throw new IllegalArgumentException("Unsupported audio encoding.");
822        }
823        mAudioFormat = audioFormat;
824
825        //--------------
826        // audio load mode
827        if (((mode != MODE_STREAM) && (mode != MODE_STATIC)) ||
828                ((mode != MODE_STREAM) && !AudioFormat.isEncodingLinearPcm(mAudioFormat))) {
829            throw new IllegalArgumentException("Invalid mode.");
830        }
831        mDataLoadMode = mode;
832    }
833
834    /**
835     * Convenience method to check that the channel configuration (a.k.a channel mask) is supported
836     * @param channelConfig the mask to validate
837     * @return false if the AudioTrack can't be used with such a mask
838     */
839    private static boolean isMultichannelConfigSupported(int channelConfig) {
840        // check for unsupported channels
841        if ((channelConfig & SUPPORTED_OUT_CHANNELS) != channelConfig) {
842            loge("Channel configuration features unsupported channels");
843            return false;
844        }
845        final int channelCount = AudioFormat.channelCountFromOutChannelMask(channelConfig);
846        if (channelCount > CHANNEL_COUNT_MAX) {
847            loge("Channel configuration contains too many channels " +
848                    channelCount + ">" + CHANNEL_COUNT_MAX);
849            return false;
850        }
851        // check for unsupported multichannel combinations:
852        // - FL/FR must be present
853        // - L/R channels must be paired (e.g. no single L channel)
854        final int frontPair =
855                AudioFormat.CHANNEL_OUT_FRONT_LEFT | AudioFormat.CHANNEL_OUT_FRONT_RIGHT;
856        if ((channelConfig & frontPair) != frontPair) {
857                loge("Front channels must be present in multichannel configurations");
858                return false;
859        }
860        final int backPair =
861                AudioFormat.CHANNEL_OUT_BACK_LEFT | AudioFormat.CHANNEL_OUT_BACK_RIGHT;
862        if ((channelConfig & backPair) != 0) {
863            if ((channelConfig & backPair) != backPair) {
864                loge("Rear channels can't be used independently");
865                return false;
866            }
867        }
868        final int sidePair =
869                AudioFormat.CHANNEL_OUT_SIDE_LEFT | AudioFormat.CHANNEL_OUT_SIDE_RIGHT;
870        if ((channelConfig & sidePair) != 0
871                && (channelConfig & sidePair) != sidePair) {
872            loge("Side channels can't be used independently");
873            return false;
874        }
875        return true;
876    }
877
878
879    // Convenience method for the constructor's audio buffer size check.
880    // preconditions:
881    //    mChannelCount is valid
882    //    mAudioFormat is valid
883    // postcondition:
884    //    mNativeBufferSizeInBytes is valid (multiple of frame size, positive)
885    private void audioBuffSizeCheck(int audioBufferSize) {
886        // NB: this section is only valid with PCM data.
887        //     To update when supporting compressed formats
888        int frameSizeInBytes;
889        if (AudioFormat.isEncodingLinearPcm(mAudioFormat)) {
890            frameSizeInBytes = mChannelCount
891                    * (AudioFormat.getBytesPerSample(mAudioFormat));
892        } else {
893            frameSizeInBytes = 1;
894        }
895        if ((audioBufferSize % frameSizeInBytes != 0) || (audioBufferSize < 1)) {
896            throw new IllegalArgumentException("Invalid audio buffer size.");
897        }
898
899        mNativeBufferSizeInBytes = audioBufferSize;
900        mNativeBufferSizeInFrames = audioBufferSize / frameSizeInBytes;
901    }
902
903
904    /**
905     * Releases the native AudioTrack resources.
906     */
907    public void release() {
908        // even though native_release() stops the native AudioTrack, we need to stop
909        // AudioTrack subclasses too.
910        try {
911            stop();
912        } catch(IllegalStateException ise) {
913            // don't raise an exception, we're releasing the resources.
914        }
915        native_release();
916        mState = STATE_UNINITIALIZED;
917    }
918
919    @Override
920    protected void finalize() {
921        native_finalize();
922    }
923
924    //--------------------------------------------------------------------------
925    // Getters
926    //--------------------
927    /**
928     * Returns the minimum gain value, which is the constant 0.0.
929     * Gain values less than 0.0 will be clamped to 0.0.
930     * <p>The word "volume" in the API name is historical; this is actually a linear gain.
931     * @return the minimum value, which is the constant 0.0.
932     */
933    static public float getMinVolume() {
934        return GAIN_MIN;
935    }
936
937    /**
938     * Returns the maximum gain value, which is greater than or equal to 1.0.
939     * Gain values greater than the maximum will be clamped to the maximum.
940     * <p>The word "volume" in the API name is historical; this is actually a gain.
941     * expressed as a linear multiplier on sample values, where a maximum value of 1.0
942     * corresponds to a gain of 0 dB (sample values left unmodified).
943     * @return the maximum value, which is greater than or equal to 1.0.
944     */
945    static public float getMaxVolume() {
946        return GAIN_MAX;
947    }
948
949    /**
950     * Returns the configured audio data sample rate in Hz
951     */
952    public int getSampleRate() {
953        return mSampleRate;
954    }
955
956    /**
957     * Returns the current playback sample rate rate in Hz.
958     */
959    public int getPlaybackRate() {
960        return native_get_playback_rate();
961    }
962
963    /**
964     * Returns the current playback settings.
965     * See {@link #setPlaybackSettings(PlaybackSettings)} to set playback settings
966     * @return current {@link PlaybackSettings}.
967     * @throws IllegalStateException if track is not initialized.
968     */
969    public @NonNull PlaybackSettings getPlaybackSettings() {
970        float[] floatArray = new float[2];
971        int[] intArray = new int[2];
972        native_get_playback_settings(floatArray, intArray);
973        return new PlaybackSettings()
974                .setSpeed(floatArray[0])
975                .setPitch(floatArray[1])
976                .setAudioFallbackMode(intArray[0])
977                .setAudioStretchMode(intArray[1]);
978    }
979
980    /**
981     * Returns the configured audio data encoding. See {@link AudioFormat#ENCODING_PCM_8BIT},
982     * {@link AudioFormat#ENCODING_PCM_16BIT}, and {@link AudioFormat#ENCODING_PCM_FLOAT}.
983     */
984    public int getAudioFormat() {
985        return mAudioFormat;
986    }
987
988    /**
989     * Returns the type of audio stream this AudioTrack is configured for.
990     * Compare the result against {@link AudioManager#STREAM_VOICE_CALL},
991     * {@link AudioManager#STREAM_SYSTEM}, {@link AudioManager#STREAM_RING},
992     * {@link AudioManager#STREAM_MUSIC}, {@link AudioManager#STREAM_ALARM},
993     * {@link AudioManager#STREAM_NOTIFICATION}, or {@link AudioManager#STREAM_DTMF}.
994     */
995    public int getStreamType() {
996        return mStreamType;
997    }
998
999    /**
1000     * Returns the configured channel position mask.
1001     * <p> For example, refer to {@link AudioFormat#CHANNEL_OUT_MONO},
1002     * {@link AudioFormat#CHANNEL_OUT_STEREO}, {@link AudioFormat#CHANNEL_OUT_5POINT1}.
1003     * This method may return {@link AudioFormat#CHANNEL_INVALID} if
1004     * a channel index mask is used. Consider
1005     * {@link #getFormat()} instead, to obtain an {@link AudioFormat},
1006     * which contains both the channel position mask and the channel index mask.
1007     */
1008    public int getChannelConfiguration() {
1009        return mChannelConfiguration;
1010    }
1011
1012    /**
1013     * Returns the configured <code>AudioTrack</code> format.
1014     * @return an {@link AudioFormat} containing the
1015     * <code>AudioTrack</code> parameters at the time of configuration.
1016     */
1017    public @NonNull AudioFormat getFormat() {
1018        AudioFormat.Builder builder = new AudioFormat.Builder()
1019            .setSampleRate(mSampleRate)
1020            .setEncoding(mAudioFormat);
1021        if (mChannelConfiguration != AudioFormat.CHANNEL_INVALID) {
1022            builder.setChannelMask(mChannelConfiguration);
1023        }
1024        if (mChannelIndexMask != AudioFormat.CHANNEL_INVALID /* 0 */) {
1025            builder.setChannelIndexMask(mChannelIndexMask);
1026        }
1027        return builder.build();
1028    }
1029
1030    /**
1031     * Returns the configured number of channels.
1032     */
1033    public int getChannelCount() {
1034        return mChannelCount;
1035    }
1036
1037    /**
1038     * Returns the state of the AudioTrack instance. This is useful after the
1039     * AudioTrack instance has been created to check if it was initialized
1040     * properly. This ensures that the appropriate resources have been acquired.
1041     * @see #STATE_INITIALIZED
1042     * @see #STATE_NO_STATIC_DATA
1043     * @see #STATE_UNINITIALIZED
1044     */
1045    public int getState() {
1046        return mState;
1047    }
1048
1049    /**
1050     * Returns the playback state of the AudioTrack instance.
1051     * @see #PLAYSTATE_STOPPED
1052     * @see #PLAYSTATE_PAUSED
1053     * @see #PLAYSTATE_PLAYING
1054     */
1055    public int getPlayState() {
1056        synchronized (mPlayStateLock) {
1057            return mPlayState;
1058        }
1059    }
1060
1061    /**
1062     *  Returns the "native frame count" of the <code>AudioTrack</code> buffer.
1063     *  <p> If the track's creation mode is {@link #MODE_STATIC},
1064     *  it is equal to the specified bufferSizeInBytes on construction, converted to frame units.
1065     *  A static track's native frame count will not change.
1066     *  <p> If the track's creation mode is {@link #MODE_STREAM},
1067     *  it is greater than or equal to the specified bufferSizeInBytes converted to frame units.
1068     *  For streaming tracks, this value may be rounded up to a larger value if needed by
1069     *  the target output sink, and
1070     *  if the track is subsequently routed to a different output sink, the native
1071     *  frame count may enlarge to accommodate.
1072     *  See also {@link AudioManager#getProperty(String)} for key
1073     *  {@link AudioManager#PROPERTY_OUTPUT_FRAMES_PER_BUFFER}.
1074     *  @return current size in frames of the audio track buffer.
1075     *  @throws IllegalStateException
1076     */
1077    public int getNativeFrameCount() throws IllegalStateException {
1078        return native_get_native_frame_count();
1079    }
1080
1081    /**
1082     * Returns marker position expressed in frames.
1083     * @return marker position in wrapping frame units similar to {@link #getPlaybackHeadPosition},
1084     * or zero if marker is disabled.
1085     */
1086    public int getNotificationMarkerPosition() {
1087        return native_get_marker_pos();
1088    }
1089
1090    /**
1091     * Returns the notification update period expressed in frames.
1092     * Zero means that no position update notifications are being delivered.
1093     */
1094    public int getPositionNotificationPeriod() {
1095        return native_get_pos_update_period();
1096    }
1097
1098    /**
1099     * Returns the playback head position expressed in frames.
1100     * Though the "int" type is signed 32-bits, the value should be reinterpreted as if it is
1101     * unsigned 32-bits.  That is, the next position after 0x7FFFFFFF is (int) 0x80000000.
1102     * This is a continuously advancing counter.  It will wrap (overflow) periodically,
1103     * for example approximately once every 27:03:11 hours:minutes:seconds at 44.1 kHz.
1104     * It is reset to zero by {@link #flush()}, {@link #reloadStaticData()}, and {@link #stop()}.
1105     * If the track's creation mode is {@link #MODE_STATIC}, the return value indicates
1106     * the total number of frames played since reset,
1107     * <i>not</i> the current offset within the buffer.
1108     */
1109    public int getPlaybackHeadPosition() {
1110        return native_get_position();
1111    }
1112
1113    /**
1114     * Returns this track's estimated latency in milliseconds. This includes the latency due
1115     * to AudioTrack buffer size, AudioMixer (if any) and audio hardware driver.
1116     *
1117     * DO NOT UNHIDE. The existing approach for doing A/V sync has too many problems. We need
1118     * a better solution.
1119     * @hide
1120     */
1121    public int getLatency() {
1122        return native_get_latency();
1123    }
1124
1125    /**
1126     *  Returns the output sample rate in Hz for the specified stream type.
1127     */
1128    static public int getNativeOutputSampleRate(int streamType) {
1129        return native_get_output_sample_rate(streamType);
1130    }
1131
1132    /**
1133     * Returns the minimum buffer size required for the successful creation of an AudioTrack
1134     * object to be created in the {@link #MODE_STREAM} mode. Note that this size doesn't
1135     * guarantee a smooth playback under load, and higher values should be chosen according to
1136     * the expected frequency at which the buffer will be refilled with additional data to play.
1137     * For example, if you intend to dynamically set the source sample rate of an AudioTrack
1138     * to a higher value than the initial source sample rate, be sure to configure the buffer size
1139     * based on the highest planned sample rate.
1140     * @param sampleRateInHz the source sample rate expressed in Hz.
1141     * @param channelConfig describes the configuration of the audio channels.
1142     *   See {@link AudioFormat#CHANNEL_OUT_MONO} and
1143     *   {@link AudioFormat#CHANNEL_OUT_STEREO}
1144     * @param audioFormat the format in which the audio data is represented.
1145     *   See {@link AudioFormat#ENCODING_PCM_16BIT} and
1146     *   {@link AudioFormat#ENCODING_PCM_8BIT},
1147     *   and {@link AudioFormat#ENCODING_PCM_FLOAT}.
1148     * @return {@link #ERROR_BAD_VALUE} if an invalid parameter was passed,
1149     *   or {@link #ERROR} if unable to query for output properties,
1150     *   or the minimum buffer size expressed in bytes.
1151     */
1152    static public int getMinBufferSize(int sampleRateInHz, int channelConfig, int audioFormat) {
1153        int channelCount = 0;
1154        switch(channelConfig) {
1155        case AudioFormat.CHANNEL_OUT_MONO:
1156        case AudioFormat.CHANNEL_CONFIGURATION_MONO:
1157            channelCount = 1;
1158            break;
1159        case AudioFormat.CHANNEL_OUT_STEREO:
1160        case AudioFormat.CHANNEL_CONFIGURATION_STEREO:
1161            channelCount = 2;
1162            break;
1163        default:
1164            if (!isMultichannelConfigSupported(channelConfig)) {
1165                loge("getMinBufferSize(): Invalid channel configuration.");
1166                return ERROR_BAD_VALUE;
1167            } else {
1168                channelCount = AudioFormat.channelCountFromOutChannelMask(channelConfig);
1169            }
1170        }
1171
1172        if (!AudioFormat.isValidEncoding(audioFormat)) {
1173            loge("getMinBufferSize(): Invalid audio format.");
1174            return ERROR_BAD_VALUE;
1175        }
1176
1177        // sample rate, note these values are subject to change
1178        if ( (sampleRateInHz < SAMPLE_RATE_HZ_MIN) || (sampleRateInHz > SAMPLE_RATE_HZ_MAX) ) {
1179            loge("getMinBufferSize(): " + sampleRateInHz + " Hz is not a supported sample rate.");
1180            return ERROR_BAD_VALUE;
1181        }
1182
1183        int size = native_get_min_buff_size(sampleRateInHz, channelCount, audioFormat);
1184        if (size <= 0) {
1185            loge("getMinBufferSize(): error querying hardware");
1186            return ERROR;
1187        }
1188        else {
1189            return size;
1190        }
1191    }
1192
1193    /**
1194     * Returns the audio session ID.
1195     *
1196     * @return the ID of the audio session this AudioTrack belongs to.
1197     */
1198    public int getAudioSessionId() {
1199        return mSessionId;
1200    }
1201
1202   /**
1203    * Poll for a timestamp on demand.
1204    * <p>
1205    * If you need to track timestamps during initial warmup or after a routing or mode change,
1206    * you should request a new timestamp once per second until the reported timestamps
1207    * show that the audio clock is stable.
1208    * Thereafter, query for a new timestamp approximately once every 10 seconds to once per minute.
1209    * Calling this method more often is inefficient.
1210    * It is also counter-productive to call this method more often than recommended,
1211    * because the short-term differences between successive timestamp reports are not meaningful.
1212    * If you need a high-resolution mapping between frame position and presentation time,
1213    * consider implementing that at application level, based on low-resolution timestamps.
1214    * <p>
1215    * The audio data at the returned position may either already have been
1216    * presented, or may have not yet been presented but is committed to be presented.
1217    * It is not possible to request the time corresponding to a particular position,
1218    * or to request the (fractional) position corresponding to a particular time.
1219    * If you need such features, consider implementing them at application level.
1220    *
1221    * @param timestamp a reference to a non-null AudioTimestamp instance allocated
1222    *        and owned by caller.
1223    * @return true if a timestamp is available, or false if no timestamp is available.
1224    *         If a timestamp if available,
1225    *         the AudioTimestamp instance is filled in with a position in frame units, together
1226    *         with the estimated time when that frame was presented or is committed to
1227    *         be presented.
1228    *         In the case that no timestamp is available, any supplied instance is left unaltered.
1229    *         A timestamp may be temporarily unavailable while the audio clock is stabilizing,
1230    *         or during and immediately after a route change.
1231    */
1232    // Add this text when the "on new timestamp" API is added:
1233    //   Use if you need to get the most recent timestamp outside of the event callback handler.
1234    public boolean getTimestamp(AudioTimestamp timestamp)
1235    {
1236        if (timestamp == null) {
1237            throw new IllegalArgumentException();
1238        }
1239        // It's unfortunate, but we have to either create garbage every time or use synchronized
1240        long[] longArray = new long[2];
1241        int ret = native_get_timestamp(longArray);
1242        if (ret != SUCCESS) {
1243            return false;
1244        }
1245        timestamp.framePosition = longArray[0];
1246        timestamp.nanoTime = longArray[1];
1247        return true;
1248    }
1249
1250
1251    //--------------------------------------------------------------------------
1252    // Initialization / configuration
1253    //--------------------
1254    /**
1255     * Sets the listener the AudioTrack notifies when a previously set marker is reached or
1256     * for each periodic playback head position update.
1257     * Notifications will be received in the same thread as the one in which the AudioTrack
1258     * instance was created.
1259     * @param listener
1260     */
1261    public void setPlaybackPositionUpdateListener(OnPlaybackPositionUpdateListener listener) {
1262        setPlaybackPositionUpdateListener(listener, null);
1263    }
1264
1265    /**
1266     * Sets the listener the AudioTrack notifies when a previously set marker is reached or
1267     * for each periodic playback head position update.
1268     * Use this method to receive AudioTrack events in the Handler associated with another
1269     * thread than the one in which you created the AudioTrack instance.
1270     * @param listener
1271     * @param handler the Handler that will receive the event notification messages.
1272     */
1273    public void setPlaybackPositionUpdateListener(OnPlaybackPositionUpdateListener listener,
1274                                                    Handler handler) {
1275        if (listener != null) {
1276            mEventHandlerDelegate = new NativePositionEventHandlerDelegate(this, listener, handler);
1277        } else {
1278            mEventHandlerDelegate = null;
1279        }
1280    }
1281
1282
1283    private static float clampGainOrLevel(float gainOrLevel) {
1284        if (Float.isNaN(gainOrLevel)) {
1285            throw new IllegalArgumentException();
1286        }
1287        if (gainOrLevel < GAIN_MIN) {
1288            gainOrLevel = GAIN_MIN;
1289        } else if (gainOrLevel > GAIN_MAX) {
1290            gainOrLevel = GAIN_MAX;
1291        }
1292        return gainOrLevel;
1293    }
1294
1295
1296     /**
1297     * Sets the specified left and right output gain values on the AudioTrack.
1298     * <p>Gain values are clamped to the closed interval [0.0, max] where
1299     * max is the value of {@link #getMaxVolume}.
1300     * A value of 0.0 results in zero gain (silence), and
1301     * a value of 1.0 means unity gain (signal unchanged).
1302     * The default value is 1.0 meaning unity gain.
1303     * <p>The word "volume" in the API name is historical; this is actually a linear gain.
1304     * @param leftGain output gain for the left channel.
1305     * @param rightGain output gain for the right channel
1306     * @return error code or success, see {@link #SUCCESS},
1307     *    {@link #ERROR_INVALID_OPERATION}
1308     * @deprecated Applications should use {@link #setVolume} instead, as it
1309     * more gracefully scales down to mono, and up to multi-channel content beyond stereo.
1310     */
1311    public int setStereoVolume(float leftGain, float rightGain) {
1312        if (isRestricted()) {
1313            return SUCCESS;
1314        }
1315        if (mState == STATE_UNINITIALIZED) {
1316            return ERROR_INVALID_OPERATION;
1317        }
1318
1319        leftGain = clampGainOrLevel(leftGain);
1320        rightGain = clampGainOrLevel(rightGain);
1321
1322        native_setVolume(leftGain, rightGain);
1323
1324        return SUCCESS;
1325    }
1326
1327
1328    /**
1329     * Sets the specified output gain value on all channels of this track.
1330     * <p>Gain values are clamped to the closed interval [0.0, max] where
1331     * max is the value of {@link #getMaxVolume}.
1332     * A value of 0.0 results in zero gain (silence), and
1333     * a value of 1.0 means unity gain (signal unchanged).
1334     * The default value is 1.0 meaning unity gain.
1335     * <p>This API is preferred over {@link #setStereoVolume}, as it
1336     * more gracefully scales down to mono, and up to multi-channel content beyond stereo.
1337     * <p>The word "volume" in the API name is historical; this is actually a linear gain.
1338     * @param gain output gain for all channels.
1339     * @return error code or success, see {@link #SUCCESS},
1340     *    {@link #ERROR_INVALID_OPERATION}
1341     */
1342    public int setVolume(float gain) {
1343        return setStereoVolume(gain, gain);
1344    }
1345
1346
1347    /**
1348     * Sets the playback sample rate for this track. This sets the sampling rate at which
1349     * the audio data will be consumed and played back
1350     * (as set by the sampleRateInHz parameter in the
1351     * {@link #AudioTrack(int, int, int, int, int, int)} constructor),
1352     * not the original sampling rate of the
1353     * content. For example, setting it to half the sample rate of the content will cause the
1354     * playback to last twice as long, but will also result in a pitch shift down by one octave.
1355     * The valid sample rate range is from 1 Hz to twice the value returned by
1356     * {@link #getNativeOutputSampleRate(int)}.
1357     * Use {@link #setPlaybackSettings(PlaybackSettings)} for speed control.
1358     * @param sampleRateInHz the sample rate expressed in Hz
1359     * @return error code or success, see {@link #SUCCESS}, {@link #ERROR_BAD_VALUE},
1360     *    {@link #ERROR_INVALID_OPERATION}
1361     */
1362    public int setPlaybackRate(int sampleRateInHz) {
1363        if (mState != STATE_INITIALIZED) {
1364            return ERROR_INVALID_OPERATION;
1365        }
1366        if (sampleRateInHz <= 0) {
1367            return ERROR_BAD_VALUE;
1368        }
1369        return native_set_playback_rate(sampleRateInHz);
1370    }
1371
1372
1373    /**
1374     * Sets the playback settings.
1375     * This method returns failure if it cannot apply the playback settings.
1376     * One possible cause is that the parameters for speed or pitch are out of range.
1377     * Another possible cause is that the <code>AudioTrack</code> is streaming
1378     * (see {@link #MODE_STREAM}) and the
1379     * buffer size is too small. For speeds greater than 1.0f, the <code>AudioTrack</code> buffer
1380     * on configuration must be larger than the speed multiplied by the minimum size
1381     * {@link #getMinBufferSize(int, int, int)}) to allow proper playback.
1382     * @param settings see {@link PlaybackSettings}. In particular,
1383     * speed, pitch, and audio mode should be set.
1384     * @throws IllegalArgumentException if the settings are invalid or not accepted.
1385     * @throws IllegalStateException if track is not initialized.
1386     */
1387    public void setPlaybackSettings(@NonNull PlaybackSettings settings) {
1388        if (settings == null) {
1389            throw new IllegalArgumentException("settings is null");
1390        }
1391        float[] floatArray;
1392        int[] intArray;
1393        try {
1394            floatArray = new float[] {
1395                    settings.getSpeed(),
1396                    settings.getPitch(),
1397            };
1398            intArray = new int[] {
1399                    settings.getAudioFallbackMode(),
1400                    settings.getAudioStretchMode(),
1401            };
1402        } catch (IllegalStateException e) {
1403            throw new IllegalArgumentException(e);
1404        }
1405        native_set_playback_settings(floatArray, intArray);
1406    }
1407
1408
1409    /**
1410     * Sets the position of the notification marker.  At most one marker can be active.
1411     * @param markerInFrames marker position in wrapping frame units similar to
1412     * {@link #getPlaybackHeadPosition}, or zero to disable the marker.
1413     * To set a marker at a position which would appear as zero due to wraparound,
1414     * a workaround is to use a non-zero position near zero, such as -1 or 1.
1415     * @return error code or success, see {@link #SUCCESS}, {@link #ERROR_BAD_VALUE},
1416     *  {@link #ERROR_INVALID_OPERATION}
1417     */
1418    public int setNotificationMarkerPosition(int markerInFrames) {
1419        if (mState == STATE_UNINITIALIZED) {
1420            return ERROR_INVALID_OPERATION;
1421        }
1422        return native_set_marker_pos(markerInFrames);
1423    }
1424
1425
1426    /**
1427     * Sets the period for the periodic notification event.
1428     * @param periodInFrames update period expressed in frames.
1429     * Zero period means no position updates.  A negative period is not allowed.
1430     * @return error code or success, see {@link #SUCCESS}, {@link #ERROR_INVALID_OPERATION}
1431     */
1432    public int setPositionNotificationPeriod(int periodInFrames) {
1433        if (mState == STATE_UNINITIALIZED) {
1434            return ERROR_INVALID_OPERATION;
1435        }
1436        return native_set_pos_update_period(periodInFrames);
1437    }
1438
1439
1440    /**
1441     * Sets the playback head position within the static buffer.
1442     * The track must be stopped or paused for the position to be changed,
1443     * and must use the {@link #MODE_STATIC} mode.
1444     * @param positionInFrames playback head position within buffer, expressed in frames.
1445     * Zero corresponds to start of buffer.
1446     * The position must not be greater than the buffer size in frames, or negative.
1447     * Though this method and {@link #getPlaybackHeadPosition()} have similar names,
1448     * the position values have different meanings.
1449     * <br>
1450     * If looping is currently enabled and the new position is greater than or equal to the
1451     * loop end marker, the behavior varies by API level: for API level 22 and above,
1452     * the looping is first disabled and then the position is set.
1453     * For earlier API levels, the behavior is unspecified.
1454     * @return error code or success, see {@link #SUCCESS}, {@link #ERROR_BAD_VALUE},
1455     *    {@link #ERROR_INVALID_OPERATION}
1456     */
1457    public int setPlaybackHeadPosition(int positionInFrames) {
1458        if (mDataLoadMode == MODE_STREAM || mState == STATE_UNINITIALIZED ||
1459                getPlayState() == PLAYSTATE_PLAYING) {
1460            return ERROR_INVALID_OPERATION;
1461        }
1462        if (!(0 <= positionInFrames && positionInFrames <= mNativeBufferSizeInFrames)) {
1463            return ERROR_BAD_VALUE;
1464        }
1465        return native_set_position(positionInFrames);
1466    }
1467
1468    /**
1469     * Sets the loop points and the loop count. The loop can be infinite.
1470     * Similarly to setPlaybackHeadPosition,
1471     * the track must be stopped or paused for the loop points to be changed,
1472     * and must use the {@link #MODE_STATIC} mode.
1473     * @param startInFrames loop start marker expressed in frames.
1474     * Zero corresponds to start of buffer.
1475     * The start marker must not be greater than or equal to the buffer size in frames, or negative.
1476     * @param endInFrames loop end marker expressed in frames.
1477     * The total buffer size in frames corresponds to end of buffer.
1478     * The end marker must not be greater than the buffer size in frames.
1479     * For looping, the end marker must not be less than or equal to the start marker,
1480     * but to disable looping
1481     * it is permitted for start marker, end marker, and loop count to all be 0.
1482     * If any input parameters are out of range, this method returns {@link #ERROR_BAD_VALUE}.
1483     * If the loop period (endInFrames - startInFrames) is too small for the implementation to
1484     * support,
1485     * {@link #ERROR_BAD_VALUE} is returned.
1486     * The loop range is the interval [startInFrames, endInFrames).
1487     * <br>
1488     * For API level 22 and above, the position is left unchanged,
1489     * unless it is greater than or equal to the loop end marker, in which case
1490     * it is forced to the loop start marker.
1491     * For earlier API levels, the effect on position is unspecified.
1492     * @param loopCount the number of times the loop is looped; must be greater than or equal to -1.
1493     *    A value of -1 means infinite looping, and 0 disables looping.
1494     *    A value of positive N means to "loop" (go back) N times.  For example,
1495     *    a value of one means to play the region two times in total.
1496     * @return error code or success, see {@link #SUCCESS}, {@link #ERROR_BAD_VALUE},
1497     *    {@link #ERROR_INVALID_OPERATION}
1498     */
1499    public int setLoopPoints(int startInFrames, int endInFrames, int loopCount) {
1500        if (mDataLoadMode == MODE_STREAM || mState == STATE_UNINITIALIZED ||
1501                getPlayState() == PLAYSTATE_PLAYING) {
1502            return ERROR_INVALID_OPERATION;
1503        }
1504        if (loopCount == 0) {
1505            ;   // explicitly allowed as an exception to the loop region range check
1506        } else if (!(0 <= startInFrames && startInFrames < mNativeBufferSizeInFrames &&
1507                startInFrames < endInFrames && endInFrames <= mNativeBufferSizeInFrames)) {
1508            return ERROR_BAD_VALUE;
1509        }
1510        return native_set_loop(startInFrames, endInFrames, loopCount);
1511    }
1512
1513    /**
1514     * Sets the initialization state of the instance. This method was originally intended to be used
1515     * in an AudioTrack subclass constructor to set a subclass-specific post-initialization state.
1516     * However, subclasses of AudioTrack are no longer recommended, so this method is obsolete.
1517     * @param state the state of the AudioTrack instance
1518     * @deprecated Only accessible by subclasses, which are not recommended for AudioTrack.
1519     */
1520    @Deprecated
1521    protected void setState(int state) {
1522        mState = state;
1523    }
1524
1525
1526    //---------------------------------------------------------
1527    // Transport control methods
1528    //--------------------
1529    /**
1530     * Starts playing an AudioTrack.
1531     * If track's creation mode is {@link #MODE_STATIC}, you must have called one of
1532     * the {@link #write(byte[], int, int)}, {@link #write(short[], int, int)},
1533     * or {@link #write(float[], int, int, int)} methods.
1534     * If the mode is {@link #MODE_STREAM}, you can optionally prime the
1535     * output buffer by writing up to bufferSizeInBytes (from constructor) before starting.
1536     * This priming will avoid an immediate underrun, but is not required.
1537     *
1538     * @throws IllegalStateException
1539     */
1540    public void play()
1541    throws IllegalStateException {
1542        if (mState != STATE_INITIALIZED) {
1543            throw new IllegalStateException("play() called on uninitialized AudioTrack.");
1544        }
1545        if (isRestricted()) {
1546            setVolume(0);
1547        }
1548        synchronized(mPlayStateLock) {
1549            native_start();
1550            mPlayState = PLAYSTATE_PLAYING;
1551        }
1552    }
1553
1554    private boolean isRestricted() {
1555        if ((mAttributes.getFlags() & AudioAttributes.FLAG_BYPASS_INTERRUPTION_POLICY) != 0) {
1556            return false;
1557        }
1558        try {
1559            final int usage = AudioAttributes.usageForLegacyStreamType(mStreamType);
1560            final int mode = mAppOps.checkAudioOperation(AppOpsManager.OP_PLAY_AUDIO, usage,
1561                    Process.myUid(), ActivityThread.currentPackageName());
1562            return mode != AppOpsManager.MODE_ALLOWED;
1563        } catch (RemoteException e) {
1564            return false;
1565        }
1566    }
1567
1568    /**
1569     * Stops playing the audio data.
1570     * When used on an instance created in {@link #MODE_STREAM} mode, audio will stop playing
1571     * after the last buffer that was written has been played. For an immediate stop, use
1572     * {@link #pause()}, followed by {@link #flush()} to discard audio data that hasn't been played
1573     * back yet.
1574     * @throws IllegalStateException
1575     */
1576    public void stop()
1577    throws IllegalStateException {
1578        if (mState != STATE_INITIALIZED) {
1579            throw new IllegalStateException("stop() called on uninitialized AudioTrack.");
1580        }
1581
1582        // stop playing
1583        synchronized(mPlayStateLock) {
1584            native_stop();
1585            mPlayState = PLAYSTATE_STOPPED;
1586            mAvSyncHeader = null;
1587            mAvSyncBytesRemaining = 0;
1588        }
1589    }
1590
1591    /**
1592     * Pauses the playback of the audio data. Data that has not been played
1593     * back will not be discarded. Subsequent calls to {@link #play} will play
1594     * this data back. See {@link #flush()} to discard this data.
1595     *
1596     * @throws IllegalStateException
1597     */
1598    public void pause()
1599    throws IllegalStateException {
1600        if (mState != STATE_INITIALIZED) {
1601            throw new IllegalStateException("pause() called on uninitialized AudioTrack.");
1602        }
1603        //logd("pause()");
1604
1605        // pause playback
1606        synchronized(mPlayStateLock) {
1607            native_pause();
1608            mPlayState = PLAYSTATE_PAUSED;
1609        }
1610    }
1611
1612
1613    //---------------------------------------------------------
1614    // Audio data supply
1615    //--------------------
1616
1617    /**
1618     * Flushes the audio data currently queued for playback. Any data that has
1619     * been written but not yet presented will be discarded.  No-op if not stopped or paused,
1620     * or if the track's creation mode is not {@link #MODE_STREAM}.
1621     * <BR> Note that although data written but not yet presented is discarded, there is no
1622     * guarantee that all of the buffer space formerly used by that data
1623     * is available for a subsequent write.
1624     * For example, a call to {@link #write(byte[], int, int)} with <code>sizeInBytes</code>
1625     * less than or equal to the total buffer size
1626     * may return a short actual transfer count.
1627     */
1628    public void flush() {
1629        if (mState == STATE_INITIALIZED) {
1630            // flush the data in native layer
1631            native_flush();
1632            mAvSyncHeader = null;
1633            mAvSyncBytesRemaining = 0;
1634        }
1635
1636    }
1637
1638    /**
1639     * Writes the audio data to the audio sink for playback (streaming mode),
1640     * or copies audio data for later playback (static buffer mode).
1641     * The format specified in the AudioTrack constructor should be
1642     * {@link AudioFormat#ENCODING_PCM_8BIT} to correspond to the data in the array.
1643     * In streaming mode, will block until all data has been written to the audio sink.
1644     * In static buffer mode, copies the data to the buffer starting at offset 0.
1645     * Note that the actual playback of this data might occur after this function
1646     * returns. This function is thread safe with respect to {@link #stop} calls,
1647     * in which case all of the specified data might not be written to the audio sink.
1648     *
1649     * @param audioData the array that holds the data to play.
1650     * @param offsetInBytes the offset expressed in bytes in audioData where the data to play
1651     *    starts.
1652     * @param sizeInBytes the number of bytes to read in audioData after the offset.
1653     * @return the number of bytes that were written or {@link #ERROR_INVALID_OPERATION}
1654     *    if the object wasn't properly initialized, or {@link #ERROR_BAD_VALUE} if
1655     *    the parameters don't resolve to valid data and indexes, or
1656     *    {@link AudioManager#ERROR_DEAD_OBJECT} if the AudioTrack is not valid anymore and
1657     *    needs to be recreated.
1658     */
1659    public int write(@NonNull byte[] audioData, int offsetInBytes, int sizeInBytes) {
1660        return write(audioData, offsetInBytes, sizeInBytes, WRITE_BLOCKING);
1661    }
1662
1663    /**
1664     * Writes the audio data to the audio sink for playback (streaming mode),
1665     * or copies audio data for later playback (static buffer mode).
1666     * The format specified in the AudioTrack constructor should be
1667     * {@link AudioFormat#ENCODING_PCM_8BIT} to correspond to the data in the array.
1668     * In streaming mode, will block until all data has been written to the audio sink.
1669     * In static buffer mode, copies the data to the buffer starting at offset 0.
1670     * Note that the actual playback of this data might occur after this function
1671     * returns. This function is thread safe with respect to {@link #stop} calls,
1672     * in which case all of the specified data might not be written to the audio sink.
1673     *
1674     * @param audioData the array that holds the data to play.
1675     * @param offsetInBytes the offset expressed in bytes in audioData where the data to play
1676     *    starts.
1677     * @param sizeInBytes the number of bytes to read in audioData after the offset.
1678     * @param writeMode one of {@link #WRITE_BLOCKING}, {@link #WRITE_NON_BLOCKING}. It has no
1679     *     effect in static mode.
1680     *     <br>With {@link #WRITE_BLOCKING}, the write will block until all data has been written
1681     *         to the audio sink.
1682     *     <br>With {@link #WRITE_NON_BLOCKING}, the write will return immediately after
1683     *     queuing as much audio data for playback as possible without blocking.
1684     * @return the number of bytes that were written or {@link #ERROR_INVALID_OPERATION}
1685     *    if the object wasn't properly initialized, or {@link #ERROR_BAD_VALUE} if
1686     *    the parameters don't resolve to valid data and indexes, or
1687     *    {@link AudioManager#ERROR_DEAD_OBJECT} if the AudioTrack is not valid anymore and
1688     *    needs to be recreated.
1689     */
1690    public int write(@NonNull byte[] audioData, int offsetInBytes, int sizeInBytes,
1691            @WriteMode int writeMode) {
1692
1693        if (mState == STATE_UNINITIALIZED || mAudioFormat == AudioFormat.ENCODING_PCM_FLOAT) {
1694            return ERROR_INVALID_OPERATION;
1695        }
1696
1697        if ((writeMode != WRITE_BLOCKING) && (writeMode != WRITE_NON_BLOCKING)) {
1698            Log.e(TAG, "AudioTrack.write() called with invalid blocking mode");
1699            return ERROR_BAD_VALUE;
1700        }
1701
1702        if ( (audioData == null) || (offsetInBytes < 0 ) || (sizeInBytes < 0)
1703                || (offsetInBytes + sizeInBytes < 0)    // detect integer overflow
1704                || (offsetInBytes + sizeInBytes > audioData.length)) {
1705            return ERROR_BAD_VALUE;
1706        }
1707
1708        int ret = native_write_byte(audioData, offsetInBytes, sizeInBytes, mAudioFormat,
1709                writeMode == WRITE_BLOCKING);
1710
1711        if ((mDataLoadMode == MODE_STATIC)
1712                && (mState == STATE_NO_STATIC_DATA)
1713                && (ret > 0)) {
1714            // benign race with respect to other APIs that read mState
1715            mState = STATE_INITIALIZED;
1716        }
1717
1718        return ret;
1719    }
1720
1721    /**
1722     * Writes the audio data to the audio sink for playback (streaming mode),
1723     * or copies audio data for later playback (static buffer mode).
1724     * The format specified in the AudioTrack constructor should be
1725     * {@link AudioFormat#ENCODING_PCM_16BIT} to correspond to the data in the array.
1726     * In streaming mode, will block until all data has been written to the audio sink.
1727     * In static buffer mode, copies the data to the buffer starting at offset 0.
1728     * Note that the actual playback of this data might occur after this function
1729     * returns. This function is thread safe with respect to {@link #stop} calls,
1730     * in which case all of the specified data might not be written to the audio sink.
1731     *
1732     * @param audioData the array that holds the data to play.
1733     * @param offsetInShorts the offset expressed in shorts in audioData where the data to play
1734     *     starts.
1735     * @param sizeInShorts the number of shorts to read in audioData after the offset.
1736     * @return the number of shorts that were written or {@link #ERROR_INVALID_OPERATION}
1737     *    if the object wasn't properly initialized, or {@link #ERROR_BAD_VALUE} if
1738     *    the parameters don't resolve to valid data and indexes, or
1739     *    {@link AudioManager#ERROR_DEAD_OBJECT} if the AudioTrack is not valid anymore and
1740     *    needs to be recreated.
1741     */
1742    public int write(@NonNull short[] audioData, int offsetInShorts, int sizeInShorts) {
1743        return write(audioData, offsetInShorts, sizeInShorts, WRITE_BLOCKING);
1744    }
1745
1746    /**
1747     * Writes the audio data to the audio sink for playback (streaming mode),
1748     * or copies audio data for later playback (static buffer mode).
1749     * The format specified in the AudioTrack constructor should be
1750     * {@link AudioFormat#ENCODING_PCM_16BIT} to correspond to the data in the array.
1751     * In streaming mode, will block until all data has been written to the audio sink.
1752     * In static buffer mode, copies the data to the buffer starting at offset 0.
1753     * Note that the actual playback of this data might occur after this function
1754     * returns. This function is thread safe with respect to {@link #stop} calls,
1755     * in which case all of the specified data might not be written to the audio sink.
1756     *
1757     * @param audioData the array that holds the data to play.
1758     * @param offsetInShorts the offset expressed in shorts in audioData where the data to play
1759     *     starts.
1760     * @param sizeInShorts the number of shorts to read in audioData after the offset.
1761     * @param writeMode one of {@link #WRITE_BLOCKING}, {@link #WRITE_NON_BLOCKING}. It has no
1762     *     effect in static mode.
1763     *     <br>With {@link #WRITE_BLOCKING}, the write will block until all data has been written
1764     *         to the audio sink.
1765     *     <br>With {@link #WRITE_NON_BLOCKING}, the write will return immediately after
1766     *     queuing as much audio data for playback as possible without blocking.
1767     * @return the number of shorts that were written or {@link #ERROR_INVALID_OPERATION}
1768     *    if the object wasn't properly initialized, or {@link #ERROR_BAD_VALUE} if
1769     *    the parameters don't resolve to valid data and indexes, or
1770     *    {@link AudioManager#ERROR_DEAD_OBJECT} if the AudioTrack is not valid anymore and
1771     *    needs to be recreated.
1772     */
1773    public int write(@NonNull short[] audioData, int offsetInShorts, int sizeInShorts,
1774            @WriteMode int writeMode) {
1775
1776        if (mState == STATE_UNINITIALIZED || mAudioFormat == AudioFormat.ENCODING_PCM_FLOAT) {
1777            return ERROR_INVALID_OPERATION;
1778        }
1779
1780        if ((writeMode != WRITE_BLOCKING) && (writeMode != WRITE_NON_BLOCKING)) {
1781            Log.e(TAG, "AudioTrack.write() called with invalid blocking mode");
1782            return ERROR_BAD_VALUE;
1783        }
1784
1785        if ( (audioData == null) || (offsetInShorts < 0 ) || (sizeInShorts < 0)
1786                || (offsetInShorts + sizeInShorts < 0)  // detect integer overflow
1787                || (offsetInShorts + sizeInShorts > audioData.length)) {
1788            return ERROR_BAD_VALUE;
1789        }
1790
1791        int ret = native_write_short(audioData, offsetInShorts, sizeInShorts, mAudioFormat,
1792                writeMode == WRITE_BLOCKING);
1793
1794        if ((mDataLoadMode == MODE_STATIC)
1795                && (mState == STATE_NO_STATIC_DATA)
1796                && (ret > 0)) {
1797            // benign race with respect to other APIs that read mState
1798            mState = STATE_INITIALIZED;
1799        }
1800
1801        return ret;
1802    }
1803
1804    /**
1805     * Writes the audio data to the audio sink for playback (streaming mode),
1806     * or copies audio data for later playback (static buffer mode).
1807     * The format specified in the AudioTrack constructor should be
1808     * {@link AudioFormat#ENCODING_PCM_FLOAT} to correspond to the data in the array.
1809     * In static buffer mode, copies the data to the buffer starting at offset 0,
1810     * and the write mode is ignored.
1811     * In streaming mode, the blocking behavior will depend on the write mode.
1812     * <p>
1813     * Note that the actual playback of this data might occur after this function
1814     * returns. This function is thread safe with respect to {@link #stop} calls,
1815     * in which case all of the specified data might not be written to the audio sink.
1816     * <p>
1817     * @param audioData the array that holds the data to play.
1818     *     The implementation does not clip for sample values within the nominal range
1819     *     [-1.0f, 1.0f], provided that all gains in the audio pipeline are
1820     *     less than or equal to unity (1.0f), and in the absence of post-processing effects
1821     *     that could add energy, such as reverb.  For the convenience of applications
1822     *     that compute samples using filters with non-unity gain,
1823     *     sample values +3 dB beyond the nominal range are permitted.
1824     *     However such values may eventually be limited or clipped, depending on various gains
1825     *     and later processing in the audio path.  Therefore applications are encouraged
1826     *     to provide samples values within the nominal range.
1827     * @param offsetInFloats the offset, expressed as a number of floats,
1828     *     in audioData where the data to play starts.
1829     * @param sizeInFloats the number of floats to read in audioData after the offset.
1830     * @param writeMode one of {@link #WRITE_BLOCKING}, {@link #WRITE_NON_BLOCKING}. It has no
1831     *     effect in static mode.
1832     *     <br>With {@link #WRITE_BLOCKING}, the write will block until all data has been written
1833     *         to the audio sink.
1834     *     <br>With {@link #WRITE_NON_BLOCKING}, the write will return immediately after
1835     *     queuing as much audio data for playback as possible without blocking.
1836     * @return the number of floats that were written, or {@link #ERROR_INVALID_OPERATION}
1837     *    if the object wasn't properly initialized, or {@link #ERROR_BAD_VALUE} if
1838     *    the parameters don't resolve to valid data and indexes, or
1839     *    {@link AudioManager#ERROR_DEAD_OBJECT} if the AudioTrack is not valid anymore and
1840     *    needs to be recreated.
1841     */
1842    public int write(@NonNull float[] audioData, int offsetInFloats, int sizeInFloats,
1843            @WriteMode int writeMode) {
1844
1845        if (mState == STATE_UNINITIALIZED) {
1846            Log.e(TAG, "AudioTrack.write() called in invalid state STATE_UNINITIALIZED");
1847            return ERROR_INVALID_OPERATION;
1848        }
1849
1850        if (mAudioFormat != AudioFormat.ENCODING_PCM_FLOAT) {
1851            Log.e(TAG, "AudioTrack.write(float[] ...) requires format ENCODING_PCM_FLOAT");
1852            return ERROR_INVALID_OPERATION;
1853        }
1854
1855        if ((writeMode != WRITE_BLOCKING) && (writeMode != WRITE_NON_BLOCKING)) {
1856            Log.e(TAG, "AudioTrack.write() called with invalid blocking mode");
1857            return ERROR_BAD_VALUE;
1858        }
1859
1860        if ( (audioData == null) || (offsetInFloats < 0 ) || (sizeInFloats < 0)
1861                || (offsetInFloats + sizeInFloats < 0)  // detect integer overflow
1862                || (offsetInFloats + sizeInFloats > audioData.length)) {
1863            Log.e(TAG, "AudioTrack.write() called with invalid array, offset, or size");
1864            return ERROR_BAD_VALUE;
1865        }
1866
1867        int ret = native_write_float(audioData, offsetInFloats, sizeInFloats, mAudioFormat,
1868                writeMode == WRITE_BLOCKING);
1869
1870        if ((mDataLoadMode == MODE_STATIC)
1871                && (mState == STATE_NO_STATIC_DATA)
1872                && (ret > 0)) {
1873            // benign race with respect to other APIs that read mState
1874            mState = STATE_INITIALIZED;
1875        }
1876
1877        return ret;
1878    }
1879
1880
1881    /**
1882     * Writes the audio data to the audio sink for playback (streaming mode),
1883     * or copies audio data for later playback (static buffer mode).
1884     * In static buffer mode, copies the data to the buffer starting at its 0 offset, and the write
1885     * mode is ignored.
1886     * In streaming mode, the blocking behavior will depend on the write mode.
1887     * @param audioData the buffer that holds the data to play, starting at the position reported
1888     *     by <code>audioData.position()</code>.
1889     *     <BR>Note that upon return, the buffer position (<code>audioData.position()</code>) will
1890     *     have been advanced to reflect the amount of data that was successfully written to
1891     *     the AudioTrack.
1892     * @param sizeInBytes number of bytes to write.
1893     *     <BR>Note this may differ from <code>audioData.remaining()</code>, but cannot exceed it.
1894     * @param writeMode one of {@link #WRITE_BLOCKING}, {@link #WRITE_NON_BLOCKING}. It has no
1895     *     effect in static mode.
1896     *     <BR>With {@link #WRITE_BLOCKING}, the write will block until all data has been written
1897     *         to the audio sink.
1898     *     <BR>With {@link #WRITE_NON_BLOCKING}, the write will return immediately after
1899     *     queuing as much audio data for playback as possible without blocking.
1900     * @return 0 or a positive number of bytes that were written, or
1901     *     {@link #ERROR_BAD_VALUE}, {@link #ERROR_INVALID_OPERATION}, or
1902     *     {@link AudioManager#ERROR_DEAD_OBJECT} if the AudioTrack is not valid anymore and
1903     *     needs to be recreated.
1904     */
1905    public int write(@NonNull ByteBuffer audioData, int sizeInBytes,
1906            @WriteMode int writeMode) {
1907
1908        if (mState == STATE_UNINITIALIZED) {
1909            Log.e(TAG, "AudioTrack.write() called in invalid state STATE_UNINITIALIZED");
1910            return ERROR_INVALID_OPERATION;
1911        }
1912
1913        if ((writeMode != WRITE_BLOCKING) && (writeMode != WRITE_NON_BLOCKING)) {
1914            Log.e(TAG, "AudioTrack.write() called with invalid blocking mode");
1915            return ERROR_BAD_VALUE;
1916        }
1917
1918        if ( (audioData == null) || (sizeInBytes < 0) || (sizeInBytes > audioData.remaining())) {
1919            Log.e(TAG, "AudioTrack.write() called with invalid size (" + sizeInBytes + ") value");
1920            return ERROR_BAD_VALUE;
1921        }
1922
1923        int ret = 0;
1924        if (audioData.isDirect()) {
1925            ret = native_write_native_bytes(audioData,
1926                    audioData.position(), sizeInBytes, mAudioFormat,
1927                    writeMode == WRITE_BLOCKING);
1928        } else {
1929            ret = native_write_byte(NioUtils.unsafeArray(audioData),
1930                    NioUtils.unsafeArrayOffset(audioData) + audioData.position(),
1931                    sizeInBytes, mAudioFormat,
1932                    writeMode == WRITE_BLOCKING);
1933        }
1934
1935        if ((mDataLoadMode == MODE_STATIC)
1936                && (mState == STATE_NO_STATIC_DATA)
1937                && (ret > 0)) {
1938            // benign race with respect to other APIs that read mState
1939            mState = STATE_INITIALIZED;
1940        }
1941
1942        if (ret > 0) {
1943            audioData.position(audioData.position() + ret);
1944        }
1945
1946        return ret;
1947    }
1948
1949    /**
1950     * Writes the audio data to the audio sink for playback (streaming mode) on a HW_AV_SYNC track.
1951     * In streaming mode, the blocking behavior will depend on the write mode.
1952     * @param audioData the buffer that holds the data to play, starting at the position reported
1953     *     by <code>audioData.position()</code>.
1954     *     <BR>Note that upon return, the buffer position (<code>audioData.position()</code>) will
1955     *     have been advanced to reflect the amount of data that was successfully written to
1956     *     the AudioTrack.
1957     * @param sizeInBytes number of bytes to write.
1958     *     <BR>Note this may differ from <code>audioData.remaining()</code>, but cannot exceed it.
1959     * @param writeMode one of {@link #WRITE_BLOCKING}, {@link #WRITE_NON_BLOCKING}.
1960     *     <BR>With {@link #WRITE_BLOCKING}, the write will block until all data has been written
1961     *         to the audio sink.
1962     *     <BR>With {@link #WRITE_NON_BLOCKING}, the write will return immediately after
1963     *     queuing as much audio data for playback as possible without blocking.
1964     * @param timestamp The timestamp of the first decodable audio frame in the provided audioData.
1965     * @return 0 or a positive number of bytes that were written, or
1966     *     {@link #ERROR_BAD_VALUE}, {@link #ERROR_INVALID_OPERATION}, or
1967     *     {@link AudioManager#ERROR_DEAD_OBJECT} if the AudioTrack is not valid anymore and
1968     *     needs to be recreated.
1969     */
1970    public int write(ByteBuffer audioData, int sizeInBytes,
1971            @WriteMode int writeMode, long timestamp) {
1972
1973        if ((mAttributes.getFlags() & AudioAttributes.FLAG_HW_AV_SYNC) == 0) {
1974            Log.d(TAG, "AudioTrack.write() called on a regular AudioTrack. Ignoring pts...");
1975            return write(audioData, sizeInBytes, writeMode);
1976        }
1977
1978        if ((audioData == null) || (sizeInBytes < 0) || (sizeInBytes > audioData.remaining())) {
1979            Log.e(TAG, "AudioTrack.write() called with invalid size (" + sizeInBytes + ") value");
1980            return ERROR_BAD_VALUE;
1981        }
1982
1983        // create timestamp header if none exists
1984        if (mAvSyncHeader == null) {
1985            mAvSyncHeader = ByteBuffer.allocate(16);
1986            mAvSyncHeader.order(ByteOrder.BIG_ENDIAN);
1987            mAvSyncHeader.putInt(0x55550001);
1988            mAvSyncHeader.putInt(sizeInBytes);
1989            mAvSyncHeader.putLong(timestamp);
1990            mAvSyncHeader.position(0);
1991            mAvSyncBytesRemaining = sizeInBytes;
1992        }
1993
1994        // write timestamp header if not completely written already
1995        int ret = 0;
1996        if (mAvSyncHeader.remaining() != 0) {
1997            ret = write(mAvSyncHeader, mAvSyncHeader.remaining(), writeMode);
1998            if (ret < 0) {
1999                Log.e(TAG, "AudioTrack.write() could not write timestamp header!");
2000                mAvSyncHeader = null;
2001                mAvSyncBytesRemaining = 0;
2002                return ret;
2003            }
2004            if (mAvSyncHeader.remaining() > 0) {
2005                Log.v(TAG, "AudioTrack.write() partial timestamp header written.");
2006                return 0;
2007            }
2008        }
2009
2010        // write audio data
2011        int sizeToWrite = Math.min(mAvSyncBytesRemaining, sizeInBytes);
2012        ret = write(audioData, sizeToWrite, writeMode);
2013        if (ret < 0) {
2014            Log.e(TAG, "AudioTrack.write() could not write audio data!");
2015            mAvSyncHeader = null;
2016            mAvSyncBytesRemaining = 0;
2017            return ret;
2018        }
2019
2020        mAvSyncBytesRemaining -= ret;
2021        if (mAvSyncBytesRemaining == 0) {
2022            mAvSyncHeader = null;
2023        }
2024
2025        return ret;
2026    }
2027
2028
2029    /**
2030     * Sets the playback head position within the static buffer to zero,
2031     * that is it rewinds to start of static buffer.
2032     * The track must be stopped or paused, and
2033     * the track's creation mode must be {@link #MODE_STATIC}.
2034     * <p>
2035     * For API level 22 and above, also resets the value returned by
2036     * {@link #getPlaybackHeadPosition()} to zero.
2037     * For earlier API levels, the reset behavior is unspecified.
2038     * <p>
2039     * {@link #setPlaybackHeadPosition(int)} to zero
2040     * is recommended instead when the reset of {@link #getPlaybackHeadPosition} is not needed.
2041     * @return error code or success, see {@link #SUCCESS}, {@link #ERROR_BAD_VALUE},
2042     *  {@link #ERROR_INVALID_OPERATION}
2043     */
2044    public int reloadStaticData() {
2045        if (mDataLoadMode == MODE_STREAM || mState != STATE_INITIALIZED) {
2046            return ERROR_INVALID_OPERATION;
2047        }
2048        return native_reload_static();
2049    }
2050
2051    //--------------------------------------------------------------------------
2052    // Audio effects management
2053    //--------------------
2054
2055    /**
2056     * Attaches an auxiliary effect to the audio track. A typical auxiliary
2057     * effect is a reverberation effect which can be applied on any sound source
2058     * that directs a certain amount of its energy to this effect. This amount
2059     * is defined by setAuxEffectSendLevel().
2060     * {@see #setAuxEffectSendLevel(float)}.
2061     * <p>After creating an auxiliary effect (e.g.
2062     * {@link android.media.audiofx.EnvironmentalReverb}), retrieve its ID with
2063     * {@link android.media.audiofx.AudioEffect#getId()} and use it when calling
2064     * this method to attach the audio track to the effect.
2065     * <p>To detach the effect from the audio track, call this method with a
2066     * null effect id.
2067     *
2068     * @param effectId system wide unique id of the effect to attach
2069     * @return error code or success, see {@link #SUCCESS},
2070     *    {@link #ERROR_INVALID_OPERATION}, {@link #ERROR_BAD_VALUE}
2071     */
2072    public int attachAuxEffect(int effectId) {
2073        if (mState == STATE_UNINITIALIZED) {
2074            return ERROR_INVALID_OPERATION;
2075        }
2076        return native_attachAuxEffect(effectId);
2077    }
2078
2079    /**
2080     * Sets the send level of the audio track to the attached auxiliary effect
2081     * {@link #attachAuxEffect(int)}.  Effect levels
2082     * are clamped to the closed interval [0.0, max] where
2083     * max is the value of {@link #getMaxVolume}.
2084     * A value of 0.0 results in no effect, and a value of 1.0 is full send.
2085     * <p>By default the send level is 0.0f, so even if an effect is attached to the player
2086     * this method must be called for the effect to be applied.
2087     * <p>Note that the passed level value is a linear scalar. UI controls should be scaled
2088     * logarithmically: the gain applied by audio framework ranges from -72dB to at least 0dB,
2089     * so an appropriate conversion from linear UI input x to level is:
2090     * x == 0 -&gt; level = 0
2091     * 0 &lt; x &lt;= R -&gt; level = 10^(72*(x-R)/20/R)
2092     *
2093     * @param level linear send level
2094     * @return error code or success, see {@link #SUCCESS},
2095     *    {@link #ERROR_INVALID_OPERATION}, {@link #ERROR}
2096     */
2097    public int setAuxEffectSendLevel(float level) {
2098        if (isRestricted()) {
2099            return SUCCESS;
2100        }
2101        if (mState == STATE_UNINITIALIZED) {
2102            return ERROR_INVALID_OPERATION;
2103        }
2104        level = clampGainOrLevel(level);
2105        int err = native_setAuxEffectSendLevel(level);
2106        return err == 0 ? SUCCESS : ERROR;
2107    }
2108
2109    //--------------------------------------------------------------------------
2110    // Explicit Routing
2111    //--------------------
2112    private AudioDeviceInfo mPreferredDevice = null;
2113
2114    /**
2115     * Specifies an audio device (via an {@link AudioDeviceInfo} object) to route
2116     * the output from this AudioTrack.
2117     * @param deviceInfo The {@link AudioDeviceInfo} specifying the audio sink.
2118     *  If deviceInfo is null, default routing is restored.
2119     * @return true if succesful, false if the specified {@link AudioDeviceInfo} is non-null and
2120     * does not correspond to a valid audio output device.
2121     */
2122    public boolean setPreferredOutputDevice(AudioDeviceInfo deviceInfo) {
2123        // Do some validation....
2124        if (deviceInfo != null && !deviceInfo.isSink()) {
2125            return false;
2126        }
2127
2128        mPreferredDevice = deviceInfo;
2129        int preferredDeviceId = mPreferredDevice != null ? deviceInfo.getId() : 0;
2130
2131        return native_setOutputDevice(preferredDeviceId);
2132    }
2133
2134    /**
2135     * Returns the selected output specified by {@link #setPreferredOutputDevice}. Note that this
2136     * is not guaranteed to correspond to the actual device being used for playback.
2137     */
2138    public AudioDeviceInfo getPreferredOutputDevice() {
2139        return mPreferredDevice;
2140    }
2141
2142    //--------------------------------------------------------------------------
2143    // (Re)Routing Info
2144    //--------------------
2145    /**
2146     * Returns an {@link AudioDeviceInfo} identifying the current routing of this AudioTrack.
2147     */
2148    public AudioDeviceInfo getRoutedDevice() {
2149        return null;
2150    }
2151
2152    /**
2153     * The message sent to apps when the routing of this AudioTrack changes if they provide
2154     * a {#link Handler} object to addOnAudioTrackRoutingListener().
2155     */
2156    private ArrayMap<OnAudioTrackRoutingListener, NativeRoutingEventHandlerDelegate>
2157        mRoutingChangeListeners =
2158            new ArrayMap<OnAudioTrackRoutingListener, NativeRoutingEventHandlerDelegate>();
2159
2160    /**
2161     * Adds an {@link OnAudioTrackRoutingListener} to receive notifications of routing changes
2162     * on this AudioTrack.
2163     */
2164    public void addOnAudioTrackRoutingListener(OnAudioTrackRoutingListener listener,
2165            android.os.Handler handler) {
2166        if (listener != null && !mRoutingChangeListeners.containsKey(listener)) {
2167            synchronized (mRoutingChangeListeners) {
2168                mRoutingChangeListeners.put(
2169                    listener, new NativeRoutingEventHandlerDelegate(this, listener, handler));
2170            }
2171        }
2172    }
2173
2174    /**
2175     * Removes an {@link OnAudioTrackRoutingListener} which has been previously added
2176     * to receive notifications of changes to the set of connected audio devices.
2177     */
2178    public void removeOnAudioTrackRoutingListener(OnAudioTrackRoutingListener listener) {
2179        synchronized (mRoutingChangeListeners) {
2180            if (mRoutingChangeListeners.containsKey(listener)) {
2181                mRoutingChangeListeners.remove(listener);
2182            }
2183        }
2184    }
2185
2186    /**
2187     * Sends device list change notification to all listeners.
2188     */
2189    private void broadcastRoutingChange() {
2190        Collection<NativeRoutingEventHandlerDelegate> values;
2191        synchronized (mRoutingChangeListeners) {
2192            values = mRoutingChangeListeners.values();
2193        }
2194        for(NativeRoutingEventHandlerDelegate delegate : values) {
2195            Handler handler = delegate.getHandler();
2196            if (handler != null) {
2197                handler.sendEmptyMessage(NATIVE_EVENT_ROUTING_CHANGE);
2198            }
2199        }
2200    }
2201
2202    //---------------------------------------------------------
2203    // Interface definitions
2204    //--------------------
2205    /**
2206     * Interface definition for a callback to be invoked when the playback head position of
2207     * an AudioTrack has reached a notification marker or has increased by a certain period.
2208     */
2209    public interface OnPlaybackPositionUpdateListener  {
2210        /**
2211         * Called on the listener to notify it that the previously set marker has been reached
2212         * by the playback head.
2213         */
2214        void onMarkerReached(AudioTrack track);
2215
2216        /**
2217         * Called on the listener to periodically notify it that the playback head has reached
2218         * a multiple of the notification period.
2219         */
2220        void onPeriodicNotification(AudioTrack track);
2221    }
2222
2223    //---------------------------------------------------------
2224    // Inner classes
2225    //--------------------
2226    /**
2227     * Helper class to handle the forwarding of native events to the appropriate listener
2228     * (potentially) handled in a different thread
2229     */
2230    private class NativePositionEventHandlerDelegate {
2231        private final Handler mHandler;
2232
2233        NativePositionEventHandlerDelegate(final AudioTrack track,
2234                                   final OnPlaybackPositionUpdateListener listener,
2235                                   Handler handler) {
2236            // find the looper for our new event handler
2237            Looper looper;
2238            if (handler != null) {
2239                looper = handler.getLooper();
2240            } else {
2241                // no given handler, use the looper the AudioTrack was created in
2242                looper = mInitializationLooper;
2243            }
2244
2245            // construct the event handler with this looper
2246            if (looper != null) {
2247                // implement the event handler delegate
2248                mHandler = new Handler(looper) {
2249                    @Override
2250                    public void handleMessage(Message msg) {
2251                        if (track == null) {
2252                            return;
2253                        }
2254                        switch(msg.what) {
2255                        case NATIVE_EVENT_MARKER:
2256                            if (listener != null) {
2257                                listener.onMarkerReached(track);
2258                            }
2259                            break;
2260                        case NATIVE_EVENT_NEW_POS:
2261                            if (listener != null) {
2262                                listener.onPeriodicNotification(track);
2263                            }
2264                            break;
2265                        default:
2266                            loge("Unknown native event type: " + msg.what);
2267                            break;
2268                        }
2269                    }
2270                };
2271            } else {
2272                mHandler = null;
2273            }
2274        }
2275
2276        Handler getHandler() {
2277            return mHandler;
2278        }
2279    }
2280
2281    /**
2282     * Helper class to handle the forwarding of native events to the appropriate listener
2283     * (potentially) handled in a different thread
2284     */
2285    private class NativeRoutingEventHandlerDelegate {
2286        private final Handler mHandler;
2287
2288        NativeRoutingEventHandlerDelegate(final AudioTrack track,
2289                                   final OnAudioTrackRoutingListener listener,
2290                                   Handler handler) {
2291            // find the looper for our new event handler
2292            Looper looper;
2293            if (handler != null) {
2294                looper = handler.getLooper();
2295            } else {
2296                // no given handler, use the looper the AudioTrack was created in
2297                looper = mInitializationLooper;
2298            }
2299
2300            // construct the event handler with this looper
2301            if (looper != null) {
2302                // implement the event handler delegate
2303                mHandler = new Handler(looper) {
2304                    @Override
2305                    public void handleMessage(Message msg) {
2306                        if (track == null) {
2307                            return;
2308                        }
2309                        switch(msg.what) {
2310                        case NATIVE_EVENT_ROUTING_CHANGE:
2311                            if (listener != null) {
2312                                listener.onAudioTrackRouting(track);
2313                            }
2314                            break;
2315                        default:
2316                            loge("Unknown native event type: " + msg.what);
2317                            break;
2318                        }
2319                    }
2320                };
2321            } else {
2322                mHandler = null;
2323            }
2324        }
2325
2326        Handler getHandler() {
2327            return mHandler;
2328        }
2329    }
2330
2331    //---------------------------------------------------------
2332    // Java methods called from the native side
2333    //--------------------
2334    @SuppressWarnings("unused")
2335    private static void postEventFromNative(Object audiotrack_ref,
2336            int what, int arg1, int arg2, Object obj) {
2337        //logd("Event posted from the native side: event="+ what + " args="+ arg1+" "+arg2);
2338        AudioTrack track = (AudioTrack)((WeakReference)audiotrack_ref).get();
2339        if (track == null) {
2340            return;
2341        }
2342
2343        NativePositionEventHandlerDelegate delegate = track.mEventHandlerDelegate;
2344        if (delegate != null) {
2345            Handler handler = delegate.getHandler();
2346            if (handler != null) {
2347                Message m = handler.obtainMessage(what, arg1, arg2, obj);
2348                handler.sendMessage(m);
2349            }
2350        }
2351
2352    }
2353
2354
2355    //---------------------------------------------------------
2356    // Native methods called from the Java side
2357    //--------------------
2358
2359    // post-condition: mStreamType is overwritten with a value
2360    //     that reflects the audio attributes (e.g. an AudioAttributes object with a usage of
2361    //     AudioAttributes.USAGE_MEDIA will map to AudioManager.STREAM_MUSIC
2362    private native final int native_setup(Object /*WeakReference<AudioTrack>*/ audiotrack_this,
2363            Object /*AudioAttributes*/ attributes,
2364            int sampleRate, int channelMask, int audioFormat,
2365            int buffSizeInBytes, int mode, int[] sessionId);
2366
2367    private native final void native_finalize();
2368
2369    private native final void native_release();
2370
2371    private native final void native_start();
2372
2373    private native final void native_stop();
2374
2375    private native final void native_pause();
2376
2377    private native final void native_flush();
2378
2379    private native final int native_write_byte(byte[] audioData,
2380                                               int offsetInBytes, int sizeInBytes, int format,
2381                                               boolean isBlocking);
2382
2383    private native final int native_write_short(short[] audioData,
2384                                                int offsetInShorts, int sizeInShorts, int format,
2385                                                boolean isBlocking);
2386
2387    private native final int native_write_float(float[] audioData,
2388                                                int offsetInFloats, int sizeInFloats, int format,
2389                                                boolean isBlocking);
2390
2391    private native final int native_write_native_bytes(Object audioData,
2392            int positionInBytes, int sizeInBytes, int format, boolean blocking);
2393
2394    private native final int native_reload_static();
2395
2396    private native final int native_get_native_frame_count();
2397
2398    private native final void native_setVolume(float leftVolume, float rightVolume);
2399
2400    private native final int native_set_playback_rate(int sampleRateInHz);
2401    private native final int native_get_playback_rate();
2402
2403    // floatArray must be a non-null array of length >= 2
2404    // [0] is speed
2405    // [1] is pitch
2406    // intArray must be a non-null array of length >= 2
2407    // [0] is audio fallback mode
2408    // [1] is audio stretch mode
2409    private native final void native_set_playback_settings(float[] floatArray, int[] intArray);
2410    private native final void native_get_playback_settings(float[] floatArray, int[] intArray);
2411
2412    private native final int native_set_marker_pos(int marker);
2413    private native final int native_get_marker_pos();
2414
2415    private native final int native_set_pos_update_period(int updatePeriod);
2416    private native final int native_get_pos_update_period();
2417
2418    private native final int native_set_position(int position);
2419    private native final int native_get_position();
2420
2421    private native final int native_get_latency();
2422
2423    // longArray must be a non-null array of length >= 2
2424    // [0] is assigned the frame position
2425    // [1] is assigned the time in CLOCK_MONOTONIC nanoseconds
2426    private native final int native_get_timestamp(long[] longArray);
2427
2428    private native final int native_set_loop(int start, int end, int loopCount);
2429
2430    static private native final int native_get_output_sample_rate(int streamType);
2431    static private native final int native_get_min_buff_size(
2432            int sampleRateInHz, int channelConfig, int audioFormat);
2433
2434    private native final int native_attachAuxEffect(int effectId);
2435    private native final int native_setAuxEffectSendLevel(float level);
2436
2437    private native final boolean native_setOutputDevice(int deviceId);
2438
2439    //---------------------------------------------------------
2440    // Utility methods
2441    //------------------
2442
2443    private static void logd(String msg) {
2444        Log.d(TAG, msg);
2445    }
2446
2447    private static void loge(String msg) {
2448        Log.e(TAG, msg);
2449    }
2450}
2451