8000 Add support for windows. (#290) · catscs/flutter-webrtc@10ae7e9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 10ae7e9

Browse files
authored
Add support for windows. (flutter-webrtc#290)
* Add support for windows. * Add .clang-format. * Fix compile error for windows. * Fix video renderer and data channel for windows. * Update README.md
1 parent e9829ff commit 10ae7e9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+5099
-11
lines changed

.clang-format

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Defines the Chromium style for automatic reformatting.
2+
# http://clang.llvm.org/docs/ClangFormatStyleOptions.html
3+
BasedOnStyle: Chromium
4+
# This defaults to 'Auto'. Explicitly set it for a while, so that
5+
# 'vector<vector<int> >' in existing files gets formatted to
6+
# 'vector<vector<int>>'. ('Auto' means that clang-format will only use
7+
# 'int>>' if the file already contains at least one such instance.)
8+
Standard: Cpp11
9+
SortIncludes: true
10+
---
11+
Language: ObjC
12+
ColumnLimit: 100

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ WebRTC plugin for Flutter Mobile/Desktop/Web
88

99
| Feature | Android | iOS | [Web](https://flutter.dev/web) | macOS | Windows | Linux | [Fuchsia](https://fuchsia.googlesource.com/) |
1010
| :-------------: | :-------------:| :-----: | :-----: | :-----: | :-----: | :-----: | :-----: |
11-
| Audio/Video | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | [WIP] | [WIP] | |
12-
| Data Channel | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | [WIP] | [WIP] | |
11+
| Audio/Video | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | [WIP] | |
12+
| Data Channel | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | [WIP] | |
1313
| Screen Capture | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | | | | |
1414
| Unified-Plan | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | | | |
1515
| Simulcast | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | | | |

example/lib/src/loopback_sample.dart

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class _MyAppState extends State<LoopBackSample> {
8989

9090
void _onAddStream(MediaStream stream) {
9191
print('New stream: ' + stream.id);
92-
//_remoteRenderer.srcObject = stream;
92+
_remoteRenderer.srcObject = stream;
9393
}
9494

9595
void _onRemoveStream(MediaStream stream) {
@@ -175,7 +175,7 @@ class _MyAppState extends State<LoopBackSample> {
175175
_localStream =
176176
await navigator.mediaDevices.getUserMedia(mediaConstraints);
177177
_localRenderer.srcObject = _localStream;
178-
178+
await _peerConnection.addStream(_localStream);
179179
/* old API
180180
await _peerConnection.addStream(_localStream);
181181
// Unified-Plan
@@ -184,13 +184,13 @@ class _MyAppState extends State<LoopBackSample> {
184184
});
185185
*/
186186
// or
187-
187+
/*
188188
await _peerConnection.addTransceiver(
189189
track: _localStream.getAudioTracks()[0],
190190
init: RTCRtpTransceiverInit(
191191
direction: TransceiverDirection.SendRecv, streams: [_localStream]),
192192
);
193-
193+
*/
194194
/*
195195
// ignore: unused_local_variable
196196
var transceiver = await _peerConnection.addTransceiver(
@@ -200,6 +200,7 @@ class _MyAppState extends State<LoopBackSample> {
200200
);
201201
*/
202202

203+
/*
203204
// Unified-Plan Simulcast
204205
await _peerConnection.addTransceiver(
205206
track: _localStream.getVideoTracks()[0],
@@ -227,7 +228,7 @@ class _MyAppState extends State<LoopBackSample> {
227228
),
228229
],
229230
));
230-
/*
231+
231232
await _peerConnection.addTransceiver(
232233
kind: RTCRtpMediaType.RTCRtpMediaTypeVideo);
233234
await _peerConnection.addTransceiver(

lib/src/native/media_stream_impl.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ class MediaStreamNative extends MediaStream {
2121

2222
void setMediaTracks(List<dynamic> audioTracks, List<dynamic> videoTracks) {
2323
_audioTracks.clear();
24-
audioTracks.forEach((track) {
24+
audioTracks?.forEach((track) {
2525
_audioTracks.add(MediaStreamTrackNative(
2626
track['id'], track['label'], track['kind'], track['enabled']));
2727
});
2828

2929
_videoTracks.clear();
30-
videoTracks.forEach((track) {
30+
videoTracks?.forEach((track) {
3131
_videoTracks.add(MediaStreamTrackNative(
3232
track['id'], track['label'], track['kind'], track['enabled']));
3333
});

lib/src/native/rtc_video_view_impl.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,14 @@ class RTCVideoView extends StatelessWidget {
5151
return SizedBox(
5252
width: constraints.maxHeight * value.aspectRatio,
5353
height: constraints.maxHeight,
54-
child: value.renderVideo ? child : Container(),
54+
child: child,
5555
);
5656
},
5757
child: Transform(
5858
transform: Matrix4.identity()..rotateY(mirror ? -pi : 0.0),
5959
alignment: FractionalOffset.center,
60-
child: videoRenderer.textureId != null
60+
child: videoRenderer.textureId != null &&
61+
videoRenderer.srcObject != null
6162
? Texture(
6263
textureId: videoRenderer.textureId,
6364
filterQuality: filterQuality,

macos/WebRTC.framework/Headers

Lines changed: 0 additions & 1 deletion
This file was deleted.

macos/WebRTC.framework/Headers

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Versions/Current/Headers

macos/WebRTC.framework/Modules

Lines changed: 0 additions & 1 deletion
This file was deleted.

macos/WebRTC.framework/Modules

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Versions/Current/Modules

macos/WebRTC.framework/Resources

Lines changed: 0 additions & 1 deletion
This file was deleted.

macos/WebRTC.framework/Resources

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Versions/Current/Resources

pubspec.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,5 @@ flutter:
2626
pluginClass: FlutterWebRTCPlugin
2727
macos:
2828
pluginClass: FlutterWebRTCPlugin
29+
windows:
30+
pluginClass: FlutterWebRTCPlugin

windows/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
flutter/
2+
3+
# Visual Studio files
4+
*.user

windows/CMakeLists.txt

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
cmake_minimum_required(VERSION 3.15)
2+
set(PROJECT_NAME "flutter_webrtc")
3+
project(${PROJECT_NAME} LANGUAGES CXX)
4+
5+
# This value is used when generating builds using this plugin, so it must
6+
# not be changed
7+
set(PLUGIN_NAME "flutter_webrtc_plugin")
8+
9+
add_definitions(-DLIB_WEBRTC_API_DLL)
10+
11+
add_library(${PLUGIN_NAME} SHARED
12+
"flutter_webrtc_plugin.cc"
13+
"src/flutter_data_channel.cc"
14+
"src/flutter_media_stream.cc"
15+
"src/flutter_peerconnection.cc"
16+
"src/flutter_video_renderer.cc"
17+
"src/flutter_webrtc.cc"
18+
"src/flutter_webrtc_base.cc"
19+
"third_party/uuidxx/uuidxx.cc"
20+
)
21+
22+
include_directories(
23+
"${CMAKE_CURRENT_SOURCE_DIR}/include"
24+
"${CMAKE_CURRENT_SOURCE_DIR}/third_party/uuidxx"
25+
"${CMAKE_CURRENT_SOURCE_DIR}/third_party/uuidxx"
26+
"${CMAKE_CURRENT_SOURCE_DIR}/third_party/libwebrtc/include"
27+
)
28+
29+
apply_standard_settings(${PLUGIN_NAME})
30+
set_target_properties(${PLUGIN_NAME} PROPERTIES
31+
CXX_VISIBILITY_PRESET hidden)
32+
target_compile_definitions(${PLUGIN_NAME} PRIVATE FLUTTER_PLUGIN_IMPL)
33+
target_include_directories(${PLUGIN_NAME} INTERFACE
34+
"${CMAKE_CURRENT_SOURCE_DIR}/include"
35+
)
36+
target_link_libraries(${PLUGIN_NAME} PRIVATE
37+
flutter
38+
flutter_wrapper_plugin
39+
"${CMAKE_CURRENT_SOURCE_DIR}/third_party/libwebrtc/lib/libwebrtc.dll.lib"
40+
)
41+
42+
# List of absolute paths to libraries that should be bundled with the plugin
43+
set(flutter_webrtc_bundled_libraries
44+
"${CMAKE_CURRENT_SOURCE_DIR}/third_party/libwebrtc/lib/libwebrtc.dll"
45+
PARENT_SCOPE
46+
)

windows/flutter_webrtc_plugin.cc

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include "flutter_webrtc/flutter_web_r_t_c_plugin.h"
2+
3+
#include <flutter/standard_message_codec.h>
4+
5+
#include "flutter_webrtc.h"
6+
7+
const char *kChannelName = "FlutterWebRTC.Method";
8+
9+
namespace flutter_webrtc_plugin {
10+
11+
// A webrtc plugin for windows/linux.
12+
class FlutterWebRTCPluginImpl : public FlutterWebRTCPlugin {
13+
public:
14+
static void RegisterWithRegistrar(flutter::PluginRegistrar *registrar) {
15+
auto channel = std::make_unique<flutter::MethodChannel<EncodableValue>>(
16+
registrar->messenger(), kChannelName,
17+
&flutter::StandardMethodCodec::GetInstance());
18+
19+
auto *channel_pointer = channel.get();
20+
21+
// Uses new instead of make_unique due to private constructor.
22+
std::unique_ptr<FlutterWebRTCPluginImpl> plugin(
23+
new FlutterWebRTCPluginImpl(registrar, std::move(channel)));
24+
25+
channel_pointer->SetMethodCallHandler(
26+
[plugin_pointer = plugin.get()](const auto &call, auto result) {
27+
plugin_pointer->HandleMethodCall(call, std::move(result));
28+
});
29+
30+
registrar->AddPlugin(std::move(plugin));
31+
}
32+
33+
virtual ~FlutterWebRTCPluginImpl() {}
34+
35+
flutter::BinaryMessenger *messenger() { return messenger_; }
36+
37+
flutter::TextureRegistrar *textures() { return textures_; }
38+
39+
private:
40+
// Creates a plugin that communicates on the given channel.
41+
FlutterWebRTCPluginImpl(
42+
flutter::PluginRegistrar *registrar,
43+
std::unique_ptr<flutter::MethodChannel<EncodableValue>> channel)
44+
: channel_(std::move(channel)),
45+
messenger_(registrar->messenger()),
46+
textures_(registrar->texture_registrar()) {
47+
webrtc_ = std::make_unique<FlutterWebRTC>(this);
48+
}
49+
50+
// Called when a method is called on |channel_|;
51+
void HandleMethodCall(
52+
const flutter::MethodCall<EncodableValue> &method_call,
53+
std::unique_ptr<flutter::MethodResult<EncodableValue>> result) {
54+
// handle method call and forward to webrtc native sdk.
55+
webrtc_->HandleMethodCall(method_call, std::move(result));
56+
}
57+
58+
private:
59+
std::unique_ptr<flutter::MethodChannel<EncodableValue>> channel_;
60+
std::unique_ptr<FlutterWebRTC> webrtc_;
61+
flutter::BinaryMessenger *messenger_;
62+
flutter::TextureRegistrar *textures_;
63+
};
64+
65+
} // namespace flutter_webrtc_plugin
66+
67+
void FlutterWebRTCPluginRegisterWithRegistrar(
68+
FlutterDesktopPluginRegistrarRef registrar) {
69+
static auto *plugin_registrar = new flutter::PluginRegistrar(registrar);
70+
flutter_webrtc_plugin::FlutterWebRTCPluginImpl::RegisterWithRegistrar(
71+
plugin_registrar);
72+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#ifndef FLUTTER_WEBRTC_RTC_DATA_CHANNEL_HXX
2+
#define FLUTTER_WEBRTC_RTC_DATA_CHANNEL_HXX
3+
4+
#include "flutter_webrtc_base.h"
5+
6+
namespace flutter_webrtc_plugin {
7+
8+
class FlutterRTCDataChannelObserver : public RTCDataChannelObserver {
9+
public:
10+
FlutterRTCDataChannelObserver(scoped_refptr<RTCDataChannel> data_channel,
11+
BinaryMessenger *messenger,
12+
const std::string &channel_name);
13+
virtual ~FlutterRTCDataChannelObserver();
14+
15+
virtual void OnStateChange(RTCDataChannelState state) override;
16+
17+
virtual void OnMessage(const char *buffer, int length, bool binary) override;
18+
19+
scoped_refptr<RTCDataChannel> data_channel() { return data_channel_; }
20+
21+
private:
22+
std::unique_ptr<EventChannel<EncodableValue>> event_channel_;
23+
std::unique_ptr<EventSink<EncodableValue>> event_sink_;
24+
scoped_refptr<RTCDataChannel> data_channel_;
25+
};
26+
27+
class FlutterDataChannel {
28+
public:
29+
FlutterDataChannel(FlutterWebRTCBase *base) : base_(base) {}
30+
31+
void CreateDataChannel(const std::string &label,
32+
const EncodableMap &dataChannelDict,
33+
RTCPeerConnection *pc,
34+
std::unique_ptr<MethodResult<EncodableValue>>);
35+
36+
void DataChannelSend(RTCDataChannel *data_channel, const std::string &type,
37+
const EncodableValue &data,
38+
std::unique_ptr<MethodResult<EncodableValue>>);
39+
40+
void DataChannelClose(RTCDataChannel *data_channel,
41+
std::unique_ptr<MethodResult<EncodableValue>>);
42+
43+
RTCDataChannel *DataChannelFormId(int id);
44+
45+
private:
46+
FlutterWebRTCBase *base_;
47+
};
48+
49+
} // namespace flutter_webrtc_plugin
50+
51+
#endif // !FLUTTER_WEBRTC_RTC_DATA_CHANNEL_HXX
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#ifndef FLUTTER_WEBRTC_RTC_GET_USERMEDIA_HXX
2+
#define FLUTTER_WEBRTC_RTC_GET_USERMEDIA_HXX
3+
4+
#include "flutter_webrtc_base.h"
5+
6+
namespace flutter_webrtc_plugin {
7+
8+
using namespace flutter;
9+
10+
class FlutterMediaStream {
11+
public:
12+
FlutterMediaStream(FlutterWebRTCBase *base) : base_(base) {}
13+
14+
void GetUserMedia(const EncodableMap &constraints,
15+
std::unique_ptr<MethodResult<EncodableValue>> result);
16+
17+
void GetUserAudio(const EncodableMap &constraints,
18+
scoped_refptr<RTCMediaStream> stream, EncodableMap &params);
19+
20+
void GetUserVideo(const EncodableMap &constraints,
21+
scoped_refptr<RTCMediaStream> stream, EncodableMap &params);
22+
23+
void GetSources(std::unique_ptr<MethodResult<EncodableValue>> result);
24+
25+
void MediaStreamGetTracks(
26+
const std::string &stream_id,
27+
std::unique_ptr<MethodResult<EncodableValue>> result);
28+
29+
void MediaStreamDispose(const std::string &stream_id,
30+
std::unique_ptr<MethodResult<EncodableValue>> result);
31+
32+
void MediaStreamTrackSetEnable(
33+
const std::string &track_id,
34+
std::unique_ptr<MethodResult<EncodableValue>> result);
35+
36+
void MediaStreamTrackSwitchCamera(
37+
const std::string &track_id,
38+
std::unique_ptr<MethodResult<EncodableValue>> result);
39+
40+
void MediaStreamTrackDispose(
41+
const std::string &track_id,
42+
std::unique_ptr<MethodResult<EncodableValue>> result);
43+
44+
private:
45+
FlutterWebRTCBase *base_;
46+
};
47+
48+
} // namespace flutter_webrtc_plugin
49+
50+
#endif // !FLUTTER_WEBRTC_RTC_GET_USERMEDIA_HXX

0 commit comments

Comments
 (0)
0