• 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_LEGACY_H
18 #define LEGACY_AUDIO_STREAM_LEGACY_H
19 
20 #include <media/AudioRecord.h>
21 #include <media/AudioSystem.h>
22 #include <media/AudioTimestamp.h>
23 #include <media/AudioTrack.h>
24 
25 #include <aaudio/AAudio.h>
26 
27 #include "AudioStream.h"
28 #include "AAudioLegacy.h"
29 #include "utility/AAudioUtilities.h"
30 #include "utility/FixedBlockAdapter.h"
31 
32 namespace aaudio {
33 
34 
35 enum {
36     /**
37      * Request that the callback function should fill the data buffer of an output stream,
38      * or process the data of an input stream.
39      * The address parameter passed to the callback function will point to a data buffer.
40      * For an input stream, the data is read-only.
41      * The value1 parameter will be the number of frames.
42      * The value2 parameter is reserved and will be set to zero.
43      * The callback should return AAUDIO_CALLBACK_RESULT_CONTINUE or AAUDIO_CALLBACK_RESULT_STOP.
44      */
45             AAUDIO_CALLBACK_OPERATION_PROCESS_DATA,
46 
47     /**
48      * Inform the callback function that the stream was disconnected.
49      * The address parameter passed to the callback function will be NULL.
50      * The value1 will be an error code or AAUDIO_OK.
51      * The value2 parameter is reserved and will be set to zero.
52      * The callback return value will be ignored.
53      */
54             AAUDIO_CALLBACK_OPERATION_DISCONNECTED,
55 };
56 typedef int32_t aaudio_callback_operation_t;
57 
58 
59 class AudioStreamLegacy : public AudioStream,
60                           public FixedBlockProcessor,
61                           protected android::AudioTrack::IAudioTrackCallback,
62                           protected android::AudioRecord::IAudioRecordCallback {
63 public:
64     AudioStreamLegacy();
65 
66     virtual ~AudioStreamLegacy() = default;
67 
68 
69     int32_t callDataCallbackFrames(uint8_t *buffer, int32_t numFrames);
70 
71 
72     // Implement FixedBlockProcessor
73     int32_t onProcessFixedBlock(uint8_t *buffer, int32_t numBytes) override;
74 
75     virtual int64_t incrementClientFrameCounter(int32_t frames)  = 0;
76 
getFramesWritten()77     virtual int64_t getFramesWritten() override {
78         return mFramesWritten.get();
79     }
80 
getFramesRead()81     virtual int64_t getFramesRead() override {
82         return mFramesRead.get();
83     }
84 
85 protected:
86     size_t onMoreData(const android::AudioTrack::Buffer& buffer) override;
87     // TODO (b/216175830) this method is duplicated in order to ease refactoring which will
88     // reconsolidate.
89     size_t onMoreData(const android::AudioRecord::Buffer& buffer) override;
90     void onNewIAudioTrack() override;
onNewIAudioRecord()91     void onNewIAudioRecord() override { onNewIAudioTrack(); }
92     aaudio_result_t getBestTimestamp(clockid_t clockId,
93                                      int64_t *framePosition,
94                                      int64_t *timeNanoseconds,
95                                      android::ExtendedTimestamp *extendedTimestamp);
96 
97     void onAudioDeviceUpdate(audio_io_handle_t audioIo,
98             audio_port_handle_t deviceId) override;
99 
100     /*
101      * Check to see whether a callback thread has requested a disconnected.
102      * @param errorCallbackEnabled set true to call errorCallback on disconnect
103      * @return AAUDIO_OK or AAUDIO_ERROR_DISCONNECTED
104      */
105     aaudio_result_t checkForDisconnectRequest(bool errorCallbackEnabled);
106 
107     void forceDisconnect(bool errorCallbackEnabled = true);
108 
incrementFramesWritten(int32_t frames)109     int64_t incrementFramesWritten(int32_t frames) {
110         return mFramesWritten.increment(frames);
111     }
112 
incrementFramesRead(int32_t frames)113     int64_t incrementFramesRead(int32_t frames) {
114         return mFramesRead.increment(frames);
115     }
116 
117     /**
118      * Get the framesPerBurst from the underlying API.
119      * @return framesPerBurst
120      */
121     virtual int32_t getFramesPerBurstFromDevice() const = 0;
122 
123     /**
124      * Get the bufferCapacity from the underlying API.
125      * @return bufferCapacity in frames
126      */
127     virtual int32_t getBufferCapacityFromDevice() const = 0;
128 
129     // This is used for exact matching by MediaMetrics. So do not change it.
130     // MediaMetricsConstants.h: AMEDIAMETRICS_PROP_CALLERNAME_VALUE_AAUDIO
131     static constexpr char     kCallerName[] = "aaudio";
132 
133     MonotonicCounter           mFramesWritten;
134     MonotonicCounter           mFramesRead;
135     MonotonicCounter           mTimestampPosition;
136 
137     FixedBlockAdapter         *mBlockAdapter = nullptr;
138     int32_t                    mBlockAdapterBytesPerFrame = 0;
139     aaudio_wrapping_frames_t   mPositionWhenStarting = 0;
140     int32_t                    mCallbackBufferSize = 0;
141 
142     AtomicRequestor            mRequestDisconnect;
143 
144 };
145 
146 } /* namespace aaudio */
147 
148 #endif //LEGACY_AUDIO_STREAM_LEGACY_H
149