summaryrefslogtreecommitdiff
path: root/tv_input.cpp (plain)
blob: 277349b2966142483927d3d4fee172d423950de0
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 "tvapi/android/tv/TvPlay.h"
26#include "tv_callback.h"
27#include "tvapi/android/include/tvcmd.h"
28#include <ui/GraphicBufferMapper.h>
29#include <ui/GraphicBuffer.h>
30/*****************************************************************************/
31
32#define LOGD(...) \
33{ \
34__android_log_print(ANDROID_LOG_DEBUG, "tv_input", __VA_ARGS__); }
35
36#ifndef container_of
37#define container_of(ptr, type, member) ({ \
38 const typeof(((type *) 0)->member) *__mptr = (ptr); \
39 (type *) ((char *) __mptr - (char *)(&((type *)0)->member)); })
40#endif
41
42struct sideband_handle_t {
43 native_handle_t nativeHandle;
44 int identflag;
45 int usage;
46};
47
48typedef struct tv_input_private {
49 tv_input_device_t device;
50 const tv_input_callback_ops_t *callback;
51 void *callback_data;
52 TvPlay *mpTv;
53 TvCallback *tvcallback;
54} tv_input_private_t;
55
56
57static int notify_ATV_device_available(tv_input_private_t *priv)
58{
59 tv_input_event_t event;
60 event.device_info.device_id = SOURCE_TV;
61 event.device_info.type = TV_INPUT_TYPE_TUNER;
62 event.type = TV_INPUT_EVENT_DEVICE_AVAILABLE;
63 event.device_info.audio_type = AUDIO_DEVICE_NONE;
64 event.device_info.audio_address = NULL;
65 priv->callback->notify(&priv->device, &event, priv->callback_data);
66 return 0;
67}
68
69static int notify_ATV_stream_configurations_change(tv_input_private_t *priv)
70{
71 tv_input_event_t event;
72 event.device_info.device_id = SOURCE_TV;
73 event.device_info.type = TV_INPUT_TYPE_TUNER;
74 event.type = TV_INPUT_EVENT_STREAM_CONFIGURATIONS_CHANGED;
75 event.device_info.audio_type = AUDIO_DEVICE_NONE;
76 event.device_info.audio_address = NULL;
77 priv->callback->notify(&priv->device, &event, priv->callback_data);
78 return 0;
79}
80
81static int notify_DTV_device_available(tv_input_private_t *priv)
82{
83 tv_input_event_t event;
84 event.device_info.device_id = SOURCE_DTV;
85 event.device_info.type = TV_INPUT_TYPE_TUNER;
86 event.type = TV_INPUT_EVENT_DEVICE_AVAILABLE;
87 event.device_info.audio_type = AUDIO_DEVICE_NONE;
88 event.device_info.audio_address = NULL;
89 priv->callback->notify(&priv->device, &event, priv->callback_data);
90 return 0;
91}
92
93static int notify_DTV_stream_configurations_change(tv_input_private_t *priv)
94{
95 tv_input_event_t event;
96 event.device_info.device_id = SOURCE_DTV;
97 event.device_info.type = TV_INPUT_TYPE_TUNER;
98 event.type = TV_INPUT_EVENT_STREAM_CONFIGURATIONS_CHANGED;
99 event.device_info.audio_type = AUDIO_DEVICE_NONE;
100 event.device_info.audio_address = NULL;
101 priv->callback->notify(&priv->device, &event, priv->callback_data);
102 return 0;
103}
104
105void TvIputHal_ChannelConl(tv_input_private_t *priv, int ops_type, int device_id)
106{
107 if (priv->mpTv) {
108 if (ops_type) {
109 LOGD ( "%s\, OpenSourceSwitchInput id = %d\n", __FUNCTION__, device_id );
110 priv->mpTv->StartTv();
111 priv->mpTv->SwitchSourceInput((tv_source_input_t) device_id);
112 } else if (priv->mpTv->GetCurrentSourceInput() == device_id) {
113 LOGD ( "%s\, StopSourceSwitchInput id = %d\n", __FUNCTION__, device_id );
114 priv->mpTv->StopTv();
115 }
116 }
117}
118
119static int notify_AV_device_available(tv_input_private_t *priv, tv_source_input_t source_input, int type)
120{
121 tv_input_event_t event;
122 event.device_info.device_id = source_input;
123 event.device_info.type = TV_INPUT_TYPE_COMPONENT;
124 event.type = type;
125 event.device_info.audio_type = AUDIO_DEVICE_NONE;
126 event.device_info.audio_address = NULL;
127 priv->callback->notify(&priv->device, &event, priv->callback_data);
128 return 0;
129}
130
131static int notify_AV_stream_configurations_change(tv_input_private_t *priv, tv_source_input_t source_input)
132{
133 tv_input_event_t event;
134 event.device_info.device_id = source_input;
135 event.device_info.type = TV_INPUT_TYPE_COMPONENT;
136 event.type = TV_INPUT_EVENT_STREAM_CONFIGURATIONS_CHANGED;
137 event.device_info.audio_type = AUDIO_DEVICE_NONE;
138 event.device_info.audio_address = NULL;
139 priv->callback->notify(&priv->device, &event, priv->callback_data);
140 return 0;
141}
142
143static int notify_HDMI_device_available(tv_input_private_t *priv, tv_source_input_t source_input, uint32_t port_id, int type)
144{
145 tv_input_event_t event;
146 event.device_info.device_id = source_input;
147 event.device_info.type = TV_INPUT_TYPE_HDMI;
148 event.type = type;
149 event.device_info.hdmi.port_id = port_id;
150 event.device_info.audio_type = AUDIO_DEVICE_NONE;
151 event.device_info.audio_address = NULL;
152 priv->callback->notify(&priv->device, &event, priv->callback_data);
153 return 0;
154}
155
156static int notify_HDMI_stream_configurations_change(tv_input_private_t *priv, tv_source_input_t source_input, uint32_t port_id)
157{
158 tv_input_event_t event;
159 event.device_info.device_id = source_input;
160 event.device_info.type = TV_INPUT_TYPE_HDMI;
161 event.type = TV_INPUT_EVENT_STREAM_CONFIGURATIONS_CHANGED;
162 event.device_info.hdmi.port_id = port_id;
163 event.device_info.audio_type = AUDIO_DEVICE_NONE;
164 event.device_info.audio_address = NULL;
165 priv->callback->notify(&priv->device, &event, priv->callback_data);
166 return 0;
167}
168
169void TvCallback::onTvEvent (int32_t msgType, const Parcel &p)
170{
171 tv_input_private_t *priv = (tv_input_private_t *)(mPri);
172 switch (msgType) {
173 case SOURCE_CONNECT_CALLBACK: {
174 int source = p.readInt32();
175 int connectState = p.readInt32();
176 LOGD("TvCallback::onTvEvent source = %d, status = %d", source, connectState)
177
178 switch (source) {
179 case SOURCE_HDMI1:
180 case SOURCE_HDMI2:
181 case SOURCE_HDMI3: {
182 if (connectState == 1) {
183 notify_HDMI_device_available(priv, (tv_source_input_t)source, 1, TV_INPUT_EVENT_DEVICE_AVAILABLE);
184 notify_HDMI_stream_configurations_change(priv, (tv_source_input_t)source, 1);
185 }
186 else {
187 notify_HDMI_device_available(priv, (tv_source_input_t)source, 1, TV_INPUT_EVENT_DEVICE_UNAVAILABLE);
188 }
189 }
190 break;
191
192 case SOURCE_AV1:
193 case SOURCE_AV2: {
194 if (connectState == 1) {
195 notify_AV_device_available(priv, (tv_source_input_t)source, TV_INPUT_EVENT_DEVICE_AVAILABLE);
196 notify_AV_stream_configurations_change(priv, (tv_source_input_t)source);
197 }
198 else {
199 notify_AV_device_available(priv, (tv_source_input_t)source, TV_INPUT_EVENT_DEVICE_UNAVAILABLE);
200 }
201 }
202 break;
203
204 default:
205 break;
206 }
207 }
208 break;
209
210 default:
211 break;
212 }
213}
214
215#define NORMAL_STREAM_ID 1
216#define FRAME_CAPTURE_STREAM_ID 2
217static tv_stream_config_t mconfig[2];
218static int get_stream_configs(int dev_id, int *num_configurations, const tv_stream_config_t **configs)
219{
220 switch (dev_id) {
221 case SOURCE_TV:
222 mconfig[0].stream_id = NORMAL_STREAM_ID;
223 mconfig[0].type = TV_STREAM_TYPE_INDEPENDENT_VIDEO_SOURCE ;
224 mconfig[0].max_video_width = 1920;
225 mconfig[0].max_video_height = 1080;
226 mconfig[1].stream_id = FRAME_CAPTURE_STREAM_ID;
227 mconfig[1].type = TV_STREAM_TYPE_BUFFER_PRODUCER ;
228 mconfig[1].max_video_width = 1920;
229 mconfig[1].max_video_height = 1080;
230 *num_configurations = 2;
231 *configs = mconfig;
232 break;
233 case SOURCE_DTV:
234 mconfig[0].stream_id = NORMAL_STREAM_ID;
235 mconfig[0].type = TV_STREAM_TYPE_INDEPENDENT_VIDEO_SOURCE ;
236 mconfig[0].max_video_width = 1920;
237 mconfig[0].max_video_height = 1080;
238 mconfig[1].stream_id = FRAME_CAPTURE_STREAM_ID;
239 mconfig[1].type = TV_STREAM_TYPE_BUFFER_PRODUCER ;
240 mconfig[1].max_video_width = 1920;
241 mconfig[1].max_video_height = 1080;
242 *num_configurations = 2;
243 *configs = mconfig;
244 break;
245 case SOURCE_AV1:
246 case SOURCE_AV2:
247 mconfig[0].stream_id = NORMAL_STREAM_ID;
248 mconfig[0].type = TV_STREAM_TYPE_INDEPENDENT_VIDEO_SOURCE ;
249 mconfig[0].max_video_width = 1920;
250 mconfig[0].max_video_height = 1080;
251 mconfig[1].stream_id = FRAME_CAPTURE_STREAM_ID;
252 mconfig[1].type = TV_STREAM_TYPE_BUFFER_PRODUCER ;
253 mconfig[1].max_video_width = 1920;
254 mconfig[1].max_video_height = 1080;
255 *num_configurations = 2;
256 *configs = mconfig;
257 break;
258 case SOURCE_HDMI1:
259 mconfig[0].stream_id = NORMAL_STREAM_ID;
260 mconfig[0].type = TV_STREAM_TYPE_INDEPENDENT_VIDEO_SOURCE ;
261 mconfig[0].max_video_width = 1920;
262 mconfig[0].max_video_height = 1080;
263 mconfig[1].stream_id = FRAME_CAPTURE_STREAM_ID;
264 mconfig[1].type = TV_STREAM_TYPE_BUFFER_PRODUCER ;
265 mconfig[1].max_video_width = 1920;
266 mconfig[1].max_video_height = 1080;
267 *num_configurations = 2;
268 *configs = mconfig;
269 break;
270 case SOURCE_HDMI2:
271 mconfig[0].stream_id = NORMAL_STREAM_ID;
272 mconfig[0].type = TV_STREAM_TYPE_INDEPENDENT_VIDEO_SOURCE ;
273 mconfig[0].max_video_width = 1920;
274 mconfig[0].max_video_height = 1080;
275 mconfig[1].stream_id = FRAME_CAPTURE_STREAM_ID;
276 mconfig[1].type = TV_STREAM_TYPE_BUFFER_PRODUCER ;
277 mconfig[1].max_video_width = 1920;
278 mconfig[1].max_video_height = 1080;
279 *num_configurations = 2;
280 *configs = mconfig;
281 break;
282 case SOURCE_HDMI3:
283 mconfig[0].stream_id = NORMAL_STREAM_ID;
284 mconfig[0].type = TV_STREAM_TYPE_INDEPENDENT_VIDEO_SOURCE ;
285 mconfig[0].max_video_width = 1920;
286 mconfig[0].max_video_height = 1080;
287 mconfig[1].stream_id = FRAME_CAPTURE_STREAM_ID;
288 mconfig[1].type = TV_STREAM_TYPE_BUFFER_PRODUCER ;
289 mconfig[1].max_video_width = 1920;
290 mconfig[1].max_video_height = 1080;
291 *num_configurations = 2;
292 *configs = mconfig;
293 break;
294 default:
295 break;
296 }
297 return 0;
298}
299
300static int get_tv_stream(tv_stream_t *stream)
301{
302 static struct sideband_handle_t *tvstream = NULL;
303 if (stream->stream_id == NORMAL_STREAM_ID) {
304 if ( !tvstream ) {
305 tvstream = (struct sideband_handle_t *)native_handle_create(0, 2);
306 if ( !tvstream ) {
307 return -EINVAL;
308 }
309 }
310 tvstream->identflag = 0xabcdcdef; //magic word
311 tvstream->usage = GRALLOC_USAGE_AML_VIDEO_OVERLAY;
312 stream->type = TV_STREAM_TYPE_INDEPENDENT_VIDEO_SOURCE;
313 stream->sideband_stream_source_handle = (native_handle_t *)tvstream;
314 } else if (stream->stream_id == NORMAL_STREAM_ID) {
315 stream->type = TV_STREAM_TYPE_BUFFER_PRODUCER;
316 }
317 return 0;
318}
319
320static int tv_input_device_open(const struct hw_module_t *module,
321 const char *name, struct hw_device_t **device);
322
323static struct hw_module_methods_t tv_input_module_methods = {
324open:
325 tv_input_device_open
326};
327
328tv_input_module_t HAL_MODULE_INFO_SYM = {
329 common: {
330 tag: HARDWARE_MODULE_TAG,
331 version_major: 0,
332 version_minor: 1,
333 id: TV_INPUT_HARDWARE_MODULE_ID,
334 name: "TVInput module",
335 author: "Amlogic",
336 methods: &tv_input_module_methods,
337 }
338};
339
340/*****************************************************************************/
341static int tv_input_initialize(struct tv_input_device *dev,
342 const tv_input_callback_ops_t *callback, void *data)
343{
344 if (dev == NULL || callback == NULL) {
345 return -EINVAL;
346 }
347 tv_input_private_t *priv = (tv_input_private_t *)dev;
348 if (priv->callback != NULL) {
349 return -EEXIST;
350 }
351 priv->callback = callback;
352 priv->callback_data = data;
353 /* ATV_DEVICE_AVAILABLE */
354 notify_ATV_device_available(priv);
355 notify_ATV_stream_configurations_change(priv);
356 /* DTV_DEVICE_AVAILABLE */
357 notify_DTV_device_available(priv);
358 notify_DTV_stream_configurations_change(priv);
359
360 if (priv->mpTv->GetHdmiAvHotplugDetectOnoff()) {
361 /* AV1_DEVICE_AVAILABLE */
362 int status = priv->mpTv->GetSourceConnectStatus(SOURCE_AV1);
363 if (status == 1) { //IN
364 notify_AV_device_available(priv, SOURCE_AV1, TV_INPUT_EVENT_DEVICE_AVAILABLE);
365 notify_AV_stream_configurations_change(priv, SOURCE_AV1);
366 }
367
368 /* AV2_DEVICE_AVAILABLE */
369 status = priv->mpTv->GetSourceConnectStatus(SOURCE_AV2);
370 if (status == 1) { //IN
371 notify_AV_device_available(priv, SOURCE_AV2, TV_INPUT_EVENT_DEVICE_AVAILABLE);
372 notify_AV_stream_configurations_change(priv, SOURCE_AV2);
373 }
374
375 /* HDMI1_DEVICE_AVAILABLE */
376 status = priv->mpTv->GetSourceConnectStatus(SOURCE_HDMI1);
377 if (status == 1) { //IN
378 notify_HDMI_device_available(priv, SOURCE_HDMI1, 1, TV_INPUT_EVENT_DEVICE_AVAILABLE);
379 notify_HDMI_stream_configurations_change(priv, SOURCE_HDMI1, 0);
380 }
381
382 /* HDMI2_DEVICE_AVAILABLE */
383 status = priv->mpTv->GetSourceConnectStatus(SOURCE_HDMI2);
384 if (status == 1) { //IN
385 notify_HDMI_device_available(priv, SOURCE_HDMI2, 1, TV_INPUT_EVENT_DEVICE_AVAILABLE);
386 notify_HDMI_stream_configurations_change(priv, SOURCE_HDMI2, 1);
387 }
388
389 /* HDMI3_DEVICE_AVAILABLE */
390 status = priv->mpTv->GetSourceConnectStatus(SOURCE_HDMI3);
391 if (status == 1) { //IN
392 notify_HDMI_device_available(priv, SOURCE_HDMI3, 2, TV_INPUT_EVENT_DEVICE_AVAILABLE);
393 notify_HDMI_stream_configurations_change(priv, SOURCE_HDMI3, 2);
394 }
395
396 priv->mpTv->setTvObserver(priv->tvcallback);
397 } else {
398 /* AV1_DEVICE_AVAILABLE */
399 notify_AV_device_available(priv, SOURCE_AV1, TV_INPUT_EVENT_DEVICE_AVAILABLE);
400 notify_AV_stream_configurations_change(priv, SOURCE_AV1);
401 /* AV2_DEVICE_AVAILABLE */
402 notify_AV_device_available(priv, SOURCE_AV2, TV_INPUT_EVENT_DEVICE_AVAILABLE);
403 notify_AV_stream_configurations_change(priv, SOURCE_AV2);
404 /* HDMI1_DEVICE_AVAILABLE */
405 notify_HDMI_device_available(priv, SOURCE_HDMI1, 1, TV_INPUT_EVENT_DEVICE_AVAILABLE);
406 notify_HDMI_stream_configurations_change(priv, SOURCE_HDMI1, 0);
407 /* HDMI2_DEVICE_AVAILABLE */
408 notify_HDMI_device_available(priv, SOURCE_HDMI2, 1, TV_INPUT_EVENT_DEVICE_AVAILABLE);
409 notify_HDMI_stream_configurations_change(priv, SOURCE_HDMI2, 1);
410 /* HDMI3_DEVICE_AVAILABLE */
411 notify_HDMI_device_available(priv, SOURCE_HDMI3, 2, TV_INPUT_EVENT_DEVICE_AVAILABLE);
412 notify_HDMI_stream_configurations_change(priv, SOURCE_HDMI3, 2);
413 }
414 return 0;
415}
416
417static int tv_input_get_stream_configurations(const struct tv_input_device *dev,
418 int device_id, int *num_configurations,
419 const tv_stream_config_t **configs)
420{
421 if (get_stream_configs(device_id, num_configurations, configs) == 0) {
422 return 0;
423 }
424 return -EINVAL;
425}
426
427static int tv_input_open_stream(struct tv_input_device *dev, int device_id,
428 tv_stream_t *stream)
429{
430 tv_input_private_t *priv = (tv_input_private_t *)dev;
431 if (priv) {
432 if (get_tv_stream(stream) != 0) {
433 return -EINVAL;
434 }
435 if (stream->stream_id == NORMAL_STREAM_ID) {
436 TvIputHal_ChannelConl(priv, 1, device_id);
437 return 0;
438 } else if (stream->stream_id == FRAME_CAPTURE_STREAM_ID) {
439 return 0;
440 }
441 }
442 return -EINVAL;
443}
444
445static int tv_input_close_stream(struct tv_input_device *dev, int device_id,
446 int stream_id)
447{
448 tv_input_private_t *priv = (tv_input_private_t *)dev;
449 if (stream_id == NORMAL_STREAM_ID) {
450 TvIputHal_ChannelConl(priv, 0, device_id);
451 return 0;
452 } else if (stream_id == FRAME_CAPTURE_STREAM_ID) {
453 return 0;
454 }
455 return -EINVAL;
456}
457
458static int tv_input_request_capture(
459 struct tv_input_device *dev, int device_id, int stream_id, buffer_handle_t buffer, uint32_t seq)
460{
461 return -EINVAL;
462}
463
464static int tv_input_cancel_capture(struct tv_input_device *, int, int, uint32_t)
465{
466 return -EINVAL;
467}
468
469/*****************************************************************************/
470
471static int tv_input_device_close(struct hw_device_t *dev)
472{
473 tv_input_private_t *priv = (tv_input_private_t *)dev;
474 if (priv) {
475 if (priv->mpTv) {
476 delete priv->mpTv;
477 }
478 free(priv);
479 }
480 return 0;
481}
482
483/*****************************************************************************/
484
485static int tv_input_device_open(const struct hw_module_t *module,
486 const char *name, struct hw_device_t **device)
487{
488 int status = -EINVAL;
489 if (!strcmp(name, TV_INPUT_DEFAULT_DEVICE)) {
490 tv_input_private_t *dev = (tv_input_private_t *)malloc(sizeof(*dev));
491
492 /* initialize our state here */
493 memset(dev, 0, sizeof(*dev));
494 dev->mpTv = new TvPlay();
495 dev->tvcallback = new TvCallback(dev);
496 /* initialize the procs */
497 dev->device.common.tag = HARDWARE_DEVICE_TAG;
498 dev->device.common.version = TV_INPUT_DEVICE_API_VERSION_0_1;
499 dev->device.common.module = const_cast<hw_module_t *>(module);
500 dev->device.common.close = tv_input_device_close;
501
502 dev->device.initialize = tv_input_initialize;
503 dev->device.get_stream_configurations =
504 tv_input_get_stream_configurations;
505 dev->device.open_stream = tv_input_open_stream;
506 dev->device.close_stream = tv_input_close_stream;
507 dev->device.request_capture = tv_input_request_capture;
508 dev->device.cancel_capture = tv_input_cancel_capture;
509
510 *device = &dev->device.common;
511 status = 0;
512 }
513 return status;
514}
515