10CLibDataChannelLogCallback::CLibDataChannelLogCallback(QObject *parent)
 
   13    rtc::InitLogger(rtc::LogLevel::Debug, logCallback);
 
   16void CLibDataChannelLogCallback::slotEnable(
bool enable)
 
   21void CLibDataChannelLogCallback::logCallback(rtc::LogLevel level, std::string message)
 
   23    if(!m_bEnable) 
return;
 
   25    case rtc::LogLevel::Verbose:
 
   26    case rtc::LogLevel::Debug:
 
   27        qDebug(
m_Log) << message.c_str();
 
   29    case rtc::LogLevel::Info:
 
   30        qInfo(
m_Log) << message.c_str();
 
   32    case rtc::LogLevel::Warning:
 
   33        qWarning(
m_Log) << message.c_str();
 
   35    case rtc::LogLevel::Error:
 
   36    case rtc::LogLevel::Fatal:
 
   37        qCritical(
m_Log) << message.c_str();
 
   39    case rtc::LogLevel::None:
 
   44bool CLibDataChannelLogCallback::m_bEnable = 
true;
 
   49CChannelIce::CChannelIce(QObject* parent) : 
CChannel(parent),
 
   53CChannelIce::CChannelIce(
CIceSignal* pSignal, QObject *parent)
 
   61CChannelIce::~CChannelIce()
 
   63    qDebug(m_Log) << 
"CChannelIce::~CChannelIce()";
 
   73                        this, SLOT(slotSignalConnected()));
 
   76                        this, SLOT(slotSignalDisconnected()));
 
   78        check = connect(m_pSignal,
 
   79                        SIGNAL(sigDescription(
const QString&,
 
   85                        SLOT(slotSignalReceiverDescription(
const QString&,
 
   91        check = connect(m_pSignal,
 
   92                        SIGNAL(sigCandidate(
const QString&,
 
   98                        SLOT(slotSignalReceiverCandidate(
const QString&,
 
  104        check = connect(m_pSignal, SIGNAL(
sigError(
int, 
const QString&)),
 
  105                        this, SLOT(slotSignalError(
int, 
const QString&)));
 
  111QString CChannelIce::GetUser()
 
  116QString CChannelIce::GetPeerUser()
 
  121QString CChannelIce::GetChannelId()
 
  123    return m_szChannelId;
 
  132int CChannelIce::SetDataChannel(std::shared_ptr<rtc::DataChannel> dc)
 
  140    dc->onOpen([
this]() {
 
  141        qInfo(m_Log) << 
"Open data channel user:" << GetUser()
 
  142                      << 
";peer:" << GetPeerUser()
 
  143                      << 
";channelId:" << GetChannelId()
 
  144                      << 
";label:" << m_dataChannel->label().c_str();
 
  145        if(QIODevice::open(QIODevice::ReadWrite))
 
  148            qInfo(m_Log) << 
"Open Device fail:user:" << GetUser()
 
  149                          << 
";peer:" << GetPeerUser()
 
  150                          << 
";channelId:" << GetChannelId()
 
  151                          << 
";label:" << m_dataChannel->label().c_str();
 
  154    dc->onClosed([
this]() {
 
  155        qInfo(m_Log) << 
"Close data channel user:" << GetUser()
 
  156                      << 
";peer:" << GetPeerUser()
 
  157                      << 
";channelId:" << GetChannelId()
 
  158                      << 
";label:" << m_dataChannel->label().c_str();
 
  162    dc->onError([
this](std::string error){
 
  163        qInfo(m_Log) << 
"Data channel error:" << error.c_str();
 
  167    dc->onMessage([dc, 
this](std::variant<rtc::binary, std::string> data) {
 
  176        rtc::binary d = std::get<rtc::binary>(data);
 
  177        m_data.append((
char*)d.data(), d.size());
 
  178        m_MutexData.unlock();
 
  179        emit this->readyRead();
 
  185int CChannelIce::CreateDataChannel(
bool bDataChannel)
 
  187    m_peerConnection = std::make_shared<rtc::PeerConnection>(m_Config);
 
  188    if(!m_peerConnection)
 
  190        qDebug(m_Log) << 
"Peer connect don't open";
 
  193    m_peerConnection->onStateChange([
this](rtc::PeerConnection::State state) {
 
  194        qDebug(m_Log) << 
"PeerConnection State:" << (int)state;
 
  196    m_peerConnection->onGatheringStateChange(
 
  197                [
this](rtc::PeerConnection::GatheringState state) {
 
  199        qDebug(m_Log) << 
"Gathering status:" << (int)state;
 
  201    m_peerConnection->onLocalDescription(
 
  202                [
this](rtc::Description description) {
 
  211        if(m_szPeerUser.isEmpty() || m_szChannelId.isEmpty())
 
  213            qCritical(m_Log) << 
"Please set peer user and channel id";
 
  216        m_pSignal->SendDescription(GetPeerUser(), GetChannelId(), description, GetUser());
 
  218    m_peerConnection->onLocalCandidate(
 
  219                [
this](rtc::Candidate candidate){
 
  228        if(m_szPeerUser.isEmpty() || m_szChannelId.isEmpty())
 
  230           qDebug(m_Log) << 
"Please set peer user and channel id";
 
  233        m_pSignal->SendCandidate(m_szPeerUser, m_szChannelId, candidate);
 
  235    m_peerConnection->onDataChannel([
this](std::shared_ptr<rtc::DataChannel> dc) {
 
  236        qInfo(m_Log) << 
"Open data channel:" << dc->label().c_str();
 
  237        if(dc->label().c_str() != GetChannelId())
 
  239            qDebug(m_Log) << 
"Channel label different:" << GetChannelId()
 
  240                           << dc->label().c_str();
 
  249        auto dc = m_peerConnection->createDataChannel(GetChannelId().toStdString());
 
  261        m_szChannelId = GenerateID(
"c_");
 
  262    int nRet = CreateDataChannel(bChannelId);
 
  263    if(nRet) 
return false;
 
 
  268                           const QString &channelId, 
const QString &type,
 
  271    m_szChannelId = channelId;
 
  272    if(!
open(toUser, fromUser, 
false))
 
  274    slotSignalReceiverDescription(fromUser, toUser, channelId, type, sdp);
 
 
  278void CChannelIce::close()
 
  280    qDebug(m_Log) << 
"CChannelIce::Close()";
 
  282    m_pSignal->disconnect(
this);
 
  286        m_dataChannel->close();
 
  287        m_dataChannel.reset();
 
  291        m_peerConnection->close();
 
  292        m_peerConnection.reset();
 
  299#define DEFAULT_MAX_MESSAGE_SIZE 0x7FFF 
  300qint64 CChannelIce::writeData(
const char *data, qint64 len)
 
  304        qDebug(m_Log) << 
"m_dataChannel is nullptr";
 
  308    if(m_dataChannel->isClosed())
 
  310        qDebug(m_Log) << 
"m_dataChannel->isClosed():peer:" << GetPeerUser()
 
  311                       << 
"channel:" << GetChannelId();
 
  317        qCritical(m_Log) << 
"The data channel isn't open";
 
  325        qWarning(m_Log) << 
"WriteData len is zero";
 
  329    while(n > DEFAULT_MAX_MESSAGE_SIZE)
 
  331        bSend = m_dataChannel->send((
const std::byte*)data, DEFAULT_MAX_MESSAGE_SIZE);
 
  334            qCritical(m_Log) << 
"Send fail. len:" << len << 
"n:" << n;
 
  337        n -= DEFAULT_MAX_MESSAGE_SIZE;
 
  341        bSend = m_dataChannel->send((
const std::byte*)data, n);
 
  342        if(bSend) 
return len;
 
  344    qCritical(m_Log) << 
"Send fail. Len:" << len << 
"n:" << n;
 
  348qint64 CChannelIce::readData(
char *data, qint64 maxlen)
 
  350    if(!m_dataChannel || m_dataChannel->isClosed() || !isOpen()) 
return -1;
 
  352    QMutexLocker lock(&m_MutexData);
 
  354    if(m_data.size() == 0)
 
  358    if(
static_cast<int>(maxlen) > m_data.size())
 
  360    memcpy(data, m_data.data(), n);
 
  361    if(m_data.size() == n)
 
  368bool CChannelIce::isSequential()
 const 
  373void CChannelIce::slotSignalConnected()
 
  377void CChannelIce::slotSignalDisconnected()
 
  382void CChannelIce::slotSignalReceiverCandidate(
const QString& fromUser,
 
  383                                                 const QString &toUser,
 
  384                                                 const QString &channelId,
 
  395    if(GetPeerUser() != fromUser || GetUser() != toUser
 
  396            || GetChannelId() != channelId) 
return;
 
  399        rtc::Candidate Candidate(sdp.toStdString(), mid.toStdString());
 
  400        m_peerConnection->addRemoteCandidate(Candidate);
 
  404void CChannelIce::slotSignalReceiverDescription(
const QString& fromUser,
 
  405                                                    const QString &toUser,
 
  406                                                    const QString &channelId,
 
  418    if(GetPeerUser() != fromUser
 
  419            || GetUser() != toUser
 
  420            || GetChannelId() != channelId)
 
  423    rtc::Description des(sdp.toStdString(), type.toStdString());
 
  425        m_peerConnection->setRemoteDescription(des);
 
  428void CChannelIce::slotSignalError(
int error, 
const QString& szError)
 
  433QString CChannelIce::GenerateID(
const QString &label)
 
  435    static qint64 
id = 0;
 
  437    QMutexLocker locker(&mutex);
 
  438    QString szId = label;
 
  439    szId += QString::number(
id++);
 
virtual bool open(const QString &user, const QString &peer, bool bChannelId)
Open channel. for activating calls.
 
int SetConfigure(const rtc::Configuration &config)
 
The channel interface class.
 
void sigConnected()
emit when the channel is connected.
 
void sigDisconnected()
emit when the channel is disconnected
 
void sigError(int nErr, const QString &szErr)
emit when the channel is error
 
The ICE signal interface class.
 
static QLoggingCategory m_Log