summaryrefslogtreecommitdiffstats
path: root/dom/media/systemservices/CamerasParent.h
blob: 958d57237dbdb746101ada25811747726c387ba9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mozilla_CamerasParent_h
#define mozilla_CamerasParent_h

#include "nsIObserver.h"
#include "mozilla/dom/ContentParent.h"
#include "mozilla/camera/PCamerasParent.h"
#include "mozilla/ipc/Shmem.h"
#include "mozilla/ShmemPool.h"
#include "mozilla/Atomics.h"

// conflicts with #include of scoped_ptr.h
#undef FF
#include "webrtc/common.h"
// Video Engine
#include "webrtc/video_engine/include/vie_base.h"
#include "webrtc/video_engine/include/vie_capture.h"
#include "webrtc/video_engine/include/vie_render.h"
#include "CamerasChild.h"

#include "base/thread.h"

namespace mozilla {
namespace camera {

class CamerasParent;

class CallbackHelper : public webrtc::ExternalRenderer
{
public:
  CallbackHelper(CaptureEngine aCapEng, int aCapId, CamerasParent *aParent)
    : mCapEngine(aCapEng), mCapturerId(aCapId), mParent(aParent) {};

  // ViEExternalRenderer implementation. These callbacks end up
  // running on the VideoCapture thread.
  virtual int FrameSizeChange(unsigned int w, unsigned int h,
                              unsigned int streams) override;
  virtual int DeliverFrame(unsigned char* buffer,
                           size_t size,
                           uint32_t time_stamp,
                           int64_t ntp_time,
                           int64_t render_time,
                           void *handle) override;
  virtual int DeliverI420Frame(const webrtc::I420VideoFrame& webrtc_frame) override;
  virtual bool IsTextureSupported() override { return false; };

  friend CamerasParent;

private:
  CaptureEngine mCapEngine;
  int mCapturerId;
  CamerasParent *mParent;
};

class EngineHelper
{
public:
  EngineHelper() :
    mEngine(nullptr), mPtrViEBase(nullptr), mPtrViECapture(nullptr),
    mPtrViERender(nullptr), mEngineIsRunning(false) {};

  webrtc::VideoEngine *mEngine;
  webrtc::ViEBase *mPtrViEBase;
  webrtc::ViECapture *mPtrViECapture;
  webrtc::ViERender *mPtrViERender;

  // The webrtc code keeps a reference to this one.
  webrtc::Config mConfig;

  // Engine alive
  bool mEngineIsRunning;
};

class InputObserver :  public webrtc::ViEInputObserver
{
public:
  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(InputObserver)

  explicit InputObserver(CamerasParent* aParent)
    : mParent(aParent) {};
  virtual void DeviceChange();

  friend CamerasParent;

private:
  ~InputObserver() {}

  RefPtr<CamerasParent> mParent;
};

class CamerasParent :  public PCamerasParent,
                       public nsIObserver
{
  NS_DECL_THREADSAFE_ISUPPORTS
  NS_DECL_NSIOBSERVER

public:
  static already_AddRefed<CamerasParent> Create();

  // Messages received form the child. These run on the IPC/PBackground thread.
  virtual bool RecvAllocateCaptureDevice(const CaptureEngine&, const nsCString&,
                                         const nsCString&) override;
  virtual bool RecvReleaseCaptureDevice(const CaptureEngine&,
                                        const int&) override;
  virtual bool RecvNumberOfCaptureDevices(const CaptureEngine&) override;
  virtual bool RecvNumberOfCapabilities(const CaptureEngine&,
                                        const nsCString&) override;
  virtual bool RecvGetCaptureCapability(const CaptureEngine&, const nsCString&,
                                        const int&) override;
  virtual bool RecvGetCaptureDevice(const CaptureEngine&, const int&) override;
  virtual bool RecvStartCapture(const CaptureEngine&, const int&,
                                const CaptureCapability&) override;
  virtual bool RecvStopCapture(const CaptureEngine&, const int&) override;
  virtual bool RecvReleaseFrame(mozilla::ipc::Shmem&&) override;
  virtual bool RecvAllDone() override;
  virtual void ActorDestroy(ActorDestroyReason aWhy) override;
  virtual bool RecvEnsureInitialized(const CaptureEngine&) override;

  nsIThread* GetBackgroundThread() { return mPBackgroundThread; };
  bool IsShuttingDown() { return !mChildIsAlive
                              ||  mDestroyed
                              || !mWebRTCAlive; };
  ShmemBuffer GetBuffer(size_t aSize);

  // helper to forward to the PBackground thread
  int DeliverFrameOverIPC(CaptureEngine capEng,
                          int cap_id,
                          ShmemBuffer buffer,
                          unsigned char* altbuffer,
                          size_t size,
                          uint32_t time_stamp,
                          int64_t ntp_time,
                          int64_t render_time);


  CamerasParent();

protected:
  virtual ~CamerasParent();

  // We use these helpers for shutdown and for the respective IPC commands.
  void StopCapture(const CaptureEngine& aCapEngine, const int& capnum);
  int ReleaseCaptureDevice(const CaptureEngine& aCapEngine, const int& capnum);

  bool SetupEngine(CaptureEngine aCapEngine);
  bool EnsureInitialized(int aEngine);
  void CloseEngines();
  void StopIPC();
  void StopVideoCapture();
  // Can't take already_AddRefed because it can fail in stupid ways.
  nsresult DispatchToVideoCaptureThread(Runnable* event);

  EngineHelper mEngines[CaptureEngine::MaxEngine];
  nsTArray<CallbackHelper*> mCallbacks;

  // image buffers
  mozilla::ShmemPool mShmemPool;

  // PBackground parent thread
  nsCOMPtr<nsIThread> mPBackgroundThread;

  // Monitors creation of the thread below
  Monitor mThreadMonitor;

  // video processing thread - where webrtc.org capturer code runs
  base::Thread* mVideoCaptureThread;

  // Shutdown handling
  bool mChildIsAlive;
  bool mDestroyed;
  // Above 2 are PBackground only, but this is potentially
  // read cross-thread.
  mozilla::Atomic<bool> mWebRTCAlive;
  nsTArray<RefPtr<InputObserver>> mObservers;
};

PCamerasParent* CreateCamerasParent();

} // namespace camera
} // namespace mozilla

#endif  // mozilla_CameraParent_h