diff --git a/API.md b/API.md index d927a3b2..40781558 100644 --- a/API.md +++ b/API.md @@ -74,6 +74,17 @@ Video with caching ([more info](docs/caching.md)): end ``` +#### Enable custom feature in podfile file + +##### Google IMA + +Google IMA is the google SDK to support Client Side Ads Integration (CSAI), see [google documentation](https://developers.google.com/interactive-media-ads/docs/sdks/ios/client-side) for more informations. + +To enable google IMA usage define add following line in your podfile: +```podfile +$RNVideoUseGoogleIMA=true +``` + ### tvOS installation diff --git a/examples/basic/ios/Podfile b/examples/basic/ios/Podfile index 5677fd6c..9438f885 100644 --- a/examples/basic/ios/Podfile +++ b/examples/basic/ios/Podfile @@ -7,6 +7,8 @@ install! 'cocoapods', :deterministic_uuids => false target 'videoplayer' do config = use_native_modules! + # $RNVideoUseGoogleIMA = true + # Flags change depending on the env values. flags = get_default_flags() diff --git a/ios/Video/Features/RCTIMAAdsManager.swift b/ios/Video/Features/RCTIMAAdsManager.swift index 059ec639..d2a3833f 100644 --- a/ios/Video/Features/RCTIMAAdsManager.swift +++ b/ios/Video/Features/RCTIMAAdsManager.swift @@ -1,3 +1,4 @@ +#if USE_GOOGLE_IMA import Foundation import GoogleInteractiveMediaAds @@ -185,3 +186,4 @@ class RCTIMAAdsManager: NSObject, IMAAdsLoaderDelegate, IMAAdsManagerDelegate { return result; } } +#endif diff --git a/ios/Video/RCTVideo.swift b/ios/Video/RCTVideo.swift index be1c0981..678623d4 100644 --- a/ios/Video/RCTVideo.swift +++ b/ios/Video/RCTVideo.swift @@ -1,7 +1,9 @@ import AVFoundation import AVKit import Foundation +#if USE_GOOGLE_IMA import GoogleInteractiveMediaAds +#endif import React import Promises @@ -63,11 +65,13 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH /* IMA Ads */ private var _adTagUrl:String? +#if USE_GOOGLE_IMA private var _imaAdsManager: RCTIMAAdsManager! - private var _didRequestAds:Bool = false - private var _adPlaying:Bool = false /* Playhead used by the SDK to track content video progress and insert mid-rolls. */ private var _contentPlayhead: IMAAVPlayerContentPlayhead? +#endif + private var _didRequestAds:Bool = false + private var _adPlaying:Bool = false private var _resouceLoaderDelegate: RCTResourceLoaderDelegate? private var _playerObserver: RCTPlayerObserver = RCTPlayerObserver() @@ -107,8 +111,9 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH init(eventDispatcher:RCTEventDispatcher!) { super.init(frame: CGRect(x: 0, y: 0, width: 100, height: 100)) - +#if USE_GOOGLE_IMA _imaAdsManager = RCTIMAAdsManager(video: self) +#endif _eventDispatcher = eventDispatcher @@ -147,8 +152,9 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) - +#if USE_GOOGLE_IMA _imaAdsManager = RCTIMAAdsManager(video: self) +#endif } deinit { @@ -217,10 +223,12 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH ]) if currentTimeSecs >= 0 { +#if USE_GOOGLE_IMA if !_didRequestAds && currentTimeSecs >= 0.0001 && _adTagUrl != nil { _imaAdsManager.requestAds() _didRequestAds = true } +#endif onVideoProgress?([ "currentTime": NSNumber(value: Float(currentTimeSecs)), "playableDuration": RCTVideoUtils.calculatePlayableDuration(_player), @@ -307,13 +315,14 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH self.setAutomaticallyWaitsToMinimizeStalling(self._automaticallyWaitsToMinimizeStalling) } +#if USE_GOOGLE_IMA if self._adTagUrl != nil { // Set up your content playhead and contentComplete callback. self._contentPlayhead = IMAAVPlayerContentPlayhead(avPlayer: self._player!) self._imaAdsManager.setUpAdsLoader() } - +#endif //Perform on next run loop, otherwise onVideoLoadStart is nil self.onVideoLoadStart?([ "src": [ @@ -424,7 +433,9 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH func setPaused(_ paused:Bool) { if paused { if _adPlaying { +#if USE_GOOGLE_IMA _imaAdsManager.getAdsManager()?.pause() +#endif } else { _player?.pause() _player?.rate = 0.0 @@ -433,7 +444,9 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH RCTPlayerOperations.configureAudio(ignoreSilentSwitch:_ignoreSilentSwitch, mixWithOthers:_mixWithOthers) if _adPlaying { +#if USE_GOOGLE_IMA _imaAdsManager.getAdsManager()?.resume() +#endif } else { if #available(iOS 10.0, *), !_automaticallyWaitsToMinimizeStalling { _player?.playImmediately(atRate: _rate) @@ -824,11 +837,11 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH func setAdTagUrl(_ adTagUrl:String!) { _adTagUrl = adTagUrl } - +#if USE_GOOGLE_IMA func getContentPlayhead() -> IMAAVPlayerContentPlayhead? { return _contentPlayhead } - +#endif func setAdPlaying(_ adPlaying:Bool) { _adPlaying = adPlaying } @@ -1106,11 +1119,11 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH @objc func handlePlayerItemDidReachEnd(notification:NSNotification!) { onVideoEnd?(["target": reactTag as Any]) - +#if USE_GOOGLE_IMA if notification.object as? AVPlayerItem == _player?.currentItem { _imaAdsManager.getAdsLoader()?.contentComplete() } - +#endif if _repeat { let item:AVPlayerItem! = notification.object as? AVPlayerItem item.seek(to: CMTime.zero, completionHandler: nil) diff --git a/react-native-video.podspec b/react-native-video.podspec index c0c4a86a..45710b3a 100644 --- a/react-native-video.podspec +++ b/react-native-video.podspec @@ -19,8 +19,15 @@ Pod::Spec.new do |s| ss.source_files = "ios/Video/**/*.{h,m,swift}" ss.dependency "PromisesSwift" - ss.ios.dependency 'GoogleAds-IMA-iOS-SDK', '~> 3.18.1' - ss.tvos.dependency 'GoogleAds-IMA-tvOS-SDK', '~> 4.2' + if defined?($RNVideoUseGoogleIMA) + Pod::UI.puts "RNVideo: enable IMA SDK" + + ss.ios.dependency 'GoogleAds-IMA-iOS-SDK', '~> 3.18.1' + ss.tvos.dependency 'GoogleAds-IMA-tvOS-SDK', '~> 4.2' + ss.pod_target_xcconfig = { + 'OTHER_SWIFT_FLAGS' => '$(inherited) -D USE_GOOGLE_IMA' + } + end end s.subspec "VideoCaching" do |ss|