Skip to content

Commit

Permalink
キャプチャラの作成を Unity スレッドで行うようにする
Browse files Browse the repository at this point in the history
  • Loading branch information
melpon committed Sep 7, 2023
1 parent 5d710be commit f9e7692
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 11 deletions.
49 changes: 39 additions & 10 deletions src/sora.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -541,11 +541,10 @@ void Sora::Disconnect() {

void Sora::SwitchCamera(const sora_conf::internal::CameraConfig& cc) {
RTC_LOG(LS_INFO) << "SwitchCamera: " << jsonif::to_json(cc);
boost::asio::post(*ioc_, [self = shared_from_this(), cc = cc]() {
self->DoSwitchCamera(cc);
});
}
void Sora::DoSwitchCamera(const sora_conf::internal::CameraConfig& cc) {

// このあたりのキャプチャラ作成は、IO スレッドではなく Unity スレッドで行う。
// (DoConnect で作成したキャプチャラは Unity スレッド上で実行されてるので、
// 全て同じスレッドでやる必要がある)
std::function<void(const webrtc::VideoFrame& frame)> on_frame;
if (on_capturer_frame_) {
on_frame = [on_frame =
Expand Down Expand Up @@ -582,10 +581,40 @@ void Sora::DoSwitchCamera(const sora_conf::internal::CameraConfig& cc) {

std::string video_track_id = rtc::CreateRandomString(16);
auto video_track = factory_->CreateVideoTrack(video_track_id, capturer.get());
renderer_->ReplaceTrack(video_track_.get(), video_track.get());
video_sender_->SetTrack(video_track.get());

// video_track_ をこのスレッド(Unity スレッド)で設定したいので、
// IO スレッドの実行完了を待つ
std::promise<void> p;
std::future<void> f = p.get_future();
boost::asio::post(*ioc_,
[self = shared_from_this(), cc = cc, video_track, &p]() {
self->DoSwitchCamera(cc, video_track);
p.set_value();
});
f.wait();

video_track_ = video_track;
}
void Sora::DoSwitchCamera(
const sora_conf::internal::CameraConfig& cc,
rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track) {
if (video_track_ == nullptr) {
webrtc::RTCErrorOr<rtc::scoped_refptr<webrtc::RtpSenderInterface>>
video_result = signaling_->GetPeerConnection()->AddTrack(video_track,
{stream_id_});
video_sender_ = video_result.value();

auto track_id = renderer_->AddTrack(video_track.get());
PushEvent([this, track_id]() {
if (on_add_track_) {
on_add_track_(track_id, "");
}
});
} else {
renderer_->ReplaceTrack(video_track_.get(), video_track.get());
}
video_sender_->SetTrack(video_track.get());
}

void Sora::RenderCallbackStatic(int event_id) {
auto sora = (Sora*)IdPointer::Instance().Lookup(event_id);
Expand Down Expand Up @@ -838,16 +867,16 @@ void Sora::OnSetOffer(std::string offer) {
on_set_offer_(offer);
}
});
std::string stream_id = rtc::CreateRandomString(16);
stream_id_ = rtc::CreateRandomString(16);
if (audio_track_ != nullptr) {
webrtc::RTCErrorOr<rtc::scoped_refptr<webrtc::RtpSenderInterface>>
audio_result = signaling_->GetPeerConnection()->AddTrack(audio_track_,
{stream_id});
{stream_id_});
}
if (video_track_ != nullptr) {
webrtc::RTCErrorOr<rtc::scoped_refptr<webrtc::RtpSenderInterface>>
video_result = signaling_->GetPeerConnection()->AddTrack(video_track_,
{stream_id});
{stream_id_});
video_sender_ = video_result.value();
}

Expand Down
6 changes: 5 additions & 1 deletion src/sora.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ class Sora : public std::enable_shared_from_this<Sora>,
private:
void DoConnect(const sora_conf::internal::ConnectConfig& config,
std::function<void(int, std::string)> on_disconnect);
void DoSwitchCamera(const sora_conf::internal::CameraConfig& cc);
void DoSwitchCamera(
const sora_conf::internal::CameraConfig& cc,
rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track);

static rtc::scoped_refptr<UnityAudioDevice> CreateADM(
webrtc::TaskQueueFactory* task_queue_factory,
Expand Down Expand Up @@ -168,6 +170,8 @@ class Sora : public std::enable_shared_from_this<Sora>,

std::map<ptrid_t, std::string> connection_ids_;

std::string stream_id_;

rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> capturer_;
int capturer_type_ = 0;
std::shared_ptr<CapturerSink> capturer_sink_;
Expand Down

0 comments on commit f9e7692

Please sign in to comment.