summaryrefslogtreecommitdiffstats
path: root/netwerk/protocol/device/CameraStreamImpl.cpp
blob: f4a2cf4a4fda795824d52fe7126bccf81d6b5907 (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
/* 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/. */

#include "CameraStreamImpl.h"
#include "GeneratedJNINatives.h"
#include "nsCRTGlue.h"
#include "nsThreadUtils.h"
#include "nsXULAppAPI.h"
#include "mozilla/Monitor.h"

using namespace mozilla;

namespace mozilla {
namespace net {

static CameraStreamImpl* mCamera0 = nullptr;
static CameraStreamImpl* mCamera1 = nullptr;

class CameraStreamImpl::Callback
    : public java::GeckoAppShell::CameraCallback::Natives<Callback>
{
public:
    static void OnFrameData(int32_t aCamera, jni::ByteArray::Param aData)
    {
        MOZ_ASSERT(NS_IsMainThread());

        CameraStreamImpl* impl = GetInstance(uint32_t(aCamera));
        if (impl) {
            impl->TransmitFrame(aData);
        }
    }
};

/**
 * CameraStreamImpl
 */

void CameraStreamImpl::TransmitFrame(jni::ByteArray::Param aData) {
    if (!mCallback) {
        return;
    }

    JNIEnv* const env = jni::GetGeckoThreadEnv();
    const size_t length = size_t(env->GetArrayLength(aData.Get()));

    if (!length) {
        return;
    }

    jbyte* const data = env->GetByteArrayElements(aData.Get(), nullptr);
    mCallback->ReceiveFrame(reinterpret_cast<char*>(data), length);
    env->ReleaseByteArrayElements(aData.Get(), data, JNI_ABORT);
}

CameraStreamImpl* CameraStreamImpl::GetInstance(uint32_t aCamera) {
    CameraStreamImpl* res = nullptr;
    switch(aCamera) {
        case 0:
            if (mCamera0)
                res = mCamera0;
            else
                res = mCamera0 = new CameraStreamImpl(aCamera);
            break;
        case 1:
            if (mCamera1)
                res = mCamera1;
            else
                res = mCamera1 = new CameraStreamImpl(aCamera);
            break;
    }
    return res;
}


CameraStreamImpl::CameraStreamImpl(uint32_t aCamera) :
 mInit(false), mCamera(aCamera)
{
    NS_WARNING("CameraStreamImpl::CameraStreamImpl()");
    mWidth = 0;
    mHeight = 0;
    mFps = 0;
}

CameraStreamImpl::~CameraStreamImpl()
{
    NS_WARNING("CameraStreamImpl::~CameraStreamImpl()");
}

bool CameraStreamImpl::Init(const nsCString& contentType, const uint32_t& camera, const uint32_t& width, const uint32_t& height, FrameCallback* aCallback)
{
    mCallback = aCallback;
    mWidth = width;
    mHeight = height;

    Callback::Init();
    jni::IntArray::LocalRef retArray = java::GeckoAppShell::InitCamera(
            contentType, int32_t(camera), int32_t(width), int32_t(height));
    nsTArray<int32_t> ret = retArray->GetElements();

    mWidth = uint32_t(ret[1]);
    mHeight = uint32_t(ret[2]);
    mFps = uint32_t(ret[3]);

    return !!ret[0];
}

void CameraStreamImpl::Close() {
    java::GeckoAppShell::CloseCamera();
    mCallback = nullptr;
}

} // namespace net
} // namespace mozilla