vdocipher-rn-bridge 1.16.0 → 1.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +11 -0
- package/VdoPlayerUIView.tsx +10 -10
- package/VdoPlayerView.tsx +170 -70
- package/android/build.gradle +3 -3
- package/android/src/main/java/com/vdocipher/rnbridge/ReactVdoPlayerUIView.java +13 -7
- package/android/src/main/java/com/vdocipher/rnbridge/VdoDownloadModule.java +67 -0
- package/downloadMonitor.ts +2 -2
- package/downloads.ts +63 -1
- package/index.ts +4 -1
- package/ios/Podfile +10 -0
- package/ios/Podfile.lock +3 -0
- package/ios/Pods/Manifest.lock +3 -0
- package/ios/Pods/Pods.xcodeproj/project.pbxproj +397 -0
- package/ios/Pods/Pods.xcodeproj/xcuserdata/mansisharma.xcuserdatad/xcschemes/Pods-VdocipherRnBridge.xcscheme +58 -0
- package/ios/Pods/Pods.xcodeproj/xcuserdata/mansisharma.xcuserdatad/xcschemes/xcschememanagement.plist +16 -0
- package/ios/Pods/Target Support Files/Pods-VdocipherRnBridge/Pods-VdocipherRnBridge-Info.plist +26 -0
- package/ios/Pods/Target Support Files/Pods-VdocipherRnBridge/Pods-VdocipherRnBridge-acknowledgements.markdown +3 -0
- package/ios/Pods/Target Support Files/Pods-VdocipherRnBridge/Pods-VdocipherRnBridge-acknowledgements.plist +29 -0
- package/ios/Pods/Target Support Files/Pods-VdocipherRnBridge/Pods-VdocipherRnBridge-dummy.m +5 -0
- package/ios/Pods/Target Support Files/Pods-VdocipherRnBridge/Pods-VdocipherRnBridge-umbrella.h +16 -0
- package/ios/Pods/Target Support Files/Pods-VdocipherRnBridge/Pods-VdocipherRnBridge.debug.xcconfig +8 -0
- package/ios/Pods/Target Support Files/Pods-VdocipherRnBridge/Pods-VdocipherRnBridge.modulemap +6 -0
- package/ios/Pods/Target Support Files/Pods-VdocipherRnBridge/Pods-VdocipherRnBridge.release.xcconfig +8 -0
- package/ios/RCTVdoPlayerUIView.h +19 -0
- package/ios/RCTVdoPlayerUIView.swift +74 -0
- package/ios/RCTVdoPlayerView.h +18 -0
- package/ios/RCTVdoPlayerView.swift +84 -0
- package/ios/TrackSelection.swift +64 -0
- package/ios/VdoCipherRNView.h +18 -0
- package/ios/VdoCipherRNView.swift +312 -0
- package/ios/VdoDownload.swift +103 -0
- package/ios/VdoEventEmitter.swift +157 -0
- package/ios/VdoPlaybackProperties.h +19 -0
- package/ios/VdoPlaybackProperties.swift +21 -0
- package/ios/VdocipherRnBridge.h +2 -10
- package/ios/VdocipherRnBridgeViewManager.m +99 -0
- package/ios/VdocipherRnBridgeViewManager.swift +90 -0
- package/package.json +3 -2
- package/react-native-vdocipher-rn-bridge.podspec +36 -0
- package/type.ts +23 -0
- package/ios/VdocipherRnBridge.m +0 -13
- package/ios/VdocipherRnBridge.xcodeproj/project.pbxproj +0 -259
- package/ios/VdocipherRnBridge.xcworkspace/contents.xcworkspacedata +0 -9
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
//
|
|
2
|
+
// RCTVdoPlayerUIView.swift
|
|
3
|
+
// react-native-vdocipher-rn-bridge
|
|
4
|
+
//
|
|
5
|
+
// Created by Manish Gupta on 22/05/23.
|
|
6
|
+
//
|
|
7
|
+
|
|
8
|
+
import React
|
|
9
|
+
import Foundation
|
|
10
|
+
import VdoFramework
|
|
11
|
+
|
|
12
|
+
@objc(RCTVdoPlayerUIView)
|
|
13
|
+
class RCTVdoPlayerUIView: VdoCipherRNView {
|
|
14
|
+
|
|
15
|
+
override func setEmbedInfo(_ embedInfo: [String: Any]) {
|
|
16
|
+
super.setEmbedInfo(embedInfo)
|
|
17
|
+
|
|
18
|
+
DispatchQueue.main.asyncAfter(deadline: .now() + 0.33 , execute: {
|
|
19
|
+
|
|
20
|
+
EventEmitter.sharedInstance.dispatch(name: EventName.EVENT_LOADING, params: nil)
|
|
21
|
+
|
|
22
|
+
let playerViewController = VdoCipher.getVdoPlayerViewController()
|
|
23
|
+
playerViewController.view.frame = self.bounds
|
|
24
|
+
playerViewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
|
|
25
|
+
self.reactViewController().addChild(playerViewController)
|
|
26
|
+
self.addSubview(playerViewController.view)
|
|
27
|
+
playerViewController.didMove(toParent: self.reactViewController())
|
|
28
|
+
self.reactViewController().navigationController?.delegate = self
|
|
29
|
+
guard let playbackInfo = embedInfo["playbackInfo"] as? String,
|
|
30
|
+
let otp = embedInfo["otp"] as? String,
|
|
31
|
+
let decoded = Data(base64Encoded: playbackInfo),
|
|
32
|
+
let parsed = try? JSONDecoder().decode(PlaybackInfo.self, from: decoded) else {
|
|
33
|
+
let errorDescription = ErrorDescription(errorCode: 4101,
|
|
34
|
+
errorMsg: "Error: 4101 Invalid video parameters. Please contact the app developer.",
|
|
35
|
+
httpStatusCode: -1)
|
|
36
|
+
self.sendErrorEventWithDescription(errorDescription)
|
|
37
|
+
return
|
|
38
|
+
}
|
|
39
|
+
let customPlayerId = embedInfo["customPlayerId"] as? String
|
|
40
|
+
let preferredCaptionsLanguage = embedInfo["preferredCaptionsLanguage"] as? String
|
|
41
|
+
let enableAutoResume = embedInfo["enableAutoResume"] as? Bool
|
|
42
|
+
if enableAutoResume == false {
|
|
43
|
+
let userDefaults: UserDefaults? = UserDefaults.init(suiteName: "com.vdocipher.ios.VdoFramework")
|
|
44
|
+
if var assetsLastPlaybackTimeSet = userDefaults?.value(forKey: "assetsLastPlaybackTimeSet") as? [String: Double] {
|
|
45
|
+
assetsLastPlaybackTimeSet[parsed.videoId] = Double(0)
|
|
46
|
+
userDefaults?.set(assetsLastPlaybackTimeSet, forKey: "assetsLastPlaybackTimeSet")
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
VdoAsset.createAsset(videoId: parsed.videoId, playerId: customPlayerId, onLoad: { asset, err in
|
|
50
|
+
DispatchQueue.main.async {
|
|
51
|
+
guard let asset = asset else {
|
|
52
|
+
if let error = err {
|
|
53
|
+
let errorDescription = ErrorDescription(errorCode: error.code, errorMsg: error.message, httpStatusCode: -1)
|
|
54
|
+
self.sendErrorEventWithDescription(errorDescription)
|
|
55
|
+
}
|
|
56
|
+
return
|
|
57
|
+
}
|
|
58
|
+
self.asset = asset
|
|
59
|
+
TrackSelection.asset = asset
|
|
60
|
+
if preferredCaptionsLanguage?.isEmpty == false {
|
|
61
|
+
DispatchQueue.main.async {
|
|
62
|
+
_ = self.setCaptionLanguage(lanCode: preferredCaptionsLanguage)
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
if asset.downloadState == .downloaded {
|
|
66
|
+
asset.playOffline()
|
|
67
|
+
} else {
|
|
68
|
+
asset.playOnline(otp: otp, playbackInfo: playbackInfo)
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
})
|
|
72
|
+
})
|
|
73
|
+
}
|
|
74
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
//
|
|
2
|
+
// RCTVdoPlayerView.h
|
|
3
|
+
// Pods
|
|
4
|
+
//
|
|
5
|
+
// Created by Manish Gupta on 25/05/23.
|
|
6
|
+
//
|
|
7
|
+
|
|
8
|
+
#import "VdoCipherRNView.h"
|
|
9
|
+
|
|
10
|
+
#ifndef RCTVdoPlayerView_h
|
|
11
|
+
#define RCTVdoPlayerView_h
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
#endif /* RCTVdoPlayerView_h */
|
|
15
|
+
|
|
16
|
+
@interface RCTVdoPlayerView : VdoCipherRNView
|
|
17
|
+
|
|
18
|
+
@end
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
//
|
|
2
|
+
// RCTVdoPlayerView.swift
|
|
3
|
+
// react-native-vdocipher-rn-bridge
|
|
4
|
+
//
|
|
5
|
+
// Created by Manish Gupta on 25/05/23.
|
|
6
|
+
//
|
|
7
|
+
|
|
8
|
+
import React
|
|
9
|
+
import Foundation
|
|
10
|
+
import VdoFramework
|
|
11
|
+
import AVKit
|
|
12
|
+
|
|
13
|
+
@objc(RCTVdoPlayerView)
|
|
14
|
+
class RCTVdoPlayerView: VdoCipherRNView {
|
|
15
|
+
|
|
16
|
+
var playerLayer: AVPlayerLayer?
|
|
17
|
+
|
|
18
|
+
override func setEmbedInfo(_ embedInfo: [String: Any]) {
|
|
19
|
+
super.setEmbedInfo(embedInfo)
|
|
20
|
+
EventEmitter.sharedInstance.dispatch(name: EventName.EVENT_LOADING, params: nil)
|
|
21
|
+
DispatchQueue.main.asyncAfter(deadline: .now() + 0.33 , execute: {
|
|
22
|
+
self.reactViewController().navigationController?.delegate = self
|
|
23
|
+
})
|
|
24
|
+
guard let playbackInfo = embedInfo["playbackInfo"] as? String,
|
|
25
|
+
let otp = embedInfo["otp"] as? String,
|
|
26
|
+
let decoded = Data(base64Encoded: playbackInfo),
|
|
27
|
+
let parsed = try? JSONDecoder().decode(PlaybackInfo.self, from: decoded) else {
|
|
28
|
+
let errorDescription = ErrorDescription(errorCode: 4101,
|
|
29
|
+
errorMsg: "Error: 4101 Invalid video parameters. Please contact the app developer.",
|
|
30
|
+
httpStatusCode: -1)
|
|
31
|
+
self.sendErrorEventWithDescription(errorDescription)
|
|
32
|
+
return
|
|
33
|
+
}
|
|
34
|
+
let preferredCaptionsLanguage = embedInfo["preferredCaptionsLanguage"] as? String
|
|
35
|
+
let enableAutoResume = embedInfo["enableAutoResume"] as? Bool
|
|
36
|
+
if enableAutoResume == false {
|
|
37
|
+
let userDefaults: UserDefaults? = UserDefaults.init(suiteName: "com.vdocipher.ios.VdoFramework")
|
|
38
|
+
if var assetsLastPlaybackTimeSet = userDefaults?.value(forKey: "assetsLastPlaybackTimeSet") as? [String: Double] {
|
|
39
|
+
assetsLastPlaybackTimeSet[parsed.videoId] = Double(0)
|
|
40
|
+
userDefaults?.set(assetsLastPlaybackTimeSet, forKey: "assetsLastPlaybackTimeSet")
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
VdoAsset.createAsset(videoId: parsed.videoId, onLoad: { asset, err in
|
|
44
|
+
DispatchQueue.main.async {
|
|
45
|
+
guard let asset = asset else {
|
|
46
|
+
if let error = err {
|
|
47
|
+
let errorDescription = ErrorDescription(errorCode: error.code, errorMsg: error.message, httpStatusCode: -1)
|
|
48
|
+
self.sendErrorEventWithDescription(errorDescription)
|
|
49
|
+
}
|
|
50
|
+
return
|
|
51
|
+
}
|
|
52
|
+
self.asset = asset
|
|
53
|
+
TrackSelection.asset = asset
|
|
54
|
+
if preferredCaptionsLanguage?.isEmpty == false {
|
|
55
|
+
DispatchQueue.main.async {
|
|
56
|
+
_ = self.setCaptionLanguage(lanCode: preferredCaptionsLanguage)
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
if asset.downloadState == .downloaded {
|
|
60
|
+
asset.playOffline()
|
|
61
|
+
} else {
|
|
62
|
+
asset.playOnline(otp: otp, playbackInfo: playbackInfo)
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
})
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
override func playerReadyToPlay() {
|
|
69
|
+
playerLayer = AVPlayerLayer(player: player)
|
|
70
|
+
playerLayer?.frame = self.bounds
|
|
71
|
+
playerLayer?.videoGravity = .resizeAspect
|
|
72
|
+
self.layer.sublayers?
|
|
73
|
+
.filter { $0 is AVPlayerLayer }
|
|
74
|
+
.forEach { $0.removeFromSuperlayer() }
|
|
75
|
+
self.layer.addSublayer(playerLayer!)
|
|
76
|
+
super.playerReadyToPlay()
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
override func layoutSubviews() {
|
|
80
|
+
super.layoutSubviews()
|
|
81
|
+
self.playerLayer?.frame = self.bounds
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
//
|
|
2
|
+
// TrackSelection.swift
|
|
3
|
+
// react-native-vdocipher-rn-bridge
|
|
4
|
+
//
|
|
5
|
+
// Created by Manish Gupta on 14/06/23.
|
|
6
|
+
//
|
|
7
|
+
|
|
8
|
+
import Foundation
|
|
9
|
+
import VdoFramework
|
|
10
|
+
import React
|
|
11
|
+
|
|
12
|
+
@objc(TrackSelection)
|
|
13
|
+
class TrackSelection: NSObject {
|
|
14
|
+
|
|
15
|
+
/// Current Asset
|
|
16
|
+
static var asset: VdoAsset? {
|
|
17
|
+
didSet{
|
|
18
|
+
guard let captions = TrackSelection.asset?.captions else {
|
|
19
|
+
return
|
|
20
|
+
}
|
|
21
|
+
var captionLanguages = [[String: Any]] ()
|
|
22
|
+
var counter = 1
|
|
23
|
+
for caption in captions {
|
|
24
|
+
guard let lang = caption.lang,
|
|
25
|
+
let label = caption.label,
|
|
26
|
+
let name = caption.captionName else {continue}
|
|
27
|
+
captionLanguages.append(["id": counter,
|
|
28
|
+
"language": lang,
|
|
29
|
+
"label": label,
|
|
30
|
+
"name": name])
|
|
31
|
+
counter += 1
|
|
32
|
+
}
|
|
33
|
+
TrackSelection.captionLanguagesMap = captionLanguages
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
static var captionLanguagesMap: [[String: Any]]?
|
|
37
|
+
|
|
38
|
+
@objc func getCaptionLanguages(_ callback: RCTResponseSenderBlock) {
|
|
39
|
+
// print("========= TrackSelection getCaptionLanguages called")
|
|
40
|
+
guard let captionLanguagesMap = TrackSelection.captionLanguagesMap else {
|
|
41
|
+
callback(nil)
|
|
42
|
+
return
|
|
43
|
+
}
|
|
44
|
+
callback([captionLanguagesMap])
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
@objc func getSelectedCaptionLanguage(_ callback: RCTResponseSenderBlock) {
|
|
48
|
+
// print("========= TrackSelection getSelectedCaptionLanguage called")
|
|
49
|
+
guard let selectedCaption = TrackSelection.asset?.getCurrentEnabledCaption(),
|
|
50
|
+
let name = selectedCaption.captionName,
|
|
51
|
+
let selectedCaptionMap = TrackSelection.captionLanguagesMap?.first(where: {$0["name"] as? String == name}) else {
|
|
52
|
+
callback([])
|
|
53
|
+
return
|
|
54
|
+
}
|
|
55
|
+
// print("Selected Caption Map is \(selectedCaptionMap)")
|
|
56
|
+
callback([selectedCaptionMap])
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
@objc func getDuration(_ callback: RCTResponseSenderBlock) {
|
|
60
|
+
// print("========= TrackSelection getDuration called")
|
|
61
|
+
guard let duration = TrackSelection.asset?.duration else {return}
|
|
62
|
+
callback([["duration": duration]])
|
|
63
|
+
}
|
|
64
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
//
|
|
2
|
+
// VdoCipherRNView.h
|
|
3
|
+
// Pods
|
|
4
|
+
//
|
|
5
|
+
// Created by Manish Gupta on 31/05/23.
|
|
6
|
+
//
|
|
7
|
+
|
|
8
|
+
#ifndef VdoCipherRNView_h
|
|
9
|
+
#define VdoCipherRNView_h
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
#endif /* VdoCipherRNView_h */
|
|
13
|
+
|
|
14
|
+
#import <VdoFramework/VdoFramework.h>
|
|
15
|
+
|
|
16
|
+
@interface VdoCipherRNView: UIView
|
|
17
|
+
|
|
18
|
+
@end
|
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
//
|
|
2
|
+
// VdoCipherRNView.swift
|
|
3
|
+
// react-native-vdocipher-rn-bridge
|
|
4
|
+
//
|
|
5
|
+
// Created by Manish Gupta on 29/05/23.
|
|
6
|
+
//
|
|
7
|
+
|
|
8
|
+
import Foundation
|
|
9
|
+
import AVKit
|
|
10
|
+
import VdoFramework
|
|
11
|
+
|
|
12
|
+
class VdoCipherRNView: UIView {
|
|
13
|
+
|
|
14
|
+
@objc var player: AVPlayer?
|
|
15
|
+
@objc var asset: VdoAsset?
|
|
16
|
+
|
|
17
|
+
private var playWhenReady: Bool = true
|
|
18
|
+
private var progressTimer: Timer?
|
|
19
|
+
private var isLoaded = false
|
|
20
|
+
private var targetPlaybackSpeed: Float = 1.0
|
|
21
|
+
|
|
22
|
+
private var playerItemBufferEmptyObserver: NSKeyValueObservation?
|
|
23
|
+
private var playerItemBufferKeepUpObserver: NSKeyValueObservation?
|
|
24
|
+
private var playerItemBufferFullObserver: NSKeyValueObservation?
|
|
25
|
+
|
|
26
|
+
private var playerState: PlayerState = .unknown {
|
|
27
|
+
didSet{
|
|
28
|
+
if oldValue != playerState {
|
|
29
|
+
EventEmitter.sharedInstance.dispatch(name: .EVENT_PLAYER_STATE_CHANGED, params: [Keys.EVENT_PROP_PLAYER_STATE.rawValue : playerState.rawValue, Keys.EVENT_PROP_PLAY_WHEN_READY.rawValue : self.playWhenReady])
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
override init(frame: CGRect) {
|
|
35
|
+
super.init(frame: frame)
|
|
36
|
+
let userDefaults: UserDefaults? = UserDefaults.init(suiteName: "com.vdocipher.ios.rn.VdoFramework")
|
|
37
|
+
userDefaults?.set(["name": "react-native", "version": VdocipherRnBridge.vdocipherRnBridgeVersionString], forKey: "rnToolkitInfo")
|
|
38
|
+
VdoCipher.setPlaybackDelegate(delegate: self)
|
|
39
|
+
VdoCipher.setUIPlayerDelegate(self)
|
|
40
|
+
EventEmitter.sharedInstance.dispatch(name: EventName.EVENT_INIT_SUCCESS, params: [Keys.EVENT_PROP_RESTORED.rawValue : false])
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
required init?(coder: NSCoder) {
|
|
44
|
+
super.init(coder: coder)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
override func willMove(toSuperview newSuperview: UIView?) {
|
|
48
|
+
super.willMove(toSuperview: newSuperview)
|
|
49
|
+
guard newSuperview != nil else {
|
|
50
|
+
return
|
|
51
|
+
}
|
|
52
|
+
// print("added to a superView \(newSuperview) reactViewController is \(self.reactViewController())")
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
override func didMoveToSuperview() {
|
|
56
|
+
super.didMoveToSuperview()
|
|
57
|
+
if self.superview == nil {
|
|
58
|
+
player?.pause()
|
|
59
|
+
self.removeObservers()
|
|
60
|
+
player?.replaceCurrentItem(with: nil)
|
|
61
|
+
player = nil
|
|
62
|
+
} /* else {
|
|
63
|
+
// print("did move to a superView, reactViewController is \(self.reactViewController())")
|
|
64
|
+
} */
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
func addObserversToPlayer()
|
|
68
|
+
{
|
|
69
|
+
NotificationCenter.default.addObserver(self,
|
|
70
|
+
selector: #selector(videoDidEnd),
|
|
71
|
+
name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,
|
|
72
|
+
object: nil)
|
|
73
|
+
|
|
74
|
+
playerItemBufferEmptyObserver = self.player?.currentItem?.observe(\AVPlayerItem.isPlaybackBufferEmpty, options: [.new]) { [weak self] (_, _) in
|
|
75
|
+
guard let self = self else { return }
|
|
76
|
+
self.playerState = .buffering
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
playerItemBufferKeepUpObserver = self.player?.currentItem?.observe(\AVPlayerItem.isPlaybackLikelyToKeepUp, options: [.new]) { [weak self] (_, _) in
|
|
80
|
+
guard let self = self else { return }
|
|
81
|
+
self.playerState = .ready
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
playerItemBufferFullObserver = self.player?.currentItem?.observe(\AVPlayerItem.isPlaybackBufferFull, options: [.new]) { [weak self] (_, _) in
|
|
85
|
+
guard let self = self else { return }
|
|
86
|
+
self.playerState = .ready
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
progressTimer = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true, block: {_ in
|
|
90
|
+
self.sendPlayerProgress()
|
|
91
|
+
})
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
private func sendPlayerProgress() {
|
|
95
|
+
let currentTime = Int(self.player?.currentItem?.currentTime().seconds.rounded( .down) ?? 0.0 ) * 1000
|
|
96
|
+
// print("Current time send as event emitter is \(currentTime)")
|
|
97
|
+
if currentTime > 0 {
|
|
98
|
+
EventEmitter.sharedInstance.dispatch(name: .EVENT_PROGRESS, params: [Keys.EVENT_PROP_CURRENT_TIME.rawValue : currentTime])
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
func setCaptionLanguage(lanCode: String?) -> Bool {
|
|
103
|
+
var isSuccess: Bool = false
|
|
104
|
+
guard let asset = self.asset else { return isSuccess } // return false
|
|
105
|
+
if lanCode == nil {
|
|
106
|
+
isSuccess = asset.setCaption(nil)
|
|
107
|
+
return isSuccess
|
|
108
|
+
} else {
|
|
109
|
+
if let caption = asset.captions?.first(where: { $0.lang == lanCode }) {
|
|
110
|
+
isSuccess = asset.setCaption(caption)
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return isSuccess
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
func removeObservers()
|
|
117
|
+
{
|
|
118
|
+
NotificationCenter.default.removeObserver(self,
|
|
119
|
+
name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,
|
|
120
|
+
object: nil)
|
|
121
|
+
|
|
122
|
+
playerItemBufferEmptyObserver?.invalidate()
|
|
123
|
+
playerItemBufferEmptyObserver = nil
|
|
124
|
+
|
|
125
|
+
playerItemBufferKeepUpObserver?.invalidate()
|
|
126
|
+
playerItemBufferKeepUpObserver = nil
|
|
127
|
+
|
|
128
|
+
playerItemBufferFullObserver?.invalidate()
|
|
129
|
+
playerItemBufferFullObserver = nil
|
|
130
|
+
|
|
131
|
+
progressTimer?.invalidate()
|
|
132
|
+
progressTimer = nil
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
@objc func videoDidEnd(notification: NSNotification) {
|
|
136
|
+
if self.playerState == .ended {
|
|
137
|
+
return
|
|
138
|
+
}
|
|
139
|
+
self.sendPlayerProgress()
|
|
140
|
+
self.playerState = .ended
|
|
141
|
+
self.playWhenReady = false
|
|
142
|
+
EventEmitter.sharedInstance.dispatch(name: .EVENT_MEDIA_ENDED, params: nil)
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
@objc func playerReadyToPlay() {
|
|
146
|
+
self.playerState = .ready
|
|
147
|
+
if self.playWhenReady == true {
|
|
148
|
+
self.player?.rate = targetPlaybackSpeed
|
|
149
|
+
} else {
|
|
150
|
+
self.player?.rate = 0
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
@objc func setEmbedInfo(_ embedInfo: [String: Any]) {
|
|
155
|
+
// print("Embed Info is \(embedInfo)")
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
@objc func setResizeMode(_ resizeMode: String) {
|
|
159
|
+
// print("Resize Mode called with value == \(resizeMode)")
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
@objc func setPlayWhenReady(_ playWhenReady: Bool) {
|
|
163
|
+
// print("playWhenReady called with value == \(playWhenReady)")
|
|
164
|
+
self.playWhenReady = playWhenReady
|
|
165
|
+
if playWhenReady == true {
|
|
166
|
+
if targetPlaybackSpeed == 0 {
|
|
167
|
+
targetPlaybackSpeed = 1.0
|
|
168
|
+
}
|
|
169
|
+
// Above Condition implies the playback was paused and play is triggered
|
|
170
|
+
// The condition is required since we need to keep the playbackSpeed in case user navigates to and fro this screen.
|
|
171
|
+
self.player?.rate = targetPlaybackSpeed
|
|
172
|
+
} else {
|
|
173
|
+
// Below on pause 'targetPlaybackSpeed' is set to 0 which implies when user navigates back to this screen playback shouldn't resume
|
|
174
|
+
self.targetPlaybackSpeed = 0
|
|
175
|
+
self.player?.rate = 0
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
@objc func setPlaybackSpeed(_ speed: Float) {
|
|
180
|
+
// print("playbackSpeed called with value == \(speed)")
|
|
181
|
+
self.targetPlaybackSpeed = speed
|
|
182
|
+
self.player?.rate = speed
|
|
183
|
+
EventEmitter.sharedInstance.dispatch(name: .EVENT_PLAYBACK_SPEED_CHANGED, params: [Keys.EVENT_PROP_PLAYBACK_SPEED.rawValue : speed])
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
@objc func setSeek(_ seekTime: Int) {
|
|
187
|
+
// print("seek called with value == \(seekTime)")
|
|
188
|
+
self.player?.seek(to: CMTime(value: CMTimeValue(seekTime), timescale: 1000))
|
|
189
|
+
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
@objc func setCaptionLanguage(_ caption: [String: Any]) {
|
|
193
|
+
// print("========= TrackSelection setCaptionLanguage called")
|
|
194
|
+
guard let asset = TrackSelection.asset,
|
|
195
|
+
let captions = asset.captions,
|
|
196
|
+
let selectedCaptionName = caption["name"] as? String,
|
|
197
|
+
let selectedCaptionObject = captions.first(where: {$0.captionName ?? "" == selectedCaptionName}) else {return}
|
|
198
|
+
if (asset.setCaption(selectedCaptionObject) == true) {
|
|
199
|
+
guard let selectedCaption = TrackSelection.asset?.getCurrentEnabledCaption(),
|
|
200
|
+
let name = selectedCaption.captionName,
|
|
201
|
+
let selectedCaptionMap = TrackSelection.captionLanguagesMap?.first(where: {$0["name"] as? String == name}) else {
|
|
202
|
+
return
|
|
203
|
+
}
|
|
204
|
+
EventEmitter.sharedInstance.dispatch(name: .EVENT_TRACKS_CHANGED, params: [Keys.EVENT_PROP_AVAILABLE_TRACKS.rawValue : TrackSelection.captionLanguagesMap ?? [], Keys.EVENT_PROP_SELECTED_TRACKS.rawValue: [selectedCaptionMap]])
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
@objc func setDisableCaptions(_ val: Int) {
|
|
209
|
+
// print("========= TrackSelection disableCaptions called")
|
|
210
|
+
if (TrackSelection.asset?.setCaption(nil) == true) {
|
|
211
|
+
EventEmitter.sharedInstance.dispatch(name: .EVENT_TRACKS_CHANGED, params: [Keys.EVENT_PROP_AVAILABLE_TRACKS.rawValue : TrackSelection.captionLanguagesMap ?? []])
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
func sendErrorEventWithDescription(_ errorDescription: ErrorDescription) {
|
|
216
|
+
if let errorInfo = getJSONEncodedEventDictionary(info: errorDescription) {
|
|
217
|
+
EventEmitter.sharedInstance.dispatch(name: .EVENT_LOAD_ERROR, params: [Keys.EVENT_PROP_ERROR_DESCRIPTION.rawValue : errorInfo])
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
func getJSONEncodedEventDictionary<T>(info eventInfo: T) -> [String: Any]? where T : Encodable {
|
|
222
|
+
let jsonEncoder = JSONEncoder()
|
|
223
|
+
guard let encoded = try? jsonEncoder.encode(eventInfo),
|
|
224
|
+
let info = try? JSONSerialization.jsonObject(with: encoded, options: .allowFragments) as? [String: Any] else {
|
|
225
|
+
EventEmitter.sharedInstance.dispatch(name: .EVENT_LOAD_ERROR,
|
|
226
|
+
params: [Keys.EVENT_PROP_ERROR_DESCRIPTION.rawValue : EventEmitter.sharedInstance.errorParsingFailDefaultErrInfo])
|
|
227
|
+
return nil
|
|
228
|
+
}
|
|
229
|
+
return info
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
extension VdoCipherRNView: AssetPlaybackDelegate {
|
|
234
|
+
func streamLoadError(error: VdoError) {
|
|
235
|
+
let errorDescription = ErrorDescription(errorCode: error.code, errorMsg: error.message, httpStatusCode: -1)
|
|
236
|
+
self.sendErrorEventWithDescription(errorDescription)
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
func streamPlaybackManager(playerReadyToPlay player: AVPlayer) {
|
|
240
|
+
self.playerReadyToPlay()
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
func streamPlaybackManager(playerCurrentItemDidChange player: AVPlayer) {
|
|
244
|
+
self.setUpPlayer(player)
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
func setUpPlayer(_ player: AVPlayer) {
|
|
248
|
+
if isLoaded == true {
|
|
249
|
+
return
|
|
250
|
+
}
|
|
251
|
+
isLoaded = true
|
|
252
|
+
self.player = player
|
|
253
|
+
self.removeObservers()
|
|
254
|
+
self.addObserversToPlayer()
|
|
255
|
+
|
|
256
|
+
guard let asset = asset else { return }
|
|
257
|
+
let mInfo = Media(mediaId: asset.videoId,
|
|
258
|
+
type: "streaming",
|
|
259
|
+
title: asset.title ?? "",
|
|
260
|
+
description: asset.description,
|
|
261
|
+
duration: (Int(asset.duration ?? 0.0) * 1000) )
|
|
262
|
+
guard let eventInfo = getJSONEncodedEventDictionary(info: mInfo) else { return }
|
|
263
|
+
EventEmitter.sharedInstance.dispatch(name: EventName.EVENT_LOADED,
|
|
264
|
+
params: [Keys.EVENT_PROP_MEDIA_INFO.rawValue : eventInfo])
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
extension VdoCipherRNView: VdoPlayerViewControllerDelegate {
|
|
269
|
+
func didTapEnterFullScreen(for id: String) {
|
|
270
|
+
EventEmitter.sharedInstance.dispatch(name: .EVENT_ENTER_FULLSCREEN, params: nil)
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
func didTapExitFullScreen(for id: String) {
|
|
274
|
+
EventEmitter.sharedInstance.dispatch(name: .EVENT_EXIT_FULLSCREEN, params: nil)
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
func didChangePlayerRate(for id: String, to speed: Float) {
|
|
278
|
+
self.targetPlaybackSpeed = speed
|
|
279
|
+
EventEmitter.sharedInstance.dispatch(name: .EVENT_PLAYBACK_SPEED_CHANGED, params: [Keys.EVENT_PROP_PLAYBACK_SPEED.rawValue : speed])
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
func didChangeCaption(for id: String, to name: String, code: String) {
|
|
283
|
+
guard let selectedCaptionMap = TrackSelection.captionLanguagesMap?.first(where: {$0["language"] as! String == code}) else {
|
|
284
|
+
return
|
|
285
|
+
}
|
|
286
|
+
EventEmitter.sharedInstance.dispatch(name: .EVENT_TRACKS_CHANGED, params: [Keys.EVENT_PROP_AVAILABLE_TRACKS.rawValue : TrackSelection.captionLanguagesMap ?? [], Keys.EVENT_PROP_SELECTED_TRACKS.rawValue: [selectedCaptionMap]])
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
extension VdoCipherRNView: UINavigationControllerDelegate {
|
|
291
|
+
|
|
292
|
+
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
|
|
293
|
+
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
|
|
297
|
+
guard let parentController = self.reactViewController() else {return}
|
|
298
|
+
if viewController != parentController {
|
|
299
|
+
player?.pause()
|
|
300
|
+
} else if viewController == parentController {
|
|
301
|
+
player?.rate = targetPlaybackSpeed
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
private enum PlayerState : String
|
|
307
|
+
{
|
|
308
|
+
case buffering = "buffering",
|
|
309
|
+
ready = "ready",
|
|
310
|
+
ended = "ended",
|
|
311
|
+
unknown = "unknown"
|
|
312
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
//
|
|
2
|
+
// VdoDownload.swift
|
|
3
|
+
// react-native-vdocipher-rn-bridge
|
|
4
|
+
//
|
|
5
|
+
// Created by Manish Gupta on 05/06/23.
|
|
6
|
+
//
|
|
7
|
+
|
|
8
|
+
import Foundation
|
|
9
|
+
import React
|
|
10
|
+
|
|
11
|
+
class DownloadEventEmitter {
|
|
12
|
+
|
|
13
|
+
/// Shared Instance.
|
|
14
|
+
public static var sharedInstance = DownloadEventEmitter()
|
|
15
|
+
|
|
16
|
+
// ReactNativeEventEmitter is instantiated by React Native with the bridge.
|
|
17
|
+
private static var eventEmitter: VdoDownload!
|
|
18
|
+
|
|
19
|
+
private init() {}
|
|
20
|
+
|
|
21
|
+
// When React Native instantiates the emitter it is registered here.
|
|
22
|
+
func registerEventEmitter(eventEmitter: VdoDownload) {
|
|
23
|
+
DownloadEventEmitter.eventEmitter = eventEmitter
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
func dispatch(name: DownloadEventName, params: [String: Any]?) {
|
|
27
|
+
if DownloadEventEmitter.eventEmitter.hasListener == true {
|
|
28
|
+
var data = [String: Any] ()
|
|
29
|
+
if let p = params {
|
|
30
|
+
data["nativeEvent"] = p
|
|
31
|
+
}
|
|
32
|
+
// print("event data sent is \(data)")
|
|
33
|
+
DownloadEventEmitter.eventEmitter.sendEvent(withName: name.rawValue, body: data)
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/// All Events which must be support by React Native.
|
|
38
|
+
lazy var allEvents: [String] = {
|
|
39
|
+
|
|
40
|
+
return DownloadEventName.allCases.map { $0.rawValue }
|
|
41
|
+
}()
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
enum DownloadEventName: String, CaseIterable {
|
|
46
|
+
case EVENT_QUEUED = "onQueued"
|
|
47
|
+
case EVENT_CHANGED = "onChanged"
|
|
48
|
+
case EVENT_COMPLETED = "onCompleted"
|
|
49
|
+
case EVENT_FAILED = "onFailed"
|
|
50
|
+
case EVENT_DELETED = "onDeleted"
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
@objc(VdoDownload)
|
|
54
|
+
class VdoDownload: RCTEventEmitter {
|
|
55
|
+
|
|
56
|
+
override init() {
|
|
57
|
+
super.init()
|
|
58
|
+
// print("\(type(of: self))).\(#function)")
|
|
59
|
+
DownloadEventEmitter.sharedInstance.registerEventEmitter(eventEmitter: self)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
var hasListener: Bool?
|
|
63
|
+
|
|
64
|
+
/// Base overide for RCTEventEmitter.
|
|
65
|
+
///
|
|
66
|
+
/// - Returns: all supported events
|
|
67
|
+
@objc open override func supportedEvents() -> [String] {
|
|
68
|
+
// print("\(type(of: self))).\(#function)")
|
|
69
|
+
return DownloadEventEmitter.sharedInstance.allEvents
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
@objc open override func startObserving() {
|
|
73
|
+
super.startObserving()
|
|
74
|
+
hasListener = true
|
|
75
|
+
// print("\(type(of: self))).\(#function)")
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
@objc open override func stopObserving() {
|
|
79
|
+
super.stopObserving()
|
|
80
|
+
hasListener = false
|
|
81
|
+
// print("\(type(of: self))).\(#function)")
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
open override class func requiresMainQueueSetup() -> Bool {
|
|
85
|
+
// print("\(type(of: self))).\(#function)")
|
|
86
|
+
return true
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
open override func addListener(_ eventName: String!) {
|
|
90
|
+
super.addListener(eventName)
|
|
91
|
+
// print("\(type(of: self))).\(#function) eventName: \(eventName)")
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
open override func removeListeners(_ count: Double) {
|
|
95
|
+
super.removeListeners(count)
|
|
96
|
+
// print("\(type(of: self))).\(#function) count: \(count)")
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
open override func invalidate() {
|
|
100
|
+
super.invalidate()
|
|
101
|
+
// print("\(type(of: self))).\(#function)")
|
|
102
|
+
}
|
|
103
|
+
}
|