blob: b17d7b8ec340cfbfa735852a3f1c2f879c2a8957 [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 Zhao6859d412020-06-15 17:16:04 -070033
34static int config_sys_node(const char* path, const char* value)
35{
36 int fd;
37 fd = open(path, O_RDWR);
38 if (fd < 0) {
Song Zhaoebb53bb2020-07-10 17:57:34 -070039 log_error("fail to open %s\n", path);
Song Zhao6859d412020-06-15 17:16:04 -070040 return -1;
41 }
42 if (write(fd, value, strlen(value)) != strlen(value)) {
Song Zhaoebb53bb2020-07-10 17:57:34 -070043 log_error("fail to write %s to %s\n", value, path);
Song Zhao6859d412020-06-15 17:16:04 -070044 close(fd);
45 return -1;
46 }
47 close(fd);
48
49 return 0;
50}
51
52static int get_sysfs_uint32(const char *path, uint32_t *value)
53{
54 int fd;
55 char valstr[64];
56 uint32_t val = 0;
57
58 fd = open(path, O_RDONLY);
59 if (fd >= 0) {
60 memset(valstr, 0, 64);
61 read(fd, valstr, 64 - 1);
62 valstr[strlen(valstr)] = '\0';
63 close(fd);
64 } else {
Song Zhaoebb53bb2020-07-10 17:57:34 -070065 log_error("unable to open file %s\n", path);
Song Zhao6859d412020-06-15 17:16:04 -070066 return -1;
67 }
68 if (sscanf(valstr, "0x%x", &val) < 1) {
Song Zhaoebb53bb2020-07-10 17:57:34 -070069 log_error("unable to get pts from: %s", valstr);
Song Zhao6859d412020-06-15 17:16:04 -070070 return -1;
71 }
72 *value = val;
73 return 0;
74}
75
Song Zhaobf35d4a2020-06-18 22:36:29 -070076int tsync_enable(int session, bool enable)
Song Zhao6859d412020-06-15 17:16:04 -070077{
78 const char *val;
79
80 if (enable)
81 val = "1";
82 else
83 val = "0";
Song Zhaoebb53bb2020-07-10 17:57:34 -070084 log_info("%s", val);
Song Zhaobf35d4a2020-06-18 22:36:29 -070085 return config_sys_node(TSYNC_ENABLE, val);
Song Zhao6859d412020-06-15 17:16:04 -070086}
87
88uint32_t tsync_get_pcr(int session)
89{
90 uint32_t pcr = 0;
91
92 get_sysfs_uint32(TSYNC_PCRSCR, &pcr);
93 return pcr;
94}
95
96//uint32_t tsync_get_vpts(int session);
97
98int tsync_send_video_start(int session, uint32_t vpts)
99{
100 char val[50];
101
102 snprintf(val, sizeof(val), "VIDEO_START:0x%x", vpts);
Song Zhaoebb53bb2020-07-10 17:57:34 -0700103 log_info("%s", val);
Song Zhao6859d412020-06-15 17:16:04 -0700104 return config_sys_node(TSYNC_EVENT, val);
105}
106
107int tsync_send_video_pause(int session, bool pause)
108{
109 const char *val;
110
111 if (pause)
112 val = "VIDEO_PAUSE:0x1";
113 else
114 val = "VIDEO_PAUSE:0x0";
Song Zhaoebb53bb2020-07-10 17:57:34 -0700115 log_info("%s", val);
Song Zhao6859d412020-06-15 17:16:04 -0700116 return config_sys_node(TSYNC_EVENT, val);
117}
118
119int tsync_send_video_disc(int session, uint32_t vpts)
120{
121 char val[50];
122
123 snprintf(val, sizeof(val), "VIDEO_TSTAMP_DISCONTINUITY:0x%x", vpts);
Song Zhaoebb53bb2020-07-10 17:57:34 -0700124 log_info("%s", val);
Song Zhao6859d412020-06-15 17:16:04 -0700125 return config_sys_node(TSYNC_EVENT, val);
126}
Song Zhaobf35d4a2020-06-18 22:36:29 -0700127
128int tsync_set_pcr(int session, uint32_t pcr)
129{
130 char val[20];
131
132 snprintf(val, sizeof(val), "%u", pcr);
133 config_sys_node(TSYNC_PCRSCR, val);
134 return 0;
135}
136
137static int video_device_ioctl(int ctl, int value)
138{
139 int video_fd;
140
141 video_fd = open(VIDEO_DEVICE, O_RDWR);
142 if (video_fd < 0) {
143 return -1;
144 }
145
Song Zhaoebb53bb2020-07-10 17:57:34 -0700146 ioctl(video_fd, ctl, value);
Song Zhaobf35d4a2020-06-18 22:36:29 -0700147
148 close(video_fd);
149
150 return 0;
151}
152
153int tsync_set_pts_inc_mode(int session, bool enable)
154{
155 return video_device_ioctl(AMSTREAM_IOC_SET_VSYNC_UPINT, enable);
156}
157
Song Zhao0adb3b52020-07-27 16:15:04 -0700158int tsync_set_video_sync_thres(int session, bool enable)
159{
160 return video_device_ioctl(AMSTREAM_IOC_SYNCTHRESH, enable);
161}
162
Song Zhaobf35d4a2020-06-18 22:36:29 -0700163int tsync_set_mode(int session, enum sync_mode mode)
164{
165 const char* val = NULL;
166 if (mode == AV_SYNC_MODE_VMASTER)
167 val = "0";
168 else if (mode == AV_SYNC_MODE_AMASTER)
169 val = "1";
170 else if (mode == AV_SYNC_MODE_PCR_MASTER)
171 val = "2";
172 return config_sys_node(TSYNC_MODE, val);
173}
174
Song Zhaoc83a9b82020-07-25 00:08:42 -0700175/* do not send VIDEO_START from video.c */
Song Zhaobf35d4a2020-06-18 22:36:29 -0700176int tsync_set_video_peek_mode(int session)
177{
Song Zhaoc83a9b82020-07-25 00:08:42 -0700178 log_info("set video peek");
Song Zhaobf35d4a2020-06-18 22:36:29 -0700179 return video_device_ioctl(AMSTREAM_IOC_SET_VIDEOPEEK, 0);
180}