blob: 32ca499b9359a5b1ff156cbc5b1ba4e16687c29d [file] [log] [blame]
Song Zhao6859d412020-06-15 17:16:04 -07001/*
2 * Copyright (c) 2020 Amlogic, Inc. All rights reserved.
3 *
4 * This source code is subject to the terms and conditions defined in the
5 * file 'LICENSE' which is part of this source code package.
6 *
7 * Description: tsync warrper. Single instance ONLY. Session will be ignored
8 * Author: song.zhao@amlogic.com
9 */
10
11#include <stdbool.h>
12#include <stdlib.h>
13#include <stdio.h>
14#include <sys/types.h>
15#include <sys/stat.h>
16#include <fcntl.h>
17#include <unistd.h>
18#include <string.h>
Song Zhaobf35d4a2020-06-18 22:36:29 -070019#include <sys/ioctl.h>
Song Zhao6859d412020-06-15 17:16:04 -070020#include "tsync.h"
Song Zhaoebb53bb2020-07-10 17:57:34 -070021#include "aml_avsync_log.h"
Song Zhao6859d412020-06-15 17:16:04 -070022
Song Zhaobf35d4a2020-06-18 22:36:29 -070023#define VIDEO_DEVICE "/dev/amvideo"
Song Zhao6859d412020-06-15 17:16:04 -070024#define TSYNC_ENABLE "/sys/class/tsync/enable"
25#define TSYNC_PCRSCR "/sys/class/tsync/pts_pcrscr"
26#define TSYNC_EVENT "/sys/class/tsync/event"
Song Zhaobf35d4a2020-06-18 22:36:29 -070027#define TSYNC_MODE "/sys/class/tsync/mode"
Song Zhaoa2318622020-12-23 00:21:42 -080028#define TSYNC_VPTS "/sys/class/tsync/pts_video"
Song Zhaobf35d4a2020-06-18 22:36:29 -070029
30#define _A_M 'S'
Song Zhao0adb3b52020-07-27 16:15:04 -070031#define AMSTREAM_IOC_SYNCTHRESH _IOW((_A_M), 0x19, int)
Song Zhaobf35d4a2020-06-18 22:36:29 -070032#define AMSTREAM_IOC_SET_VSYNC_UPINT _IOW((_A_M), 0x89, int)
33#define AMSTREAM_IOC_SET_VIDEOPEEK _IOW(_A_M, 0xbf, unsigned int)
Song Zhao58d3a1c2020-08-04 16:51:30 -070034#define AMSTREAM_IOC_SET_NO_VIDEO_STOP _IOW(_A_M, 0xf5, unsigned int)
Song Zhaocdf13b62020-08-14 17:46:41 -070035#define AMSTREAM_IOC_SET_VSYNC_SLOW_FACTOR _IOW((_A_M), 0x8b, int)
Song Zhao6859d412020-06-15 17:16:04 -070036
37static int config_sys_node(const char* path, const char* value)
38{
39 int fd;
40 fd = open(path, O_RDWR);
41 if (fd < 0) {
Song Zhaoebb53bb2020-07-10 17:57:34 -070042 log_error("fail to open %s\n", path);
Song Zhao6859d412020-06-15 17:16:04 -070043 return -1;
44 }
45 if (write(fd, value, strlen(value)) != strlen(value)) {
Song Zhaoebb53bb2020-07-10 17:57:34 -070046 log_error("fail to write %s to %s\n", value, path);
Song Zhao6859d412020-06-15 17:16:04 -070047 close(fd);
48 return -1;
49 }
50 close(fd);
51
52 return 0;
53}
54
55static int get_sysfs_uint32(const char *path, uint32_t *value)
56{
57 int fd;
58 char valstr[64];
59 uint32_t val = 0;
60
61 fd = open(path, O_RDONLY);
62 if (fd >= 0) {
63 memset(valstr, 0, 64);
64 read(fd, valstr, 64 - 1);
65 valstr[strlen(valstr)] = '\0';
66 close(fd);
67 } else {
Song Zhaoebb53bb2020-07-10 17:57:34 -070068 log_error("unable to open file %s\n", path);
Song Zhao6859d412020-06-15 17:16:04 -070069 return -1;
70 }
71 if (sscanf(valstr, "0x%x", &val) < 1) {
Song Zhaoebb53bb2020-07-10 17:57:34 -070072 log_error("unable to get pts from: %s", valstr);
Song Zhao6859d412020-06-15 17:16:04 -070073 return -1;
74 }
75 *value = val;
76 return 0;
77}
78
Song Zhaobf35d4a2020-06-18 22:36:29 -070079int tsync_enable(int session, bool enable)
Song Zhao6859d412020-06-15 17:16:04 -070080{
81 const char *val;
82
83 if (enable)
84 val = "1";
85 else
86 val = "0";
Song Zhaoebb53bb2020-07-10 17:57:34 -070087 log_info("%s", val);
Song Zhaobf35d4a2020-06-18 22:36:29 -070088 return config_sys_node(TSYNC_ENABLE, val);
Song Zhao6859d412020-06-15 17:16:04 -070089}
90
91uint32_t tsync_get_pcr(int session)
92{
93 uint32_t pcr = 0;
94
95 get_sysfs_uint32(TSYNC_PCRSCR, &pcr);
96 return pcr;
97}
98
99//uint32_t tsync_get_vpts(int session);
100
101int tsync_send_video_start(int session, uint32_t vpts)
102{
103 char val[50];
104
105 snprintf(val, sizeof(val), "VIDEO_START:0x%x", vpts);
Song Zhaoebb53bb2020-07-10 17:57:34 -0700106 log_info("%s", val);
Song Zhao6859d412020-06-15 17:16:04 -0700107 return config_sys_node(TSYNC_EVENT, val);
108}
109
110int tsync_send_video_pause(int session, bool pause)
111{
112 const char *val;
113
114 if (pause)
115 val = "VIDEO_PAUSE:0x1";
116 else
117 val = "VIDEO_PAUSE:0x0";
Song Zhaoebb53bb2020-07-10 17:57:34 -0700118 log_info("%s", val);
Song Zhao6859d412020-06-15 17:16:04 -0700119 return config_sys_node(TSYNC_EVENT, val);
120}
121
122int tsync_send_video_disc(int session, uint32_t vpts)
123{
124 char val[50];
125
126 snprintf(val, sizeof(val), "VIDEO_TSTAMP_DISCONTINUITY:0x%x", vpts);
Song Zhaoebb53bb2020-07-10 17:57:34 -0700127 log_info("%s", val);
Song Zhao6859d412020-06-15 17:16:04 -0700128 return config_sys_node(TSYNC_EVENT, val);
129}
Song Zhaobf35d4a2020-06-18 22:36:29 -0700130
131int tsync_set_pcr(int session, uint32_t pcr)
132{
133 char val[20];
134
135 snprintf(val, sizeof(val), "%u", pcr);
136 config_sys_node(TSYNC_PCRSCR, val);
137 return 0;
138}
139
140static int video_device_ioctl(int ctl, int value)
141{
142 int video_fd;
143
144 video_fd = open(VIDEO_DEVICE, O_RDWR);
145 if (video_fd < 0) {
146 return -1;
147 }
148
Song Zhaoebb53bb2020-07-10 17:57:34 -0700149 ioctl(video_fd, ctl, value);
Song Zhaobf35d4a2020-06-18 22:36:29 -0700150
151 close(video_fd);
152
153 return 0;
154}
155
156int tsync_set_pts_inc_mode(int session, bool enable)
157{
158 return video_device_ioctl(AMSTREAM_IOC_SET_VSYNC_UPINT, enable);
159}
160
Song Zhao0adb3b52020-07-27 16:15:04 -0700161int tsync_set_video_sync_thres(int session, bool enable)
162{
163 return video_device_ioctl(AMSTREAM_IOC_SYNCTHRESH, enable);
164}
165
Song Zhaobf35d4a2020-06-18 22:36:29 -0700166int tsync_set_mode(int session, enum sync_mode mode)
167{
168 const char* val = NULL;
169 if (mode == AV_SYNC_MODE_VMASTER)
170 val = "0";
171 else if (mode == AV_SYNC_MODE_AMASTER)
172 val = "1";
173 else if (mode == AV_SYNC_MODE_PCR_MASTER)
174 val = "2";
175 return config_sys_node(TSYNC_MODE, val);
176}
177
Song Zhaoc83a9b82020-07-25 00:08:42 -0700178/* do not send VIDEO_START from video.c */
Song Zhaobf35d4a2020-06-18 22:36:29 -0700179int tsync_set_video_peek_mode(int session)
180{
Song Zhaoc83a9b82020-07-25 00:08:42 -0700181 log_info("set video peek");
Song Zhaobf35d4a2020-06-18 22:36:29 -0700182 return video_device_ioctl(AMSTREAM_IOC_SET_VIDEOPEEK, 0);
183}
Song Zhao58d3a1c2020-08-04 16:51:30 -0700184
185/* Control VIDEO_STOP from video.c */
186int tsync_disable_video_stop_event(int session, bool disable)
187{
188 log_info("no_video_stop %d", disable);
189 return video_device_ioctl(AMSTREAM_IOC_SET_NO_VIDEO_STOP, disable);
190}
Song Zhaocdf13b62020-08-14 17:46:41 -0700191
192int tsync_set_speed(int session, float speed)
193{
194 return video_device_ioctl(AMSTREAM_IOC_SET_VSYNC_SLOW_FACTOR, 1000000 * speed);
195}
Song Zhaoa2318622020-12-23 00:21:42 -0800196
197int tsync_set_vpts(int session, pts90K pts)
198{
199 char val[50];
200
201 snprintf(val, sizeof(val), "0x%x", pts);
202 return config_sys_node(TSYNC_VPTS, val);
203}