blob: 2c7395320de144678387342af880af04287d1f32 [file] [log] [blame]
Phil Burk15f7cab2017-08-04 09:13:31 -07001/*
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_SHARED_H
18#define AAUDIO_SERVICE_ENDPOINT_SHARED_H
19
20#include <atomic>
Phil Burk15f7cab2017-08-04 09:13:31 -070021
Phil Burk0bd745e2020-10-17 18:20:01 +000022#include <android-base/thread_annotations.h>
23
Phil Burk15f7cab2017-08-04 09:13:31 -070024#include "AAudioServiceEndpoint.h"
25#include "client/AudioStreamInternal.h"
26#include "client/AudioStreamInternalPlay.h"
27#include "AAudioServiceStreamShared.h"
28#include "AAudioServiceStreamMMAP.h"
29#include "AAudioService.h"
30
31namespace aaudio {
32
33/**
Phil Burkbbd52862018-04-13 11:37:42 -070034 * This manages an AudioStreamInternal that is shared by multiple Client streams.
Phil Burk15f7cab2017-08-04 09:13:31 -070035 */
36class AAudioServiceEndpointShared : public AAudioServiceEndpoint {
37
38public:
Phil Burk58f5ce12020-08-12 14:29:10 +000039 explicit AAudioServiceEndpointShared(AudioStreamInternal *streamInternal);
Phil Burk15f7cab2017-08-04 09:13:31 -070040
jiabin613e6ae2022-12-21 20:20:11 +000041 ~AAudioServiceEndpointShared() override = default;
Phil Burkdd582922020-10-15 20:29:51 +000042
Phil Burk15f7cab2017-08-04 09:13:31 -070043 std::string dump() const override;
44
45 aaudio_result_t open(const aaudio::AAudioStreamRequest &request) override;
46
Phil Burk320910f2020-08-12 14:29:10 +000047 void close() override;
Phil Burk15f7cab2017-08-04 09:13:31 -070048
49 aaudio_result_t startStream(android::sp<AAudioServiceStreamBase> stream,
50 audio_port_handle_t *clientHandle) override;
51
52 aaudio_result_t stopStream(android::sp<AAudioServiceStreamBase> stream,
53 audio_port_handle_t clientHandle) override;
54
55 aaudio_result_t getFreeRunningPosition(int64_t *positionFrames, int64_t *timeNanos) override;
56
57 aaudio_result_t getTimestamp(int64_t *positionFrames, int64_t *timeNanos) override;
58
Phil Burka3901e92018-10-08 13:54:38 -070059 virtual void *callbackLoop() = 0;
Phil Burk19e990e2018-03-22 13:59:34 -070060
Phil Burk15f7cab2017-08-04 09:13:31 -070061 AudioStreamInternal *getStreamInternal() const {
Phil Burk58f5ce12020-08-12 14:29:10 +000062 return mStreamInternal.get();
Phil Burk15f7cab2017-08-04 09:13:31 -070063 };
64
Phil Burkdd582922020-10-15 20:29:51 +000065protected:
66
Phil Burk0bd745e2020-10-17 18:20:01 +000067 aaudio_result_t startSharingThread_l() REQUIRES(mLockStreams);
Phil Burk15f7cab2017-08-04 09:13:31 -070068
69 aaudio_result_t stopSharingThread();
70
jiabin6604ce92022-01-22 00:29:17 +000071 void handleDisconnectRegisteredStreamsAsync();
72
Phil Burk58f5ce12020-08-12 14:29:10 +000073 // An MMAP stream that is shared by multiple clients.
74 android::sp<AudioStreamInternal> mStreamInternal;
Phil Burk15f7cab2017-08-04 09:13:31 -070075
76 std::atomic<bool> mCallbackEnabled{false};
77
78 std::atomic<int> mRunningStreamCount{0};
79};
80
jiabin613e6ae2022-12-21 20:20:11 +000081} // namespace aaudio
Phil Burk15f7cab2017-08-04 09:13:31 -070082
83#endif //AAUDIO_SERVICE_ENDPOINT_SHARED_H