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" |
| 9 | #include "record_device.h" |
| 10 | #include "segment.h" |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 11 | |
| 12 | #define MAX_DVR_RECORD_SESSION_COUNT 2 |
hualing chen | 266b950 | 2020-04-04 17:39:39 +0800 | [diff] [blame] | 13 | #define RECORD_BLOCK_SIZE (256 * 1024) |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 14 | |
| 15 | /**\brief DVR index file type*/ |
| 16 | typedef enum { |
| 17 | DVR_INDEX_TYPE_PCR, /**< DVR index file use pcr*/ |
| 18 | DVR_INDEX_TYPE_LOCAL_CLOCK, /**< DVR index file use local clock*/ |
| 19 | DVR_INDEX_TYPE_INVALID /**< DVR index file type invalid type*/ |
| 20 | } DVR_IndexType_t; |
| 21 | |
| 22 | /**\brief DVR VOD context*/ |
| 23 | typedef struct { |
| 24 | pthread_mutex_t mutex; /**< VOD mutex lock*/ |
| 25 | pthread_cond_t cond; /**< VOD condition*/ |
| 26 | void *buffer; /**< VOD buffer*/ |
| 27 | uint32_t buf_len; /**< VOD buffer len*/ |
| 28 | } DVR_VodContext_t; |
| 29 | |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 30 | /**\brief DVR record secure mode buffer*/ |
| 31 | typedef struct { |
| 32 | uint32_t addr; /**< Secure mode record buffer address*/ |
| 33 | uint32_t len; /**< Secure mode record buffer length*/ |
| 34 | } DVR_SecureBuffer_t; |
| 35 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 36 | /**\brief DVR record context*/ |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 37 | typedef struct { |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 38 | pthread_t thread; /**< DVR thread handle*/ |
| 39 | Record_DeviceHandle_t dev_handle; /**< DVR device handle*/ |
| 40 | Segment_Handle_t segment_handle; /**< DVR segment handle*/ |
| 41 | DVR_RecordState_t state; /**< DVR record state*/ |
| 42 | char location[DVR_MAX_LOCATION_SIZE]; /**< DVR record file location*/ |
| 43 | DVR_RecordSegmentStartParams_t segment_params; /**< DVR record start parameters*/ |
| 44 | DVR_RecordSegmentInfo_t segment_info; /**< DVR record current segment info*/ |
| 45 | size_t notification_size; /**< DVR record nogification size*/ |
| 46 | DVR_RecordEventFunction_t event_notify_fn; /**< DVR record event notify function*/ |
| 47 | void *event_userdata; /**< DVR record event userdata*/ |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 48 | //DVR_VodContext_t vod; /**< DVR record vod context*/ |
| 49 | int is_vod; /**< Indicate current mode is VOD record mode*/ |
pengfei.liu | 27cc4ec | 2020-04-03 16:28:16 +0800 | [diff] [blame] | 50 | DVR_CryptoFunction_t enc_func; /**< Encrypt function*/ |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 51 | void *enc_userdata; /**< Encrypt userdata*/ |
| 52 | int is_secure_mode; /**< Record session run in secure pipeline */ |
hualing chen | 2615aa8 | 2020-04-02 21:32:51 +0800 | [diff] [blame] | 53 | size_t last_send_size; /**< Last send notify segment size */ |
pengfei.liu | fda2a97 | 2020-04-09 14:47:15 +0800 | [diff] [blame^] | 54 | uint32_t block_size; /**< DVR record block size */ |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 55 | } DVR_RecordContext_t; |
| 56 | |
| 57 | static DVR_RecordContext_t record_ctx[MAX_DVR_RECORD_SESSION_COUNT] = { |
| 58 | { |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 59 | .state = DVR_RECORD_STATE_CLOSED |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 60 | }, |
| 61 | { |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 62 | .state = DVR_RECORD_STATE_CLOSED |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 63 | } |
| 64 | }; |
| 65 | |
Zhiqiang Han | 51646a0 | 2020-04-05 18:43:22 +0800 | [diff] [blame] | 66 | static int record_is_valid_pid(DVR_RecordContext_t *p_ctx, int pid) |
| 67 | { |
| 68 | int i; |
| 69 | |
| 70 | for (i = 0; i < p_ctx->segment_info.nb_pids; i++) { |
| 71 | if (pid == p_ctx->segment_info.pids[i].pid) |
| 72 | return 1; |
| 73 | } |
| 74 | return 0; |
| 75 | } |
| 76 | |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 77 | static int record_save_pcr(DVR_RecordContext_t *p_ctx, uint8_t *buf, loff_t pos) |
| 78 | { |
| 79 | uint8_t *p = buf; |
| 80 | int len; |
| 81 | uint8_t afc; |
| 82 | uint64_t pcr = 0; |
| 83 | int has_pcr = 0; |
| 84 | int pid; |
| 85 | int adp_field_len; |
| 86 | |
| 87 | pid = ((p[1] & 0x1f) << 8) | p[2]; |
Zhiqiang Han | 51646a0 | 2020-04-05 18:43:22 +0800 | [diff] [blame] | 88 | if (pid == 0x1fff || !record_is_valid_pid(p_ctx, pid)) |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 89 | return has_pcr; |
| 90 | |
| 91 | //scramble = p[3] >> 6; |
| 92 | //cc = p[3] & 0x0f; |
| 93 | afc = (p[3] >> 4) & 0x03; |
| 94 | |
| 95 | p += 4; |
| 96 | len = 184; |
| 97 | |
| 98 | if (afc & 2) { |
| 99 | adp_field_len = p[0]; |
| 100 | /* Skip adaptation len */ |
| 101 | p++; |
| 102 | len--; |
| 103 | /* Parse pcr field, see 13818 spec table I-2-6,adaptation_field */ |
| 104 | if (p[0] & 0x10 && len >= 6) { |
| 105 | /* get pcr value,pcr is 33bit value */ |
| 106 | pcr = (((uint64_t)(p[1])) << 25) |
| 107 | | (((uint64_t)p[2]) << 17) |
| 108 | | (((uint64_t)(p[3])) << 9) |
| 109 | | (((uint64_t)p[4]) << 1) |
| 110 | | ((((uint64_t)p[5]) & 0x80) >> 7); |
| 111 | has_pcr = 1; |
| 112 | } |
| 113 | |
| 114 | p += adp_field_len; |
| 115 | len -= adp_field_len; |
| 116 | |
| 117 | if (len < 0) { |
| 118 | DVR_DEBUG(1, "parser pcr: illegal adaptation field length"); |
| 119 | return 0; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | if (has_pcr) { |
| 124 | segment_update_pts(p_ctx->segment_handle, pcr/90, pos); |
| 125 | } |
| 126 | return has_pcr; |
| 127 | } |
| 128 | |
| 129 | static int record_do_pcr_index(DVR_RecordContext_t *p_ctx, uint8_t *buf, int len) |
| 130 | { |
| 131 | uint8_t *p = buf; |
| 132 | int left = len; |
| 133 | loff_t pos; |
| 134 | int has_pcr = 0; |
| 135 | |
| 136 | pos = segment_tell_position(p_ctx->segment_handle); |
| 137 | while (left >= 188) { |
| 138 | if (*p == 0x47) { |
| 139 | has_pcr |= record_save_pcr(p_ctx, p, pos); |
| 140 | p += 188; |
| 141 | left -= 188; |
| 142 | pos += 188; |
| 143 | } else { |
| 144 | p++; |
| 145 | left --; |
| 146 | pos++; |
| 147 | } |
| 148 | } |
| 149 | return has_pcr; |
| 150 | } |
| 151 | |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 152 | void *record_thread(void *arg) |
| 153 | { |
| 154 | DVR_RecordContext_t *p_ctx = (DVR_RecordContext_t *)arg; |
| 155 | ssize_t len; |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 156 | uint8_t *buf, *buf_out; |
pengfei.liu | fda2a97 | 2020-04-09 14:47:15 +0800 | [diff] [blame^] | 157 | uint32_t block_size = p_ctx->block_size; |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 158 | loff_t pos = 0; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 159 | int ret; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 160 | struct timespec start_ts, end_ts; |
| 161 | DVR_RecordStatus_t record_status; |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 162 | int has_pcr; |
| 163 | int index_type = DVR_INDEX_TYPE_INVALID; |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 164 | time_t pre_time = 0; |
| 165 | #define DVR_STORE_INFO_TIME (1000) |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 166 | DVR_SecureBuffer_t secure_buf; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 167 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 168 | buf = (uint8_t *)malloc(block_size); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 169 | if (!buf) { |
| 170 | DVR_DEBUG(1, "%s, malloc failed", __func__); |
| 171 | return NULL; |
| 172 | } |
| 173 | |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 174 | buf_out = (uint8_t *)malloc(block_size + 188); |
| 175 | if (!buf_out) { |
| 176 | DVR_DEBUG(1, "%s, malloc failed", __func__); |
| 177 | return NULL; |
| 178 | } |
| 179 | |
hualing chen | 266b950 | 2020-04-04 17:39:39 +0800 | [diff] [blame] | 180 | memset(&record_status, 0, sizeof(record_status)); |
| 181 | record_status.state = DVR_RECORD_STATE_STARTED; |
pengfei.liu | fda2a97 | 2020-04-09 14:47:15 +0800 | [diff] [blame^] | 182 | if (p_ctx->event_notify_fn) { |
| 183 | p_ctx->event_notify_fn(DVR_RECORD_EVENT_STATUS, &record_status, p_ctx->event_userdata); |
| 184 | DVR_DEBUG(1, "%s notify record status, state:%d", |
hualing chen | 266b950 | 2020-04-04 17:39:39 +0800 | [diff] [blame] | 185 | __func__, record_status.state); |
pengfei.liu | fda2a97 | 2020-04-09 14:47:15 +0800 | [diff] [blame^] | 186 | } |
| 187 | DVR_DEBUG(1, "%s, secure_mode:%d, block_size:%d", __func__, p_ctx->is_secure_mode, block_size); |
hualing chen | 266b950 | 2020-04-04 17:39:39 +0800 | [diff] [blame] | 188 | |
| 189 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 190 | clock_gettime(CLOCK_MONOTONIC, &start_ts); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 191 | while (p_ctx->state == DVR_RECORD_STATE_STARTED) { |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 192 | /* data from dmx, normal dvr case */ |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 193 | if (p_ctx->is_secure_mode) { |
| 194 | memset(&secure_buf, 0, sizeof(secure_buf)); |
| 195 | len = record_device_read(p_ctx->dev_handle, &secure_buf, sizeof(secure_buf), 1000); |
hualing chen | 266b950 | 2020-04-04 17:39:39 +0800 | [diff] [blame] | 196 | if (len != DVR_FAILURE) { |
| 197 | DVR_DEBUG(1, "%s, secure_buf:%#x, size:%#x", __func__, secure_buf.addr, secure_buf.len); |
| 198 | } |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 199 | } else { |
| 200 | len = record_device_read(p_ctx->dev_handle, buf, block_size, 1000); |
| 201 | } |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 202 | if (len == DVR_FAILURE) { |
| 203 | usleep(10*1000); |
| 204 | continue; |
| 205 | } |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 206 | |
| 207 | /* Got data from device, record it */ |
| 208 | if (p_ctx->enc_func) { |
| 209 | /* Encrypt record data */ |
pengfei.liu | 27cc4ec | 2020-04-03 16:28:16 +0800 | [diff] [blame] | 210 | DVR_CryptoParams_t crypto_params; |
| 211 | |
| 212 | memset(&crypto_params, 0, sizeof(crypto_params)); |
| 213 | crypto_params.type = DVR_CRYPTO_TYPE_ENCRYPT; |
| 214 | memcpy(crypto_params.location, p_ctx->location, strlen(p_ctx->location)); |
| 215 | crypto_params.segment_id = p_ctx->segment_info.id; |
| 216 | crypto_params.offset = p_ctx->segment_info.size; |
| 217 | |
| 218 | if (p_ctx->is_secure_mode) { |
| 219 | crypto_params.input_buffer.type = DVR_BUFFER_TYPE_SECURE; |
| 220 | crypto_params.input_buffer.addr = secure_buf.addr; |
| 221 | crypto_params.input_buffer.size = secure_buf.len; |
| 222 | } else { |
| 223 | crypto_params.input_buffer.type = DVR_BUFFER_TYPE_NORMAL; |
| 224 | crypto_params.input_buffer.addr = (size_t)buf; |
| 225 | crypto_params.input_buffer.size = len; |
| 226 | } |
| 227 | |
| 228 | crypto_params.output_buffer.type = DVR_BUFFER_TYPE_NORMAL; |
| 229 | crypto_params.output_buffer.addr = (size_t)buf_out; |
| 230 | crypto_params.output_buffer.size = block_size + 188; |
| 231 | |
| 232 | p_ctx->enc_func(&crypto_params, p_ctx->enc_userdata); |
| 233 | /* Out buffer length may not equal in buffer length */ |
| 234 | ret = segment_write(p_ctx->segment_handle, buf_out, crypto_params.output_size); |
| 235 | len = crypto_params.output_size; |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 236 | } else { |
| 237 | ret = segment_write(p_ctx->segment_handle, buf, len); |
| 238 | } |
hualing chen | 626204e | 2020-04-07 11:55:08 +0800 | [diff] [blame] | 239 | //add DVR_RECORD_EVENT_WRITE_ERROR event if write error |
pengfei.liu | fda2a97 | 2020-04-09 14:47:15 +0800 | [diff] [blame^] | 240 | if (ret == -1 && len > 0 && p_ctx->event_notify_fn) { |
hualing chen | 626204e | 2020-04-07 11:55:08 +0800 | [diff] [blame] | 241 | //send write event |
| 242 | p_ctx->event_notify_fn(DVR_RECORD_EVENT_WRITE_ERROR, NULL, p_ctx->event_userdata); |
pengfei.liu | fda2a97 | 2020-04-09 14:47:15 +0800 | [diff] [blame^] | 243 | DVR_DEBUG(1, "%s notify DVR_RECORD_EVENT_WRITE_ERROR, exit record thread", __func__); |
hualing chen | 626204e | 2020-04-07 11:55:08 +0800 | [diff] [blame] | 244 | goto end; |
| 245 | } |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 246 | /* Do time index */ |
| 247 | uint8_t *index_buf = p_ctx->enc_func ? buf_out : buf; |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 248 | pos = segment_tell_position(p_ctx->segment_handle); |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 249 | has_pcr = record_do_pcr_index(p_ctx, index_buf, len); |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 250 | if (has_pcr == 0 && index_type == DVR_INDEX_TYPE_INVALID) { |
| 251 | clock_gettime(CLOCK_MONOTONIC, &end_ts); |
| 252 | if ((end_ts.tv_sec*1000 + end_ts.tv_nsec/1000000) - |
| 253 | (start_ts.tv_sec*1000 + start_ts.tv_nsec/1000000) > 40) { |
| 254 | /* PCR interval threshlod > 40 ms*/ |
| 255 | DVR_DEBUG(1, "%s use local clock time index", __func__); |
| 256 | index_type = DVR_INDEX_TYPE_LOCAL_CLOCK; |
| 257 | } |
| 258 | } else if (has_pcr && index_type == DVR_INDEX_TYPE_INVALID){ |
| 259 | DVR_DEBUG(1, "%s use pcr time index", __func__); |
| 260 | index_type = DVR_INDEX_TYPE_PCR; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 261 | } |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 262 | |
| 263 | /* Update segment info */ |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 264 | p_ctx->segment_info.size += len; |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 265 | /*Duration need use pcr to calculate, todo...*/ |
| 266 | if (index_type == DVR_INDEX_TYPE_PCR) { |
pengfei.liu | 8b56329 | 2020-02-26 15:49:02 +0800 | [diff] [blame] | 267 | p_ctx->segment_info.duration = segment_tell_total_time(p_ctx->segment_handle); |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 268 | if (pre_time == 0) |
| 269 | pre_time = p_ctx->segment_info.duration; |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 270 | } else if (index_type == DVR_INDEX_TYPE_LOCAL_CLOCK) { |
| 271 | clock_gettime(CLOCK_MONOTONIC, &end_ts); |
| 272 | p_ctx->segment_info.duration = (end_ts.tv_sec*1000 + end_ts.tv_nsec/1000000) - |
| 273 | (start_ts.tv_sec*1000 + start_ts.tv_nsec/1000000); |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 274 | if (pre_time == 0) |
| 275 | pre_time = p_ctx->segment_info.duration; |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 276 | segment_update_pts(p_ctx->segment_handle, p_ctx->segment_info.duration, pos); |
| 277 | } else { |
| 278 | DVR_DEBUG(1, "%s can NOT do time index", __func__); |
| 279 | } |
| 280 | p_ctx->segment_info.nb_packets = p_ctx->segment_info.size/188; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 281 | |
hualing chen | 87072a8 | 2020-03-12 16:20:12 +0800 | [diff] [blame] | 282 | if (p_ctx->segment_info.duration - pre_time > DVR_STORE_INFO_TIME) { |
| 283 | pre_time = p_ctx->segment_info.duration + DVR_STORE_INFO_TIME; |
| 284 | segment_store_info(p_ctx->segment_handle, &(p_ctx->segment_info)); |
| 285 | } |
| 286 | /*Event notification*/ |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 287 | if (p_ctx->notification_size && |
| 288 | p_ctx->event_notify_fn && |
hualing chen | 2615aa8 | 2020-04-02 21:32:51 +0800 | [diff] [blame] | 289 | /*!(p_ctx->segment_info.size % p_ctx->notification_size)*/ |
| 290 | (p_ctx->segment_info.size -p_ctx->last_send_size) >= p_ctx->notification_size&& |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 291 | p_ctx->segment_info.duration > 0) { |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 292 | memset(&record_status, 0, sizeof(record_status)); |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 293 | //clock_gettime(CLOCK_MONOTONIC, &end_ts); |
hualing chen | 2615aa8 | 2020-04-02 21:32:51 +0800 | [diff] [blame] | 294 | p_ctx->last_send_size = p_ctx->segment_info.size; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 295 | record_status.state = p_ctx->state; |
| 296 | record_status.info.id = p_ctx->segment_info.id; |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 297 | record_status.info.duration = p_ctx->segment_info.duration; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 298 | record_status.info.size = p_ctx->segment_info.size; |
| 299 | record_status.info.nb_packets = p_ctx->segment_info.size/188; |
| 300 | p_ctx->event_notify_fn(DVR_RECORD_EVENT_STATUS, &record_status, p_ctx->event_userdata); |
| 301 | DVR_DEBUG(1, "%s notify record status, state:%d, id:%lld, duration:%ld ms, size:%zu", |
| 302 | __func__, record_status.state, record_status.info.id, record_status.info.duration, record_status.info.size); |
| 303 | } |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 304 | } |
hualing chen | 626204e | 2020-04-07 11:55:08 +0800 | [diff] [blame] | 305 | end: |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 306 | free((void *)buf); |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 307 | free((void *)buf_out); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 308 | DVR_DEBUG(1, "exit %s", __func__); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 309 | return NULL; |
| 310 | } |
| 311 | |
| 312 | int dvr_record_open(DVR_RecordHandle_t *p_handle, DVR_RecordOpenParams_t *params) |
| 313 | { |
| 314 | DVR_RecordContext_t *p_ctx; |
| 315 | Record_DeviceOpenParams_t dev_open_params; |
| 316 | int ret; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 317 | uint32_t i; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 318 | |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 319 | DVR_RETURN_IF_FALSE(p_handle); |
| 320 | DVR_RETURN_IF_FALSE(params); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 321 | |
| 322 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 323 | if (record_ctx[i].state == DVR_RECORD_STATE_CLOSED) { |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 324 | break; |
| 325 | } |
| 326 | } |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 327 | DVR_RETURN_IF_FALSE(record_ctx[i].state == DVR_RECORD_STATE_CLOSED); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 328 | p_ctx = &record_ctx[i]; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 329 | DVR_DEBUG(1, "%s , current state:%d, dmx_id:%d, notification_size:%zu", __func__, |
| 330 | p_ctx->state, params->dmx_dev_id, params->notification_size); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 331 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 332 | /*Process event params*/ |
| 333 | p_ctx->notification_size = params->notification_size; |
| 334 | p_ctx->event_notify_fn = params->event_fn; |
| 335 | p_ctx->event_userdata = params->event_userdata; |
hualing chen | 2615aa8 | 2020-04-02 21:32:51 +0800 | [diff] [blame] | 336 | p_ctx->last_send_size = 0; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 337 | /*Process crypto params, todo*/ |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 338 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 339 | memset((void *)&dev_open_params, 0, sizeof(dev_open_params)); |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 340 | if (params->data_from_memory) { |
| 341 | /* data from memory, VOD case */ |
| 342 | p_ctx->is_vod = 1; |
| 343 | } else { |
| 344 | p_ctx->is_vod = 0; |
| 345 | /* data from dmx, normal dvr case */ |
hualing chen | 958fe4c | 2020-03-24 17:35:07 +0800 | [diff] [blame] | 346 | dev_open_params.dmx_dev_id = params->dmx_dev_id; |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 347 | dev_open_params.buf_size = (params->flush_size > 0 ? params->flush_size : RECORD_BLOCK_SIZE); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 348 | |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 349 | ret = record_device_open(&p_ctx->dev_handle, &dev_open_params); |
| 350 | if (ret != DVR_SUCCESS) { |
| 351 | DVR_DEBUG(1, "%s, open record devices failed", __func__); |
| 352 | return DVR_FAILURE; |
| 353 | } |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 354 | } |
| 355 | |
pengfei.liu | fda2a97 | 2020-04-09 14:47:15 +0800 | [diff] [blame^] | 356 | p_ctx->block_size = (params->flush_size > 0 ? params->flush_size : RECORD_BLOCK_SIZE); |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 357 | p_ctx->enc_func = NULL; |
| 358 | p_ctx->enc_userdata = NULL; |
| 359 | p_ctx->is_secure_mode = 0; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 360 | p_ctx->state = DVR_RECORD_STATE_OPENED; |
| 361 | |
Pengfei Liu | 47ed6c9 | 2020-01-17 11:23:41 +0800 | [diff] [blame] | 362 | *p_handle = p_ctx; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 363 | return DVR_SUCCESS; |
| 364 | } |
| 365 | |
| 366 | int dvr_record_close(DVR_RecordHandle_t handle) |
| 367 | { |
| 368 | DVR_RecordContext_t *p_ctx; |
| 369 | int ret; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 370 | uint32_t i; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 371 | |
Pengfei Liu | 47ed6c9 | 2020-01-17 11:23:41 +0800 | [diff] [blame] | 372 | p_ctx = (DVR_RecordContext_t *)handle; |
| 373 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 374 | if (p_ctx == &record_ctx[i]) |
| 375 | break; |
| 376 | } |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 377 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 378 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 379 | DVR_DEBUG(1, "%s , current state:%d", __func__, p_ctx->state); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 380 | DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 381 | |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 382 | if (p_ctx->is_vod) { |
| 383 | ret = DVR_SUCCESS; |
| 384 | } else { |
| 385 | ret = record_device_close(p_ctx->dev_handle); |
| 386 | if (ret != DVR_SUCCESS) { |
| 387 | DVR_DEBUG(1, "%s, failed", __func__); |
| 388 | } |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 389 | } |
| 390 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 391 | p_ctx->state = DVR_RECORD_STATE_CLOSED; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 392 | return ret; |
| 393 | } |
| 394 | |
Pengfei Liu | 47ed6c9 | 2020-01-17 11:23:41 +0800 | [diff] [blame] | 395 | #if 0 |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 396 | int dvr_record_register_encryption(DVR_RecordHandle_t handle, |
| 397 | DVR_CryptoFunction_t cb, |
| 398 | DVR_CryptoParams_t params, |
| 399 | void *userdata) |
| 400 | { |
| 401 | return DVR_SUCCESS; |
| 402 | } |
Pengfei Liu | 47ed6c9 | 2020-01-17 11:23:41 +0800 | [diff] [blame] | 403 | #endif |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 404 | |
| 405 | int dvr_record_start_segment(DVR_RecordHandle_t handle, DVR_RecordStartParams_t *params) |
| 406 | { |
| 407 | DVR_RecordContext_t *p_ctx; |
| 408 | Segment_OpenParams_t open_params; |
| 409 | int ret; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 410 | uint32_t i; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 411 | |
Pengfei Liu | 47ed6c9 | 2020-01-17 11:23:41 +0800 | [diff] [blame] | 412 | p_ctx = (DVR_RecordContext_t *)handle; |
| 413 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 414 | if (p_ctx == &record_ctx[i]) |
| 415 | break; |
| 416 | } |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 417 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 418 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 419 | DVR_DEBUG(1, "%s , current state:%d", __func__, p_ctx->state); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 420 | DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_STARTED); |
| 421 | DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED); |
| 422 | DVR_RETURN_IF_FALSE(params); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 423 | |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 424 | 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] | 425 | memset(&open_params, 0, sizeof(open_params)); |
| 426 | memcpy(open_params.location, params->location, strlen(params->location)); |
| 427 | open_params.segment_id = params->segment.segment_id; |
| 428 | open_params.mode = SEGMENT_MODE_WRITE; |
| 429 | |
| 430 | ret = segment_open(&open_params, &p_ctx->segment_handle); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 431 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 432 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 433 | /*process params*/ |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 434 | { |
| 435 | memcpy(p_ctx->location, params->location, strlen(params->location)); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 436 | //need all params?? |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 437 | memcpy(&p_ctx->segment_params, ¶ms->segment, sizeof(params->segment)); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 438 | /*save current segment info*/ |
| 439 | memset(&p_ctx->segment_info, 0, sizeof(p_ctx->segment_info)); |
| 440 | p_ctx->segment_info.id = params->segment.segment_id; |
| 441 | p_ctx->segment_info.nb_pids = params->segment.nb_pids; |
| 442 | 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] | 443 | } |
| 444 | |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 445 | if (!p_ctx->is_vod) { |
| 446 | /* normal dvr case */ |
| 447 | for (i = 0; i < params->segment.nb_pids; i++) { |
| 448 | ret = record_device_add_pid(p_ctx->dev_handle, params->segment.pids[i].pid); |
| 449 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
| 450 | } |
| 451 | ret = record_device_start(p_ctx->dev_handle); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 452 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 453 | } |
| 454 | |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 455 | p_ctx->state = DVR_RECORD_STATE_STARTED; |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 456 | if (!p_ctx->is_vod) |
| 457 | pthread_create(&p_ctx->thread, NULL, record_thread, p_ctx); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 458 | |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 459 | return DVR_SUCCESS; |
| 460 | } |
| 461 | |
| 462 | int dvr_record_next_segment(DVR_RecordHandle_t handle, DVR_RecordStartParams_t *params, DVR_RecordSegmentInfo_t *p_info) |
| 463 | { |
| 464 | DVR_RecordContext_t *p_ctx; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 465 | Segment_OpenParams_t open_params; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 466 | int ret; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 467 | uint32_t i; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 468 | |
Pengfei Liu | 47ed6c9 | 2020-01-17 11:23:41 +0800 | [diff] [blame] | 469 | p_ctx = (DVR_RecordContext_t *)handle; |
| 470 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 471 | if (p_ctx == &record_ctx[i]) |
| 472 | break; |
| 473 | } |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 474 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 475 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 476 | DVR_DEBUG(1, "%s , current state:%d", __func__, p_ctx->state); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 477 | DVR_RETURN_IF_FALSE(p_ctx->state == DVR_RECORD_STATE_STARTED); |
| 478 | //DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED); |
| 479 | DVR_RETURN_IF_FALSE(params); |
| 480 | DVR_RETURN_IF_FALSE(p_info); |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 481 | DVR_RETURN_IF_FALSE(!p_ctx->is_vod); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 482 | |
| 483 | /*Stop the on going record segment*/ |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 484 | //ret = record_device_stop(p_ctx->dev_handle); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 485 | //DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 486 | |
| 487 | p_ctx->state = DVR_RECORD_STATE_STOPPED; |
| 488 | pthread_join(p_ctx->thread, NULL); |
| 489 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 490 | /*Update segment info*/ |
| 491 | memcpy(p_info, &p_ctx->segment_info, sizeof(p_ctx->segment_info)); |
| 492 | |
| 493 | ret = segment_store_info(p_ctx->segment_handle, p_info); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 494 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 495 | |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 496 | DVR_DEBUG(1, "%s dump segment info, id:%lld, nb_pids:%d, duration:%ld ms, size:%zu, nb_packets:%d params->segment.nb_pids:%d", |
| 497 | __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] | 498 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 499 | /*Close current segment*/ |
| 500 | ret = segment_close(p_ctx->segment_handle); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 501 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 502 | /*Open the new record segment*/ |
| 503 | memset(&open_params, 0, sizeof(open_params)); |
| 504 | memcpy(open_params.location, p_ctx->location, strlen(p_ctx->location)); |
| 505 | open_params.segment_id = params->segment.segment_id; |
| 506 | open_params.mode = SEGMENT_MODE_WRITE; |
| 507 | |
| 508 | ret = segment_open(&open_params, &p_ctx->segment_handle); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 509 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 510 | /*process params*/ |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 511 | { |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 512 | //need all params?? |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 513 | memcpy(&p_ctx->segment_params, ¶ms->segment, sizeof(params->segment)); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 514 | /*save current segment info*/ |
| 515 | memset(&p_ctx->segment_info, 0, sizeof(p_ctx->segment_info)); |
| 516 | p_ctx->segment_info.id = params->segment.segment_id; |
| 517 | 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] | 518 | } |
| 519 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 520 | p_ctx->segment_info.nb_pids = 0; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 521 | for (i = 0; i < params->segment.nb_pids; i++) { |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 522 | switch (params->segment.pid_action[i]) { |
| 523 | case DVR_RECORD_PID_CREATE: |
| 524 | DVR_DEBUG(1, "%s create pid:%d", __func__, params->segment.pids[i].pid); |
| 525 | ret = record_device_add_pid(p_ctx->dev_handle, params->segment.pids[i].pid); |
| 526 | p_ctx->segment_info.nb_pids++; |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 527 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 528 | break; |
| 529 | case DVR_RECORD_PID_KEEP: |
| 530 | DVR_DEBUG(1, "%s keep pid:%d", __func__, params->segment.pids[i].pid); |
| 531 | p_ctx->segment_info.nb_pids++; |
| 532 | break; |
| 533 | case DVR_RECORD_PID_CLOSE: |
| 534 | DVR_DEBUG(1, "%s close pid:%d", __func__, params->segment.pids[i].pid); |
| 535 | 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] | 536 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 537 | break; |
| 538 | default: |
| 539 | DVR_DEBUG(1, "%s wrong action pid:%d", __func__, params->segment.pids[i].pid); |
| 540 | return DVR_FAILURE; |
| 541 | } |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 542 | } |
| 543 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 544 | //ret = record_device_start(p_ctx->dev_handle); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 545 | //DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 546 | |
| 547 | pthread_create(&p_ctx->thread, NULL, record_thread, p_ctx); |
| 548 | p_ctx->state = DVR_RECORD_STATE_STARTED; |
| 549 | return DVR_SUCCESS; |
| 550 | } |
| 551 | |
| 552 | int dvr_record_stop_segment(DVR_RecordHandle_t handle, DVR_RecordSegmentInfo_t *p_info) |
| 553 | { |
| 554 | DVR_RecordContext_t *p_ctx; |
| 555 | int ret; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 556 | uint32_t i; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 557 | |
Pengfei Liu | 47ed6c9 | 2020-01-17 11:23:41 +0800 | [diff] [blame] | 558 | p_ctx = (DVR_RecordContext_t *)handle; |
| 559 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 560 | if (p_ctx == &record_ctx[i]) |
| 561 | break; |
| 562 | } |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 563 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 564 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 565 | DVR_DEBUG(1, "%s , current state:%d", __func__, p_ctx->state); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 566 | DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_STOPPED); |
| 567 | DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED); |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 568 | DVR_RETURN_IF_FALSE(p_info);/*should support NULL*/ |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 569 | |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 570 | p_ctx->state = DVR_RECORD_STATE_STOPPED; |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 571 | if (p_ctx->is_vod) { |
pengfei.liu | 8b56329 | 2020-02-26 15:49:02 +0800 | [diff] [blame] | 572 | 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] | 573 | p_ctx->segment_info.duration = 10*1000; //debug, should delete it |
| 574 | } else { |
| 575 | ret = record_device_stop(p_ctx->dev_handle); |
| 576 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
| 577 | //p_ctx->state = DVR_RECORD_STATE_STOPPED; |
| 578 | pthread_join(p_ctx->thread, NULL); |
| 579 | } |
| 580 | |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 581 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 582 | /*Update segment info*/ |
| 583 | memcpy(p_info, &p_ctx->segment_info, sizeof(p_ctx->segment_info)); |
| 584 | |
| 585 | ret = segment_store_info(p_ctx->segment_handle, p_info); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 586 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 587 | |
| 588 | DVR_DEBUG(1, "%s dump segment info, id:%lld, nb_pids:%d, duration:%ld ms, size:%zu, nb_packets:%d", |
| 589 | __func__, p_info->id, p_info->nb_pids, p_info->duration, p_info->size, p_info->nb_packets); |
| 590 | |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 591 | ret = segment_close(p_ctx->segment_handle); |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 592 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 593 | return DVR_SUCCESS; |
| 594 | } |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 595 | |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 596 | int dvr_record_resume_segment(DVR_RecordHandle_t handle, DVR_RecordStartParams_t *params, uint64_t *p_resume_size) |
| 597 | { |
| 598 | DVR_RecordContext_t *p_ctx; |
| 599 | uint32_t i; |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 600 | int ret; |
Pengfei Liu | b473423 | 2020-01-17 18:25:10 +0800 | [diff] [blame] | 601 | |
| 602 | p_ctx = (DVR_RecordContext_t *)handle; |
| 603 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 604 | if (p_ctx == &record_ctx[i]) |
| 605 | break; |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 606 | } |
Pengfei Liu | 3b1a820 | 2020-02-12 23:04:21 +0800 | [diff] [blame] | 607 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
| 608 | DVR_RETURN_IF_FALSE(params); |
| 609 | DVR_RETURN_IF_FALSE(p_resume_size); |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 610 | |
pengfei.liu | ab5a226 | 2020-02-14 17:33:40 +0800 | [diff] [blame] | 611 | DVR_DEBUG(1, "%s , current state:%d, resume size:%lld", __func__, p_ctx->state, *p_resume_size); |
| 612 | ret = dvr_record_start_segment(handle, params); |
| 613 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
| 614 | |
| 615 | p_ctx->segment_info.size = *p_resume_size; |
| 616 | |
| 617 | return DVR_SUCCESS; |
| 618 | } |
| 619 | |
| 620 | int dvr_record_get_status(DVR_RecordHandle_t handle, DVR_RecordStatus_t *p_status) |
| 621 | { |
| 622 | DVR_RecordContext_t *p_ctx; |
| 623 | int i; |
| 624 | |
| 625 | p_ctx = (DVR_RecordContext_t *)handle; |
| 626 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 627 | if (p_ctx == &record_ctx[i]) |
| 628 | break; |
| 629 | } |
| 630 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
| 631 | DVR_RETURN_IF_FALSE(p_status); |
| 632 | |
| 633 | //lock |
| 634 | p_status->state = p_ctx->state; |
| 635 | p_status->info.id = p_ctx->segment_info.id; |
| 636 | p_status->info.duration = p_ctx->segment_info.duration; |
| 637 | p_status->info.size = p_ctx->segment_info.size; |
| 638 | p_status->info.nb_packets = p_ctx->segment_info.size/188; |
| 639 | |
| 640 | return DVR_SUCCESS; |
| 641 | } |
| 642 | |
| 643 | int dvr_record_write(DVR_RecordHandle_t handle, void *buffer, uint32_t len) |
| 644 | { |
| 645 | DVR_RecordContext_t *p_ctx; |
| 646 | uint32_t i; |
| 647 | off_t pos = 0; |
| 648 | int ret; |
| 649 | int has_pcr; |
| 650 | |
| 651 | p_ctx = (DVR_RecordContext_t *)handle; |
| 652 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 653 | if (p_ctx == &record_ctx[i]) |
| 654 | break; |
| 655 | } |
| 656 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
| 657 | DVR_RETURN_IF_FALSE(buffer); |
| 658 | DVR_RETURN_IF_FALSE(len); |
| 659 | |
| 660 | pos = segment_tell_position(p_ctx->segment_handle); |
| 661 | has_pcr = record_do_pcr_index(p_ctx, buffer, len); |
| 662 | if (has_pcr == 0) { |
| 663 | /* Pull VOD record shoud use PCR time index */ |
| 664 | DVR_DEBUG(1, "%s has no pcr, can NOT do time index", __func__); |
| 665 | } |
| 666 | ret = segment_write(p_ctx->segment_handle, buffer, len); |
| 667 | p_ctx->segment_info.size += len; |
| 668 | p_ctx->segment_info.nb_packets = p_ctx->segment_info.size/188; |
| 669 | |
Pengfei Liu | c181a98 | 2020-01-07 19:27:13 +0800 | [diff] [blame] | 670 | return DVR_SUCCESS; |
| 671 | } |
pengfei.liu | 07ddc8a | 2020-03-24 23:36:53 +0800 | [diff] [blame] | 672 | |
pengfei.liu | 27cc4ec | 2020-04-03 16:28:16 +0800 | [diff] [blame] | 673 | 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] | 674 | { |
| 675 | DVR_RecordContext_t *p_ctx; |
| 676 | uint32_t i; |
| 677 | |
| 678 | p_ctx = (DVR_RecordContext_t *)handle; |
| 679 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 680 | if (p_ctx == &record_ctx[i]) |
| 681 | break; |
| 682 | } |
| 683 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
| 684 | DVR_RETURN_IF_FALSE(func); |
| 685 | |
| 686 | DVR_DEBUG(1, "%s , current state:%d", __func__, p_ctx->state); |
| 687 | DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_STARTED); |
| 688 | DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED); |
| 689 | |
| 690 | p_ctx->enc_func = func; |
| 691 | p_ctx->enc_userdata = userdata; |
| 692 | return DVR_SUCCESS; |
| 693 | } |
| 694 | |
| 695 | int dvr_record_set_secure_buffer(DVR_RecordHandle_t handle, uint8_t *p_secure_buf, uint32_t len) |
| 696 | { |
| 697 | DVR_RecordContext_t *p_ctx; |
| 698 | uint32_t i; |
| 699 | int ret; |
| 700 | |
| 701 | p_ctx = (DVR_RecordContext_t *)handle; |
| 702 | for (i = 0; i < MAX_DVR_RECORD_SESSION_COUNT; i++) { |
| 703 | if (p_ctx == &record_ctx[i]) |
| 704 | break; |
| 705 | } |
| 706 | DVR_RETURN_IF_FALSE(p_ctx == &record_ctx[i]); |
| 707 | DVR_RETURN_IF_FALSE(p_secure_buf); |
| 708 | DVR_RETURN_IF_FALSE(len); |
| 709 | |
| 710 | DVR_DEBUG(1, "%s , current state:%d", __func__, p_ctx->state); |
| 711 | DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_STARTED); |
| 712 | DVR_RETURN_IF_FALSE(p_ctx->state != DVR_RECORD_STATE_CLOSED); |
| 713 | |
| 714 | ret = record_device_set_secure_buffer(p_ctx->dev_handle, p_secure_buf, len); |
| 715 | DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS); |
| 716 | |
| 717 | p_ctx->is_secure_mode = 1; |
| 718 | return ret; |
| 719 | } |