blob: bad292826b5d45cd66b6735dfdf963d7e7eb38c3 [file] [log] [blame]
Zhiqiang Han2f019af2023-08-31 11:12:02 +08001#include <stdio.h>
2#include <string.h>
3#include <stdlib.h>
4#include "dvr_types.h"
5#include "segment_dataout.h"
6
7
8/**\brief Segment context*/
9typedef struct {
10 uint64_t segment_id;
11
12 uint64_t cur_time;
13 uint64_t size_written;
14 uint64_t pkts_written;
15
16 Segment_DataoutCallback_t dataout_callback;
17} Segment_Context_t;
18
19int segment_dataout_open(Segment_OpenParams_t *params, Segment_Handle_t *p_handle)
20{
21 Segment_Context_t *p_ctx;
22
23 DVR_RETURN_IF_FALSE(params);
24 DVR_RETURN_IF_FALSE(p_handle);
25
26 p_ctx = (void*)malloc(sizeof(Segment_Context_t));
27 DVR_RETURN_IF_FALSE(p_ctx);
28
29 memset(p_ctx, 0, sizeof(Segment_Context_t));
30
31 p_ctx->segment_id = params->segment_id;
32
33 *p_handle = (Segment_Handle_t)p_ctx;
34 return DVR_SUCCESS;
35}
36
37int segment_dataout_close(Segment_Handle_t handle)
38{
39 Segment_Context_t *p_ctx = (void *)handle;
40
41 DVR_RETURN_IF_FALSE(p_ctx);
42
43 free(p_ctx);
44 return DVR_SUCCESS;
45}
46
47int segment_dataout_ioctl(Segment_Handle_t handle, int cmd, void *data, size_t size)
48{
49 Segment_Context_t *p_ctx = (void *)handle;
50
51 DVR_RETURN_IF_FALSE(p_ctx);
52
53 switch (cmd) {
54 case SEGMENT_DATAOUT_CMD_SET_CALLBACK:
55 DVR_RETURN_IF_FALSE(data != NULL);
56 p_ctx->dataout_callback = *(Segment_DataoutCallback_t *)data;
57 break;
58
59 default:
60 return DVR_FAILURE;
61 }
62 return DVR_SUCCESS;
63}
64
65ssize_t segment_dataout_write(Segment_Handle_t handle, void *buf, size_t count)
66{
67 ssize_t len = 0;
68 Segment_Context_t *p_ctx = (Segment_Context_t *)handle;
69
70 DVR_RETURN_IF_FALSE(p_ctx);
71 DVR_RETURN_IF_FALSE(buf);
72
73 if (p_ctx->dataout_callback.callback)
74 len = p_ctx->dataout_callback.callback(buf, count, p_ctx->dataout_callback.priv);
75
76 p_ctx->size_written += count;
77
78 return count;
79}
80
81loff_t segment_dataout_tell_total_time(Segment_Handle_t handle)
82{
83 uint64_t pts = ULLONG_MAX;
84 Segment_Context_t *p_ctx = (Segment_Context_t *)handle;
85
86 DVR_RETURN_IF_FALSE(p_ctx);
87
88 pts = p_ctx->cur_time;
89
90 return pts;
91}
92
93int segment_dataout_store_info(Segment_Handle_t handle, Segment_StoreInfo_t *p_info)
94{
95 Segment_Context_t *p_ctx = (Segment_Context_t *)handle;
96
97 DVR_RETURN_IF_FALSE(p_ctx);
98 DVR_RETURN_IF_FALSE(p_info);
99
100 p_ctx->segment_id = p_info->id;
101 p_ctx->cur_time = p_info->duration;
102 p_ctx->size_written = p_info->size;
103 p_ctx->pkts_written = p_info->nb_packets;
104
105 return DVR_SUCCESS;
106}
107
108int segment_dataout_store_allInfo(Segment_Handle_t handle, Segment_StoreInfo_t *p_info)
109{
110 return segment_dataout_store_info(handle, p_info);
111}
112
113
114int segment_dataout_update_pts_force(Segment_Handle_t handle, uint64_t pts, loff_t offset)
115{
116 Segment_Context_t *p_ctx = (Segment_Context_t *)handle;
117 DVR_RETURN_IF_FALSE(p_ctx);
118
119 if (offset == p_ctx->size_written)
120 p_ctx->cur_time = pts;
121 return DVR_SUCCESS;
122}
123
124int segment_dataout_update_pts(Segment_Handle_t handle, uint64_t pts, loff_t offset)
125{
126 return segment_dataout_update_pts_force(handle, pts, offset);
127}
128
129loff_t segment_dataout_tell_position(Segment_Handle_t handle)
130{
131 loff_t pos;
132 Segment_Context_t *p_ctx = (Segment_Context_t *)handle;
133
134 DVR_RETURN_IF_FALSE(p_ctx);
135
136 pos = p_ctx->size_written;
137
138 return pos;
139}