• 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 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 release_l() override;
45     void close_l() override;
46 
47 protected:
48     aaudio_result_t requestStart_l() REQUIRES(mStreamLock)  override;
49     aaudio_result_t requestPause_l() REQUIRES(mStreamLock) override;
50     aaudio_result_t requestFlush_l() REQUIRES(mStreamLock) override;
51     aaudio_result_t requestStop_l() REQUIRES(mStreamLock) override;
52 
53 public:
isFlushSupported()54     bool isFlushSupported() const override {
55         // Only implement FLUSH for OUTPUT streams.
56         return true;
57     }
58 
isPauseSupported()59     bool isPauseSupported() const override {
60         // Only implement PAUSE for OUTPUT streams.
61         return true;
62     }
63 
64     aaudio_result_t getTimestamp(clockid_t clockId,
65                                        int64_t *framePosition,
66                                        int64_t *timeNanoseconds) override;
67 
68     aaudio_result_t write(const void *buffer,
69                              int32_t numFrames,
70                              int64_t timeoutNanoseconds) override;
71 
72     aaudio_result_t setBufferSize(int32_t requestedFrames) override;
73     int32_t getBufferSize() const override;
74     int32_t getXRunCount() const override;
75 
76     int64_t getFramesRead() override;
77 
getDirection()78     aaudio_direction_t getDirection() const override {
79         return AAUDIO_DIRECTION_OUTPUT;
80     }
81 
82     aaudio_result_t updateStateMachine() override;
83 
incrementClientFrameCounter(int32_t frames)84     int64_t incrementClientFrameCounter(int32_t frames) override {
85         return incrementFramesWritten(frames);
86     }
87 
88     android::status_t doSetVolume() override;
89 
90 #if AAUDIO_USE_VOLUME_SHAPER
91     virtual android::binder::Status applyVolumeShaper(
92             const android::media::VolumeShaper::Configuration& configuration,
93             const android::media::VolumeShaper::Operation& operation) override;
94 #endif
95 
96 protected:
97 
98     int32_t getFramesPerBurstFromDevice() const override;
99     int32_t getBufferCapacityFromDevice() const override;
100     void onNewIAudioTrack() override;
101 
102 private:
103 
104     android::sp<android::AudioTrack> mAudioTrack;
105 
106     // adapts between variable sized blocks and fixed size blocks
107     FixedBlockReader                 mFixedBlockReader;
108 
109     // TODO add 64-bit position reporting to AudioTrack and use it.
110     aaudio_wrapping_frames_t         mPositionWhenPausing = 0;
111 };
112 
113 } /* namespace aaudio */
114 
115 #endif /* LEGACY_AUDIO_STREAM_TRACK_H */
116