CamerasParent.cpp 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  4. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "CamerasParent.h"
  6. #include "MediaEngine.h"
  7. #include "MediaUtils.h"
  8. #include "mozilla/Assertions.h"
  9. #include "mozilla/Unused.h"
  10. #include "mozilla/Services.h"
  11. #include "mozilla/Logging.h"
  12. #include "mozilla/ipc/BackgroundParent.h"
  13. #include "mozilla/Preferences.h"
  14. #include "nsIPermissionManager.h"
  15. #include "nsThreadUtils.h"
  16. #include "nsXPCOM.h"
  17. #include "nsNetUtil.h"
  18. #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
  19. #if defined(_WIN32)
  20. #include <process.h>
  21. #define getpid() _getpid()
  22. #endif
  23. #undef LOG
  24. #undef LOG_ENABLED
  25. mozilla::LazyLogModule gCamerasParentLog("CamerasParent");
  26. #define LOG(args) MOZ_LOG(gCamerasParentLog, mozilla::LogLevel::Debug, args)
  27. #define LOG_ENABLED() MOZ_LOG_TEST(gCamerasParentLog, mozilla::LogLevel::Debug)
  28. namespace mozilla {
  29. namespace camera {
  30. // 3 threads are involved in this code:
  31. // - the main thread for some setups, and occassionally for video capture setup
  32. // calls that don't work correctly elsewhere.
  33. // - the IPC thread on which PBackground is running and which receives and
  34. // sends messages
  35. // - a thread which will execute the actual (possibly slow) camera access
  36. // called "VideoCapture". On Windows this is a thread with an event loop
  37. // suitable for UI access.
  38. // InputObserver is owned by CamerasParent, and it has a ref to CamerasParent
  39. void InputObserver::DeviceChange() {
  40. LOG((__PRETTY_FUNCTION__));
  41. MOZ_ASSERT(mParent);
  42. RefPtr<InputObserver> self(this);
  43. RefPtr<nsIRunnable> ipc_runnable =
  44. media::NewRunnableFrom([self]() -> nsresult {
  45. if (self->mParent->IsShuttingDown()) {
  46. return NS_ERROR_FAILURE;
  47. }
  48. Unused << self->mParent->SendDeviceChange();
  49. return NS_OK;
  50. });
  51. nsIThread* thread = mParent->GetBackgroundThread();
  52. MOZ_ASSERT(thread != nullptr);
  53. thread->Dispatch(ipc_runnable, NS_DISPATCH_NORMAL);
  54. };
  55. class FrameSizeChangeRunnable : public Runnable {
  56. public:
  57. FrameSizeChangeRunnable(CamerasParent *aParent, CaptureEngine capEngine,
  58. int cap_id, unsigned int aWidth, unsigned int aHeight)
  59. : mParent(aParent), mCapEngine(capEngine), mCapId(cap_id),
  60. mWidth(aWidth), mHeight(aHeight) {}
  61. NS_IMETHOD Run() override {
  62. if (mParent->IsShuttingDown()) {
  63. // Communication channel is being torn down
  64. LOG(("FrameSizeChangeRunnable is active without active Child"));
  65. mResult = 0;
  66. return NS_OK;
  67. }
  68. if (!mParent->SendFrameSizeChange(mCapEngine, mCapId, mWidth, mHeight)) {
  69. mResult = -1;
  70. } else {
  71. mResult = 0;
  72. }
  73. return NS_OK;
  74. }
  75. int GetResult() {
  76. return mResult;
  77. }
  78. private:
  79. RefPtr<CamerasParent> mParent;
  80. CaptureEngine mCapEngine;
  81. int mCapId;
  82. unsigned int mWidth;
  83. unsigned int mHeight;
  84. int mResult;
  85. };
  86. int
  87. CallbackHelper::FrameSizeChange(unsigned int w, unsigned int h,
  88. unsigned int streams)
  89. {
  90. LOG(("CallbackHelper Video FrameSizeChange: %ux%u", w, h));
  91. RefPtr<FrameSizeChangeRunnable> runnable =
  92. new FrameSizeChangeRunnable(mParent, mCapEngine, mCapturerId, w, h);
  93. MOZ_ASSERT(mParent);
  94. nsIThread * thread = mParent->GetBackgroundThread();
  95. MOZ_ASSERT(thread != nullptr);
  96. thread->Dispatch(runnable, NS_DISPATCH_NORMAL);
  97. return 0;
  98. }
  99. class DeliverFrameRunnable : public Runnable {
  100. public:
  101. DeliverFrameRunnable(CamerasParent *aParent,
  102. CaptureEngine engine,
  103. int cap_id,
  104. ShmemBuffer buffer,
  105. unsigned char* altbuffer,
  106. size_t size,
  107. uint32_t time_stamp,
  108. int64_t ntp_time,
  109. int64_t render_time)
  110. : mParent(aParent), mCapEngine(engine), mCapId(cap_id), mBuffer(Move(buffer)),
  111. mSize(size), mTimeStamp(time_stamp), mNtpTime(ntp_time),
  112. mRenderTime(render_time) {
  113. // No ShmemBuffer (of the right size) was available, so make an
  114. // extra buffer here. We have no idea when we are going to run and
  115. // it will be potentially long after the webrtc frame callback has
  116. // returned, so the copy needs to be no later than here.
  117. // We will need to copy this back into a Shmem later on so we prefer
  118. // using ShmemBuffers to avoid the extra copy.
  119. if (altbuffer != nullptr) {
  120. mAlternateBuffer.reset(new unsigned char[size]);
  121. memcpy(mAlternateBuffer.get(), altbuffer, size);
  122. }
  123. };
  124. NS_IMETHOD Run() override {
  125. if (mParent->IsShuttingDown()) {
  126. // Communication channel is being torn down
  127. mResult = 0;
  128. return NS_OK;
  129. }
  130. if (!mParent->DeliverFrameOverIPC(mCapEngine, mCapId,
  131. Move(mBuffer), mAlternateBuffer.get(),
  132. mSize, mTimeStamp,
  133. mNtpTime, mRenderTime)) {
  134. mResult = -1;
  135. } else {
  136. mResult = 0;
  137. }
  138. return NS_OK;
  139. }
  140. int GetResult() {
  141. return mResult;
  142. }
  143. private:
  144. RefPtr<CamerasParent> mParent;
  145. CaptureEngine mCapEngine;
  146. int mCapId;
  147. ShmemBuffer mBuffer;
  148. mozilla::UniquePtr<unsigned char[]> mAlternateBuffer;
  149. size_t mSize;
  150. uint32_t mTimeStamp;
  151. int64_t mNtpTime;
  152. int64_t mRenderTime;
  153. int mResult;
  154. };
  155. NS_IMPL_ISUPPORTS(CamerasParent, nsIObserver)
  156. NS_IMETHODIMP
  157. CamerasParent::Observe(nsISupports *aSubject,
  158. const char *aTopic,
  159. const char16_t *aData)
  160. {
  161. MOZ_ASSERT(!strcmp(aTopic, NS_XPCOM_WILL_SHUTDOWN_OBSERVER_ID));
  162. nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
  163. MOZ_ASSERT(obs);
  164. obs->RemoveObserver(this, NS_XPCOM_WILL_SHUTDOWN_OBSERVER_ID);
  165. StopVideoCapture();
  166. return NS_OK;
  167. }
  168. nsresult
  169. CamerasParent::DispatchToVideoCaptureThread(Runnable* event)
  170. {
  171. // Don't try to dispatch if we're already on the right thread.
  172. // There's a potential deadlock because the mThreadMonitor is likely
  173. // to be taken already.
  174. MOZ_ASSERT(!mVideoCaptureThread ||
  175. mVideoCaptureThread->thread_id() != PlatformThread::CurrentId());
  176. MonitorAutoLock lock(mThreadMonitor);
  177. while(mChildIsAlive && mWebRTCAlive &&
  178. (!mVideoCaptureThread || !mVideoCaptureThread->IsRunning())) {
  179. mThreadMonitor.Wait();
  180. }
  181. if (!mVideoCaptureThread || !mVideoCaptureThread->IsRunning()) {
  182. return NS_ERROR_FAILURE;
  183. }
  184. RefPtr<Runnable> addrefedEvent = event;
  185. mVideoCaptureThread->message_loop()->PostTask(addrefedEvent.forget());
  186. return NS_OK;
  187. }
  188. void
  189. CamerasParent::StopVideoCapture()
  190. {
  191. LOG((__PRETTY_FUNCTION__));
  192. // We are called from the main thread (xpcom-shutdown) or
  193. // from PBackground (when the Actor shuts down).
  194. // Shut down the WebRTC stack (on the capture thread)
  195. RefPtr<CamerasParent> self(this);
  196. RefPtr<Runnable> webrtc_runnable =
  197. media::NewRunnableFrom([self]() -> nsresult {
  198. MonitorAutoLock lock(self->mThreadMonitor);
  199. self->CloseEngines();
  200. self->mThreadMonitor.NotifyAll();
  201. return NS_OK;
  202. });
  203. DebugOnly<nsresult> rv = DispatchToVideoCaptureThread(webrtc_runnable);
  204. #ifdef DEBUG
  205. // It's ok for the dispatch to fail if the cleanup it has to do
  206. // has been done already.
  207. MOZ_ASSERT(NS_SUCCEEDED(rv) || !mWebRTCAlive);
  208. #endif
  209. // Hold here until the WebRTC thread is gone. We need to dispatch
  210. // the thread deletion *now*, or there will be no more possibility
  211. // to get to the main thread.
  212. MonitorAutoLock lock(mThreadMonitor);
  213. while (mWebRTCAlive) {
  214. mThreadMonitor.Wait();
  215. }
  216. // After closing the WebRTC stack, clean up the
  217. // VideoCapture thread.
  218. if (self->mVideoCaptureThread) {
  219. base::Thread *thread = self->mVideoCaptureThread;
  220. self->mVideoCaptureThread = nullptr;
  221. RefPtr<Runnable> threadShutdown =
  222. media::NewRunnableFrom([thread]() -> nsresult {
  223. if (thread->IsRunning()) {
  224. thread->Stop();
  225. }
  226. delete thread;
  227. return NS_OK;
  228. });
  229. if (NS_FAILED(NS_DispatchToMainThread(threadShutdown))) {
  230. LOG(("Could not dispatch VideoCaptureThread destruction"));
  231. }
  232. }
  233. }
  234. int
  235. CamerasParent::DeliverFrameOverIPC(CaptureEngine cap_engine,
  236. int cap_id,
  237. ShmemBuffer buffer,
  238. unsigned char* altbuffer,
  239. size_t size,
  240. uint32_t time_stamp,
  241. int64_t ntp_time,
  242. int64_t render_time)
  243. {
  244. // No ShmemBuffers were available, so construct one now of the right size
  245. // and copy into it. That is an extra copy, but we expect this to be
  246. // the exceptional case, because we just assured the next call *will* have a
  247. // buffer of the right size.
  248. if (altbuffer != nullptr) {
  249. // Get a shared memory buffer from the pool, at least size big
  250. ShmemBuffer shMemBuff = mShmemPool.Get(this, size);
  251. if (!shMemBuff.Valid()) {
  252. LOG(("No usable Video shmem in DeliverFrame (out of buffers?)"));
  253. // We can skip this frame if we run out of buffers, it's not a real error.
  254. return 0;
  255. }
  256. // get() and Size() check for proper alignment of the segment
  257. memcpy(shMemBuff.GetBytes(), altbuffer, size);
  258. if (!SendDeliverFrame(cap_engine, cap_id,
  259. shMemBuff.Get(), size,
  260. time_stamp, ntp_time, render_time)) {
  261. return -1;
  262. }
  263. } else {
  264. MOZ_ASSERT(buffer.Valid());
  265. // ShmemBuffer was available, we're all good. A single copy happened
  266. // in the original webrtc callback.
  267. if (!SendDeliverFrame(cap_engine, cap_id,
  268. buffer.Get(), size,
  269. time_stamp, ntp_time, render_time)) {
  270. return -1;
  271. }
  272. }
  273. return 0;
  274. }
  275. ShmemBuffer
  276. CamerasParent::GetBuffer(size_t aSize)
  277. {
  278. return mShmemPool.GetIfAvailable(aSize);
  279. }
  280. int
  281. CallbackHelper::DeliverFrame(unsigned char* buffer,
  282. size_t size,
  283. uint32_t time_stamp,
  284. int64_t ntp_time,
  285. int64_t render_time,
  286. void *handle)
  287. {
  288. // Get a shared memory buffer to copy the frame data into
  289. ShmemBuffer shMemBuffer = mParent->GetBuffer(size);
  290. if (!shMemBuffer.Valid()) {
  291. // Either we ran out of buffers or they're not the right size yet
  292. LOG(("Correctly sized Video shmem not available in DeliverFrame"));
  293. // We will do the copy into a(n extra) temporary buffer inside
  294. // the DeliverFrameRunnable constructor.
  295. } else {
  296. // Shared memory buffers of the right size are available, do the copy here.
  297. memcpy(shMemBuffer.GetBytes(), buffer, size);
  298. // Mark the original buffer as cleared.
  299. buffer = nullptr;
  300. }
  301. RefPtr<DeliverFrameRunnable> runnable =
  302. new DeliverFrameRunnable(mParent, mCapEngine, mCapturerId,
  303. Move(shMemBuffer), buffer, size, time_stamp,
  304. ntp_time, render_time);
  305. MOZ_ASSERT(mParent);
  306. nsIThread* thread = mParent->GetBackgroundThread();
  307. MOZ_ASSERT(thread != nullptr);
  308. thread->Dispatch(runnable, NS_DISPATCH_NORMAL);
  309. return 0;
  310. }
  311. // XXX!!! FIX THIS -- we should move to pure DeliverI420Frame
  312. int
  313. CallbackHelper::DeliverI420Frame(const webrtc::I420VideoFrame& webrtc_frame)
  314. {
  315. return DeliverFrame(const_cast<uint8_t*>(webrtc_frame.buffer(webrtc::kYPlane)),
  316. CalcBufferSize(webrtc::kI420, webrtc_frame.width(), webrtc_frame.height()),
  317. webrtc_frame.timestamp(),
  318. webrtc_frame.ntp_time_ms(),
  319. webrtc_frame.render_time_ms(),
  320. (void*) webrtc_frame.native_handle());
  321. }
  322. bool
  323. CamerasParent::RecvReleaseFrame(mozilla::ipc::Shmem&& s) {
  324. mShmemPool.Put(ShmemBuffer(s));
  325. return true;
  326. }
  327. bool
  328. CamerasParent::SetupEngine(CaptureEngine aCapEngine)
  329. {
  330. MOZ_ASSERT(mVideoCaptureThread->thread_id() == PlatformThread::CurrentId());
  331. EngineHelper *helper = &mEngines[aCapEngine];
  332. // Already initialized
  333. if (helper->mEngine) {
  334. return true;
  335. }
  336. webrtc::CaptureDeviceInfo *captureDeviceInfo = nullptr;
  337. switch (aCapEngine) {
  338. case ScreenEngine:
  339. captureDeviceInfo =
  340. new webrtc::CaptureDeviceInfo(webrtc::CaptureDeviceType::Screen);
  341. break;
  342. case BrowserEngine:
  343. captureDeviceInfo =
  344. new webrtc::CaptureDeviceInfo(webrtc::CaptureDeviceType::Browser);
  345. break;
  346. case WinEngine:
  347. captureDeviceInfo =
  348. new webrtc::CaptureDeviceInfo(webrtc::CaptureDeviceType::Window);
  349. break;
  350. case AppEngine:
  351. captureDeviceInfo =
  352. new webrtc::CaptureDeviceInfo(webrtc::CaptureDeviceType::Application);
  353. break;
  354. case CameraEngine:
  355. captureDeviceInfo =
  356. new webrtc::CaptureDeviceInfo(webrtc::CaptureDeviceType::Camera);
  357. break;
  358. default:
  359. LOG(("Invalid webrtc Video engine"));
  360. MOZ_CRASH();
  361. break;
  362. }
  363. helper->mConfig.Set<webrtc::CaptureDeviceInfo>(captureDeviceInfo);
  364. helper->mEngine = webrtc::VideoEngine::Create(helper->mConfig);
  365. if (!helper->mEngine) {
  366. LOG(("VideoEngine::Create failed"));
  367. return false;
  368. }
  369. helper->mPtrViEBase = webrtc::ViEBase::GetInterface(helper->mEngine);
  370. if (!helper->mPtrViEBase) {
  371. LOG(("ViEBase::GetInterface failed"));
  372. return false;
  373. }
  374. if (helper->mPtrViEBase->Init() < 0) {
  375. LOG(("ViEBase::Init failed"));
  376. return false;
  377. }
  378. helper->mPtrViECapture = webrtc::ViECapture::GetInterface(helper->mEngine);
  379. if (!helper->mPtrViECapture) {
  380. LOG(("ViECapture::GetInterface failed"));
  381. return false;
  382. }
  383. RefPtr<InputObserver>* observer = mObservers.AppendElement(new InputObserver(this));
  384. #ifdef DEBUG
  385. MOZ_ASSERT(0 == helper->mPtrViECapture->RegisterInputObserver(observer->get()));
  386. #else
  387. helper->mPtrViECapture->RegisterInputObserver(observer->get());
  388. #endif
  389. helper->mPtrViERender = webrtc::ViERender::GetInterface(helper->mEngine);
  390. if (!helper->mPtrViERender) {
  391. LOG(("ViERender::GetInterface failed"));
  392. return false;
  393. }
  394. return true;
  395. }
  396. void
  397. CamerasParent::CloseEngines()
  398. {
  399. LOG((__PRETTY_FUNCTION__));
  400. if (!mWebRTCAlive) {
  401. return;
  402. }
  403. MOZ_ASSERT(mVideoCaptureThread->thread_id() == PlatformThread::CurrentId());
  404. // Stop the callers
  405. while (mCallbacks.Length()) {
  406. auto capEngine = mCallbacks[0]->mCapEngine;
  407. auto capNum = mCallbacks[0]->mCapturerId;
  408. LOG(("Forcing shutdown of engine %d, capturer %d", capEngine, capNum));
  409. StopCapture(capEngine, capNum);
  410. Unused << ReleaseCaptureDevice(capEngine, capNum);
  411. }
  412. for (int i = 0; i < CaptureEngine::MaxEngine; i++) {
  413. if (mEngines[i].mEngineIsRunning) {
  414. LOG(("Being closed down while engine %d is running!", i));
  415. }
  416. if (mEngines[i].mPtrViERender) {
  417. mEngines[i].mPtrViERender->Release();
  418. mEngines[i].mPtrViERender = nullptr;
  419. }
  420. if (mEngines[i].mPtrViECapture) {
  421. #ifdef DEBUG
  422. MOZ_ASSERT(0 == mEngines[i].mPtrViECapture->DeregisterInputObserver());
  423. #else
  424. mEngines[i].mPtrViECapture->DeregisterInputObserver();
  425. #endif
  426. mEngines[i].mPtrViECapture->Release();
  427. mEngines[i].mPtrViECapture = nullptr;
  428. }
  429. if(mEngines[i].mPtrViEBase) {
  430. mEngines[i].mPtrViEBase->Release();
  431. mEngines[i].mPtrViEBase = nullptr;
  432. }
  433. if (mEngines[i].mEngine) {
  434. mEngines[i].mEngine->SetTraceCallback(nullptr);
  435. webrtc::VideoEngine::Delete(mEngines[i].mEngine);
  436. mEngines[i].mEngine = nullptr;
  437. }
  438. }
  439. mObservers.Clear();
  440. mWebRTCAlive = false;
  441. }
  442. bool
  443. CamerasParent::EnsureInitialized(int aEngine)
  444. {
  445. LOG((__PRETTY_FUNCTION__));
  446. // We're shutting down, don't try to do new WebRTC ops.
  447. if (!mWebRTCAlive) {
  448. return false;
  449. }
  450. CaptureEngine capEngine = static_cast<CaptureEngine>(aEngine);
  451. if (!SetupEngine(capEngine)) {
  452. LOG(("CamerasParent failed to initialize engine"));
  453. return false;
  454. }
  455. return true;
  456. }
  457. // Dispatch the runnable to do the camera operation on the
  458. // specific Cameras thread, preventing us from blocking, and
  459. // chain a runnable to send back the result on the IPC thread.
  460. // It would be nice to get rid of the code duplication here,
  461. // perhaps via Promises.
  462. bool
  463. CamerasParent::RecvNumberOfCaptureDevices(const CaptureEngine& aCapEngine)
  464. {
  465. LOG((__PRETTY_FUNCTION__));
  466. RefPtr<CamerasParent> self(this);
  467. RefPtr<Runnable> webrtc_runnable =
  468. media::NewRunnableFrom([self, aCapEngine]() -> nsresult {
  469. int num = -1;
  470. if (self->EnsureInitialized(aCapEngine)) {
  471. num = self->mEngines[aCapEngine].mPtrViECapture->NumberOfCaptureDevices();
  472. }
  473. RefPtr<nsIRunnable> ipc_runnable =
  474. media::NewRunnableFrom([self, num]() -> nsresult {
  475. if (self->IsShuttingDown()) {
  476. return NS_ERROR_FAILURE;
  477. }
  478. if (num < 0) {
  479. LOG(("RecvNumberOfCaptureDevices couldn't find devices"));
  480. Unused << self->SendReplyFailure();
  481. return NS_ERROR_FAILURE;
  482. } else {
  483. LOG(("RecvNumberOfCaptureDevices: %d", num));
  484. Unused << self->SendReplyNumberOfCaptureDevices(num);
  485. return NS_OK;
  486. }
  487. });
  488. self->mPBackgroundThread->Dispatch(ipc_runnable, NS_DISPATCH_NORMAL);
  489. return NS_OK;
  490. });
  491. DispatchToVideoCaptureThread(webrtc_runnable);
  492. return true;
  493. }
  494. bool
  495. CamerasParent::RecvEnsureInitialized(const CaptureEngine& aCapEngine)
  496. {
  497. LOG((__PRETTY_FUNCTION__));
  498. RefPtr<CamerasParent> self(this);
  499. RefPtr<Runnable> webrtc_runnable =
  500. media::NewRunnableFrom([self, aCapEngine]() -> nsresult {
  501. bool result = self->EnsureInitialized(aCapEngine);
  502. RefPtr<nsIRunnable> ipc_runnable =
  503. media::NewRunnableFrom([self, result]() -> nsresult {
  504. if (self->IsShuttingDown()) {
  505. return NS_ERROR_FAILURE;
  506. }
  507. if (!result) {
  508. LOG(("RecvEnsureInitialized failed"));
  509. Unused << self->SendReplyFailure();
  510. return NS_ERROR_FAILURE;
  511. } else {
  512. LOG(("RecvEnsureInitialized succeeded"));
  513. Unused << self->SendReplySuccess();
  514. return NS_OK;
  515. }
  516. });
  517. self->mPBackgroundThread->Dispatch(ipc_runnable, NS_DISPATCH_NORMAL);
  518. return NS_OK;
  519. });
  520. DispatchToVideoCaptureThread(webrtc_runnable);
  521. return true;
  522. }
  523. bool
  524. CamerasParent::RecvNumberOfCapabilities(const CaptureEngine& aCapEngine,
  525. const nsCString& unique_id)
  526. {
  527. LOG((__PRETTY_FUNCTION__));
  528. LOG(("Getting caps for %s", unique_id.get()));
  529. RefPtr<CamerasParent> self(this);
  530. RefPtr<Runnable> webrtc_runnable =
  531. media::NewRunnableFrom([self, unique_id, aCapEngine]() -> nsresult {
  532. int num = -1;
  533. if (self->EnsureInitialized(aCapEngine)) {
  534. num =
  535. self->mEngines[aCapEngine].mPtrViECapture->NumberOfCapabilities(
  536. unique_id.get(),
  537. MediaEngineSource::kMaxUniqueIdLength);
  538. }
  539. RefPtr<nsIRunnable> ipc_runnable =
  540. media::NewRunnableFrom([self, num]() -> nsresult {
  541. if (self->IsShuttingDown()) {
  542. return NS_ERROR_FAILURE;
  543. }
  544. if (num < 0) {
  545. LOG(("RecvNumberOfCapabilities couldn't find capabilities"));
  546. Unused << self->SendReplyFailure();
  547. return NS_ERROR_FAILURE;
  548. } else {
  549. LOG(("RecvNumberOfCapabilities: %d", num));
  550. }
  551. Unused << self->SendReplyNumberOfCapabilities(num);
  552. return NS_OK;
  553. });
  554. self->mPBackgroundThread->Dispatch(ipc_runnable, NS_DISPATCH_NORMAL);
  555. return NS_OK;
  556. });
  557. DispatchToVideoCaptureThread(webrtc_runnable);
  558. return true;
  559. }
  560. bool
  561. CamerasParent::RecvGetCaptureCapability(const CaptureEngine& aCapEngine,
  562. const nsCString& unique_id,
  563. const int& num)
  564. {
  565. LOG((__PRETTY_FUNCTION__));
  566. LOG(("RecvGetCaptureCapability: %s %d", unique_id.get(), num));
  567. RefPtr<CamerasParent> self(this);
  568. RefPtr<Runnable> webrtc_runnable =
  569. media::NewRunnableFrom([self, unique_id, aCapEngine, num]() -> nsresult {
  570. webrtc::CaptureCapability webrtcCaps;
  571. int error = -1;
  572. if (self->EnsureInitialized(aCapEngine)) {
  573. error = self->mEngines[aCapEngine].mPtrViECapture->GetCaptureCapability(
  574. unique_id.get(), MediaEngineSource::kMaxUniqueIdLength, num, webrtcCaps);
  575. }
  576. RefPtr<nsIRunnable> ipc_runnable =
  577. media::NewRunnableFrom([self, webrtcCaps, error]() -> nsresult {
  578. if (self->IsShuttingDown()) {
  579. return NS_ERROR_FAILURE;
  580. }
  581. CaptureCapability capCap(webrtcCaps.width,
  582. webrtcCaps.height,
  583. webrtcCaps.maxFPS,
  584. webrtcCaps.expectedCaptureDelay,
  585. webrtcCaps.rawType,
  586. webrtcCaps.codecType,
  587. webrtcCaps.interlaced);
  588. LOG(("Capability: %u %u %u %u %d %d",
  589. webrtcCaps.width,
  590. webrtcCaps.height,
  591. webrtcCaps.maxFPS,
  592. webrtcCaps.expectedCaptureDelay,
  593. webrtcCaps.rawType,
  594. webrtcCaps.codecType));
  595. if (error) {
  596. Unused << self->SendReplyFailure();
  597. return NS_ERROR_FAILURE;
  598. }
  599. Unused << self->SendReplyGetCaptureCapability(capCap);
  600. return NS_OK;
  601. });
  602. self->mPBackgroundThread->Dispatch(ipc_runnable, NS_DISPATCH_NORMAL);
  603. return NS_OK;
  604. });
  605. DispatchToVideoCaptureThread(webrtc_runnable);
  606. return true;
  607. }
  608. bool
  609. CamerasParent::RecvGetCaptureDevice(const CaptureEngine& aCapEngine,
  610. const int& aListNumber)
  611. {
  612. LOG((__PRETTY_FUNCTION__));
  613. RefPtr<CamerasParent> self(this);
  614. RefPtr<Runnable> webrtc_runnable =
  615. media::NewRunnableFrom([self, aCapEngine, aListNumber]() -> nsresult {
  616. char deviceName[MediaEngineSource::kMaxDeviceNameLength];
  617. char deviceUniqueId[MediaEngineSource::kMaxUniqueIdLength];
  618. nsCString name;
  619. nsCString uniqueId;
  620. int devicePid = 0;
  621. int error = -1;
  622. if (self->EnsureInitialized(aCapEngine)) {
  623. error = self->mEngines[aCapEngine].mPtrViECapture->GetCaptureDevice(aListNumber,
  624. deviceName,
  625. sizeof(deviceName),
  626. deviceUniqueId,
  627. sizeof(deviceUniqueId),
  628. &devicePid);
  629. }
  630. if (!error) {
  631. name.Assign(deviceName);
  632. uniqueId.Assign(deviceUniqueId);
  633. }
  634. RefPtr<nsIRunnable> ipc_runnable =
  635. media::NewRunnableFrom([self, error, name, uniqueId, devicePid]() {
  636. if (self->IsShuttingDown()) {
  637. return NS_ERROR_FAILURE;
  638. }
  639. if (error) {
  640. LOG(("GetCaptureDevice failed: %d", error));
  641. Unused << self->SendReplyFailure();
  642. return NS_ERROR_FAILURE;
  643. }
  644. bool scary = (devicePid == getpid());
  645. LOG(("Returning %s name %s id (pid = %d)%s", name.get(),
  646. uniqueId.get(), devicePid, (scary? " (scary)" : "")));
  647. Unused << self->SendReplyGetCaptureDevice(name, uniqueId, scary);
  648. return NS_OK;
  649. });
  650. self->mPBackgroundThread->Dispatch(ipc_runnable, NS_DISPATCH_NORMAL);
  651. return NS_OK;
  652. });
  653. DispatchToVideoCaptureThread(webrtc_runnable);
  654. return true;
  655. }
  656. static nsresult
  657. GetPrincipalFromOrigin(const nsACString& aOrigin, nsIPrincipal** aPrincipal)
  658. {
  659. nsAutoCString originNoSuffix;
  660. mozilla::PrincipalOriginAttributes attrs;
  661. if (!attrs.PopulateFromOrigin(aOrigin, originNoSuffix)) {
  662. return NS_ERROR_FAILURE;
  663. }
  664. nsCOMPtr<nsIURI> uri;
  665. nsresult rv = NS_NewURI(getter_AddRefs(uri), originNoSuffix);
  666. NS_ENSURE_SUCCESS(rv, rv);
  667. nsCOMPtr<nsIPrincipal> principal = mozilla::BasePrincipal::CreateCodebasePrincipal(uri, attrs);
  668. principal.forget(aPrincipal);
  669. return NS_OK;
  670. }
  671. // Find out whether the given origin has permission to use the
  672. // camera. If the permission is not persistent, we'll make it
  673. // a one-shot by removing the (session) permission.
  674. static bool
  675. HasCameraPermission(const nsCString& aOrigin)
  676. {
  677. // Name used with nsIPermissionManager
  678. static const char* cameraPermission = "MediaManagerVideo";
  679. bool allowed = false;
  680. nsresult rv;
  681. nsCOMPtr<nsIPermissionManager> mgr =
  682. do_GetService(NS_PERMISSIONMANAGER_CONTRACTID, &rv);
  683. if (NS_SUCCEEDED(rv)) {
  684. nsCOMPtr<nsIIOService> ioServ(do_GetIOService());
  685. nsCOMPtr<nsIURI> uri;
  686. rv = ioServ->NewURI(aOrigin, nullptr, nullptr, getter_AddRefs(uri));
  687. if (NS_SUCCEEDED(rv)) {
  688. // Permanent permissions are only retrievable via principal, not uri
  689. nsCOMPtr<nsIPrincipal> principal;
  690. rv = GetPrincipalFromOrigin(aOrigin, getter_AddRefs(principal));
  691. if (NS_SUCCEEDED(rv)) {
  692. uint32_t video = nsIPermissionManager::UNKNOWN_ACTION;
  693. rv = mgr->TestExactPermissionFromPrincipal(principal,
  694. cameraPermission,
  695. &video);
  696. if (NS_SUCCEEDED(rv)) {
  697. allowed = (video == nsIPermissionManager::ALLOW_ACTION);
  698. }
  699. // Session permissions are removed after one use.
  700. if (allowed) {
  701. mgr->RemoveFromPrincipal(principal, cameraPermission);
  702. }
  703. }
  704. }
  705. }
  706. return allowed;
  707. }
  708. bool
  709. CamerasParent::RecvAllocateCaptureDevice(const CaptureEngine& aCapEngine,
  710. const nsCString& unique_id,
  711. const nsCString& aOrigin)
  712. {
  713. LOG(("%s: Verifying permissions for %s", __PRETTY_FUNCTION__, aOrigin.get()));
  714. RefPtr<CamerasParent> self(this);
  715. RefPtr<Runnable> mainthread_runnable =
  716. media::NewRunnableFrom([self, aCapEngine, unique_id, aOrigin]() -> nsresult {
  717. // Verify whether the claimed origin has received permission
  718. // to use the camera, either persistently or this session (one shot).
  719. bool allowed = HasCameraPermission(aOrigin);
  720. if (!allowed) {
  721. // Developer preference for turning off permission check.
  722. if (Preferences::GetBool("media.navigator.permission.disabled", false)
  723. || Preferences::GetBool("media.navigator.permission.fake")) {
  724. allowed = true;
  725. LOG(("No permission but checks are disabled or fake sources active"));
  726. } else {
  727. LOG(("No camera permission for this origin"));
  728. }
  729. }
  730. // After retrieving the permission (or not) on the main thread,
  731. // bounce to the WebRTC thread to allocate the device (or not),
  732. // then bounce back to the IPC thread for the reply to content.
  733. RefPtr<Runnable> webrtc_runnable =
  734. media::NewRunnableFrom([self, allowed, aCapEngine, unique_id]() -> nsresult {
  735. int numdev = -1;
  736. int error = -1;
  737. if (allowed && self->EnsureInitialized(aCapEngine)) {
  738. error = self->mEngines[aCapEngine].mPtrViECapture->AllocateCaptureDevice(
  739. unique_id.get(), MediaEngineSource::kMaxUniqueIdLength, numdev);
  740. }
  741. RefPtr<nsIRunnable> ipc_runnable =
  742. media::NewRunnableFrom([self, numdev, error]() -> nsresult {
  743. if (self->IsShuttingDown()) {
  744. return NS_ERROR_FAILURE;
  745. }
  746. if (error) {
  747. Unused << self->SendReplyFailure();
  748. return NS_ERROR_FAILURE;
  749. } else {
  750. LOG(("Allocated device nr %d", numdev));
  751. Unused << self->SendReplyAllocateCaptureDevice(numdev);
  752. return NS_OK;
  753. }
  754. });
  755. self->mPBackgroundThread->Dispatch(ipc_runnable, NS_DISPATCH_NORMAL);
  756. return NS_OK;
  757. });
  758. self->DispatchToVideoCaptureThread(webrtc_runnable);
  759. return NS_OK;
  760. });
  761. NS_DispatchToMainThread(mainthread_runnable);
  762. return true;
  763. }
  764. int
  765. CamerasParent::ReleaseCaptureDevice(const CaptureEngine& aCapEngine,
  766. const int& capnum)
  767. {
  768. int error = -1;
  769. if (EnsureInitialized(aCapEngine)) {
  770. error = mEngines[aCapEngine].mPtrViECapture->ReleaseCaptureDevice(capnum);
  771. }
  772. return error;
  773. }
  774. bool
  775. CamerasParent::RecvReleaseCaptureDevice(const CaptureEngine& aCapEngine,
  776. const int& numdev)
  777. {
  778. LOG((__PRETTY_FUNCTION__));
  779. LOG(("RecvReleaseCamera device nr %d", numdev));
  780. RefPtr<CamerasParent> self(this);
  781. RefPtr<Runnable> webrtc_runnable =
  782. media::NewRunnableFrom([self, aCapEngine, numdev]() -> nsresult {
  783. int error = self->ReleaseCaptureDevice(aCapEngine, numdev);
  784. RefPtr<nsIRunnable> ipc_runnable =
  785. media::NewRunnableFrom([self, error, numdev]() -> nsresult {
  786. if (self->IsShuttingDown()) {
  787. LOG(("In Shutdown, not Releasing"));
  788. return NS_ERROR_FAILURE;
  789. }
  790. if (error) {
  791. Unused << self->SendReplyFailure();
  792. LOG(("Failed to free device nr %d", numdev));
  793. return NS_ERROR_FAILURE;
  794. } else {
  795. Unused << self->SendReplySuccess();
  796. LOG(("Freed device nr %d", numdev));
  797. return NS_OK;
  798. }
  799. });
  800. self->mPBackgroundThread->Dispatch(ipc_runnable, NS_DISPATCH_NORMAL);
  801. return NS_OK;
  802. });
  803. DispatchToVideoCaptureThread(webrtc_runnable);
  804. return true;
  805. }
  806. bool
  807. CamerasParent::RecvStartCapture(const CaptureEngine& aCapEngine,
  808. const int& capnum,
  809. const CaptureCapability& ipcCaps)
  810. {
  811. LOG((__PRETTY_FUNCTION__));
  812. RefPtr<CamerasParent> self(this);
  813. RefPtr<Runnable> webrtc_runnable =
  814. media::NewRunnableFrom([self, aCapEngine, capnum, ipcCaps]() -> nsresult {
  815. CallbackHelper** cbh;
  816. webrtc::ExternalRenderer* render;
  817. EngineHelper* helper = nullptr;
  818. int error = -1;
  819. if (self->EnsureInitialized(aCapEngine)) {
  820. cbh = self->mCallbacks.AppendElement(
  821. new CallbackHelper(static_cast<CaptureEngine>(aCapEngine), capnum, self));
  822. render = static_cast<webrtc::ExternalRenderer*>(*cbh);
  823. helper = &self->mEngines[aCapEngine];
  824. error =
  825. helper->mPtrViERender->AddRenderer(capnum, webrtc::kVideoI420, render);
  826. if (!error) {
  827. error = helper->mPtrViERender->StartRender(capnum);
  828. }
  829. webrtc::CaptureCapability capability;
  830. capability.width = ipcCaps.width();
  831. capability.height = ipcCaps.height();
  832. capability.maxFPS = ipcCaps.maxFPS();
  833. capability.expectedCaptureDelay = ipcCaps.expectedCaptureDelay();
  834. capability.rawType = static_cast<webrtc::RawVideoType>(ipcCaps.rawType());
  835. capability.codecType = static_cast<webrtc::VideoCodecType>(ipcCaps.codecType());
  836. capability.interlaced = ipcCaps.interlaced();
  837. if (!error) {
  838. error = helper->mPtrViECapture->StartCapture(capnum, capability);
  839. }
  840. if (!error) {
  841. helper->mEngineIsRunning = true;
  842. }
  843. }
  844. RefPtr<nsIRunnable> ipc_runnable =
  845. media::NewRunnableFrom([self, error]() -> nsresult {
  846. if (self->IsShuttingDown()) {
  847. return NS_ERROR_FAILURE;
  848. }
  849. if (!error) {
  850. Unused << self->SendReplySuccess();
  851. return NS_OK;
  852. } else {
  853. Unused << self->SendReplyFailure();
  854. return NS_ERROR_FAILURE;
  855. }
  856. });
  857. self->mPBackgroundThread->Dispatch(ipc_runnable, NS_DISPATCH_NORMAL);
  858. return NS_OK;
  859. });
  860. DispatchToVideoCaptureThread(webrtc_runnable);
  861. return true;
  862. }
  863. void
  864. CamerasParent::StopCapture(const CaptureEngine& aCapEngine,
  865. const int& capnum)
  866. {
  867. if (EnsureInitialized(aCapEngine)) {
  868. mEngines[aCapEngine].mPtrViECapture->StopCapture(capnum);
  869. mEngines[aCapEngine].mPtrViERender->StopRender(capnum);
  870. mEngines[aCapEngine].mPtrViERender->RemoveRenderer(capnum);
  871. mEngines[aCapEngine].mEngineIsRunning = false;
  872. for (size_t i = 0; i < mCallbacks.Length(); i++) {
  873. if (mCallbacks[i]->mCapEngine == aCapEngine
  874. && mCallbacks[i]->mCapturerId == capnum) {
  875. delete mCallbacks[i];
  876. mCallbacks.RemoveElementAt(i);
  877. break;
  878. }
  879. }
  880. }
  881. }
  882. bool
  883. CamerasParent::RecvStopCapture(const CaptureEngine& aCapEngine,
  884. const int& capnum)
  885. {
  886. LOG((__PRETTY_FUNCTION__));
  887. RefPtr<CamerasParent> self(this);
  888. RefPtr<Runnable> webrtc_runnable =
  889. media::NewRunnableFrom([self, aCapEngine, capnum]() -> nsresult {
  890. self->StopCapture(aCapEngine, capnum);
  891. return NS_OK;
  892. });
  893. nsresult rv = DispatchToVideoCaptureThread(webrtc_runnable);
  894. if (self->IsShuttingDown()) {
  895. return NS_SUCCEEDED(rv);
  896. } else {
  897. if (NS_SUCCEEDED(rv)) {
  898. return SendReplySuccess();
  899. } else {
  900. return SendReplyFailure();
  901. }
  902. }
  903. }
  904. void
  905. CamerasParent::StopIPC()
  906. {
  907. MOZ_ASSERT(!mDestroyed);
  908. // Release shared memory now, it's our last chance
  909. mShmemPool.Cleanup(this);
  910. // We don't want to receive callbacks or anything if we can't
  911. // forward them anymore anyway.
  912. mChildIsAlive = false;
  913. mDestroyed = true;
  914. }
  915. bool
  916. CamerasParent::RecvAllDone()
  917. {
  918. LOG((__PRETTY_FUNCTION__));
  919. // Don't try to send anything to the child now
  920. mChildIsAlive = false;
  921. return Send__delete__(this);
  922. }
  923. void
  924. CamerasParent::ActorDestroy(ActorDestroyReason aWhy)
  925. {
  926. // No more IPC from here
  927. LOG((__PRETTY_FUNCTION__));
  928. StopIPC();
  929. // Shut down WebRTC (if we're not in full shutdown, else this
  930. // will already have happened)
  931. StopVideoCapture();
  932. }
  933. CamerasParent::CamerasParent()
  934. : mShmemPool(CaptureEngine::MaxEngine),
  935. mThreadMonitor("CamerasParent::mThreadMonitor"),
  936. mVideoCaptureThread(nullptr),
  937. mChildIsAlive(true),
  938. mDestroyed(false),
  939. mWebRTCAlive(true)
  940. {
  941. LOG(("CamerasParent: %p", this));
  942. mPBackgroundThread = NS_GetCurrentThread();
  943. MOZ_ASSERT(mPBackgroundThread != nullptr, "GetCurrentThread failed");
  944. LOG(("Spinning up WebRTC Cameras Thread"));
  945. RefPtr<CamerasParent> self(this);
  946. RefPtr<Runnable> threadStart =
  947. media::NewRunnableFrom([self]() -> nsresult {
  948. // Register thread shutdown observer
  949. nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
  950. if (NS_WARN_IF(!obs)) {
  951. return NS_ERROR_FAILURE;
  952. }
  953. nsresult rv =
  954. obs->AddObserver(self, NS_XPCOM_WILL_SHUTDOWN_OBSERVER_ID, false);
  955. if (NS_WARN_IF(NS_FAILED(rv))) {
  956. return rv;
  957. }
  958. // Start the thread
  959. MonitorAutoLock lock(self->mThreadMonitor);
  960. self->mVideoCaptureThread = new base::Thread("VideoCapture");
  961. base::Thread::Options options;
  962. #if defined(_WIN32)
  963. options.message_loop_type = MessageLoop::TYPE_MOZILLA_NONMAINUITHREAD;
  964. #else
  965. options.message_loop_type = MessageLoop::TYPE_MOZILLA_NONMAINTHREAD;
  966. #endif
  967. if (!self->mVideoCaptureThread->StartWithOptions(options)) {
  968. MOZ_CRASH();
  969. }
  970. self->mThreadMonitor.NotifyAll();
  971. return NS_OK;
  972. });
  973. NS_DispatchToMainThread(threadStart);
  974. MOZ_COUNT_CTOR(CamerasParent);
  975. }
  976. CamerasParent::~CamerasParent()
  977. {
  978. LOG(("~CamerasParent: %p", this));
  979. MOZ_COUNT_DTOR(CamerasParent);
  980. #ifdef DEBUG
  981. // Verify we have shut down the webrtc engines, this is
  982. // supposed to happen in ActorDestroy.
  983. // That runnable takes a ref to us, so it must have finished
  984. // by the time we get here.
  985. for (int i = 0; i < CaptureEngine::MaxEngine; i++) {
  986. MOZ_ASSERT(!mEngines[i].mEngine);
  987. }
  988. #endif
  989. }
  990. already_AddRefed<CamerasParent>
  991. CamerasParent::Create() {
  992. mozilla::ipc::AssertIsOnBackgroundThread();
  993. RefPtr<CamerasParent> camerasParent = new CamerasParent();
  994. return camerasParent.forget();
  995. }
  996. }
  997. }