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 | 6859d41 | 2020-06-15 17:16:04 -0700 | [diff] [blame] | 33 | |
| 34 | static 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 Zhao | ebb53bb | 2020-07-10 17:57:34 -0700 | [diff] [blame] | 39 | log_error("fail to open %s\n", path); |
Song Zhao | 6859d41 | 2020-06-15 17:16:04 -0700 | [diff] [blame] | 40 | return -1; |
| 41 | } |
| 42 | if (write(fd, value, strlen(value)) != strlen(value)) { |
Song Zhao | ebb53bb | 2020-07-10 17:57:34 -0700 | [diff] [blame] | 43 | log_error("fail to write %s to %s\n", value, path); |
Song Zhao | 6859d41 | 2020-06-15 17:16:04 -0700 | [diff] [blame] | 44 | close(fd); |
| 45 | return -1; |
| 46 | } |
| 47 | close(fd); |
| 48 | |
| 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | static 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 Zhao | ebb53bb | 2020-07-10 17:57:34 -0700 | [diff] [blame] | 65 | log_error("unable to open file %s\n", path); |
Song Zhao | 6859d41 | 2020-06-15 17:16:04 -0700 | [diff] [blame] | 66 | return -1; |
| 67 | } |
| 68 | if (sscanf(valstr, "0x%x", &val) < 1) { |
Song Zhao | ebb53bb | 2020-07-10 17:57:34 -0700 | [diff] [blame] | 69 | log_error("unable to get pts from: %s", valstr); |
Song Zhao | 6859d41 | 2020-06-15 17:16:04 -0700 | [diff] [blame] | 70 | return -1; |
| 71 | } |
| 72 | *value = val; |
| 73 | return 0; |
| 74 | } |
| 75 | |
Song Zhao | bf35d4a | 2020-06-18 22:36:29 -0700 | [diff] [blame] | 76 | int tsync_enable(int session, bool enable) |
Song Zhao | 6859d41 | 2020-06-15 17:16:04 -0700 | [diff] [blame] | 77 | { |
| 78 | const char *val; |
| 79 | |
| 80 | if (enable) |
| 81 | val = "1"; |
| 82 | else |
| 83 | val = "0"; |
Song Zhao | ebb53bb | 2020-07-10 17:57:34 -0700 | [diff] [blame] | 84 | log_info("%s", val); |
Song Zhao | bf35d4a | 2020-06-18 22:36:29 -0700 | [diff] [blame] | 85 | return config_sys_node(TSYNC_ENABLE, val); |
Song Zhao | 6859d41 | 2020-06-15 17:16:04 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | uint32_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 | |
| 98 | int 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 Zhao | ebb53bb | 2020-07-10 17:57:34 -0700 | [diff] [blame] | 103 | log_info("%s", val); |
Song Zhao | 6859d41 | 2020-06-15 17:16:04 -0700 | [diff] [blame] | 104 | return config_sys_node(TSYNC_EVENT, val); |
| 105 | } |
| 106 | |
| 107 | int 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 Zhao | ebb53bb | 2020-07-10 17:57:34 -0700 | [diff] [blame] | 115 | log_info("%s", val); |
Song Zhao | 6859d41 | 2020-06-15 17:16:04 -0700 | [diff] [blame] | 116 | return config_sys_node(TSYNC_EVENT, val); |
| 117 | } |
| 118 | |
| 119 | int 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 Zhao | ebb53bb | 2020-07-10 17:57:34 -0700 | [diff] [blame] | 124 | log_info("%s", val); |
Song Zhao | 6859d41 | 2020-06-15 17:16:04 -0700 | [diff] [blame] | 125 | return config_sys_node(TSYNC_EVENT, val); |
| 126 | } |
Song Zhao | bf35d4a | 2020-06-18 22:36:29 -0700 | [diff] [blame] | 127 | |
| 128 | int 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 | |
| 137 | static 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 Zhao | ebb53bb | 2020-07-10 17:57:34 -0700 | [diff] [blame] | 146 | ioctl(video_fd, ctl, value); |
Song Zhao | bf35d4a | 2020-06-18 22:36:29 -0700 | [diff] [blame] | 147 | |
| 148 | close(video_fd); |
| 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | int tsync_set_pts_inc_mode(int session, bool enable) |
| 154 | { |
| 155 | return video_device_ioctl(AMSTREAM_IOC_SET_VSYNC_UPINT, enable); |
| 156 | } |
| 157 | |
Song Zhao | 0adb3b5 | 2020-07-27 16:15:04 -0700 | [diff] [blame^] | 158 | int tsync_set_video_sync_thres(int session, bool enable) |
| 159 | { |
| 160 | return video_device_ioctl(AMSTREAM_IOC_SYNCTHRESH, enable); |
| 161 | } |
| 162 | |
Song Zhao | bf35d4a | 2020-06-18 22:36:29 -0700 | [diff] [blame] | 163 | int 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 Zhao | c83a9b8 | 2020-07-25 00:08:42 -0700 | [diff] [blame] | 175 | /* do not send VIDEO_START from video.c */ |
Song Zhao | bf35d4a | 2020-06-18 22:36:29 -0700 | [diff] [blame] | 176 | int tsync_set_video_peek_mode(int session) |
| 177 | { |
Song Zhao | c83a9b8 | 2020-07-25 00:08:42 -0700 | [diff] [blame] | 178 | log_info("set video peek"); |
Song Zhao | bf35d4a | 2020-06-18 22:36:29 -0700 | [diff] [blame] | 179 | return video_device_ioctl(AMSTREAM_IOC_SET_VIDEOPEEK, 0); |
| 180 | } |