Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 1 | #include <stdio.h> |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 2 | #include <unistd.h> |
| 3 | #include <stdlib.h> |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 4 | #include <pthread.h> |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 5 | #include <string.h> |
Pengfei Liu | 47ed6c9 | 2020-01-17 11:23:41 +0800 | [diff] [blame] | 6 | #include "dvr_types.h" |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 7 | #include "dvr_record.h" |
| 8 | #include "dvr_crypto.h" |
Gong Ke | 2a0ebbe | 2021-05-25 15:22:50 +0800 | [diff] [blame] | 9 | #include "dvb_utils.h" |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 10 | #include "record_device.h" |
| 11 | #include "segment.h" |
pengfei.liu | 567d6d8 | 2020-04-17 16:48:59 +0800 | [diff] [blame] | 12 | #include <sys/time.h> |
Wentao MA | 361eaac | 2023-03-21 13:12:28 +0800 | [diff] [blame^] | 13 | #include <sys/prctl.h> |
Yahui Han | 1fbf329 | 2021-11-08 18:17:19 +0800 | [diff] [blame] | 14 | #include "am_crypt.h" |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 15 | |
hualing chen | 3092e1c | 2022-01-21 20:02:21 +0800 | [diff] [blame] | 16 | #define CHECK_PTS_MAX_COUNT (20) |
| 17 | |
hualing chen | c7aa4c8 | 2021-02-03 15:41:37 +0800 | [diff] [blame] | 18 | //#define DEBUG_PERFORMANCE |
Yahui Han | 1099389 | 2021-11-02 14:27:33 +0800 | [diff] [blame] | 19 | #define MAX_DVR_RECORD_SESSION_COUNT 4 |
hualing chen | 266b950 | 2020-04-04 17:39:39 +0800 | [diff] [blame] | 20 | #define RECORD_BLOCK_SIZE (256 * 1024) |
Yahui Han | ce15e9c | 2020-12-08 18:08:32 +0800 | [diff] [blame] | 21 | #define NEW_DEVICE_RECORD_BLOCK_SIZE (1024 * 188) |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 22 | |
| 23 | /**\brief DVR index file type*/ |
| 24 | typedef enum { |
| 25 | DVR_INDEX_TYPE_PCR, /**< DVR index file use pcr*/ |
| 26 | DVR_INDEX_TYPE_LOCAL_CLOCK, /**< DVR index file use local clock*/ |
| 27 | DVR_INDEX_TYPE_INVALID /**< DVR index file type invalid type*/ |
| 28 | } DVR_IndexType_t; |
| 29 | |
| 30 | /**\brief DVR VOD context*/ |
| 31 | typedef struct { |
| 32 | pthread_mutex_t mutex; /**< VOD mutex lock*/ |
| 33 | pthread_cond_t cond; /**< VOD condition*/ |
| 34 | void *buffer; /**< VOD buffer*/ |
| 35 | uint32_t buf_len; /**< VOD buffer len*/ |
| 36 | } DVR_VodContext_t; |
| 37 | |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 38 | /**\brief DVR record secure mode buffer*/ |
| 39 | typedef struct { |
| 40 | uint32_t addr; /**< Secure mode record buffer address*/ |
| 41 | uint32_t len; /**< Secure mode record buffer length*/ |
| 42 | } DVR_SecureBuffer_t; |
| 43 | |
hualing chen | f986740 | 2020-09-23 17:06:20 +0800 | [diff] [blame] | 44 | /**\brief DVR record new dmx secure mode buffer*/ |
| 45 | typedef struct { |
| 46 | uint32_t buf_start; /**< Secure mode record buffer address*/ |
| 47 | uint32_t buf_end; /**< Secure mode record buffer length*/ |
| 48 | uint32_t data_start; /**< Secure mode record buffer address*/ |
| 49 | uint32_t data_end; /**< Secure mode record buffer length*/ |
| 50 | } DVR_NewDmxSecureBuffer_t; |
| 51 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 52 | /**\brief DVR record context*/ |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 53 | typedef struct { |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 54 | pthread_t thread; /**< DVR thread handle*/ |
| 55 | Record_DeviceHandle_t dev_handle; /**< DVR device handle*/ |
| 56 | Segment_Handle_t segment_handle; /**< DVR segment handle*/ |
| 57 | DVR_RecordState_t state; /**< DVR record state*/ |
| 58 | char location[DVR_MAX_LOCATION_SIZE]; /**< DVR record file location*/ |
| 59 | DVR_RecordSegmentStartParams_t segment_params; /**< DVR record start parameters*/ |
| 60 | DVR_RecordSegmentInfo_t segment_info; /**< DVR record current segment info*/ |
Wentao MA | 270dc0f | 2022-08-23 13:17:26 +0800 | [diff] [blame] | 61 | size_t notification_size; /**< DVR record notification size*/ |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 62 | DVR_RecordEventFunction_t event_notify_fn; /**< DVR record event notify function*/ |
| 63 | void *event_userdata; /**< DVR record event userdata*/ |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 64 | //DVR_VodContext_t vod; /**< DVR record vod context*/ |
| 65 | int is_vod; /**< Indicate current mode is VOD record mode*/ |
pengfei.liu | 27cc4ec | 2020-04-03 16:28:16 +0800 | [diff] [blame] | 66 | DVR_CryptoFunction_t enc_func; /**< Encrypt function*/ |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 67 | void *enc_userdata; /**< Encrypt userdata*/ |
| 68 | int is_secure_mode; /**< Record session run in secure pipeline */ |
Yahui Han | 1fbf329 | 2021-11-08 18:17:19 +0800 | [diff] [blame] | 69 | void *cryptor; /**< Cryptor for encrypted PVR on FTA.*/ |
hualing chen | 2615aa8 | 2020-04-02 21:32:51 +0800 | [diff] [blame] | 70 | size_t last_send_size; /**< Last send notify segment size */ |
pengfei.liu | fda2a97 | 2020-04-09 14:47:15 +0800 | [diff] [blame] | 71 | uint32_t block_size; /**< DVR record block size */ |
hualing chen | f986740 | 2020-09-23 17:06:20 +0800 | [diff] [blame] | 72 | DVR_Bool_t is_new_dmx; /**< DVR is used new dmx driver */ |
hualing chen | 40accc3 | 2021-04-21 15:58:09 +0800 | [diff] [blame] | 73 | int index_type; /**< DVR is used pcr or local time */ |
wentao.ma | 35a69d4 | 2022-03-10 18:08:40 +0800 | [diff] [blame] | 74 | DVR_Bool_t force_sysclock; /**< If ture, force to use system clock as PVR index time source. If false, libdvr can determine index time source based on actual situation*/ |
hualing chen | 94d8dd2 | 2021-12-29 15:13:43 +0800 | [diff] [blame] | 75 | uint64_t pts; /**< The newest pcr or local time */ |
hualing chen | 3092e1c | 2022-01-21 20:02:21 +0800 | [diff] [blame] | 76 | int check_pts_count; /**< The check count of pts */ |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 77 | int check_no_pts_count; /**< The check count of no pts */ |
Wentao MA | 270dc0f | 2022-08-23 13:17:26 +0800 | [diff] [blame] | 78 | int notification_time; /**< DVR record notification time*/ |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 79 | time_t last_send_time; /**< Last send notify segment duration */ |
Wentao MA | eeffdb0 | 2022-06-27 16:34:35 +0800 | [diff] [blame] | 80 | loff_t guarded_segment_size; /**< Guarded segment size in bytes. Libdvr will be forcely stopped to write anymore if current segment reaches this size*/ |
Yahui Han | 4577db8 | 2022-07-05 17:46:19 +0800 | [diff] [blame] | 81 | size_t secbuf_size; /**< DVR record secure buffer length*/ |
Wentao MA | f407203 | 2022-06-30 13:50:45 +0800 | [diff] [blame] | 82 | DVR_Bool_t discard_coming_data; /**< Whether to discard subsequent recording data due to exceeding total size limit too much.*/ |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 83 | } DVR_RecordContext_t; |
| 84 | |
Gong Ke | 2a0ebbe | 2021-05-25 15:22:50 +0800 | [diff] [blame] | 85 | extern ssize_t record_device_read_ext(Record_DeviceHandle_t handle, size_t *buf, size_t *len); |
| 86 | |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 87 | static DVR_RecordContext_t record_ctx[MAX_DVR_RECORD_SESSION_COUNT] = { |
| 88 | { |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 89 | .state = DVR_RECORD_STATE_CLOSED |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 90 | }, |
| 91 | { |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 92 | .state = DVR_RECORD_STATE_CLOSED |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 93 | } |
| 94 | }; |
| 95 | |
Zhiqiang Han | 51646a0 | 2020-04-05 18:43:22 +0800 | [diff] [blame] | 96 | static int record_is_valid_pid(DVR_RecordContext_t *p_ctx, int pid) |
| 97 | { |
| 98 | int i; |
| 99 | |
| 100 | for (i = 0; i < p_ctx->segment_info.nb_pids; i++) { |
| 101 | if (pid == p_ctx->segment_info.pids[i].pid) |
| 102 | return 1; |
| 103 | } |
| 104 | return 0; |
| 105 | } |
| 106 | |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 107 | static int record_save_pcr(DVR_RecordContext_t *p_ctx, uint8_t *buf, loff_t pos) |
| 108 | { |
| 109 | uint8_t *p = buf; |
| 110 | int len; |
| 111 | uint8_t afc; |
| 112 | uint64_t pcr = 0; |
| 113 | int has_pcr = 0; |
| 114 | int pid; |
| 115 | int adp_field_len; |
| 116 | |
| 117 | pid = ((p[1] & 0x1f) << 8) | p[2]; |
Zhiqiang Han | 51646a0 | 2020-04-05 18:43:22 +0800 | [diff] [blame] | 118 | if (pid == 0x1fff || !record_is_valid_pid(p_ctx, pid)) |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 119 | return has_pcr; |
| 120 | |
| 121 | //scramble = p[3] >> 6; |
| 122 | //cc = p[3] & 0x0f; |
| 123 | afc = (p[3] >> 4) & 0x03; |
| 124 | |
| 125 | p += 4; |
| 126 | len = 184; |
| 127 | |
| 128 | if (afc & 2) { |
| 129 | adp_field_len = p[0]; |
| 130 | /* Skip adaptation len */ |
| 131 | p++; |
| 132 | len--; |
| 133 | /* Parse pcr field, see 13818 spec table I-2-6,adaptation_field */ |
hualing chen | 5605eed | 2020-05-26 18:18:06 +0800 | [diff] [blame] | 134 | if (p[0] & 0x10 && len >= 6 && adp_field_len >= 6) { |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 135 | /* get pcr value,pcr is 33bit value */ |
| 136 | pcr = (((uint64_t)(p[1])) << 25) |
| 137 | | (((uint64_t)p[2]) << 17) |
| 138 | | (((uint64_t)(p[3])) << 9) |
| 139 | | (((uint64_t)p[4]) << 1) |
| 140 | | ((((uint64_t)p[5]) & 0x80) >> 7); |
| 141 | has_pcr = 1; |
| 142 | } |
| 143 | |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 144 | len -= adp_field_len; |
| 145 | |
| 146 | if (len < 0) { |
Wentao MA | 96f6896 | 2022-06-15 19:45:35 +0800 | [diff] [blame] | 147 | DVR_INFO("parser pcr: illegal adaptation field length"); |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 148 | return 0; |
| 149 | } |
| 150 | } |
| 151 | |
hualing chen | 40accc3 | 2021-04-21 15:58:09 +0800 | [diff] [blame] | 152 | if (has_pcr && p_ctx->index_type == DVR_INDEX_TYPE_PCR) { |
hualing chen | 94d8dd2 | 2021-12-29 15:13:43 +0800 | [diff] [blame] | 153 | //save newest pcr |
hualing chen | 3092e1c | 2022-01-21 20:02:21 +0800 | [diff] [blame] | 154 | if (p_ctx->pts == pcr/90 && |
| 155 | p_ctx->check_pts_count < CHECK_PTS_MAX_COUNT) { |
hualing chen | 3092e1c | 2022-01-21 20:02:21 +0800 | [diff] [blame] | 156 | p_ctx->check_pts_count ++; |
| 157 | } |
hualing chen | 94d8dd2 | 2021-12-29 15:13:43 +0800 | [diff] [blame] | 158 | p_ctx->pts = pcr/90; |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 159 | segment_update_pts(p_ctx->segment_handle, pcr/90, pos); |
| 160 | } |
| 161 | return has_pcr; |
| 162 | } |
| 163 | |
| 164 | static int record_do_pcr_index(DVR_RecordContext_t *p_ctx, uint8_t *buf, int len) |
| 165 | { |
| 166 | uint8_t *p = buf; |
| 167 | int left = len; |
| 168 | loff_t pos; |
| 169 | int has_pcr = 0; |
| 170 | |
| 171 | pos = segment_tell_position(p_ctx->segment_handle); |
hualing chen | 3e0d3a4 | 2022-01-25 17:22:01 +0800 | [diff] [blame] | 172 | if (pos >= len) { |
| 173 | pos = pos - len; |
| 174 | } |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 175 | while (left >= 188) { |
| 176 | if (*p == 0x47) { |
| 177 | has_pcr |= record_save_pcr(p_ctx, p, pos); |
| 178 | p += 188; |
| 179 | left -= 188; |
| 180 | pos += 188; |
| 181 | } else { |
| 182 | p++; |
| 183 | left --; |
| 184 | pos++; |
| 185 | } |
| 186 | } |
| 187 | return has_pcr; |
| 188 | } |
| 189 | |
pengfei.liu | 567d6d8 | 2020-04-17 16:48:59 +0800 | [diff] [blame] | 190 | static int get_diff_time(struct timeval start_tv, struct timeval end_tv) |
| 191 | { |
| 192 | return end_tv.tv_sec * 1000 + end_tv.tv_usec / 1000 - start_tv.tv_sec * 1000 - start_tv.tv_usec / 1000; |
| 193 | } |
| 194 | |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 195 | void *record_thread(void *arg) |
| 196 | { |
| 197 | DVR_RecordContext_t *p_ctx = (DVR_RecordContext_t *)arg; |
| 198 | ssize_t len; |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 199 | uint8_t *buf, *buf_out; |
pengfei.liu | fda2a97 | 2020-04-09 14:47:15 +0800 | [diff] [blame] | 200 | uint32_t block_size = p_ctx->block_size; |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 201 | loff_t pos = 0; |
Wentao MA | 139fc61 | 2022-08-29 14:10:07 +0800 | [diff] [blame] | 202 | int ret = DVR_SUCCESS; |
Wentao MA | 270dc0f | 2022-08-23 13:17:26 +0800 | [diff] [blame] | 203 | struct timespec start_ts, end_ts, start_no_pcr_ts, end_no_pcr_ts; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 204 | DVR_RecordStatus_t record_status; |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 205 | int has_pcr; |
hualing chen | 3092e1c | 2022-01-21 20:02:21 +0800 | [diff] [blame] | 206 | int pcr_rec_len = 0; |
Wentao MA | eeffdb0 | 2022-06-27 16:34:35 +0800 | [diff] [blame] | 207 | DVR_Bool_t guarded_size_exceeded = DVR_FALSE; |
hualing chen | 40accc3 | 2021-04-21 15:58:09 +0800 | [diff] [blame] | 208 | |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 209 | time_t pre_time = 0; |
hualing chen | 2932d37 | 2020-04-29 13:44:00 +0800 | [diff] [blame] | 210 | #define DVR_STORE_INFO_TIME (400) |
Wentao MA | 139fc61 | 2022-08-29 14:10:07 +0800 | [diff] [blame] | 211 | DVR_SecureBuffer_t secure_buf = {0,0}; |
hualing chen | f986740 | 2020-09-23 17:06:20 +0800 | [diff] [blame] | 212 | DVR_NewDmxSecureBuffer_t new_dmx_secure_buf; |
hualing chen | a5f0322 | 2021-12-02 11:22:35 +0800 | [diff] [blame] | 213 | int first_read = 0; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 214 | |
Wentao MA | 361eaac | 2023-03-21 13:12:28 +0800 | [diff] [blame^] | 215 | prctl(PR_SET_NAME,"DvrRecording"); |
| 216 | |
wentao.ma | 35a69d4 | 2022-03-10 18:08:40 +0800 | [diff] [blame] | 217 | // Force to use LOCAL_CLOCK as index type if force_sysclock is on. Please |
| 218 | // refer to SWPL-75327 |
| 219 | if (p_ctx->force_sysclock) |
| 220 | p_ctx->index_type = DVR_INDEX_TYPE_LOCAL_CLOCK; |
shenghui.geng | bec6a46 | 2023-01-12 15:21:02 +0800 | [diff] [blame] | 221 | else |
| 222 | p_ctx->index_type = DVR_INDEX_TYPE_INVALID; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 223 | buf = (uint8_t *)malloc(block_size); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 224 | if (!buf) { |
Wentao MA | 96f6896 | 2022-06-15 19:45:35 +0800 | [diff] [blame] | 225 | DVR_INFO("%s, malloc failed", __func__); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 226 | return NULL; |
| 227 | } |
| 228 | |
Yahui Han | 4577db8 | 2022-07-05 17:46:19 +0800 | [diff] [blame] | 229 | if (p_ctx->is_secure_mode) { |
| 230 | buf_out = (uint8_t *)malloc(p_ctx->secbuf_size + 188); |
| 231 | } else { |
| 232 | buf_out = (uint8_t *)malloc(block_size + 188); |
| 233 | } |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 234 | if (!buf_out) { |
Wentao MA | 96f6896 | 2022-06-15 19:45:35 +0800 | [diff] [blame] | 235 | DVR_INFO("%s, malloc failed", __func__); |
Pengfei Liu | faf38e4 | 2020-05-22 00:28:02 +0800 | [diff] [blame] | 236 | free(buf); |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 237 | return NULL; |
| 238 | } |
| 239 | |
hualing chen | 266b950 | 2020-04-04 17:39:39 +0800 | [diff] [blame] | 240 | memset(&record_status, 0, sizeof(record_status)); |
| 241 | record_status.state = DVR_RECORD_STATE_STARTED; |
pengfei.liu | fda2a97 | 2020-04-09 14:47:15 +0800 | [diff] [blame] | 242 | if (p_ctx->event_notify_fn) { |
hualing chen | 4b7c15d | 2020-04-07 16:13:48 +0800 | [diff] [blame] | 243 | record_status.info.id = p_ctx->segment_info.id; |
pengfei.liu | fda2a97 | 2020-04-09 14:47:15 +0800 | [diff] [blame] | 244 | p_ctx->event_notify_fn(DVR_RECORD_EVENT_STATUS, &record_status, p_ctx->event_userdata); |
Wentao MA | 96f6896 | 2022-06-15 19:45:35 +0800 | [diff] [blame] | 245 | DVR_INFO("%s line %d notify record status, state:%d id=%lld", |
hualing chen | 4b7c15d | 2020-04-07 16:13:48 +0800 | [diff] [blame] | 246 | __func__,__LINE__, record_status.state, p_ctx->segment_info.id); |
pengfei.liu | fda2a97 | 2020-04-09 14:47:15 +0800 | [diff] [blame] | 247 | } |
Wentao MA | 96f6896 | 2022-06-15 19:45:35 +0800 | [diff] [blame] | 248 | DVR_INFO("%s, --secure_mode:%d, block_size:%d, cryptor:%p", |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 249 | __func__, p_ctx->is_secure_mode, |
| 250 | block_size, p_ctx->cryptor); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 251 | clock_gettime(CLOCK_MONOTONIC, &start_ts); |
hualing chen | 3092e1c | 2022-01-21 20:02:21 +0800 | [diff] [blame] | 252 | p_ctx->check_pts_count = 0; |
| 253 | p_ctx->check_no_pts_count++; |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 254 | p_ctx->last_send_size = 0; |
| 255 | p_ctx->last_send_time = 0; |
pengfei.liu | 567d6d8 | 2020-04-17 16:48:59 +0800 | [diff] [blame] | 256 | struct timeval t1, t2, t3, t4, t5, t6, t7; |
hualing chen | 03fd494 | 2021-07-15 15:56:41 +0800 | [diff] [blame] | 257 | while (p_ctx->state == DVR_RECORD_STATE_STARTED || |
| 258 | p_ctx->state == DVR_RECORD_STATE_PAUSE) { |
| 259 | |
| 260 | if (p_ctx->state == DVR_RECORD_STATE_PAUSE) { |
| 261 | //wait resume record |
| 262 | usleep(20*1000); |
| 263 | continue; |
| 264 | } |
pengfei.liu | 567d6d8 | 2020-04-17 16:48:59 +0800 | [diff] [blame] | 265 | gettimeofday(&t1, NULL); |
hualing chen | c70a8df | 2020-05-12 19:23:11 +0800 | [diff] [blame] | 266 | |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 267 | /* data from dmx, normal dvr case */ |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 268 | if (p_ctx->is_secure_mode) { |
hualing chen | f986740 | 2020-09-23 17:06:20 +0800 | [diff] [blame] | 269 | if (p_ctx->is_new_dmx) { |
Wentao MA | 804bab1 | 2022-11-29 10:01:26 +0800 | [diff] [blame] | 270 | /* We resolve the below invoke for dvbcore to be under safety status */ |
| 271 | memset(&new_dmx_secure_buf, 0, sizeof(new_dmx_secure_buf)); |
| 272 | len = record_device_read(p_ctx->dev_handle, &new_dmx_secure_buf, |
| 273 | sizeof(new_dmx_secure_buf), 10); |
Yahui Han | ce15e9c | 2020-12-08 18:08:32 +0800 | [diff] [blame] | 274 | |
Wentao MA | 804bab1 | 2022-11-29 10:01:26 +0800 | [diff] [blame] | 275 | /* Read data from secure demux TA */ |
| 276 | len = record_device_read_ext(p_ctx->dev_handle, &secure_buf.addr, |
| 277 | &secure_buf.len); |
hualing chen | f986740 | 2020-09-23 17:06:20 +0800 | [diff] [blame] | 278 | } else { |
| 279 | memset(&secure_buf, 0, sizeof(secure_buf)); |
Wentao MA | 804bab1 | 2022-11-29 10:01:26 +0800 | [diff] [blame] | 280 | len = record_device_read(p_ctx->dev_handle, &secure_buf, |
| 281 | sizeof(secure_buf), 1000); |
hualing chen | f986740 | 2020-09-23 17:06:20 +0800 | [diff] [blame] | 282 | } |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 283 | } else { |
| 284 | len = record_device_read(p_ctx->dev_handle, buf, block_size, 1000); |
| 285 | } |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 286 | if (len == DVR_FAILURE) { |
hualing chen | 6c12638 | 2020-04-13 15:47:51 +0800 | [diff] [blame] | 287 | //usleep(10*1000); |
Wentao MA | 96f6896 | 2022-06-15 19:45:35 +0800 | [diff] [blame] | 288 | //DVR_INFO("%s, start_read error", __func__); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 289 | continue; |
| 290 | } |
pengfei.liu | 567d6d8 | 2020-04-17 16:48:59 +0800 | [diff] [blame] | 291 | gettimeofday(&t2, NULL); |
hualing chen | c70a8df | 2020-05-12 19:23:11 +0800 | [diff] [blame] | 292 | |
Wentao MA | eeffdb0 | 2022-06-27 16:34:35 +0800 | [diff] [blame] | 293 | guarded_size_exceeded = DVR_FALSE; |
wentao.ma | d08db84 | 2022-11-14 17:35:40 +0800 | [diff] [blame] | 294 | if ( p_ctx->guarded_segment_size > 0 && |
| 295 | p_ctx->segment_info.size+len >= p_ctx->guarded_segment_size) { |
Wentao MA | eeffdb0 | 2022-06-27 16:34:35 +0800 | [diff] [blame] | 296 | guarded_size_exceeded = DVR_TRUE; |
| 297 | } |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 298 | /* Got data from device, record it */ |
Wentao MA | eeffdb0 | 2022-06-27 16:34:35 +0800 | [diff] [blame] | 299 | if (guarded_size_exceeded) { |
| 300 | len = 0; |
| 301 | ret = 0; |
Wentao MA | f407203 | 2022-06-30 13:50:45 +0800 | [diff] [blame] | 302 | DVR_ERROR("Skip segment_write due to current segment size %u exceeding" |
| 303 | " guarded segment size", p_ctx->segment_info.size); |
| 304 | } else if (p_ctx->discard_coming_data) { |
| 305 | len = 0; |
| 306 | ret = 0; |
| 307 | DVR_ERROR("Skip segment_write due to total size exceeding max size too much"); |
Wentao MA | eeffdb0 | 2022-06-27 16:34:35 +0800 | [diff] [blame] | 308 | } else if (p_ctx->enc_func) { |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 309 | /* Encrypt record data */ |
pengfei.liu | 27cc4ec | 2020-04-03 16:28:16 +0800 | [diff] [blame] | 310 | DVR_CryptoParams_t crypto_params; |
| 311 | |
| 312 | memset(&crypto_params, 0, sizeof(crypto_params)); |
| 313 | crypto_params.type = DVR_CRYPTO_TYPE_ENCRYPT; |
hualing chen | 7a56cba | 2020-04-14 14:09:27 +0800 | [diff] [blame] | 314 | memcpy(crypto_params.location, p_ctx->location, sizeof(p_ctx->location)); |
pengfei.liu | 27cc4ec | 2020-04-03 16:28:16 +0800 | [diff] [blame] | 315 | crypto_params.segment_id = p_ctx->segment_info.id; |
| 316 | crypto_params.offset = p_ctx->segment_info.size; |
| 317 | |
| 318 | if (p_ctx->is_secure_mode) { |
| 319 | crypto_params.input_buffer.type = DVR_BUFFER_TYPE_SECURE; |
Yahui Han | 1fbf329 | 2021-11-08 18:17:19 +0800 | [diff] [blame] | 320 | crypto_params.input_buffer.addr = secure_buf.addr; |
| 321 | crypto_params.input_buffer.size = secure_buf.len; |
Yahui Han | af03b14 | 2022-08-17 17:52:37 +0800 | [diff] [blame] | 322 | crypto_params.output_buffer.size = p_ctx->secbuf_size + 188; |
pengfei.liu | 27cc4ec | 2020-04-03 16:28:16 +0800 | [diff] [blame] | 323 | } else { |
| 324 | crypto_params.input_buffer.type = DVR_BUFFER_TYPE_NORMAL; |
| 325 | crypto_params.input_buffer.addr = (size_t)buf; |
| 326 | crypto_params.input_buffer.size = len; |
Yahui Han | af03b14 | 2022-08-17 17:52:37 +0800 | [diff] [blame] | 327 | crypto_params.output_buffer.size = block_size + 188; |
pengfei.liu | 27cc4ec | 2020-04-03 16:28:16 +0800 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | crypto_params.output_buffer.type = DVR_BUFFER_TYPE_NORMAL; |
| 331 | crypto_params.output_buffer.addr = (size_t)buf_out; |
hualing chen | 9811b21 | 2020-10-29 11:21:44 +0800 | [diff] [blame] | 332 | |
pengfei.liu | 27cc4ec | 2020-04-03 16:28:16 +0800 | [diff] [blame] | 333 | p_ctx->enc_func(&crypto_params, p_ctx->enc_userdata); |
pengfei.liu | 567d6d8 | 2020-04-17 16:48:59 +0800 | [diff] [blame] | 334 | gettimeofday(&t3, NULL); |
pengfei.liu | 27cc4ec | 2020-04-03 16:28:16 +0800 | [diff] [blame] | 335 | /* Out buffer length may not equal in buffer length */ |
hualing chen | f986740 | 2020-09-23 17:06:20 +0800 | [diff] [blame] | 336 | if (crypto_params.output_size > 0) { |
| 337 | ret = segment_write(p_ctx->segment_handle, buf_out, crypto_params.output_size); |
| 338 | len = crypto_params.output_size; |
hualing chen | bafc62d | 2020-11-02 15:44:05 +0800 | [diff] [blame] | 339 | } else { |
| 340 | len = 0; |
hualing chen | f986740 | 2020-09-23 17:06:20 +0800 | [diff] [blame] | 341 | } |
Yahui Han | 1fbf329 | 2021-11-08 18:17:19 +0800 | [diff] [blame] | 342 | } else if (p_ctx->cryptor) { |
| 343 | /* Encrypt with clear key */ |
Yahui Han | 63b23b4 | 2021-12-07 15:37:46 +0800 | [diff] [blame] | 344 | int crypt_len = len; |
| 345 | am_crypt_des_crypt(p_ctx->cryptor, buf_out, buf, &crypt_len, 0); |
| 346 | len = crypt_len; |
Yahui Han | 1fbf329 | 2021-11-08 18:17:19 +0800 | [diff] [blame] | 347 | gettimeofday(&t3, NULL); |
| 348 | ret = segment_write(p_ctx->segment_handle, buf_out, len); |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 349 | } else { |
hualing chen | a5f0322 | 2021-12-02 11:22:35 +0800 | [diff] [blame] | 350 | if (first_read == 0) { |
| 351 | first_read = 1; |
Wentao MA | 96f6896 | 2022-06-15 19:45:35 +0800 | [diff] [blame] | 352 | DVR_INFO("%s:%d,first read ts", __func__,__LINE__); |
hualing chen | a5f0322 | 2021-12-02 11:22:35 +0800 | [diff] [blame] | 353 | } |
pengfei.liu | 567d6d8 | 2020-04-17 16:48:59 +0800 | [diff] [blame] | 354 | gettimeofday(&t3, NULL); |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 355 | ret = segment_write(p_ctx->segment_handle, buf, len); |
| 356 | } |
pengfei.liu | 567d6d8 | 2020-04-17 16:48:59 +0800 | [diff] [blame] | 357 | gettimeofday(&t4, NULL); |
hualing chen | 626204e | 2020-04-07 11:55:08 +0800 | [diff] [blame] | 358 | //add DVR_RECORD_EVENT_WRITE_ERROR event if write error |
pengfei.liu | fda2a97 | 2020-04-09 14:47:15 +0800 | [diff] [blame] | 359 | if (ret == -1 && len > 0 && p_ctx->event_notify_fn) { |
hualing chen | 626204e | 2020-04-07 11:55:08 +0800 | [diff] [blame] | 360 | //send write event |
hualing chen | 9b434f0 | 2020-06-10 15:06:54 +0800 | [diff] [blame] | 361 | if (p_ctx->event_notify_fn) { |
hualing chen | 4b7c15d | 2020-04-07 16:13:48 +0800 | [diff] [blame] | 362 | memset(&record_status, 0, sizeof(record_status)); |
Wentao MA | eeffdb0 | 2022-06-27 16:34:35 +0800 | [diff] [blame] | 363 | DVR_INFO("%s:%d,send event write error", __func__,__LINE__); |
hualing chen | 9ce7d94 | 2021-06-30 13:31:01 +0800 | [diff] [blame] | 364 | record_status.info.id = p_ctx->segment_info.id; |
hualing chen | 4b7c15d | 2020-04-07 16:13:48 +0800 | [diff] [blame] | 365 | p_ctx->event_notify_fn(DVR_RECORD_EVENT_WRITE_ERROR, &record_status, p_ctx->event_userdata); |
| 366 | } |
Wentao MA | 96f6896 | 2022-06-15 19:45:35 +0800 | [diff] [blame] | 367 | DVR_INFO("%s,write error %d", __func__,__LINE__); |
hualing chen | 626204e | 2020-04-07 11:55:08 +0800 | [diff] [blame] | 368 | goto end; |
| 369 | } |
Wentao MA | f407203 | 2022-06-30 13:50:45 +0800 | [diff] [blame] | 370 | if (len>0) { |
| 371 | /* Do time index */ |
| 372 | uint8_t *index_buf = (p_ctx->enc_func || p_ctx->cryptor)? buf_out : buf; |
| 373 | pos = segment_tell_position(p_ctx->segment_handle); |
| 374 | has_pcr = record_do_pcr_index(p_ctx, index_buf, len); |
| 375 | if (has_pcr == 0 && p_ctx->index_type == DVR_INDEX_TYPE_INVALID) { |
| 376 | clock_gettime(CLOCK_MONOTONIC, &end_ts); |
| 377 | if ((end_ts.tv_sec*1000 + end_ts.tv_nsec/1000000) - |
| 378 | (start_ts.tv_sec*1000 + start_ts.tv_nsec/1000000) > 40) { |
Wentao MA | 270dc0f | 2022-08-23 13:17:26 +0800 | [diff] [blame] | 379 | /* PCR interval threshold > 40 ms*/ |
Wentao MA | f407203 | 2022-06-30 13:50:45 +0800 | [diff] [blame] | 380 | DVR_INFO("%s use local clock time index", __func__); |
hualing chen | 3092e1c | 2022-01-21 20:02:21 +0800 | [diff] [blame] | 381 | p_ctx->index_type = DVR_INDEX_TYPE_LOCAL_CLOCK; |
Wentao MA | f407203 | 2022-06-30 13:50:45 +0800 | [diff] [blame] | 382 | } |
| 383 | } else if (has_pcr && p_ctx->index_type == DVR_INDEX_TYPE_INVALID){ |
| 384 | DVR_INFO("%s use pcr time index", __func__); |
| 385 | p_ctx->index_type = DVR_INDEX_TYPE_PCR; |
| 386 | record_do_pcr_index(p_ctx, index_buf, len); |
| 387 | } |
| 388 | gettimeofday(&t5, NULL); |
| 389 | if (p_ctx->index_type == DVR_INDEX_TYPE_PCR) { |
| 390 | if (has_pcr == 0) { |
| 391 | if (p_ctx->check_no_pts_count < 2 * CHECK_PTS_MAX_COUNT) { |
| 392 | if (p_ctx->check_no_pts_count == 0) { |
Wentao MA | 270dc0f | 2022-08-23 13:17:26 +0800 | [diff] [blame] | 393 | clock_gettime(CLOCK_MONOTONIC, &start_no_pcr_ts); |
Wentao MA | f407203 | 2022-06-30 13:50:45 +0800 | [diff] [blame] | 394 | clock_gettime(CLOCK_MONOTONIC, &start_ts); |
| 395 | } |
| 396 | p_ctx->check_no_pts_count++; |
| 397 | } |
| 398 | } else { |
Wentao MA | 270dc0f | 2022-08-23 13:17:26 +0800 | [diff] [blame] | 399 | clock_gettime(CLOCK_MONOTONIC, &start_no_pcr_ts); |
Wentao MA | f407203 | 2022-06-30 13:50:45 +0800 | [diff] [blame] | 400 | p_ctx->check_no_pts_count = 0; |
| 401 | } |
| 402 | } |
| 403 | /* Update segment i nfo */ |
| 404 | p_ctx->segment_info.size += len; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 405 | |
Wentao MA | f407203 | 2022-06-30 13:50:45 +0800 | [diff] [blame] | 406 | /*Duration need use pcr to calculate, todo...*/ |
| 407 | if (p_ctx->index_type == DVR_INDEX_TYPE_PCR) { |
hualing chen | c94227e | 2022-02-17 09:48:43 +0800 | [diff] [blame] | 408 | p_ctx->segment_info.duration = segment_tell_total_time(p_ctx->segment_handle); |
Wentao MA | f407203 | 2022-06-30 13:50:45 +0800 | [diff] [blame] | 409 | if (pre_time == 0) |
| 410 | pre_time = p_ctx->segment_info.duration; |
| 411 | } else if (p_ctx->index_type == DVR_INDEX_TYPE_LOCAL_CLOCK) { |
| 412 | clock_gettime(CLOCK_MONOTONIC, &end_ts); |
| 413 | p_ctx->segment_info.duration = (end_ts.tv_sec*1000 + end_ts.tv_nsec/1000000) - |
| 414 | (start_ts.tv_sec*1000 + start_ts.tv_nsec/1000000) + pcr_rec_len; |
| 415 | if (pre_time == 0) |
| 416 | pre_time = p_ctx->segment_info.duration; |
| 417 | segment_update_pts(p_ctx->segment_handle, p_ctx->segment_info.duration, pos); |
| 418 | } else { |
| 419 | DVR_INFO("%s can NOT do time index", __func__); |
| 420 | } |
| 421 | if (p_ctx->index_type == DVR_INDEX_TYPE_PCR && |
| 422 | p_ctx->check_pts_count == CHECK_PTS_MAX_COUNT) { |
| 423 | DVR_INFO("%s change time from pcr to local time", __func__); |
| 424 | if (pcr_rec_len == 0) |
| 425 | pcr_rec_len = segment_tell_total_time(p_ctx->segment_handle); |
| 426 | p_ctx->index_type = DVR_INDEX_TYPE_LOCAL_CLOCK; |
| 427 | if (pcr_rec_len == 0) |
| 428 | pcr_rec_len = segment_tell_total_time(p_ctx->segment_handle); |
| 429 | clock_gettime(CLOCK_MONOTONIC, &start_ts); |
| 430 | } |
| 431 | |
| 432 | if (p_ctx->index_type == DVR_INDEX_TYPE_PCR ) { |
Wentao MA | 270dc0f | 2022-08-23 13:17:26 +0800 | [diff] [blame] | 433 | clock_gettime(CLOCK_MONOTONIC, &end_no_pcr_ts); |
| 434 | int diff = (int)(end_no_pcr_ts.tv_sec*1000 + end_no_pcr_ts.tv_nsec/1000000) - |
| 435 | (int)(start_no_pcr_ts.tv_sec*1000 + start_no_pcr_ts.tv_nsec/1000000); |
Wentao MA | f407203 | 2022-06-30 13:50:45 +0800 | [diff] [blame] | 436 | if (diff > 3000) { |
| 437 | DVR_INFO("%s no pcr change time from pcr to local time diff[%d]", __func__, diff); |
| 438 | if (pcr_rec_len == 0) |
| 439 | pcr_rec_len = segment_tell_total_time(p_ctx->segment_handle); |
| 440 | p_ctx->index_type = DVR_INDEX_TYPE_LOCAL_CLOCK; |
| 441 | } |
| 442 | } |
| 443 | p_ctx->segment_info.nb_packets = p_ctx->segment_info.size/188; |
| 444 | |
| 445 | if (p_ctx->segment_info.duration - pre_time > DVR_STORE_INFO_TIME) { |
| 446 | pre_time = p_ctx->segment_info.duration + DVR_STORE_INFO_TIME; |
| 447 | time_t duration = p_ctx->segment_info.duration; |
| 448 | if (p_ctx->index_type == DVR_INDEX_TYPE_LOCAL_CLOCK) |
| 449 | p_ctx->segment_info.duration = segment_tell_total_time(p_ctx->segment_handle); |
| 450 | segment_store_info(p_ctx->segment_handle, &(p_ctx->segment_info)); |
| 451 | p_ctx->segment_info.duration = duration; |
| 452 | } |
| 453 | } else { |
| 454 | gettimeofday(&t5, NULL); |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 455 | } |
pengfei.liu | 567d6d8 | 2020-04-17 16:48:59 +0800 | [diff] [blame] | 456 | gettimeofday(&t6, NULL); |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 457 | /*Event notification*/ |
Wentao MA | f407203 | 2022-06-30 13:50:45 +0800 | [diff] [blame] | 458 | DVR_Bool_t condA1 = (p_ctx->notification_size > 0); |
| 459 | DVR_Bool_t condA2 = ((p_ctx->segment_info.size-p_ctx->last_send_size) >= p_ctx->notification_size); |
| 460 | DVR_Bool_t condA3 = (p_ctx->notification_time > 0); |
| 461 | DVR_Bool_t condA4 = ((p_ctx->segment_info.duration-p_ctx->last_send_time) >= p_ctx->notification_time); |
| 462 | DVR_Bool_t condA5 = (guarded_size_exceeded); |
| 463 | DVR_Bool_t condA6 = (p_ctx->discard_coming_data); |
| 464 | DVR_Bool_t condB = (p_ctx->event_notify_fn != NULL); |
| 465 | DVR_Bool_t condC = (p_ctx->segment_info.duration > 0); |
| 466 | DVR_Bool_t condD = (p_ctx->state == DVR_RECORD_STATE_STARTED); |
| 467 | if (((condA1 && condA2) || (condA3 && condA4) || condA5 || condA6) |
| 468 | && condB && condC && condD) { |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 469 | memset(&record_status, 0, sizeof(record_status)); |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 470 | //clock_gettime(CLOCK_MONOTONIC, &end_ts); |
hualing chen | 2615aa8 | 2020-04-02 21:32:51 +0800 | [diff] [blame] | 471 | p_ctx->last_send_size = p_ctx->segment_info.size; |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 472 | p_ctx->last_send_time = p_ctx->segment_info.duration; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 473 | record_status.state = p_ctx->state; |
| 474 | record_status.info.id = p_ctx->segment_info.id; |
hualing chen | c94227e | 2022-02-17 09:48:43 +0800 | [diff] [blame] | 475 | if (p_ctx->index_type == DVR_INDEX_TYPE_LOCAL_CLOCK) |
| 476 | record_status.info.duration = segment_tell_total_time(p_ctx->segment_handle); |
| 477 | else |
| 478 | record_status.info.duration = p_ctx->segment_info.duration; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 479 | record_status.info.size = p_ctx->segment_info.size; |
| 480 | record_status.info.nb_packets = p_ctx->segment_info.size/188; |
| 481 | p_ctx->event_notify_fn(DVR_RECORD_EVENT_STATUS, &record_status, p_ctx->event_userdata); |
Wentao MA | 96f6896 | 2022-06-15 19:45:35 +0800 | [diff] [blame] | 482 | DVR_INFO("%s notify record status, state:%d, id:%lld, duration:%ld ms, size:%zu loc[%s]", |
hualing chen | 4fe3bee | 2020-10-23 13:58:52 +0800 | [diff] [blame] | 483 | __func__, record_status.state, |
| 484 | record_status.info.id, record_status.info.duration, |
| 485 | record_status.info.size, p_ctx->location); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 486 | } |
pengfei.liu | 567d6d8 | 2020-04-17 16:48:59 +0800 | [diff] [blame] | 487 | gettimeofday(&t7, NULL); |
| 488 | #ifdef DEBUG_PERFORMANCE |
Wentao MA | 96f6896 | 2022-06-15 19:45:35 +0800 | [diff] [blame] | 489 | DVR_INFO("record count, read:%dms, encrypt:%dms, write:%dms, index:%dms, store:%dms, notify:%dms total:%dms read len:%zd notify [%d]diff[%d]", |
pengfei.liu | 567d6d8 | 2020-04-17 16:48:59 +0800 | [diff] [blame] | 490 | get_diff_time(t1, t2), get_diff_time(t2, t3), get_diff_time(t3, t4), get_diff_time(t4, t5), |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 491 | get_diff_time(t5, t6), get_diff_time(t6, t7), get_diff_time(t1, t5), len, |
| 492 | p_ctx->notification_time,p_ctx->segment_info.duration -p_ctx->last_send_time); |
pengfei.liu | 567d6d8 | 2020-04-17 16:48:59 +0800 | [diff] [blame] | 493 | #endif |
Wentao MA | f407203 | 2022-06-30 13:50:45 +0800 | [diff] [blame] | 494 | if (len == 0) { |
| 495 | usleep(20*1000); |
| 496 | } |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 497 | } |
hualing chen | 626204e | 2020-04-07 11:55:08 +0800 | [diff] [blame] | 498 | end: |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 499 | free((void *)buf); |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 500 | free((void *)buf_out); |
Wentao MA | 96f6896 | 2022-06-15 19:45:35 +0800 | [diff] [blame] | 501 | DVR_INFO("exit %s", __func__); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 502 | return NULL; |
| 503 | } |
| 504 | |
| 505 | int dvr_record_open(DVR_RecordHandle_t *p_handle, DVR_RecordOpenParams_t *params) |
| 506 | { |
| 507 | DVR_RecordContext_t *p_ctx; |
| 508 | Record_DeviceOpenParams_t dev_open_params; |
Wentao MA | 139fc61 | 2022-08-29 14:10:07 +0800 | [diff] [blame] | 509 | int ret = DVR_SUCCESS; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 510 | uint32_t i; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 511 | |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 512 | DVR_RETURN_IF_FALSE(p_handle); |
| 513 | DVR_RETURN_IF_FALSE(params); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 514 | |
| 515 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 516 | if (record_ctx[i].state == DVR_RECORD_STATE_CLOSED) { |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 517 | break; |
| 518 | } |
| 519 | } |
Pengfei Liu | faf38e4 | 2020-05-22 00:28:02 +0800 | [diff] [blame] | 520 | DVR_RETURN_IF_FALSE(i < MAX_DVR_RECORD_SESSION_COUNT); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 521 | DVR_RETURN_IF_FALSE(record_ctx[i].state == DVR_RECORD_STATE_CLOSED); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 522 | p_ctx = &record_ctx[i]; |
Wentao MA | 96f6896 | 2022-06-15 19:45:35 +0800 | [diff] [blame] | 523 | DVR_INFO("%s , current state:%d, dmx_id:%d, notification_size:%zu, flags:%d, keylen:%d ", |
Yahui Han | 1fbf329 | 2021-11-08 18:17:19 +0800 | [diff] [blame] | 524 | __func__, p_ctx->state, params->dmx_dev_id, |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 525 | params->notification_size, |
| 526 | params->flags, params->keylen); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 527 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 528 | /*Process event params*/ |
| 529 | p_ctx->notification_size = params->notification_size; |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 530 | p_ctx->notification_time = params->notification_time; |
| 531 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 532 | p_ctx->event_notify_fn = params->event_fn; |
| 533 | p_ctx->event_userdata = params->event_userdata; |
hualing chen | 2615aa8 | 2020-04-02 21:32:51 +0800 | [diff] [blame] | 534 | p_ctx->last_send_size = 0; |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 535 | p_ctx->last_send_time = 0; |
hualing chen | 94d8dd2 | 2021-12-29 15:13:43 +0800 | [diff] [blame] | 536 | p_ctx->pts = ULLONG_MAX; |
Yahui Han | 1fbf329 | 2021-11-08 18:17:19 +0800 | [diff] [blame] | 537 | |
| 538 | if (params->keylen > 0) { |
| 539 | p_ctx->cryptor = am_crypt_des_open(params->clearkey, |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 540 | params->cleariv, |
| 541 | params->keylen * 8); |
Yahui Han | 1fbf329 | 2021-11-08 18:17:19 +0800 | [diff] [blame] | 542 | if (!p_ctx->cryptor) |
Wentao MA | 96f6896 | 2022-06-15 19:45:35 +0800 | [diff] [blame] | 543 | DVR_INFO("%s , open des cryptor failed!!!\n", __func__); |
Yahui Han | 1fbf329 | 2021-11-08 18:17:19 +0800 | [diff] [blame] | 544 | } else { |
| 545 | p_ctx->cryptor = NULL; |
| 546 | } |
| 547 | |
hualing chen | f986740 | 2020-09-23 17:06:20 +0800 | [diff] [blame] | 548 | //check is new driver |
| 549 | p_ctx->is_new_dmx = dvr_check_dmx_isNew(); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 550 | /*Process crypto params, todo*/ |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 551 | memset((void *)&dev_open_params, 0, sizeof(dev_open_params)); |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 552 | if (params->data_from_memory) { |
| 553 | /* data from memory, VOD case */ |
| 554 | p_ctx->is_vod = 1; |
| 555 | } else { |
| 556 | p_ctx->is_vod = 0; |
| 557 | /* data from dmx, normal dvr case */ |
hualing chen | 958fe4c | 2020-03-24 17:35:07 +0800 | [diff] [blame] | 558 | dev_open_params.dmx_dev_id = params->dmx_dev_id; |
hualing chen | 157641d | 2022-02-22 17:54:52 +0800 | [diff] [blame] | 559 | //set dvr flush size |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 560 | dev_open_params.buf_size = (params->flush_size > 0 ? params->flush_size : RECORD_BLOCK_SIZE); |
hualing chen | 157641d | 2022-02-22 17:54:52 +0800 | [diff] [blame] | 561 | //set dvbcore ringbuf size |
hualing chen | 03fd494 | 2021-07-15 15:56:41 +0800 | [diff] [blame] | 562 | dev_open_params.ringbuf_size = params->ringbuf_size; |
hualing chen | 157641d | 2022-02-22 17:54:52 +0800 | [diff] [blame] | 563 | |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 564 | ret = record_device_open(&p_ctx->dev_handle, &dev_open_params); |
| 565 | if (ret != DVR_SUCCESS) { |
Wentao MA | 96f6896 | 2022-06-15 19:45:35 +0800 | [diff] [blame] | 566 | DVR_INFO("%s, open record devices failed", __func__); |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 567 | return DVR_FAILURE; |
| 568 | } |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 569 | } |
| 570 | |
pengfei.liu | fda2a97 | 2020-04-09 14:47:15 +0800 | [diff] [blame] | 571 | p_ctx->block_size = (params->flush_size > 0 ? params->flush_size : RECORD_BLOCK_SIZE); |
hualing chen | 157641d | 2022-02-22 17:54:52 +0800 | [diff] [blame] | 572 | |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 573 | p_ctx->enc_func = NULL; |
| 574 | p_ctx->enc_userdata = NULL; |
| 575 | p_ctx->is_secure_mode = 0; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 576 | p_ctx->state = DVR_RECORD_STATE_OPENED; |
wentao.ma | 35a69d4 | 2022-03-10 18:08:40 +0800 | [diff] [blame] | 577 | p_ctx->force_sysclock = params->force_sysclock; |
Wentao MA | eeffdb0 | 2022-06-27 16:34:35 +0800 | [diff] [blame] | 578 | p_ctx->guarded_segment_size = params->guarded_segment_size; |
wentao.ma | d08db84 | 2022-11-14 17:35:40 +0800 | [diff] [blame] | 579 | if (p_ctx->guarded_segment_size <= 0) { |
| 580 | DVR_WARN("Odd guarded_segment_size value %lld is given. Change it to" |
| 581 | " 0 to disable segment guarding mechanism.", p_ctx->guarded_segment_size); |
| 582 | p_ctx->guarded_segment_size = 0; |
| 583 | } |
Wentao MA | f407203 | 2022-06-30 13:50:45 +0800 | [diff] [blame] | 584 | p_ctx->discard_coming_data = DVR_FALSE; |
Wentao MA | 96f6896 | 2022-06-15 19:45:35 +0800 | [diff] [blame] | 585 | DVR_INFO("%s, block_size:%d is_new:%d", __func__, p_ctx->block_size, p_ctx->is_new_dmx); |
Pengfei Liu | 47ed6c9 | 2020-01-17 11:23:41 +0800 | [diff] [blame] | 586 | *p_handle = p_ctx; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 587 | return DVR_SUCCESS; |
| 588 | } |
| 589 | |
| 590 | int dvr_record_close(DVR_RecordHandle_t handle) |
| 591 | { |
| 592 | DVR_RecordContext_t *p_ctx; |
Wentao MA | 139fc61 | 2022-08-29 14:10:07 +0800 | [diff] [blame] | 593 | int ret = DVR_SUCCESS; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 594 | uint32_t i; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 595 | |
Pengfei Liu | 47ed6c9 | 2020-01-17 11:23:41 +0800 | [diff] [blame] | 596 | p_ctx = (DVR_RecordContext_t *)handle; |
| 597 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 598 | if (p_ctx == &record_ctx[i]) |
| 599 | break; |
| 600 | } |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 601 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 602 | |
Wentao MA | 96f6896 | 2022-06-15 19:45:35 +0800 | [diff] [blame] | 603 | DVR_INFO("%s , current state:%d", __func__, p_ctx->state); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 604 | DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED); |
Yahui Han | 1fbf329 | 2021-11-08 18:17:19 +0800 | [diff] [blame] | 605 | if (p_ctx->cryptor) { |
| 606 | am_crypt_des_close(p_ctx->cryptor); |
| 607 | p_ctx->cryptor = NULL; |
| 608 | } |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 609 | if (p_ctx->is_vod) { |
| 610 | ret = DVR_SUCCESS; |
| 611 | } else { |
| 612 | ret = record_device_close(p_ctx->dev_handle); |
| 613 | if (ret != DVR_SUCCESS) { |
Wentao MA | 96f6896 | 2022-06-15 19:45:35 +0800 | [diff] [blame] | 614 | DVR_INFO("%s, failed", __func__); |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 615 | } |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 616 | } |
hualing chen | cedcb86 | 2022-01-04 15:32:43 +0800 | [diff] [blame] | 617 | memset(p_ctx, 0, sizeof(DVR_RecordContext_t)); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 618 | p_ctx->state = DVR_RECORD_STATE_CLOSED; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 619 | return ret; |
| 620 | } |
| 621 | |
hualing chen | 03fd494 | 2021-07-15 15:56:41 +0800 | [diff] [blame] | 622 | int dvr_record_pause(DVR_RecordHandle_t handle) |
| 623 | { |
| 624 | DVR_RecordContext_t *p_ctx; |
Wentao MA | 139fc61 | 2022-08-29 14:10:07 +0800 | [diff] [blame] | 625 | int ret = DVR_SUCCESS; |
hualing chen | 03fd494 | 2021-07-15 15:56:41 +0800 | [diff] [blame] | 626 | uint32_t i; |
| 627 | |
| 628 | p_ctx = (DVR_RecordContext_t *)handle; |
| 629 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 630 | if (p_ctx == &record_ctx[i]) |
| 631 | break; |
| 632 | } |
| 633 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
| 634 | |
Wentao MA | 96f6896 | 2022-06-15 19:45:35 +0800 | [diff] [blame] | 635 | DVR_INFO("%s , current state:%d", __func__, p_ctx->state); |
hualing chen | 03fd494 | 2021-07-15 15:56:41 +0800 | [diff] [blame] | 636 | DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED); |
| 637 | |
| 638 | if (p_ctx->is_vod) { |
| 639 | ret = DVR_SUCCESS; |
| 640 | } |
| 641 | //set pause state,will not store ts into segment |
| 642 | p_ctx->state = DVR_RECORD_STATE_PAUSE; |
| 643 | return ret; |
| 644 | } |
| 645 | |
| 646 | int dvr_record_resume(DVR_RecordHandle_t handle) |
| 647 | { |
| 648 | DVR_RecordContext_t *p_ctx; |
Wentao MA | 139fc61 | 2022-08-29 14:10:07 +0800 | [diff] [blame] | 649 | int ret = DVR_SUCCESS; |
hualing chen | 03fd494 | 2021-07-15 15:56:41 +0800 | [diff] [blame] | 650 | uint32_t i; |
| 651 | |
| 652 | p_ctx = (DVR_RecordContext_t *)handle; |
| 653 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 654 | if (p_ctx == &record_ctx[i]) |
| 655 | break; |
| 656 | } |
| 657 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
| 658 | |
Wentao MA | 96f6896 | 2022-06-15 19:45:35 +0800 | [diff] [blame] | 659 | DVR_INFO("%s , current state:%d", __func__, p_ctx->state); |
hualing chen | 03fd494 | 2021-07-15 15:56:41 +0800 | [diff] [blame] | 660 | DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED); |
| 661 | |
| 662 | if (p_ctx->is_vod) { |
| 663 | ret = DVR_SUCCESS; |
| 664 | } |
| 665 | //set stated state,will resume store ts into segment |
| 666 | p_ctx->state = DVR_RECORD_STATE_STARTED; |
| 667 | return ret; |
| 668 | } |
| 669 | |
Pengfei Liu | 47ed6c9 | 2020-01-17 11:23:41 +0800 | [diff] [blame] | 670 | #if 0 |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 671 | int dvr_record_register_encryption(DVR_RecordHandle_t handle, |
| 672 | DVR_CryptoFunction_t cb, |
| 673 | DVR_CryptoParams_t params, |
| 674 | void *userdata) |
| 675 | { |
| 676 | return DVR_SUCCESS; |
| 677 | } |
Pengfei Liu | 47ed6c9 | 2020-01-17 11:23:41 +0800 | [diff] [blame] | 678 | #endif |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 679 | |
| 680 | int dvr_record_start_segment(DVR_RecordHandle_t handle, DVR_RecordStartParams_t *params) |
| 681 | { |
| 682 | DVR_RecordContext_t *p_ctx; |
| 683 | Segment_OpenParams_t open_params; |
Wentao MA | 139fc61 | 2022-08-29 14:10:07 +0800 | [diff] [blame] | 684 | int ret = DVR_SUCCESS; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 685 | uint32_t i; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 686 | |
Pengfei Liu | 47ed6c9 | 2020-01-17 11:23:41 +0800 | [diff] [blame] | 687 | p_ctx = (DVR_RecordContext_t *)handle; |
| 688 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 689 | if (p_ctx == &record_ctx[i]) |
| 690 | break; |
| 691 | } |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 692 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 693 | |
Wentao MA | 96f6896 | 2022-06-15 19:45:35 +0800 | [diff] [blame] | 694 | DVR_INFO("%s , current state:%d pids:%d params->location:%s", __func__, p_ctx->state, params->segment.nb_pids, params->location); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 695 | DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_STARTED); |
| 696 | DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED); |
| 697 | DVR_RETURN_IF_FALSE(params); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 698 | |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 699 | DVR_RETURN_IF_FALSE(strlen((const char *)params->location) < DVR_MAX_LOCATION_SIZE); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 700 | memset(&open_params, 0, sizeof(open_params)); |
hualing chen | 7a56cba | 2020-04-14 14:09:27 +0800 | [diff] [blame] | 701 | memcpy(open_params.location, params->location, sizeof(params->location)); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 702 | open_params.segment_id = params->segment.segment_id; |
| 703 | open_params.mode = SEGMENT_MODE_WRITE; |
wentao.ma | 35a69d4 | 2022-03-10 18:08:40 +0800 | [diff] [blame] | 704 | open_params.force_sysclock = p_ctx->force_sysclock; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 705 | |
| 706 | ret = segment_open(&open_params, &p_ctx->segment_handle); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 707 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 708 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 709 | /*process params*/ |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 710 | { |
hualing chen | 7a56cba | 2020-04-14 14:09:27 +0800 | [diff] [blame] | 711 | memcpy(p_ctx->location, params->location, sizeof(params->location)); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 712 | //need all params?? |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 713 | memcpy(&p_ctx->segment_params, ¶ms->segment, sizeof(params->segment)); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 714 | /*save current segment info*/ |
| 715 | memset(&p_ctx->segment_info, 0, sizeof(p_ctx->segment_info)); |
| 716 | p_ctx->segment_info.id = params->segment.segment_id; |
| 717 | p_ctx->segment_info.nb_pids = params->segment.nb_pids; |
| 718 | memcpy(p_ctx->segment_info.pids, params->segment.pids, params->segment.nb_pids*sizeof(DVR_StreamPid_t)); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 719 | } |
| 720 | |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 721 | if (!p_ctx->is_vod) { |
| 722 | /* normal dvr case */ |
| 723 | for (i = 0; i < params->segment.nb_pids; i++) { |
| 724 | ret = record_device_add_pid(p_ctx->dev_handle, params->segment.pids[i].pid); |
| 725 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
| 726 | } |
| 727 | ret = record_device_start(p_ctx->dev_handle); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 728 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 729 | } |
| 730 | |
Zhiqiang Han | 5ebad99 | 2020-04-28 18:19:59 +0800 | [diff] [blame] | 731 | ret = segment_store_info(p_ctx->segment_handle, &p_ctx->segment_info); |
wentao.ma | a210e5e | 2022-10-12 16:10:03 +0800 | [diff] [blame] | 732 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Zhiqiang Han | 5ebad99 | 2020-04-28 18:19:59 +0800 | [diff] [blame] | 733 | |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 734 | p_ctx->state = DVR_RECORD_STATE_STARTED; |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 735 | if (!p_ctx->is_vod) |
| 736 | pthread_create(&p_ctx->thread, NULL, record_thread, p_ctx); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 737 | |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 738 | return DVR_SUCCESS; |
| 739 | } |
| 740 | |
| 741 | int dvr_record_next_segment(DVR_RecordHandle_t handle, DVR_RecordStartParams_t *params, DVR_RecordSegmentInfo_t *p_info) |
| 742 | { |
| 743 | DVR_RecordContext_t *p_ctx; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 744 | Segment_OpenParams_t open_params; |
Wentao MA | 139fc61 | 2022-08-29 14:10:07 +0800 | [diff] [blame] | 745 | int ret = DVR_SUCCESS; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 746 | uint32_t i; |
hualing chen | 2932d37 | 2020-04-29 13:44:00 +0800 | [diff] [blame] | 747 | loff_t pos; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 748 | |
Pengfei Liu | 47ed6c9 | 2020-01-17 11:23:41 +0800 | [diff] [blame] | 749 | p_ctx = (DVR_RecordContext_t *)handle; |
| 750 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 751 | if (p_ctx == &record_ctx[i]) |
| 752 | break; |
| 753 | } |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 754 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 755 | |
Wentao MA | 96f6896 | 2022-06-15 19:45:35 +0800 | [diff] [blame] | 756 | DVR_INFO("%s , current state:%d p_ctx->location:%s", __func__, p_ctx->state, p_ctx->location); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 757 | DVR_RETURN_IF_FALSE(p_ctx->state == DVR_RECORD_STATE_STARTED); |
| 758 | //DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED); |
| 759 | DVR_RETURN_IF_FALSE(params); |
| 760 | DVR_RETURN_IF_FALSE(p_info); |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 761 | DVR_RETURN_IF_FALSE(!p_ctx->is_vod); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 762 | |
| 763 | /*Stop the on going record segment*/ |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 764 | //ret = record_device_stop(p_ctx->dev_handle); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 765 | //DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 766 | p_ctx->state = DVR_RECORD_STATE_STOPPED; |
| 767 | pthread_join(p_ctx->thread, NULL); |
| 768 | |
hualing chen | 2932d37 | 2020-04-29 13:44:00 +0800 | [diff] [blame] | 769 | //add index file store |
| 770 | pos = segment_tell_position(p_ctx->segment_handle); |
| 771 | segment_update_pts_force(p_ctx->segment_handle, p_ctx->segment_info.duration, pos); |
| 772 | |
| 773 | p_ctx->segment_info.duration = segment_tell_total_time(p_ctx->segment_handle); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 774 | /*Update segment info*/ |
| 775 | memcpy(p_info, &p_ctx->segment_info, sizeof(p_ctx->segment_info)); |
| 776 | |
| 777 | ret = segment_store_info(p_ctx->segment_handle, p_info); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 778 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
wentao.ma | a210e5e | 2022-10-12 16:10:03 +0800 | [diff] [blame] | 779 | segment_store_allInfo(p_ctx->segment_handle, p_info); |
Wentao MA | 96f6896 | 2022-06-15 19:45:35 +0800 | [diff] [blame] | 780 | DVR_INFO("%s dump segment info, id:%lld, nb_pids:%d, duration:%ld ms, size:%zu, nb_packets:%d params->segment.nb_pids:%d", |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 781 | __func__, p_info->id, p_info->nb_pids, p_info->duration, p_info->size, p_info->nb_packets, params->segment.nb_pids); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 782 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 783 | /*Close current segment*/ |
| 784 | ret = segment_close(p_ctx->segment_handle); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 785 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 786 | /*Open the new record segment*/ |
| 787 | memset(&open_params, 0, sizeof(open_params)); |
hualing chen | 7a56cba | 2020-04-14 14:09:27 +0800 | [diff] [blame] | 788 | memcpy(open_params.location, p_ctx->location, sizeof(p_ctx->location)); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 789 | open_params.segment_id = params->segment.segment_id; |
| 790 | open_params.mode = SEGMENT_MODE_WRITE; |
wentao.ma | 35a69d4 | 2022-03-10 18:08:40 +0800 | [diff] [blame] | 791 | open_params.force_sysclock = p_ctx->force_sysclock; |
Wentao MA | 96f6896 | 2022-06-15 19:45:35 +0800 | [diff] [blame] | 792 | DVR_INFO("%s: p_ctx->location:%s params->location:%s", __func__, p_ctx->location,params->location); |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 793 | p_ctx->last_send_size = 0; |
| 794 | p_ctx->last_send_time = 0; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 795 | ret = segment_open(&open_params, &p_ctx->segment_handle); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 796 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 797 | /*process params*/ |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 798 | { |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 799 | //need all params?? |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 800 | memcpy(&p_ctx->segment_params, ¶ms->segment, sizeof(params->segment)); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 801 | /*save current segment info*/ |
| 802 | memset(&p_ctx->segment_info, 0, sizeof(p_ctx->segment_info)); |
| 803 | p_ctx->segment_info.id = params->segment.segment_id; |
| 804 | memcpy(p_ctx->segment_info.pids, params->segment.pids, params->segment.nb_pids*sizeof(DVR_StreamPid_t)); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 805 | } |
| 806 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 807 | p_ctx->segment_info.nb_pids = 0; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 808 | for (i = 0; i < params->segment.nb_pids; i++) { |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 809 | switch (params->segment.pid_action[i]) { |
| 810 | case DVR_RECORD_PID_CREATE: |
Wentao MA | 96f6896 | 2022-06-15 19:45:35 +0800 | [diff] [blame] | 811 | DVR_INFO("%s create pid:%d", __func__, params->segment.pids[i].pid); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 812 | ret = record_device_add_pid(p_ctx->dev_handle, params->segment.pids[i].pid); |
| 813 | p_ctx->segment_info.nb_pids++; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 814 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 815 | break; |
| 816 | case DVR_RECORD_PID_KEEP: |
Wentao MA | 96f6896 | 2022-06-15 19:45:35 +0800 | [diff] [blame] | 817 | DVR_INFO("%s keep pid:%d", __func__, params->segment.pids[i].pid); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 818 | p_ctx->segment_info.nb_pids++; |
| 819 | break; |
| 820 | case DVR_RECORD_PID_CLOSE: |
Wentao MA | 96f6896 | 2022-06-15 19:45:35 +0800 | [diff] [blame] | 821 | DVR_INFO("%s close pid:%d", __func__, params->segment.pids[i].pid); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 822 | ret = record_device_remove_pid(p_ctx->dev_handle, params->segment.pids[i].pid); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 823 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 824 | break; |
| 825 | default: |
Wentao MA | 96f6896 | 2022-06-15 19:45:35 +0800 | [diff] [blame] | 826 | DVR_INFO("%s wrong action pid:%d", __func__, params->segment.pids[i].pid); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 827 | return DVR_FAILURE; |
| 828 | } |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 829 | } |
| 830 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 831 | //ret = record_device_start(p_ctx->dev_handle); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 832 | //DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
hualing chen | 4b7c15d | 2020-04-07 16:13:48 +0800 | [diff] [blame] | 833 | /*Update segment info*/ |
| 834 | ret = segment_store_info(p_ctx->segment_handle, &p_ctx->segment_info); |
wentao.ma | a210e5e | 2022-10-12 16:10:03 +0800 | [diff] [blame] | 835 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
hualing chen | 94d8dd2 | 2021-12-29 15:13:43 +0800 | [diff] [blame] | 836 | if (p_ctx->pts != ULLONG_MAX) |
| 837 | segment_update_pts(p_ctx->segment_handle, p_ctx->pts, 0); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 838 | |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 839 | p_ctx->state = DVR_RECORD_STATE_STARTED; |
Zhiqiang Han | 0d60f2b | 2020-05-26 14:39:54 +0800 | [diff] [blame] | 840 | pthread_create(&p_ctx->thread, NULL, record_thread, p_ctx); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 841 | return DVR_SUCCESS; |
| 842 | } |
| 843 | |
| 844 | int dvr_record_stop_segment(DVR_RecordHandle_t handle, DVR_RecordSegmentInfo_t *p_info) |
| 845 | { |
| 846 | DVR_RecordContext_t *p_ctx; |
Wentao MA | 139fc61 | 2022-08-29 14:10:07 +0800 | [diff] [blame] | 847 | int ret = DVR_SUCCESS; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 848 | uint32_t i; |
hualing chen | 2932d37 | 2020-04-29 13:44:00 +0800 | [diff] [blame] | 849 | loff_t pos; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 850 | |
Pengfei Liu | 47ed6c9 | 2020-01-17 11:23:41 +0800 | [diff] [blame] | 851 | p_ctx = (DVR_RecordContext_t *)handle; |
| 852 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 853 | if (p_ctx == &record_ctx[i]) |
| 854 | break; |
| 855 | } |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 856 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 857 | |
Wentao MA | d71e206 | 2023-02-15 10:10:49 +0800 | [diff] [blame] | 858 | if (p_ctx->segment_handle == NULL) { |
| 859 | // It seems this stop function has been called twice on the same recording, |
| 860 | // so just return success. |
| 861 | return DVR_SUCCESS; |
| 862 | } |
| 863 | |
Wentao MA | 96f6896 | 2022-06-15 19:45:35 +0800 | [diff] [blame] | 864 | DVR_INFO("%s , current state:%d p_ctx->location:%s", __func__, p_ctx->state, p_ctx->location); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 865 | DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_STOPPED); |
| 866 | DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED); |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 867 | DVR_RETURN_IF_FALSE(p_info);/*should support NULL*/ |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 868 | |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 869 | p_ctx->state = DVR_RECORD_STATE_STOPPED; |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 870 | if (p_ctx->is_vod) { |
pengfei.liu | 8b56329 | 2020-02-26 15:49:02 +0800 | [diff] [blame] | 871 | p_ctx->segment_info.duration = segment_tell_total_time(p_ctx->segment_handle); |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 872 | p_ctx->segment_info.duration = 10*1000; //debug, should delete it |
| 873 | } else { |
Chao Yin | 0e6cb60 | 2022-08-02 15:41:24 +0800 | [diff] [blame] | 874 | pthread_join(p_ctx->thread, NULL); |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 875 | ret = record_device_stop(p_ctx->dev_handle); |
Zhiqiang Han | 5c805cf | 2020-05-09 16:51:08 +0800 | [diff] [blame] | 876 | //DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
| 877 | if (ret != DVR_SUCCESS) |
| 878 | goto end; |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 879 | //p_ctx->state = DVR_RECORD_STATE_STOPPED; |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 880 | } |
| 881 | |
hualing chen | 2932d37 | 2020-04-29 13:44:00 +0800 | [diff] [blame] | 882 | //add index file store |
| 883 | pos = segment_tell_position(p_ctx->segment_handle); |
| 884 | segment_update_pts_force(p_ctx->segment_handle, p_ctx->segment_info.duration, pos); |
hualing chen | e41f437 | 2020-06-06 16:29:17 +0800 | [diff] [blame] | 885 | p_ctx->segment_info.duration = segment_tell_total_time(p_ctx->segment_handle); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 886 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 887 | /*Update segment info*/ |
| 888 | memcpy(p_info, &p_ctx->segment_info, sizeof(p_ctx->segment_info)); |
| 889 | |
| 890 | ret = segment_store_info(p_ctx->segment_handle, p_info); |
Zhiqiang Han | 5c805cf | 2020-05-09 16:51:08 +0800 | [diff] [blame] | 891 | //DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
| 892 | if (ret != DVR_SUCCESS) |
| 893 | goto end; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 894 | |
hualing chen | b9a0292 | 2021-12-14 11:29:47 +0800 | [diff] [blame] | 895 | segment_store_allInfo(p_ctx->segment_handle, p_info); |
| 896 | |
Wentao MA | 96f6896 | 2022-06-15 19:45:35 +0800 | [diff] [blame] | 897 | DVR_INFO("%s dump segment info, id:%lld, nb_pids:%d, duration:%ld ms, size:%zu, nb_packets:%d", |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 898 | __func__, p_info->id, p_info->nb_pids, p_info->duration, p_info->size, p_info->nb_packets); |
| 899 | |
Zhiqiang Han | 5c805cf | 2020-05-09 16:51:08 +0800 | [diff] [blame] | 900 | end: |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 901 | ret = segment_close(p_ctx->segment_handle); |
hualing chen | cedcb86 | 2022-01-04 15:32:43 +0800 | [diff] [blame] | 902 | p_ctx->segment_handle = NULL; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 903 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 904 | return DVR_SUCCESS; |
| 905 | } |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 906 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 907 | int dvr_record_resume_segment(DVR_RecordHandle_t handle, DVR_RecordStartParams_t *params, uint64_t *p_resume_size) |
| 908 | { |
| 909 | DVR_RecordContext_t *p_ctx; |
| 910 | uint32_t i; |
Wentao MA | 139fc61 | 2022-08-29 14:10:07 +0800 | [diff] [blame] | 911 | int ret = DVR_SUCCESS; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 912 | |
| 913 | p_ctx = (DVR_RecordContext_t *)handle; |
| 914 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 915 | if (p_ctx == &record_ctx[i]) |
| 916 | break; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 917 | } |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 918 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
| 919 | DVR_RETURN_IF_FALSE(params); |
| 920 | DVR_RETURN_IF_FALSE(p_resume_size); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 921 | |
Wentao MA | 96f6896 | 2022-06-15 19:45:35 +0800 | [diff] [blame] | 922 | DVR_INFO("%s , current state:%d, resume size:%lld", __func__, p_ctx->state, *p_resume_size); |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 923 | ret = dvr_record_start_segment(handle, params); |
| 924 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
| 925 | |
| 926 | p_ctx->segment_info.size = *p_resume_size; |
| 927 | |
| 928 | return DVR_SUCCESS; |
| 929 | } |
| 930 | |
| 931 | int dvr_record_get_status(DVR_RecordHandle_t handle, DVR_RecordStatus_t *p_status) |
| 932 | { |
| 933 | DVR_RecordContext_t *p_ctx; |
| 934 | int i; |
| 935 | |
| 936 | p_ctx = (DVR_RecordContext_t *)handle; |
| 937 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 938 | if (p_ctx == &record_ctx[i]) |
| 939 | break; |
| 940 | } |
| 941 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
| 942 | DVR_RETURN_IF_FALSE(p_status); |
| 943 | |
| 944 | //lock |
| 945 | p_status->state = p_ctx->state; |
| 946 | p_status->info.id = p_ctx->segment_info.id; |
| 947 | p_status->info.duration = p_ctx->segment_info.duration; |
| 948 | p_status->info.size = p_ctx->segment_info.size; |
| 949 | p_status->info.nb_packets = p_ctx->segment_info.size/188; |
| 950 | |
| 951 | return DVR_SUCCESS; |
| 952 | } |
| 953 | |
| 954 | int dvr_record_write(DVR_RecordHandle_t handle, void *buffer, uint32_t len) |
| 955 | { |
| 956 | DVR_RecordContext_t *p_ctx; |
| 957 | uint32_t i; |
| 958 | off_t pos = 0; |
Wentao MA | 139fc61 | 2022-08-29 14:10:07 +0800 | [diff] [blame] | 959 | int ret = DVR_SUCCESS; |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 960 | int has_pcr; |
| 961 | |
| 962 | p_ctx = (DVR_RecordContext_t *)handle; |
| 963 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 964 | if (p_ctx == &record_ctx[i]) |
| 965 | break; |
| 966 | } |
| 967 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
| 968 | DVR_RETURN_IF_FALSE(buffer); |
| 969 | DVR_RETURN_IF_FALSE(len); |
| 970 | |
| 971 | pos = segment_tell_position(p_ctx->segment_handle); |
| 972 | has_pcr = record_do_pcr_index(p_ctx, buffer, len); |
| 973 | if (has_pcr == 0) { |
Wentao MA | 270dc0f | 2022-08-23 13:17:26 +0800 | [diff] [blame] | 974 | /* Pull VOD record should use PCR time index */ |
Wentao MA | 96f6896 | 2022-06-15 19:45:35 +0800 | [diff] [blame] | 975 | DVR_INFO("%s has no pcr, can NOT do time index", __func__); |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 976 | } |
| 977 | ret = segment_write(p_ctx->segment_handle, buffer, len); |
hualing chen | 2932d37 | 2020-04-29 13:44:00 +0800 | [diff] [blame] | 978 | if (ret != len) { |
Wentao MA | 96f6896 | 2022-06-15 19:45:35 +0800 | [diff] [blame] | 979 | DVR_INFO("%s write error ret:%d len:%d", __func__, ret, len); |
hualing chen | 2932d37 | 2020-04-29 13:44:00 +0800 | [diff] [blame] | 980 | } |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 981 | p_ctx->segment_info.size += len; |
| 982 | p_ctx->segment_info.nb_packets = p_ctx->segment_info.size/188; |
| 983 | |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 984 | return DVR_SUCCESS; |
| 985 | } |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 986 | |
pengfei.liu | 27cc4ec | 2020-04-03 16:28:16 +0800 | [diff] [blame] | 987 | int dvr_record_set_encrypt_callback(DVR_RecordHandle_t handle, DVR_CryptoFunction_t func, void *userdata) |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 988 | { |
| 989 | DVR_RecordContext_t *p_ctx; |
| 990 | uint32_t i; |
| 991 | |
| 992 | p_ctx = (DVR_RecordContext_t *)handle; |
| 993 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 994 | if (p_ctx == &record_ctx[i]) |
| 995 | break; |
| 996 | } |
| 997 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
| 998 | DVR_RETURN_IF_FALSE(func); |
| 999 | |
Wentao MA | 96f6896 | 2022-06-15 19:45:35 +0800 | [diff] [blame] | 1000 | DVR_INFO("%s , current state:%d", __func__, p_ctx->state); |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 1001 | DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_STARTED); |
| 1002 | DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED); |
| 1003 | |
| 1004 | p_ctx->enc_func = func; |
| 1005 | p_ctx->enc_userdata = userdata; |
| 1006 | return DVR_SUCCESS; |
| 1007 | } |
| 1008 | |
| 1009 | int dvr_record_set_secure_buffer(DVR_RecordHandle_t handle, uint8_t *p_secure_buf, uint32_t len) |
| 1010 | { |
| 1011 | DVR_RecordContext_t *p_ctx; |
| 1012 | uint32_t i; |
Wentao MA | 139fc61 | 2022-08-29 14:10:07 +0800 | [diff] [blame] | 1013 | int ret = DVR_SUCCESS; |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 1014 | |
| 1015 | p_ctx = (DVR_RecordContext_t *)handle; |
| 1016 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 1017 | if (p_ctx == &record_ctx[i]) |
| 1018 | break; |
| 1019 | } |
| 1020 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
| 1021 | DVR_RETURN_IF_FALSE(p_secure_buf); |
| 1022 | DVR_RETURN_IF_FALSE(len); |
| 1023 | |
Wentao MA | 96f6896 | 2022-06-15 19:45:35 +0800 | [diff] [blame] | 1024 | DVR_INFO("%s , current state:%d", __func__, p_ctx->state); |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 1025 | DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_STARTED); |
| 1026 | DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED); |
| 1027 | |
| 1028 | ret = record_device_set_secure_buffer(p_ctx->dev_handle, p_secure_buf, len); |
| 1029 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
| 1030 | |
| 1031 | p_ctx->is_secure_mode = 1; |
Yahui Han | 4577db8 | 2022-07-05 17:46:19 +0800 | [diff] [blame] | 1032 | p_ctx->secbuf_size = len; |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 1033 | return ret; |
| 1034 | } |
hualing chen | 4fe3bee | 2020-10-23 13:58:52 +0800 | [diff] [blame] | 1035 | |
| 1036 | int dvr_record_is_secure_mode(DVR_RecordHandle_t handle) |
| 1037 | { |
| 1038 | DVR_RecordContext_t *p_ctx; |
| 1039 | uint32_t i; |
Wentao MA | 139fc61 | 2022-08-29 14:10:07 +0800 | [diff] [blame] | 1040 | int ret = DVR_SUCCESS; |
hualing chen | 4fe3bee | 2020-10-23 13:58:52 +0800 | [diff] [blame] | 1041 | |
| 1042 | p_ctx = (DVR_RecordContext_t *)handle; |
| 1043 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 1044 | if (p_ctx == &record_ctx[i]) |
| 1045 | break; |
| 1046 | } |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 1047 | |
hualing chen | 4fe3bee | 2020-10-23 13:58:52 +0800 | [diff] [blame] | 1048 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
| 1049 | |
| 1050 | if (p_ctx->is_secure_mode == 1) |
| 1051 | ret = 1; |
| 1052 | else |
| 1053 | ret = 0; |
| 1054 | return ret; |
Yahui Han | ce15e9c | 2020-12-08 18:08:32 +0800 | [diff] [blame] | 1055 | } |
Wentao MA | f407203 | 2022-06-30 13:50:45 +0800 | [diff] [blame] | 1056 | |
| 1057 | int dvr_record_discard_coming_data(DVR_RecordHandle_t handle, DVR_Bool_t discard) |
| 1058 | { |
| 1059 | DVR_RecordContext_t *p_ctx; |
| 1060 | int i; |
| 1061 | |
| 1062 | p_ctx = (DVR_RecordContext_t *)handle; |
| 1063 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 1064 | if (p_ctx == &record_ctx[i]) |
| 1065 | break; |
| 1066 | } |
| 1067 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
| 1068 | |
| 1069 | if (p_ctx->discard_coming_data != discard) { |
| 1070 | p_ctx->discard_coming_data = discard; |
| 1071 | if (discard) { |
| 1072 | DVR_WARN("%s, start discarding coming data. discard:%d",__func__,discard); |
| 1073 | } else { |
| 1074 | DVR_WARN("%s, finish discarding coming data. discard:%d",__func__,discard); |
| 1075 | } |
| 1076 | } |
| 1077 | |
| 1078 | return DVR_TRUE; |
| 1079 | } |