blob: 0277884e04fdd7c8f048e96d19c7779ec7f74a4f
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 | #include <hardware/aml_screen.h> |
38 | |
39 | namespace android { |
40 | |
41 | #define NB_BUFFER 6 |
42 | |
43 | struct VideoInfo { |
44 | struct v4l2_capability cap; |
45 | struct v4l2_format format; |
46 | struct v4l2_buffer buf; |
47 | struct v4l2_requestbuffers rb; |
48 | long *mem[NB_BUFFER]; |
49 | unsigned canvas[NB_BUFFER]; |
50 | unsigned refcount[NB_BUFFER]; |
51 | bool isStreaming; |
52 | int width; |
53 | int height; |
54 | int formatIn; |
55 | int framesizeIn; |
56 | }; |
57 | enum State { |
58 | START, |
59 | PAUSE, |
60 | STOPING, |
61 | STOP, |
62 | }; |
63 | |
64 | enum FrameType { |
65 | NATIVE_WINDOW_DATA = 0x1, |
66 | CALL_BACK_DATA = 0x2, |
67 | }; |
68 | |
69 | typedef void (*olStateCB)(int state); |
70 | |
71 | typedef void (*app_data_callback)(void *user, aml_screen_buffer_info_t *buff_info); |
72 | |
73 | #define SCREENSOURCE_GRALLOC_USAGE GRALLOC_USAGE_HW_TEXTURE | \ |
74 | GRALLOC_USAGE_HW_RENDER | \ |
75 | GRALLOC_USAGE_SW_READ_RARELY | \ |
76 | GRALLOC_USAGE_SW_WRITE_NEVER |
77 | |
78 | class vdin_screen_source { |
79 | public: |
80 | vdin_screen_source(); |
81 | ~vdin_screen_source(); |
82 | int init(); |
83 | int start(); |
84 | int stop(); |
85 | int pause(); |
86 | int get_format(); |
87 | int set_format(int width = 640, int height = 480, int color_format = V4L2_PIX_FMT_NV21); |
88 | int set_rotation(int degree); |
89 | int set_crop(int x, int y, int width, int height); |
90 | int set_amlvideo2_crop(int x, int y, int width, int height); |
91 | int aquire_buffer(aml_screen_buffer_info_t *buff_info); |
92 | // int inc_buffer_refcount(int* ptr); |
93 | int release_buffer(long *ptr); |
94 | int set_state_callback(olStateCB callback); |
95 | int set_data_callback(app_data_callback callback, void *user); |
96 | int set_preview_window(ANativeWindow *window); |
97 | int set_frame_rate(int frameRate); |
98 | int set_source_type(int sourceType); |
99 | int get_source_type(); |
100 | int start_v4l2_device(); |
101 | int stop_v4l2_device(); |
102 | private: |
103 | int init_native_window(); |
104 | int workThread(); |
105 | private: |
106 | class WorkThread : public Thread { |
107 | vdin_screen_source *mSource; |
108 | public: |
109 | WorkThread(vdin_screen_source *source) : |
110 | Thread(false), mSource(source) { } |
111 | virtual void onFirstRef() |
112 | { |
113 | run("vdin screen source work thread", PRIORITY_URGENT_DISPLAY); |
114 | } |
115 | virtual bool threadLoop() |
116 | { |
117 | mSource->workThread(); |
118 | // loop until we need to quit |
119 | return true; |
120 | } |
121 | }; |
122 | private: |
123 | int mCurrentIndex; |
124 | KeyedVector<long *, long> mBufs; |
125 | int mBufferCount; |
126 | int mFrameWidth; |
127 | int mFrameHeight; |
128 | int mBufferSize; |
129 | volatile int mState; |
130 | olStateCB mSetStateCB; |
131 | int mPixelFormat; |
132 | int mNativeWindowPixelFormat; |
133 | sp<ANativeWindow> mANativeWindow; |
134 | sp<WorkThread> mWorkThread; |
135 | mutable Mutex mLock; |
136 | int mCameraHandle; |
137 | struct VideoInfo *mVideoInfo; |
138 | int mFrameType; |
139 | app_data_callback mDataCB; |
140 | bool mOpen; |
141 | void *mUser; |
142 | }; |
143 | |
144 | } |
145 |