blob: 61d9f3777a499d48a4c8e6480c9626dc4ad10b8e
1 | /* |
2 | * Copyright (C) 2013 The Android Open Source Project |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | */ |
16 | #include <signal.h> |
17 | #include <stdio.h> |
18 | #include <stdlib.h> |
19 | #include <string.h> |
20 | #include <fcntl.h> |
21 | #include <unistd.h> |
22 | #include <errno.h> |
23 | #include <sys/ioctl.h> |
24 | #include <sys/mman.h> |
25 | #include <sys/select.h> |
26 | #include <linux/videodev2.h> |
27 | #include <sys/time.h> |
28 | |
29 | #include <utils/KeyedVector.h> |
30 | #include <cutils/properties.h> |
31 | #include <sys/types.h> |
32 | #include <sys/stat.h> |
33 | #include <utils/threads.h> |
34 | #include <android/native_window.h> |
35 | #include <gralloc_priv.h> |
36 | |
37 | namespace android { |
38 | |
39 | #define NB_BUFFER 6 |
40 | |
41 | struct VideoInfo { |
42 | struct v4l2_capability cap; |
43 | struct v4l2_format format; |
44 | struct v4l2_buffer buf; |
45 | struct v4l2_requestbuffers rb; |
46 | void *mem[NB_BUFFER]; |
47 | unsigned canvas[NB_BUFFER]; |
48 | unsigned refcount[NB_BUFFER]; |
49 | bool isStreaming; |
50 | int width; |
51 | int height; |
52 | int formatIn; |
53 | int framesizeIn; |
54 | }; |
55 | enum State{ |
56 | START, |
57 | PAUSE, |
58 | STOPING, |
59 | STOP, |
60 | }; |
61 | |
62 | enum FrameType{ |
63 | NATIVE_WINDOW_DATA = 0x1, |
64 | CALL_BACK_DATA = 0x2, |
65 | }; |
66 | |
67 | typedef void (*olStateCB)(int state); |
68 | |
69 | typedef void (*app_data_callback)(void *user,int *buffer); |
70 | |
71 | #define SCREENSOURCE_GRALLOC_USAGE GRALLOC_USAGE_HW_TEXTURE | \ |
72 | GRALLOC_USAGE_HW_RENDER | \ |
73 | GRALLOC_USAGE_SW_READ_RARELY | \ |
74 | GRALLOC_USAGE_SW_WRITE_NEVER |
75 | |
76 | class vdin_screen_source { |
77 | public: |
78 | vdin_screen_source(); |
79 | ~vdin_screen_source(); |
80 | int init(); |
81 | int start(); |
82 | int stop(); |
83 | int pause(); |
84 | int get_format(); |
85 | int set_format(int width = 640, int height = 480, int color_format = V4L2_PIX_FMT_NV21); |
86 | int set_rotation(int degree); |
87 | int set_crop(int x, int y, int width, int height); |
88 | int aquire_buffer(int* buff_info); |
89 | // int inc_buffer_refcount(int* ptr); |
90 | int release_buffer(int* ptr); |
91 | int set_state_callback(olStateCB callback); |
92 | int set_data_callback(app_data_callback callback, void* user); |
93 | int set_preview_window(ANativeWindow* window); |
94 | int set_frame_rate(int frameRate); |
95 | int set_source_type(int sourceType); |
96 | int get_source_type(); |
97 | private: |
98 | int init_native_window(); |
99 | int start_v4l2_device(); |
100 | int workThread(); |
101 | private: |
102 | class WorkThread : public Thread { |
103 | vdin_screen_source* mSource; |
104 | public: |
105 | WorkThread(vdin_screen_source* source) : |
106 | Thread(false), mSource(source) { } |
107 | virtual void onFirstRef() { |
108 | run("vdin screen source work thread", PRIORITY_URGENT_DISPLAY); |
109 | } |
110 | virtual bool threadLoop() { |
111 | mSource->workThread(); |
112 | // loop until we need to quit |
113 | return true; |
114 | } |
115 | }; |
116 | private: |
117 | int mCurrentIndex; |
118 | KeyedVector<int, int> mBufs; |
119 | int mBufferCount; |
120 | int mFrameWidth; |
121 | int mFrameHeight; |
122 | int mBufferSize; |
123 | volatile int mState; |
124 | olStateCB mSetStateCB; |
125 | int mPixelFormat; |
126 | int mNativeWindowPixelFormat; |
127 | sp<ANativeWindow> mANativeWindow; |
128 | sp<WorkThread> mWorkThread; |
129 | mutable Mutex mLock; |
130 | int mCameraHandle; |
131 | struct VideoInfo *mVideoInfo; |
132 | int mFrameType; |
133 | app_data_callback mDataCB; |
134 | bool mOpen; |
135 | void *mUser; |
136 | }; |
137 | |
138 | } |
139 |