blob: 9c24b2be7f18da0571b8cf71e9e0a120b492a59b [file] [log] [blame]
Phil Burke4d7bb42017-03-28 11:32:39 -07001/*
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
Phil Burk5204d312017-05-04 17:16:13 -070020#include <media/AudioTimestamp.h>
Eric Laurentfb00fc72017-05-25 18:17:12 -070021#include <media/AudioSystem.h>
Phil Burke4d7bb42017-03-28 11:32:39 -070022
23#include <aaudio/AAudio.h>
24
25#include "AudioStream.h"
26#include "AAudioLegacy.h"
Phil Burk2d5ba532017-09-06 14:36:11 -070027#include "utility/AAudioUtilities.h"
Phil Burke4d7bb42017-03-28 11:32:39 -070028#include "utility/FixedBlockAdapter.h"
29
30namespace aaudio {
31
32
33typedef void (*aaudio_legacy_callback_t)(int event, void* user, void *info);
34
35enum {
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};
56typedef int32_t aaudio_callback_operation_t;
57
58
59class AudioStreamLegacy : public AudioStream, public FixedBlockProcessor {
60public:
61 AudioStreamLegacy();
62
63 virtual ~AudioStreamLegacy();
64
65 aaudio_legacy_callback_t getLegacyCallback();
66
Phil Burk7328a802017-08-30 09:29:48 -070067 int32_t callDataCallbackFrames(uint8_t *buffer, int32_t numFrames);
68
Phil Burke4d7bb42017-03-28 11:32:39 -070069 // This is public so it can be called from the C callback function.
70 // This is called from the AudioTrack/AudioRecord client.
71 virtual void processCallback(int event, void *info) = 0;
72
73 void processCallbackCommon(aaudio_callback_operation_t opcode, void *info);
74
75 // Implement FixedBlockProcessor
76 int32_t onProcessFixedBlock(uint8_t *buffer, int32_t numBytes) override;
77
Phil Burk4c5129b2017-04-28 15:17:32 -070078 virtual int64_t incrementClientFrameCounter(int32_t frames) = 0;
79
Phil Burkec89b2e2017-06-20 15:05:06 -070080 virtual int64_t getFramesWritten() override {
81 return mFramesWritten.get();
82 }
83
84 virtual int64_t getFramesRead() override {
85 return mFramesRead.get();
86 }
87
Phil Burke4d7bb42017-03-28 11:32:39 -070088protected:
Phil Burk5204d312017-05-04 17:16:13 -070089
Eric Laurentfb00fc72017-05-25 18:17:12 -070090 class StreamDeviceCallback : public android::AudioSystem::AudioDeviceCallback
91 {
92 public:
93
94 StreamDeviceCallback(AudioStreamLegacy *parent) : mParent(parent) {}
95 virtual ~StreamDeviceCallback() {}
96
97 virtual void onAudioDeviceUpdate(audio_io_handle_t audioIo __unused,
98 audio_port_handle_t deviceId) {
99 if (mParent != nullptr) {
100 mParent->onAudioDeviceUpdate(deviceId);
101 }
102 }
103
104 AudioStreamLegacy *mParent;
105 };
106
Phil Burk5204d312017-05-04 17:16:13 -0700107 aaudio_result_t getBestTimestamp(clockid_t clockId,
108 int64_t *framePosition,
109 int64_t *timeNanoseconds,
110 android::ExtendedTimestamp *extendedTimestamp);
111
Eric Laurentfb00fc72017-05-25 18:17:12 -0700112 void onAudioDeviceUpdate(audio_port_handle_t deviceId);
113
Phil Burk134f1972017-12-08 13:06:11 -0800114 /*
115 * Check to see whether a callback thread has requested a disconnected.
116 * @param errorCallbackEnabled set true to call errorCallback on disconnect
117 * @return AAUDIO_OK or AAUDIO_ERROR_DISCONNECTED
118 */
119 aaudio_result_t checkForDisconnectRequest(bool errorCallbackEnabled);
Phil Burk2d5ba532017-09-06 14:36:11 -0700120
Phil Burk134f1972017-12-08 13:06:11 -0800121 void forceDisconnect(bool errorCallbackEnabled = true);
Phil Burk2d5ba532017-09-06 14:36:11 -0700122
Phil Burkec89b2e2017-06-20 15:05:06 -0700123 int64_t incrementFramesWritten(int32_t frames) {
124 return mFramesWritten.increment(frames);
125 }
126
127 int64_t incrementFramesRead(int32_t frames) {
128 return mFramesRead.increment(frames);
129 }
130
Phil Burkd3813f32020-04-23 16:26:15 -0700131 // This is used for exact matching by MediaMetrics. So do not change it.
Andy Hunga6b27032020-04-27 10:34:24 -0700132 // MediaMetricsConstants.h: AMEDIAMETRICS_PROP_CALLERNAME_VALUE_AAUDIO
Phil Burkd3813f32020-04-23 16:26:15 -0700133 static constexpr char kCallerName[] = "aaudio";
134
Phil Burkec89b2e2017-06-20 15:05:06 -0700135 MonotonicCounter mFramesWritten;
136 MonotonicCounter mFramesRead;
Phil Burk7328a802017-08-30 09:29:48 -0700137 MonotonicCounter mTimestampPosition;
Phil Burkec89b2e2017-06-20 15:05:06 -0700138
Phil Burke4d7bb42017-03-28 11:32:39 -0700139 FixedBlockAdapter *mBlockAdapter = nullptr;
Phil Burk4b867492020-02-12 10:58:05 -0800140 int32_t mBlockAdapterBytesPerFrame = 0;
Phil Burke4d7bb42017-03-28 11:32:39 -0700141 aaudio_wrapping_frames_t mPositionWhenStarting = 0;
142 int32_t mCallbackBufferSize = 0;
Eric Laurentfb00fc72017-05-25 18:17:12 -0700143 const android::sp<StreamDeviceCallback> mDeviceCallback;
Phil Burk2d5ba532017-09-06 14:36:11 -0700144
145 AtomicRequestor mRequestDisconnect;
Phil Burkd3813f32020-04-23 16:26:15 -0700146
Phil Burke4d7bb42017-03-28 11:32:39 -0700147};
148
149} /* namespace aaudio */
150
151#endif //LEGACY_AUDIO_STREAM_LEGACY_H