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