Skip to content

Commit

Permalink
fix #219 (#222)
Browse files Browse the repository at this point in the history
* Improve the process of handling live danmuku

* fix live danmaku

* Add buvid into cookie

* fix #219

* Reduce nanovg state switching

* Optimize method of waiting for danmaku queue

* fix gradient and danmaku sever
  • Loading branch information
maye76 authored Nov 4, 2023
1 parent 69e2a31 commit cb83585
Show file tree
Hide file tree
Showing 11 changed files with 400 additions and 117 deletions.
1 change: 1 addition & 0 deletions wiliwili/include/api/live/extract_messages.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ typedef struct {
} danmaku_t; //Maye174: 为了对齐内存,乱序排

danmaku_t* danmaku_t_init();
danmaku_t* danmaku_t_copy(const danmaku_t* p);
void danmaku_t_free(const danmaku_t* p);

typedef struct {
Expand Down
10 changes: 1 addition & 9 deletions wiliwili/include/view/danmaku_core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

#pragma once

#include "api/live/extract_messages.hpp"

#include <mutex>
#include <borealis.hpp>
#include <borealis/core/singleton.hpp>
Expand All @@ -14,7 +12,6 @@
class DanmakuItem {
public:
DanmakuItem(std::string content, const char *attributes);
DanmakuItem(const float _time, const danmaku_t *dan);

std::string msg; // 弹幕内容
float time; // 弹幕出现的时间
Expand All @@ -32,8 +29,7 @@ class DanmakuItem {
int64_t startTime = 0;
NVGcolor color = nvgRGBA(255, 255, 255, 160);
NVGcolor borderColor = nvgRGBA(0, 0, 0, 160);
int level; // 弹幕等级 1-10 直播弹幕等级0-60
int is_live; // 是否为直播弹幕
int level; // 弹幕等级 1-10
// 暂时用不到的信息,先不使用
// int pubDate; // 弹幕发送时间
// int pool; // 弹幕池类型
Expand All @@ -44,7 +40,6 @@ class DanmakuItem {
return this->time < item.time;
}
};

class DanmakuCore : public brls::Singleton<DanmakuCore> {
public:
/**
Expand Down Expand Up @@ -93,7 +88,6 @@ class DanmakuCore : public brls::Singleton<DanmakuCore> {
* @param item 单条弹幕
*/
void addSingleDanmaku(const DanmakuItem &item);
void addSingleDanmaku(DanmakuItem &&item);

/**
* 获取弹幕数据
Expand All @@ -103,8 +97,6 @@ class DanmakuCore : public brls::Singleton<DanmakuCore> {

/// range: [1 - 10], 1: show all danmaku, 10: the most strong filter
static inline int DANMAKU_FILTER_LEVEL = 1;
//0-60
static inline int DANMAKU_FILTER_LEVEL_LIVE = 0;

static inline bool DANMAKU_FILTER_SHOW_TOP = true;
static inline bool DANMAKU_FILTER_SHOW_BOTTOM = true;
Expand Down
56 changes: 56 additions & 0 deletions wiliwili/include/view/live_core.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//
// Created by maye174 on 2023/11/13.
//

#pragma once

#include "api/live/extract_messages.hpp"

#include <queue>

#include "nanovg.h"
#include <borealis.hpp>
#include <borealis/core/singleton.hpp>

class LiveDanmakuItem {
public:
LiveDanmakuItem(danmaku_t *danmaku);
LiveDanmakuItem(const LiveDanmakuItem &item);
LiveDanmakuItem(LiveDanmakuItem &&item);
~LiveDanmakuItem() {
if (!danmaku) return;
danmaku_t_free(danmaku);
free(danmaku);
}
danmaku_t *danmaku;
float time;
float length = 0;
float speed = 0;
int line = 0;
};

class LiveDanmakuCore : public brls::Singleton<LiveDanmakuCore> {
public:
//0-60
static inline int DANMAKU_FILTER_LEVEL_LIVE = 0;

std::vector<std::pair<float, float>> scroll_lines;
std::vector<int> center_lines;

float line_height;

int danmaku_font = brls::Application::getDefaultFont();

std::deque<LiveDanmakuItem> next;
std::mutex next_mutex;

std::map<int, std::deque<LiveDanmakuItem>> now;

void reset();
void add(const std::vector<LiveDanmakuItem> &dan_l);
void draw(NVGcontext *vg, float x, float y, float width, float height,
float alpha);

bool init_danmaku(NVGcontext *vg, LiveDanmakuItem &i, float width,
int LINES, float SECOND, float time);
};
4 changes: 4 additions & 0 deletions wiliwili/include/view/video_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ class VideoView : public brls::Box {

void hideBottomLineSetting();

void hideVideoProgressSlider();

/// 隐藏左下角的播放时间
void hideStatusLabel();

Expand Down Expand Up @@ -190,6 +192,8 @@ class VideoView : public brls::Box {
// Bottom progress bar
inline static bool BOTTOM_BAR = true;

inline static bool IN_LIVE = false;

private:
bool allowFullscreen = true;
bool registerMPVEvent = false;
Expand Down
30 changes: 20 additions & 10 deletions wiliwili/source/activity/live_player_activity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,33 @@
//

#include "activity/live_player_activity.hpp"
#include <vector>

#include "view/video_view.hpp"
#include "view/mpv_core.hpp"
#include "view/danmaku_core.hpp"
#include "view/live_core.hpp"
#include "view/subtitle_core.hpp"
#include "view/grid_dropdown.hpp"

#include "utils/shader_helper.hpp"
#include "utils/config_helper.hpp"

#include "live/danmaku_live.hpp"
#include "live/extract_messages.hpp"
#include "live/ws_utils.hpp"

#include "bilibili.h"

using namespace brls::literals;

static void process_danmaku(const float time, const danmaku_t* dan) {
static void process_danmaku(const std::vector<LiveDanmakuItem>& danmaku_list) {
//TODO:做其他处理
//...

//弹幕加载到视频中去
float _time = MPVCore::instance().getPlaybackTime() + time;
DanmakuCore::instance().addSingleDanmaku(DanmakuItem(_time, dan));
LiveDanmakuCore::instance().add(danmaku_list);

danmaku_t_free(dan);
// danmaku_t_free(dan);
}

static void onDanmakuReceived(std::string&& message) {
Expand All @@ -37,18 +41,20 @@ static void onDanmakuReceived(std::string&& message) {
return;
}

float time = 0.1f;
std::vector<LiveDanmakuItem> danmaku_list;

for (const auto& live_msg : extract_messages(messages)) {
if (live_msg.type == danmaku) {
if (!live_msg.ptr) continue;
process_danmaku(time, (danmaku_t*)live_msg.ptr);
free(live_msg.ptr);
time += 0.2f;
danmaku_list.emplace_back(
LiveDanmakuItem((danmaku_t*)live_msg.ptr));
// free(live_msg.ptr);
} else if (live_msg.type == watched_change) {
//TODO: 更新在线人数
free(live_msg.ptr);
}
}
process_danmaku(danmaku_list);
}

LiveActivity::LiveActivity(const bilibili::LiveVideoResult& live)
Expand All @@ -58,6 +64,7 @@ LiveActivity::LiveActivity(const bilibili::LiveVideoResult& live)
this->setCommonData();
GA("open_live", {{"id", std::to_string(live.roomid)}})
LiveDanmaku::instance().setonMessage(onDanmakuReceived);
VideoView::IN_LIVE = true;
}

LiveActivity::LiveActivity(int roomid, const std::string& name,
Expand All @@ -70,10 +77,10 @@ LiveActivity::LiveActivity(int roomid, const std::string& name,
this->setCommonData();
GA("open_live", {{"id", std::to_string(roomid)}})
LiveDanmaku::instance().setonMessage(onDanmakuReceived);
VideoView::IN_LIVE = true;
}

void LiveActivity::setCommonData() {
DanmakuCore::instance().reset();
LiveDanmaku::instance().connect(
liveData.roomid, std::stoi(ProgramConfig::instance().getUserID()));

Expand Down Expand Up @@ -122,6 +129,7 @@ void LiveActivity::onContentAvailable() {
return true;
});

this->video->hideVideoProgressSlider();
this->video->hideDLNAButton();
this->video->hideSubtitleSetting();
this->video->hideVideoRelatedSetting();
Expand Down Expand Up @@ -187,4 +195,6 @@ LiveActivity::~LiveActivity() {
// 取消监控mpv
MPV_CE->unsubscribe(eventSubscribeID);
MPVCore::instance().command_async("set", "loop-playlist", "1");
LiveDanmakuCore::instance().reset();
VideoView::IN_LIVE = false;
}
14 changes: 7 additions & 7 deletions wiliwili/source/api/danmaku_live.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ static void get_live_s(int room_id) {
std::cout << "getDanmuInfo json parse error" << std::endl;
}
if (_json["code"].get<int>() == 0) {
url = "ws://" +
_json["data"]["host_list"][0]["host"]
.get_ref<const std::string &>() +
":" +
std::to_string(
_json["data"]["host_list"][0]["ws_port"].get<int>()) +
"/sub";
// url = "ws://" +
// _json["data"]["host_list"][0]["host"]
// .get_ref<const std::string &>() +
// ":" +
// std::to_string(
// _json["data"]["host_list"][0]["ws_port"].get<int>()) +
// "/sub";
key = _json["data"]["token"].get_ref<const std::string &>();
}
}
Expand Down
32 changes: 30 additions & 2 deletions wiliwili/source/api/util/extract_messages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
#include "live/extract_messages.hpp"

#include <nlohmann/json.hpp>
#include <stdlib.h>
#include <cstdlib>
#include <cstring>

danmaku_t *danmaku_t_init() {
danmaku_t *ret = (danmaku_t *)malloc(sizeof(danmaku_t));
Expand Down Expand Up @@ -31,6 +32,27 @@ danmaku_t *danmaku_t_init() {
return ret;
}

static char *strdup_s(const char *s) {
if (!s) return nullptr;
char *ret = (char *)malloc(strlen(s) + 1);
if (!ret) return nullptr;
strcpy(ret, s);
return ret;
}

danmaku_t *danmaku_t_copy(const danmaku_t *p) {
if (!p) return nullptr;
danmaku_t *ret = (danmaku_t *)malloc(sizeof(danmaku_t));
if (!ret) return nullptr;
memcpy(ret, p, sizeof(danmaku_t));
ret->user_name = strdup_s(p->user_name);
ret->user_name_color = strdup_s(p->user_name_color);
ret->dan = strdup_s(p->dan);
ret->fan_medal_name = strdup_s(p->fan_medal_name);
ret->fan_medal_liveuser_name = strdup_s(p->fan_medal_liveuser_name);
return ret;
}

void danmaku_t_free(const danmaku_t *p) {
free(p->user_name);
free(p->user_name_color);
Expand All @@ -50,7 +72,13 @@ std::vector<live_t> extract_messages(const std::vector<std::string> &messages) {
live_messages.reserve(messages.size() / 5);

for (auto &message : messages) {
nlohmann::json json_message = nlohmann::json::parse(message);
nlohmann::json json_message;

try {
json_message = nlohmann::json::parse(message);
} catch (nlohmann::json::parse_error &e) {
continue;
}

auto it = json_message.find("cmd");

Expand Down
Loading

0 comments on commit cb83585

Please sign in to comment.