Skip to content

fix: cloneTrack on windows and linux #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions common/cpp/include/flutter_media_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ class FlutterMediaStream {
void MediaStreamTrackDispose(const std::string& track_id,
std::unique_ptr<MethodResultProxy> result);

void MediaStreamTrackClone(const std::string& track_id,
std::unique_ptr<MethodResultProxy> result);

void CreateLocalMediaStream(std::unique_ptr<MethodResultProxy> result);

void OnDeviceChange();
Expand Down
2 changes: 2 additions & 0 deletions common/cpp/include/flutter_webrtc_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ class FlutterWebRTCBase {
data_channel_observers_;
std::map<std::string, std::shared_ptr<FlutterPeerConnectionObserver>>
peerconnection_observers_;
std::map<std::string, scoped_refptr<RTCVideoSource>> video_sources_;
std::map<std::string, scoped_refptr<RTCAudioSource>> audio_sources_;
mutable std::mutex mutex_;

void lock() { mutex_.lock(); }
Expand Down
55 changes: 55 additions & 0 deletions common/cpp/src/flutter_media_stream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ void FlutterMediaStream::GetUserAudio(const EncodableMap& constraints,
params[EncodableValue("audioTracks")] = EncodableValue(audioTracks);
stream->AddTrack(track);

base_->audio_sources_[track->id().std_string()] = source;
base_->local_tracks_[track->id().std_string()] = track;
}
}
Expand Down Expand Up @@ -319,6 +320,7 @@ void FlutterMediaStream::GetUserVideo(const EncodableMap& constraints,

stream->AddTrack(track);

base_->video_sources_[track->id().std_string()] = source;
base_->local_tracks_[track->id().std_string()] = track;
base_->video_capturers_[track->id().std_string()] = video_capturer;
}
Expand Down Expand Up @@ -483,12 +485,14 @@ void FlutterMediaStream::MediaStreamDispose(
for (auto track : audio_tracks.std_vector()) {
stream->RemoveTrack(track);
base_->local_tracks_.erase(track->id().std_string());
base_->audio_sources_.erase(track->id().std_string());
}

vector<scoped_refptr<RTCVideoTrack>> video_tracks = stream->video_tracks();
for (auto track : video_tracks.std_vector()) {
stream->RemoveTrack(track);
base_->local_tracks_.erase(track->id().std_string());
base_->video_sources_.erase(track->id().std_string());
if (base_->video_capturers_.find(track->id().std_string()) !=
base_->video_capturers_.end()) {
auto video_capture = base_->video_capturers_[track->id().std_string()];
Expand Down Expand Up @@ -558,4 +562,55 @@ void FlutterMediaStream::MediaStreamTrackDispose(
base_->RemoveMediaTrackForId(track_id);
result->Success();
}

void FlutterMediaStream::MediaStreamTrackClone(
const std::string& track_id,
std::unique_ptr<MethodResultProxy> result) {

std::string new_track_id = base_->GenerateUUID();

EncodableMap track_params;

for (auto it : base_->local_streams_) {
auto stream = it.second;
auto audio_tracks = stream->audio_tracks();
for (auto original_audio_track : audio_tracks.std_vector()) {
if (original_audio_track->id().std_string() == track_id) {
// clone audio track
auto audio_source = base_->audio_sources_[track_id];
scoped_refptr<RTCAudioTrack> track =
base_->factory_->CreateAudioTrack(audio_source, new_track_id);
base_->audio_sources_[track->id().std_string()] = audio_source;

track_params[EncodableValue("readyState")] = "live";
track_params[EncodableValue("kind")] =
EncodableValue(track->kind().std_string());
track_params[EncodableValue("enabled")] =
EncodableValue(track->enabled());
}
}
auto video_tracks = stream->video_tracks();
for (auto original_video_track : video_tracks.std_vector()) {
if (original_video_track->id().std_string() == track_id) {
// clone video track
auto video_source = base_->video_sources_[track_id];
//TODO copy surface texture
scoped_refptr<RTCVideoTrack> track =
base_->factory_->CreateVideoTrack(video_source, new_track_id);
base_->video_sources_[track->id().std_string()] = video_source;

track_params[EncodableValue("readyState")] = "live";
track_params[EncodableValue("kind")] =
EncodableValue(track->kind().std_string());
track_params[EncodableValue("enabled")] =
EncodableValue(track->enabled());
}
}
}
track_params[EncodableValue("id")] = EncodableValue(new_track_id);
track_params[EncodableValue("label")] = EncodableValue(new_track_id);
track_params[EncodableValue("remote")] = EncodableValue(false);

result->Success(EncodableValue(track_params));
}
} // namespace stream_webrtc_flutter_plugin
9 changes: 9 additions & 0 deletions common/cpp/src/flutter_webrtc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,15 @@ void FlutterWebRTC::HandleMethodCall(
GetValue<EncodableMap>(*method_call.arguments());
const std::string track_id = findString(params, "trackId");
MediaStreamTrackDispose(track_id, std::move(result));
} else if (method_call.method_name().compare("trackClone") == 0) {
if (!method_call.arguments()) {
result->Error("Bad Arguments", "Null constraints arguments received");
return;
}
const EncodableMap params =
GetValue<EncodableMap>(*method_call.arguments());
const std::string track_id = findString(params, "trackId");
MediaStreamTrackClone(track_id, std::move(result));
} else if (method_call.method_name().compare("restartIce") == 0) {
if (!method_call.arguments()) {
result->Error("Bad Arguments", "Null constraints arguments received");
Expand Down
2 changes: 1 addition & 1 deletion windows/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ target_link_libraries(${PLUGIN_NAME} PRIVATE
)

# List of absolute paths to libraries that should be bundled with the plugin
set(flutter_webrtc_bundled_libraries
set(stream_webrtc_flutter_bundled_libraries
"${CMAKE_CURRENT_SOURCE_DIR}/../third_party/libwebrtc/lib/win64/libwebrtc.dll"
PARENT_SCOPE
)