summaryrefslogtreecommitdiff
path: root/v4l2_vdin.h (plain)
blob: 0db756ea3212b07fabb051207e3fc3a3b956c3b5
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
39namespace android {
40
41#define NB_BUFFER 6
42
43struct VideoInfo {
44 struct v4l2_capability cap;
45 struct v4l2_format format;
46 struct v4l2_buffer buf;
47 struct v4l2_requestbuffers rb;
48 void *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};
57enum State{
58 START,
59 PAUSE,
60 STOPING,
61 STOP,
62};
63
64enum FrameType{
65 NATIVE_WINDOW_DATA = 0x1,
66 CALL_BACK_DATA = 0x2,
67};
68
69typedef void (*olStateCB)(int state);
70
71typedef 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
78class 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 aquire_buffer(aml_screen_buffer_info_t *buff_info);
91 // int inc_buffer_refcount(int* ptr);
92 int release_buffer(void* ptr);
93 int set_state_callback(olStateCB callback);
94 int set_data_callback(app_data_callback callback, void* user);
95 int set_preview_window(ANativeWindow* window);
96 int set_frame_rate(int frameRate);
97 int set_source_type(int sourceType);
98 int get_source_type();
99 private:
100 int init_native_window();
101 int start_v4l2_device();
102 int workThread();
103 private:
104 class WorkThread : public Thread {
105 vdin_screen_source* mSource;
106 public:
107 WorkThread(vdin_screen_source* source) :
108 Thread(false), mSource(source) { }
109 virtual void onFirstRef() {
110 run("vdin screen source work thread", PRIORITY_URGENT_DISPLAY);
111 }
112 virtual bool threadLoop() {
113 mSource->workThread();
114 // loop until we need to quit
115 return true;
116 }
117 };
118 private:
119 int mCurrentIndex;
120 KeyedVector<void *, int> mBufs;
121 int mBufferCount;
122 int mFrameWidth;
123 int mFrameHeight;
124 int mBufferSize;
125 volatile int mState;
126 olStateCB mSetStateCB;
127 int mPixelFormat;
128 int mNativeWindowPixelFormat;
129 sp<ANativeWindow> mANativeWindow;
130 sp<WorkThread> mWorkThread;
131 mutable Mutex mLock;
132 int mCameraHandle;
133 struct VideoInfo *mVideoInfo;
134 int mFrameType;
135 app_data_callback mDataCB;
136 bool mOpen;
137 void *mUser;
138};
139
140}
141