From 6239264d5b162b641640fbda88ce8d65523dc2fb Mon Sep 17 00:00:00 2001 From: Jerry Hu Date: Thu, 14 Jan 2021 12:23:48 -0500 Subject: [PATCH 1/2] support audio devicechange event --- js/MediaDevices.js | 14 ++++++++++++++ src/PluginRTCAudioController.swift | 25 +++++++++++++++++++++++++ src/iosrtcPlugin.swift | 20 ++++++++++++++++++++ www/cordova-plugin-iosrtc.js | 26 +++++++++++++++++++++----- 4 files changed, 80 insertions(+), 5 deletions(-) diff --git a/js/MediaDevices.js b/js/MediaDevices.js index 1af4e3d1..f65db7e8 100644 --- a/js/MediaDevices.js +++ b/js/MediaDevices.js @@ -11,6 +11,7 @@ module.exports = MediaDevices; * Dependencies. */ var EventTarget = require('./EventTarget'), + exec = require('cordova/exec'), getUserMedia = require('./getUserMedia'), enumerateDevices = require('./enumerateDevices'); @@ -22,11 +23,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); diff --git a/src/PluginRTCAudioController.swift b/src/PluginRTCAudioController.swift index d39e0ef2..f119c810 100644 --- a/src/PluginRTCAudioController.swift +++ b/src/PluginRTCAudioController.swift @@ -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 // @@ -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 } diff --git a/src/iosrtcPlugin.swift b/src/iosrtcPlugin.swift index 5270f409..06216edd 100644 --- a/src/iosrtcPlugin.swift +++ b/src/iosrtcPlugin.swift @@ -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()") diff --git a/www/cordova-plugin-iosrtc.js b/www/cordova-plugin-iosrtc.js index 816a152e..a5f972cf 100644 --- a/www/cordova-plugin-iosrtc.js +++ b/www/cordova-plugin-iosrtc.js @@ -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 */ @@ -136,6 +136,7 @@ module.exports = MediaDevices; * Dependencies. */ var EventTarget = _dereq_('./EventTarget'), + exec = _dereq_('cordova/exec'), getUserMedia = _dereq_('./getUserMedia'), enumerateDevices = _dereq_('./enumerateDevices'); @@ -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); @@ -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. */ @@ -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 () { From 8862ae4fcc264507636b168cc2c4d81860420f75 Mon Sep 17 00:00:00 2001 From: Jerry Hu Date: Thu, 14 Jan 2021 13:55:55 -0500 Subject: [PATCH 2/2] lint --- js/MediaDevices.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/js/MediaDevices.js b/js/MediaDevices.js index f65db7e8..fd82a4ee 100644 --- a/js/MediaDevices.js +++ b/js/MediaDevices.js @@ -32,7 +32,9 @@ function MediaDevices(data) { Object.defineProperty(this, 'ondevicechange', { get: () => ondevicechange || null, - set: (handler) => { ondevicechange = handler; } + set: (handler) => { + ondevicechange = handler; + } }); function onResultOK(data) { self.dispatchEvent(new Event(data.type));