1 /*
2 **
3 ** Copyright 2012, The Android Open Source Project
4 **
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 **     http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
17 
18 #ifndef ANDROID_AUDIO_STREAM_OUT_H
19 #define ANDROID_AUDIO_STREAM_OUT_H
20 
21 #include <stdint.h>
22 #include <sys/types.h>
23 
24 #include <common_time/local_clock.h>
25 #include <hardware/audio.h>
26 #include <media/AudioParameter.h>
27 
28 #include "AudioOutput.h"
29 
30 namespace android {
31 
32 class AudioHardwareOutput;
33 
34 class AudioStreamOut {
35   public:
36     AudioStreamOut(AudioHardwareOutput& owner, bool mcOut, bool isIec958NonAudio);
37     ~AudioStreamOut();
38 
39     uint32_t            latency() const;
40     status_t            getRenderPosition(uint32_t *dspFrames);
41     status_t            getPresentationPosition(uint64_t *frames, struct timespec *timestamp);
42     status_t            getNextWriteTimestamp(int64_t *timestamp);
43     status_t            standby();
44     status_t            pause();
45     status_t            resume();
46     status_t            flush();
47     status_t            dump(int fd);
48 
sampleRate()49     uint32_t            sampleRate()        const { return mInputSampleRate; }
50     uint32_t            outputSampleRate()  const;
51 
bufferSize()52     size_t              bufferSize()        const { return mInputBufSize; }
chanMask()53     uint32_t            chanMask()          const { return mInputChanMask; }
format()54     audio_format_t      format()            const { return mInputFormat; }
framesPerChunk()55     uint32_t            framesPerChunk()    const { return mInputChunkFrames; }
nomChunksInFlight()56     uint32_t            nomChunksInFlight() const { return mInputNominalChunksInFlight; }
57 
58     status_t            set(audio_format_t *pFormat,
59                             uint32_t       *pChannels,
60                             uint32_t       *pRate);
61     void                setTgtDevices(uint32_t tgtDevices);
62 
63     status_t            setParameters(struct audio_stream *stream,
64                                       const char *kvpairs);
65     char*               getParameters(const char* keys);
getName()66     const char*         getName() const { return mIsMCOutput ? "Multi-channel"
67                                                              : "Main"; }
68 
69     ssize_t             write(const void* buffer, size_t bytes);
70 
isIec958NonAudio()71     bool                isIec958NonAudio() const { return mIsIec958NonAudio; }
72 
73 protected:
74     // Lock in this order to avoid deadlock.
75     //    mRoutingLock
76     //    mPresentationLock
77 
78     // Track frame position for timestamps, etc.
79     uint64_t        mRenderPosition;  // in frames, increased by write
80     uint64_t        mFramesPresented; // increased by write
81 
82     // Cache of the last PresentationPosition.
83     // This cache is used in case of retrograde timestamps or if the mRoutingLock is held.
84     Mutex           mPresentationLock; // protects these mLastPresentation* variables
85     uint64_t        mLastPresentationPosition; // frames
86     struct timespec mLastPresentationTime;
87     bool            mLastPresentationValid;
88 
89     // Our HAL, used as the middle-man to collect and trade AudioOutputs.
90     AudioHardwareOutput&  mOwnerHAL;
91 
92     // Details about the format of the audio we have been configured to receive
93     // from audio flinger.
94     uint32_t        mInputSampleRate;
95     uint32_t        mInputChanMask;
96     audio_format_t  mInputFormat;
97     uint32_t        mInputNominalChunksInFlight;
98 
99     // Handy values pre-computed from the audio configuration.
100     uint32_t        mInputBufSize;
101     uint32_t        mInputChanCount;
102     uint32_t        mInputFrameSize;
103     uint32_t        mInputChunkFrames;
104     uint32_t        mInputNominalLatencyUSec;
105     LinearTransform mLocalTimeToFrames;
106 
107     // Bookkeeping used to throttle audio flinger when this audio stream has no
108     // actual physical outputs.
109     LocalClock      mLocalClock;
110     bool            mThrottleValid;
111     int64_t         mWriteStartLT;
112     int64_t         mFramesWritten; // application rate frames, not device rate frames
113     LinearTransform mUSecToLocalTime;
114 
115     // State to track which actual outputs are assigned to this output stream.
116     Mutex           mRoutingLock; // This protects mPhysOutputs and mTgtDevices
117     AudioOutputList mPhysOutputs;
118     uint32_t        mTgtDevices;
119     bool            mTgtDevicesDirty;
120     uint32_t        mAudioFlingerTgtDevices;
121 
122     // Flag to track if this StreamOut was created to sink a direct output
123     // multichannel stream.
124     bool            mIsMCOutput;
125     // Is the stream on standby?
126     bool            mInStandby;
127     // Is the stream compressed audio in SPDIF data bursts?
128     const bool      mIsIec958NonAudio;
129 
130     // reduce log spew
131     bool            mReportedAvailFail;
132 
133     status_t        standbyHardware();
134     void            releaseAllOutputs(); // locks mRoutingLock
135     void            updateTargetOutputs();  // locks mRoutingLock
136     void            updateInputNums();
137     void            finishedWriteOp(size_t framesWritten, bool needThrottle);
resetThrottle()138     void            resetThrottle() { mThrottleValid = false; }
139     status_t        getNextWriteTimestamp_internal(int64_t *timestamp);
140     void            adjustOutputs(int64_t maxTime);
141     ssize_t         writeInternal(const void* buffer, size_t bytes);
142 
143     // mRoutingLock should be held before calling this.
144     status_t        getPresentationPosition_l(uint64_t *frames, struct timespec *timestamp);
145 };
146 
147 }  // android
148 #endif  // ANDROID_AUDIO_STREAM_OUT_H
149