blob: 0ded8e1fe6e72a3ba56506ed1f4c3c6b4ff69fc8 [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 Burke4d7bb42017-03-28 11:32:39 -070077protected:
Phil Burk5204d312017-05-04 17:16:13 -070078
Eric Laurentfb00fc72017-05-25 18:17:12 -070079 class StreamDeviceCallback : public android::AudioSystem::AudioDeviceCallback
80 {
81 public:
82
83 StreamDeviceCallback(AudioStreamLegacy *parent) : mParent(parent) {}
84 virtual ~StreamDeviceCallback() {}
85
86 virtual void onAudioDeviceUpdate(audio_io_handle_t audioIo __unused,
87 audio_port_handle_t deviceId) {
88 if (mParent != nullptr) {
89 mParent->onAudioDeviceUpdate(deviceId);
90 }
91 }
92
93 AudioStreamLegacy *mParent;
94 };
95
Phil Burk5204d312017-05-04 17:16:13 -070096 aaudio_result_t getBestTimestamp(clockid_t clockId,
97 int64_t *framePosition,
98 int64_t *timeNanoseconds,
99 android::ExtendedTimestamp *extendedTimestamp);
100
Eric Laurentfb00fc72017-05-25 18:17:12 -0700101 void onAudioDeviceUpdate(audio_port_handle_t deviceId);
102
103 void onStart() { mCallbackEnabled.store(true); }
104 void onStop() { mCallbackEnabled.store(false); }
105
Phil Burke4d7bb42017-03-28 11:32:39 -0700106 FixedBlockAdapter *mBlockAdapter = nullptr;
107 aaudio_wrapping_frames_t mPositionWhenStarting = 0;
108 int32_t mCallbackBufferSize = 0;
Eric Laurentfb00fc72017-05-25 18:17:12 -0700109 const android::sp<StreamDeviceCallback> mDeviceCallback;
Phil Burke4d7bb42017-03-28 11:32:39 -0700110};
111
112} /* namespace aaudio */
113
114#endif //LEGACY_AUDIO_STREAM_LEGACY_H