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 <aaudio/AAudio.h>
23
24#include "AudioStreamBuilder.h"
25#include "AudioStream.h"
26#include "legacy/AAudioLegacy.h"
27#include "legacy/AudioStreamLegacy.h"
28#include "utility/FixedBlockReader.h"
29
30namespace aaudio {
31
32/**
33 * Internal stream that uses the legacy AudioTrack path.
34 */
35class AudioStreamTrack : public AudioStreamLegacy, public android::TrackPlayerBase {
36public:
37    AudioStreamTrack();
38
39    virtual ~AudioStreamTrack();
40
41
42    aaudio_result_t open(const AudioStreamBuilder & builder) override;
43    aaudio_result_t close() override;
44
45    aaudio_result_t requestStart() override;
46    aaudio_result_t requestPause() override;
47    aaudio_result_t requestFlush() override;
48    aaudio_result_t requestStop() override;
49
50    aaudio_result_t getTimestamp(clockid_t clockId,
51                                       int64_t *framePosition,
52                                       int64_t *timeNanoseconds) override;
53
54    aaudio_result_t write(const void *buffer,
55                             int32_t numFrames,
56                             int64_t timeoutNanoseconds) override;
57
58    aaudio_result_t setBufferSize(int32_t requestedFrames) override;
59    int32_t getBufferSize() const override;
60    int32_t getBufferCapacity() const override;
61    int32_t getFramesPerBurst()const  override;
62    int32_t getXRunCount() const override;
63
64    int64_t getFramesRead() override;
65
66    aaudio_direction_t getDirection() const override {
67        return AAUDIO_DIRECTION_OUTPUT;
68    }
69
70    aaudio_result_t updateStateWhileWaiting() override;
71
72    // This is public so it can be called from the C callback function.
73    void processCallback(int event, void *info) override;
74
75    int64_t incrementClientFrameCounter(int32_t frames) override {
76        return incrementFramesWritten(frames);
77    }
78
79private:
80
81    // adapts between variable sized blocks and fixed size blocks
82    FixedBlockReader                 mFixedBlockReader;
83
84    // TODO add 64-bit position reporting to AudioRecord and use it.
85    aaudio_wrapping_frames_t         mPositionWhenStarting = 0;
86    aaudio_wrapping_frames_t         mPositionWhenPausing = 0;
87};
88
89} /* namespace aaudio */
90
91#endif /* LEGACY_AUDIO_STREAM_TRACK_H */
92