1 /*
2  * Copyright 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef AAUDIO_AUDIOSTREAM_H
18 #define AAUDIO_AUDIOSTREAM_H
19 
20 #include <atomic>
21 #include <mutex>
22 #include <stdint.h>
23 #include <aaudio/AAudio.h>
24 #include <binder/IServiceManager.h>
25 #include <binder/Status.h>
26 #include <utils/StrongPointer.h>
27 
28 #include "media/VolumeShaper.h"
29 #include "media/PlayerBase.h"
30 #include "utility/AAudioUtilities.h"
31 #include "utility/MonotonicCounter.h"
32 
33 // Cannot get android::media::VolumeShaper to compile!
34 #define AAUDIO_USE_VOLUME_SHAPER  0
35 
36 namespace aaudio {
37 
38 typedef void *(*aaudio_audio_thread_proc_t)(void *);
39 typedef uint32_t aaudio_stream_id_t;
40 
41 class AudioStreamBuilder;
42 
43 constexpr pid_t        CALLBACK_THREAD_NONE = 0;
44 
45 /**
46  * AAudio audio stream.
47  */
48 class AudioStream {
49 public:
50 
51     AudioStream();
52 
53     virtual ~AudioStream();
54 
55 protected:
56 
57     /* Asynchronous requests.
58      * Use waitForStateChange() to wait for completion.
59      */
60     virtual aaudio_result_t requestStart() = 0;
61 
62     /**
63      * Check the state to see if Pause is currently legal.
64      *
65      * @param result pointer to return code
66      * @return true if OK to continue, if false then return result
67      */
68     bool checkPauseStateTransition(aaudio_result_t *result);
69 
isFlushSupported()70     virtual bool isFlushSupported() const {
71         // Only implement FLUSH for OUTPUT streams.
72         return false;
73     }
74 
isPauseSupported()75     virtual bool isPauseSupported() const {
76         // Only implement PAUSE for OUTPUT streams.
77         return false;
78     }
79 
requestPause()80     virtual aaudio_result_t requestPause()
81     {
82         // Only implement this for OUTPUT streams.
83         return AAUDIO_ERROR_UNIMPLEMENTED;
84     }
85 
requestFlush()86     virtual aaudio_result_t requestFlush() {
87         // Only implement this for OUTPUT streams.
88         return AAUDIO_ERROR_UNIMPLEMENTED;
89     }
90 
91     virtual aaudio_result_t requestStop() = 0;
92 
93 public:
94     virtual aaudio_result_t getTimestamp(clockid_t clockId,
95                                        int64_t *framePosition,
96                                        int64_t *timeNanoseconds) = 0;
97 
98     /**
99      * Update state machine.()
100      * @return
101      */
102     virtual aaudio_result_t updateStateMachine() = 0;
103 
104     // =========== End ABSTRACT methods ===========================
105 
106     virtual aaudio_result_t waitForStateChange(aaudio_stream_state_t currentState,
107                                                aaudio_stream_state_t *nextState,
108                                                int64_t timeoutNanoseconds);
109 
110     /**
111      * Open the stream using the parameters in the builder.
112      * Allocate the necessary resources.
113      */
114     virtual aaudio_result_t open(const AudioStreamBuilder& builder);
115 
116     // log to MediaMetrics
117     virtual void logOpen();
118     void logReleaseBufferState();
119 
120     /**
121      * Free any hardware or system resources from the open() call.
122      * It is safe to call release_l() multiple times.
123      */
release_l()124     virtual aaudio_result_t release_l() {
125         setState(AAUDIO_STREAM_STATE_CLOSING);
126         return AAUDIO_OK;
127     }
128 
closeFinal()129     aaudio_result_t closeFinal() {
130         // State is checked by destructor.
131         setState(AAUDIO_STREAM_STATE_CLOSED);
132         return AAUDIO_OK;
133     }
134 
135     /**
136      * Release then close the stream.
137      * @return AAUDIO_OK or negative error.
138      */
releaseCloseFinal()139     aaudio_result_t releaseCloseFinal() {
140         aaudio_result_t result = release_l(); // TODO review locking
141         if (result == AAUDIO_OK) {
142           result = closeFinal();
143         }
144         return result;
145     }
146 
147     // This is only used to identify a stream in the logs without
148     // revealing any pointers.
getId()149     aaudio_stream_id_t getId() {
150         return mStreamId;
151     }
152 
153     virtual aaudio_result_t setBufferSize(int32_t requestedFrames) = 0;
154 
155     virtual aaudio_result_t createThread(int64_t periodNanoseconds,
156                                        aaudio_audio_thread_proc_t threadProc,
157                                        void *threadArg);
158 
159     aaudio_result_t joinThread(void **returnArg, int64_t timeoutNanoseconds);
160 
registerThread()161     virtual aaudio_result_t registerThread() {
162         return AAUDIO_OK;
163     }
164 
unregisterThread()165     virtual aaudio_result_t unregisterThread() {
166         return AAUDIO_OK;
167     }
168 
169     /**
170      * Internal function used to call the audio thread passed by the user.
171      * It is unfortunately public because it needs to be called by a static 'C' function.
172      */
173     void* wrapUserThread();
174 
175     // ============== Queries ===========================
176 
getState()177     aaudio_stream_state_t getState() const {
178         return mState;
179     }
180 
getBufferSize()181     virtual int32_t getBufferSize() const {
182         return AAUDIO_ERROR_UNIMPLEMENTED;
183     }
184 
getBufferCapacity()185     virtual int32_t getBufferCapacity() const {
186         return AAUDIO_ERROR_UNIMPLEMENTED;
187     }
188 
getFramesPerBurst()189     virtual int32_t getFramesPerBurst() const {
190         return AAUDIO_ERROR_UNIMPLEMENTED;
191     }
192 
getXRunCount()193     virtual int32_t getXRunCount() const {
194         return AAUDIO_ERROR_UNIMPLEMENTED;
195     }
196 
isActive()197     bool isActive() const {
198         return mState == AAUDIO_STREAM_STATE_STARTING || mState == AAUDIO_STREAM_STATE_STARTED;
199     }
200 
isMMap()201     virtual bool isMMap() {
202         return false;
203     }
204 
getSampleRate()205     aaudio_result_t getSampleRate() const {
206         return mSampleRate;
207     }
208 
getFormat()209     audio_format_t getFormat()  const {
210         return mFormat;
211     }
212 
getSamplesPerFrame()213     aaudio_result_t getSamplesPerFrame() const {
214         return mSamplesPerFrame;
215     }
216 
getPerformanceMode()217     virtual int32_t getPerformanceMode() const {
218         return mPerformanceMode;
219     }
220 
setPerformanceMode(aaudio_performance_mode_t performanceMode)221     void setPerformanceMode(aaudio_performance_mode_t performanceMode) {
222         mPerformanceMode = performanceMode;
223     }
224 
getDeviceId()225     int32_t getDeviceId() const {
226         return mDeviceId;
227     }
228 
getSharingMode()229     aaudio_sharing_mode_t getSharingMode() const {
230         return mSharingMode;
231     }
232 
isSharingModeMatchRequired()233     bool isSharingModeMatchRequired() const {
234         return mSharingModeMatchRequired;
235     }
236 
237     virtual aaudio_direction_t getDirection() const = 0;
238 
getUsage()239     aaudio_usage_t getUsage() const {
240         return mUsage;
241     }
242 
getContentType()243     aaudio_content_type_t getContentType() const {
244         return mContentType;
245     }
246 
getInputPreset()247     aaudio_input_preset_t getInputPreset() const {
248         return mInputPreset;
249     }
250 
getAllowedCapturePolicy()251     aaudio_allowed_capture_policy_t getAllowedCapturePolicy() const {
252         return mAllowedCapturePolicy;
253     }
254 
getSessionId()255     int32_t getSessionId() const {
256         return mSessionId;
257     }
258 
isPrivacySensitive()259     bool isPrivacySensitive() const {
260         return mIsPrivacySensitive;
261     }
262 
263     /**
264      * This is only valid after setSamplesPerFrame() and setFormat() have been called.
265      */
getBytesPerFrame()266     int32_t getBytesPerFrame() const {
267         return mSamplesPerFrame * getBytesPerSample();
268     }
269 
270     /**
271      * This is only valid after setFormat() has been called.
272      */
getBytesPerSample()273     int32_t getBytesPerSample() const {
274         return audio_bytes_per_sample(mFormat);
275     }
276 
277     /**
278      * This is only valid after setSamplesPerFrame() and setDeviceFormat() have been called.
279      */
getBytesPerDeviceFrame()280     int32_t getBytesPerDeviceFrame() const {
281         return getSamplesPerFrame() * audio_bytes_per_sample(getDeviceFormat());
282     }
283 
284     virtual int64_t getFramesWritten() = 0;
285 
286     virtual int64_t getFramesRead() = 0;
287 
getDataCallbackProc()288     AAudioStream_dataCallback getDataCallbackProc() const {
289         return mDataCallbackProc;
290     }
291 
getErrorCallbackProc()292     AAudioStream_errorCallback getErrorCallbackProc() const {
293         return mErrorCallbackProc;
294     }
295 
296     aaudio_data_callback_result_t maybeCallDataCallback(void *audioData, int32_t numFrames);
297 
298     void maybeCallErrorCallback(aaudio_result_t result);
299 
getDataCallbackUserData()300     void *getDataCallbackUserData() const {
301         return mDataCallbackUserData;
302     }
303 
getErrorCallbackUserData()304     void *getErrorCallbackUserData() const {
305         return mErrorCallbackUserData;
306     }
307 
getFramesPerDataCallback()308     int32_t getFramesPerDataCallback() const {
309         return mFramesPerDataCallback;
310     }
311 
312     /**
313      * @return true if data callback has been specified
314      */
isDataCallbackSet()315     bool isDataCallbackSet() const {
316         return mDataCallbackProc != nullptr;
317     }
318 
319     /**
320      * @return true if data callback has been specified and stream is running
321      */
isDataCallbackActive()322     bool isDataCallbackActive() const {
323         return isDataCallbackSet() && isActive();
324     }
325 
326     /**
327      * @return true if called from the same thread as the callback
328      */
329     bool collidesWithCallback() const;
330 
331     // ============== I/O ===========================
332     // A Stream will only implement read() or write() depending on its direction.
write(const void * buffer __unused,int32_t numFrames __unused,int64_t timeoutNanoseconds __unused)333     virtual aaudio_result_t write(const void *buffer __unused,
334                              int32_t numFrames __unused,
335                              int64_t timeoutNanoseconds __unused) {
336         return AAUDIO_ERROR_UNIMPLEMENTED;
337     }
338 
read(void * buffer __unused,int32_t numFrames __unused,int64_t timeoutNanoseconds __unused)339     virtual aaudio_result_t read(void *buffer __unused,
340                             int32_t numFrames __unused,
341                             int64_t timeoutNanoseconds __unused) {
342         return AAUDIO_ERROR_UNIMPLEMENTED;
343     }
344 
345     // This is used by the AudioManager to duck and mute the stream when changing audio focus.
346     void setDuckAndMuteVolume(float duckAndMuteVolume);
347 
getDuckAndMuteVolume()348     float getDuckAndMuteVolume() const {
349         return mDuckAndMuteVolume;
350     }
351 
352     // Implement this in the output subclasses.
doSetVolume()353     virtual android::status_t doSetVolume() { return android::NO_ERROR; }
354 
355 #if AAUDIO_USE_VOLUME_SHAPER
356     virtual ::android::binder::Status applyVolumeShaper(
357             const ::android::media::VolumeShaper::Configuration& configuration __unused,
358             const ::android::media::VolumeShaper::Operation& operation __unused);
359 #endif
360 
361     /**
362      * Register this stream's PlayerBase with the AudioManager if needed.
363      * Only register output streams.
364      * This should only be called for client streams and not for streams
365      * that run in the service.
366      */
registerPlayerBase()367     void registerPlayerBase() {
368         if (getDirection() == AAUDIO_DIRECTION_OUTPUT) {
369             mPlayerBase->registerWithAudioManager();
370         }
371     }
372 
373     /**
374      * Unregister this stream's PlayerBase with the AudioManager.
375      * This will only unregister if already registered.
376      */
unregisterPlayerBase()377     void unregisterPlayerBase() {
378         mPlayerBase->unregisterWithAudioManager();
379     }
380 
381     aaudio_result_t systemStart();
382 
383     aaudio_result_t systemPause();
384 
385     aaudio_result_t safeFlush();
386 
387     /**
388      * This is called when an app calls AAudioStream_requestStop();
389      * It prevents calls from a callback.
390      */
391     aaudio_result_t systemStopFromApp();
392 
393     /**
394      * This is called internally when an app callback returns AAUDIO_CALLBACK_RESULT_STOP.
395      */
396     aaudio_result_t systemStopFromCallback();
397 
398     aaudio_result_t safeRelease();
399 
400 protected:
401 
402     // PlayerBase allows the system to control the stream volume.
403     class MyPlayerBase : public android::PlayerBase {
404     public:
405         explicit MyPlayerBase(AudioStream *parent);
406 
407         virtual ~MyPlayerBase();
408 
409         /**
410          * Register for volume changes and remote control.
411          */
412         void registerWithAudioManager();
413 
414         /**
415          * UnRegister.
416          */
417         void unregisterWithAudioManager();
418 
419         /**
420          * Just calls unregisterWithAudioManager().
421          */
422         void destroy() override;
423 
clearParentReference()424         void clearParentReference() { mParent = nullptr; }
425 
426         // Just a stub. The ability to start audio through PlayerBase is being deprecated.
playerStart()427         android::status_t playerStart() override {
428             return android::NO_ERROR;
429         }
430 
431         // Just a stub. The ability to pause audio through PlayerBase is being deprecated.
playerPause()432         android::status_t playerPause() override {
433             return android::NO_ERROR;
434         }
435 
436         // Just a stub. The ability to stop audio through PlayerBase is being deprecated.
playerStop()437         android::status_t playerStop() override {
438             return android::NO_ERROR;
439         }
440 
playerSetVolume()441         android::status_t playerSetVolume() override {
442             // No pan and only left volume is taken into account from IPLayer interface
443             mParent->setDuckAndMuteVolume(mVolumeMultiplierL  /* * mPanMultiplierL */);
444             return android::NO_ERROR;
445         }
446 
447 #if AAUDIO_USE_VOLUME_SHAPER
applyVolumeShaper(const::android::media::VolumeShaper::Configuration & configuration,const::android::media::VolumeShaper::Operation & operation)448         ::android::binder::Status applyVolumeShaper(
449                 const ::android::media::VolumeShaper::Configuration& configuration,
450                 const ::android::media::VolumeShaper::Operation& operation) {
451             return mParent->applyVolumeShaper(configuration, operation);
452         }
453 #endif
454 
getResult()455         aaudio_result_t getResult() {
456             return mResult;
457         }
458 
459     private:
460         AudioStream          *mParent;
461         aaudio_result_t       mResult = AAUDIO_OK;
462         bool                  mRegistered = false;
463     };
464 
465     /**
466      * This should not be called after the open() call.
467      * TODO for multiple setters: assert(mState == AAUDIO_STREAM_STATE_UNINITIALIZED)
468      */
setSampleRate(int32_t sampleRate)469     void setSampleRate(int32_t sampleRate) {
470         mSampleRate = sampleRate;
471     }
472 
473     /**
474      * This should not be called after the open() call.
475      */
setSamplesPerFrame(int32_t samplesPerFrame)476     void setSamplesPerFrame(int32_t samplesPerFrame) {
477         mSamplesPerFrame = samplesPerFrame;
478     }
479 
480     /**
481      * This should not be called after the open() call.
482      */
setSharingMode(aaudio_sharing_mode_t sharingMode)483     void setSharingMode(aaudio_sharing_mode_t sharingMode) {
484         mSharingMode = sharingMode;
485     }
486 
487     /**
488      * This should not be called after the open() call.
489      */
setFormat(audio_format_t format)490     void setFormat(audio_format_t format) {
491         mFormat = format;
492     }
493 
494     /**
495      * This should not be called after the open() call.
496      */
setDeviceFormat(audio_format_t format)497     void setDeviceFormat(audio_format_t format) {
498         mDeviceFormat = format;
499     }
500 
getDeviceFormat()501     audio_format_t getDeviceFormat() const {
502         return mDeviceFormat;
503     }
504 
505     void setState(aaudio_stream_state_t state);
506 
setDeviceId(int32_t deviceId)507     void setDeviceId(int32_t deviceId) {
508         mDeviceId = deviceId;
509     }
510 
setSessionId(int32_t sessionId)511     void setSessionId(int32_t sessionId) {
512         mSessionId = sessionId;
513     }
514 
515     std::atomic<bool>    mCallbackEnabled{false};
516 
517     float                mDuckAndMuteVolume = 1.0f;
518 
519 protected:
520 
521     /**
522      * Either convert the data from device format to app format and return a pointer
523      * to the conversion buffer,
524      * OR just pass back the original pointer.
525      *
526      * Note that this is only used for the INPUT path.
527      *
528      * @param audioData
529      * @param numFrames
530      * @return original pointer or the conversion buffer
531      */
maybeConvertDeviceData(const void * audioData,int32_t numFrames)532     virtual const void * maybeConvertDeviceData(const void *audioData, int32_t numFrames) {
533         return audioData;
534     }
535 
setPeriodNanoseconds(int64_t periodNanoseconds)536     void setPeriodNanoseconds(int64_t periodNanoseconds) {
537         mPeriodNanoseconds.store(periodNanoseconds, std::memory_order_release);
538     }
539 
getPeriodNanoseconds()540     int64_t getPeriodNanoseconds() {
541         return mPeriodNanoseconds.load(std::memory_order_acquire);
542     }
543 
544     /**
545      * This should not be called after the open() call.
546      */
setUsage(aaudio_usage_t usage)547     void setUsage(aaudio_usage_t usage) {
548         mUsage = usage;
549     }
550 
551     /**
552      * This should not be called after the open() call.
553      */
setContentType(aaudio_content_type_t contentType)554     void setContentType(aaudio_content_type_t contentType) {
555         mContentType = contentType;
556     }
557 
558     /**
559      * This should not be called after the open() call.
560      */
setInputPreset(aaudio_input_preset_t inputPreset)561     void setInputPreset(aaudio_input_preset_t inputPreset) {
562         mInputPreset = inputPreset;
563     }
564 
565     /**
566      * This should not be called after the open() call.
567      */
setAllowedCapturePolicy(aaudio_allowed_capture_policy_t policy)568     void setAllowedCapturePolicy(aaudio_allowed_capture_policy_t policy) {
569         mAllowedCapturePolicy = policy;
570     }
571 
572     /**
573      * This should not be called after the open() call.
574      */
setPrivacySensitive(bool privacySensitive)575     void setPrivacySensitive(bool privacySensitive) {
576         mIsPrivacySensitive = privacySensitive;
577     }
578 
579     std::string mMetricsId; // set once during open()
580 
581 private:
582 
583     aaudio_result_t safeStop();
584 
585     std::mutex                 mStreamLock;
586 
587     const android::sp<MyPlayerBase>   mPlayerBase;
588 
589     // These do not change after open().
590     int32_t                     mSamplesPerFrame = AAUDIO_UNSPECIFIED;
591     int32_t                     mSampleRate = AAUDIO_UNSPECIFIED;
592     int32_t                     mDeviceId = AAUDIO_UNSPECIFIED;
593     aaudio_sharing_mode_t       mSharingMode = AAUDIO_SHARING_MODE_SHARED;
594     bool                        mSharingModeMatchRequired = false; // must match sharing mode requested
595     audio_format_t              mFormat = AUDIO_FORMAT_DEFAULT;
596     aaudio_stream_state_t       mState = AAUDIO_STREAM_STATE_UNINITIALIZED;
597     aaudio_performance_mode_t   mPerformanceMode = AAUDIO_PERFORMANCE_MODE_NONE;
598 
599     aaudio_usage_t              mUsage           = AAUDIO_UNSPECIFIED;
600     aaudio_content_type_t       mContentType     = AAUDIO_UNSPECIFIED;
601     aaudio_input_preset_t       mInputPreset     = AAUDIO_UNSPECIFIED;
602     aaudio_allowed_capture_policy_t mAllowedCapturePolicy = AAUDIO_ALLOW_CAPTURE_BY_ALL;
603     bool                        mIsPrivacySensitive = false;
604 
605     int32_t                     mSessionId = AAUDIO_UNSPECIFIED;
606 
607     // Sometimes the hardware is operating with a different format from the app.
608     // Then we require conversion in AAudio.
609     audio_format_t              mDeviceFormat = AUDIO_FORMAT_INVALID;
610 
611     // callback ----------------------------------
612 
613     AAudioStream_dataCallback   mDataCallbackProc = nullptr;  // external callback functions
614     void                       *mDataCallbackUserData = nullptr;
615     int32_t                     mFramesPerDataCallback = AAUDIO_UNSPECIFIED; // frames
616     std::atomic<pid_t>          mDataCallbackThread{CALLBACK_THREAD_NONE};
617 
618     AAudioStream_errorCallback  mErrorCallbackProc = nullptr;
619     void                       *mErrorCallbackUserData = nullptr;
620     std::atomic<pid_t>          mErrorCallbackThread{CALLBACK_THREAD_NONE};
621 
622     // background thread ----------------------------------
623     bool                        mHasThread = false;
624     pthread_t                   mThread; // initialized in constructor
625 
626     // These are set by the application thread and then read by the audio pthread.
627     std::atomic<int64_t>        mPeriodNanoseconds; // for tuning SCHED_FIFO threads
628     // TODO make atomic?
629     aaudio_audio_thread_proc_t  mThreadProc = nullptr;
630     void                       *mThreadArg = nullptr;
631     aaudio_result_t             mThreadRegistrationResult = AAUDIO_OK;
632 
633     const aaudio_stream_id_t    mStreamId;
634 
635 };
636 
637 } /* namespace aaudio */
638 
639 #endif /* AAUDIO_AUDIOSTREAM_H */
640