stream pushing ok without access permission of /dev/video0
[rtmpclient.git] / app / src / main / jni / libuvc / src / test.c
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 *  Copyright (C) 2010-2012 Ken Tossell
5 *  All rights reserved.
6 *
7 *  Redistribution and use in source and binary forms, with or without
8 *  modification, are permitted provided that the following conditions
9 *  are met:
10 *
11 *   * Redistributions of source code must retain the above copyright
12 *     notice, this list of conditions and the following disclaimer.
13 *   * Redistributions in binary form must reproduce the above
14 *     copyright notice, this list of conditions and the following
15 *     disclaimer in the documentation and/or other materials provided
16 *     with the distribution.
17 *   * Neither the name of the author nor other contributors may be
18 *     used to endorse or promote products derived from this software
19 *     without specific prior written permission.
20 *
21 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 *  POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34 #include <stdio.h>
35 #include <opencv/highgui.h>
36
37 #include "libuvc/libuvc.h"
38
39 void cb(uvc_frame_t *frame, void *ptr) {
40   uvc_frame_t *bgr;
41   uvc_error_t ret;
42   IplImage* cvImg;
43
44   printf("callback! length = %u, ptr = %d\n", frame->data_bytes, (int) ptr);
45
46   bgr = uvc_allocate_frame(frame->width * frame->height * 3);
47   if (!bgr) {
48     printf("unable to allocate bgr frame!");
49     return;
50   }
51
52   ret = uvc_any2bgr(frame, bgr);
53   if (ret) {
54     uvc_perror(ret, "uvc_any2bgr");
55     uvc_free_frame(bgr);
56     return;
57   }
58
59   cvImg = cvCreateImageHeader(
60       cvSize(bgr->width, bgr->height),
61       IPL_DEPTH_8U,
62       3);
63
64   cvSetData(cvImg, bgr->data, bgr->width * 3); 
65
66   cvNamedWindow("Test", CV_WINDOW_AUTOSIZE);
67   cvShowImage("Test", cvImg);
68   cvWaitKey(10);
69
70   cvReleaseImageHeader(&cvImg);
71
72   uvc_free_frame(bgr);
73 }
74
75 int main(int argc, char **argv) {
76   uvc_context_t *ctx;
77   uvc_error_t res;
78   uvc_device_t *dev;
79   uvc_device_handle_t *devh;
80   uvc_stream_ctrl_t ctrl;
81
82   res = uvc_init(&ctx, NULL);
83
84   if (res < 0) {
85     uvc_perror(res, "uvc_init");
86     return res;
87   }
88
89   puts("UVC initialized");
90
91   res = uvc_find_device(
92       ctx, &dev,
93       0, 0, NULL);
94
95   if (res < 0) {
96     uvc_perror(res, "uvc_find_device");
97   } else {
98     puts("Device found");
99
100     res = uvc_open(dev, &devh);
101
102     if (res < 0) {
103       uvc_perror(res, "uvc_open");
104     } else {
105       puts("Device opened");
106
107       uvc_print_diag(devh, stderr);
108
109       res = uvc_get_stream_ctrl_format_size(
110           devh, &ctrl, UVC_FRAME_FORMAT_YUYV, 640, 480, 30
111       );
112
113       uvc_print_stream_ctrl(&ctrl, stderr);
114
115       if (res < 0) {
116         uvc_perror(res, "get_mode");
117       } else {
118         res = uvc_start_iso_streaming(devh, &ctrl, cb, 12345);
119
120         if (res < 0) {
121           uvc_perror(res, "start_streaming");
122         } else {
123           puts("Streaming for 10 seconds...");
124           uvc_error_t resAEMODE = uvc_set_ae_mode(devh, 1);
125           uvc_perror(resAEMODE, "set_ae_mode");
126           int i;
127           for (i = 1; i <= 10; i++) {
128             /* uvc_error_t resPT = uvc_set_pantilt_abs(devh, i * 20 * 3600, 0); */
129             /* uvc_perror(resPT, "set_pt_abs"); */
130             uvc_error_t resEXP = uvc_set_exposure_abs(devh, 20 + i * 5);
131             uvc_perror(resEXP, "set_exp_abs");
132             
133             sleep(1);
134           }
135           sleep(10);
136           uvc_stop_streaming(devh);
137           puts("Done streaming.");
138         }
139       }
140
141       uvc_close(devh);
142       puts("Device closed");
143     }
144
145     uvc_unref_device(dev);
146   }
147
148   uvc_exit(ctx);
149   puts("UVC exited");
150
151   return 0;
152 }
153