|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 #include "CameraStreamImpl.h" |
|
6 #include "nsCRTGlue.h" |
|
7 #include "nsThreadUtils.h" |
|
8 #include "nsXULAppAPI.h" |
|
9 #include "mozilla/Monitor.h" |
|
10 |
|
11 /** |
|
12 * JNI part & helper runnable |
|
13 */ |
|
14 |
|
15 extern "C" { |
|
16 NS_EXPORT void JNICALL Java_org_mozilla_gecko_GeckoAppShell_cameraCallbackBridge(JNIEnv *, jclass, jbyteArray data); |
|
17 } |
|
18 |
|
19 NS_EXPORT void JNICALL |
|
20 Java_org_mozilla_gecko_GeckoAppShell_cameraCallbackBridge(JNIEnv *env, jclass, jbyteArray data) { |
|
21 mozilla::net::CameraStreamImpl* impl = mozilla::net::CameraStreamImpl::GetInstance(0); |
|
22 |
|
23 impl->transmitFrame(env, &data); |
|
24 } |
|
25 |
|
26 using namespace mozilla; |
|
27 |
|
28 namespace mozilla { |
|
29 namespace net { |
|
30 |
|
31 static CameraStreamImpl* mCamera0 = nullptr; |
|
32 static CameraStreamImpl* mCamera1 = nullptr; |
|
33 |
|
34 /** |
|
35 * CameraStreamImpl |
|
36 */ |
|
37 |
|
38 void CameraStreamImpl::transmitFrame(JNIEnv *env, jbyteArray *data) { |
|
39 if (!mCallback) |
|
40 return; |
|
41 jboolean isCopy; |
|
42 jbyte* jFrame = env->GetByteArrayElements(*data, &isCopy); |
|
43 uint32_t length = env->GetArrayLength(*data); |
|
44 if (length > 0) { |
|
45 mCallback->ReceiveFrame((char*)jFrame, length); |
|
46 } |
|
47 env->ReleaseByteArrayElements(*data, jFrame, 0); |
|
48 } |
|
49 |
|
50 CameraStreamImpl* CameraStreamImpl::GetInstance(uint32_t aCamera) { |
|
51 CameraStreamImpl* res = nullptr; |
|
52 switch(aCamera) { |
|
53 case 0: |
|
54 if (mCamera0) |
|
55 res = mCamera0; |
|
56 else |
|
57 res = mCamera0 = new CameraStreamImpl(aCamera); |
|
58 break; |
|
59 case 1: |
|
60 if (mCamera1) |
|
61 res = mCamera1; |
|
62 else |
|
63 res = mCamera1 = new CameraStreamImpl(aCamera); |
|
64 break; |
|
65 } |
|
66 return res; |
|
67 } |
|
68 |
|
69 |
|
70 CameraStreamImpl::CameraStreamImpl(uint32_t aCamera) : |
|
71 mInit(false), mCamera(aCamera) |
|
72 { |
|
73 NS_WARNING("CameraStreamImpl::CameraStreamImpl()"); |
|
74 mWidth = 0; |
|
75 mHeight = 0; |
|
76 mFps = 0; |
|
77 } |
|
78 |
|
79 CameraStreamImpl::~CameraStreamImpl() |
|
80 { |
|
81 NS_WARNING("CameraStreamImpl::~CameraStreamImpl()"); |
|
82 } |
|
83 |
|
84 bool CameraStreamImpl::Init(const nsCString& contentType, const uint32_t& camera, const uint32_t& width, const uint32_t& height, FrameCallback* aCallback) |
|
85 { |
|
86 mCallback = aCallback; |
|
87 mWidth = width; |
|
88 mHeight = height; |
|
89 return AndroidBridge::Bridge()->InitCamera(contentType, camera, &mWidth, &mHeight, &mFps); |
|
90 } |
|
91 |
|
92 void CameraStreamImpl::Close() { |
|
93 mozilla::widget::android::GeckoAppShell::CloseCamera(); |
|
94 mCallback = nullptr; |
|
95 } |
|
96 |
|
97 } // namespace net |
|
98 } // namespace mozilla |