1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
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
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef IMAGECAPTURE_H
8 #define IMAGECAPTURE_H
9 
10 #include "mozilla/DOMEventTargetHelper.h"
11 #include "mozilla/dom/ImageCaptureBinding.h"
12 #include "mozilla/Logging.h"
13 
14 namespace mozilla {
15 
16 #ifndef IC_LOG
17 LogModule* GetICLog();
18 #  define IC_LOG(...) \
19     MOZ_LOG(GetICLog(), mozilla::LogLevel::Debug, (__VA_ARGS__))
20 #endif
21 
22 namespace dom {
23 
24 class Blob;
25 class MediaStreamTrack;
26 class VideoStreamTrack;
27 
28 /**
29  * Implementation of https://dvcs.w3.org/hg/dap/raw-file/default/media-stream-
30  * capture/ImageCapture.html.
31  * The ImageCapture accepts a video MediaStreamTrack as input source. The image
32  * will be sent back as a JPG format via Blob event.
33  *
34  * All the functions in ImageCapture are run in main thread.
35  *
36  * There are two ways to capture image, MediaEngineSource and MediaTrackGraph.
37  * When the implementation of MediaEngineSource supports TakePhoto(),
38  * it uses the platform camera to grab image. Otherwise, it falls back
39  * to the MediaTrackGraph way.
40  */
41 
42 class ImageCapture final : public DOMEventTargetHelper {
43  public:
44   NS_DECL_ISUPPORTS_INHERITED
45   NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(ImageCapture, DOMEventTargetHelper)
46 
47   IMPL_EVENT_HANDLER(photo)
48   IMPL_EVENT_HANDLER(error)
49 
50   // WebIDL members.
51   void TakePhoto(ErrorResult& aResult);
52 
53   // The MediaStreamTrack passed into the constructor.
54   MediaStreamTrack* GetVideoStreamTrack() const;
55 
56   // nsWrapperCache member
WrapObject(JSContext * aCx,JS::Handle<JSObject * > aGivenProto)57   JSObject* WrapObject(JSContext* aCx,
58                        JS::Handle<JSObject*> aGivenProto) override {
59     return ImageCapture_Binding::Wrap(aCx, this, aGivenProto);
60   }
61 
62   // ImageCapture class members
GetParentObject()63   nsPIDOMWindowInner* GetParentObject() { return GetOwner(); }
64 
65   static already_AddRefed<ImageCapture> Constructor(const GlobalObject& aGlobal,
66                                                     MediaStreamTrack& aTrack,
67                                                     ErrorResult& aRv);
68 
69   ImageCapture(VideoStreamTrack* aTrack, nsPIDOMWindowInner* aOwnerWindow);
70 
71   // Post a Blob event to script.
72   nsresult PostBlobEvent(Blob* aBlob);
73 
74   // Post an error event to script.
75   // aErrorCode should be one of error codes defined in ImageCaptureError.h.
76   // aReason is the nsresult which maps to a error string in
77   // dom/base/domerr.msg.
78   nsresult PostErrorEvent(uint16_t aErrorCode, nsresult aReason = NS_OK);
79 
80   bool CheckPrincipal();
81 
82  protected:
83   virtual ~ImageCapture();
84 
85   // Capture image by MediaEngine. If it's not support taking photo, this
86   // function should return NS_ERROR_NOT_IMPLEMENTED.
87   nsresult TakePhotoByMediaEngine();
88 
89   RefPtr<VideoStreamTrack> mTrack;
90 };
91 
92 }  // namespace dom
93 }  // namespace mozilla
94 
95 #endif  // IMAGECAPTURE_H
96