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