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 LEGACY_AUDIO_STREAM_TRACK_H
18 #define LEGACY_AUDIO_STREAM_TRACK_H
19 
20 #include <math.h>
21 #include <media/TrackPlayerBase.h>
22 #include <media/AudioTrack.h>
23 #include <aaudio/AAudio.h>
24 
25 #include "AudioStreamBuilder.h"
26 #include "AudioStream.h"
27 #include "legacy/AAudioLegacy.h"
28 #include "legacy/AudioStreamLegacy.h"
29 #include "utility/FixedBlockReader.h"
30 
31 namespace aaudio {
32 
33 /**
34  * Internal stream that uses the legacy AudioTrack path.
35  */
36 class AudioStreamTrack : public AudioStreamLegacy {
37 public:
38     AudioStreamTrack();
39 
40     virtual ~AudioStreamTrack();
41 
42 
43     aaudio_result_t open(const AudioStreamBuilder & builder) override;
44     aaudio_result_t close() override;
45 
46     aaudio_result_t requestStart() override;
47     aaudio_result_t requestPause() override;
48     aaudio_result_t requestFlush() override;
49     aaudio_result_t requestStop() override;
50 
isFlushSupported()51     bool isFlushSupported() const override {
52         // Only implement FLUSH for OUTPUT streams.
53         return true;
54     }
55 
isPauseSupported()56     bool isPauseSupported() const override {
57         // Only implement PAUSE for OUTPUT streams.
58         return true;
59     }
60 
61     aaudio_result_t getTimestamp(clockid_t clockId,
62                                        int64_t *framePosition,
63                                        int64_t *timeNanoseconds) override;
64 
65     aaudio_result_t write(const void *buffer,
66                              int32_t numFrames,
67                              int64_t timeoutNanoseconds) override;
68 
69     aaudio_result_t setBufferSize(int32_t requestedFrames) override;
70     int32_t getBufferSize() const override;
71     int32_t getBufferCapacity() const override;
72     int32_t getFramesPerBurst()const  override;
73     int32_t getXRunCount() const override;
74 
75     int64_t getFramesRead() override;
76 
getDirection()77     aaudio_direction_t getDirection() const override {
78         return AAUDIO_DIRECTION_OUTPUT;
79     }
80 
81     aaudio_result_t updateStateMachine() override;
82 
83     // This is public so it can be called from the C callback function.
84     void processCallback(int event, void *info) override;
85 
incrementClientFrameCounter(int32_t frames)86     int64_t incrementClientFrameCounter(int32_t frames) override {
87         return incrementFramesWritten(frames);
88     }
89 
90     android::status_t doSetVolume() override;
91 
92 #if AAUDIO_USE_VOLUME_SHAPER
93     virtual android::binder::Status applyVolumeShaper(
94             const android::media::VolumeShaper::Configuration& configuration,
95             const android::media::VolumeShaper::Operation& operation) override;
96 #endif
97 
98 private:
99 
100     android::sp<android::AudioTrack> mAudioTrack;
101 
102     // adapts between variable sized blocks and fixed size blocks
103     FixedBlockReader                 mFixedBlockReader;
104 
105     // TODO add 64-bit position reporting to AudioTrack and use it.
106     aaudio_wrapping_frames_t         mPositionWhenPausing = 0;
107 };
108 
109 } /* namespace aaudio */
110 
111 #endif /* LEGACY_AUDIO_STREAM_TRACK_H */
112