Skip to content

Commit f2ec5a3

Browse files
committed
update demo
1 parent fcd6009 commit f2ec5a3

File tree

5 files changed

+189
-25
lines changed

5 files changed

+189
-25
lines changed

example/common/cpp/libKmpWebrtc.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,7 @@ void AddRemoteTrackRenderer(void* pc_client, void* renderer) {
162162
void LogInfo(const char* log) {
163163
KFunc(utils.logInfo)(log);
164164
}
165+
166+
const char* PreferSdp(const char* sdp, int codec) {
167+
return KFunc(utils.preferCodec)(sdp, codec);
168+
}

example/common/cpp/libKmpWebrtc.h

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,29 @@
1616
extern "C" {
1717
#endif
1818

19-
enum KmpWebRTCCaptureImpl {
20-
kKmpWebRTCCaptureSystemCamera = 1,
21-
kKmpWebRTCCaptureScreen = 2,
22-
kKmpWebRTCCaptureFile = 3,
23-
kKmpWebRTCCaptureApp = 4,
24-
};
25-
2619
enum KmpWebRTCDir {
2720
kKmpWebRTCDirSendRecv = 0,
2821
kKmpWebRTCDirSendOnly = 1,
2922
kKmpWebRTCDirRecvOnly = 2,
3023
kKmpWebRTCDirInactive = 3,
3124
};
3225

26+
enum KmpWebRTCVideoCodec {
27+
kKmpWebRTCVideoCodecVP8 = 1,
28+
kKmpWebRTCVideoCodecVP9 = 2,
29+
kKmpWebRTCVideoCodecH264Baseline = 3,
30+
kKmpWebRTCVideoCodecH264HighProfile = 4,
31+
kKmpWebRTCVideoCodecH265 = 5,
32+
kKmpWebRTCVideoCodecAV1 = 6,
33+
};
34+
35+
enum KmpWebRTCCaptureImpl {
36+
kKmpWebRTCCaptureSystemCamera = 1,
37+
kKmpWebRTCCaptureScreen = 2,
38+
kKmpWebRTCCaptureFile = 3,
39+
kKmpWebRTCCaptureApp = 4,
40+
};
41+
3342
enum KmpWebRTCSdpType {
3443
kKmpWebRTCSdpOffer = 1,
3544
kKmpWebRTCSdpPrAnswer = 2,
@@ -94,6 +103,8 @@ KMP_WEBRTC_API void AddRemoteTrackRenderer(void* pc_client, void* renderer);
94103

95104
KMP_WEBRTC_API void LogInfo(const char* log);
96105

106+
KMP_WEBRTC_API const char* PreferSdp(const char* sdp, int codec);
107+
97108
#if __cplusplus
98109
}
99110
#endif

example/linuxApp/CMakeLists.txt

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,37 @@ project(linuxApp)
44
set(CMAKE_CXX_STANDARD 11)
55
set(CMAKE_CXX_STANDARD_REQUIRED True)
66

7-
find_package(Qt5 REQUIRED COMPONENTS Widgets)
7+
#find_package(Qt5 REQUIRED COMPONENTS Widgets)
8+
9+
#include_directories(${Qt5Widgets_INCLUDE_DIRS})
10+
11+
#set(CMAKE_AUTOMOC ON)
12+
#set(CMAKE_AUTORCC ON)
13+
#set(CMAKE_AUTOUIC ON)
814

9-
include_directories(${Qt5Widgets_INCLUDE_DIRS})
1015
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../libs/linux/x64)
1116
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../libs/windows_linux/include)
1217
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../common/cpp)
1318

14-
set(CMAKE_AUTOMOC ON)
15-
set(CMAKE_AUTORCC ON)
16-
set(CMAKE_AUTOUIC ON)
17-
1819
add_definitions(-DWEBRTC_LINUX=1)
1920

2021
file(GLOB deps
2122
${CMAKE_CURRENT_SOURCE_DIR}/../../libs/linux/x64/libkmp_webrtc.so
2223
${CMAKE_CURRENT_SOURCE_DIR}/../../libs/linux/x64/liblinux_pc_client.so
2324
)
2425

25-
add_executable(${PROJECT_NAME}
26-
main.cpp
27-
main_window.cpp
28-
main_window.h
29-
loopback.cpp
30-
${CMAKE_CURRENT_SOURCE_DIR}/../common/cpp/libKmpWebrtc.cpp
31-
)
26+
#add_executable(${PROJECT_NAME}
27+
# main.cpp
28+
# main_window.cpp
29+
# main_window.h
30+
# loopback.cpp
31+
# ${CMAKE_CURRENT_SOURCE_DIR}/../common/cpp/libKmpWebrtc.cpp
32+
# )
3233

33-
target_link_libraries(${PROJECT_NAME}
34-
${Qt5Widgets_LIBRARIES}
35-
${deps}
36-
)
34+
#target_link_libraries(${PROJECT_NAME}
35+
# ${Qt5Widgets_LIBRARIES}
36+
# ${deps}
37+
# )
3738

3839
add_executable(loopback
3940
console_app.cpp
@@ -44,3 +45,17 @@ add_executable(loopback
4445
target_link_libraries(loopback
4546
${deps}
4647
)
48+
49+
set(FFMPEG_INCLUDE_DIR /home/linker/src/FFmpeg/build/include)
50+
set(FFMPEG_LIB_DIR /home/linker/src/FFmpeg/build/lib)
51+
include_directories(${FFMPEG_INCLUDE_DIR})
52+
53+
add_executable(ffmpeg_test
54+
ffmpeg_test.cpp
55+
)
56+
57+
target_link_libraries(ffmpeg_test
58+
${FFMPEG_LIB_DIR}/libavcodec.so
59+
${FFMPEG_LIB_DIR}/libavformat.so
60+
${FFMPEG_LIB_DIR}/libavutil.so
61+
)

example/linuxApp/ffmpeg_test.cpp

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
// main.cpp
2+
3+
extern "C" {
4+
#include <libavformat/avformat.h>
5+
#include <libavutil/timestamp.h>
6+
}
7+
8+
#include <iostream>
9+
#include <unistd.h>
10+
11+
int main(int argc, char* argv[]) {
12+
if (argc < 2) {
13+
std::cerr << "Usage: " << argv[0] << " <video_file>" << std::endl;
14+
return 1;
15+
}
16+
17+
const char* input_file = argv[1];
18+
AVPacket* packet = av_packet_alloc();
19+
if (!packet) {
20+
std::cerr << "FileCapturer::Start av_packet_alloc fail";
21+
return -1;
22+
}
23+
av_init_packet(packet);
24+
25+
AVFormatContext* format_context = nullptr;
26+
int error =
27+
avformat_open_input(&format_context, input_file, nullptr, nullptr);
28+
if (error < 0) {
29+
std::cerr << "FileCapturer::Start avformat_open_input fail "
30+
<< input_file << " " << error;//av_err2str(error);
31+
av_packet_free(&packet);
32+
return -2;
33+
}
34+
35+
error = avformat_find_stream_info(format_context, nullptr);
36+
if (error < 0) {
37+
std::cerr << "FileCapturer::Start avformat_find_stream_info fail "
38+
<< error;//av_err2str(error);
39+
av_packet_free(&packet);
40+
avformat_close_input(&format_context);
41+
return -3;
42+
}
43+
44+
int v_stream_no = av_find_best_stream(format_context, AVMEDIA_TYPE_VIDEO, -1,
45+
-1, nullptr, 0);
46+
if (v_stream_no < 0 ||
47+
format_context->streams[v_stream_no]->time_base.den <= 0) {
48+
std::cerr << "FileCapturer::Start av_find_best_stream fail "
49+
<< v_stream_no;//av_err2str(v_stream_no);
50+
av_packet_free(&packet);
51+
avformat_close_input(&format_context);
52+
return -4;
53+
}
54+
AVStream* v_stream = format_context->streams[v_stream_no];
55+
56+
bool first_packet = true;
57+
int64_t last_pts_ms = 0;
58+
int64_t emit_next_packet_ms = 0;
59+
60+
bool need_loop_again = false;
61+
bool running_ = true;
62+
while (running_) {
63+
int ret = av_read_frame(format_context, packet);
64+
if (ret < 0 && ret != AVERROR_EOF) {
65+
std::cerr << "FileCapturer::Start av_read_frame fail "
66+
<< ret;//av_err2str(ret);
67+
running_ = false;
68+
break;
69+
}
70+
if (ret == AVERROR_EOF) {
71+
if (true) {
72+
std::cerr << "FileCapturer::Start got EOF, loop again";
73+
running_ = false;
74+
need_loop_again = true;
75+
break;
76+
} else {
77+
std::cerr << "FileCapturer::Start got EOF, quit";
78+
running_ = false;
79+
break;
80+
}
81+
}
82+
if (packet->stream_index != v_stream->index) {
83+
continue;
84+
}
85+
int64_t now_ms = 0;//rtc::TimeMicros() / 1000;
86+
int64_t this_pts_ms = 1000 * packet->pts * v_stream->time_base.num /
87+
v_stream->time_base.den;
88+
if (first_packet) {
89+
emit_next_packet_ms = now_ms;
90+
last_pts_ms = this_pts_ms;
91+
}
92+
emit_next_packet_ms += this_pts_ms - last_pts_ms;
93+
last_pts_ms = this_pts_ms;
94+
int sleep_ms = (int)(emit_next_packet_ms - now_ms);
95+
96+
// 获取流的时间基
97+
AVRational time_base = v_stream->time_base;
98+
99+
// 将 pts 转换为毫秒
100+
int64_t pts_ms = av_rescale_q(packet->pts, time_base, (AVRational){1, 1000});
101+
first_packet = false;
102+
printf("av_read_frame packet->stream_index %d, v_stream->index %d, v_stream_no %d, packet->pts %d, v_stream->time_base.num %d, v_stream->time_base.den %d, this_pts_ms %d, pts_ms %d\n", packet->stream_index, v_stream->index, v_stream_no, (int) packet->pts, v_stream->time_base.num, v_stream->time_base.den, (int) this_pts_ms, (int) pts_ms);
103+
104+
if (!first_packet && sleep_ms > 5) {
105+
#if defined(WEBRTC_WIN)
106+
Sleep(sleep_ms);
107+
#else
108+
usleep(sleep_ms * 1000);
109+
#endif
110+
}
111+
112+
// if (video_callback_ && running_) {
113+
// rtc::scoped_refptr<webrtc::TransitVideoFrameBuffer> frame_buffer =
114+
// rtc::make_ref_counted<webrtc::TransitVideoFrameBuffer>(
115+
// width_, height_, packet->size);
116+
// memcpy(frame_buffer->mutable_data(), packet->data, packet->size);
117+
118+
// video_callback_->OnFrame(
119+
// VideoFrame::Builder()
120+
// .set_video_frame_buffer(frame_buffer)
121+
// .set_rotation(VideoRotation::kVideoRotation_0)
122+
// .set_dummy(false)
123+
// .set_transit(true)
124+
// .set_rtp_timestamp(0)
125+
// .set_timestamp_ms(rtc::TimeMillis())
126+
// .build());
127+
// }
128+
}
129+
130+
av_packet_free(&packet);
131+
avformat_close_input(&format_context);
132+
133+
return 0;
134+
}

example/linuxApp/loopback.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ static void pcClientFactoryErrorHandler(void*, int error, const char* message) {
2121
}
2222

2323
static const char* pcClientOnPreferCodecs(void*, const char* peer_uid, const char* sdp) {
24-
return sdp;
24+
return PreferSdp(sdp, kKmpWebRTCVideoCodecH264HighProfile);
2525
}
2626

2727
static void pcClientOnPeerConnectionStatsReady(void*, const char* peer_uid, const char* stats) {

0 commit comments

Comments
 (0)