Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/media/systemservices/CamerasParent.h
Line
Count
Source (jump to first uncovered line)
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
class CallbackHelper :
41
  public rtc::VideoSinkInterface<webrtc::VideoFrame>
42
{
43
public:
44
  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
  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
  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(InputObserver)
63
64
  explicit InputObserver(CamerasParent* aParent)
65
0
    : mParent(aParent) {};
66
67
  virtual void OnDeviceChange() override;
68
69
  friend CamerasParent;
70
71
private:
72
0
  ~InputObserver() {}
73
74
  RefPtr<CamerasParent> mParent;
75
};
76
77
class CamerasParent final
78
  : public PCamerasParent
79
  , public nsIObserver
80
{
81
  NS_DECL_THREADSAFE_ISUPPORTS
82
  NS_DECL_NSIOBSERVER
83
84
public:
85
  static already_AddRefed<CamerasParent> Create();
86
87
  // Messages received form the child. These run on the IPC/PBackground thread.
88
  mozilla::ipc::IPCResult
89
  RecvAllocateCaptureDevice(const CaptureEngine& aEngine,
90
                            const nsCString& aUnique_idUTF8,
91
                            const ipc::PrincipalInfo& aPrincipalInfo) override;
92
  mozilla::ipc::IPCResult RecvReleaseCaptureDevice(const CaptureEngine&,
93
                                                   const int&) override;
94
  mozilla::ipc::IPCResult RecvNumberOfCaptureDevices(const CaptureEngine&) override;
95
  mozilla::ipc::IPCResult RecvNumberOfCapabilities(const CaptureEngine&,
96
                                                   const nsCString&) override;
97
  mozilla::ipc::IPCResult RecvGetCaptureCapability(const CaptureEngine&, const nsCString&,
98
                                                   const int&) override;
99
  mozilla::ipc::IPCResult RecvGetCaptureDevice(const CaptureEngine&, const int&) override;
100
  mozilla::ipc::IPCResult RecvStartCapture(const CaptureEngine&, const int&,
101
                                           const VideoCaptureCapability&) override;
102
  mozilla::ipc::IPCResult RecvFocusOnSelectedSource(const CaptureEngine&,
103
                                                    const int&) override;
104
  mozilla::ipc::IPCResult RecvStopCapture(const CaptureEngine&, const int&) override;
105
  mozilla::ipc::IPCResult RecvReleaseFrame(mozilla::ipc::Shmem&&) override;
106
  mozilla::ipc::IPCResult RecvAllDone() override;
107
  void ActorDestroy(ActorDestroyReason aWhy) override;
108
  mozilla::ipc::IPCResult RecvEnsureInitialized(const CaptureEngine&) override;
109
110
0
  nsIEventTarget* GetBackgroundEventTarget() { return mPBackgroundEventTarget; };
111
  bool IsShuttingDown()
112
0
  {
113
0
    return !mChildIsAlive || mDestroyed || !mWebRTCAlive;
114
0
  };
115
  ShmemBuffer GetBuffer(size_t aSize);
116
117
  // helper to forward to the PBackground thread
118
  int DeliverFrameOverIPC(CaptureEngine capEng,
119
                          uint32_t aStreamId,
120
                          ShmemBuffer buffer,
121
                          unsigned char* altbuffer,
122
                          VideoFrameProperties& aProps);
123
124
125
  CamerasParent();
126
127
protected:
128
  virtual ~CamerasParent();
129
130
  // We use these helpers for shutdown and for the respective IPC commands.
131
  void StopCapture(const CaptureEngine& aCapEngine, const int& capnum);
132
  int ReleaseCaptureDevice(const CaptureEngine& aCapEngine, const int& capnum);
133
134
  bool SetupEngine(CaptureEngine aCapEngine);
135
  VideoEngine* EnsureInitialized(int aEngine);
136
  void CloseEngines();
137
  void StopIPC();
138
  void StopVideoCapture();
139
  // Can't take already_AddRefed because it can fail in stupid ways.
140
  nsresult DispatchToVideoCaptureThread(Runnable* event);
141
142
  // sEngines will be accessed by VideoCapture thread only
143
  // sNumOfCamerasParent, sNumOfOpenCamerasParentEngines, and sVideoCaptureThread will
144
  // be accessed by main thread / PBackground thread / VideoCapture thread
145
  // sNumOfCamerasParent and sThreadMonitor create & delete are protected by sMutex
146
  // sNumOfOpenCamerasParentEngines and sVideoCaptureThread are protected by sThreadMonitor
147
  static StaticRefPtr<VideoEngine> sEngines[CaptureEngine::MaxEngine];
148
  static int32_t sNumOfOpenCamerasParentEngines;
149
  static int32_t sNumOfCamerasParents;
150
  nsTArray<CallbackHelper*> mCallbacks;
151
152
  // image buffers
153
  ShmemPool mShmemPool;
154
155
  // PBackground parent thread
156
  nsCOMPtr<nsISerialEventTarget> mPBackgroundEventTarget;
157
158
  static StaticMutex sMutex;
159
  static Monitor* sThreadMonitor;
160
161
  // video processing thread - where webrtc.org capturer code runs
162
  static base::Thread* sVideoCaptureThread;
163
164
  // Shutdown handling
165
  bool mChildIsAlive;
166
  bool mDestroyed;
167
  // Above 2 are PBackground only, but this is potentially
168
  // read cross-thread.
169
  Atomic<bool> mWebRTCAlive;
170
  RefPtr<InputObserver> mCameraObserver;
171
  std::map<nsCString, std::map<uint32_t, webrtc::VideoCaptureCapability>>
172
    mAllCandidateCapabilities;
173
};
174
175
PCamerasParent* CreateCamerasParent();
176
177
} // namespace camera
178
} // namespace mozilla
179
180
#endif  // mozilla_CameraParent_h