Cordova iOS plugin exposing the full WebRTC W3C JavaScript APIs.
Yet another WebRTC SDK for iOS?
Absolutely not. This plugin exposes the WebRTC W3C API for Cordova iOS apps (you know there is no WebRTC in iOS, right?), which means no need to learn "yet another WebRTC API" and no need to use a specific service/product/provider.
Why?
Check the release announcement at the eFace2Face site.
Resources:
- NPM package.
- Public Google Group for questions and discussions about cordova-plugin-iosrtc.
- Bug Tracker for reporting issues and requesting new features (please don't use the bug tracker for questions or problems, use the Google Group instead).
Within your Cordova project:
$ cordova plugin add cordova-plugin-iosrtc
(or add it into a <plugin>
entry in the config.xml
of your app).
This plugin needs Swift support so some steps are needed to get your project working with it. These steps are explained in the Building documentation, please check it.
IMPORTANT: It seems that the incoming Swift 2.0 (included in the next OS X El Capitan) requires code changes for the plugin to compile. It will be addressed in issue #28.
The plugin exposes the cordova.plugins.iosrtc
JavaScript namespace which contains all the WebRTC classes and functions.
var pc = new cordova.plugins.iosrtc.RTCPeerConnection({
iceServers: []
});
cordova.plugins.iosrtc.getUserMedia(
// constraints
{ audio: true, video: true },
// success callback
function (stream) {
console.log('got local MediaStream: ', stream);
pc.addStream(stream);
},
// failure callback
function (error) {
console.error('getUserMedia failed: ', error);
}
);
Q: But... wait! Does it mean that there is no window.RTCPeerConnection
nor navigator.getUserMedia
?
R: A Cordova plugin is supposed to expose its JavaScript stuff in a specific namespace and, personally, I just hate those libraries that pollute the global namespace. Said that, the plugin provides a registerGlobals()
method, so you just need the following extra-code in your existing WebRTC app (assuming that cordova-plugin-device is installed):
// Just for Cordova apps.
document.addEventListener('deviceready', function () {
// Just for iOS devices.
if (window.device.platform === 'iOS') {
cordova.plugins.iosrtc.registerGlobals();
}
});
And that's all. Now you have window.RTCPeerConnection
, navigator.getUserMedia
, etc.
Q: What about <video>
elements and video.src = URL.createObjectURL(stream)
? do I need custom HTML tags or functions to display WebRTC videos?
R: No. Just use an HTML video element as usual, really. The plugin will properly place a native UIView layer on top of it by respecting its properties such as the CSS display
, opacity
, visibility
, z-index
, object-fit
and also horizontal mirror effect with -webkit-transform: scaleX(-1);
or equivalent.
Q: Can I place HTML elements (buttons and so on) on top of active <video>
elements?
R: Unfortunately not. The native UIView rendering the video stream is placed on top of the HTML view. :(
Q: What about HTML5 video events? Can I rely on video.oncanplay
?
R: I see what you mean. As there is no real video attached to the <video>
element, media events are artificially emitted by the plugin. The following events are emitted when the MediaStream
attached to a video element is ready to render video: onloadedmetadata
, onloadeddata
, oncanplay
, oncanplaythrough
. So yes, you can rely on them.
Q: Can I read <video>
properties such as readyState
, videoWidth
, etc?
Again, there is no real video attached to the <video>
element so some peroperties are artificially set by the plugin. These are readyState
, videoWidth
and videoHeight
.
Q: Do I need to call special methods to release/free native WebRTC objects? How are they garbage collected?
R: Good question. An RTCPeerConnection
is released when close()
is called on it, a MediaStream
is released when all its tracks end, and other elements are garbage collected when no longer needed. Basically the same behavior as in a WebRTC capable browser.
Q: What about Android? Why just iOS?
R: In modern versions of Android the WebView component is based on the Chromium open source project which already includes WebRTC (more info). For older versions of Android the CrossWalk project provides new WebView versions with WebRTC support as well.
Read the full documentation in the docs folder.
Check our iOSRTCApp (Google's AppRTC adapted to Cordova iOS with pure HTML5/JavaScript and cordova-plugin-iosrtc).
People and companies using cordova-plugin-iosrtc.
If you are using the plugin we would love to heard back from you!
Don't call plugin methods within WebSocket events (onopen
, onmessage
, etc). There is an issue in iOS Safari (see issue #12). Instead run a setTimeout()
within the WebSocket event if you need to call plugin methods on it.
Or better, just load the provided ios-websocket-hack.js script into your Cordova iOS app and you are done.
As explained above, there is no real media source attached to the <video>
element so some HTML5 video events and properties are artificially emitted/set by the plugin on behalf of the video element.
Methods such as play()
, pause()
are not implemented. In order to pause a video just set enabled = false
on the associated MediaStreamTrack
.
(since version 1.2.8)
- Make private properties more private (issue #34).
- Use yaeti module as
EventTarget
shim.
- Release
MediaStreamRenderer
and revert<video>
properties when the attachedMediaStream
emits "inactive" (issue #27).
- Implemented some
<video>
properties such asreadyState
,videoWidth
andvideoHeight
(issue #25). - Building simplified for both Cordova CLI and Xcode by providing a single "hook" the user must add into his Cordova application (check the Building documentation for further details).
- CSS
object-fit: none
properly implemented (don't clip the video).
- CSS object-fit property implemented in
<video>
elements (issue #23).
- Stop "error" event propagation in
<video>
element when attaching aMediaStream
to it (issue #22).
- Plugin moved to NPM registry and plugin ID renamed to cordova-plugin-iosrtc.
iosrtc.registerGlobals()
also generateswindow.webkitRTCPeerConnection
andnavigator.webkitGetUserMedia()
for backwards compatibility with WebRTC JavaScript wrappers/adapters that assume browser vendor prefixes (webkit
,moz
) in the underlying WebRTC API.
Iñaki Baz Castillo at eFace2Face, inc.
MIT :)