webrtc_environment.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. // This file is part of Desktop App Toolkit,
  2. // a set of libraries for developing nice desktop applications.
  3. //
  4. // For license and copyright information please follow this link:
  5. // https://github.com/desktop-app/legal/blob/master/LEGAL
  6. //
  7. #include "webrtc/webrtc_environment.h"
  8. #include "base/debug_log.h"
  9. #include "webrtc/platform/webrtc_platform_environment.h"
  10. #include "webrtc/details/webrtc_environment_openal.h"
  11. #include "webrtc/details/webrtc_environment_video_capture.h"
  12. #ifdef WEBRTC_MAC
  13. #include "webrtc/platform/mac/webrtc_environment_mac.h"
  14. #endif // WEBRTC_MAC
  15. #include <crl/crl_async.h>
  16. namespace Webrtc {
  17. namespace {
  18. [[nodiscard]] QString SerializeDevices(const std::vector<DeviceInfo> &list) {
  19. auto result = QStringList();
  20. for (const auto &device : list) {
  21. result.push_back('"' + device.name + "\" <" + device.id + ">");
  22. }
  23. return "{ " + result.join(", ") + " }";
  24. }
  25. [[nodiscard]] QString TypeToString(DeviceType type) {
  26. switch (type) {
  27. case DeviceType::Playback: return "Playback";
  28. case DeviceType::Capture: return "Capture";
  29. case DeviceType::Camera: return "Camera";
  30. }
  31. Unexpected("Type in TypeToString.");
  32. }
  33. } // namespace
  34. Environment::Environment()
  35. : _platform(
  36. Platform::CreateEnvironment((Platform::EnvironmentDelegate*)this))
  37. , _devices{
  38. resolveDevices(DeviceType::Playback),
  39. resolveDevices(DeviceType::Capture),
  40. resolveDevices(DeviceType::Camera),
  41. } {
  42. using Type = DeviceType;
  43. for (const auto type : { Type::Playback, Type::Capture, Type::Camera }) {
  44. if (synced(type)) {
  45. logState(type, LogType::Initial);
  46. } else {
  47. logSyncError(type);
  48. }
  49. }
  50. }
  51. Environment::~Environment() = default;
  52. int Environment::TypeToIndex(DeviceType type) {
  53. const auto result = int(type);
  54. Ensures(result >= 0 && result < kTypeCount);
  55. return result;
  56. }
  57. Environment::Devices Environment::resolveDevices(DeviceType type) const {
  58. return {
  59. .defaultId = _platform->defaultId(type),
  60. .list = _platform->devices(type),
  61. .refreshFullListOnChange = _platform->refreshFullListOnChange(type),
  62. };
  63. }
  64. QString Environment::defaultId(DeviceType type) const {
  65. validateDefaultId(type);
  66. return _devices[TypeToIndex(type)].defaultId;
  67. }
  68. std::vector<DeviceInfo> Environment::devices(DeviceType type) const {
  69. validateDevices(type);
  70. return _devices[TypeToIndex(type)].list;
  71. }
  72. rpl::producer<DevicesChange> Environment::changes(
  73. DeviceType type) const {
  74. const auto devices = &_devices[TypeToIndex(type)];
  75. return devices->changes.events(
  76. ) | rpl::map([=](DevicesChangeEvent &&event) -> DevicesChange {
  77. return { std::move(event.defaultChange), devices->list };
  78. });
  79. }
  80. rpl::producer<DeviceChange> Environment::defaultChanges(
  81. DeviceType type) const {
  82. return _devices[TypeToIndex(type)].changes.events(
  83. ) | rpl::filter([](const DevicesChangeEvent &event) {
  84. return !!event.defaultChange;
  85. }) | rpl::map([](DevicesChangeEvent &&event) {
  86. return std::move(event.defaultChange);
  87. });
  88. }
  89. rpl::producer<std::vector<DeviceInfo>> Environment::devicesValue(
  90. DeviceType type) const {
  91. validateDevices(type);
  92. const auto devices = &_devices[TypeToIndex(type)];
  93. return devices->changes.events_starting_with(
  94. DevicesChangeEvent{ .listChanged = true }
  95. ) | rpl::filter([](const DevicesChangeEvent &event) {
  96. return event.listChanged;
  97. }) | rpl::map([=] {
  98. return devices->list;
  99. });
  100. }
  101. void Environment::forceRefresh(DeviceType type) {
  102. auto &devices = _devices[TypeToIndex(type)];
  103. devices.defaultChangeFrom = std::exchange(
  104. devices.defaultId,
  105. _platform->defaultId(type));
  106. const auto &old = *devices.defaultChangeFrom;
  107. const auto newIsInOldList = ranges::contains(
  108. devices.list,
  109. devices.defaultId,
  110. &DeviceInfo::id);
  111. refreshDevices(type);
  112. const auto oldIsInNewList = ranges::contains(
  113. devices.list,
  114. old,
  115. &DeviceInfo::id);
  116. if (devices.defaultId != old) {
  117. devices.defaultChangeReason = !oldIsInNewList
  118. ? DeviceChangeReason::Disconnected
  119. : !newIsInOldList
  120. ? DeviceChangeReason::Connected
  121. : DeviceChangeReason::Manual;
  122. }
  123. maybeNotify(type);
  124. }
  125. bool Environment::desktopCaptureAllowed() const {
  126. return _platform->desktopCaptureAllowed();
  127. }
  128. std::optional<QString> Environment::uniqueDesktopCaptureSource() const {
  129. return _platform->uniqueDesktopCaptureSource();
  130. }
  131. DeviceResolvedId Environment::threadSafeResolveId(
  132. const DeviceResolvedId &lastResolvedId,
  133. const QString &savedId) const {
  134. return _platform->threadSafeResolveId(lastResolvedId, savedId);
  135. }
  136. void Environment::setCaptureMuted(bool muted) {
  137. _platform->setCaptureMuted(muted);
  138. }
  139. void Environment::setCaptureMuteTracker(
  140. not_null<CaptureMuteTracker*> tracker,
  141. bool track) {
  142. _platform->setCaptureMuteTracker(tracker, track);
  143. }
  144. RecordAvailability Environment::recordAvailability() const {
  145. const_cast<Environment*>(this)->refreshRecordAvailability();
  146. return _recordAvailability.current();
  147. }
  148. auto Environment::recordAvailabilityValue() const
  149. ->rpl::producer<RecordAvailability> {
  150. const_cast<Environment*>(this)->refreshRecordAvailability();
  151. return _recordAvailability.value();
  152. }
  153. void Environment::refreshRecordAvailability() {
  154. if (_recordAvailabilityRefreshing) {
  155. _recordAvailabilityRefreshPending = true;
  156. return;
  157. }
  158. _recordAvailabilityRefreshing = true;
  159. const auto strong = static_cast<base::has_weak_ptr*>(this);
  160. const auto weak = base::make_weak(strong);
  161. crl::async([weak] {
  162. const auto type = DeviceType::Capture;
  163. const auto audio = details::EnvironmentOpenAL::DefaultId(type);
  164. #ifdef WEBRTC_MAC
  165. const auto vtype = DeviceType::Camera;
  166. const auto video = Platform::EnvironmentMac::DefaultId(vtype);
  167. #else // WEBRTC_MAC
  168. const auto video = details::EnvironmentVideoCapture::DefaultId();
  169. #endif // WEBRTC_MAC
  170. const auto availability = audio.isEmpty()
  171. ? RecordAvailability::None
  172. : video.isEmpty()
  173. ? RecordAvailability::Audio
  174. : RecordAvailability::VideoAndAudio;
  175. crl::on_main([weak, availability] {
  176. if (const auto strong = weak.get()) {
  177. const auto that = static_cast<Environment*>(strong);
  178. that->applyRecordAvailability(availability);
  179. }
  180. });
  181. });
  182. }
  183. void Environment::applyRecordAvailability(RecordAvailability value) {
  184. _recordAvailability = value;
  185. _recordAvailabilityRefreshing = false;
  186. if (base::take(_recordAvailabilityRefreshPending)) {
  187. refreshRecordAvailability();
  188. }
  189. }
  190. void Environment::defaultChanged(
  191. DeviceType type,
  192. DeviceChangeReason reason,
  193. QString nowId) {
  194. const auto guard = gsl::finally([&] {
  195. validateAfterDefaultChange(type);
  196. maybeNotify(type);
  197. });
  198. auto &devices = _devices[TypeToIndex(type)];
  199. devices.defaultChangeFrom = std::exchange(
  200. devices.defaultId,
  201. std::move(nowId));
  202. devices.defaultChangeReason = reason;
  203. }
  204. void Environment::deviceStateChanged(
  205. DeviceType type,
  206. QString id,
  207. DeviceStateChange state) {
  208. const auto guard = gsl::finally([&] {
  209. validateAfterListChange(type);
  210. maybeNotify(type);
  211. });
  212. auto &devices = _devices[TypeToIndex(type)];
  213. if (devices.refreshFullListOnChange) {
  214. refreshDevices(type);
  215. }
  216. const auto i = ranges::find(
  217. devices.list,
  218. id,
  219. &DeviceInfo::id);
  220. if (i == end(devices.list)) {
  221. if (state == DeviceStateChange::Disconnected) {
  222. return;
  223. } else if (auto info = _platform->device(type, id)) {
  224. devices.list.push_back(std::move(info));
  225. devices.listChanged = true;
  226. }
  227. } else if (state == DeviceStateChange::Disconnected) {
  228. const auto from = ranges::remove(
  229. i,
  230. end(devices.list),
  231. id,
  232. &DeviceInfo::id);
  233. if (from != end(devices.list)) {
  234. devices.list.erase(from, end(devices.list));
  235. devices.listChanged = true;
  236. }
  237. } else if (i != end(devices.list)) {
  238. const auto inactive = (state != DeviceStateChange::Active);
  239. if (i->inactive != inactive) {
  240. i->inactive = inactive;
  241. devices.listChanged = true;
  242. }
  243. }
  244. }
  245. void Environment::devicesForceRefresh(DeviceType type) {
  246. forceRefresh(type);
  247. }
  248. void Environment::validateDefaultId(DeviceType type) const {
  249. _platform->defaultIdRequested(type);
  250. }
  251. void Environment::validateDevices(DeviceType type) const {
  252. _platform->devicesRequested(type);
  253. }
  254. bool Environment::synced(DeviceType type) const {
  255. const auto &devices = _devices[TypeToIndex(type)];
  256. return ranges::contains(
  257. devices.list,
  258. devices.defaultId,
  259. &DeviceInfo::id);
  260. }
  261. void Environment::validateAfterListChange(DeviceType type) {
  262. auto &devices = _devices[TypeToIndex(type)];
  263. if (!devices.listChanged || synced(type)) {
  264. return;
  265. }
  266. devices.defaultChangeFrom = std::exchange(
  267. devices.defaultId,
  268. _platform->defaultId(type));
  269. devices.defaultChangeReason = DeviceChangeReason::Disconnected;
  270. if (devices.defaultChangeFrom != devices.defaultId
  271. && synced(type)) {
  272. return;
  273. }
  274. refreshDevices(type);
  275. if (!devices.listChanged || !synced(type)) {
  276. logSyncError(type);
  277. }
  278. }
  279. void Environment::validateAfterDefaultChange(DeviceType type) {
  280. auto &devices = _devices[TypeToIndex(type)];
  281. if (!devices.defaultChangeFrom
  282. || devices.defaultChangeFrom == devices.defaultId
  283. || synced(type)) {
  284. return;
  285. }
  286. refreshDevices(type);
  287. if (devices.listChanged && synced(type)) {
  288. return;
  289. }
  290. auto changedOneMoreFromId = std::exchange(
  291. devices.defaultId,
  292. _platform->defaultId(type));
  293. devices.defaultChangeReason = DeviceChangeReason::Disconnected;
  294. if (devices.defaultId == changedOneMoreFromId || !synced(type)) {
  295. logSyncError(type);
  296. }
  297. }
  298. void Environment::maybeNotify(DeviceType type) {
  299. auto &devices = _devices[TypeToIndex(type)];
  300. if (devices.defaultChangeFrom == devices.defaultId) {
  301. devices.defaultChangeFrom = std::nullopt;
  302. }
  303. if (!devices.listChanged && !devices.defaultChangeFrom) {
  304. return;
  305. }
  306. const auto listChanged = base::take(devices.listChanged);
  307. const auto from = base::take(devices.defaultChangeFrom);
  308. const auto reason = base::take(devices.defaultChangeReason);
  309. devices.changes.fire({
  310. .defaultChange = {
  311. .wasId = from.value_or(QString()),
  312. .nowId = from ? devices.defaultId : QString(),
  313. .reason = (from ? reason : DeviceChangeReason::Manual),
  314. },
  315. .listChanged = listChanged,
  316. });
  317. }
  318. void Environment::logSyncError(DeviceType type) {
  319. auto &devices = _devices[TypeToIndex(type)];
  320. LOG(("Media Error: "
  321. "Can't sync default device for type %1, default: %2, list: %3"
  322. ).arg(TypeToString(type)
  323. ).arg(devices.defaultId
  324. ).arg(SerializeDevices(devices.list)));
  325. }
  326. void Environment::logState(DeviceType type, LogType log) {
  327. auto &devices = _devices[TypeToIndex(type)];
  328. auto phrase = u"Media Info: Type %1, default: %2, list: %3"_q
  329. .arg(TypeToString(type))
  330. .arg(devices.defaultId)
  331. .arg(SerializeDevices(devices.list));
  332. if (log == LogType::Initial) {
  333. phrase += u", full list refresh: %1"_q.arg(
  334. devices.refreshFullListOnChange ? "true" : "false");
  335. }
  336. switch (log) {
  337. case LogType::Initial:
  338. case LogType::Always:
  339. LOG((phrase));
  340. break;
  341. case LogType::Debug:
  342. DEBUG_LOG((phrase));
  343. break;
  344. }
  345. }
  346. void Environment::refreshDevices(DeviceType type) {
  347. auto &devices = _devices[TypeToIndex(type)];
  348. auto list = _platform->devices(type);
  349. if (devices.list != list) {
  350. devices.list = std::move(list);
  351. devices.listChanged = true;
  352. }
  353. }
  354. } // namespace Webrtc