blob: f07b203713c0aebfafd5e3ec755ef8505302f8d1 [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 Zhao6859d412020-06-15 17:16:04 -070034
35static int config_sys_node(const char* path, const char* value)
36{
37 int fd;
38 fd = open(path, O_RDWR);
39 if (fd < 0) {
Song Zhaoebb53bb2020-07-10 17:57:34 -070040 log_error("fail to open %s\n", path);
Song Zhao6859d412020-06-15 17:16:04 -070041 return -1;
42 }
43 if (write(fd, value, strlen(value)) != strlen(value)) {
Song Zhaoebb53bb2020-07-10 17:57:34 -070044 log_error("fail to write %s to %s\n", value, path);
Song Zhao6859d412020-06-15 17:16:04 -070045 close(fd);
46 return -1;
47 }
48 close(fd);
49
50 return 0;
51}
52
53static int get_sysfs_uint32(const char *path, uint32_t *value)
54{
55 int fd;
56 char valstr[64];
57 uint32_t val = 0;
58
59 fd = open(path, O_RDONLY);
60 if (fd >= 0) {
61 memset(valstr, 0, 64);
62 read(fd, valstr, 64 - 1);
63 valstr[strlen(valstr)] = '\0';
64 close(fd);
65 } else {
Song Zhaoebb53bb2020-07-10 17:57:34 -070066 log_error("unable to open file %s\n", path);
Song Zhao6859d412020-06-15 17:16:04 -070067 return -1;
68 }
69 if (sscanf(valstr, "0x%x", &val) < 1) {
Song Zhaoebb53bb2020-07-10 17:57:34 -070070 log_error("unable to get pts from: %s", valstr);
Song Zhao6859d412020-06-15 17:16:04 -070071 return -1;
72 }
73 *value = val;
74 return 0;
75}
76
Song Zhaobf35d4a2020-06-18 22:36:29 -070077int tsync_enable(int session, bool enable)
Song Zhao6859d412020-06-15 17:16:04 -070078{
79 const char *val;
80
81 if (enable)
82 val = "1";
83 else
84 val = "0";
Song Zhaoebb53bb2020-07-10 17:57:34 -070085 log_info("%s", val);
Song Zhaobf35d4a2020-06-18 22:36:29 -070086 return config_sys_node(TSYNC_ENABLE, val);
Song Zhao6859d412020-06-15 17:16:04 -070087}
88
89uint32_t tsync_get_pcr(int session)
90{
91 uint32_t pcr = 0;
92
93 get_sysfs_uint32(TSYNC_PCRSCR, &pcr);
94 return pcr;
95}
96
97//uint32_t tsync_get_vpts(int session);
98
99int tsync_send_video_start(int session, uint32_t vpts)
100{
101 char val[50];
102
103 snprintf(val, sizeof(val), "VIDEO_START:0x%x", vpts);
Song Zhaoebb53bb2020-07-10 17:57:34 -0700104 log_info("%s", val);
Song Zhao6859d412020-06-15 17:16:04 -0700105 return config_sys_node(TSYNC_EVENT, val);
106}
107
108int tsync_send_video_pause(int session, bool pause)
109{
110 const char *val;
111
112 if (pause)
113 val = "VIDEO_PAUSE:0x1";
114 else
115 val = "VIDEO_PAUSE:0x0";
Song Zhaoebb53bb2020-07-10 17:57:34 -0700116 log_info("%s", val);
Song Zhao6859d412020-06-15 17:16:04 -0700117 return config_sys_node(TSYNC_EVENT, val);
118}
119
120int tsync_send_video_disc(int session, uint32_t vpts)
121{
122 char val[50];
123
124 snprintf(val, sizeof(val), "VIDEO_TSTAMP_DISCONTINUITY:0x%x", vpts);
Song Zhaoebb53bb2020-07-10 17:57:34 -0700125 log_info("%s", val);
Song Zhao6859d412020-06-15 17:16:04 -0700126 return config_sys_node(TSYNC_EVENT, val);
127}
Song Zhaobf35d4a2020-06-18 22:36:29 -0700128
129int tsync_set_pcr(int session, uint32_t pcr)
130{
131 char val[20];
132
133 snprintf(val, sizeof(val), "%u", pcr);
134 config_sys_node(TSYNC_PCRSCR, val);
135 return 0;
136}
137
138static int video_device_ioctl(int ctl, int value)
139{
140 int video_fd;
141
142 video_fd = open(VIDEO_DEVICE, O_RDWR);
143 if (video_fd < 0) {
144 return -1;
145 }
146
Song Zhaoebb53bb2020-07-10 17:57:34 -0700147 ioctl(video_fd, ctl, value);
Song Zhaobf35d4a2020-06-18 22:36:29 -0700148
149 close(video_fd);
150
151 return 0;
152}
153
154int tsync_set_pts_inc_mode(int session, bool enable)
155{
156 return video_device_ioctl(AMSTREAM_IOC_SET_VSYNC_UPINT, enable);
157}
158
Song Zhao0adb3b52020-07-27 16:15:04 -0700159int tsync_set_video_sync_thres(int session, bool enable)
160{
161 return video_device_ioctl(AMSTREAM_IOC_SYNCTHRESH, enable);
162}
163
Song Zhaobf35d4a2020-06-18 22:36:29 -0700164int tsync_set_mode(int session, enum sync_mode mode)
165{
166 const char* val = NULL;
167 if (mode == AV_SYNC_MODE_VMASTER)
168 val = "0";
169 else if (mode == AV_SYNC_MODE_AMASTER)
170 val = "1";
171 else if (mode == AV_SYNC_MODE_PCR_MASTER)
172 val = "2";
173 return config_sys_node(TSYNC_MODE, val);
174}
175
Song Zhaoc83a9b82020-07-25 00:08:42 -0700176/* do not send VIDEO_START from video.c */
Song Zhaobf35d4a2020-06-18 22:36:29 -0700177int tsync_set_video_peek_mode(int session)
178{
Song Zhaoc83a9b82020-07-25 00:08:42 -0700179 log_info("set video peek");
Song Zhaobf35d4a2020-06-18 22:36:29 -0700180 return video_device_ioctl(AMSTREAM_IOC_SET_VIDEOPEEK, 0);
181}
Song Zhao58d3a1c2020-08-04 16:51:30 -0700182
183/* Control VIDEO_STOP from video.c */
184int tsync_disable_video_stop_event(int session, bool disable)
185{
186 log_info("no_video_stop %d", disable);
187 return video_device_ioctl(AMSTREAM_IOC_SET_NO_VIDEO_STOP, disable);
188}