Skip to content

Commit

Permalink
うごいた
Browse files Browse the repository at this point in the history
  • Loading branch information
tnoho committed Jan 2, 2025
1 parent af03656 commit ede26ea
Show file tree
Hide file tree
Showing 17 changed files with 137 additions and 164 deletions.
10 changes: 4 additions & 6 deletions src/sora.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ Sora::Sora(std::optional<bool> use_hardware_encoder,
factory_.reset(new SoraFactory(use_hardware_encoder, openh264));
}

Sora::~Sora() {
Disposed();
}
Sora::~Sora() {}

std::shared_ptr<SoraConnection> Sora::CreateConnection(
const nb::handle& signaling_urls,
Expand Down Expand Up @@ -60,7 +58,7 @@ std::shared_ptr<SoraConnection> Sora::CreateConnection(
std::optional<std::string> proxy_username,
std::optional<std::string> proxy_password,
std::optional<std::string> proxy_agent) {
std::shared_ptr<SoraConnection> conn = std::make_shared<SoraConnection>(this);
std::shared_ptr<SoraConnection> conn = std::make_shared<SoraConnection>();
sora::SoraSignalingConfig config;
config.pc_factory = factory_->GetPeerConnectionFactory();
config.observer = conn;
Expand Down Expand Up @@ -220,7 +218,7 @@ SoraAudioSource* Sora::CreateAudioSource(size_t channels, int sample_rate) {
auto track = factory_->GetPeerConnectionFactory()->CreateAudioTrack(
track_id, source.get());
SoraAudioSource* audio_source =
new SoraAudioSource(this, source, track, channels, sample_rate);
new SoraAudioSource(source, track, channels, sample_rate);
return audio_source;
}

Expand All @@ -232,7 +230,7 @@ SoraVideoSource* Sora::CreateVideoSource() {
auto track =
factory_->GetPeerConnectionFactory()->CreateVideoTrack(source, track_id);

SoraVideoSource* video_source = new SoraVideoSource(this, source, track);
SoraVideoSource* video_source = new SoraVideoSource(source, track);
return video_source;
}

Expand Down
2 changes: 1 addition & 1 deletion src/sora.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* 同じ Sora インスタンス内でしか Connection や AudioSource、VideoSource を共有できないので、
* 複数の Sora インスタンスを生成することは不具合の原因になります。
*/
class Sora : public DisposePublisher {
class Sora {
public:
/**
* このタイミングで SoraFactory の生成まで行うため SoraFactory の生成にあたって必要な引数はここで設定します。
Expand Down
12 changes: 0 additions & 12 deletions src/sora_audio_sink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,12 @@ SoraAudioSinkImpl::SoraAudioSinkImpl(SoraTrackInterface* track,
sample_rate_(0),
number_of_channels_(0) {
audio_frame_ = std::make_unique<webrtc::AudioFrame>();
track_->AddSubscriber(this);
webrtc::AudioTrackInterface* audio_track =
static_cast<webrtc::AudioTrackInterface*>(track_->GetTrack().get());
audio_track->AddSink(this);
}

SoraAudioSinkImpl::~SoraAudioSinkImpl() {
Del();
}

void SoraAudioSinkImpl::Del() {
if (track_) {
track_->RemoveSubscriber(this);
}
Disposed();
}

Expand All @@ -43,10 +35,6 @@ void SoraAudioSinkImpl::Disposed() {
track_ = nullptr;
}

void SoraAudioSinkImpl::PublisherDisposed() {
Disposed();
}

void SoraAudioSinkImpl::OnData(
const void* audio_data,
int bits_per_sample,
Expand Down
5 changes: 1 addition & 4 deletions src/sora_audio_sink.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ namespace nb = nanobind;
* 実装上の留意点:Track の参照保持のための Impl のない SoraAudioSink を __init__.py に定義しています。
* SoraAudioSinkImpl を直接 Python から呼び出すことは想定していません。
*/
class SoraAudioSinkImpl : public webrtc::AudioTrackSinkInterface,
public DisposeSubscriber {
class SoraAudioSinkImpl : public webrtc::AudioTrackSinkInterface {
public:
/**
* @param track 音声を取り出す OnTrack コールバックから渡されるリモート Track
Expand All @@ -41,9 +40,7 @@ class SoraAudioSinkImpl : public webrtc::AudioTrackSinkInterface,
size_t output_channels);
~SoraAudioSinkImpl();

void Del();
void Disposed();
void PublisherDisposed() override;
// webrtc::AudioTrackSinkInterface
void OnData(const void* audio_data,
int bits_per_sample,
Expand Down
5 changes: 1 addition & 4 deletions src/sora_audio_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,11 @@ void SoraAudioSourceInterface::Add10MsData(const int16_t* data,
}

SoraAudioSource::SoraAudioSource(
DisposePublisher* publisher,
rtc::scoped_refptr<SoraAudioSourceInterface> source,
rtc::scoped_refptr<webrtc::MediaStreamTrackInterface> track,
size_t channels,
int sample_rate)
: SoraTrackInterface(publisher, track), source_(source) {
publisher_->AddSubscriber(this);
}
: SoraTrackInterface(track), source_(source) {}

void SoraAudioSource::OnData(const int16_t* data,
size_t samples_per_channel,
Expand Down
3 changes: 1 addition & 2 deletions src/sora_audio_source.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ class SoraAudioSourceInterface
*/
class SoraAudioSource : public SoraTrackInterface {
public:
SoraAudioSource(DisposePublisher* publisher,
rtc::scoped_refptr<SoraAudioSourceInterface> source,
SoraAudioSource(rtc::scoped_refptr<SoraAudioSourceInterface> source,
rtc::scoped_refptr<webrtc::MediaStreamTrackInterface> track,
size_t channels,
int sample_rate);
Expand Down
12 changes: 0 additions & 12 deletions src/sora_audio_stream_sink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,20 +143,12 @@ SoraAudioStreamSinkImpl::SoraAudioStreamSinkImpl(SoraTrackInterface* track,
: track_(track),
output_sample_rate_(output_sample_rate),
output_channels_(output_channels) {
track_->AddSubscriber(this);
webrtc::AudioTrackInterface* audio_track =
static_cast<webrtc::AudioTrackInterface*>(track_->GetTrack().get());
audio_track->AddSink(this);
}

SoraAudioStreamSinkImpl::~SoraAudioStreamSinkImpl() {
Del();
}

void SoraAudioStreamSinkImpl::Del() {
if (track_) {
track_->RemoveSubscriber(this);
}
Disposed();
}

Expand All @@ -169,10 +161,6 @@ void SoraAudioStreamSinkImpl::Disposed() {
track_ = nullptr;
}

void SoraAudioStreamSinkImpl::PublisherDisposed() {
Disposed();
}

void SoraAudioStreamSinkImpl::OnData(
const void* audio_data,
int bits_per_sample,
Expand Down
8 changes: 3 additions & 5 deletions src/sora_audio_stream_sink.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,17 +163,13 @@ class SoraAudioFrame {
* 実装上の留意点:Track の参照保持のための Impl のない SoraAudioStreamSink を __init__.py に定義しています。
* SoraAudioStreamSinkImpl を直接 Python から呼び出すことは想定していません。
*/
class SoraAudioStreamSinkImpl : public webrtc::AudioTrackSinkInterface,
public DisposeSubscriber {
class SoraAudioStreamSinkImpl : public webrtc::AudioTrackSinkInterface {
public:
SoraAudioStreamSinkImpl(SoraTrackInterface* track,
int output_sample_rate,
size_t output_channels);
~SoraAudioStreamSinkImpl();

void Del();
void Disposed();
void PublisherDisposed() override;
// webrtc::AudioTrackSinkInterface
void OnData(const void* audio_data,
int bits_per_sample,
Expand All @@ -192,6 +188,8 @@ class SoraAudioStreamSinkImpl : public webrtc::AudioTrackSinkInterface,
std::function<void(std::shared_ptr<SoraAudioFrame>)> on_frame_;

private:
void Disposed();

SoraTrackInterface* track_;
const int output_sample_rate_;
const size_t output_channels_;
Expand Down
33 changes: 3 additions & 30 deletions src/sora_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,9 @@

namespace nb = nanobind;

SoraConnection::SoraConnection(DisposePublisher* publisher)
: publisher_(publisher) {
publisher_->AddSubscriber(this);
}

SoraConnection::~SoraConnection() {
if (publisher_) {
publisher_->RemoveSubscriber(this);
}
Disposed();
}
SoraConnection::SoraConnection() {}

void SoraConnection::Disposed() {
DisposePublisher::Disposed();
Disconnect();
publisher_ = nullptr;
}

void SoraConnection::PublisherDisposed() {
Disposed();
}
SoraConnection::~SoraConnection() {}

void SoraConnection::Init(sora::SoraSignalingConfig& config) {
// TODO(tnoho): 複数回の呼び出しは禁止なので、ちゃんと throw する
Expand Down Expand Up @@ -91,10 +73,6 @@ void SoraConnection::SetAudioTrack(SoraTrackInterface* audio_source) {
if (audio_sender_) {
audio_sender_->SetTrack(audio_source->GetTrack().get());
}
if (audio_source_) {
audio_source_->RemoveSubscriber(this);
}
audio_source->AddSubscriber(this);
audio_source_ = audio_source;
}

Expand All @@ -103,10 +81,6 @@ void SoraConnection::SetVideoTrack(SoraTrackInterface* video_source) {
if (video_sender_) {
video_sender_->SetTrack(video_source->GetTrack().get());
}
if (video_source_) {
video_source_->RemoveSubscriber(this);
}
video_source->AddSubscriber(this);
video_source_ = video_source;
}

Expand Down Expand Up @@ -243,8 +217,7 @@ void SoraConnection::OnTrack(
if (on_track_) {
auto receiver = transceiver->receiver();
// shared_ptr になってないとリークする
auto track = std::make_shared<SoraMediaTrack>(this, receiver);
AddSubscriber(track.get());
auto track = std::make_shared<SoraMediaTrack>(receiver);
call_python(on_track_, track);
}
}
Expand Down
10 changes: 2 additions & 8 deletions src/sora_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,14 @@ namespace nb = nanobind;
*
* Python に Connection を制御する関数を提供します。
*/
class SoraConnection : public sora::SoraSignalingObserver,
public DisposePublisher,
public DisposeSubscriber {
class SoraConnection : public sora::SoraSignalingObserver {
public:
/**
* コンストラクタではインスタンスの生成のみで実際の生成処理は Init 関数で行います。
*/
SoraConnection(DisposePublisher* publisher);
SoraConnection();
~SoraConnection();

void Disposed() override;
void PublisherDisposed() override;

/**
* SoraConnection の初期化を行う関数です。
*
Expand Down Expand Up @@ -146,7 +141,6 @@ class SoraConnection : public sora::SoraSignalingObserver,
std::function<void(std::string)> on_data_channel_;

private:
DisposePublisher* publisher_;
std::unique_ptr<boost::asio::io_context> ioc_;
std::shared_ptr<sora::SoraSignaling> conn_;
std::unique_ptr<std::thread> thread_;
Expand Down
Loading

0 comments on commit ede26ea

Please sign in to comment.