Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set sw=2 ts=8 et ft=cpp : */
3 : /* This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 : * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #ifndef mozilla_CamerasParent_h
8 : #define mozilla_CamerasParent_h
9 :
10 : #include "nsIObserver.h"
11 : #include "VideoEngine.h"
12 : #include "mozilla/dom/ContentParent.h"
13 : #include "mozilla/camera/PCamerasParent.h"
14 : #include "mozilla/ipc/Shmem.h"
15 : #include "mozilla/ShmemPool.h"
16 : #include "mozilla/Atomics.h"
17 : #include "webrtc/modules/video_capture/video_capture.h"
18 : #include "webrtc/modules/video_capture/video_capture_defines.h"
19 : #include "webrtc/common_video/include/incoming_video_stream.h"
20 : #include "webrtc/media/base/videosinkinterface.h"
21 :
22 : // conflicts with #include of scoped_ptr.h
23 : #undef FF
24 : #include "webrtc/common_types.h"
25 :
26 : #include "CamerasChild.h"
27 :
28 : #include "base/thread.h"
29 :
30 : namespace mozilla {
31 :
32 : namespace ipc {
33 : class PrincipalInfo;
34 : }
35 :
36 : namespace camera {
37 :
38 : class CamerasParent;
39 :
40 0 : class CallbackHelper :
41 : public rtc::VideoSinkInterface<webrtc::VideoFrame>
42 : {
43 : public:
44 0 : CallbackHelper(CaptureEngine aCapEng, uint32_t aStreamId, CamerasParent *aParent)
45 0 : : mCapEngine(aCapEng), mStreamId(aStreamId), mParent(aParent) {};
46 :
47 : // These callbacks end up running on the VideoCapture thread.
48 : // From VideoCaptureCallback
49 : virtual void OnFrame(const webrtc::VideoFrame& videoFrame) override;
50 :
51 : friend CamerasParent;
52 :
53 : private:
54 : CaptureEngine mCapEngine;
55 : uint32_t mStreamId;
56 : CamerasParent *mParent;
57 : };
58 :
59 : class InputObserver : public webrtc::VideoInputFeedBack
60 : {
61 : public:
62 0 : NS_INLINE_DECL_THREADSAFE_REFCOUNTING(InputObserver)
63 :
64 0 : explicit InputObserver(CamerasParent* aParent)
65 0 : : mParent(aParent) {};
66 : virtual void OnDeviceChange();
67 :
68 : friend CamerasParent;
69 :
70 : private:
71 0 : ~InputObserver() {}
72 :
73 : RefPtr<CamerasParent> mParent;
74 : };
75 :
76 : class CamerasParent : public PCamerasParent,
77 : public nsIObserver
78 : {
79 : NS_DECL_THREADSAFE_ISUPPORTS
80 : NS_DECL_NSIOBSERVER
81 :
82 : public:
83 : static already_AddRefed<CamerasParent> Create();
84 :
85 : // Messages received form the child. These run on the IPC/PBackground thread.
86 : virtual mozilla::ipc::IPCResult
87 : RecvAllocateCaptureDevice(const CaptureEngine& aEngine,
88 : const nsCString& aUnique_idUTF8,
89 : const ipc::PrincipalInfo& aPrincipalInfo) override;
90 : virtual mozilla::ipc::IPCResult RecvReleaseCaptureDevice(const CaptureEngine&,
91 : const int&) override;
92 : virtual mozilla::ipc::IPCResult RecvNumberOfCaptureDevices(const CaptureEngine&) override;
93 : virtual mozilla::ipc::IPCResult RecvNumberOfCapabilities(const CaptureEngine&,
94 : const nsCString&) override;
95 : virtual mozilla::ipc::IPCResult RecvGetCaptureCapability(const CaptureEngine&, const nsCString&,
96 : const int&) override;
97 : virtual mozilla::ipc::IPCResult RecvGetCaptureDevice(const CaptureEngine&, const int&) override;
98 : virtual mozilla::ipc::IPCResult RecvStartCapture(const CaptureEngine&, const int&,
99 : const VideoCaptureCapability&) override;
100 : virtual mozilla::ipc::IPCResult RecvStopCapture(const CaptureEngine&, const int&) override;
101 : virtual mozilla::ipc::IPCResult RecvReleaseFrame(mozilla::ipc::Shmem&&) override;
102 : virtual mozilla::ipc::IPCResult RecvAllDone() override;
103 : virtual void ActorDestroy(ActorDestroyReason aWhy) override;
104 : virtual mozilla::ipc::IPCResult RecvEnsureInitialized(const CaptureEngine&) override;
105 :
106 0 : nsIEventTarget* GetBackgroundEventTarget() { return mPBackgroundEventTarget; };
107 0 : bool IsShuttingDown() { return !mChildIsAlive
108 0 : || mDestroyed
109 0 : || !mWebRTCAlive; };
110 : ShmemBuffer GetBuffer(size_t aSize);
111 :
112 : // helper to forward to the PBackground thread
113 : int DeliverFrameOverIPC(CaptureEngine capEng,
114 : uint32_t aStreamId,
115 : ShmemBuffer buffer,
116 : unsigned char* altbuffer,
117 : VideoFrameProperties& aProps);
118 :
119 :
120 : CamerasParent();
121 :
122 : protected:
123 : virtual ~CamerasParent();
124 :
125 : // We use these helpers for shutdown and for the respective IPC commands.
126 : void StopCapture(const CaptureEngine& aCapEngine, const int& capnum);
127 : int ReleaseCaptureDevice(const CaptureEngine& aCapEngine, const int& capnum);
128 :
129 : bool SetupEngine(CaptureEngine aCapEngine);
130 : VideoEngine* EnsureInitialized(int aEngine);
131 : void CloseEngines();
132 : void StopIPC();
133 : void StopVideoCapture();
134 : // Can't take already_AddRefed because it can fail in stupid ways.
135 : nsresult DispatchToVideoCaptureThread(Runnable* event);
136 :
137 : RefPtr<VideoEngine> mEngines[CaptureEngine::MaxEngine];
138 : nsTArray<CallbackHelper*> mCallbacks;
139 :
140 : // image buffers
141 : mozilla::ShmemPool mShmemPool;
142 :
143 : // PBackground parent thread
144 : nsCOMPtr<nsISerialEventTarget> mPBackgroundEventTarget;
145 :
146 : // Monitors creation of the thread below
147 : Monitor mThreadMonitor;
148 :
149 : // video processing thread - where webrtc.org capturer code runs
150 : base::Thread* mVideoCaptureThread;
151 :
152 : // Shutdown handling
153 : bool mChildIsAlive;
154 : bool mDestroyed;
155 : // Above 2 are PBackground only, but this is potentially
156 : // read cross-thread.
157 : mozilla::Atomic<bool> mWebRTCAlive;
158 : nsTArray<RefPtr<InputObserver>> mObservers;
159 : };
160 :
161 : PCamerasParent* CreateCamerasParent();
162 :
163 : } // namespace camera
164 : } // namespace mozilla
165 :
166 : #endif // mozilla_CameraParent_h
|