Skip to content
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

support audio devicechange event #631

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
16 changes: 16 additions & 0 deletions js/MediaDevices.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = MediaDevices;
* Dependencies.
*/
var EventTarget = require('./EventTarget'),
exec = require('cordova/exec'),
getUserMedia = require('./getUserMedia'),
enumerateDevices = require('./enumerateDevices');

Expand All @@ -22,11 +23,26 @@ function MediaDevices(data) {
//getUserMedia

var self = this;
var ondevicechange;

// Make this an EventTarget.
EventTarget.call(self);

data = data || {};

Object.defineProperty(this, 'ondevicechange', {
get: () => ondevicechange || null,
set: (handler) => {
ondevicechange = handler;
}
});
function onResultOK(data) {
self.dispatchEvent(new Event(data.type));
if (data.type === 'devicechange' && typeof ondevicechange === 'function') {
ondevicechange();
}
}
exec(onResultOK, null, 'iosrtcPlugin', 'MediaDevices_setListener', []);
}

MediaDevices.prototype = Object.create(EventTarget.prototype);
Expand Down
25 changes: 25 additions & 0 deletions src/PluginRTCAudioController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,29 @@ class PluginRTCAudioController {
};
}

//
// Event
//

static private var eventListener: ((_ data: NSDictionary) -> Void)? = nil

static fileprivate func deviceChanged() {
NSLog("PluginRTCAudioController#deviceChanged()")
DispatchQueue.main.async {
PluginRTCAudioController.eventListener?([
"type": "devicechange"
])
}
}

static func setListener(
_ eventListener: @escaping (_ data: NSDictionary) -> Void
) {
NSLog("PluginRTCAudioController#setListener()")

PluginRTCAudioController.eventListener = eventListener
}

//
// Audio Output
//
Expand All @@ -178,9 +201,11 @@ class PluginRTCAudioController {
switch audioRouteChangeReason {
case AVAudioSession.RouteChangeReason.newDeviceAvailable.rawValue:
NSLog("PluginRTCAudioController#audioRouteChangeListener() | headphone plugged in")
PluginRTCAudioController.deviceChanged()
case AVAudioSession.RouteChangeReason.oldDeviceUnavailable.rawValue:
NSLog("PluginRTCAudioController#audioRouteChangeListener() | headphone pulled out -> restore state speakerEnabled: %@", PluginRTCAudioController.speakerEnabled ? "true" : "false")
PluginRTCAudioController.setOutputSpeakerIfNeed(enabled: PluginRTCAudioController.speakerEnabled)
PluginRTCAudioController.deviceChanged()
default:
break
}
Expand Down
20 changes: 20 additions & 0 deletions src/iosrtcPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1131,6 +1131,26 @@ class iosrtcPlugin : CDVPlugin {
PluginRTCAudioController.selectAudioOutputSpeaker()
}

@objc(MediaDevices_setListener:) func MediaDevices_setListener(_ command: CDVInvokedUrlCommand) {
NSLog("iosrtcPlugin#MediaDevices_setListener()")

DispatchQueue.main.async {
// Set the eventListener.
PluginRTCAudioController.setListener(
{ (data: NSDictionary) -> Void in
let result = CDVPluginResult(
status: CDVCommandStatus_OK,
messageAs: data as? [AnyHashable: Any]
)

// Allow more callbacks.
result!.setKeepCallbackAs(true);
self.emit(command.callbackId, result: result!)
}
)
}
}

@objc(dump:) func dump(_ command: CDVInvokedUrlCommand) {
NSLog("iosrtcPlugin#dump()")

Expand Down
26 changes: 21 additions & 5 deletions www/cordova-plugin-iosrtc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Cordova iOS plugin exposing the full WebRTC W3C JavaScript APIs
* Copyright 2015-2017 eFace2Face, Inc. (https://eface2face.com)
* Copyright 2015-2019 BasqueVoIPMafia (https://github.com/BasqueVoIPMafia)
* Copyright 2017-2020 Cordova-RTC (https://github.com/cordova-rtc)
* Copyright 2017-2021 Cordova-RTC (https://github.com/cordova-rtc)
* License MIT
*/

Expand Down Expand Up @@ -136,6 +136,7 @@ module.exports = MediaDevices;
* Dependencies.
*/
var EventTarget = _dereq_('./EventTarget'),
exec = _dereq_('cordova/exec'),
getUserMedia = _dereq_('./getUserMedia'),
enumerateDevices = _dereq_('./enumerateDevices');

Expand All @@ -147,11 +148,24 @@ function MediaDevices(data) {
//getUserMedia

var self = this;
var ondevicechange;

// Make this an EventTarget.
EventTarget.call(self);

data = data || {};

Object.defineProperty(this, 'ondevicechange', {
get: () => ondevicechange || null,
set: (handler) => { ondevicechange = handler; }
});
function onResultOK(data) {
self.dispatchEvent(new Event(data.type));
if (data.type === 'devicechange' && typeof ondevicechange === 'function') {
ondevicechange();
}
}
exec(onResultOK, null, 'iosrtcPlugin', 'MediaDevices_setListener', []);
}

MediaDevices.prototype = Object.create(EventTarget.prototype);
Expand Down Expand Up @@ -202,7 +216,7 @@ MediaDevices.prototype.getSupportedConstraints = function () {
};
};

},{"./EventTarget":2,"./enumerateDevices":20,"./getUserMedia":21}],5:[function(_dereq_,module,exports){
},{"./EventTarget":2,"./enumerateDevices":20,"./getUserMedia":21,"cordova/exec":undefined}],5:[function(_dereq_,module,exports){
/**
* Expose the MediaStream class.
*/
Expand Down Expand Up @@ -1202,11 +1216,13 @@ Object.defineProperty(MediaStreamTrack.prototype, 'enabled', {
});

MediaStreamTrack.prototype.getConstraints = function () {
throw new Error('Not implemented.');
debug('MediaStreamTrack.prototype.getConstraints is not implemented.');
return {};
};

MediaStreamTrack.prototype.applyConstraints = function () {
throw new Error('Not implemented.');
MediaStreamTrack.prototype.applyConstraints = function (constraints) {
debug('MediaStreamTrack.prototype.applyConstraints is not implemented.', constraints);
return Promise.reject(new Error('applyConstraints is not implemented.'));
};

MediaStreamTrack.prototype.clone = function () {
Expand Down