Skip to content

Commit

Permalink
fix live crash (#232)
Browse files Browse the repository at this point in the history
* Fix error handling in get_live_s function

* Remove unnecessary cout statement and replace with logger

* Fix error handling in getDanmuInfo and extract_messages functions.

* fix live crash

* use getResultAsync and update wait time
  • Loading branch information
lanytcc authored Nov 13, 2023
1 parent d2d4d03 commit a3ed021
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 130 deletions.
28 changes: 24 additions & 4 deletions wiliwili/include/api/live/danmaku_live.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,30 @@
#include <atomic>
#include <thread>
#include <mutex>
#include <functional>

#include <borealis.hpp>
#include <borealis/core/singleton.hpp>
#include "mongoose.h"
#include <nlohmann/json.hpp>
#include <vector>

using json = nlohmann::json;

class LiveDanmakuHostinfo {
public:
std::string host;
int ws_port;
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(LiveDanmakuHostinfo, host, ws_port);

class LiveDanmakuinfo {
public:
std::vector<LiveDanmakuHostinfo> host_list;
std::string token = "";
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(LiveDanmakuinfo, host_list, token);

typedef void (*on_message_func_t)(const std::string &);
class LiveDanmaku : public brls::Singleton<LiveDanmaku> {
public:
int room_id;
Expand All @@ -26,11 +44,11 @@ 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(on_message_func_t func);
on_message_func_t onMessage = nullptr;

void set_wait_time(size_t time);
size_t wait_time = 800;
size_t wait_time = 100;

LiveDanmaku();
~LiveDanmaku();
Expand All @@ -40,6 +58,8 @@ class LiveDanmaku : public brls::Singleton<LiveDanmaku> {
bool is_evOK();
std::atomic_bool ms_ev_ok{false};

LiveDanmakuinfo info;

std::thread mongoose_thread;
std::thread task_thread;
std::mutex mongoose_mutex;
Expand Down
35 changes: 15 additions & 20 deletions wiliwili/source/activity/live_player_activity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ static void process_danmaku(const std::vector<LiveDanmakuItem>& danmaku_list) {
// danmaku_t_free(dan);
}

static void onDanmakuReceived(std::string&& message) {
const std::string& msg = message;
static void onDanmakuReceived(const std::string& msg) {
std::vector<uint8_t> payload(msg.begin(), msg.end());
std::vector<std::string> messages = parse_packet(payload);

Expand All @@ -62,7 +61,7 @@ static void onDanmakuReceived(std::string&& message) {
}

static void showDialog(const std::string& msg, const std::string& pic,
bool forceQuit) {
bool forceQuit) {
brls::Dialog* dialog;
if (pic.empty()) {
dialog = new brls::Dialog(msg);
Expand Down Expand Up @@ -90,14 +89,10 @@ static void showDialog(const std::string& msg, const std::string& pic,
dialog->open();
}


LiveActivity::LiveActivity(const bilibili::LiveVideoResult& live)
: liveData(live) {
brls::Logger::debug("LiveActivity: create: {}", live.roomid);
this->setCommonData();
GA("open_live", {{"id", std::to_string(live.roomid)}})
GA("open_live", {{"live_id", std::to_string(live.roomid)}})
LiveDanmaku::instance().setonMessage(onDanmakuReceived);
}

LiveActivity::LiveActivity(int roomid, const std::string& name,
Expand All @@ -107,12 +102,13 @@ LiveActivity::LiveActivity(int roomid, const std::string& name,
this->liveData.title = name;
this->liveData.watched_show.text_large = views;
this->setCommonData();
GA("open_live", {{"id", std::to_string(roomid)}})
GA("open_live", {{"live_id", std::to_string(roomid)}})
LiveDanmaku::instance().setonMessage(onDanmakuReceived);
}

void LiveActivity::setCommonData() {
GA("open_live", {{"id", std::to_string(this->liveData.roomid)}})
GA("open_live", {{"live_id", std::to_string(this->liveData.roomid)}})

LiveDanmaku::instance().setonMessage(onDanmakuReceived);
LiveDanmaku::instance().connect(
liveData.roomid, std::stoll(ProgramConfig::instance().getUserID()));

Expand Down Expand Up @@ -261,10 +257,9 @@ void LiveActivity::onLiveData(const bilibili::LiveRoomPlayInfo& result) {
if (result.is_locked) {
brls::Logger::error("LiveActivity: live {} is locked", result.room_id);
this->video->showOSD(false);
showDialog(
fmt::format("这个房间已经被封禁(至 {})!(╯°口°)╯(┴—┴",
wiliwili::sec2FullDate(result.lock_till)),
"pictures/room-block.png", true);
showDialog(fmt::format("这个房间已经被封禁(至 {})!(╯°口°)╯(┴—┴",
wiliwili::sec2FullDate(result.lock_till)),
"pictures/room-block.png", true);
return;
}
// 0: 未开播 1: 直播中 2: 轮播中
Expand Down Expand Up @@ -306,17 +301,17 @@ void LiveActivity::onError(const std::string& error) {
}

void LiveActivity::onNeedPay(const std::string& msg, const std::string& link,
const std::string& startTime,
const std::string& endTime) {
const std::string& startTime,
const std::string& endTime) {
if (link.empty()) {
showDialog(msg, "", true);
return;
}

auto box = new brls::Box();
auto img = new QRImage();
auto label = new brls::Label();
auto header = new brls::Label();
auto box = new brls::Box();
auto img = new QRImage();
auto label = new brls::Label();
auto header = new brls::Label();
auto subtitle = new brls::Label();
header->setFontSize(24);
header->setMargins(10, 0, 20, 0);
Expand Down
Loading

0 comments on commit a3ed021

Please sign in to comment.