blob: 66c216c163e7f46bc73f570bd2c26fb1f660044f [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
Phil Burk7328a802017-08-30 09:29:48 -070066 int32_t callDataCallbackFrames(uint8_t *buffer, int32_t numFrames);
67
Phil Burke4d7bb42017-03-28 11:32:39 -070068 // This is public so it can be called from the C callback function.
69 // This is called from the AudioTrack/AudioRecord client.
70 virtual void processCallback(int event, void *info) = 0;
71
72 void processCallbackCommon(aaudio_callback_operation_t opcode, void *info);
73
74 // Implement FixedBlockProcessor
75 int32_t onProcessFixedBlock(uint8_t *buffer, int32_t numBytes) override;
76
Phil Burk4c5129b2017-04-28 15:17:32 -070077 virtual int64_t incrementClientFrameCounter(int32_t frames) = 0;
78
Phil Burkec89b2e2017-06-20 15:05:06 -070079
80 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
114 void onStart() { mCallbackEnabled.store(true); }
115 void onStop() { mCallbackEnabled.store(false); }
116
Phil Burkec89b2e2017-06-20 15:05:06 -0700117 int64_t incrementFramesWritten(int32_t frames) {
118 return mFramesWritten.increment(frames);
119 }
120
121 int64_t incrementFramesRead(int32_t frames) {
122 return mFramesRead.increment(frames);
123 }
124
125 MonotonicCounter mFramesWritten;
126 MonotonicCounter mFramesRead;
Phil Burk7328a802017-08-30 09:29:48 -0700127 MonotonicCounter mTimestampPosition;
Phil Burkec89b2e2017-06-20 15:05:06 -0700128
Phil Burke4d7bb42017-03-28 11:32:39 -0700129 FixedBlockAdapter *mBlockAdapter = nullptr;
130 aaudio_wrapping_frames_t mPositionWhenStarting = 0;
131 int32_t mCallbackBufferSize = 0;
Eric Laurentfb00fc72017-05-25 18:17:12 -0700132 const android::sp<StreamDeviceCallback> mDeviceCallback;
Phil Burke4d7bb42017-03-28 11:32:39 -0700133};
134
135} /* namespace aaudio */
136
137#endif //LEGACY_AUDIO_STREAM_LEGACY_H