blob: a4ceae6089b151e9dc58425d759d91e89f69ac48 [file] [log] [blame]
Phil Burkc0c70e32017-02-09 13:18:38 -08001/*
2 * Copyright (C) 2017 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 AAUDIO_SERVICE_ENDPOINT_H
18#define AAUDIO_SERVICE_ENDPOINT_H
19
20#include <atomic>
21#include <functional>
22#include <mutex>
23#include <vector>
24
25#include "client/AudioStreamInternal.h"
26#include "binding/AAudioServiceMessage.h"
27#include "AAudioServiceStreamShared.h"
28#include "AAudioServiceStreamMMAP.h"
29#include "AAudioMixer.h"
30#include "AAudioService.h"
31
32namespace aaudio {
33
34class AAudioServiceEndpoint {
35public:
36 explicit AAudioServiceEndpoint(android::AAudioService &audioService);
37 virtual ~AAudioServiceEndpoint();
38
39 aaudio_result_t open(int32_t deviceId, aaudio_direction_t direction);
40
41 int32_t getSampleRate() const { return mStreamInternal.getSampleRate(); }
42 int32_t getSamplesPerFrame() const { return mStreamInternal.getSamplesPerFrame(); }
43 int32_t getFramesPerBurst() const { return mStreamInternal.getFramesPerBurst(); }
44
45 aaudio_result_t registerStream(AAudioServiceStreamShared *sharedStream);
46 aaudio_result_t unregisterStream(AAudioServiceStreamShared *sharedStream);
47 aaudio_result_t startStream(AAudioServiceStreamShared *sharedStream);
48 aaudio_result_t stopStream(AAudioServiceStreamShared *sharedStream);
49 aaudio_result_t close();
50
51 int32_t getDeviceId() const { return mStreamInternal.getDeviceId(); }
52
53 aaudio_direction_t getDirection() const { return mStreamInternal.getDirection(); }
54
55 void disconnectRegisteredStreams();
56
57 void *callbackLoop();
58
Phil Burk71f35bb2017-04-13 16:05:07 -070059 // This should only be called from the AAudioEndpointManager under a mutex.
60 int32_t getReferenceCount() const {
61 return mReferenceCount;
62 }
63
64 // This should only be called from the AAudioEndpointManager under a mutex.
65 void setReferenceCount(int32_t count) {
66 mReferenceCount = count;
67 }
68
Phil Burkc0c70e32017-02-09 13:18:38 -080069private:
70 aaudio_result_t startMixer_l();
71 aaudio_result_t stopMixer_l();
72
73 int64_t calculateReasonableTimeout(int32_t framesPerOperation);
74
75 AudioStreamInternal mStreamInternal;
76 AAudioMixer mMixer;
Phil Burkc0c70e32017-02-09 13:18:38 -080077
78 std::atomic<bool> mCallbackEnabled;
Phil Burk71f35bb2017-04-13 16:05:07 -070079 int32_t mReferenceCount = 0;
Phil Burkc0c70e32017-02-09 13:18:38 -080080
81 std::mutex mLockStreams;
82 std::vector<AAudioServiceStreamShared *> mRegisteredStreams;
83 std::vector<AAudioServiceStreamShared *> mRunningStreams;
Phil Burk71f35bb2017-04-13 16:05:07 -070084
Phil Burkc0c70e32017-02-09 13:18:38 -080085};
86
87} /* namespace aaudio */
88
89
90#endif //AAUDIO_SERVICE_ENDPOINT_H