summaryrefslogtreecommitdiff
path: root/hwc2/common/utils/AmVideo.cpp (plain)
blob: 89e0fbcd25b9f9d4d2ad2ac699e27934fbd4aac9
1/*
2// Copyright (c) 2017 Amlogic
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
18#define LOG_TAG "AmVideo"
19//#define LOG_NDEBUG 0
20#include <cutils/log.h>
21
22#include <AmVideo.h>
23#include <fcntl.h>
24#include <sys/ioctl.h>
25#include <errno.h>
26
27//#define AMVIDEO_DEBUG
28
29using namespace android;
30
31#define AM_VIDEO_DEV "/dev/amvideo"
32
33#define AMSTREAM_IOC_MAGIC 'S'
34#define AMSTREAM_IOC_GLOBAL_GET_VIDEO_OUTPUT _IOR(AMSTREAM_IOC_MAGIC, 0x21, int)
35#define AMSTREAM_IOC_GLOBAL_SET_VIDEO_OUTPUT _IOW(AMSTREAM_IOC_MAGIC, 0x22, int)
36#define AMSTREAM_IOC_GET_VIDEO_DISABLE _IOR((AMSTREAM_IOC_MAGIC), 0x48, int)
37#define AMSTREAM_IOC_SET_VIDEO_DISABLE _IOW((AMSTREAM_IOC_MAGIC), 0x49, int)
38#define AMSTREAM_IOC_GET_OMX_INFO _IOR((AMSTREAM_IOC_MAGIC), 0xb2, unsigned int)
39
40AmVideo* AmVideo::mInstance = NULL;
41Mutex AmVideo::mLock;
42
43AmVideo::AmVideo() {
44 mDevFd = open(AM_VIDEO_DEV, O_RDWR | O_NONBLOCK);
45 if (mDevFd < 0) {
46 ALOGE("Open %s Failed. ", AM_VIDEO_DEV);
47 }
48
49 if (getVideoPresent(mVideoPresent) != 0) {
50 ALOGE("Get video mute failed.");
51 mVideoPresent = true;
52 }
53}
54
55AmVideo::~AmVideo() {
56 if (mDevFd >= 0) {
57 close(mDevFd);
58 mDevFd = -1;
59 }
60}
61
62AmVideo* AmVideo::getInstance() {
63 if (mInstance == NULL) {
64 Mutex::Autolock _l(mLock);
65 if (mInstance == NULL) {
66 mInstance = new AmVideo();
67 }
68 }
69
70 return mInstance;
71}
72
73int AmVideo::presentVideo(bool bPresent) {
74 if (mDevFd < 0)
75 return -EBADF;
76
77 if (mVideoPresent != bPresent) {
78 ALOGD("muteVideo to %d", bPresent);
79 uint32_t val = bPresent ? 1 : 0;
80 if (ioctl(mDevFd, AMSTREAM_IOC_GLOBAL_SET_VIDEO_OUTPUT, val) != 0) {
81 ALOGE("AMSTREAM_SET_VIDEO_OUTPUT ioctl (%d) return(%d)", bPresent, errno);
82 return -EINVAL;
83 }
84 mVideoPresent = bPresent;
85 } else {
86 #ifdef AMVIDEO_DEBUG
87 bool val = true;
88 getVideoPresent(val);
89 if (mVideoPresent != val) {
90 ALOGE("presentVideo (%d) vs (%d)", mVideoPresent, val);
91 }
92 ALOGD("Already set video to (%d)", bPresent);
93 #endif
94 }
95
96 return 0;
97}
98
99int AmVideo::getVideoPresent(bool& output) {
100 if (mDevFd < 0)
101 return -EBADF;
102
103 uint32_t val = 1;
104 if (ioctl(mDevFd, AMSTREAM_IOC_GLOBAL_GET_VIDEO_OUTPUT, &val) != 0) {
105 ALOGE("AMSTREAM_GET_VIDEO_OUTPUT ioctl fail(%d)", errno);
106 return -EINVAL;
107 }
108
109 output = (val ==0) ? false : true;
110 return 0;
111}
112
113int AmVideo::getvideodisable(int* mode) {
114 if (mDevFd < 0)
115 return -EBADF;
116
117 int ret = ioctl(mDevFd, AMSTREAM_IOC_GET_VIDEO_DISABLE, mode);
118 if (ret < 0) {
119 ALOGE("getvideodisable error, ret=%d", ret);
120 return ret;
121 }
122 return 0;
123}
124
125int AmVideo::setvideodisable(int mode) {
126 if (mDevFd < 0)
127 return -EBADF;
128 int ret = ioctl(mDevFd, AMSTREAM_IOC_SET_VIDEO_DISABLE, &mode);
129 if (ret < 0) {
130 ALOGE("setvideodisable error, ret=%d", ret);
131 return ret;
132 }
133 return 0;
134}
135
136int AmVideo::getOmxKeepLastFrame(unsigned int *keepLastFrame) {
137 if (mDevFd < 0)
138 return -EBADF;
139
140 unsigned long omx_info = 0;
141 int ret = ioctl(mDevFd, AMSTREAM_IOC_GET_OMX_INFO, &omx_info);
142 if (ret < 0) {
143 ALOGE("get omx info error, ret =%d", ret);
144 *keepLastFrame = 0;
145 return ret;
146 } else {
147 *keepLastFrame = omx_info & 0x1; //omx_info bit0: keep last frmame
148 }
149 //ALOGV("video layer keepLastFrame %d", *keepLastFrame);
150 return 0;
151
152}
153
154