1
+ //
2
+ // Created by fang on 2023/11/14.
3
+ //
4
+
5
+ #include < borealis/core/thread.hpp>
6
+ #include < borealis/core/logger.hpp>
7
+
8
+ #include " presenter/video_detail.hpp"
9
+ #include " presenter/live_data.hpp"
10
+ #include " utils/config_helper.hpp"
11
+ #include " bilibili.h"
12
+
13
+ void LiveDataRequest::requestData (int roomid) {
14
+ ASYNC_RETAIN
15
+ BILI::get_live_room_play_info (
16
+ roomid, defaultQuality,
17
+ [ASYNC_TOKEN](const auto & result) {
18
+ liveRoomPlayInfo = result;
19
+ qualityDescriptionMap.clear ();
20
+ for (auto & i :
21
+ liveRoomPlayInfo.playurl_info .playurl .g_qn_desc ) {
22
+ qualityDescriptionMap[i.qn ] = i.desc ;
23
+ }
24
+
25
+ // 选择第一个 protocol 的 第一个 format 的第一个 codec 作为播放源
26
+ // protocol: http_stream / http_hls
27
+ // format: flv / ts / fmp4
28
+ // codec: avc / hevc / av1
29
+ bilibili::LiveStream stream;
30
+ for (auto & i : liveRoomPlayInfo.playurl_info .playurl .stream ) {
31
+ stream = i;
32
+ break ;
33
+ }
34
+ bilibili::LiveStreamFormat format;
35
+ for (auto & i : stream.format ) {
36
+ format = i;
37
+ break ;
38
+ }
39
+ for (auto & i : format.codec ) {
40
+ liveUrl = i;
41
+ break ;
42
+ }
43
+ brls::sync ([ASYNC_TOKEN]() {
44
+ ASYNC_RELEASE
45
+ onLiveData (liveRoomPlayInfo);
46
+ });
47
+ },
48
+ [ASYNC_TOKEN](BILI_ERR) {
49
+ brls::sync ([ASYNC_TOKEN, error]() {
50
+ ASYNC_RELEASE
51
+ this ->onError (error);
52
+ });
53
+ });
54
+
55
+ reportHistory (roomid);
56
+ }
57
+
58
+ void LiveDataRequest::reportHistory (int roomid) {
59
+ // 复用视频播放页面的标记
60
+ if (!VideoDetail::REPORT_HISTORY) return ;
61
+
62
+ BILI::report_live_history (
63
+ roomid, ProgramConfig::instance ().getCSRF (),
64
+ [roomid]() {
65
+ brls::Logger::debug (" report live history {}" , roomid);
66
+ },
67
+ [](BILI_ERR) {
68
+ brls::Logger::error (" report live history: {}" , error);
69
+ });
70
+ }
71
+
72
+ void LiveDataRequest::requestPayLiveInfo (int roomid) {
73
+ ASYNC_RETAIN
74
+ BILI::get_live_pay_info (
75
+ roomid,
76
+ [ASYNC_TOKEN, roomid](const auto & payInfo) {
77
+ brls::Logger::debug (" get live pay info {}" , payInfo.permission );
78
+ if (payInfo.permission != 0 ) return ;
79
+ BILI::get_live_pay_link (
80
+ roomid,
81
+ [ASYNC_TOKEN, payInfo](const auto & payLink) {
82
+ brls::Logger::debug (" get live pay link {}" ,
83
+ payLink.goods_link );
84
+ brls::sync ([ASYNC_TOKEN, payInfo, payLink]() {
85
+ ASYNC_RELEASE
86
+ this ->onNeedPay (payInfo.message , payLink.goods_link ,
87
+ payLink.start_time ,
88
+ payLink.end_time );
89
+ });
90
+ },
91
+ [ASYNC_TOKEN, payInfo](BILI_ERR) {
92
+ brls::Logger::error (" get live pay link:" , error);
93
+ brls::sync ([ASYNC_TOKEN, payInfo]() {
94
+ ASYNC_RELEASE
95
+ this ->onNeedPay (payInfo.message , " " , " " , " " );
96
+ });
97
+ });
98
+ },
99
+ [ASYNC_TOKEN](BILI_ERR) {
100
+ ASYNC_RELEASE
101
+ brls::Logger::error (" get live pay info: {}" , error);
102
+ });
103
+ }
104
+
105
+ void LiveDataRequest::requestLiveDanmakuToken (int roomid) {
106
+ ASYNC_RETAIN
107
+ BILI::get_live_danmaku_info (
108
+ roomid,
109
+ [ASYNC_TOKEN, roomid](const auto & info) {
110
+ brls::sync ([ASYNC_TOKEN, roomid, info]() {
111
+ ASYNC_RELEASE
112
+ this ->onDanmakuInfo (roomid, info);
113
+ });
114
+ },
115
+ [ASYNC_TOKEN](BILI_ERR) {
116
+ ASYNC_RELEASE
117
+ brls::Logger::error (" get live danmaku info: {}" , error);
118
+ });
119
+ }
120
+
121
+ std::string LiveDataRequest::getQualityDescription (int qn) {
122
+ if (qualityDescriptionMap.count (qn) == 0 )
123
+ return " Unknown Quality " + std::to_string (qn);
124
+ return qualityDescriptionMap[qn];
125
+ }
0 commit comments