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 22 #include <android-base/thread_annotations.h> 23 24 #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 31 namespace aaudio { 32 33 /** 34 * This manages an AudioStreamInternal that is shared by multiple Client streams. 35 */ 36 class AAudioServiceEndpointShared : public AAudioServiceEndpoint { 37 38 public: 39 explicit AAudioServiceEndpointShared(AudioStreamInternal *streamInternal); 40 41 ~AAudioServiceEndpointShared() override = default; 42 43 std::string dump() const override; 44 45 aaudio_result_t open(const aaudio::AAudioStreamRequest &request) override; 46 47 void close() override; 48 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 59 virtual void *callbackLoop() = 0; 60 getStreamInternal()61 AudioStreamInternal *getStreamInternal() const { 62 return mStreamInternal.get(); 63 }; 64 65 protected: 66 67 aaudio_result_t startSharingThread_l() REQUIRES(mLockStreams); 68 69 aaudio_result_t stopSharingThread(); 70 71 void handleDisconnectRegisteredStreamsAsync(); 72 73 // An MMAP stream that is shared by multiple clients. 74 android::sp<AudioStreamInternal> mStreamInternal; 75 76 std::atomic<bool> mCallbackEnabled{false}; 77 78 std::atomic<int> mRunningStreamCount{0}; 79 }; 80 81 } // namespace aaudio 82 83 #endif //AAUDIO_SERVICE_ENDPOINT_SHARED_H 84