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