stream pushing ok without access permission of /dev/video0
[rtmpclient.git] / app / src / main / jni / UVCCamera.cpp
1 //
2 // Created by Peng Li on 14/5/2018.
3 //
4
5 #include "UVCCamera.h"
6 //#include <jni.h>
7 #include "log.h"
8 #include "libuvc/libuvc.h"
9
10
11
12 UVCCamera::~UVCCamera()
13 {
14 }
15
16 //jint UVCCamera::nativeOnLoad(JavaVM *jvm, void* reserved)
17 //{    
18     // N.B. within the context of a JVM thread here
19     // get a JNIEnv by attempting to get it directly
20 //    JNIEnv *env;
21 //    if (jvm) jvm->GetEnv (reinterpret_cast<void**>(&env), JNI_VERSION_1_2);
22
23     // get a handle to the Java class in the JVM 
24     // and cache it becuse FindClass doesn't work in native worker threads because Java is stupid
25     // http://developer.android.com/training/articles/perf-jni.html#faq_FindClass
26 //    jclass local_ref = 0;
27 //    if (env) local_ref = env->FindClass ("com/cisco/ecc/testapp/EccTestApp");
28 //    jclass global_ref = reinterpret_cast<jclass> (env->NewGlobalRef (local_ref));
29
30     // Create the singleton
31 //    singleton.reset (new EccTestApp (jvm, global_ref));
32
33     // return the JNI version needed. I think we can basicaly return anything other than 1.1 here
34     // unless it turns out we need some specific JNI methods from a later version
35 //    return JNI_VERSION_1_2;
36  //   return 0;
37 //}
38
39 void UVCCamera::init()
40 {
41     LOGE("UVCCamera::init");
42 }
43
44 int UVCCamera::open(int vid, int pid, int fd, int busnum, int devaddr, const char* usbFsName)
45 {
46     LOGE("UVCCamera::open");
47
48     uvc_context_t *ctx;
49     uvc_device_t *dev;
50     uvc_device_handle_t *devh;
51     uvc_error_t res;
52
53     char *mUsbFs = strdup(usbFsName);
54
55         fd = dup(fd);
56     res = uvc_init2(&ctx, NULL, mUsbFs);
57         if(res < 0){
58 //    if((res = uvc_init(&ctx, NULL)) < 0){
59         uvc_perror(res, "unc_init");
60         return res;
61     }
62     LOGE("UVC Initialized");
63
64 //    if ((res = uvc_find_device(ctx, &dev, 0, 0, NULL)) < 0){
65 //        uvc_perror(res, "uvc_find_device");
66 //        return res;
67 //    }
68 //    LOGE("Device Found");
69 //
70
71         res = uvc_get_device_with_fd(ctx, &dev, vid, pid, NULL, fd, busnum, devaddr);
72     if (res < 0){
73         LOGE("uvc_get_device_with_fd: %d", __LINE__);
74         uvc_perror(res, uvc_strerror(res));
75         LOGE("uvc_get_device_with_fd error: %s(%d)", uvc_strerror(res), res);
76         return res;
77     }
78     LOGE("uvc_get_device_with_fd");
79
80         //uvc_print_diag(mDeviceHandle, stderr);
81     
82     if ((res = uvc_open(dev, &devh)) < 0){
83         uvc_perror(res, uvc_strerror(res));
84         LOGE("uvc_open error: %s(%d)", uvc_strerror(res), res);
85         return res;
86     }
87     LOGE("Device Opened");
88 //    uvc_print_diag(devh, stderr);
89
90     uvc_exit(ctx);
91     return 1;
92 }