Sungtak Lee | 97e1dfb | 2022-12-07 07:45:45 +0000 | [diff] [blame] | 1 | /* |
Sungtak Lee | 8878a13 | 2022-12-07 11:42:03 +0000 | [diff] [blame^] | 2 | * Copyright (C) 2022 The Android Open Source Project |
Sungtak Lee | 97e1dfb | 2022-12-07 07:45:45 +0000 | [diff] [blame] | 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 | #include "Observer.h" |
Sungtak Lee | 8878a13 | 2022-12-07 11:42:03 +0000 | [diff] [blame^] | 18 | #include "BufferPoolClient.h" |
Sungtak Lee | 97e1dfb | 2022-12-07 07:45:45 +0000 | [diff] [blame] | 19 | |
Sungtak Lee | 8878a13 | 2022-12-07 11:42:03 +0000 | [diff] [blame^] | 20 | namespace aidl::android::hardware::media::bufferpool2::implementation { |
Sungtak Lee | 97e1dfb | 2022-12-07 07:45:45 +0000 | [diff] [blame] | 21 | |
| 22 | Observer::Observer() { |
| 23 | } |
| 24 | |
| 25 | Observer::~Observer() { |
| 26 | } |
| 27 | |
Sungtak Lee | 8878a13 | 2022-12-07 11:42:03 +0000 | [diff] [blame^] | 28 | ::ndk::ScopedAStatus Observer::onMessage(int64_t in_connectionId, int32_t in_msgId) { |
Sungtak Lee | 97e1dfb | 2022-12-07 07:45:45 +0000 | [diff] [blame] | 29 | std::unique_lock<std::mutex> lock(mLock); |
Sungtak Lee | 8878a13 | 2022-12-07 11:42:03 +0000 | [diff] [blame^] | 30 | auto it = mClients.find(in_connectionId); |
Sungtak Lee | 97e1dfb | 2022-12-07 07:45:45 +0000 | [diff] [blame] | 31 | if (it != mClients.end()) { |
| 32 | const std::shared_ptr<BufferPoolClient> client = it->second.lock(); |
| 33 | if (!client) { |
| 34 | mClients.erase(it); |
| 35 | } else { |
| 36 | lock.unlock(); |
Sungtak Lee | 8878a13 | 2022-12-07 11:42:03 +0000 | [diff] [blame^] | 37 | client->receiveInvalidation(in_msgId); |
Sungtak Lee | 97e1dfb | 2022-12-07 07:45:45 +0000 | [diff] [blame] | 38 | } |
| 39 | } |
Sungtak Lee | 8878a13 | 2022-12-07 11:42:03 +0000 | [diff] [blame^] | 40 | return ::ndk::ScopedAStatus::ok(); |
Sungtak Lee | 97e1dfb | 2022-12-07 07:45:45 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | void Observer::addClient(ConnectionId connectionId, |
| 44 | const std::weak_ptr<BufferPoolClient> &wclient) { |
| 45 | std::lock_guard<std::mutex> lock(mLock); |
| 46 | for (auto it = mClients.begin(); it != mClients.end();) { |
| 47 | if (!it->second.lock() || it->first == connectionId) { |
| 48 | it = mClients.erase(it); |
| 49 | } else { |
| 50 | ++it; |
| 51 | } |
| 52 | } |
| 53 | mClients.insert(std::make_pair(connectionId, wclient)); |
| 54 | |
| 55 | } |
| 56 | |
| 57 | void Observer::delClient(ConnectionId connectionId) { |
| 58 | std::lock_guard<std::mutex> lock(mLock); |
| 59 | mClients.erase(connectionId); |
| 60 | } |
| 61 | |
| 62 | |
Sungtak Lee | 8878a13 | 2022-12-07 11:42:03 +0000 | [diff] [blame^] | 63 | } // namespace aidl::android::hardware::media::bufferpool2::implementation |