33// SPDX-License-Identifier: Apache-2.0
44
55#include " talk/owt/sdk/wasm/media_session.h"
6+ #include < emscripten/threading.h>
67#include " third_party/webrtc/api/rtc_event_log/rtc_event_log_factory.h"
78#include " third_party/webrtc/api/transport/field_trial_based_config.h"
9+ #include " third_party/webrtc/logging/rtc_event_log/fake_rtc_event_log_factory.h"
10+ #include " third_party/webrtc/rtc_base/event.h"
811#include " third_party/webrtc/rtc_base/task_queue_stdlib.h"
912
1013namespace owt {
1114namespace wasm {
1215MediaSession::MediaSession ()
13- : task_queue_factory_(webrtc::CreateTaskQueueStdlibFactory ()),
14- event_log_factory_ (std::make_unique< webrtc::RtcEventLogFactory>(
15- task_queue_factory_.get() )),
16+ : worker_thread_(rtc::Thread::Create ()),
17+ task_queue_factory_ ( webrtc::CreateTaskQueueStdlibFactory()),
18+ event_log_factory_(std::make_unique<webrtc::FakeRtcEventLogFactory>( )),
1619 event_log_(event_log_factory_->CreateRtcEventLog (
1720 webrtc::RtcEventLog::EncodingType::NewFormat)),
1821 web_transport_session_(std::make_unique<WebTransportSession>()),
@@ -22,32 +25,34 @@ MediaSession::MediaSession()
2225 receiver_process_thread_(nullptr ),
2326 video_receiver_(nullptr ),
2427 rtcp_callback_(emscripten::val::null()) {
25- rtc::ThreadManager::Instance ()->WrapCurrentThread ();
26- receiver_process_thread_ =
27- webrtc::ProcessThread::Create (" ReceiverProcessThread" );
28- webrtc::Call::Config config (event_log_.get ());
29- config.task_queue_factory = task_queue_factory_.get ();
30- webrtc::FieldTrialBasedConfig trials;
31- config.trials = &trials;
32- call_ = std::unique_ptr<webrtc::Call>(webrtc::Call::Create (config));
28+ RTC_CHECK (worker_thread_);
29+ worker_thread_->Start ();
30+ worker_thread_->Invoke <void >(RTC_FROM_HERE, [&] {
31+ receiver_process_thread_ =
32+ webrtc::ProcessThread::Create (" ReceiverProcessThread" );
33+ webrtc::Call::Config config (event_log_.get ());
34+ config.task_queue_factory = task_queue_factory_.get ();
35+ webrtc::FieldTrialBasedConfig trials;
36+ config.trials = &trials;
37+ call_ = std::unique_ptr<webrtc::Call>(webrtc::Call::Create (config));
38+ });
3339}
3440
3541std::shared_ptr<RtpVideoReceiver> MediaSession::CreateRtpVideoReceiver (
3642 uint32_t remote_ssrc) {
37- webrtc::VideoReceiveStream::Config config (this );
38- config.rtp .remote_ssrc = remote_ssrc;
39- config.rtp .local_ssrc = 1 ;
40- // Same as `kNackRtpHistoryMs` in third_party/webrtc/media/engine/webrtc_voice_engine.cc.
41- config.rtp .nack .rtp_history_ms = 5000 ;
42- // TODO: Use call_->worker_thread() when libwebrtc is rolled to a newer version.
43- webrtc::TaskQueueBase* current = webrtc::TaskQueueBase::Current ();
44- if (!current)
45- current = rtc::ThreadManager::Instance ()->CurrentThread ();
46- RTC_DCHECK (current);
47- video_receiver_ = std::make_shared<RtpVideoReceiver>(
48- current, this , &config, receive_statistics_.get (),
49- receiver_process_thread_.get ());
50- return video_receiver_;
43+ return worker_thread_->Invoke <std::shared_ptr<RtpVideoReceiver>>(
44+ RTC_FROM_HERE, [&] {
45+ webrtc::VideoReceiveStream::Config config (this );
46+ config.rtp .remote_ssrc = remote_ssrc;
47+ config.rtp .local_ssrc = 1 ;
48+ // Same as `kNackRtpHistoryMs` in
49+ // third_party/webrtc/media/engine/webrtc_voice_engine.cc.
50+ config.rtp .nack .rtp_history_ms = 5000 ;
51+ video_receiver_ = std::make_shared<RtpVideoReceiver>(
52+ worker_thread_.get (), this , &config, receive_statistics_.get (),
53+ receiver_process_thread_.get ());
54+ return video_receiver_;
55+ });
5156}
5257
5358void MediaSession::SetRtcpCallback (emscripten::val callback) {
@@ -64,10 +69,20 @@ bool MediaSession::SendRtcp(const uint8_t* packet, size_t length) {
6469 if (!rtcp_callback_) {
6570 return false ;
6671 }
67- emscripten::val buffer ( emscripten::typed_memory_view (length, packet));
68- rtcp_callback_ (buffer );
72+ emscripten_sync_run_in_main_runtime_thread (
73+ EM_FUNC_SIG_VIII, MediaSession::RunRtcpCallback, this , packet, length );
6974 return true ;
7075}
7176
77+ void MediaSession::RunRtcpCallback (MediaSession* session,
78+ const uint8_t * packet,
79+ size_t length) {
80+ if (!session->rtcp_callback_ ) {
81+ return ;
82+ }
83+ emscripten::val buffer (emscripten::typed_memory_view (length, packet));
84+ session->rtcp_callback_ (buffer);
85+ }
86+
7287} // namespace wasm
7388} // namespace owt
0 commit comments