blob: 020b926717e70da2b675de17f5b1d0b13bfa269f [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>
21#include <mutex>
22
23#include "AAudioServiceEndpoint.h"
24#include "client/AudioStreamInternal.h"
25#include "client/AudioStreamInternalPlay.h"
26#include "AAudioServiceStreamShared.h"
27#include "AAudioServiceStreamMMAP.h"
28#include "AAudioService.h"
29
30namespace aaudio {
31
32/**
Phil Burkbbd52862018-04-13 11:37:42 -070033 * This manages an AudioStreamInternal that is shared by multiple Client streams.
Phil Burk15f7cab2017-08-04 09:13:31 -070034 */
35class AAudioServiceEndpointShared : public AAudioServiceEndpoint {
36
37public:
Phil Burk79224ca2020-08-12 14:29:10 +000038 explicit AAudioServiceEndpointShared(AudioStreamInternal *streamInternal);
Phil Burk15f7cab2017-08-04 09:13:31 -070039
40 std::string dump() const override;
41
42 aaudio_result_t open(const aaudio::AAudioStreamRequest &request) override;
43
Phil Burk47190482020-08-12 14:29:10 +000044 void close() override;
Phil Burk15f7cab2017-08-04 09:13:31 -070045
46 aaudio_result_t startStream(android::sp<AAudioServiceStreamBase> stream,
47 audio_port_handle_t *clientHandle) override;
48
49 aaudio_result_t stopStream(android::sp<AAudioServiceStreamBase> stream,
50 audio_port_handle_t clientHandle) override;
51
52 aaudio_result_t getFreeRunningPosition(int64_t *positionFrames, int64_t *timeNanos) override;
53
54 aaudio_result_t getTimestamp(int64_t *positionFrames, int64_t *timeNanos) override;
55
Phil Burka3901e92018-10-08 13:54:38 -070056 virtual void *callbackLoop() = 0;
Phil Burk19e990e2018-03-22 13:59:34 -070057
58protected:
59
Phil Burk15f7cab2017-08-04 09:13:31 -070060 AudioStreamInternal *getStreamInternal() const {
Phil Burk79224ca2020-08-12 14:29:10 +000061 return mStreamInternal.get();
Phil Burk15f7cab2017-08-04 09:13:31 -070062 };
63
Phil Burk15f7cab2017-08-04 09:13:31 -070064 aaudio_result_t startSharingThread_l();
65
66 aaudio_result_t stopSharingThread();
67
Phil Burk79224ca2020-08-12 14:29:10 +000068 // An MMAP stream that is shared by multiple clients.
69 android::sp<AudioStreamInternal> mStreamInternal;
Phil Burk15f7cab2017-08-04 09:13:31 -070070
71 std::atomic<bool> mCallbackEnabled{false};
72
73 std::atomic<int> mRunningStreamCount{0};
74};
75
76}
77
78#endif //AAUDIO_SERVICE_ENDPOINT_SHARED_H