blob: a22e8256370904d6007beeac6747764b97978544 [file] [log] [blame]
Sungtak Lee97e1dfb2022-12-07 07:45:45 +00001/*
Sungtak Lee8878a132022-12-07 11:42:03 +00002 * Copyright (C) 2022 The Android Open Source Project
Sungtak Lee97e1dfb2022-12-07 07:45:45 +00003 *
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 Lee8878a132022-12-07 11:42:03 +000018#include "BufferPoolClient.h"
Sungtak Lee97e1dfb2022-12-07 07:45:45 +000019
Sungtak Lee8878a132022-12-07 11:42:03 +000020namespace aidl::android::hardware::media::bufferpool2::implementation {
Sungtak Lee97e1dfb2022-12-07 07:45:45 +000021
22Observer::Observer() {
23}
24
25Observer::~Observer() {
26}
27
Sungtak Lee8878a132022-12-07 11:42:03 +000028::ndk::ScopedAStatus Observer::onMessage(int64_t in_connectionId, int32_t in_msgId) {
Sungtak Lee97e1dfb2022-12-07 07:45:45 +000029 std::unique_lock<std::mutex> lock(mLock);
Sungtak Lee8878a132022-12-07 11:42:03 +000030 auto it = mClients.find(in_connectionId);
Sungtak Lee97e1dfb2022-12-07 07:45:45 +000031 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 Lee8878a132022-12-07 11:42:03 +000037 client->receiveInvalidation(in_msgId);
Sungtak Lee97e1dfb2022-12-07 07:45:45 +000038 }
39 }
Sungtak Lee8878a132022-12-07 11:42:03 +000040 return ::ndk::ScopedAStatus::ok();
Sungtak Lee97e1dfb2022-12-07 07:45:45 +000041}
42
43void 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
57void Observer::delClient(ConnectionId connectionId) {
58 std::lock_guard<std::mutex> lock(mLock);
59 mClients.erase(connectionId);
60}
61
62
Sungtak Lee8878a132022-12-07 11:42:03 +000063} // namespace aidl::android::hardware::media::bufferpool2::implementation