Song Zhao | 6859d41 | 2020-06-15 17:16:04 -0700 | [diff] [blame] | 1 | /* |
| 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 Zhao | bf35d4a | 2020-06-18 22:36:29 -0700 | [diff] [blame] | 19 | #include <sys/ioctl.h> |
Song Zhao | 6859d41 | 2020-06-15 17:16:04 -0700 | [diff] [blame] | 20 | #include "tsync.h" |
Song Zhao | ebb53bb | 2020-07-10 17:57:34 -0700 | [diff] [blame] | 21 | #include "aml_avsync_log.h" |
Song Zhao | 6859d41 | 2020-06-15 17:16:04 -0700 | [diff] [blame] | 22 | |
Song Zhao | bf35d4a | 2020-06-18 22:36:29 -0700 | [diff] [blame] | 23 | #define VIDEO_DEVICE "/dev/amvideo" |
Song Zhao | 6859d41 | 2020-06-15 17:16:04 -0700 | [diff] [blame] | 24 | #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 Zhao | bf35d4a | 2020-06-18 22:36:29 -0700 | [diff] [blame] | 27 | #define TSYNC_MODE "/sys/class/tsync/mode" |
| 28 | |
| 29 | #define _A_M 'S' |
Song Zhao | 0adb3b5 | 2020-07-27 16:15:04 -0700 | [diff] [blame] | 30 | #define AMSTREAM_IOC_SYNCTHRESH _IOW((_A_M), 0x19, int) |
Song Zhao | bf35d4a | 2020-06-18 22:36:29 -0700 | [diff] [blame] | 31 | #define AMSTREAM_IOC_SET_VSYNC_UPINT _IOW((_A_M), 0x89, int) |
| 32 | #define AMSTREAM_IOC_SET_VIDEOPEEK _IOW(_A_M, 0xbf, unsigned int) |
Song Zhao | 58d3a1c | 2020-08-04 16:51:30 -0700 | [diff] [blame^] | 33 | #define AMSTREAM_IOC_SET_NO_VIDEO_STOP _IOW(_A_M, 0xf5, unsigned int) |
Song Zhao | 6859d41 | 2020-06-15 17:16:04 -0700 | [diff] [blame] | 34 | |
| 35 | static 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 Zhao | ebb53bb | 2020-07-10 17:57:34 -0700 | [diff] [blame] | 40 | log_error("fail to open %s\n", path); |
Song Zhao | 6859d41 | 2020-06-15 17:16:04 -0700 | [diff] [blame] | 41 | return -1; |
| 42 | } |
| 43 | if (write(fd, value, strlen(value)) != strlen(value)) { |
Song Zhao | ebb53bb | 2020-07-10 17:57:34 -0700 | [diff] [blame] | 44 | log_error("fail to write %s to %s\n", value, path); |
Song Zhao | 6859d41 | 2020-06-15 17:16:04 -0700 | [diff] [blame] | 45 | close(fd); |
| 46 | return -1; |
| 47 | } |
| 48 | close(fd); |
| 49 | |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | static 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 Zhao | ebb53bb | 2020-07-10 17:57:34 -0700 | [diff] [blame] | 66 | log_error("unable to open file %s\n", path); |
Song Zhao | 6859d41 | 2020-06-15 17:16:04 -0700 | [diff] [blame] | 67 | return -1; |
| 68 | } |
| 69 | if (sscanf(valstr, "0x%x", &val) < 1) { |
Song Zhao | ebb53bb | 2020-07-10 17:57:34 -0700 | [diff] [blame] | 70 | log_error("unable to get pts from: %s", valstr); |
Song Zhao | 6859d41 | 2020-06-15 17:16:04 -0700 | [diff] [blame] | 71 | return -1; |
| 72 | } |
| 73 | *value = val; |
| 74 | return 0; |
| 75 | } |
| 76 | |
Song Zhao | bf35d4a | 2020-06-18 22:36:29 -0700 | [diff] [blame] | 77 | int tsync_enable(int session, bool enable) |
Song Zhao | 6859d41 | 2020-06-15 17:16:04 -0700 | [diff] [blame] | 78 | { |
| 79 | const char *val; |
| 80 | |
| 81 | if (enable) |
| 82 | val = "1"; |
| 83 | else |
| 84 | val = "0"; |
Song Zhao | ebb53bb | 2020-07-10 17:57:34 -0700 | [diff] [blame] | 85 | log_info("%s", val); |
Song Zhao | bf35d4a | 2020-06-18 22:36:29 -0700 | [diff] [blame] | 86 | return config_sys_node(TSYNC_ENABLE, val); |
Song Zhao | 6859d41 | 2020-06-15 17:16:04 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | uint32_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 | |
| 99 | int 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 Zhao | ebb53bb | 2020-07-10 17:57:34 -0700 | [diff] [blame] | 104 | log_info("%s", val); |
Song Zhao | 6859d41 | 2020-06-15 17:16:04 -0700 | [diff] [blame] | 105 | return config_sys_node(TSYNC_EVENT, val); |
| 106 | } |
| 107 | |
| 108 | int 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 Zhao | ebb53bb | 2020-07-10 17:57:34 -0700 | [diff] [blame] | 116 | log_info("%s", val); |
Song Zhao | 6859d41 | 2020-06-15 17:16:04 -0700 | [diff] [blame] | 117 | return config_sys_node(TSYNC_EVENT, val); |
| 118 | } |
| 119 | |
| 120 | int 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 Zhao | ebb53bb | 2020-07-10 17:57:34 -0700 | [diff] [blame] | 125 | log_info("%s", val); |
Song Zhao | 6859d41 | 2020-06-15 17:16:04 -0700 | [diff] [blame] | 126 | return config_sys_node(TSYNC_EVENT, val); |
| 127 | } |
Song Zhao | bf35d4a | 2020-06-18 22:36:29 -0700 | [diff] [blame] | 128 | |
| 129 | int 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 | |
| 138 | static 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 Zhao | ebb53bb | 2020-07-10 17:57:34 -0700 | [diff] [blame] | 147 | ioctl(video_fd, ctl, value); |
Song Zhao | bf35d4a | 2020-06-18 22:36:29 -0700 | [diff] [blame] | 148 | |
| 149 | close(video_fd); |
| 150 | |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | int tsync_set_pts_inc_mode(int session, bool enable) |
| 155 | { |
| 156 | return video_device_ioctl(AMSTREAM_IOC_SET_VSYNC_UPINT, enable); |
| 157 | } |
| 158 | |
Song Zhao | 0adb3b5 | 2020-07-27 16:15:04 -0700 | [diff] [blame] | 159 | int tsync_set_video_sync_thres(int session, bool enable) |
| 160 | { |
| 161 | return video_device_ioctl(AMSTREAM_IOC_SYNCTHRESH, enable); |
| 162 | } |
| 163 | |
Song Zhao | bf35d4a | 2020-06-18 22:36:29 -0700 | [diff] [blame] | 164 | int 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 Zhao | c83a9b8 | 2020-07-25 00:08:42 -0700 | [diff] [blame] | 176 | /* do not send VIDEO_START from video.c */ |
Song Zhao | bf35d4a | 2020-06-18 22:36:29 -0700 | [diff] [blame] | 177 | int tsync_set_video_peek_mode(int session) |
| 178 | { |
Song Zhao | c83a9b8 | 2020-07-25 00:08:42 -0700 | [diff] [blame] | 179 | log_info("set video peek"); |
Song Zhao | bf35d4a | 2020-06-18 22:36:29 -0700 | [diff] [blame] | 180 | return video_device_ioctl(AMSTREAM_IOC_SET_VIDEOPEEK, 0); |
| 181 | } |
Song Zhao | 58d3a1c | 2020-08-04 16:51:30 -0700 | [diff] [blame^] | 182 | |
| 183 | /* Control VIDEO_STOP from video.c */ |
| 184 | int 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 | } |