blob: d2ef3c7fde6f7abd58edea1c4f0b7a9657f0f651 [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"
27#include "utility/FixedBlockAdapter.h"
28
29namespace aaudio {
30
31
32typedef void (*aaudio_legacy_callback_t)(int event, void* user, void *info);
33
34enum {
35 /**
36 * Request that the callback function should fill the data buffer of an output stream,
37 * or process the data of an input stream.
38 * The address parameter passed to the callback function will point to a data buffer.
39 * For an input stream, the data is read-only.
40 * The value1 parameter will be the number of frames.
41 * The value2 parameter is reserved and will be set to zero.
42 * The callback should return AAUDIO_CALLBACK_RESULT_CONTINUE or AAUDIO_CALLBACK_RESULT_STOP.
43 */
44 AAUDIO_CALLBACK_OPERATION_PROCESS_DATA,
45
46 /**
47 * Inform the callback function that the stream was disconnected.
48 * The address parameter passed to the callback function will be NULL.
49 * The value1 will be an error code or AAUDIO_OK.
50 * The value2 parameter is reserved and will be set to zero.
51 * The callback return value will be ignored.
52 */
53 AAUDIO_CALLBACK_OPERATION_DISCONNECTED,
54};
55typedef int32_t aaudio_callback_operation_t;
56
57
58class AudioStreamLegacy : public AudioStream, public FixedBlockProcessor {
59public:
60 AudioStreamLegacy();
61
62 virtual ~AudioStreamLegacy();
63
64 aaudio_legacy_callback_t getLegacyCallback();
65
66 // This is public so it can be called from the C callback function.
67 // This is called from the AudioTrack/AudioRecord client.
68 virtual void processCallback(int event, void *info) = 0;
69
70 void processCallbackCommon(aaudio_callback_operation_t opcode, void *info);
71
72 // Implement FixedBlockProcessor
73 int32_t onProcessFixedBlock(uint8_t *buffer, int32_t numBytes) override;
74
Phil Burk4c5129b2017-04-28 15:17:32 -070075 virtual int64_t incrementClientFrameCounter(int32_t frames) = 0;
76
Phil Burkec89b2e2017-06-20 15:05:06 -070077
78 virtual int64_t getFramesWritten() override {
79 return mFramesWritten.get();
80 }
81
82 virtual int64_t getFramesRead() override {
83 return mFramesRead.get();
84 }
85
Phil Burke4d7bb42017-03-28 11:32:39 -070086protected:
Phil Burk5204d312017-05-04 17:16:13 -070087
Eric Laurentfb00fc72017-05-25 18:17:12 -070088 class StreamDeviceCallback : public android::AudioSystem::AudioDeviceCallback
89 {
90 public:
91
92 StreamDeviceCallback(AudioStreamLegacy *parent) : mParent(parent) {}
93 virtual ~StreamDeviceCallback() {}
94
95 virtual void onAudioDeviceUpdate(audio_io_handle_t audioIo __unused,
96 audio_port_handle_t deviceId) {
97 if (mParent != nullptr) {
98 mParent->onAudioDeviceUpdate(deviceId);
99 }
100 }
101
102 AudioStreamLegacy *mParent;
103 };
104
Phil Burk5204d312017-05-04 17:16:13 -0700105 aaudio_result_t getBestTimestamp(clockid_t clockId,
106 int64_t *framePosition,
107 int64_t *timeNanoseconds,
108 android::ExtendedTimestamp *extendedTimestamp);
109
Eric Laurentfb00fc72017-05-25 18:17:12 -0700110 void onAudioDeviceUpdate(audio_port_handle_t deviceId);
111
112 void onStart() { mCallbackEnabled.store(true); }
113 void onStop() { mCallbackEnabled.store(false); }
114
Phil Burkec89b2e2017-06-20 15:05:06 -0700115 int64_t incrementFramesWritten(int32_t frames) {
116 return mFramesWritten.increment(frames);
117 }
118
119 int64_t incrementFramesRead(int32_t frames) {
120 return mFramesRead.increment(frames);
121 }
122
123 MonotonicCounter mFramesWritten;
124 MonotonicCounter mFramesRead;
125
Phil Burke4d7bb42017-03-28 11:32:39 -0700126 FixedBlockAdapter *mBlockAdapter = nullptr;
127 aaudio_wrapping_frames_t mPositionWhenStarting = 0;
128 int32_t mCallbackBufferSize = 0;
Eric Laurentfb00fc72017-05-25 18:17:12 -0700129 const android::sp<StreamDeviceCallback> mDeviceCallback;
Phil Burke4d7bb42017-03-28 11:32:39 -0700130};
131
132} /* namespace aaudio */
133
134#endif //LEGACY_AUDIO_STREAM_LEGACY_H