Phil Burk | 15f7cab | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
Phil Burk | 0bd745e | 2020-10-17 18:20:01 +0000 | [diff] [blame] | 23 | #include <android-base/thread_annotations.h> |
| 24 | |
Phil Burk | 15f7cab | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 25 | #include "AAudioServiceEndpoint.h" |
| 26 | #include "client/AudioStreamInternal.h" |
| 27 | #include "client/AudioStreamInternalPlay.h" |
| 28 | #include "AAudioServiceStreamShared.h" |
| 29 | #include "AAudioServiceStreamMMAP.h" |
| 30 | #include "AAudioService.h" |
| 31 | |
| 32 | namespace aaudio { |
| 33 | |
| 34 | /** |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 35 | * This manages an AudioStreamInternal that is shared by multiple Client streams. |
Phil Burk | 15f7cab | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 36 | */ |
| 37 | class AAudioServiceEndpointShared : public AAudioServiceEndpoint { |
| 38 | |
| 39 | public: |
Phil Burk | 58f5ce1 | 2020-08-12 14:29:10 +0000 | [diff] [blame] | 40 | explicit AAudioServiceEndpointShared(AudioStreamInternal *streamInternal); |
Phil Burk | 15f7cab | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 41 | |
Phil Burk | dd58292 | 2020-10-15 20:29:51 +0000 | [diff] [blame] | 42 | virtual ~AAudioServiceEndpointShared() = default; |
| 43 | |
Phil Burk | 15f7cab | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 44 | std::string dump() const override; |
| 45 | |
| 46 | aaudio_result_t open(const aaudio::AAudioStreamRequest &request) override; |
| 47 | |
Phil Burk | 320910f | 2020-08-12 14:29:10 +0000 | [diff] [blame] | 48 | void close() override; |
Phil Burk | 15f7cab | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 49 | |
| 50 | aaudio_result_t startStream(android::sp<AAudioServiceStreamBase> stream, |
| 51 | audio_port_handle_t *clientHandle) override; |
| 52 | |
| 53 | aaudio_result_t stopStream(android::sp<AAudioServiceStreamBase> stream, |
| 54 | audio_port_handle_t clientHandle) override; |
| 55 | |
| 56 | aaudio_result_t getFreeRunningPosition(int64_t *positionFrames, int64_t *timeNanos) override; |
| 57 | |
| 58 | aaudio_result_t getTimestamp(int64_t *positionFrames, int64_t *timeNanos) override; |
| 59 | |
Phil Burk | a3901e9 | 2018-10-08 13:54:38 -0700 | [diff] [blame] | 60 | virtual void *callbackLoop() = 0; |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 61 | |
Phil Burk | 15f7cab | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 62 | AudioStreamInternal *getStreamInternal() const { |
Phil Burk | 58f5ce1 | 2020-08-12 14:29:10 +0000 | [diff] [blame] | 63 | return mStreamInternal.get(); |
Phil Burk | 15f7cab | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 64 | }; |
| 65 | |
Phil Burk | dd58292 | 2020-10-15 20:29:51 +0000 | [diff] [blame] | 66 | protected: |
| 67 | |
Phil Burk | 0bd745e | 2020-10-17 18:20:01 +0000 | [diff] [blame] | 68 | aaudio_result_t startSharingThread_l() REQUIRES(mLockStreams); |
Phil Burk | 15f7cab | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 69 | |
| 70 | aaudio_result_t stopSharingThread(); |
| 71 | |
Phil Burk | 58f5ce1 | 2020-08-12 14:29:10 +0000 | [diff] [blame] | 72 | // An MMAP stream that is shared by multiple clients. |
| 73 | android::sp<AudioStreamInternal> mStreamInternal; |
Phil Burk | 15f7cab | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 74 | |
| 75 | std::atomic<bool> mCallbackEnabled{false}; |
| 76 | |
| 77 | std::atomic<int> mRunningStreamCount{0}; |
| 78 | }; |
| 79 | |
| 80 | } |
| 81 | |
| 82 | #endif //AAUDIO_SERVICE_ENDPOINT_SHARED_H |