nativescript-opentok
Version:
Integrates OpenTok for NativeScript.
249 lines • 9.7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var observable_1 = require("tns-core-modules/data/observable");
var utils = require("tns-core-modules/utils/utils");
var Session = com.opentok.android.Session;
var Subscriber = com.opentok.android.Subscriber;
var Publisher = com.opentok.android.Publisher;
var BaseVideoRenderer = com.opentok.android.BaseVideoRenderer;
var AbsoluteLayout = android.widget.AbsoluteLayout;
var RelativeLayout = android.widget.RelativeLayout;
var SessionListener = com.opentok.android.Session.SessionListener;
var SignalListener = com.opentok.android.Session.SignalListener;
var ReconnectionListener = com.opentok.android.Session.ReconnectionListener;
var ConnectionListener = com.opentok.android.Session.ConnectionListener;
var ArchiveListener = com.opentok.android.Session.ArchiveListener;
var MARSHMALLOW = 23;
var currentapiVersion = android.os.Build.VERSION.SDK_INT;
var permissions = require("nativescript-permissions");
var TNSOTSession = (function () {
function TNSOTSession() {
}
TNSOTSession.initWithApiKeySessionId = function (apiKey, sessionId) {
var tnsSession = new TNSOTSession();
tnsSession._sessionEvents = new observable_1.Observable();
tnsSession.apiKey = apiKey;
tnsSession.session = new Session(utils.ad.getApplicationContext(), apiKey, sessionId);
tnsSession.session.setSessionListener(new SessionListener({
onConnected: function (session) {
if (tnsSession._sessionEvents) {
tnsSession._sessionEvents.notify({
eventName: 'sessionDidConnect',
object: observable_1.fromObject({
session: session
})
});
}
},
onDisconnected: function (session) {
if (tnsSession._sessionEvents) {
tnsSession._sessionEvents.notify({
eventName: 'sessionDidDisconnect',
object: observable_1.fromObject({
session: session
})
});
}
},
onError: function (session, error) {
if (tnsSession._sessionEvents) {
tnsSession._sessionEvents.notify({
eventName: 'didFailWithError',
object: observable_1.fromObject({
session: session,
error: error
})
});
}
},
onStreamDropped: function (session, stream) {
if (tnsSession._sessionEvents) {
tnsSession._sessionEvents.notify({
eventName: 'streamDropped',
object: observable_1.fromObject({
session: session,
stream: stream
})
});
}
},
onStreamReceived: function (session, stream) {
if (tnsSession._sessionEvents) {
tnsSession._sessionEvents.notify({
eventName: 'streamReceived',
object: observable_1.fromObject({
session: session,
stream: stream
})
});
}
if (tnsSession.subscriber) {
tnsSession.subscriber.subscribe(session, stream);
}
}
}));
tnsSession.session.setSignalListener(new SignalListener({
onSignalReceived: function (session, type, data, connection) {
if (tnsSession._sessionEvents) {
tnsSession._sessionEvents.notify({
eventName: 'signalReceived',
object: observable_1.fromObject({
session: session,
type: type,
data: data,
connection: connection
})
});
}
}
}));
tnsSession.session.setArchiveListener(new ArchiveListener({
onArchiveStarted: function (session, id, name) {
if (tnsSession._sessionEvents) {
tnsSession._sessionEvents.notify({
eventName: 'archiveStartedWithId',
object: observable_1.fromObject({
session: session,
archiveId: id,
name: name
})
});
}
}, onArchiveStopped: function (session, id) {
if (tnsSession._sessionEvents) {
tnsSession._sessionEvents.notify({
eventName: 'archiveStoppedWithId',
object: observable_1.fromObject({
session: session,
archiveId: id
})
});
}
}
}));
tnsSession.session.setConnectionListener(new ConnectionListener({
onConnectionCreated: function (session, connection) {
if (tnsSession._sessionEvents) {
tnsSession._sessionEvents.notify({
eventName: 'connectionCreated',
object: observable_1.fromObject({
session: session,
connection: connection
})
});
}
},
onConnectionDestroyed: function (session, connection) {
if (tnsSession._sessionEvents) {
tnsSession._sessionEvents.notify({
eventName: 'connectionDestroyed',
object: observable_1.fromObject({
session: session,
connection: connection
})
});
}
}
}));
tnsSession.session.setReconnectionListener(new ReconnectionListener({
onReconnected: function (session) {
if (tnsSession._sessionEvents) {
tnsSession._sessionEvents.notify({
eventName: 'sessionDidReconnect',
object: observable_1.fromObject({
session: session
})
});
}
}, onReconnecting: function (session) {
if (tnsSession._sessionEvents) {
tnsSession._sessionEvents.notify({
eventName: 'sessionDidBeginReconnecting',
object: observable_1.fromObject({
session: session
})
});
}
}
}));
return tnsSession;
};
TNSOTSession.requestPermission = function () {
if (currentapiVersion >= MARSHMALLOW) {
var perms = [android.Manifest.permission.CAMERA, android.Manifest.permission.RECORD_AUDIO];
return permissions.requestPermission(perms);
}
};
TNSOTSession.prototype.connect = function (token) {
var _this = this;
return new Promise(function (resolve, reject) {
var session = _this.session;
if (session) {
try {
session.connect(token);
resolve(true);
}
catch (err) {
reject(err);
}
}
});
};
TNSOTSession.prototype.disconnect = function () {
var _this = this;
return new Promise(function (resolve, reject) {
try {
_this.session.disconnect();
resolve();
}
catch (err) {
reject(err);
}
});
};
TNSOTSession.prototype.sendSignal = function (type, message) {
var _this = this;
return new Promise(function (resolve, reject) {
var session = _this.session;
if (session) {
try {
session.sendSignal(type, message);
resolve(true);
}
catch (err) {
reject(err);
}
}
});
};
TNSOTSession.prototype.subscribe = function (subInstance) {
this.session.subscribe(subInstance);
};
Object.defineProperty(TNSOTSession.prototype, "sessionEvents", {
get: function () {
return this._sessionEvents;
},
enumerable: true,
configurable: true
});
Object.defineProperty(TNSOTSession.prototype, "events", {
get: function () {
return this._sessionEvents;
},
enumerable: true,
configurable: true
});
Object.defineProperty(TNSOTSession.prototype, "subscriber", {
get: function () {
return this._subscriber;
},
set: function (subscriber) {
this._subscriber = subscriber;
},
enumerable: true,
configurable: true
});
return TNSOTSession;
}());
exports.TNSOTSession = TNSOTSession;
//# sourceMappingURL=session.js.map