调整数据库全部走LiveDatabaseServer

This commit is contained in:
zlzw 2024-11-29 10:42:47 +08:00
parent 8afb8f8f0b
commit 5ae1b474f9
12 changed files with 75 additions and 58 deletions

View File

@ -10,7 +10,7 @@
<body> <body>
<div id="header"></div> <div id="header"></div>
<div class="layui-row layui-col-space15"> <div class="layui-row">
<div id="follow"> <div id="follow">
</div> </div>
<div id="card"></div> <div id="card"></div>
@ -141,7 +141,7 @@
}) })
} }
}, function () { }, function () {
layer.msg('点击取消的回调');
}); });
} }
function confirmFollow(userId) { function confirmFollow(userId) {

View File

@ -42,9 +42,11 @@ public class LiveRoomConfig {
return Objects.hashCode(roomId); return Objects.hashCode(roomId);
} }
public static LiveRoomConfig buildConfig(String roomId){
@Deprecated
public static LiveRoomConfig buildConfigTmp(String roomId) {
BiliLiveConfigDatabase database = new BiliLiveConfigDatabase(); BiliLiveConfigDatabase database = new BiliLiveConfigDatabase();
LiveConfigDatabaseBean bean = database.getConfig(new String(roomId)); LiveConfigDatabaseBean bean = database.getConfig(roomId);
database.close(); database.close();
LiveRoomConfig config = new LiveRoomConfig(); LiveRoomConfig config = new LiveRoomConfig();
config.setLoginUid(bean.getRecordUid()); config.setLoginUid(bean.getRecordUid());

View File

@ -271,7 +271,7 @@ public class BiliLiveDatabase extends SQLiteManager {
} }
public static void main(String[] args) { public static void main(String[] args) {
BiliLiveDatabase biliLiveDatabase = new BiliLiveDatabase(LiveRoomConfig.buildConfig("33989")); BiliLiveDatabase biliLiveDatabase = new BiliLiveDatabase(LiveRoomConfig.buildConfigTmp("33989"));
// biliLiveDatabase.resetSQL(); // biliLiveDatabase.resetSQL();
biliLiveDatabase.resetData(); biliLiveDatabase.resetData();

View File

@ -5,6 +5,7 @@ import com.yutou.biliapi.bean.live.database.LiveConfigDatabaseBean;
import com.yutou.biliapi.databases.BiliLiveConfigDatabase; import com.yutou.biliapi.databases.BiliLiveConfigDatabase;
import com.yutou.bilibili.datas.ResultData; import com.yutou.bilibili.datas.ResultData;
import com.yutou.bilibili.datas.ReturnCode; import com.yutou.bilibili.datas.ReturnCode;
import com.yutou.bilibili.services.LiveDatabasesService;
import com.yutou.bilibili.services.LiveVideoDownloadService; import com.yutou.bilibili.services.LiveVideoDownloadService;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
@ -17,6 +18,8 @@ import java.util.List;
public class LiveVideoController { public class LiveVideoController {
@Resource @Resource
LiveVideoDownloadService videoService; LiveVideoDownloadService videoService;
@Resource
LiveDatabasesService databasesService;
@RequestMapping("/live/video/list") @RequestMapping("/live/video/list")
@ResponseBody @ResponseBody
@ -34,9 +37,7 @@ public class LiveVideoController {
@RequestMapping("/live/video/start") @RequestMapping("/live/video/start")
@ResponseBody @ResponseBody
public JSONObject startDownload(String roomId) { public JSONObject startDownload(String roomId) {
BiliLiveConfigDatabase liveConfigDatabase = new BiliLiveConfigDatabase(); List<LiveConfigDatabaseBean> list = databasesService.getConfigDatabase().getAllConfig();
List<LiveConfigDatabaseBean> list = liveConfigDatabase.getAllConfig();
liveConfigDatabase.close();
for (LiveConfigDatabaseBean bean : list) { for (LiveConfigDatabaseBean bean : list) {
if (bean.getRoomId().toString().equals(roomId)) { if (bean.getRoomId().toString().equals(roomId)) {
videoService.start(bean, true); videoService.start(bean, true);

View File

@ -1,9 +1,11 @@
package com.yutou.bilibili.Tools; package com.yutou.bilibili.Tools;
import com.yutou.bilibili.services.LiveDatabasesService;
import com.yutou.bilibili.services.LiveVideoDownloadService; import com.yutou.bilibili.services.LiveVideoDownloadService;
import com.yutou.bilibili.services.SystemService; import com.yutou.bilibili.services.SystemService;
import com.yutou.common.utils.Log; import com.yutou.common.utils.Log;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.jetbrains.annotations.NotNull;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent; import org.springframework.context.event.ContextClosedEvent;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -14,10 +16,14 @@ public class ApplicationClose implements ApplicationListener<ContextClosedEvent>
SystemService systemConfigService; SystemService systemConfigService;
@Resource @Resource
LiveVideoDownloadService videoService; LiveVideoDownloadService videoService;
@Resource
LiveDatabasesService databasesService;
@Override @Override
public void onApplicationEvent(ContextClosedEvent contextClosedEvent) { public void onApplicationEvent(@NotNull ContextClosedEvent contextClosedEvent) {
Log.i("服务结束"); Log.i("服务结束");
systemConfigService.stop(); systemConfigService.stop();
videoService.stopAll(); videoService.stopAll();
databasesService.closeAll();
} }
} }

View File

@ -22,7 +22,7 @@ import java.io.File;
public class LiveInfoNfoTools { public class LiveInfoNfoTools {
public static void saveLiveInfoNfo(LiveRoomInfo info, String path,String xmlName) { public static void saveLiveInfoNfo(LiveRoomInfo info, String path,String xmlName) {
try { try {
LiveRoomConfig config = LiveRoomConfig.buildConfig(info.getRoomId().toString()); LiveRoomConfig config = LiveRoomConfig.buildConfigTmp(info.getRoomId());
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = null; DocumentBuilder dBuilder = null;
dBuilder = dbFactory.newDocumentBuilder(); dBuilder = dbFactory.newDocumentBuilder();

View File

@ -17,7 +17,8 @@ import java.util.List;
@Service @Service
public class LiveConfigService { public class LiveConfigService {
BiliLiveConfigDatabase database = new BiliLiveConfigDatabase(); @Resource
LiveDatabasesService databasesService;
public LiveConfigDatabaseBean addConfig(String url, LiveConfigDatabaseBean bean) { public LiveConfigDatabaseBean addConfig(String url, LiveConfigDatabaseBean bean) {
if (!StringUtils.hasText(url)) { if (!StringUtils.hasText(url)) {
@ -35,7 +36,7 @@ public class LiveConfigService {
bean.setRoomId(body.getRoomId()); bean.setRoomId(body.getRoomId());
bean.setAnchorFace(infoBean.getInfo().getFace()); bean.setAnchorFace(infoBean.getInfo().getFace());
bean.setAnchorName(infoBean.getInfo().getUname()); bean.setAnchorName(infoBean.getInfo().getUname());
database.setConfig(bean); databasesService.getConfigDatabase().setConfig(bean);
return bean; return bean;
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
@ -43,38 +44,38 @@ public class LiveConfigService {
} }
public LiveConfigDatabaseBean updateConfig(String roomId, LiveConfigDatabaseBean bean) { public LiveConfigDatabaseBean updateConfig(String roomId, LiveConfigDatabaseBean bean) {
LiveConfigDatabaseBean config = database.getConfig(roomId); LiveConfigDatabaseBean config = databasesService.getConfigDatabase().getConfig(roomId);
if (config == null) { if (config == null) {
return null; return null;
} }
bean.setRoomId(roomId); bean.setRoomId(roomId);
bean.setSql_time(config.getSql_time()); bean.setSql_time(config.getSql_time());
database.setConfig(bean); databasesService.getConfigDatabase().setConfig(bean);
return bean; return bean;
} }
public boolean deleteConfig(String roomId) { public boolean deleteConfig(String roomId) {
LiveConfigDatabaseBean config = database.getConfig(roomId); LiveConfigDatabaseBean config = databasesService.getConfigDatabase().getConfig(roomId);
if (config == null) { if (config == null) {
return false; return false;
} }
return database.deleteConfig(roomId); return databasesService.getConfigDatabase().deleteConfig(roomId);
} }
public List<LiveConfigDatabaseBean> getAllConfig() { public List<LiveConfigDatabaseBean> getAllConfig() {
return database.getAllConfig(); return databasesService.getConfigDatabase().getAllConfig();
} }
public List<LiveConfigDatabaseBean> getConfigs(int page,int limit) { public List<LiveConfigDatabaseBean> getConfigs(int page,int limit) {
return database.getConfigs(page, limit); return databasesService.getConfigDatabase().getConfigs(page, limit);
} }
public LiveConfigDatabaseBean getConfig(String roomId) { public LiveConfigDatabaseBean getConfig(String roomId) {
return database.getConfig(roomId); return databasesService.getConfigDatabase().getConfig(roomId);
} }
public File getFace(String roomId) { public File getFace(String roomId) {
LiveConfigDatabaseBean config = database.getConfig(new String(roomId)); LiveConfigDatabaseBean config = databasesService.getConfigDatabase().getConfig(new String(roomId));
if (config == null) { if (config == null) {
return null; return null;
} }
@ -102,6 +103,6 @@ public class LiveConfigService {
} }
public int getConfigCount() { public int getConfigCount() {
return database.getConfigCount(); return databasesService.getConfigDatabase().getConfigCount();
} }
} }

View File

@ -30,11 +30,11 @@ public class LiveDanmuService {
WebSocketServer webSocketServer; WebSocketServer webSocketServer;
public void start(String roomId, boolean isUser) { public void start(String roomId, boolean isUser) {
webSocketServer.addRoom(LiveRoomConfig.buildConfig(roomId), isUser); webSocketServer.addRoom(liveDatabasesService.buildConfig(roomId), isUser);
} }
public void start(LiveConfigDatabaseBean roomId, boolean isUser) { public void start(LiveConfigDatabaseBean roomId, boolean isUser) {
webSocketServer.addRoom(LiveRoomConfig.buildConfig(roomId.getRoomId()), isUser); webSocketServer.addRoom(liveDatabasesService.buildConfig(roomId.getRoomId()), isUser);
} }
public boolean check(String roomId) { public boolean check(String roomId) {
@ -56,9 +56,7 @@ public class LiveDanmuService {
} }
public List<File> getDanmuFileList(String roomId) { public List<File> getDanmuFileList(String roomId) {
BiliLiveConfigDatabase configDatabase = new BiliLiveConfigDatabase(); LiveConfigDatabaseBean bean = liveDatabasesService.getConfigDatabase().getConfig(roomId);
LiveConfigDatabaseBean bean = configDatabase.getConfig(roomId);
configDatabase.close();
return Tools.scanFile(new File(bean.getRecordPath() + File.separator + bean.getAnchorName())); return Tools.scanFile(new File(bean.getRecordPath() + File.separator + bean.getAnchorName()));
} }
@ -150,7 +148,7 @@ public class LiveDanmuService {
} }
public static void main(String[] args) { public static void main(String[] args) {
BiliLiveDatabase database = new BiliLiveDatabase(LiveRoomConfig.buildConfig("17961")); BiliLiveDatabase database = new BiliLiveDatabase(LiveRoomConfig.buildConfigTmp("17961"));
for (LiveVideoDatabaseBean info : database.getLiveInfos()) { for (LiveVideoDatabaseBean info : database.getLiveInfos()) {
System.out.println(info); System.out.println(info);
} }

View File

@ -1,8 +1,12 @@
package com.yutou.bilibili.services; package com.yutou.bilibili.services;
import com.yutou.biliapi.bean.live.LiveRoomConfig; import com.yutou.biliapi.bean.live.LiveRoomConfig;
import com.yutou.biliapi.bean.live.database.LiveConfigDatabaseBean;
import com.yutou.biliapi.databases.BiliLiveConfigDatabase;
import com.yutou.biliapi.databases.BiliLiveDatabase; import com.yutou.biliapi.databases.BiliLiveDatabase;
import lombok.Getter;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -10,12 +14,33 @@ import java.util.Map;
@Service @Service
public class LiveDatabasesService { public class LiveDatabasesService {
private final Map<String, BiliLiveDatabase> liveDatabases=new HashMap<>(); private final Map<String, BiliLiveDatabase> liveDatabases=new HashMap<>();
@Getter
private final BiliLiveConfigDatabase configDatabase;
private LiveDatabasesService() {
configDatabase=new BiliLiveConfigDatabase();
}
public BiliLiveDatabase getLiveDatabase(String roomId) { public BiliLiveDatabase getLiveDatabase(String roomId) {
if(liveDatabases.containsKey(roomId)) { if(liveDatabases.containsKey(roomId)) {
return liveDatabases.get(roomId); return liveDatabases.get(roomId);
} }
BiliLiveDatabase liveDatabase = new BiliLiveDatabase(LiveRoomConfig.buildConfig(roomId)); BiliLiveDatabase liveDatabase = new BiliLiveDatabase(buildConfig(roomId));
liveDatabases.put(roomId, liveDatabase); liveDatabases.put(roomId, liveDatabase);
return liveDatabase; return liveDatabase;
} }
public void closeAll(){
liveDatabases.forEach((k,v)->v.close());
configDatabase.close();
}
public LiveRoomConfig buildConfig(String roomId){
LiveConfigDatabaseBean bean = configDatabase.getConfig(roomId);
LiveRoomConfig config = new LiveRoomConfig();
config.setLoginUid(bean.getRecordUid());
config.setRoomId(bean.getRoomId());
config.setAnchorName(bean.getAnchorName());
config.setLogin(StringUtils.hasText(bean.getRecordUid()));
config.setRootPath(bean.getRecordPath());
return config;
}
} }

View File

@ -4,31 +4,22 @@ import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject; import com.alibaba.fastjson2.JSONObject;
import com.yutou.biliapi.api.LiveApi; import com.yutou.biliapi.api.LiveApi;
import com.yutou.biliapi.bean.live.LiveAnchorInfo; import com.yutou.biliapi.bean.live.LiveAnchorInfo;
import com.yutou.biliapi.bean.live.LiveRoomConfig;
import com.yutou.biliapi.bean.live.LiveRoomInfo;
import com.yutou.biliapi.bean.live.LiveRoomPlayInfo;
import com.yutou.biliapi.bean.live.database.LiveConfigDatabaseBean; import com.yutou.biliapi.bean.live.database.LiveConfigDatabaseBean;
import com.yutou.biliapi.bean.live.database.LiveVideoDatabaseBean; import com.yutou.biliapi.bean.live.database.LiveVideoDatabaseBean;
import com.yutou.biliapi.databases.BiliLiveConfigDatabase;
import com.yutou.biliapi.databases.BiliLiveDatabase; import com.yutou.biliapi.databases.BiliLiveDatabase;
import com.yutou.biliapi.net.BiliLiveNetApiManager; import com.yutou.biliapi.net.BiliLiveNetApiManager;
import com.yutou.bilibili.Tools.DateFormatUtils; import com.yutou.bilibili.Tools.DateFormatUtils;
import com.yutou.bilibili.datas.web.LiveData; import com.yutou.bilibili.datas.web.LiveData;
import com.yutou.common.okhttp.BaseBean;
import com.yutou.common.okhttp.HttpLoggingInterceptor; import com.yutou.common.okhttp.HttpLoggingInterceptor;
import com.yutou.common.utils.FFmpegUtils;
import com.yutou.common.utils.Log;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.*; import java.util.*;
@Service @Service
public class LiveService { public class LiveService {
BiliLiveConfigDatabase liveConfigDatabase;
@Resource @Resource
LiveVideoDownloadService videoDownloadService; LiveVideoDownloadService videoDownloadService;
@Resource @Resource
@ -39,16 +30,15 @@ public class LiveService {
public LiveService() { public LiveService() {
liveConfigDatabase = new BiliLiveConfigDatabase();
api = BiliLiveNetApiManager.getInstance().getApi(null); api = BiliLiveNetApiManager.getInstance().getApi(null);
} }
public int getConfigCount() { public int getConfigCount() {
return liveConfigDatabase.getAllConfig().size(); return databasesService.getConfigDatabase().getAllConfig().size();
} }
public List<LiveData> getLiveList(int page, int limit) { public List<LiveData> getLiveList(int page, int limit) {
List<LiveConfigDatabaseBean> allConfig = liveConfigDatabase.getAllConfig(); List<LiveConfigDatabaseBean> allConfig = databasesService.getConfigDatabase().getAllConfig();
List<LiveData> liveDataList = new ArrayList<>(); List<LiveData> liveDataList = new ArrayList<>();
if (allConfig.isEmpty()) { if (allConfig.isEmpty()) {
return liveDataList; return liveDataList;

View File

@ -281,6 +281,7 @@ public class LiveVideoDownloadService {
// .withNotSymbolParam("-threads", "8")//看bili-go也没有加这个改成设置好了 // .withNotSymbolParam("-threads", "8")//看bili-go也没有加这个改成设置好了
// .withNotSymbolParam("-bufsize", "10M") // .withNotSymbolParam("-bufsize", "10M")
.withNotSymbolParam("-f", "segment") .withNotSymbolParam("-f", "segment")
.withNotSymbolParam("-rw_timeout","60000000")
.withNotSymbolParam("-segment_time", "60") .withNotSymbolParam("-segment_time", "60")
.withNotSymbolParam("-segment_format", "mpegts") .withNotSymbolParam("-segment_format", "mpegts")
.withNotSymbolParam("-map", "0") .withNotSymbolParam("-map", "0")
@ -360,13 +361,13 @@ public class LiveVideoDownloadService {
//录制弹幕 //录制弹幕
private void recordDanmu() { private void recordDanmu() {
if (bean.isSyncDanmuForLive() && !webSocketServer.checkRoom(LiveRoomConfig.buildConfig(bean.getRoomId()))) { if (bean.isSyncDanmuForLive() && !webSocketServer.checkRoom(liveDatabasesService.buildConfig(bean.getRoomId()))) {
webSocketServer.addRoom(LiveRoomConfig.buildConfig(bean.getRoomId()), true); webSocketServer.addRoom(liveDatabasesService.buildConfig(bean.getRoomId()), true);
} }
} }
private void stopRecordDanmu() { private void stopRecordDanmu() {
if (bean.isSyncDanmuForLive() && webSocketServer.checkRoom(LiveRoomConfig.buildConfig(bean.getRoomId()))) { if (bean.isSyncDanmuForLive() && webSocketServer.checkRoom(liveDatabasesService.buildConfig(bean.getRoomId()))) {
webSocketServer.stopRoom(bean.getRoomId(), false); webSocketServer.stopRoom(bean.getRoomId(), false);
} }
} }
@ -377,9 +378,7 @@ public class LiveVideoDownloadService {
} }
public VideoFilePath getVideoPath(String roomId) { public VideoFilePath getVideoPath(String roomId) {
BiliLiveConfigDatabase configDatabase = new BiliLiveConfigDatabase(); LiveConfigDatabaseBean bean = liveDatabasesService.getConfigDatabase().getConfig(roomId);
LiveConfigDatabaseBean bean = configDatabase.getConfig(roomId);
configDatabase.close();
return getVideoFilePath(bean); return getVideoFilePath(bean);
} }
@ -428,10 +427,8 @@ public class LiveVideoDownloadService {
public String getVideoPlay(String roomId, String videoId) { public String getVideoPlay(String roomId, String videoId) {
String ffmpegPath = ConfigTools.load(ConfigTools.CONFIG, "ffmpeg", String.class); String ffmpegPath = ConfigTools.load(ConfigTools.CONFIG, "ffmpeg", String.class);
BiliLiveConfigDatabase configDatabase = new BiliLiveConfigDatabase(); LiveConfigDatabaseBean config = liveDatabasesService.getConfigDatabase().getConfig(roomId);
LiveConfigDatabaseBean config = configDatabase.getConfig(roomId);
String recordPath = config.getRecordPath() + File.separator + config.getAnchorName(); String recordPath = config.getRecordPath() + File.separator + config.getAnchorName();
configDatabase.close();
LiveVideoDatabaseBean videoInfo = null; LiveVideoDatabaseBean videoInfo = null;
for (LiveVideoDatabaseBean info : liveDatabasesService.getLiveDatabase(roomId).getLiveInfos()) { for (LiveVideoDatabaseBean info : liveDatabasesService.getLiveDatabase(roomId).getLiveInfos()) {
if (videoId.trim().equals(String.valueOf(info.getSql_time().getTime()))) { if (videoId.trim().equals(String.valueOf(info.getSql_time().getTime()))) {

View File

@ -22,11 +22,12 @@ public class SystemService {
LiveVideoDownloadService videoService; LiveVideoDownloadService videoService;
@Resource @Resource
LiveDanmuService danmuService; LiveDanmuService danmuService;
@Resource
LiveDatabasesService databasesService;
SystemConfigDatabases databases = new SystemConfigDatabases(); SystemConfigDatabases databases = new SystemConfigDatabases();
private ScheduledExecutorService timer; private ScheduledExecutorService timer;
private ScheduledFuture<?> scheduled; private ScheduledFuture<?> scheduled;
BiliLiveConfigDatabase liveConfigDatabase = new BiliLiveConfigDatabase();
public long getLoopTimer() { public long getLoopTimer() {
SystemConfigDatabaseBean config = databases.getConfig(); SystemConfigDatabaseBean config = databases.getConfig();
@ -47,7 +48,7 @@ public class SystemService {
scheduled.cancel(true); scheduled.cancel(true);
} }
scheduled = timer.scheduleAtFixedRate(() -> { scheduled = timer.scheduleAtFixedRate(() -> {
List<LiveConfigDatabaseBean> list = liveConfigDatabase.getAllConfig(); List<LiveConfigDatabaseBean> list = databasesService.getConfigDatabase().getAllConfig();
Log.i("循环任务:" + list.size()); Log.i("循环任务:" + list.size());
if (DateFormatUtils.getInstance().checkTime(null, resetTimer)) { if (DateFormatUtils.getInstance().checkTime(null, resetTimer)) {
videoService.clearUserStopList(); videoService.clearUserStopList();
@ -64,10 +65,6 @@ public class SystemService {
} }
// 如果bean需要录制直播并且检查录制直播时间 // 如果bean需要录制直播并且检查录制直播时间
if (bean.isRecordLive() && bean.checkRecordLiveTime()) { if (bean.isRecordLive() && bean.checkRecordLiveTime()) {
// 如果需要同步直播弹幕则录制弹幕
if (bean.isSyncDanmuForLive()) {
recordDanmu(bean);
}
// 录制视频 // 录制视频
recordVideo(bean); recordVideo(bean);
} }