|
| 1 | +/* |
| 2 | + * Copyright 2026 Alex Andres |
| 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 JNI_WEBRTC_API_OBSERVER_SLOT_H_ |
| 18 | +#define JNI_WEBRTC_API_OBSERVER_SLOT_H_ |
| 19 | + |
| 20 | +#include "JavaUtils.h" |
| 21 | + |
| 22 | +#include <jni.h> |
| 23 | + |
| 24 | +#include <functional> |
| 25 | +#include <memory> |
| 26 | +#include <mutex> |
| 27 | +#include <vector> |
| 28 | + |
| 29 | +namespace jni |
| 30 | +{ |
| 31 | + // Guards the creation and the taking of observer slots, never held while |
| 32 | + // WebRTC is called. |
| 33 | + std::mutex & ObserverSlotMutex(); |
| 34 | + |
| 35 | + // The native observer a Java object registers on its WebRTC object, which |
| 36 | + // has room for one, kept in a long field of that Java object. |
| 37 | + // |
| 38 | + // Registering and removing may happen on several threads at once, and |
| 39 | + // while the object is disposed. WebRTC is told by one thread at a time: |
| 40 | + // a thread that finds another one telling WebRTC leaves its observer to |
| 41 | + // that thread and returns, so no thread waits for another, which may be |
| 42 | + // waiting for a WebRTC thread that runs a callback of the observer. An |
| 43 | + // observer is freed once WebRTC has been given a newer one, and never |
| 44 | + // twice. |
| 45 | + // |
| 46 | + // dispose() takes the slot right after the object's handle, and frees it |
| 47 | + // in the handle's release. That release runs once no native method of the |
| 48 | + // object is running any more, the ones that register included, and none |
| 49 | + // can start, so it is the last to tell WebRTC and finds the observer that |
| 50 | + // WebRTC has. A registration that comes too late to get a slot frees its |
| 51 | + // observer without telling WebRTC. |
| 52 | + template <typename T> |
| 53 | + class ObserverSlot |
| 54 | + { |
| 55 | + public: |
| 56 | + // Tells WebRTC about the observer that is to be registered now, or |
| 57 | + // to be removed when target is null. previous is the one it was |
| 58 | + // told about last through this slot, or null. |
| 59 | + using Apply = std::function<void(T * previous, T * target)>; |
| 60 | + |
| 61 | + ~ObserverSlot() |
| 62 | + { |
| 63 | + delete desired; |
| 64 | + |
| 65 | + for (T * observer : retired) { |
| 66 | + delete observer; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + // Registers the observer through the Java object, or removes the |
| 71 | + // one registered through it when observer is null, and takes |
| 72 | + // ownership of it. |
| 73 | + static void Set(JNIEnv * env, jobject object, const char * field, T * observer, const Apply & apply) |
| 74 | + { |
| 75 | + std::unique_ptr<T> owned(observer); |
| 76 | + ObserverSlot * slot = Get(env, object, field); |
| 77 | + |
| 78 | + if (slot == nullptr) { |
| 79 | + // Disposed meanwhile. |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + std::vector<T *> unused; |
| 84 | + std::unique_lock<std::mutex> lock(slot->mutex); |
| 85 | + |
| 86 | + if (slot->desired != nullptr) { |
| 87 | + slot->retired.push_back(slot->desired); |
| 88 | + } |
| 89 | + |
| 90 | + slot->desired = owned.release(); |
| 91 | + |
| 92 | + if (slot->busy) { |
| 93 | + // The thread telling WebRTC goes on with this observer. |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + slot->busy = true; |
| 98 | + |
| 99 | + while (true) { |
| 100 | + T * target = slot->desired; |
| 101 | + T * previous = slot->applied_; |
| 102 | + |
| 103 | + lock.unlock(); |
| 104 | + |
| 105 | + try { |
| 106 | + apply(previous, target); |
| 107 | + } |
| 108 | + catch (...) { |
| 109 | + lock.lock(); |
| 110 | + slot->busy = false; |
| 111 | + throw; |
| 112 | + } |
| 113 | + |
| 114 | + lock.lock(); |
| 115 | + |
| 116 | + slot->applied_ = target; |
| 117 | + |
| 118 | + if (slot->desired == target) { |
| 119 | + // WebRTC has the latest; it has let go of all the others. |
| 120 | + slot->busy = false; |
| 121 | + unused.swap(slot->retired); |
| 122 | + break; |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + lock.unlock(); |
| 127 | + |
| 128 | + for (T * unusedObserver : unused) { |
| 129 | + delete unusedObserver; |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + // Takes the slot of a Java object whose handle has been taken, for |
| 134 | + // the handle's release to free. No slot is made for it afterwards. |
| 135 | + static std::shared_ptr<ObserverSlot> Take(JNIEnv * env, jobject object, const char * field) |
| 136 | + { |
| 137 | + std::lock_guard<std::mutex> lock(ObserverSlotMutex()); |
| 138 | + |
| 139 | + ObserverSlot * slot = GetHandle<ObserverSlot>(env, object, field); |
| 140 | + |
| 141 | + if (slot != nullptr) { |
| 142 | + SetHandle<std::nullptr_t>(env, object, field, nullptr); |
| 143 | + } |
| 144 | + |
| 145 | + return std::shared_ptr<ObserverSlot>(slot); |
| 146 | + } |
| 147 | + |
| 148 | + // The observer WebRTC was told about last, or null. Only for the |
| 149 | + // release of a disposed object, when nothing registers any more. |
| 150 | + T * applied() const |
| 151 | + { |
| 152 | + return applied_; |
| 153 | + } |
| 154 | + |
| 155 | + private: |
| 156 | + ObserverSlot() = default; |
| 157 | + |
| 158 | + // The slot of the Java object, made on first use, or null once its |
| 159 | + // handle has been taken. |
| 160 | + static ObserverSlot * Get(JNIEnv * env, jobject object, const char * field) |
| 161 | + { |
| 162 | + std::lock_guard<std::mutex> lock(ObserverSlotMutex()); |
| 163 | + |
| 164 | + ObserverSlot * slot = GetHandle<ObserverSlot>(env, object, field); |
| 165 | + |
| 166 | + if (slot != nullptr) { |
| 167 | + return slot; |
| 168 | + } |
| 169 | + |
| 170 | + // Read under the lock that Take() holds: a dispose that took |
| 171 | + // the handle before takes this slot too, or finds none. |
| 172 | + if (GetHandle<void>(env, object) == nullptr) { |
| 173 | + return nullptr; |
| 174 | + } |
| 175 | + |
| 176 | + slot = new ObserverSlot(); |
| 177 | + |
| 178 | + SetHandle(env, object, field, slot); |
| 179 | + |
| 180 | + return slot; |
| 181 | + } |
| 182 | + |
| 183 | + private: |
| 184 | + std::mutex mutex; |
| 185 | + // The observer to be registered last, owned. |
| 186 | + T * desired = nullptr; |
| 187 | + // What WebRTC was told about last, desired or one of retired. |
| 188 | + T * applied_ = nullptr; |
| 189 | + // Replaced observers WebRTC may still have, owned. |
| 190 | + std::vector<T *> retired; |
| 191 | + // Whether a thread is telling WebRTC. |
| 192 | + bool busy = false; |
| 193 | + }; |
| 194 | +} |
| 195 | + |
| 196 | +#endif |
0 commit comments