liusuyi
2026-06-02 f652169cc5d6501511eece5df57b7b396fd7a7ab
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package com.ard.zlm.controller;
 
import com.ard.common.core.domain.R;
import com.ard.common.core.web.controller.BaseController;
import com.ard.common.core.web.domain.AjaxResult;
import com.ard.common.core.web.page.TableDataInfo;
import com.ard.common.log.annotation.Log;
import com.ard.common.log.enums.BusinessType;
import com.ard.zlm.api.domain.StreamContent;
import com.ard.zlm.api.domain.StreamInfo;
import com.ard.zlm.api.domain.ZlmCloudRecord;
import com.ard.zlm.common.InviteErrorCode;
import com.ard.zlm.config.UserSetting;
import com.ard.zlm.domain.CloudRecordUrl;
import com.ard.zlm.service.ErrorCallback;
import com.ard.zlm.service.IZlmCloudRecordService;
import com.ard.zlm.utils.HttpUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.async.DeferredResult;
 
import java.io.FileInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
 
/**
 * 云端录像Controller
 *
 * @author fengcheng
 * @date 2026-04-10
 */
@Slf4j
@RestController
@RequestMapping("/cloudRecord")
public class ZlmCloudRecordController extends BaseController {
    @Autowired
    private IZlmCloudRecordService zlmCloudRecordService;
 
    @Autowired
    private UserSetting userSetting;
 
    /**
     * 查询云端录像列表(分页)
     */
    @GetMapping("/list")
    public TableDataInfo list(ZlmCloudRecord zlmCloudRecord) {
        startPage();
        List<ZlmCloudRecord> list = zlmCloudRecordService.selectZlmCloudRecordList(zlmCloudRecord);
        return getDataTable(list);
    }
 
    /**
     * 查询云端录像列表(不分页)
     */
    @GetMapping("/allList")
    public AjaxResult allList(ZlmCloudRecord zlmCloudRecord) {
        List<ZlmCloudRecord> list = zlmCloudRecordService.selectZlmCloudRecordList(zlmCloudRecord);
        return success(list);
    }
 
    /**
     * 获取云端录像详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(zlmCloudRecordService.selectZlmCloudRecordById(id));
    }
 
    /**
     * 删除云端录像
     */
    @Log(title = "云端录像", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        zlmCloudRecordService.deleteZlmCloudRecordByIds(ids);
        return success();
    }
 
    /**
     * 播放云端录像
     *
     * @param id 云端录像id
     * @return
     */
    @GetMapping("/loadRecord/{id}")
    public DeferredResult<R<StreamContent>> loadRecord(@PathVariable Long id, HttpServletRequest request) {
        DeferredResult<R<StreamContent>> result = new DeferredResult<>();
 
        result.onTimeout(() -> {
            log.info("[加载录像文件超时] id={}", id);
            R<StreamContent> wvpResult = R.fail();
            wvpResult.setMsg("加载录像文件超时");
            result.setResult(wvpResult);
        });
 
        ErrorCallback<StreamInfo> callback = (code, msg, streamInfo) -> {
 
            R<StreamContent> wvpResult = new R<>();
            if (code == InviteErrorCode.SUCCESS.getCode()) {
                wvpResult.setCode(200);
                wvpResult.setMsg("操作成功");
 
                if (streamInfo != null) {
                    if (userSetting.getUseSourceIpAsStreamIp()) {
                        streamInfo = streamInfo.clone();//深拷贝
                        String host;
                        try {
                            URL url = new URL(request.getRequestURL().toString());
                            host = url.getHost();
                        } catch (MalformedURLException e) {
                            host = request.getLocalAddr();
                        }
                        streamInfo.changeStreamIp(host);
                    }
                    if (!org.springframework.util.ObjectUtils.isEmpty(streamInfo.getMediaServer().getTranscodeSuffix()) && !"null".equalsIgnoreCase(streamInfo.getMediaServer().getTranscodeSuffix())) {
                        streamInfo.setStream(streamInfo.getStream() + "_" + streamInfo.getMediaServer().getTranscodeSuffix());
                    }
                    wvpResult.setData(new StreamContent(streamInfo));
                } else {
                    wvpResult.setCode(code);
                    wvpResult.setMsg(msg);
                }
            } else {
                wvpResult.setCode(code);
                wvpResult.setMsg(msg);
            }
            result.setResult(wvpResult);
        };
 
        zlmCloudRecordService.loadRecord(id, callback);
        return result;
    }
 
 
    /**
     * 关闭流文件形成播放地址
     *
     * @param id 云端录像id
     * @return
     */
    @GetMapping("/closeStreams/{id}")
    public AjaxResult closeStreams(@PathVariable Long id) {
        zlmCloudRecordService.closeStreams(id);
        return AjaxResult.success();
    }
 
    /**
     * 下载指定录像文件的压缩包
     *
     * @param response HttpServletResponse
     * @param ids      云端录像id数组
     */
    @GetMapping("/download/zip")
    public void downloadZipFileFromUrl(HttpServletResponse response, Long[] ids) {
        log.info("[下载指定录像文件的压缩包] 查询 ids->{}", Arrays.toString(ids));
        List<Long> arrayList = new ArrayList<>(List.of(ids));
        List<CloudRecordUrl> cloudRecordItemList = zlmCloudRecordService.getUrlListByIds(arrayList);
        if (ObjectUtils.isEmpty(cloudRecordItemList)) {
            log.warn("[下载指定录像文件的压缩包] 未找到录像文件,ids->{}", Arrays.toString(ids));
            return;
        }
 
        // 设置响应头
        response.setContentType("application/zip");
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Disposition", "attachment; filename=record_" + System.currentTimeMillis() + ".zip");
 
        try (ZipOutputStream zos = new ZipOutputStream(response.getOutputStream())) {
            for (CloudRecordUrl recordUrl : cloudRecordItemList) {
                try {
                    zos.putNextEntry(new ZipEntry(recordUrl.getFileName()));
                    boolean success = false;
                    
                    // 优先尝试从本地文件读取
                    if (recordUrl.getFilePath() != null && !recordUrl.getFilePath().isEmpty()) {
                        try (FileInputStream fis = new FileInputStream(recordUrl.getFilePath())) {
                            byte[] buf = new byte[8192]; // 8KB 缓冲区,提高性能
                            int len;
                            while ((len = fis.read(buf)) != -1) {
                                zos.write(buf, 0, len);
                            }
                            success = true;
                        } catch (IOException e) {
                            log.warn("[下载指定录像文件的压缩包] 本地文件读取失败,尝试网络下载: {}", recordUrl.getFilePath());
                        }
                    }
                    
                    // 如果本地读取失败,尝试网络下载
                    if (!success && recordUrl.getDownloadUrl() != null && !recordUrl.getDownloadUrl().isEmpty()) {
                        success = HttpUtils.downLoadFile(recordUrl.getDownloadUrl(), zos);
                    }
 
                    if (!success) {
                        log.warn("[下载指定录像文件的压缩包] 文件获取失败: {}", recordUrl.getFileName());
                    }
 
                    zos.closeEntry();
                } catch (Exception e) {
                    log.error("[下载指定录像文件的压缩包] 处理文件失败: {}, 错误: {}", recordUrl.getFileName(), e.getMessage());
                    // 继续处理下一个文件
                }
            }
        } catch (IOException e) {
            log.error("[下载指定录像文件的压缩包] 创建压缩包失败,查询 ids->{}", ids, e);
        }
    }
 
    /**
     * 设置录像播放速度
     *
     * @param mediaServerId 使用的节点Id
     * @param app           应用名
     * @param stream        流id
     * @param speed         播放速度
     * @param schema        播放协议
     */
    @GetMapping("/speed")
    public AjaxResult setRecordSpeed(
            @RequestParam(required = true) String mediaServerId,
            @RequestParam(required = true) String app,
            @RequestParam(required = true) String stream,
            @RequestParam(required = true) Integer speed,
            @RequestParam(required = false) String schema
    ) {
        if (schema == null) {
            schema = "ts";
        }
 
        zlmCloudRecordService.setRecordSpeed(mediaServerId, app, stream, speed, schema);
        return AjaxResult.success();
    }
 
    /**
     * 定位录像播放到制定位置
     *
     * @param mediaServerId 使用的节点Id
     * @param app           应用名
     * @param stream        流ID
     * @param seek          要定位的时间位置,从录像开始的时间算起
     * @param schema        播放协议
     */
    @GetMapping("/seek")
    public AjaxResult seekRecord(
            @RequestParam(required = true) String mediaServerId,
            @RequestParam(required = true) String app,
            @RequestParam(required = true) String stream,
            @RequestParam(required = true) Double stamp,
            @RequestParam(required = false) String schema
    ) {
        if (schema == null) {
            schema = "ts";
        }
        zlmCloudRecordService.seekRecord(mediaServerId, app, stream, stamp, schema);
        return AjaxResult.success();
    }
}