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: test for avsync |
| 8 | * |
| 9 | * Author: song.zhao@amlogic.com |
| 10 | */ |
| 11 | #include <errno.h> |
| 12 | #include <sys/types.h> |
| 13 | #include <sys/stat.h> |
| 14 | #include <fcntl.h> |
| 15 | #include <stdbool.h> |
| 16 | #include <stdlib.h> |
| 17 | #include <stdio.h> |
| 18 | #include <string.h> |
| 19 | #include <unistd.h> |
| 20 | |
| 21 | #include "aml_avsync.h" |
| 22 | #include "aml_avsync_log.h" |
| 23 | |
| 24 | #define TSYNC_MODE "/sys/class/tsync/mode" |
| 25 | #define TSYNC_PCRSCR "/sys/class/tsync/pts_pcrscr" |
| 26 | #define VPTS_INC_UPINT "/sys/class/video/vsync_pts_inc_upint" |
| 27 | |
| 28 | #define FRAME_NUM 32 |
| 29 | #define PATTERN_32_DURATION 3750 |
| 30 | #define PATTERN_22_DURATION 3000 |
| 31 | |
| 32 | static struct vframe frame[FRAME_NUM]; |
| 33 | static int frame_received; |
| 34 | |
| 35 | static int sysfs_set_sysfs_str(const char *path, const char *val) |
| 36 | { |
| 37 | int fd; |
| 38 | fd = open(path, O_CREAT | O_RDWR | O_TRUNC, 0644); |
| 39 | if (fd >= 0) { |
| 40 | if(write(fd, val, strlen(val)) != strlen(val)) |
| 41 | log_error("write fail"); |
| 42 | close(fd); |
| 43 | return 0; |
| 44 | } else { |
| 45 | log_error("test: unable to open file %s,err: %s", path, strerror(errno)); |
| 46 | } |
| 47 | return -1; |
| 48 | } |
| 49 | |
| 50 | static void frame_free(struct vframe * frame) |
| 51 | { |
| 52 | log_info("free %d\n", (int)frame->private); |
| 53 | frame_received++; |
| 54 | } |
| 55 | |
| 56 | static void test(int refresh_rate, int pts_interval, struct vframe* frame) |
| 57 | { |
| 58 | int i = 0; |
| 59 | void* handle; |
| 60 | int sleep_us = 1000000/refresh_rate; |
| 61 | struct vframe *last_frame = NULL, *pop_frame; |
| 62 | |
| 63 | /* vmaster and reset pcr to 0 */ |
| 64 | sysfs_set_sysfs_str(TSYNC_MODE, "0"); |
| 65 | sysfs_set_sysfs_str(TSYNC_PCRSCR, "0"); |
| 66 | sysfs_set_sysfs_str(VPTS_INC_UPINT, "1"); |
| 67 | |
| 68 | handle = av_sync_create(0, 2, 2, 90000/refresh_rate); |
| 69 | frame = (struct vframe*)calloc(FRAME_NUM, sizeof(*frame)); |
| 70 | if (!frame) { |
| 71 | log_error("oom"); |
| 72 | exit(1); |
| 73 | } |
| 74 | |
| 75 | /* push max frames */ |
| 76 | while (i < FRAME_NUM) { |
| 77 | frame[i].private = (void *)i; |
| 78 | frame[i].pts = PATTERN_22_DURATION * i; |
| 79 | frame[i].duration = PATTERN_22_DURATION; |
| 80 | frame[i].free = frame_free; |
| 81 | if (av_sync_push_frame(handle, &frame[i])) { |
| 82 | log_error("queue %d fail", i); |
| 83 | break; |
| 84 | } |
| 85 | log_info("queue %d", i); |
| 86 | i++; |
| 87 | } |
| 88 | |
| 89 | i = 0; |
| 90 | while (frame_received < FRAME_NUM) { |
| 91 | usleep(sleep_us); |
| 92 | pop_frame = av_sync_pop_frame(handle); |
| 93 | if (pop_frame) |
| 94 | log_info("pop frame %02d", (int)pop_frame->private); |
| 95 | if (pop_frame != last_frame) { |
| 96 | i++; |
| 97 | last_frame = pop_frame; |
| 98 | frame_received++; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | frame_received = 0; |
| 103 | av_sync_destroy(handle); |
| 104 | } |
| 105 | |
| 106 | int main(int argc, const char** argv) |
| 107 | { |
| 108 | log_set_level(LOG_TRACE); |
| 109 | |
| 110 | log_info("\n----------------22 start------------\n"); |
| 111 | test(60, PATTERN_22_DURATION, frame); |
| 112 | log_info("\n----------------22 end--------------\n"); |
| 113 | log_info("\n----------------32 start------------\n"); |
| 114 | test(60, PATTERN_32_DURATION, frame); |
| 115 | log_info("\n----------------32 start------------\n"); |
| 116 | log_info("\n----------------41 start------------\n"); |
| 117 | test(30, PATTERN_32_DURATION, frame); |
| 118 | log_info("\n----------------41 end--------------\n"); |
| 119 | log_info("\n----------------11 start------------\n"); |
| 120 | test(30, PATTERN_22_DURATION, frame); |
| 121 | log_info("\n----------------11 end--------------\n"); |
| 122 | |
| 123 | return 0; |
| 124 | } |