summaryrefslogtreecommitdiff
path: root/hwc2/common/utils/AmVideo.cpp (plain)
blob: 15db0c96b1f6dd28eaeeb5e02b96dcfa36e94759
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
37AmVideo* AmVideo::mInstance = NULL;
38Mutex AmVideo::mLock;
39
40AmVideo::AmVideo() {
41 mDevFd = open(AM_VIDEO_DEV, O_RDWR | O_NONBLOCK);
42 if (mDevFd < 0) {
43 ALOGE("Open %s Failed. ", AM_VIDEO_DEV);
44 }
45
46 if (getVideoPresent(mVideoPresent) != 0) {
47 ALOGE("Get video mute failed.");
48 mVideoPresent = true;
49 }
50}
51
52AmVideo::~AmVideo() {
53 if (mDevFd >= 0) {
54 close(mDevFd);
55 mDevFd = -1;
56 }
57}
58
59AmVideo* AmVideo::getInstance() {
60 if (mInstance == NULL) {
61 Mutex::Autolock _l(mLock);
62 if (mInstance == NULL) {
63 mInstance = new AmVideo();
64 }
65 }
66
67 return mInstance;
68}
69
70int AmVideo::presentVideo(bool bPresent) {
71 if (mDevFd < 0)
72 return -EBADF;
73
74 if (mVideoPresent != bPresent) {
75 ALOGD("muteVideo to %d", bPresent);
76 uint32_t val = bPresent ? 1 : 0;
77 if (ioctl(mDevFd, AMSTREAM_IOC_GLOBAL_SET_VIDEO_OUTPUT, val) != 0) {
78 ALOGE("AMSTREAM_SET_VIDEO_OUTPUT ioctl (%d) return(%d)", bPresent, errno);
79 return -EINVAL;
80 }
81 mVideoPresent = bPresent;
82 } else {
83 #ifdef AMVIDEO_DEBUG
84 bool val = true;
85 getVideoPresent(val);
86 if (mVideoPresent != val) {
87 ALOGE("presentVideo (%d) vs (%d)", mVideoPresent, val);
88 }
89 ALOGD("Already set video to (%d)", bPresent);
90 #endif
91 }
92
93 return 0;
94}
95
96int AmVideo::getVideoPresent(bool& output) {
97 if (mDevFd < 0)
98 return -EBADF;
99
100 uint32_t val = 1;
101 if (ioctl(mDevFd, AMSTREAM_IOC_GLOBAL_GET_VIDEO_OUTPUT, &val) != 0) {
102 ALOGE("AMSTREAM_GET_VIDEO_OUTPUT ioctl fail(%d)", errno);
103 return -EINVAL;
104 }
105
106 output = (val ==0) ? false : true;
107 return 0;
108}
109
110