nativescript-opentok
Version:
Integrates OpenTok for NativeScript.
187 lines • 7.33 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var observable_1 = require("tns-core-modules/data/observable");
var view_1 = require("tns-core-modules/ui/core/view");
var utils = require("tns-core-modules/utils/utils");
var TNSOTPublisher = (function (_super) {
__extends(TNSOTPublisher, _super);
function TNSOTPublisher() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this._ios = {};
return _this;
}
TNSOTPublisher.prototype.createNativeView = function () {
return UIView.new();
};
TNSOTPublisher.prototype.initNativeView = function () {
this._publisherKitDelegate = TNSPublisherKitDelegateImpl.initWithOwner(new WeakRef(this));
};
TNSOTPublisher.prototype.disposeNativeView = function () {
this._publisherKitDelegate = null;
};
TNSOTPublisher.prototype.onMeasure = function (widthMeasureSpec, heightMeasureSpec) {
var nativeView = this.nativeView;
if (nativeView) {
var width = view_1.layout.getMeasureSpecSize(widthMeasureSpec);
var height = view_1.layout.getMeasureSpecSize(heightMeasureSpec);
this.setMeasuredDimension(width, height);
}
};
TNSOTPublisher.prototype.publish = function (session, name, cameraResolution, cameraFrameRate) {
var _this = this;
this._ios = OTPublisher.alloc().initWithDelegateNameCameraResolutionCameraFrameRate(this._publisherKitDelegate, name ? name : '', this.getCameraResolution(cameraResolution), this.getCameraFrameRate(cameraFrameRate));
this._ios.view.frame = this.nativeView.bounds;
this.nativeView.addSubview(this._ios.view);
session.events.on('sessionDidConnect', function (result) {
_this._ios.publishAudio = true;
var stream = result.object;
_this.setIdleTimer(true);
try {
stream.session.publish(_this._ios);
}
catch (error) {
console.log(error);
}
});
};
TNSOTPublisher.prototype.unpublish = function (session) {
try {
if (session) {
var errorRef = new interop.Reference();
this.setIdleTimer(false);
session._ios.unpublishError(this._ios, errorRef);
if (errorRef.value) {
console.log(errorRef.value);
}
}
}
catch (error) {
console.log(error);
}
};
Object.defineProperty(TNSOTPublisher.prototype, "ios", {
get: function () {
return this._ios;
},
enumerable: true,
configurable: true
});
TNSOTPublisher.prototype.setIdleTimer = function (idleTimerDisabled) {
var app;
app = utils.ios.getter(UIApplication, UIApplication.sharedApplication);
app.idleTimerDisabled = idleTimerDisabled;
};
TNSOTPublisher.prototype.getCameraResolution = function (cameraResolution) {
if (cameraResolution) {
switch (cameraResolution) {
case 'LOW':
return OTCameraCaptureResolution.OTCameraCaptureResolutionLow;
case 'MEDIUM':
return OTCameraCaptureResolution.OTCameraCaptureResolutionMedium;
case 'HIGH':
return OTCameraCaptureResolution.OTCameraCaptureResolutionHigh;
}
}
return OTCameraCaptureResolution.OTCameraCaptureResolutionMedium;
};
TNSOTPublisher.prototype.getCameraFrameRate = function (cameraFrameRate) {
if (cameraFrameRate) {
switch (Number(cameraFrameRate)) {
case 30:
return OTCameraCaptureFrameRate.OTCameraCaptureFrameRate30FPS;
case 15:
return OTCameraCaptureFrameRate.OTCameraCaptureFrameRate15FPS;
case 7:
return OTCameraCaptureFrameRate.OTCameraCaptureFrameRate7FPS;
case 1:
return OTCameraCaptureFrameRate.OTCameraCaptureFrameRate1FPS;
}
}
return OTCameraCaptureFrameRate.OTCameraCaptureFrameRate30FPS;
};
TNSOTPublisher.prototype.cycleCamera = function () {
if (this._ios) {
if (this._ios.cameraPosition === AVCaptureDevicePositionBack) {
this._ios.cameraPosition = AVCaptureDevicePositionFront;
}
else {
this._ios.cameraPosition = AVCaptureDevicePositionBack;
}
}
};
TNSOTPublisher.prototype.toggleCamera = function () {
if (this._ios) {
this._ios.publishVideo = !this._ios.publishVideo;
}
};
TNSOTPublisher.prototype.toggleMute = function () {
if (this._ios) {
this._ios.publishAudio = !this._ios.publishAudio;
}
};
Object.defineProperty(TNSOTPublisher.prototype, "events", {
get: function () {
return this._publisherKitDelegate.events;
},
enumerable: true,
configurable: true
});
return TNSOTPublisher;
}(view_1.View));
exports.TNSOTPublisher = TNSOTPublisher;
var TNSPublisherKitDelegateImpl = (function (_super) {
__extends(TNSPublisherKitDelegateImpl, _super);
function TNSPublisherKitDelegateImpl() {
return _super !== null && _super.apply(this, arguments) || this;
}
TNSPublisherKitDelegateImpl.initWithOwner = function (owner) {
var publisherKitDelegate = new TNSPublisherKitDelegateImpl();
publisherKitDelegate._events = new observable_1.Observable();
publisherKitDelegate._owner = owner;
return publisherKitDelegate;
};
TNSPublisherKitDelegateImpl.prototype.publisherStreamCreated = function (publisher, stream) {
if (this._events) {
this._events.notify({
eventName: 'streamCreated',
object: observable_1.fromObject({
publisher: publisher,
stream: stream
})
});
}
};
TNSPublisherKitDelegateImpl.prototype.publisherStreamDestroyed = function (publisher, stream) {
if (this._events) {
this._events.notify({
eventName: 'streamDestroyed',
object: observable_1.fromObject({
publisher: publisher,
stream: stream
})
});
}
};
TNSPublisherKitDelegateImpl.prototype.publisherDidFailWithError = function (publisher, error) {
if (this._events) {
this._events.notify({
eventName: 'didFailWithError',
object: observable_1.fromObject({
publisher: publisher,
error: error
})
});
}
console.log(error);
};
Object.defineProperty(TNSPublisherKitDelegateImpl.prototype, "events", {
get: function () {
return this._events;
},
enumerable: true,
configurable: true
});
return TNSPublisherKitDelegateImpl;
}(NSObject));
TNSPublisherKitDelegateImpl.ObjCProtocols = [OTPublisherKitDelegate];
//# sourceMappingURL=publisher.js.map