際際滷

際際滷Share a Scribd company logo
C++ WebRTC
(DataChannel)
/ @llamerada_jp
WebRTC Meetup Tokyo #20
SE
/ /
GitHub : llamerada-jp
facebook : ito.yuuji
twitter : @llamerada_jp
blog : http://llamerad-jp.hatenablog.com/
/
PROCESSWARP
Web /
/
www.oinari.app
libvein
Server
/
libvein
Web /
2 PubSub
KVS
Seed / Server
Documents /
https://webrtc.org/native-code/development/
&
API code reading
WebRTC ( )
?https://gist.github.com/szktty/
a47213f0077294c64ea58621c2dcfaf2
android iOS
CUI WebRTC
https://github.com/llamerada-jp/webrtc-cpp-sample
WebRTC
https://github.com/llamerada-jp/libwebrtc
Native APIs
WebRTC Native APIs ?
WebView / Electron
WebSocket
iOS / Android (C++ )
WebRTC Native APIs
API
API
WebRTC API ( )
connect

SDP

ICE

send data

disconnect

event

change status

recv data

raise error
libuv
libuv
main Thread
rtc::Runnable Thread
webrtc::

CreatePeerConnectionFactory
rtc::Runnable::Run
uv_loop
uv_async_send Event!
libvein
JavaScritp
WebAssembly
WebRTC/WebSocket API
SDP / ICE (Server /
Seed )
libvein core (C++)

WebSocket

Wrapper
WebRTC

Wrapper
Native
WebRTC
Browser

WebRTC
Browser

WebSocket
WebSocket++
C++

API
C

API
Python JavaScript
https://webrtc.org/native-code/development/
http://commondatastorage.googleapis.com/chrome-infra-docs/?at/depot_tools/
docs/html/depot_tools_tutorial.html
$ cd <workdir>

$ git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git

$ export PATH=$PATH:`pwd`/depot_tools

$ cd <workdir>

$ mkdir webrtc-checkout

$ cd webrtc-checkout

$ fetch --nohooks webrtc

$ gclient sync

$ gn gen out/Default --args=`is_debug=false¨

$ ninja -C out/Default
Chrome WebRTC
@ WebRTC

Σ( ω )
chromium URL
https://omahaproxy.appspot.com/all
OS stable
branch_commit

HASH of chromium
$ cd <workdir>

$ git clone https://chromium.googlesource.com/chromium/src chromium

$ cd chromium

$ git show <HASH of chromium>:DEPS

< >

`src/third_party/webrtc':

Var('webrtc_git') + '/src.git' + '@' + `784fccbd71c0130b32345d81459b5d0cb07ff6e5',

< >
HASH of webrtc
< gclient sync >
$ git fetch
$ git checkout <HASH of webrtc>
$ gclient sync
$ gn gen out/Default [--args='is_debug=false']
$ ninja -C out/Default [<target>]
@ Linux
static library(.a ) .o
.a
ar 1 .a
src/out/Default/obj/examples/peerconnection_client.ninja

build <target>: link <obj1> <obj2> ´ 

build ./peerconnection_client: link obj/examples/
peerconnection_client/conductor.o obj/examples/
peerconnection_client/defaults.o obj/examples/
peerconnection_client/peer_connection_client.o obj/
examples/peerconnection_client/main.o obj/examples/
peerconnection_client/main_wnd.o obj/api/video/
video_frame_i420/i420_buffer.o obj/rtc_base/checks/
checks.o obj/rtc_base/stringutils/string_to_number.o
@ macOS
obj/sdk/mac_framework_objc_shared_library/WebRTC
obj/api/video_codecs/libbuiltin_video_encoder_factory.a
obj/api/video_codecs/libbuiltin_video_decoder_factory.a
src/out/Default/obj/sdk/mac_framework_objc_shared_library.ninja
src/obj/api/video_codecs/libbuiltin_video_encoder_factory.a
src/obj/api/video_codecs/libbuiltin_video_decoder_factory.a
nm
https://github.com/llamerada-jp/search-
symbol
chromium
peerconnection_client
https://github.com/llamerada-jp/webrtc-cpp-sample
API JavaScript JavaScript
C++ API
PeerConnectionObserver
class PCO : public webrtc::PeerConnectionObserver {

void OnSignalingChange(webrtc::PeerConnectionInterface::SignalingState new_state) override;

void OnAddStream(rtc::scoped_refptr<webrtc::MediaStreamInterface> stream) override;

void OnRemoveStream(rtc::scoped_refptr<webrtc::MediaStreamInterface> stream) override;

// DataChannel !

void OnDataChannel(rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel) override;

void OnRenegotiationNeeded() override;

void OnIceConnectionChange(webrtc::PeerConnectionInterface::IceConnectionState new_state) override;

void OnIceGatheringChange(webrtc::PeerConnectionInterface::IceGatheringState new_state) override;

// ICE 

void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override;

};
DataChannelObserver
class DCO : public webrtc::DataChannelObserver {

// 

void OnStateChange() override;

// 

void OnMessage(const webrtc::DataBuffer& buffer) override;

void OnBufferedAmountChange(uint64_t previous_amount) override;

};
SessionDescriptionObserver
class CSDO : public webrtc::CreateSessionDescriptionObserver {

void OnSuccess(webrtc::SessionDescriptionInterface* desc) override;

void OnFailure(const std::string& error) override;

};

class SSDO : public webrtc::SetSessionDescriptionObserver {

void OnSuccess() override;

void OnFailure(const std::string& error) override;

};

rtc::scoped_refptr<CSDO> csdo (new rtc::RefCountedObject<CSDO>());

rtc::scoped_refptr<SSDO> ssdo (new rtc::RefCountedObject<SSDO>());
&
CreatePeerConnectionFactory
class CustomRunnable : public rtc::Runnable {

public:

void Run(rtc::Thread* subthread) override {

peer_connection_factory = webrtc::CreatePeerConnectionFactory(

nullptr /* network_thread */, nullptr /* worker_thread */,

nullptr /* signaling_thread */, nullptr /* default_adm */,

webrtc::CreateBuiltinAudioEncoderFactory(),

webrtc::CreateBuiltinAudioDecoderFactory(),

webrtc::CreateBuiltinVideoEncoderFactory(),

webrtc::CreateBuiltinVideoDecoderFactory(),

nullptr /* audio_mixer */, nullptr /* audio_processing */);

if (peer_connection_factory.get() == nullptr) {

// Error

}

subthread->Run();

}

};
rtc::PhysicalSocketServer socket_server;

thread.reset(new rtc::Thread(&socket_server));

rtc::InitializeSSL();

CustomRunnable runnable;

thread->Start(&runnable);

< peer_connection_factory >

peer_connection = peer_connection_factory->CreatePeerConnection(con?guration, nullptr, nullptr, &pco);

webrtc::DataChannelInit con?g;

// DataChannel 

data_channel = peer_connection->CreateDataChannel("data_channel", &con?g);

data_channel->RegisterObserver(&dco);

peer_connection->CreateOffer(csdo, webrtc::PeerConnectionInterface::RTCOfferAnswerOptions());
std::string message(^hello!! ̄,);

webrtc::DataBuffer buffer(

rtc::CopyOnWriteBuffer(

message.c_str(),

message.size()),

true);

data_channel->Send(buffer);
class DCO : public webrtc::DataChannelObserver {

// 

void OnMessage(const webrtc::DataBuffer& buffer) override {

std::cout << std::string(buffer.data.data<char>(), buffer.data.size()) << std::endl;

}

};
// Close 

peer_connection->Close();

peer_connection = nullptr;

data_channel = nullptr;

peer_connection_factory = nullptr;

thread->Quit();

thread.reset();

rtc::CleanupSSL();
macOS : WEBRTC_MAC=1 WEBRTC_POSIX=1
Linux : WEBRTC_LINUX=1 WEBRTC_POSIX=1
clang (libc++ )
-stdlib=libc++
C++ RTTI -fno-rtti
typeinfo dynamic_cast
--start-group --end-group
$ clang++ -I<workdir/webrtc-checkout/src> -I<workdir/webrtc-checkout/src/
third_party/abseil-cpp> -DWEBRTC_MAC=1 -DWEBRTC_POSIX=1 -std=c++11 -W -Wall -
Wno-unused-parameter -O0 -g -fno-rtti -o <output> -o <source>

(github ) build
./build.sh 

$ cd <path to build>

$ make clean

$ make VERBOSE=1
OpenSSL BoringSSL API
HTTP WebSocket
BoringSSL
WebRTC Third party src/third_party
WebRTC BSD
src/third_party
OSS WebRTC
WebRTC C++

More Related Content

C++からWebRTC (DataChannel)を旋喘する