mirror of
https://github.com/kunkundi/crossdesk.git
synced 2025-10-26 12:15:34 +08:00
Support openh264 codec on MacOS
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
#if __APPLE__
|
||||
#include "ffmpeg/ffmpeg_video_decoder.h"
|
||||
#include "openh264/openh264_decoder.h"
|
||||
#else
|
||||
#include "ffmpeg/ffmpeg_video_decoder.h"
|
||||
#include "nvcodec/nvidia_video_decoder.h"
|
||||
@@ -17,6 +18,7 @@ VideoDecoderFactory::~VideoDecoderFactory() {}
|
||||
std::unique_ptr<VideoDecoder> VideoDecoderFactory::CreateVideoDecoder(
|
||||
bool hardware_acceleration) {
|
||||
#if __APPLE__
|
||||
// return std::make_unique<OpenH264Decoder>(OpenH264Decoder());
|
||||
return std::make_unique<FfmpegVideoDecoder>(FfmpegVideoDecoder());
|
||||
#else
|
||||
if (hardware_acceleration) {
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#if __APPLE__
|
||||
#include "ffmpeg/ffmpeg_video_encoder.h"
|
||||
#include "openh264/openh264_encoder.h"
|
||||
#else
|
||||
#include "ffmpeg/ffmpeg_video_encoder.h"
|
||||
#include "nvcodec/nvidia_video_encoder.h"
|
||||
@@ -17,7 +18,8 @@ VideoEncoderFactory::~VideoEncoderFactory() {}
|
||||
std::unique_ptr<VideoEncoder> VideoEncoderFactory::CreateVideoEncoder(
|
||||
bool hardware_acceleration) {
|
||||
#if __APPLE__
|
||||
return std::make_unique<FFmpegVideoEncoder>(FFmpegVideoEncoder());
|
||||
// return std::make_unique<FFmpegVideoEncoder>(FFmpegVideoEncoder());
|
||||
return std::make_unique<OpenH264Encoder>(OpenH264Encoder());
|
||||
#else
|
||||
if (hardware_acceleration) {
|
||||
if (CheckIsHardwareAccerlerationSupported()) {
|
||||
|
||||
@@ -11,16 +11,20 @@ using nlohmann::json;
|
||||
|
||||
PeerConnection::PeerConnection() {}
|
||||
|
||||
PeerConnection::~PeerConnection() {
|
||||
if (nv12_data_) {
|
||||
PeerConnection::~PeerConnection()
|
||||
{
|
||||
if (nv12_data_)
|
||||
{
|
||||
delete nv12_data_;
|
||||
nv12_data_ = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
int PeerConnection::Init(PeerConnectionParams params,
|
||||
const std::string &user_id) {
|
||||
if (inited_) {
|
||||
const std::string &user_id)
|
||||
{
|
||||
if (inited_)
|
||||
{
|
||||
LOG_INFO("Peer already inited");
|
||||
return 0;
|
||||
}
|
||||
@@ -52,7 +56,8 @@ int PeerConnection::Init(PeerConnectionParams params,
|
||||
|
||||
if (!cfg_turn_server_ip_.empty() && 0 != turn_server_port_ &&
|
||||
!cfg_turn_server_username_.empty() &&
|
||||
!cfg_turn_server_password_.empty()) {
|
||||
!cfg_turn_server_password_.empty())
|
||||
{
|
||||
LOG_INFO("turn server ip [{}] port [{}] username [{}] password [{}]",
|
||||
cfg_turn_server_ip_, turn_server_port_, cfg_turn_server_username_,
|
||||
cfg_turn_server_password_);
|
||||
@@ -68,33 +73,48 @@ int PeerConnection::Init(PeerConnectionParams params,
|
||||
on_signal_status_ = params.on_signal_status;
|
||||
on_connection_status_ = params.on_connection_status;
|
||||
|
||||
on_receive_ws_msg_ = [this](const std::string &msg) { ProcessSignal(msg); };
|
||||
on_receive_ws_msg_ = [this](const std::string &msg)
|
||||
{ ProcessSignal(msg); };
|
||||
|
||||
on_ws_status_ = [this](WsStatus ws_status) {
|
||||
if (WsStatus::WsOpening == ws_status) {
|
||||
on_ws_status_ = [this](WsStatus ws_status)
|
||||
{
|
||||
if (WsStatus::WsOpening == ws_status)
|
||||
{
|
||||
signal_status_ = SignalStatus::SignalConnecting;
|
||||
on_signal_status_(SignalStatus::SignalConnecting);
|
||||
} else if (WsStatus::WsOpened == ws_status) {
|
||||
}
|
||||
else if (WsStatus::WsOpened == ws_status)
|
||||
{
|
||||
signal_status_ = SignalStatus::SignalConnected;
|
||||
on_signal_status_(SignalStatus::SignalConnected);
|
||||
} else if (WsStatus::WsFailed == ws_status) {
|
||||
}
|
||||
else if (WsStatus::WsFailed == ws_status)
|
||||
{
|
||||
signal_status_ = SignalStatus::SignalFailed;
|
||||
on_signal_status_(SignalStatus::SignalFailed);
|
||||
} else if (WsStatus::WsClosed == ws_status) {
|
||||
}
|
||||
else if (WsStatus::WsClosed == ws_status)
|
||||
{
|
||||
signal_status_ = SignalStatus::SignalClosed;
|
||||
on_signal_status_(SignalStatus::SignalClosed);
|
||||
} else if (WsStatus::WsReconnecting == ws_status) {
|
||||
}
|
||||
else if (WsStatus::WsReconnecting == ws_status)
|
||||
{
|
||||
signal_status_ = SignalStatus::SignalReconnecting;
|
||||
on_signal_status_(SignalStatus::SignalReconnecting);
|
||||
}
|
||||
};
|
||||
|
||||
on_receive_video_ = [this](const char *data, size_t size, const char *user_id,
|
||||
size_t user_id_size) {
|
||||
size_t user_id_size)
|
||||
{
|
||||
int num_frame_returned = video_decoder_->Decode(
|
||||
(uint8_t *)data, size,
|
||||
[this, user_id, user_id_size](VideoFrame video_frame) {
|
||||
if (on_receive_video_buffer_) {
|
||||
[this, user_id, user_id_size](VideoFrame video_frame)
|
||||
{
|
||||
if (on_receive_video_buffer_)
|
||||
{
|
||||
// LOG_ERROR("Receive video, size {}", video_frame.Size());
|
||||
on_receive_video_buffer_((const char *)video_frame.Buffer(),
|
||||
video_frame.Size(), user_id, user_id_size);
|
||||
}
|
||||
@@ -102,11 +122,14 @@ int PeerConnection::Init(PeerConnectionParams params,
|
||||
};
|
||||
|
||||
on_receive_audio_ = [this](const char *data, size_t size, const char *user_id,
|
||||
size_t user_id_size) {
|
||||
size_t user_id_size)
|
||||
{
|
||||
int num_frame_returned = audio_decoder_->Decode(
|
||||
(uint8_t *)data, size,
|
||||
[this, user_id, user_id_size](uint8_t *data, int size) {
|
||||
if (on_receive_audio_buffer_) {
|
||||
[this, user_id, user_id_size](uint8_t *data, int size)
|
||||
{
|
||||
if (on_receive_audio_buffer_)
|
||||
{
|
||||
on_receive_audio_buffer_((const char *)data, size, user_id,
|
||||
user_id_size);
|
||||
}
|
||||
@@ -114,28 +137,40 @@ int PeerConnection::Init(PeerConnectionParams params,
|
||||
};
|
||||
|
||||
on_receive_data_ = [this](const char *data, size_t size, const char *user_id,
|
||||
size_t user_id_size) {
|
||||
if (on_receive_data_buffer_) {
|
||||
size_t user_id_size)
|
||||
{
|
||||
if (on_receive_data_buffer_)
|
||||
{
|
||||
on_receive_data_buffer_(data, size, user_id, user_id_size);
|
||||
}
|
||||
};
|
||||
|
||||
on_ice_status_change_ = [this](std::string ice_status) {
|
||||
if ("connecting" == ice_status) {
|
||||
on_ice_status_change_ = [this](std::string ice_status)
|
||||
{
|
||||
if ("connecting" == ice_status)
|
||||
{
|
||||
on_connection_status_(ConnectionStatus::Connecting);
|
||||
} else if ("disconnected" == ice_status) {
|
||||
}
|
||||
else if ("disconnected" == ice_status)
|
||||
{
|
||||
on_connection_status_(ConnectionStatus::Disconnected);
|
||||
} else if ("completed" == ice_status || "ready" == ice_status ||
|
||||
"connected" == ice_status) {
|
||||
}
|
||||
else if ("completed" == ice_status || "ready" == ice_status ||
|
||||
"connected" == ice_status)
|
||||
{
|
||||
ice_ready_ = true;
|
||||
on_connection_status_(ConnectionStatus::Connected);
|
||||
b_force_i_frame_ = true;
|
||||
LOG_INFO("Ice finish");
|
||||
} else if ("closed" == ice_status) {
|
||||
}
|
||||
else if ("closed" == ice_status)
|
||||
{
|
||||
ice_ready_ = false;
|
||||
on_connection_status_(ConnectionStatus::Closed);
|
||||
LOG_INFO("Ice closed");
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
ice_ready_ = false;
|
||||
}
|
||||
};
|
||||
@@ -143,7 +178,8 @@ int PeerConnection::Init(PeerConnectionParams params,
|
||||
ws_transport_ =
|
||||
std::make_shared<WsTransmission>(on_receive_ws_msg_, on_ws_status_);
|
||||
uri_ = "ws://" + cfg_signal_server_ip_ + ":" + cfg_signal_server_port_;
|
||||
if (ws_transport_) {
|
||||
if (ws_transport_)
|
||||
{
|
||||
ws_transport_->Connect(uri_);
|
||||
}
|
||||
|
||||
@@ -152,12 +188,14 @@ int PeerConnection::Init(PeerConnectionParams params,
|
||||
|
||||
nv12_data_ = new char[1280 * 720 * 3 / 2];
|
||||
|
||||
if (0 != CreateVideoCodec(hardware_acceleration_)) {
|
||||
if (0 != CreateVideoCodec(hardware_acceleration_))
|
||||
{
|
||||
LOG_ERROR("Create video codec failed");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (0 != CreateAudioCodec()) {
|
||||
if (0 != CreateAudioCodec())
|
||||
{
|
||||
LOG_ERROR("Create audio codec failed");
|
||||
return -1;
|
||||
}
|
||||
@@ -166,9 +204,11 @@ int PeerConnection::Init(PeerConnectionParams params,
|
||||
return 0;
|
||||
}
|
||||
|
||||
int PeerConnection::CreateVideoCodec(bool hardware_acceleration) {
|
||||
int PeerConnection::CreateVideoCodec(bool hardware_acceleration)
|
||||
{
|
||||
#ifdef __APPLE__
|
||||
if (hardware_acceleration) {
|
||||
if (hardware_acceleration)
|
||||
{
|
||||
hardware_acceleration = false;
|
||||
LOG_WARN(
|
||||
"MacOS not support hardware acceleration, use default software codec");
|
||||
@@ -178,21 +218,25 @@ int PeerConnection::CreateVideoCodec(bool hardware_acceleration) {
|
||||
|
||||
video_encoder_ =
|
||||
VideoEncoderFactory::CreateVideoEncoder(hardware_acceleration_);
|
||||
if (hardware_acceleration_ && !video_encoder_) {
|
||||
if (hardware_acceleration_ && !video_encoder_)
|
||||
{
|
||||
LOG_WARN(
|
||||
"Hardware accelerated encoder not available, use default software "
|
||||
"encoder");
|
||||
video_encoder_ = VideoEncoderFactory::CreateVideoEncoder(false);
|
||||
if (!video_encoder_) {
|
||||
if (!video_encoder_)
|
||||
{
|
||||
LOG_ERROR(
|
||||
"Hardware accelerated encoder and software encoder both not "
|
||||
"available");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
if (0 != video_encoder_->Init()) {
|
||||
if (0 != video_encoder_->Init())
|
||||
{
|
||||
video_encoder_ = VideoEncoderFactory::CreateVideoEncoder(false);
|
||||
if (!video_encoder_ || 0 != video_encoder_->Init()) {
|
||||
if (!video_encoder_ || 0 != video_encoder_->Init())
|
||||
{
|
||||
LOG_ERROR("Encoder init failed");
|
||||
return -1;
|
||||
}
|
||||
@@ -200,21 +244,25 @@ int PeerConnection::CreateVideoCodec(bool hardware_acceleration) {
|
||||
|
||||
video_decoder_ =
|
||||
VideoDecoderFactory::CreateVideoDecoder(hardware_acceleration_);
|
||||
if (hardware_acceleration_ && !video_decoder_) {
|
||||
if (hardware_acceleration_ && !video_decoder_)
|
||||
{
|
||||
LOG_WARN(
|
||||
"Hardware accelerated decoder not available, use default software "
|
||||
"decoder");
|
||||
video_decoder_ = VideoDecoderFactory::CreateVideoDecoder(false);
|
||||
if (!video_decoder_) {
|
||||
if (!video_decoder_)
|
||||
{
|
||||
LOG_ERROR(
|
||||
"Hardware accelerated decoder and software decoder both not "
|
||||
"available");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
if (0 != video_decoder_->Init()) {
|
||||
if (0 != video_decoder_->Init())
|
||||
{
|
||||
video_decoder_ = VideoDecoderFactory::CreateVideoDecoder(false);
|
||||
if (!video_decoder_ || video_decoder_->Init()) {
|
||||
if (!video_decoder_ || video_decoder_->Init())
|
||||
{
|
||||
LOG_ERROR("Decoder init failed");
|
||||
return -1;
|
||||
}
|
||||
@@ -222,15 +270,18 @@ int PeerConnection::CreateVideoCodec(bool hardware_acceleration) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int PeerConnection::CreateAudioCodec() {
|
||||
int PeerConnection::CreateAudioCodec()
|
||||
{
|
||||
audio_encoder_ = std::make_unique<AudioEncoder>(AudioEncoder(48000, 1, 480));
|
||||
if (!audio_encoder_ || 0 != audio_encoder_->Init()) {
|
||||
if (!audio_encoder_ || 0 != audio_encoder_->Init())
|
||||
{
|
||||
LOG_ERROR("Audio encoder init failed");
|
||||
return -1;
|
||||
}
|
||||
|
||||
audio_decoder_ = std::make_unique<AudioDecoder>(AudioDecoder(48000, 1, 480));
|
||||
if (!audio_decoder_ || 0 != audio_decoder_->Init()) {
|
||||
if (!audio_decoder_ || 0 != audio_decoder_->Init())
|
||||
{
|
||||
LOG_ERROR("Audio decoder init failed");
|
||||
return -1;
|
||||
}
|
||||
@@ -240,8 +291,10 @@ int PeerConnection::CreateAudioCodec() {
|
||||
|
||||
int PeerConnection::Create(PeerConnectionParams params,
|
||||
const std::string &transmission_id,
|
||||
const std::string &password) {
|
||||
if (SignalStatus::SignalConnected != GetSignalStatus()) {
|
||||
const std::string &password)
|
||||
{
|
||||
if (SignalStatus::SignalConnected != GetSignalStatus())
|
||||
{
|
||||
LOG_ERROR("Signal not connected");
|
||||
return -1;
|
||||
}
|
||||
@@ -255,7 +308,8 @@ int PeerConnection::Create(PeerConnectionParams params,
|
||||
{"transmission_id", transmission_id},
|
||||
{"password", password}};
|
||||
|
||||
if (ws_transport_) {
|
||||
if (ws_transport_)
|
||||
{
|
||||
ws_transport_->Send(message.dump());
|
||||
LOG_INFO("Send create transmission request, transmission_id [{}]",
|
||||
transmission_id);
|
||||
@@ -265,8 +319,10 @@ int PeerConnection::Create(PeerConnectionParams params,
|
||||
|
||||
int PeerConnection::Join(PeerConnectionParams params,
|
||||
const std::string &transmission_id,
|
||||
const std::string &password) {
|
||||
if (SignalStatus::SignalConnected != GetSignalStatus()) {
|
||||
const std::string &password)
|
||||
{
|
||||
if (SignalStatus::SignalConnected != GetSignalStatus())
|
||||
{
|
||||
LOG_ERROR("Signal not connected");
|
||||
return -1;
|
||||
}
|
||||
@@ -280,8 +336,10 @@ int PeerConnection::Join(PeerConnectionParams params,
|
||||
return ret;
|
||||
}
|
||||
|
||||
int PeerConnection::Leave() {
|
||||
if (SignalStatus::SignalConnected != GetSignalStatus()) {
|
||||
int PeerConnection::Leave()
|
||||
{
|
||||
if (SignalStatus::SignalConnected != GetSignalStatus())
|
||||
{
|
||||
LOG_ERROR("Signal not connected");
|
||||
return -1;
|
||||
}
|
||||
@@ -289,7 +347,8 @@ int PeerConnection::Leave() {
|
||||
json message = {{"type", "leave_transmission"},
|
||||
{"user_id", user_id_},
|
||||
{"transmission_id", transmission_id_}};
|
||||
if (ws_transport_) {
|
||||
if (ws_transport_)
|
||||
{
|
||||
ws_transport_->Send(message.dump());
|
||||
LOG_INFO("[{}] sends leave transmission [{}] notification ", user_id_,
|
||||
transmission_id_);
|
||||
@@ -297,117 +356,78 @@ int PeerConnection::Leave() {
|
||||
|
||||
ice_ready_ = false;
|
||||
|
||||
for (auto &user_id_it : ice_transmission_list_) {
|
||||
for (auto &user_id_it : ice_transmission_list_)
|
||||
{
|
||||
user_id_it.second->DestroyIceTransmission();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void PeerConnection::ProcessSignal(const std::string &signal) {
|
||||
void PeerConnection::ProcessSignal(const std::string &signal)
|
||||
{
|
||||
auto j = json::parse(signal);
|
||||
std::string type = j["type"];
|
||||
LOG_INFO("signal type: {}", type);
|
||||
switch (HASH_STRING_PIECE(type.c_str())) {
|
||||
case "ws_connection_id"_H: {
|
||||
ws_connection_id_ = j["ws_connection_id"].get<unsigned int>();
|
||||
LOG_INFO("Receive local peer websocket connection id [{}]",
|
||||
ws_connection_id_);
|
||||
break;
|
||||
switch (HASH_STRING_PIECE(type.c_str()))
|
||||
{
|
||||
case "ws_connection_id"_H:
|
||||
{
|
||||
ws_connection_id_ = j["ws_connection_id"].get<unsigned int>();
|
||||
LOG_INFO("Receive local peer websocket connection id [{}]",
|
||||
ws_connection_id_);
|
||||
break;
|
||||
}
|
||||
case "transmission_id"_H:
|
||||
{
|
||||
if (j["status"].get<std::string>() == "success")
|
||||
{
|
||||
transmission_id_ = j["transmission_id"].get<std::string>();
|
||||
LOG_INFO("Create transmission success with id [{}]", transmission_id_);
|
||||
}
|
||||
case "transmission_id"_H: {
|
||||
if (j["status"].get<std::string>() == "success") {
|
||||
transmission_id_ = j["transmission_id"].get<std::string>();
|
||||
LOG_INFO("Create transmission success with id [{}]", transmission_id_);
|
||||
|
||||
} else if (j["status"].get<std::string>() == "fail") {
|
||||
LOG_WARN("Create transmission failed with id [{}], due to [{}]",
|
||||
transmission_id_, j["reason"].get<std::string>().c_str());
|
||||
}
|
||||
break;
|
||||
else if (j["status"].get<std::string>() == "fail")
|
||||
{
|
||||
LOG_WARN("Create transmission failed with id [{}], due to [{}]",
|
||||
transmission_id_, j["reason"].get<std::string>().c_str());
|
||||
}
|
||||
case "user_id_list"_H: {
|
||||
user_id_list_ = j["user_id_list"];
|
||||
break;
|
||||
}
|
||||
case "user_id_list"_H:
|
||||
{
|
||||
user_id_list_ = j["user_id_list"];
|
||||
|
||||
std::string transmission_id = j["transmission_id"].get<std::string>();
|
||||
std::string status = j["status"].get<std::string>();
|
||||
if (status == "failed") {
|
||||
std::string reason = j["reason"].get<std::string>();
|
||||
LOG_ERROR("{}", reason);
|
||||
on_connection_status_(ConnectionStatus::IncorrectPassword);
|
||||
} else {
|
||||
if (user_id_list_.empty()) {
|
||||
LOG_WARN("Wait for host create transmission [{}]", transmission_id);
|
||||
RequestTransmissionMemberList(transmission_id, password_);
|
||||
break;
|
||||
}
|
||||
|
||||
LOG_INFO("Transmission [{}] members: [", transmission_id);
|
||||
for (auto user_id : user_id_list_) {
|
||||
LOG_INFO("{}", user_id);
|
||||
}
|
||||
LOG_INFO("]");
|
||||
|
||||
for (auto &remote_user_id : user_id_list_) {
|
||||
if (remote_user_id == user_id_) {
|
||||
continue;
|
||||
}
|
||||
ice_transmission_list_[remote_user_id] =
|
||||
std::make_unique<IceTransmission>(true, transmission_id, user_id_,
|
||||
remote_user_id, ws_transport_,
|
||||
on_ice_status_change_);
|
||||
|
||||
ice_transmission_list_[remote_user_id]->SetOnReceiveVideoFunc(
|
||||
on_receive_video_);
|
||||
ice_transmission_list_[remote_user_id]->SetOnReceiveAudioFunc(
|
||||
on_receive_audio_);
|
||||
ice_transmission_list_[remote_user_id]->SetOnReceiveDataFunc(
|
||||
on_receive_data_);
|
||||
|
||||
ice_transmission_list_[remote_user_id]->InitIceTransmission(
|
||||
cfg_stun_server_ip_, stun_server_port_, cfg_turn_server_ip_,
|
||||
turn_server_port_, cfg_turn_server_username_,
|
||||
cfg_turn_server_password_);
|
||||
ice_transmission_list_[remote_user_id]->JoinTransmission();
|
||||
}
|
||||
|
||||
// on_connection_status_(ConnectionStatus::Connecting);
|
||||
std::string transmission_id = j["transmission_id"].get<std::string>();
|
||||
std::string status = j["status"].get<std::string>();
|
||||
if (status == "failed")
|
||||
{
|
||||
std::string reason = j["reason"].get<std::string>();
|
||||
LOG_ERROR("{}", reason);
|
||||
on_connection_status_(ConnectionStatus::IncorrectPassword);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (user_id_list_.empty())
|
||||
{
|
||||
LOG_WARN("Wait for host create transmission [{}]", transmission_id);
|
||||
RequestTransmissionMemberList(transmission_id, password_);
|
||||
break;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case "user_leave_transmission"_H: {
|
||||
std::string user_id = j["user_id"];
|
||||
LOG_INFO("Receive notification: user id [{}] leave transmission",
|
||||
user_id);
|
||||
auto user_id_it = ice_transmission_list_.find(user_id);
|
||||
if (user_id_it != ice_transmission_list_.end()) {
|
||||
user_id_it->second->DestroyIceTransmission();
|
||||
ice_transmission_list_.erase(user_id_it);
|
||||
ice_ready_ = false;
|
||||
LOG_INFO("Terminate transmission to user [{}]", user_id);
|
||||
|
||||
if (std::string::npos != user_id.find("S-")) {
|
||||
LOG_INFO("Server leaves, try to rejoin transmission");
|
||||
|
||||
RequestTransmissionMemberList(transmission_id_, password_);
|
||||
}
|
||||
LOG_INFO("Transmission [{}] members: [", transmission_id);
|
||||
for (auto user_id : user_id_list_)
|
||||
{
|
||||
LOG_INFO("{}", user_id);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "offer"_H: {
|
||||
std::string remote_sdp = j["sdp"].get<std::string>();
|
||||
|
||||
if (remote_sdp.empty()) {
|
||||
LOG_INFO("Invalid remote sdp");
|
||||
} else {
|
||||
std::string transmission_id = j["transmission_id"].get<std::string>();
|
||||
std::string sdp = j["sdp"].get<std::string>();
|
||||
std::string remote_user_id = j["remote_user_id"].get<std::string>();
|
||||
LOG_INFO("[{}] receive offer from [{}]", user_id_, remote_user_id);
|
||||
LOG_INFO("]");
|
||||
|
||||
for (auto &remote_user_id : user_id_list_)
|
||||
{
|
||||
if (remote_user_id == user_id_)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
ice_transmission_list_[remote_user_id] =
|
||||
std::make_unique<IceTransmission>(false, transmission_id, user_id_,
|
||||
std::make_unique<IceTransmission>(true, transmission_id, user_id_,
|
||||
remote_user_id, ws_transport_,
|
||||
on_ice_status_change_);
|
||||
|
||||
@@ -422,47 +442,116 @@ void PeerConnection::ProcessSignal(const std::string &signal) {
|
||||
cfg_stun_server_ip_, stun_server_port_, cfg_turn_server_ip_,
|
||||
turn_server_port_, cfg_turn_server_username_,
|
||||
cfg_turn_server_password_);
|
||||
ice_transmission_list_[remote_user_id]->JoinTransmission();
|
||||
}
|
||||
|
||||
ice_transmission_list_[remote_user_id]->SetTransmissionId(
|
||||
transmission_id_);
|
||||
// on_connection_status_(ConnectionStatus::Connecting);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case "user_leave_transmission"_H:
|
||||
{
|
||||
std::string user_id = j["user_id"];
|
||||
LOG_INFO("Receive notification: user id [{}] leave transmission",
|
||||
user_id);
|
||||
auto user_id_it = ice_transmission_list_.find(user_id);
|
||||
if (user_id_it != ice_transmission_list_.end())
|
||||
{
|
||||
user_id_it->second->DestroyIceTransmission();
|
||||
ice_transmission_list_.erase(user_id_it);
|
||||
ice_ready_ = false;
|
||||
LOG_INFO("Terminate transmission to user [{}]", user_id);
|
||||
|
||||
if (std::string::npos != user_id.find("S-"))
|
||||
{
|
||||
LOG_INFO("Server leaves, try to rejoin transmission");
|
||||
|
||||
RequestTransmissionMemberList(transmission_id_, password_);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "offer"_H:
|
||||
{
|
||||
std::string remote_sdp = j["sdp"].get<std::string>();
|
||||
|
||||
if (remote_sdp.empty())
|
||||
{
|
||||
LOG_INFO("Invalid remote sdp");
|
||||
}
|
||||
else
|
||||
{
|
||||
std::string transmission_id = j["transmission_id"].get<std::string>();
|
||||
std::string sdp = j["sdp"].get<std::string>();
|
||||
std::string remote_user_id = j["remote_user_id"].get<std::string>();
|
||||
LOG_INFO("[{}] receive offer from [{}]", user_id_, remote_user_id);
|
||||
|
||||
ice_transmission_list_[remote_user_id] =
|
||||
std::make_unique<IceTransmission>(false, transmission_id, user_id_,
|
||||
remote_user_id, ws_transport_,
|
||||
on_ice_status_change_);
|
||||
|
||||
ice_transmission_list_[remote_user_id]->SetOnReceiveVideoFunc(
|
||||
on_receive_video_);
|
||||
ice_transmission_list_[remote_user_id]->SetOnReceiveAudioFunc(
|
||||
on_receive_audio_);
|
||||
ice_transmission_list_[remote_user_id]->SetOnReceiveDataFunc(
|
||||
on_receive_data_);
|
||||
|
||||
ice_transmission_list_[remote_user_id]->InitIceTransmission(
|
||||
cfg_stun_server_ip_, stun_server_port_, cfg_turn_server_ip_,
|
||||
turn_server_port_, cfg_turn_server_username_,
|
||||
cfg_turn_server_password_);
|
||||
|
||||
ice_transmission_list_[remote_user_id]->SetTransmissionId(
|
||||
transmission_id_);
|
||||
|
||||
ice_transmission_list_[remote_user_id]->SetRemoteSdp(remote_sdp);
|
||||
|
||||
ice_transmission_list_[remote_user_id]->GatherCandidates();
|
||||
|
||||
on_connection_status_(ConnectionStatus::Connecting);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "answer"_H:
|
||||
{
|
||||
std::string remote_sdp = j["sdp"].get<std::string>();
|
||||
if (remote_sdp.empty())
|
||||
{
|
||||
LOG_INFO("remote_sdp is empty");
|
||||
}
|
||||
else
|
||||
{
|
||||
std::string transmission_id = j["transmission_id"].get<std::string>();
|
||||
std::string sdp = j["sdp"].get<std::string>();
|
||||
std::string remote_user_id = j["remote_user_id"].get<std::string>();
|
||||
|
||||
LOG_INFO("[{}] receive answer from [{}]", user_id_, remote_user_id);
|
||||
|
||||
if (ice_transmission_list_.find(remote_user_id) !=
|
||||
ice_transmission_list_.end())
|
||||
{
|
||||
ice_transmission_list_[remote_user_id]->SetRemoteSdp(remote_sdp);
|
||||
|
||||
ice_transmission_list_[remote_user_id]->GatherCandidates();
|
||||
|
||||
on_connection_status_(ConnectionStatus::Connecting);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "answer"_H: {
|
||||
std::string remote_sdp = j["sdp"].get<std::string>();
|
||||
if (remote_sdp.empty()) {
|
||||
LOG_INFO("remote_sdp is empty");
|
||||
} else {
|
||||
std::string transmission_id = j["transmission_id"].get<std::string>();
|
||||
std::string sdp = j["sdp"].get<std::string>();
|
||||
std::string remote_user_id = j["remote_user_id"].get<std::string>();
|
||||
|
||||
LOG_INFO("[{}] receive answer from [{}]", user_id_, remote_user_id);
|
||||
|
||||
if (ice_transmission_list_.find(remote_user_id) !=
|
||||
ice_transmission_list_.end()) {
|
||||
ice_transmission_list_[remote_user_id]->SetRemoteSdp(remote_sdp);
|
||||
}
|
||||
|
||||
on_connection_status_(ConnectionStatus::Connecting);
|
||||
}
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
break;
|
||||
on_connection_status_(ConnectionStatus::Connecting);
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int PeerConnection::RequestTransmissionMemberList(
|
||||
const std::string &transmission_id, const std::string &password) {
|
||||
if (SignalStatus::SignalConnected != GetSignalStatus()) {
|
||||
const std::string &transmission_id, const std::string &password)
|
||||
{
|
||||
if (SignalStatus::SignalConnected != GetSignalStatus())
|
||||
{
|
||||
LOG_ERROR("Signal not connected");
|
||||
return -1;
|
||||
}
|
||||
@@ -473,7 +562,8 @@ int PeerConnection::RequestTransmissionMemberList(
|
||||
{"transmission_id", transmission_id_},
|
||||
{"password", password}};
|
||||
|
||||
if (ws_transport_) {
|
||||
if (ws_transport_)
|
||||
{
|
||||
ws_transport_->Send(message.dump());
|
||||
}
|
||||
return 0;
|
||||
@@ -481,37 +571,43 @@ int PeerConnection::RequestTransmissionMemberList(
|
||||
|
||||
int PeerConnection::Destroy() { return 0; }
|
||||
|
||||
SignalStatus PeerConnection::GetSignalStatus() {
|
||||
SignalStatus PeerConnection::GetSignalStatus()
|
||||
{
|
||||
std::lock_guard<std::mutex> l(signal_status_mutex_);
|
||||
return signal_status_;
|
||||
}
|
||||
|
||||
int PeerConnection::SendVideoData(const char *data, size_t size) {
|
||||
if (!ice_ready_) {
|
||||
int PeerConnection::SendVideoData(const char *data, size_t size)
|
||||
{
|
||||
if (!ice_ready_)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (ice_transmission_list_.empty()) {
|
||||
if (ice_transmission_list_.empty())
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (b_force_i_frame_) {
|
||||
if (b_force_i_frame_)
|
||||
{
|
||||
video_encoder_->ForceIdr();
|
||||
LOG_INFO("Force I frame");
|
||||
b_force_i_frame_ = false;
|
||||
}
|
||||
|
||||
int ret = video_encoder_->Encode(
|
||||
(uint8_t *)data, size, [this](char *encoded_frame, size_t size) -> int {
|
||||
(uint8_t *)data, size, [this](char *encoded_frame, size_t size) -> int
|
||||
{
|
||||
for (auto &ice_trans : ice_transmission_list_) {
|
||||
// LOG_ERROR("H264 frame size: [{}]", size);
|
||||
LOG_ERROR("H264 frame size: [{}]", size);
|
||||
ice_trans.second->SendData(IceTransmission::DATA_TYPE::VIDEO,
|
||||
encoded_frame, size);
|
||||
}
|
||||
return 0;
|
||||
});
|
||||
return 0; });
|
||||
|
||||
if (0 != ret) {
|
||||
if (0 != ret)
|
||||
{
|
||||
LOG_ERROR("Encode failed");
|
||||
return -1;
|
||||
}
|
||||
@@ -519,11 +615,14 @@ int PeerConnection::SendVideoData(const char *data, size_t size) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int PeerConnection::SendAudioData(const char *data, size_t size) {
|
||||
int PeerConnection::SendAudioData(const char *data, size_t size)
|
||||
{
|
||||
int ret = audio_encoder_->Encode(
|
||||
(uint8_t *)data, size,
|
||||
[this](char *encoded_audio_buffer, size_t size) -> int {
|
||||
for (auto &ice_trans : ice_transmission_list_) {
|
||||
[this](char *encoded_audio_buffer, size_t size) -> int
|
||||
{
|
||||
for (auto &ice_trans : ice_transmission_list_)
|
||||
{
|
||||
// LOG_ERROR("opus frame size: [{}]", size);
|
||||
ice_trans.second->SendData(IceTransmission::DATA_TYPE::AUDIO,
|
||||
encoded_audio_buffer, size);
|
||||
@@ -534,8 +633,10 @@ int PeerConnection::SendAudioData(const char *data, size_t size) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int PeerConnection::SendUserData(const char *data, size_t size) {
|
||||
for (auto &ice_trans : ice_transmission_list_) {
|
||||
int PeerConnection::SendUserData(const char *data, size_t size)
|
||||
{
|
||||
for (auto &ice_trans : ice_transmission_list_)
|
||||
{
|
||||
ice_trans.second->SendData(IceTransmission::DATA_TYPE::DATA, data, size);
|
||||
}
|
||||
return 0;
|
||||
|
||||
2
thirdparty/ffmpeg/xmake.lua
vendored
2
thirdparty/ffmpeg/xmake.lua
vendored
@@ -45,7 +45,7 @@ package("ffmpeg")
|
||||
add_configs("libopenh264", {description = "Enable libopenh264", default = false, type = "boolean"})
|
||||
end
|
||||
|
||||
add_links("avfilter", "avdevice", "avformat", "avcodec", "swscale", "swresample", "avutil")
|
||||
add_links("avfilter", "avdevice", "avformat", "avcodec", "swscale", "swresample", "avutil", "postproc")
|
||||
if is_plat("macosx") then
|
||||
add_frameworks("CoreFoundation", "Foundation", "CoreVideo", "CoreMedia", "AudioToolbox", "VideoToolbox", "Security")
|
||||
elseif is_plat("linux") then
|
||||
|
||||
14
xmake.lua
14
xmake.lua
@@ -33,9 +33,9 @@ elseif is_os("linux") then
|
||||
add_syslinks("pthread")
|
||||
elseif is_os("macosx") then
|
||||
add_requires("ffmpeg 5.1.2", {system = false})
|
||||
add_requires("brew::libnice", "brew::glib")
|
||||
add_requires("brew::libnice")
|
||||
add_requires("brew::openh264", {configs = {shared = false}})
|
||||
add_packages("ffmpeg", "glib", "libnice")
|
||||
add_packages("ffmpeg", "brew::libnice", "brew::openh264")
|
||||
add_ldflags("-Wl,-ld_classic")
|
||||
end
|
||||
|
||||
@@ -162,13 +162,17 @@ target("media")
|
||||
add_files("src/media/video/encode/*.cpp",
|
||||
"src/media/video/decode/*.cpp",
|
||||
"src/media/video/encode/ffmpeg/*.cpp",
|
||||
"src/media/video/decode/ffmpeg/*.cpp")
|
||||
"src/media/video/decode/ffmpeg/*.cpp",
|
||||
"src/media/video/encode/openh264/*.cpp",
|
||||
"src/media/video/decode/openh264/*.cpp")
|
||||
add_includedirs("src/media/video/encode",
|
||||
"src/media/video/decode",
|
||||
"src/media/video/encode/ffmpeg",
|
||||
"src/media/video/decode/ffmpeg", {public = true})
|
||||
"src/media/video/decode/ffmpeg",
|
||||
"src/media/video/encode/openh264",
|
||||
"src/media/video/decode/openh264", {public = true})
|
||||
end
|
||||
add_files("src/media/audio/encode/*.cpp",
|
||||
add_files("src/media/audio/encode/*.cpp",
|
||||
"src/media/audio/decode/*.cpp")
|
||||
add_includedirs("src/media/audio/encode",
|
||||
"src/media/audio/decode", {public = true})
|
||||
|
||||
Reference in New Issue
Block a user