blob: d6383a282c380cd7b648b698bb112d6e8969de64 [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>
19#include "tsync.h"
20
21#define TSYNC_ENABLE "/sys/class/tsync/enable"
22#define TSYNC_PCRSCR "/sys/class/tsync/pts_pcrscr"
23#define TSYNC_EVENT "/sys/class/tsync/event"
24
25static int config_sys_node(const char* path, const char* value)
26{
27 int fd;
28 fd = open(path, O_RDWR);
29 if (fd < 0) {
30 printf("fail to open %s\n", path);
31 return -1;
32 }
33 if (write(fd, value, strlen(value)) != strlen(value)) {
34 printf("fail to write %s to %s\n", value, path);
35 close(fd);
36 return -1;
37 }
38 close(fd);
39
40 return 0;
41}
42
43static int get_sysfs_uint32(const char *path, uint32_t *value)
44{
45 int fd;
46 char valstr[64];
47 uint32_t val = 0;
48
49 fd = open(path, O_RDONLY);
50 if (fd >= 0) {
51 memset(valstr, 0, 64);
52 read(fd, valstr, 64 - 1);
53 valstr[strlen(valstr)] = '\0';
54 close(fd);
55 } else {
56 printf("unable to open file %s\n", path);
57 return -1;
58 }
59 if (sscanf(valstr, "0x%x", &val) < 1) {
60 printf("unable to get pts from: %s", valstr);
61 return -1;
62 }
63 *value = val;
64 return 0;
65}
66
67void tsync_enable(int session, bool enable)
68{
69 const char *val;
70
71 if (enable)
72 val = "1";
73 else
74 val = "0";
75 config_sys_node(TSYNC_ENABLE, val);
76}
77
78uint32_t tsync_get_pcr(int session)
79{
80 uint32_t pcr = 0;
81
82 get_sysfs_uint32(TSYNC_PCRSCR, &pcr);
83 return pcr;
84}
85
86//uint32_t tsync_get_vpts(int session);
87
88int tsync_send_video_start(int session, uint32_t vpts)
89{
90 char val[50];
91
92 snprintf(val, sizeof(val), "VIDEO_START:0x%x", vpts);
93 return config_sys_node(TSYNC_EVENT, val);
94}
95
96int tsync_send_video_pause(int session, bool pause)
97{
98 const char *val;
99
100 if (pause)
101 val = "VIDEO_PAUSE:0x1";
102 else
103 val = "VIDEO_PAUSE:0x0";
104 return config_sys_node(TSYNC_EVENT, val);
105}
106
107int tsync_send_video_disc(int session, uint32_t vpts)
108{
109 char val[50];
110
111 snprintf(val, sizeof(val), "VIDEO_TSTAMP_DISCONTINUITY:0x%x", vpts);
112 return config_sys_node(TSYNC_EVENT, val);
113}