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