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 #include <audio_utils/spdif/SPDIFEncoder.h>
28 
29 #include "AudioOutput.h"
30 
31 namespace android {
32 
33 class AudioHardwareOutput;
34 
35 class AudioStreamOut {
36   public:
37     AudioStreamOut(AudioHardwareOutput& owner, bool mcOut);
38     ~AudioStreamOut();
39 
40     uint32_t            latency() const;
41     status_t            getRenderPosition(uint32_t *dspFrames);
42     status_t            getPresentationPosition(uint64_t *frames, struct timespec *timestamp);
43     status_t            getNextWriteTimestamp(int64_t *timestamp);
44     status_t            standby();
45     status_t            dump(int fd);
46 
sampleRate()47     uint32_t            sampleRate()        const { return mInputSampleRate; }
48     uint32_t            outputSampleRate()  const;
49     uint32_t            getRateMultiplier() const;
50 
bufferSize()51     size_t              bufferSize()        const { return mInputBufSize; }
chanMask()52     uint32_t            chanMask()          const { return mInputChanMask; }
format()53     audio_format_t      format()            const { return mInputFormat; }
framesPerChunk()54     uint32_t            framesPerChunk()    const { return mInputChunkFrames; }
nomChunksInFlight()55     uint32_t            nomChunksInFlight() const { return mInputNominalChunksInFlight; }
56 
57     status_t            set(audio_format_t *pFormat,
58                             uint32_t       *pChannels,
59                             uint32_t       *pRate);
60     void                setTgtDevices(uint32_t tgtDevices);
61 
62     status_t            setParameters(struct audio_stream *stream,
63                                       const char *kvpairs);
64     char*               getParameters(const char* keys);
getName()65     const char*         getName() const { return mIsMCOutput ? "Multi-channel"
66                                                              : "Main"; }
67 
68     ssize_t             write(const void* buffer, size_t bytes);
69 
isEncoded()70     bool                isEncoded() const { return mIsEncoded; }
71 
72     class MySPDIFEncoder : public SPDIFEncoder
73     {
74     public:
MySPDIFEncoder(AudioStreamOut * streamOut)75         MySPDIFEncoder(AudioStreamOut *streamOut)
76           : mStreamOut(streamOut)
77         {};
78 
writeOutput(const void * buffer,size_t bytes)79         virtual ssize_t writeOutput(const void* buffer, size_t bytes)
80         {
81             return mStreamOut->writeInternal(buffer, bytes);
82         }
83     protected:
84         AudioStreamOut * const mStreamOut;
85     };
86 
87 protected:
88     Mutex           mLock;
89     Mutex           mRoutingLock;
90 
91     // Used to implment get_presentation_position()
92     int64_t         mFramesPresented; // application rate frames, not device rate frames
93     // Used to implement get_render_position()
94     int64_t         mFramesRendered; // application rate frames, not device rate frames
95     uint32_t        mFramesWrittenRemainder; // needed when device rate > app rate
96 
97     // Our HAL, used as the middle-man to collect and trade AudioOutputs.
98     AudioHardwareOutput&  mOwnerHAL;
99 
100     // Details about the format of the audio we have been configured to receive
101     // from audio flinger.
102     uint32_t        mInputSampleRate;
103     uint32_t        mInputChanMask;
104     audio_format_t  mInputFormat;
105     uint32_t        mInputNominalChunksInFlight;
106 
107     // Handy values pre-computed from the audio configuration.
108     uint32_t        mInputBufSize;
109     uint32_t        mInputChanCount;
110     uint32_t        mInputChunkFrames;
111     uint32_t        mInputNominalLatencyUSec;
112     LinearTransform mLocalTimeToFrames;
113 
114     // Bookkeeping used to throttle audio flinger when this audio stream has no
115     // actual physical outputs.
116     LocalClock      mLocalClock;
117     bool            mThrottleValid;
118     int64_t         mWriteStartLT;
119     int64_t         mFramesWritten; // application rate frames, not device rate frames
120     LinearTransform mUSecToLocalTime;
121 
122     // State to track which actual outputs are assigned to this output stream.
123     AudioOutputList mPhysOutputs;
124     uint32_t        mTgtDevices;
125     bool            mTgtDevicesDirty;
126     uint32_t        mAudioFlingerTgtDevices;
127 
128     // Flag to track if this StreamOut was created to sink a direct output
129     // multichannel stream.
130     bool            mIsMCOutput;
131     // Is the audio data encoded, eg. AC3?
132     bool            mIsEncoded;
133     // Is the stream on standby?
134     bool            mInStandby;
135 
136     MySPDIFEncoder  mSPDIFEncoder;
137 
138     void            releaseAllOutputs();
139     void            updateTargetOutputs();
140     void            updateInputNums();
141     void            finishedWriteOp(size_t framesWritten, bool needThrottle);
resetThrottle()142     void            resetThrottle() { mThrottleValid = false; }
143     status_t        getNextWriteTimestamp_internal(int64_t *timestamp);
144     void            adjustOutputs(int64_t maxTime);
145     ssize_t         writeInternal(const void* buffer, size_t bytes);
146     int             getBytesPerOutputFrame();
147 };
148 
149 }  // android
150 #endif  // ANDROID_AUDIO_STREAM_OUT_H
151