Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed the issue of too little or even no live danmaku #218

Merged
merged 4 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions wiliwili/include/api/live/danmaku_live.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,15 @@

#pragma once

#include <borealis.hpp>
#include <borealis/core/singleton.hpp>

#include <string>
#include <atomic>
#include <vector>
#include <thread>
#include <mutex>
#include <functional>
#include <utility>

#include "mongoose.h" // Include Mongoose header file
#include <borealis.hpp>
#include <borealis/core/singleton.hpp>
#include "mongoose.h"

class LiveDanmaku : public brls::Singleton<LiveDanmaku> {
public:
Expand All @@ -28,8 +25,8 @@ class LiveDanmaku : public brls::Singleton<LiveDanmaku> {
void send_heartbeat();
void send_text_message(const std::string &message);

void setonMessage(std::function<void(std::string&&)> func);
std::function<void(std::string&&)> onMessage;
void setonMessage(std::function<void(std::string &&)> func);
std::function<void(std::string &&)> onMessage;

void set_wait_time(int time);
int wait_time = 800;
Expand Down
14 changes: 14 additions & 0 deletions wiliwili/include/api/live/dl_emoticon.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
//Created by Maye174 on 2023/8/7.
//

#pragma once

#include <unordered_map>
#include <vector>
#include <string>
#include <cstdint>

using lmp = std::unordered_map<std::string, std::vector<uint8_t>>;

lmp dl_emoticon(int room_id);
94 changes: 46 additions & 48 deletions wiliwili/include/api/live/extract_messages.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,53 +10,52 @@
#include <cstdint>

typedef enum {
watched_change,//在线人数更新,xx人看过
online_cnt,//当前在线人数
online_v2,//待确定,似乎是当前在线高能用户
danmaku,//弹幕
super_chat,//sc,醒目留言
initeract_word,//普通用户的进场消息
entry_effect,//舰长进场消息
send_gift,//发送的礼物
combo_send,//连击礼物
like_info_update,//人气值(助力值)
like_info_click,//给主播点赞
}message_t; //还有上舰长,续费信息什么的,暂时不加


typedef struct{
watched_change, //在线人数更新,xx人看过
online_cnt, //当前在线人数
online_v2, //待确定,似乎是当前在线高能用户
danmaku, //弹幕
super_chat, //sc,醒目留言
initeract_word, //普通用户的进场消息
entry_effect, //舰长进场消息
send_gift, //发送的礼物
combo_send, //连击礼物
like_info_update, //人气值(助力值)
like_info_click, //给主播点赞
} message_t; //还有上舰长,续费信息什么的,暂时不加

typedef struct {
//b站会直接传3个值{"num":23592,"text_large":"2.3万人看过","text_small":"2.3万"}
//真的浪费
int num;
}watched_change_t;
} watched_change_t;

typedef struct{
typedef struct {
//代表在线人数
int count;
}online_cnt_t;
} online_cnt_t;

typedef struct{
typedef struct {
//头像
void *face_photo[10];
void* face_photo[10];
//名字
char * user_name[10];
char* user_name[10];
//暂时先写这俩
}online_v2_t;
} online_v2_t;

//弹幕类型,内容太多,暂时写这么多
//
//很多重复内容,感觉不是同一批人写的,或者可能b站想换协议,
typedef struct{
typedef struct {
//用户名字
char * user_name;
char* user_name;
//用户名字颜色,一般为舰长以上有,即vip等级1以上
char * user_name_color;
char* user_name_color;
//弹幕内容
char * dan;
char* dan;
//粉丝牌子名字
char * fan_medal_name;
char* fan_medal_name;
//粉丝牌子对应主播名字
char * fan_medal_liveuser_name;
char* fan_medal_liveuser_name;
//用户uid
int user_uid;
//弹幕颜色
Expand Down Expand Up @@ -92,20 +91,19 @@ typedef struct{
uint8_t is_guard;
//荣耀值,就是用户名和牌子前面那个带蓝色框子的数字 暂时不用
uint8_t glory_v;
}danmaku_t; //Maye174: 为了对齐内存,乱序排
} danmaku_t; //Maye174: 为了对齐内存,乱序排

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

typedef struct{
typedef struct {
//todo
}super_chat_t;

} super_chat_t;

//这里b站的接口命名更混乱,感觉b站后面会逐步换掉
typedef struct{
typedef struct {
//用户名字
char * user_name;
char* user_name;
//牌子字体颜色 目前应该都是白色
int fan_medal_font_color;
//牌子边框颜色
Expand All @@ -118,30 +116,30 @@ typedef struct{
int fan_medal_uid;
//牌子等级
uint8_t fan_medal_level;
}initeract_word_t;
} initeract_word_t;

initeract_word_t *initeract_word_t_init();
initeract_word_t* initeract_word_t_init();

typedef struct{
typedef struct {
//todo
}send_gift_t;
} send_gift_t;

typedef struct{
typedef struct {
//todo
}combo_send_t;
} combo_send_t;

typedef struct{
typedef struct {
//人气值,但是这个值叫click_count,我有点搞不清
int count;
}like_info_update_t;
} like_info_update_t;

typedef struct{
typedef struct {
//todo
}like_info_click_t;
} like_info_click_t;

typedef struct{
typedef struct {
message_t type;
void *ptr;
}live_t;
void* ptr;
} live_t;

std::vector<live_t> extract_messages(const std::vector<std::string>& messages);
4 changes: 2 additions & 2 deletions wiliwili/include/utils/config_helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@ namespace fs = std::experimental::filesystem;
#endif

#ifdef PS4
const std::string primaryDNSStr = "223.5.5.5";
const std::string primaryDNSStr = "223.5.5.5";
const std::string secondaryDNSStr = "1.1.1.1";
#endif


typedef std::map<std::string, std::string> Cookie;
constexpr uint32_t MINIMUM_WINDOW_WIDTH = 640;
constexpr uint32_t MINIMUM_WINDOW_HEIGHT = 360;
Expand Down Expand Up @@ -157,6 +156,7 @@ class ProgramConfig : public brls::Singleton<ProgramConfig> {
std::string getRefreshToken() const;
std::string getCSRF();
std::string getUserID();
std::string getBuvid3();
bool hasLoginInfo();

// Google Analytics ID
Expand Down
9 changes: 7 additions & 2 deletions wiliwili/include/view/danmaku_core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#pragma once

#include "api/live/extract_messages.hpp"

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

std::string msg; // 弹幕内容
float time; // 弹幕出现的时间
Expand All @@ -30,7 +32,8 @@ class DanmakuItem {
int64_t startTime = 0;
NVGcolor color = nvgRGBA(255, 255, 255, 160);
NVGcolor borderColor = nvgRGBA(0, 0, 0, 160);
int level; // 弹幕等级 1-10
int level; // 弹幕等级 1-10 直播弹幕等级0-60
int is_live; // 是否为直播弹幕
// 暂时用不到的信息,先不使用
// int pubDate; // 弹幕发送时间
// int pool; // 弹幕池类型
Expand Down Expand Up @@ -100,6 +103,8 @@ 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
32 changes: 15 additions & 17 deletions wiliwili/source/activity/live_player_activity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,21 @@
#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;

#define tostr(x) std::to_string(x)
static void process_danmaku(danmaku_t *dan){
char comma = ',';
std::string tem = "0,0,0,0,";
//做其他处理
static void process_danmaku(const float time, const danmaku_t* dan) {
//TODO:做其他处理
//...

//弹幕加载到视频中去
double time = MPVCore::instance().getPlaybackTime() + 0.3;
std::string combined_attr = tostr(time) + comma + tostr(dan->dan_type) + comma
+ tostr(dan->dan_size) + comma + tostr(dan->dan_color) + comma
+ tem + tostr(dan->user_level);
DanmakuCore::instance().addSingleDanmaku(DanmakuItem(dan->dan, combined_attr));
float _time = MPVCore::instance().getPlaybackTime() + time;
DanmakuCore::instance().addSingleDanmaku(DanmakuItem(_time, dan));

danmaku_t_free(dan);
}
Expand All @@ -38,17 +33,19 @@ static void onDanmakuReceived(std::string&& message) {
std::vector<uint8_t> payload(msg.begin(), msg.end());
std::vector<std::string> messages = parse_packet(payload);

if(messages.size() == 0){
if (messages.size() == 0) {
return;
}

for(auto &&live_msg : extract_messages(messages)){
if(live_msg.type == danmaku){
if(!live_msg.ptr) continue;
process_danmaku((danmaku_t *)live_msg.ptr);
float time = 0.1f;
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;
} else if (live_msg.type == watched_change) {
//todo
//TODO: 更新在线人数
free(live_msg.ptr);
}
}
Expand Down Expand Up @@ -77,7 +74,8 @@ LiveActivity::LiveActivity(int roomid, const std::string& name,

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

// 清空字幕
SubtitleCore::instance().reset();
Expand Down
Loading