You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JMCallKitEmitter.swift 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright @ 2018-present Atlassian Pty Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import AVKit
  17. import CallKit
  18. import Foundation
  19. internal final class JMCallKitEmitter: NSObject, CXProviderDelegate {
  20. private var listeners = Set<JMCallKitEventListenerWrapper>()
  21. private var pendingMuteActions = Set<UUID>()
  22. internal override init() {}
  23. // MARK: - Add/remove listeners
  24. func addListener(_ listener: JMCallKitListener) {
  25. let wrapper = JMCallKitEventListenerWrapper(listener: listener)
  26. listeners.insert(wrapper)
  27. }
  28. func removeListener(_ listener: JMCallKitListener) {
  29. // XXX Constructing a new JMCallKitEventListenerWrapper instance in
  30. // order to remove the specified listener from listeners is (1) a bit
  31. // funny (though may make a statement about performance) and (2) not
  32. // really an option because the specified listener may already be
  33. // executing its dealloc (like RNCallKit).
  34. listeners.forEach {
  35. // 1. JMCallKitEventListenerWrapper weakly references
  36. // JMCallKitListener so it may be nice to clean
  37. // JMCallKitEventListenerWrapperinstances up if they've lost
  38. // their associated JMCallKitListener instances (e.g. for
  39. // example, because whoever did addListener forgot to
  40. // removeListener). Unfortunately, I don't know how to do it
  41. // because JMCallKitEventListenerWrapper is a struct.
  42. //
  43. // 2. XXX JMCallKitEventListenerWrapper implements the weird
  44. // equality by JMCallKitListener hash which (1) I don't
  45. // understand and (2) I don't know how to invoke without
  46. // duplicating.
  47. if ($0.hashValue == listener.hash) {
  48. listeners.remove($0)
  49. }
  50. }
  51. }
  52. // MARK: - Add mute action
  53. func addMuteAction(_ actionUUID: UUID) {
  54. pendingMuteActions.insert(actionUUID)
  55. }
  56. // MARK: - CXProviderDelegate
  57. func providerDidReset(_ provider: CXProvider) {
  58. listeners.forEach { $0.listener?.providerDidReset?() }
  59. pendingMuteActions.removeAll()
  60. }
  61. func provider(_ provider: CXProvider, perform action: CXAnswerCallAction) {
  62. listeners.forEach {
  63. $0.listener?.performAnswerCall?(UUID: action.callUUID)
  64. }
  65. action.fulfill()
  66. }
  67. func provider(_ provider: CXProvider, perform action: CXEndCallAction) {
  68. listeners.forEach {
  69. $0.listener?.performEndCall?(UUID: action.callUUID)
  70. }
  71. action.fulfill()
  72. }
  73. func provider(_ provider: CXProvider, perform action: CXSetMutedCallAction) {
  74. let uuid = pendingMuteActions.remove(action.uuid)
  75. // XXX avoid mute actions ping-pong: if the mute action was caused by
  76. // the JS side (we requested a transaction) don't call the delegate
  77. // method. If it was called by the provder itself (when the user presses
  78. // the mute button in the CallKit view) then call the delegate method.
  79. if (uuid == nil) {
  80. listeners.forEach {
  81. $0.listener?.performSetMutedCall?(UUID: action.callUUID,
  82. isMuted: action.isMuted)
  83. }
  84. }
  85. action.fulfill()
  86. }
  87. func provider(_ provider: CXProvider, perform action: CXStartCallAction) {
  88. listeners.forEach {
  89. $0.listener?.performStartCall?(UUID: action.callUUID,
  90. isVideo: action.isVideo)
  91. }
  92. action.fulfill()
  93. }
  94. func provider(_ provider: CXProvider,
  95. didActivate audioSession: AVAudioSession) {
  96. listeners.forEach {
  97. $0.listener?.providerDidActivateAudioSession?(session: audioSession)
  98. }
  99. }
  100. func provider(_ provider: CXProvider,
  101. didDeactivate audioSession: AVAudioSession) {
  102. listeners.forEach {
  103. $0.listener?.providerDidDeactivateAudioSession?(
  104. session: audioSession)
  105. }
  106. }
  107. }
  108. fileprivate struct JMCallKitEventListenerWrapper: Hashable {
  109. internal weak var listener: JMCallKitListener?
  110. public init(listener: JMCallKitListener) {
  111. self.listener = listener
  112. }
  113. public static func ==(lhs: JMCallKitEventListenerWrapper,
  114. rhs: JMCallKitEventListenerWrapper) -> Bool {
  115. // XXX We're aware that "[t]wo instances with equal hash values are not
  116. // necessarily equal to each other."
  117. return lhs.hashValue == rhs.hashValue
  118. }
  119. func hash(into hasher: inout Hasher) {
  120. hasher.combine(self.listener?.hash)
  121. }
  122. }