blob: c29242d91a4e2761157ef81a5bd056251efb58b1 [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"
28
29#define _A_M 'S'
Song Zhao0adb3b52020-07-27 16:15:04 -070030#define AMSTREAM_IOC_SYNCTHRESH _IOW((_A_M), 0x19, int)
Song Zhaobf35d4a2020-06-18 22:36:29 -070031#define AMSTREAM_IOC_SET_VSYNC_UPINT _IOW((_A_M), 0x89, int)
32#define AMSTREAM_IOC_SET_VIDEOPEEK _IOW(_A_M, 0xbf, unsigned int)
Song Zhao58d3a1c2020-08-04 16:51:30 -070033#define AMSTREAM_IOC_SET_NO_VIDEO_STOP _IOW(_A_M, 0xf5, unsigned int)
Song Zhaocdf13b62020-08-14 17:46:41 -070034#define AMSTREAM_IOC_SET_VSYNC_SLOW_FACTOR _IOW((_A_M), 0x8b, int)
Song Zhao6859d412020-06-15 17:16:04 -070035
36static int config_sys_node(const char* path, const char* value)
37{
38 int fd;
39 fd = open(path, O_RDWR);
40 if (fd < 0) {
Song Zhaoebb53bb2020-07-10 17:57:34 -070041 log_error("fail to open %s\n", path);
Song Zhao6859d412020-06-15 17:16:04 -070042 return -1;
43 }
44 if (write(fd, value, strlen(value)) != strlen(value)) {
Song Zhaoebb53bb2020-07-10 17:57:34 -070045 log_error("fail to write %s to %s\n", value, path);
Song Zhao6859d412020-06-15 17:16:04 -070046 close(fd);
47 return -1;
48 }
49 close(fd);
50
51 return 0;
52}
53
54static int get_sysfs_uint32(const char *path, uint32_t *value)
55{
56 int fd;
57 char valstr[64];
58 uint32_t val = 0;
59
60 fd = open(path, O_RDONLY);
61 if (fd >= 0) {
62 memset(valstr, 0, 64);
63 read(fd, valstr, 64 - 1);
64 valstr[strlen(valstr)] = '\0';
65 close(fd);
66 } else {
Song Zhaoebb53bb2020-07-10 17:57:34 -070067 log_error("unable to open file %s\n", path);
Song Zhao6859d412020-06-15 17:16:04 -070068 return -1;
69 }
70 if (sscanf(valstr, "0x%x", &val) < 1) {
Song Zhaoebb53bb2020-07-10 17:57:34 -070071 log_error("unable to get pts from: %s", valstr);
Song Zhao6859d412020-06-15 17:16:04 -070072 return -1;
73 }
74 *value = val;
75 return 0;
76}
77
Song Zhaobf35d4a2020-06-18 22:36:29 -070078int tsync_enable(int session, bool enable)
Song Zhao6859d412020-06-15 17:16:04 -070079{
80 const char *val;
81
82 if (enable)
83 val = "1";
84 else
85 val = "0";
Song Zhaoebb53bb2020-07-10 17:57:34 -070086 log_info("%s", val);
Song Zhaobf35d4a2020-06-18 22:36:29 -070087 return config_sys_node(TSYNC_ENABLE, val);
Song Zhao6859d412020-06-15 17:16:04 -070088}
89
90uint32_t tsync_get_pcr(int session)
91{
92 uint32_t pcr = 0;
93
94 get_sysfs_uint32(TSYNC_PCRSCR, &pcr);
95 return pcr;
96}
97
98//uint32_t tsync_get_vpts(int session);
99
100int tsync_send_video_start(int session, uint32_t vpts)
101{
102 char val[50];
103
104 snprintf(val, sizeof(val), "VIDEO_START:0x%x", vpts);
Song Zhaoebb53bb2020-07-10 17:57:34 -0700105 log_info("%s", val);
Song Zhao6859d412020-06-15 17:16:04 -0700106 return config_sys_node(TSYNC_EVENT, val);
107}
108
109int tsync_send_video_pause(int session, bool pause)
110{
111 const char *val;
112
113 if (pause)
114 val = "VIDEO_PAUSE:0x1";
115 else
116 val = "VIDEO_PAUSE:0x0";
Song Zhaoebb53bb2020-07-10 17:57:34 -0700117 log_info("%s", val);
Song Zhao6859d412020-06-15 17:16:04 -0700118 return config_sys_node(TSYNC_EVENT, val);
119}
120
121int tsync_send_video_disc(int session, uint32_t vpts)
122{
123 char val[50];
124
125 snprintf(val, sizeof(val), "VIDEO_TSTAMP_DISCONTINUITY:0x%x", vpts);
Song Zhaoebb53bb2020-07-10 17:57:34 -0700126 log_info("%s", val);
Song Zhao6859d412020-06-15 17:16:04 -0700127 return config_sys_node(TSYNC_EVENT, val);
128}
Song Zhaobf35d4a2020-06-18 22:36:29 -0700129
130int tsync_set_pcr(int session, uint32_t pcr)
131{
132 char val[20];
133
134 snprintf(val, sizeof(val), "%u", pcr);
135 config_sys_node(TSYNC_PCRSCR, val);
136 return 0;
137}
138
139static int video_device_ioctl(int ctl, int value)
140{
141 int video_fd;
142
143 video_fd = open(VIDEO_DEVICE, O_RDWR);
144 if (video_fd < 0) {
145 return -1;
146 }
147
Song Zhaoebb53bb2020-07-10 17:57:34 -0700148 ioctl(video_fd, ctl, value);
Song Zhaobf35d4a2020-06-18 22:36:29 -0700149
150 close(video_fd);
151
152 return 0;
153}
154
155int tsync_set_pts_inc_mode(int session, bool enable)
156{
157 return video_device_ioctl(AMSTREAM_IOC_SET_VSYNC_UPINT, enable);
158}
159
Song Zhao0adb3b52020-07-27 16:15:04 -0700160int tsync_set_video_sync_thres(int session, bool enable)
161{
162 return video_device_ioctl(AMSTREAM_IOC_SYNCTHRESH, enable);
163}
164
Song Zhaobf35d4a2020-06-18 22:36:29 -0700165int tsync_set_mode(int session, enum sync_mode mode)
166{
167 const char* val = NULL;
168 if (mode == AV_SYNC_MODE_VMASTER)
169 val = "0";
170 else if (mode == AV_SYNC_MODE_AMASTER)
171 val = "1";
172 else if (mode == AV_SYNC_MODE_PCR_MASTER)
173 val = "2";
174 return config_sys_node(TSYNC_MODE, val);
175}
176
Song Zhaoc83a9b82020-07-25 00:08:42 -0700177/* do not send VIDEO_START from video.c */
Song Zhaobf35d4a2020-06-18 22:36:29 -0700178int tsync_set_video_peek_mode(int session)
179{
Song Zhaoc83a9b82020-07-25 00:08:42 -0700180 log_info("set video peek");
Song Zhaobf35d4a2020-06-18 22:36:29 -0700181 return video_device_ioctl(AMSTREAM_IOC_SET_VIDEOPEEK, 0);
182}
Song Zhao58d3a1c2020-08-04 16:51:30 -0700183
184/* Control VIDEO_STOP from video.c */
185int tsync_disable_video_stop_event(int session, bool disable)
186{
187 log_info("no_video_stop %d", disable);
188 return video_device_ioctl(AMSTREAM_IOC_SET_NO_VIDEO_STOP, disable);
189}
Song Zhaocdf13b62020-08-14 17:46:41 -0700190
191int tsync_set_speed(int session, float speed)
192{
193 return video_device_ioctl(AMSTREAM_IOC_SET_VSYNC_SLOW_FACTOR, 1000000 * speed);
194}