blob: 2c4579a84bd80cfc5b3cebc06cee0eb4f71c6f17
1 | /* |
2 | * Copyright 2014 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 | |
17 | #define LOG_TAG "TvInput" |
18 | #include <fcntl.h> |
19 | #include <errno.h> |
20 | |
21 | #include <cutils/log.h> |
22 | #include <cutils/native_handle.h> |
23 | |
24 | #include <hardware/tv_input.h> |
25 | #include "TvPlay.h" |
26 | #include "tv_callback.h" |
27 | #include <tvcmd.h> |
28 | #include <ui/GraphicBufferMapper.h> |
29 | #include <ui/GraphicBuffer.h> |
30 | #include <gralloc_priv.h> |
31 | #include <gralloc_helper.h> |
32 | #include <hardware/hardware.h> |
33 | #include <hardware/aml_screen.h> |
34 | #include <linux/videodev2.h> |
35 | #include <android/native_window.h> |
36 | /*****************************************************************************/ |
37 | |
38 | #define LOGD(...) \ |
39 | { \ |
40 | __android_log_print(ANDROID_LOG_DEBUG, "tv_input", __VA_ARGS__); } |
41 | |
42 | #ifndef container_of |
43 | #define container_of(ptr, type, member) \ |
44 | (type *)((char*)(ptr) - offsetof(type, member)) |
45 | #endif |
46 | |
47 | struct sideband_handle_t { |
48 | native_handle_t nativeHandle; |
49 | int identflag; |
50 | int usage; |
51 | }; |
52 | |
53 | typedef struct tv_input_private { |
54 | tv_input_device_t device; |
55 | const tv_input_callback_ops_t *callback; |
56 | void *callback_data; |
57 | aml_screen_device_t *mDev; |
58 | TvPlay *mpTv; |
59 | TvCallback *tvcallback; |
60 | } tv_input_private_t; |
61 | |
62 | #define SCREENSOURCE_GRALLOC_USAGE ( GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_SW_READ_RARELY | GRALLOC_USAGE_SW_WRITE_NEVER) |
63 | //static int capBufferSize ; |
64 | static int capWidth; |
65 | static int capHeight; |
66 | |
67 | void TvIputHal_ChannelConl(tv_input_private_t *priv, int ops_type, int device_id) |
68 | { |
69 | if (priv->mpTv) { |
70 | if (ops_type) { |
71 | LOGD ( "%s, OpenSourceSwitchInput id = %d\n", __FUNCTION__, device_id ); |
72 | priv->mpTv->StartTv(); |
73 | priv->mpTv->SwitchSourceInput((tv_source_input_t) device_id); |
74 | } else if (priv->mpTv->GetCurrentSourceInput() == device_id) { |
75 | LOGD ( "%s, StopSourceSwitchInput id = %d\n", __FUNCTION__, device_id ); |
76 | priv->mpTv->StopTv(); |
77 | } |
78 | } |
79 | } |
80 | |
81 | static int notify_tv_device_status(tv_input_private_t *priv, tv_source_input_t source_input, int type) |
82 | { |
83 | tv_input_event_t event; |
84 | event.device_info.device_id = source_input; |
85 | event.device_info.audio_type = AUDIO_DEVICE_NONE; |
86 | event.device_info.audio_address = NULL; |
87 | event.type = type; |
88 | switch (source_input) { |
89 | case SOURCE_TV: |
90 | case SOURCE_DTV: |
91 | case SOURCE_ADTV: |
92 | event.device_info.type = TV_INPUT_TYPE_TUNER; |
93 | break; |
94 | case SOURCE_AV1: |
95 | case SOURCE_AV2: |
96 | event.device_info.type = TV_INPUT_TYPE_COMPOSITE; |
97 | break; |
98 | case SOURCE_HDMI1: |
99 | case SOURCE_HDMI2: |
100 | case SOURCE_HDMI3: |
101 | event.device_info.type = TV_INPUT_TYPE_HDMI; |
102 | event.device_info.hdmi.port_id = priv->mpTv->getHdmiPort(source_input); |
103 | break; |
104 | case SOURCE_SPDIF: |
105 | event.device_info.type = TV_INPUT_TYPE_OTHER_HARDWARE; |
106 | break; |
107 | default: |
108 | break; |
109 | } |
110 | priv->callback->notify(&priv->device, &event, priv->callback_data); |
111 | return 0; |
112 | } |
113 | |
114 | static int notify_TV_Input_Capture_Succeeded(tv_input_private_t *priv, int device_id, int stream_id, uint32_t seq) |
115 | { |
116 | tv_input_event_t event; |
117 | event.type = TV_INPUT_EVENT_CAPTURE_SUCCEEDED; |
118 | event.capture_result.device_id = device_id; |
119 | event.capture_result.stream_id = stream_id; |
120 | event.capture_result.seq = seq; |
121 | priv->callback->notify(&priv->device, &event, priv->callback_data); |
122 | return 0; |
123 | } |
124 | |
125 | static int notify_TV_Input_Capture_Fail(tv_input_private_t *priv, int device_id, int stream_id, uint32_t seq) |
126 | { |
127 | tv_input_event_t event; |
128 | event.type = TV_INPUT_EVENT_CAPTURE_FAILED; |
129 | event.capture_result.device_id = device_id; |
130 | event.capture_result.stream_id = stream_id; |
131 | event.capture_result.seq = seq; |
132 | priv->callback->notify(&priv->device, &event, priv->callback_data); |
133 | return 0; |
134 | } |
135 | void TvCallback::onTvEvent (int32_t msgType, const Parcel &p) |
136 | { |
137 | tv_input_private_t *priv = (tv_input_private_t *)(mPri); |
138 | switch (msgType) { |
139 | case SOURCE_CONNECT_CALLBACK: { |
140 | tv_source_input_t source = (tv_source_input_t)p.readInt32(); |
141 | int connectState = p.readInt32(); |
142 | LOGD("TvCallback::onTvEvent source = %d, status = %d", source, connectState); |
143 | |
144 | if (source != SOURCE_HDMI1 && source != SOURCE_HDMI2 && source != SOURCE_HDMI3 |
145 | && source != SOURCE_AV1 && source != SOURCE_AV2) |
146 | break; |
147 | |
148 | if (connectState == 1) { |
149 | notify_tv_device_status(priv, source, TV_INPUT_EVENT_DEVICE_AVAILABLE); |
150 | notify_tv_device_status(priv, source, TV_INPUT_EVENT_STREAM_CONFIGURATIONS_CHANGED); |
151 | } else { |
152 | notify_tv_device_status(priv, source, TV_INPUT_EVENT_DEVICE_UNAVAILABLE); |
153 | } |
154 | break; |
155 | } |
156 | default: |
157 | break; |
158 | } |
159 | } |
160 | |
161 | #define NORMAL_STREAM_ID 1 |
162 | #define FRAME_CAPTURE_STREAM_ID 2 |
163 | static tv_stream_config_t mconfig[2]; |
164 | static int get_stream_configs(int dev_id, int *num_configurations, const tv_stream_config_t **configs) |
165 | { |
166 | mconfig[0].stream_id = NORMAL_STREAM_ID; |
167 | mconfig[0].type = TV_STREAM_TYPE_INDEPENDENT_VIDEO_SOURCE ; |
168 | mconfig[0].max_video_width = 1920; |
169 | mconfig[0].max_video_height = 1080; |
170 | mconfig[1].stream_id = FRAME_CAPTURE_STREAM_ID; |
171 | mconfig[1].type = TV_STREAM_TYPE_BUFFER_PRODUCER ; |
172 | mconfig[1].max_video_width = 1920; |
173 | mconfig[1].max_video_height = 1080; |
174 | *num_configurations = 2; |
175 | *configs = mconfig; |
176 | return 0; |
177 | } |
178 | |
179 | static int get_tv_stream(tv_stream_t *stream) |
180 | { |
181 | static struct sideband_handle_t *tvstream = NULL; |
182 | if (stream->stream_id == NORMAL_STREAM_ID) { |
183 | if ( !tvstream ) { |
184 | tvstream = (struct sideband_handle_t *)native_handle_create(0, 2); |
185 | if ( !tvstream ) { |
186 | return -EINVAL; |
187 | } |
188 | } |
189 | tvstream->identflag = 0xabcdcdef; //magic word |
190 | tvstream->usage = GRALLOC_USAGE_AML_VIDEO_OVERLAY; |
191 | stream->type = TV_STREAM_TYPE_INDEPENDENT_VIDEO_SOURCE; |
192 | stream->sideband_stream_source_handle = (native_handle_t *)tvstream; |
193 | } else if (stream->stream_id == FRAME_CAPTURE_STREAM_ID) { |
194 | stream->type = TV_STREAM_TYPE_BUFFER_PRODUCER; |
195 | } |
196 | return 0; |
197 | } |
198 | |
199 | static void available_all_tv_device(tv_input_private_t *priv) |
200 | { |
201 | int tv_devices[15]; |
202 | int count = 0; |
203 | priv->mpTv->getAllTvDevices(tv_devices, &count); |
204 | |
205 | if (count == 0) { |
206 | ALOGE("tv.source.input.ids.default is not set."); |
207 | return; |
208 | } |
209 | |
210 | for (int i=0; i < count; i++) { |
211 | tv_source_input_t source_input = (tv_source_input_t)tv_devices[i]; |
212 | if (source_input == SOURCE_TV) { |
213 | notify_tv_device_status(priv, SOURCE_TV, TV_INPUT_EVENT_DEVICE_AVAILABLE); |
214 | notify_tv_device_status(priv, SOURCE_TV, TV_INPUT_EVENT_STREAM_CONFIGURATIONS_CHANGED); |
215 | } |
216 | if (source_input == SOURCE_DTV) { |
217 | notify_tv_device_status(priv, SOURCE_DTV, TV_INPUT_EVENT_DEVICE_AVAILABLE); |
218 | notify_tv_device_status(priv, SOURCE_DTV, TV_INPUT_EVENT_STREAM_CONFIGURATIONS_CHANGED); |
219 | } |
220 | if (source_input == SOURCE_ADTV) { |
221 | notify_tv_device_status(priv, SOURCE_ADTV, TV_INPUT_EVENT_DEVICE_AVAILABLE); |
222 | notify_tv_device_status(priv, SOURCE_ADTV, TV_INPUT_EVENT_STREAM_CONFIGURATIONS_CHANGED); |
223 | } |
224 | |
225 | int status = 1; |
226 | if (priv->mpTv->GetHdmiAvHotplugDetectOnoff()) { |
227 | status = priv->mpTv->GetSourceConnectStatus(source_input); |
228 | priv->mpTv->setTvObserver(priv->tvcallback); |
229 | } |
230 | if (status == 1) { |
231 | notify_tv_device_status(priv, source_input, TV_INPUT_EVENT_DEVICE_AVAILABLE); |
232 | notify_tv_device_status(priv, source_input, TV_INPUT_EVENT_STREAM_CONFIGURATIONS_CHANGED); |
233 | } |
234 | } |
235 | } |
236 | |
237 | static int tv_input_device_open(const struct hw_module_t *module, |
238 | const char *name, struct hw_device_t **device); |
239 | |
240 | static struct hw_module_methods_t tv_input_module_methods = { |
241 | open: |
242 | tv_input_device_open |
243 | }; |
244 | |
245 | tv_input_module_t HAL_MODULE_INFO_SYM = { |
246 | common: |
247 | { |
248 | tag: |
249 | HARDWARE_MODULE_TAG, |
250 | version_major: 0, |
251 | version_minor: 1, |
252 | id: |
253 | TV_INPUT_HARDWARE_MODULE_ID, |
254 | name: "TVInput module" |
255 | , |
256 | author: "Amlogic" |
257 | , |
258 | methods: |
259 | &tv_input_module_methods, |
260 | dso: NULL, |
261 | reserved: {0}, |
262 | } |
263 | }; |
264 | |
265 | /*****************************************************************************/ |
266 | static int tv_input_initialize(struct tv_input_device *dev, |
267 | const tv_input_callback_ops_t *callback, void *data) |
268 | { |
269 | if (dev == NULL || callback == NULL) { |
270 | return -EINVAL; |
271 | } |
272 | tv_input_private_t *priv = (tv_input_private_t *)dev; |
273 | if (priv->callback != NULL) { |
274 | return -EEXIST; |
275 | } |
276 | priv->callback = callback; |
277 | priv->callback_data = data; |
278 | |
279 | available_all_tv_device(priv); |
280 | return 0; |
281 | } |
282 | |
283 | static int tv_input_get_stream_configurations(const struct tv_input_device *dev __unused, |
284 | int device_id, int *num_configurations, |
285 | const tv_stream_config_t **configs) |
286 | { |
287 | if (get_stream_configs(device_id, num_configurations, configs) == 0) { |
288 | return 0; |
289 | } |
290 | return -EINVAL; |
291 | } |
292 | |
293 | static int tv_input_open_stream(struct tv_input_device *dev, int device_id, |
294 | tv_stream_t *stream) |
295 | { |
296 | tv_input_private_t *priv = (tv_input_private_t *)dev; |
297 | if (priv) { |
298 | if (get_tv_stream(stream) != 0) { |
299 | return -EINVAL; |
300 | } |
301 | if (stream->stream_id == NORMAL_STREAM_ID) { |
302 | TvIputHal_ChannelConl(priv, 1, device_id); |
303 | return 0; |
304 | } else if (stream->stream_id == FRAME_CAPTURE_STREAM_ID) { |
305 | aml_screen_module_t* mModule; |
306 | if (hw_get_module(AML_SCREEN_HARDWARE_MODULE_ID, |
307 | (const hw_module_t **)&mModule) < 0) { |
308 | ALOGE("can not get screen source module"); |
309 | } else { |
310 | mModule->common.methods->open((const hw_module_t *)mModule, |
311 | AML_SCREEN_SOURCE, (struct hw_device_t**)&(priv->mDev)); |
312 | //do test here, we can use ops of mDev to operate vdin source |
313 | } |
314 | |
315 | if (priv->mDev) { |
316 | if (capWidth == 0 || capHeight == 0) { |
317 | capWidth = stream->buffer_producer.width; |
318 | capHeight = stream->buffer_producer.height; |
319 | } |
320 | priv->mDev->ops.set_format(priv->mDev, capWidth, capHeight, V4L2_PIX_FMT_NV21); |
321 | priv->mDev->ops.set_port_type(priv->mDev, (int)0x4000); //TVIN_PORT_HDMI0 = 0x4000 |
322 | priv->mDev->ops.start_v4l2_device(priv->mDev); |
323 | } |
324 | return 0; |
325 | } |
326 | } |
327 | return -EINVAL; |
328 | } |
329 | |
330 | static int tv_input_close_stream(struct tv_input_device *dev, int device_id, |
331 | int stream_id) |
332 | { |
333 | tv_input_private_t *priv = (tv_input_private_t *)dev; |
334 | if (stream_id == NORMAL_STREAM_ID) { |
335 | TvIputHal_ChannelConl(priv, 0, device_id); |
336 | return 0; |
337 | } else if (stream_id == FRAME_CAPTURE_STREAM_ID) { |
338 | if (priv->mDev) { |
339 | priv->mDev->ops.stop_v4l2_device(priv->mDev); |
340 | } |
341 | return 0; |
342 | } |
343 | return -EINVAL; |
344 | } |
345 | |
346 | static int tv_input_request_capture( |
347 | struct tv_input_device *dev __unused, int device_id __unused, |
348 | int stream_id __unused, buffer_handle_t buffer __unused, uint32_t seq __unused) |
349 | { |
350 | tv_input_private_t *priv = (tv_input_private_t *)dev; |
351 | int index; |
352 | aml_screen_buffer_info_t buff_info; |
353 | int mFrameWidth , mFrameHeight ; |
354 | int ret; |
355 | long *src = NULL; |
356 | unsigned char *dest = NULL; |
357 | ANativeWindowBuffer *buf; |
358 | if (priv->mDev) { |
359 | ret = priv->mDev->ops.aquire_buffer(priv->mDev, &buff_info); |
360 | if (ret != 0 || (buff_info.buffer_mem == 0)) { |
361 | LOGD("Get V4l2 buffer failed"); |
362 | notify_TV_Input_Capture_Fail(priv,device_id,stream_id,--seq); |
363 | return -EWOULDBLOCK; |
364 | } |
365 | src = (long *)buff_info.buffer_mem; |
366 | buf = container_of(buffer, ANativeWindowBuffer, handle); |
367 | sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(buf, false)); |
368 | graphicBuffer->lock(SCREENSOURCE_GRALLOC_USAGE, (void **)&dest); |
369 | if (dest == NULL) { |
370 | LOGD("Invalid Gralloc Handle"); |
371 | return -EWOULDBLOCK; |
372 | } |
373 | memcpy(dest, src, capWidth*capHeight); |
374 | graphicBuffer->unlock(); |
375 | graphicBuffer.clear(); |
376 | priv->mDev->ops.release_buffer(priv->mDev, src); |
377 | |
378 | notify_TV_Input_Capture_Succeeded(priv,device_id,stream_id,seq); |
379 | return 0; |
380 | } |
381 | return -EWOULDBLOCK; |
382 | } |
383 | |
384 | static int tv_input_cancel_capture(struct tv_input_device *, int, int, uint32_t) |
385 | { |
386 | return -EINVAL; |
387 | } |
388 | |
389 | static int tv_input_set_capturesurface_size(struct tv_input_device *dev __unused, int width, int height) |
390 | { |
391 | if (width == 0 || height == 0) { |
392 | return -EINVAL; |
393 | } else { |
394 | capWidth = width; |
395 | capHeight = height; |
396 | return 1; |
397 | } |
398 | } |
399 | /*****************************************************************************/ |
400 | |
401 | static int tv_input_device_close(struct hw_device_t *dev) |
402 | { |
403 | tv_input_private_t *priv = (tv_input_private_t *)dev; |
404 | if (priv) { |
405 | if (priv->mpTv) { |
406 | delete priv->mpTv; |
407 | } |
408 | if (priv->mDev) { |
409 | delete priv->mDev; |
410 | } |
411 | free(priv); |
412 | } |
413 | return 0; |
414 | } |
415 | |
416 | /*****************************************************************************/ |
417 | |
418 | static int tv_input_device_open(const struct hw_module_t *module, |
419 | const char *name, struct hw_device_t **device) |
420 | { |
421 | int status = -EINVAL; |
422 | if (!strcmp(name, TV_INPUT_DEFAULT_DEVICE)) { |
423 | tv_input_private_t *dev = (tv_input_private_t *)malloc(sizeof(*dev)); |
424 | /* initialize our state here */ |
425 | memset(dev, 0, sizeof(*dev)); |
426 | dev->mpTv = new TvPlay(); |
427 | dev->tvcallback = new TvCallback(dev); |
428 | /* initialize the procs */ |
429 | dev->device.common.tag = HARDWARE_DEVICE_TAG; |
430 | dev->device.common.version = TV_INPUT_DEVICE_API_VERSION_0_1; |
431 | dev->device.common.module = const_cast<hw_module_t *>(module); |
432 | dev->device.common.close = tv_input_device_close; |
433 | |
434 | dev->device.initialize = tv_input_initialize; |
435 | dev->device.get_stream_configurations = |
436 | tv_input_get_stream_configurations; |
437 | dev->device.open_stream = tv_input_open_stream; |
438 | dev->device.close_stream = tv_input_close_stream; |
439 | dev->device.request_capture = tv_input_request_capture; |
440 | dev->device.cancel_capture = tv_input_cancel_capture; |
441 | dev->device.set_capturesurface_size = tv_input_set_capturesurface_size; |
442 | |
443 | *device = &dev->device.common; |
444 | status = 0; |
445 | } |
446 | return status; |
447 | } |
448 |