summaryrefslogtreecommitdiff
path: root/v4l2_vdin.h (plain)
blob: e299d8ed3dc1f51ad4b3385a16f442928ef73437
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 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(void* 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 private:
101 int init_native_window();
102 int start_v4l2_device();
103 int workThread();
104 private:
105 class WorkThread : public Thread {
106 vdin_screen_source* mSource;
107 public:
108 WorkThread(vdin_screen_source* source) :
109 Thread(false), mSource(source) { }
110 virtual void onFirstRef() {
111 run("vdin screen source work thread", PRIORITY_URGENT_DISPLAY);
112 }
113 virtual bool threadLoop() {
114 mSource->workThread();
115 // loop until we need to quit
116 return true;
117 }
118 };
119 private:
120 int mCurrentIndex;
121 KeyedVector<void *, int> mBufs;
122 int mBufferCount;
123 int mFrameWidth;
124 int mFrameHeight;
125 int mBufferSize;
126 volatile int mState;
127 olStateCB mSetStateCB;
128 int mPixelFormat;
129 int mNativeWindowPixelFormat;
130 sp<ANativeWindow> mANativeWindow;
131 sp<WorkThread> mWorkThread;
132 mutable Mutex mLock;
133 int mCameraHandle;
134 struct VideoInfo *mVideoInfo;
135 int mFrameType;
136 app_data_callback mDataCB;
137 bool mOpen;
138 void *mUser;
139};
140
141}
142