Home | History | Annotate | Download | only in oboeservice
      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_AAUDIO_ENDPOINT_MANAGER_H
     18 #define AAUDIO_AAUDIO_ENDPOINT_MANAGER_H
     19 
     20 #include <map>
     21 #include <mutex>
     22 #include <utils/Singleton.h>
     23 
     24 #include "binding/AAudioServiceMessage.h"
     25 #include "AAudioServiceEndpoint.h"
     26 #include "AAudioServiceEndpointCapture.h"
     27 #include "AAudioServiceEndpointMMAP.h"
     28 #include "AAudioServiceEndpointPlay.h"
     29 
     30 namespace aaudio {
     31 
     32 class AAudioEndpointManager : public android::Singleton<AAudioEndpointManager> {
     33 public:
     34     AAudioEndpointManager();
     35     ~AAudioEndpointManager() = default;
     36 
     37     /**
     38      * Returns information about the state of the this class.
     39      *
     40      * Will attempt to get the object lock, but will proceed
     41      * even if it cannot.
     42      *
     43      * Each line of information ends with a newline.
     44      *
     45      * @return a string with useful information
     46      */
     47     std::string dump() const;
     48 
     49     /**
     50      * Find a service endpoint for the given deviceId, sessionId and direction.
     51      * If an endpoint does not already exist then try to create one.
     52      *
     53      * @param audioService
     54      * @param request
     55      * @param sharingMode
     56      * @return endpoint or null
     57      */
     58     android::sp<AAudioServiceEndpoint> openEndpoint(android::AAudioService &audioService,
     59                                         const aaudio::AAudioStreamRequest &request,
     60                                         aaudio_sharing_mode_t sharingMode);
     61 
     62     void closeEndpoint(android::sp<AAudioServiceEndpoint> serviceEndpoint);
     63 
     64 private:
     65     android::sp<AAudioServiceEndpoint> openExclusiveEndpoint(android::AAudioService &aaudioService,
     66                                                  const aaudio::AAudioStreamRequest &request);
     67 
     68     android::sp<AAudioServiceEndpoint> openSharedEndpoint(android::AAudioService &aaudioService,
     69                                               const aaudio::AAudioStreamRequest &request);
     70 
     71     android::sp<AAudioServiceEndpoint> findExclusiveEndpoint_l(
     72             const AAudioStreamConfiguration& configuration);
     73 
     74     android::sp<AAudioServiceEndpointShared> findSharedEndpoint_l(
     75             const AAudioStreamConfiguration& configuration);
     76 
     77     void closeExclusiveEndpoint(android::sp<AAudioServiceEndpoint> serviceEndpoint);
     78     void closeSharedEndpoint(android::sp<AAudioServiceEndpoint> serviceEndpoint);
     79 
     80     // Use separate locks because opening a Shared endpoint requires opening an Exclusive one.
     81     // That could cause a recursive lock.
     82     // Lock mSharedLock before mExclusiveLock.
     83     // it is OK to only lock mExclusiveLock.
     84     mutable std::mutex                                     mSharedLock;
     85     std::vector<android::sp<AAudioServiceEndpointShared>>  mSharedStreams;
     86 
     87     mutable std::mutex                                     mExclusiveLock;
     88     std::vector<android::sp<AAudioServiceEndpointMMAP>>    mExclusiveStreams;
     89 
     90     // Modified under a lock.
     91     int32_t mExclusiveSearchCount = 0; // number of times we SEARCHED for an exclusive endpoint
     92     int32_t mExclusiveFoundCount  = 0; // number of times we FOUND an exclusive endpoint
     93     int32_t mExclusiveOpenCount   = 0; // number of times we OPENED an exclusive endpoint
     94     int32_t mExclusiveCloseCount  = 0; // number of times we CLOSED an exclusive endpoint
     95     // Same as above but for SHARED endpoints.
     96     int32_t mSharedSearchCount    = 0;
     97     int32_t mSharedFoundCount     = 0;
     98     int32_t mSharedOpenCount      = 0;
     99     int32_t mSharedCloseCount     = 0;
    100 };
    101 } /* namespace aaudio */
    102 
    103 #endif //AAUDIO_AAUDIO_ENDPOINT_MANAGER_H
    104