├── DVVASTSample ├── Icon.png ├── DVVASTSample │ ├── en.lproj │ │ ├── InfoPlist.strings │ │ └── MainStoryboard.storyboard │ ├── DVAppDelegate.h │ ├── main.m │ ├── DVVASTSample-Prefix.pch │ ├── DVPlayerView.h │ ├── DVViewController.h │ ├── DVPlayerView.m │ ├── DVVASTSample-Info.plist │ ├── DVAppDelegate.m │ └── DVViewController.m ├── Icon-72.png ├── Icon@2x.png ├── Icon-72@2x.png ├── .gitignore ├── Default-568h@2x.png └── DVVASTSample.xcodeproj │ ├── project.xcworkspace │ └── contents.xcworkspacedata │ └── project.pbxproj ├── Podfile ├── DVVAST ├── Classes │ ├── DVVAST.pch │ ├── DVVideoAd.h │ ├── DVTimeIntervalFormatter.h │ ├── DVWrapperVideoAd.h │ ├── DVVideoAdServingTemplate.m │ ├── DVWrapperVideoAd.m │ ├── DVVideoMultipleAdPlaylist.h │ ├── DVVideoAd.m │ ├── DVVideoAdServingTemplate+Parsing.h │ ├── DVVideoAdServingTemplate.h │ ├── DVLog.h │ ├── DVInlineVideoAd.h │ ├── DVIABPlayer.h │ ├── DVVideoPlayBreak.h │ ├── DVTimeIntervalFormatter.m │ ├── DVInlineVideoAd.m │ ├── DVVideoPlayBreak.m │ ├── DVVideoMultipleAdPlaylist.m │ ├── DVVideoAdServingTemplate+Parsing.m │ └── DVIABPlayer.m ├── DVVAST.podspec └── LICENSE.txt ├── DVVASTSample.xcworkspace └── contents.xcworkspacedata ├── Podfile.lock ├── README.markdown ├── LICENSE └── .gitignore /DVVASTSample/Icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/denivip/ios-vast-player/HEAD/DVVASTSample/Icon.png -------------------------------------------------------------------------------- /DVVASTSample/DVVASTSample/en.lproj/InfoPlist.strings: -------------------------------------------------------------------------------- 1 | /* Localized versions of Info.plist keys */ 2 | 3 | -------------------------------------------------------------------------------- /DVVASTSample/Icon-72.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/denivip/ios-vast-player/HEAD/DVVASTSample/Icon-72.png -------------------------------------------------------------------------------- /DVVASTSample/Icon@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/denivip/ios-vast-player/HEAD/DVVASTSample/Icon@2x.png -------------------------------------------------------------------------------- /DVVASTSample/Icon-72@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/denivip/ios-vast-player/HEAD/DVVASTSample/Icon-72@2x.png -------------------------------------------------------------------------------- /DVVASTSample/.gitignore: -------------------------------------------------------------------------------- 1 | /DVVASTSample.xcodeproj/project.xcworkspace/xcuserdata/ 2 | /DVVASTSample.xcodeproj/xcuserdata/ 3 | -------------------------------------------------------------------------------- /DVVASTSample/Default-568h@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/denivip/ios-vast-player/HEAD/DVVASTSample/Default-568h@2x.png -------------------------------------------------------------------------------- /Podfile: -------------------------------------------------------------------------------- 1 | platform :ios, '5.0' 2 | xcodeproj 'DVVASTSample/DVVASTSample.xcodeproj' 3 | pod 'DVVAST', :local => 'DVVAST' 4 | -------------------------------------------------------------------------------- /DVVAST/Classes/DVVAST.pch: -------------------------------------------------------------------------------- 1 | // 2 | // Prefix header for all source files of the 'DVVASTSample' target in the 'DVVASTSample' project 3 | // 4 | 5 | #define VAST_LOG 1 6 | #import "DVLog.h" 7 | 8 | -------------------------------------------------------------------------------- /DVVASTSample.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /DVVASTSample/DVVASTSample.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Podfile.lock: -------------------------------------------------------------------------------- 1 | PODS: 2 | - DVVAST (0.0.1): 3 | - KissXML 4 | - KissXML (5.0) 5 | 6 | DEPENDENCIES: 7 | - DVVAST (from `DVVAST`) 8 | 9 | EXTERNAL SOURCES: 10 | DVVAST: 11 | :local: DVVAST 12 | 13 | SPEC CHECKSUMS: 14 | DVVAST: 280dbd585c631a95d326874e10a3d316f8ab7444 15 | KissXML: 8dfe0fb6d4e987fec63656ff07d5e697f49976f8 16 | 17 | COCOAPODS: 0.18.1 18 | -------------------------------------------------------------------------------- /DVVASTSample/DVVASTSample/DVAppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // DVAppDelegate.h 3 | // DVVASTSample 4 | // 5 | // Created by Nikolay Morev on 8/7/12. 6 | // Copyright (c) 2012 DENIVIP Media. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface DVAppDelegate : UIResponder 12 | 13 | @property (strong, nonatomic) UIWindow *window; 14 | 15 | @end 16 | -------------------------------------------------------------------------------- /DVVASTSample/DVVASTSample/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // DVVASTSample 4 | // 5 | // Created by Nikolay Morev on 8/7/12. 6 | // Copyright (c) 2012 DENIVIP Media. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | #import "DVAppDelegate.h" 12 | 13 | int main(int argc, char *argv[]) 14 | { 15 | @autoreleasepool { 16 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([DVAppDelegate class])); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /DVVAST/Classes/DVVideoAd.h: -------------------------------------------------------------------------------- 1 | // 2 | // DVVideoAd.h 3 | // DVVASTSample 4 | // 5 | // Created by Nikolay Morev on 8/7/12. 6 | // Copyright (c) 2012 DENIVIP Media. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | 12 | @interface DVVideoAd : NSObject 13 | 14 | @property (nonatomic, copy) NSString *identifier; 15 | @property BOOL playMediaFile; 16 | 17 | - (void)sendAsynchronousRequest:(NSURL*)url context:(NSString*)context; 18 | 19 | @end 20 | -------------------------------------------------------------------------------- /DVVAST/Classes/DVTimeIntervalFormatter.h: -------------------------------------------------------------------------------- 1 | // 2 | // DVTimeIntervalParser.h 3 | // DVVASTSample 4 | // 5 | // Created by Nikolay Morev on 8/8/12. 6 | // Copyright (c) 2012 DENIVIP Media. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | 12 | @interface DVTimeIntervalFormatter : NSObject 13 | 14 | - (NSTimeInterval)timeIntervalWithString:(NSString *)string; 15 | - (NSString *)stringWithTimeInterval:(NSTimeInterval)timeInterval; 16 | 17 | @end 18 | -------------------------------------------------------------------------------- /DVVASTSample/DVVASTSample/DVVASTSample-Prefix.pch: -------------------------------------------------------------------------------- 1 | // 2 | // Prefix header for all source files of the 'DVVASTSample' target in the 'DVVASTSample' project 3 | // 4 | 5 | #import 6 | 7 | #ifndef __IPHONE_5_0 8 | #warning "This project uses features only available in iOS SDK 5.0 and later." 9 | #endif 10 | 11 | #ifdef __OBJC__ 12 | #import 13 | #import 14 | #endif 15 | 16 | #define VAST_LOG 1 17 | #import "DVLog.h" -------------------------------------------------------------------------------- /DVVASTSample/DVVASTSample/DVPlayerView.h: -------------------------------------------------------------------------------- 1 | // 2 | // DVPlayerView.h 3 | // DVVASTSample 4 | // 5 | // Created by Nikolay Morev on 8/8/12. 6 | // Copyright (c) 2012 DENIVIP Media. All rights reserved. 7 | // 8 | 9 | #import 10 | #import 11 | 12 | 13 | @interface DVPlayerView : UIView 14 | 15 | @property (nonatomic, readonly, strong) AVPlayerLayer *playerLayer; 16 | @property (nonatomic, weak) NSString *videoGravity; 17 | 18 | @end 19 | -------------------------------------------------------------------------------- /DVVAST/Classes/DVWrapperVideoAd.h: -------------------------------------------------------------------------------- 1 | // 2 | // DVWrapperVideoAd.h 3 | // DVVASTSample 4 | // 5 | // Created by Manuel "StuFF mc" Carrasco Molina in 2013 — https://github.com/stuffmc/ios-vast-player/tree/dev 6 | // Copyright (c) 2012 DENIVIP Media. All rights reserved. 7 | // 8 | 9 | #import "DVInlineVideoAd.h" 10 | 11 | 12 | @class DVVideoPlayBreak; 13 | 14 | @interface DVWrapperVideoAd : DVInlineVideoAd 15 | 16 | @property (nonatomic, copy) NSURL *URL; 17 | @property (nonatomic, strong, readonly) DVVideoPlayBreak *videoPlayBreak; 18 | 19 | @end 20 | -------------------------------------------------------------------------------- /DVVAST/Classes/DVVideoAdServingTemplate.m: -------------------------------------------------------------------------------- 1 | // 2 | // DVVideoAdServingTemplate.m 3 | // DVVASTSample 4 | // 5 | // Created by Nikolay Morev on 8/7/12. Augmented by Manuel "StuFF mc" Carrasco Molina in 2013 — https://github.com/stuffmc/ios-vast-player/tree/dev 6 | // Copyright (c) 2012 DENIVIP Media. All rights reserved. 7 | // 8 | 9 | #import "DVVideoAdServingTemplate.h" 10 | 11 | 12 | NSString *const DVVideoAdServingTemplateErrorDomain = @"DVVideoAdServingTemplateErrorDomain"; 13 | 14 | @implementation DVVideoAdServingTemplate 15 | 16 | @synthesize ads = _ads; 17 | @synthesize document = _document; 18 | 19 | @end 20 | -------------------------------------------------------------------------------- /DVVASTSample/DVVASTSample/DVViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // DVViewController.h 3 | // DVVASTSample 4 | // 5 | // Created by Nikolay Morev on 8/7/12. 6 | // Copyright (c) 2012 DENIVIP Media. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "DVIABPlayer.h" 11 | 12 | 13 | @interface DVViewController : UIViewController 14 | 15 | @property (strong, nonatomic) IBOutlet UIView *playerView; 16 | @property (strong, nonatomic) IBOutlet UILabel *currentTimeLabel; 17 | @property (strong, nonatomic) IBOutlet UILabel *currentItemTitleLabel; 18 | 19 | @property (nonatomic, strong) DVIABPlayer *player; 20 | 21 | 22 | @end 23 | -------------------------------------------------------------------------------- /DVVAST/Classes/DVWrapperVideoAd.m: -------------------------------------------------------------------------------- 1 | // 2 | // DVWrapperVideoAd.m 3 | // DVVASTSample 4 | // 5 | // Created by Manuel "StuFF mc" Carrasco Molina in 2013 — https://github.com/stuffmc/ios-vast-player/tree/dev 6 | // Copyright (c) 2012 DENIVIP Media. All rights reserved. 7 | // 8 | 9 | #import "DVWrapperVideoAd.h" 10 | #import "DVVideoPlayBreak.h" 11 | 12 | 13 | @implementation DVWrapperVideoAd 14 | 15 | @synthesize URL = _URL; 16 | 17 | - (NSString *)description 18 | { 19 | return [NSString stringWithFormat:@"%@ id:%@ url:%@", [super description], 20 | self.identifier, self.URL]; 21 | } 22 | 23 | - (DVVideoPlayBreak *)videoPlayBreak 24 | { 25 | return [DVVideoPlayBreak playBreakAfterEndWithAdTemplateURL:self.URL]; 26 | } 27 | 28 | 29 | @end 30 | -------------------------------------------------------------------------------- /DVVAST/Classes/DVVideoMultipleAdPlaylist.h: -------------------------------------------------------------------------------- 1 | // 2 | // DVVideoMultipleAdPlaylist.h 3 | // DVVASTSample 4 | // 5 | // Created by Nikolay Morev on 8/7/12. 6 | // Copyright (c) 2012 DENIVIP Media. All rights reserved. 7 | // 8 | 9 | #import 10 | #import 11 | 12 | 13 | // http://www.iab.net/media/file/VMAPv1.0.pdf 14 | 15 | @interface DVVideoMultipleAdPlaylist : NSObject 16 | 17 | @property (nonatomic, copy) NSArray *playBreaks; 18 | 19 | - (NSArray *)midRollTimes; 20 | - (NSArray *)midRollPlayBreaksWithTime:(CMTime)time approximate:(BOOL)approximate; // approximation interval +-1sec 21 | - (NSArray *)midRollPlayBreaksWithTime:(CMTime)time; 22 | - (NSArray *)preRollPlayBreaks; 23 | - (NSArray *)postRollPlayBreaks; 24 | 25 | @end 26 | -------------------------------------------------------------------------------- /README.markdown: -------------------------------------------------------------------------------- 1 | iOS VAST library 2 | ================ 3 | 4 | This repo contains sample project and reusable classes that implement the 5 | following video advertising features, trying to follow IAB standards as close 6 | as possible: 7 | 8 | * Showing inline video ads during main content playback 9 | * Supports pre-roll, mid-roll with absolute time positions, post-roll 10 | * Download and parse basic VAST structure as provided by OpenX Source Ad Server 11 | Video Ad Plugin 12 | * Play breaks are set up using model classes that try to follow VMAP standard 13 | 14 | 15 | TODO 16 | ---- 17 | 18 | * More robust and full-featured VAST parser 19 | * Add support for other types of ads and play break positions 20 | * Implement VMAP parser 21 | * Implement NSCopying on model classes 22 | 23 | -------------------------------------------------------------------------------- /DVVAST/Classes/DVVideoAd.m: -------------------------------------------------------------------------------- 1 | // 2 | // DVVideoAd.m 3 | // DVVASTSample 4 | // 5 | // Created by Nikolay Morev on 8/7/12. 6 | // Copyright (c) 2012 DENIVIP Media. All rights reserved. 7 | // 8 | 9 | #import "DVVideoAd.h" 10 | 11 | 12 | @implementation DVVideoAd 13 | 14 | @synthesize identifier = _identifier; 15 | 16 | - (void)sendAsynchronousRequest:(NSURL*)url context:(NSString*)context 17 | { 18 | if (url) { 19 | NSURLRequest *request = [NSURLRequest requestWithURL:url]; 20 | [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { 21 | VLogV(context); 22 | VLogV(response.URL); 23 | if (error) { 24 | VLogV(error); 25 | } 26 | }]; 27 | } 28 | } 29 | 30 | @end 31 | -------------------------------------------------------------------------------- /DVVAST/DVVAST.podspec: -------------------------------------------------------------------------------- 1 | Pod::Spec.new do |s| 2 | s.name = "DVVAST" 3 | s.version = "0.0.1" 4 | s.summary = "IAB VAST ads playback in iOS AVPlayer." 5 | s.homepage = "https://github.com/denivip/ios-vast-player" 6 | s.license = 'MIT' 7 | s.authors = { "Nikolay Morev" => "kolyuchiy@gmail.com", "StuFF mc" => "mc@stuffmc.com" } 8 | s.source = { :git => "https://github.com/denivip/ios-vast-player.git", :tag => "v0.0.1" } 9 | s.platform = :ios, '5.0' 10 | s.source_files = 'Classes', 'Classes/**/*.{h,m}' 11 | s.prefix_header_file = 'Classes/DVVAST.pch' 12 | s.frameworks = 'Foundation', 'AVFoundation', 'AudioToolbox', 'CoreMedia', 'CoreGraphics', 'UIKit' 13 | s.library = 'xml2' 14 | s.requires_arc = true 15 | s.xcconfig = { 'HEADER_SEARCH_PATHS' => '$(SDKROOT)/usr/include/libxml2' } 16 | s.dependency 'KissXML' 17 | end 18 | -------------------------------------------------------------------------------- /DVVAST/Classes/DVVideoAdServingTemplate+Parsing.h: -------------------------------------------------------------------------------- 1 | // 2 | // DVVideoAdServingTemplate+Parsing.h 3 | // DVVASTSample 4 | // 5 | // Created by Nikolay Morev on 8/7/12. Augmented by Manuel "StuFF mc" Carrasco Molina in 2013 — https://github.com/stuffmc/ios-vast-player/tree/dev 6 | // Copyright (c) 2012 DENIVIP Media. All rights reserved. 7 | // 8 | 9 | #import "DVVideoAdServingTemplate.h" 10 | #import 11 | #import "DVInlineVideoAd.h" 12 | 13 | @interface DDXMLElement (VAST) 14 | 15 | @property (nonatomic, readonly) BOOL isEmpty; 16 | 17 | @end 18 | 19 | @interface DVVideoAdServingTemplate (Parsing) 20 | 21 | - (id)initWithData:(NSData *)data error:(NSError **)error; 22 | - (id)initWithXMLDocument:(DDXMLDocument *)document error:(NSError **)error; 23 | - (BOOL)populateInlineVideoAd:(DVInlineVideoAd *)videoAd withXMLElement:(DDXMLElement *)element error:(NSError **)error; 24 | 25 | @end 26 | -------------------------------------------------------------------------------- /DVVAST/Classes/DVVideoAdServingTemplate.h: -------------------------------------------------------------------------------- 1 | // 2 | // DVVideoAdServingTemplate.h 3 | // DVVASTSample 4 | // 5 | // Created by Nikolay Morev on 8/7/12. Augmented by Manuel "StuFF mc" Carrasco Molina in 2013 — https://github.com/stuffmc/ios-vast-player/tree/dev 6 | // Copyright (c) 2012 DENIVIP Media. All rights reserved. 7 | // 8 | 9 | #import 10 | #import 11 | 12 | 13 | // http://www.iab.net/media/file/VASTv3.0.pdf 14 | 15 | extern NSString *const DVVideoAdServingTemplateErrorDomain; 16 | 17 | enum { 18 | 19 | DVVideoAdServingTemplateXMLParsingErrorCode = 100, 20 | DVVideoAdServingTemplateSchemaValidationErrorCode = 101, 21 | DVVideoAdServingTemplateUnexpectedAdType = 200, 22 | DVVideoAdServingTemplateUndefinedErrorCode = 900, 23 | 24 | } DVVideoAdServingTemplateErrorCode; 25 | 26 | @interface DVVideoAdServingTemplate : NSObject 27 | 28 | @property (nonatomic, copy) NSArray *ads; 29 | @property (strong, nonatomic) DDXMLDocument *document; 30 | 31 | @end 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2012-2014 DENIVIP Group OOO, Nikolay Morev 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /DVVAST/LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright 2012-2014 DENIVIP Group OOO, Nikolay Morev 2 | https://github.com/denivip/ios-vast-player 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining 5 | a copy of this software and associated documentation files (the 6 | "Software"), to deal in the Software without restriction, including 7 | without limitation the rights to use, copy, modify, merge, publish, 8 | distribute, sublicense, and/or sell copies of the Software, and to 9 | permit persons to whom the Software is furnished to do so, subject to 10 | the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be 13 | included in all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 19 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 20 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 21 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /DVVAST/Classes/DVLog.h: -------------------------------------------------------------------------------- 1 | // 2 | // DVLog.h 3 | // DVVASTSample 4 | // 5 | // Created by Manuel "StuFF mc" Carrasco Molina on 21.02.13. 6 | // Copyright (c) 2013 DENIVIP Media. All rights reserved. 7 | // 8 | 9 | #if defined DEBUG && defined VAST_LOG && VAST_LOG 10 | #define VLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__); 11 | #define VLogC() VLog(@""); 12 | #define VLogV(var) NSLog(@"%s [Line %d] <%p> " #var ": %@", __PRETTY_FUNCTION__, __LINE__, self, var) 13 | #define VLogR(rect) VLogV(NSStringFromRect(rect)) 14 | #define VLogS(size) VLogV(NSStringFromSize(size)) 15 | #define VLogI(var) NSLog(@"%s [Line %d] " #var ": %d", __PRETTY_FUNCTION__, __LINE__, var) 16 | #define VLogF(var) NSLog(@"%s [Line %d] " #var ": %f", __PRETTY_FUNCTION__, __LINE__, var) 17 | #define VLogB(var) NSLog(@"%s [Line %d] " #var ": %@", __PRETTY_FUNCTION__, __LINE__, var ? @"YES" : @"NO") 18 | #else 19 | #define VLog(...) 20 | #define VLogC() 21 | #define VLogV(var) 22 | #define VLogR(rect) 23 | #define VLogS(size) 24 | #define VLogI(var) 25 | #define VLogF(var) 26 | #define VLogB(var) 27 | #endif -------------------------------------------------------------------------------- /DVVAST/Classes/DVInlineVideoAd.h: -------------------------------------------------------------------------------- 1 | // 2 | // DVInlineVideoAd.h 3 | // DVVASTSample 4 | // 5 | // Created by Nikolay Morev on 8/7/12. Augmented by Manuel "StuFF mc" Carrasco Molina in 2013 — https://github.com/stuffmc/ios-vast-player/tree/dev 6 | // Copyright (c) 2012 DENIVIP Media. All rights reserved. 7 | // 8 | 9 | #import "DVVideoAd.h" 10 | 11 | 12 | @interface DVInlineVideoAd : DVVideoAd 13 | 14 | @property (nonatomic, copy) NSString *system; 15 | @property (nonatomic, copy) NSString *title; 16 | @property (nonatomic, copy) NSArray *impressionURLs; // Multiple elements 17 | @property (nonatomic, copy) NSURL *impressionURL; // For compatibility sake (code using ios-vast-player's "single" impressionURL) 18 | @property (nonatomic, copy) NSURL *clickThroughURL; 19 | @property (nonatomic, copy) NSArray *clickTrackingURLs; // Multiple elements 20 | @property (nonatomic, copy) NSURL *clickTrackingURL; // For compatibility sake (code using ios-vast-player's "single" clickTrackingURL) 21 | @property (nonatomic, copy) NSDictionary *trackingEvents; 22 | 23 | @property (nonatomic) NSTimeInterval duration; 24 | @property (nonatomic, copy) NSURL *mediaFileURL; 25 | 26 | - (void)trackImpressions; 27 | - (void)trackEvent:(NSString*)event; 28 | 29 | @end 30 | -------------------------------------------------------------------------------- /DVVAST/Classes/DVIABPlayer.h: -------------------------------------------------------------------------------- 1 | // 2 | // DVVASTPlayer.h 3 | // DVVASTSample 4 | // 5 | // Created by Nikolay Morev on 8/7/12. 6 | // Copyright (c) 2012 DENIVIP Media. All rights reserved. 7 | // 8 | 9 | #import 10 | #import 11 | #import "DVVideoMultipleAdPlaylist.h" 12 | #import "DVVideoPlayBreak.h" 13 | #import "DVVideoAd.h" 14 | 15 | extern NSString *const DVIABPlayerErrorDomain; 16 | 17 | enum { 18 | 19 | DVIABPlayerInvalidAdTemplateURLErrorCode = 1, 20 | 21 | } DVIABPlayerErrorCode; 22 | 23 | 24 | @class DVIABPlayer; 25 | 26 | @protocol DVIABPlayerDelegate 27 | 28 | @optional 29 | 30 | - (BOOL)player:(DVIABPlayer *)player shouldPauseForAdBreak:(DVVideoPlayBreak *)playBreak; 31 | - (void)player:(DVIABPlayer *)player didFailPlayBreak:(DVVideoPlayBreak *)playBreak withError:(NSError *)error; 32 | 33 | @end 34 | 35 | 36 | @interface DVIABPlayer : AVPlayer 37 | 38 | @property (nonatomic, strong) AVPlayerLayer *playerLayer; 39 | @property (nonatomic, weak) id delegate; 40 | @property (nonatomic, strong) AVPlayerItem *contentPlayerItem; // main content player item as opposed to advertisement player items 41 | @property (nonatomic, strong) DVVideoMultipleAdPlaylist *adPlaylist; 42 | @property (nonatomic, strong) NSDictionary *httpHeaders; 43 | @property (nonatomic, strong, readonly) DVVideoAd *currentAd; 44 | 45 | - (void)finishPlayBreaksQueue; 46 | 47 | @end 48 | -------------------------------------------------------------------------------- /DVVASTSample/DVVASTSample/DVPlayerView.m: -------------------------------------------------------------------------------- 1 | // 2 | // DVPlayerView.m 3 | // DVVASTSample 4 | // 5 | // Created by Nikolay Morev on 8/8/12. 6 | // Copyright (c) 2012 DENIVIP Media. All rights reserved. 7 | // 8 | 9 | #import "DVPlayerView.h" 10 | 11 | 12 | @implementation DVPlayerView 13 | 14 | + (Class)layerClass 15 | { 16 | return [AVPlayerLayer class]; 17 | } 18 | 19 | - (AVPlayerLayer *)playerLayer 20 | { 21 | return (AVPlayerLayer *)self.layer; 22 | } 23 | 24 | - (void)setup 25 | { 26 | UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideButtonDoubleTapped:)]; 27 | doubleTap.numberOfTapsRequired = 2; 28 | [self addGestureRecognizer:doubleTap]; 29 | } 30 | 31 | - (id)initWithCoder:(NSCoder *)aDecoder 32 | { 33 | if (self = [super initWithCoder:aDecoder]) { 34 | [self setup]; 35 | } 36 | return self; 37 | } 38 | 39 | @synthesize videoGravity = _videoGravity; 40 | 41 | - (NSString *)videoGravity 42 | { 43 | return self.playerLayer.videoGravity; 44 | } 45 | 46 | - (void)setVideoGravity:(NSString *)gravity 47 | { 48 | self.playerLayer.videoGravity = gravity; 49 | self.playerLayer.frame = self.playerLayer.frame; // workaround iOS5 bug 50 | } 51 | 52 | - (void)hideButtonDoubleTapped:(UITapGestureRecognizer *)tap 53 | { 54 | if ([self.videoGravity isEqualToString:AVLayerVideoGravityResizeAspect]) { 55 | self.videoGravity = AVLayerVideoGravityResizeAspectFill; 56 | } 57 | else { 58 | self.videoGravity = AVLayerVideoGravityResizeAspect; 59 | } 60 | } 61 | 62 | @end 63 | -------------------------------------------------------------------------------- /DVVAST/Classes/DVVideoPlayBreak.h: -------------------------------------------------------------------------------- 1 | // 2 | // DVVideoPlayBreak.h 3 | // DVVASTSample 4 | // 5 | // Created by Nikolay Morev on 8/7/12. 6 | // Copyright (c) 2012 DENIVIP Media. All rights reserved. 7 | // 8 | 9 | #import 10 | #import 11 | #import "DVVideoAdServingTemplate.h" 12 | 13 | 14 | // http://www.iab.net/media/file/VMAPv1.0.pdf 15 | 16 | typedef enum { 17 | 18 | DVVideoPlayBreakTimeOffsetFromStart = 1, 19 | DVVideoPlayBreakTimeOffsetPercentage, 20 | DVVideoPlayBreakTimeOffsetStart, 21 | DVVideoPlayBreakTimeOffsetEnd, 22 | DVVideoPlayBreakTimeOffsetPositional, 23 | 24 | } DVVideoPlayBreakTimeOffsetType; 25 | 26 | typedef enum { 27 | 28 | DVVideoPlayBreakLinear = 1 << 0, 29 | DVVideoPlayBreakNonLinear = 1 << 1, 30 | DVVideoPlayBreakDisplay = 1 << 2, 31 | 32 | } DVVideoPlayBreakType; 33 | 34 | 35 | @interface DVVideoPlayBreak : NSObject 36 | 37 | @property (nonatomic) DVVideoPlayBreakTimeOffsetType timeOffsetType; 38 | @property (nonatomic) CMTime timeFromStart; 39 | @property (nonatomic) NSUInteger percentage; 40 | @property (nonatomic) NSUInteger position; 41 | @property (nonatomic) CMTime repeatAfterTime; 42 | 43 | @property (nonatomic) DVVideoPlayBreakType types; 44 | @property (nonatomic, copy) NSString *identifier; 45 | @property (nonatomic, copy) NSURL *adServingTemplateURL; 46 | 47 | + (id)playBreakBeforeStartWithAdTemplateURL:(NSURL *)adTemplateURL; 48 | + (id)playBreakAfterEndWithAdTemplateURL:(NSURL *)adTemplateURL; 49 | + (id)playBreakAtTimeFromStart:(CMTime)timeFromStart withAdTemplateURL:(NSURL *)adTemplateURL; 50 | 51 | @end 52 | -------------------------------------------------------------------------------- /DVVAST/Classes/DVTimeIntervalFormatter.m: -------------------------------------------------------------------------------- 1 | // 2 | // DVTimeIntervalParser.m 3 | // DVVASTSample 4 | // 5 | // Created by Nikolay Morev on 8/8/12. 6 | // Copyright (c) 2012 DENIVIP Media. All rights reserved. 7 | // 8 | 9 | #import "DVTimeIntervalFormatter.h" 10 | 11 | 12 | @implementation DVTimeIntervalFormatter 13 | 14 | - (NSTimeInterval)timeIntervalWithString:(NSString *)string 15 | { 16 | NSScanner *scanner = [NSScanner scannerWithString:string]; 17 | 18 | NSInteger hours = 0; 19 | NSInteger minutes = 0; 20 | NSInteger seconds = 0; 21 | NSInteger milliseconds = 0; 22 | 23 | if (! [scanner scanInteger:&hours]) { 24 | return NAN; 25 | } 26 | 27 | if (! [scanner scanCharactersFromSet:[NSCharacterSet characterSetWithCharactersInString:@":"] intoString:nil]) { 28 | return NAN; 29 | } 30 | 31 | if (! [scanner scanInteger:&minutes]) { 32 | return NAN; 33 | } 34 | 35 | if (! [scanner scanCharactersFromSet:[NSCharacterSet characterSetWithCharactersInString:@":"] intoString:nil]) { 36 | return NAN; 37 | } 38 | 39 | if (! [scanner scanInteger:&seconds]) { 40 | return NAN; 41 | } 42 | 43 | if ([scanner scanCharactersFromSet:[NSCharacterSet characterSetWithCharactersInString:@"."] intoString:nil]) { 44 | [scanner scanInteger:&milliseconds]; 45 | } 46 | 47 | return hours * 60 * 60 + minutes * 60 + seconds + (CGFloat)milliseconds / 1000.f; 48 | } 49 | 50 | - (NSString *)stringWithTimeInterval:(NSTimeInterval)totalSeconds 51 | { 52 | NSInteger hours = totalSeconds / (60 * 60); 53 | NSInteger minutes = (totalSeconds - hours * 60 * 60) / 60; 54 | CGFloat seconds = MAX(0, (totalSeconds - hours * 60 * 60 - minutes * 60)); 55 | return [NSString stringWithFormat:@"%02li:%02li:%06.3f", (long)hours, (long)minutes, seconds]; 56 | } 57 | 58 | @end 59 | -------------------------------------------------------------------------------- /DVVAST/Classes/DVInlineVideoAd.m: -------------------------------------------------------------------------------- 1 | // 2 | // DVInlineVideoAd.m 3 | // DVVASTSample 4 | // 5 | // Created by Nikolay Morev on 8/7/12. Augmented by Manuel "StuFF mc" Carrasco Molina in 2013 — https://github.com/stuffmc/ios-vast-player/tree/dev 6 | // Copyright (c) 2012 DENIVIP Media. All rights reserved. 7 | // 8 | 9 | #import "DVInlineVideoAd.h" 10 | 11 | 12 | @implementation DVInlineVideoAd 13 | 14 | @synthesize system = _system; 15 | @synthesize title = _title; 16 | @synthesize impressionURL = _impressionURL; 17 | @synthesize impressionURLs = _impressionsURL; 18 | @synthesize clickThroughURL = _clickThroughURL; 19 | @synthesize clickTrackingURL = _clickTrackingURL; 20 | @synthesize trackingEvents = _trackingEvents; 21 | @synthesize duration = _duration; 22 | @synthesize mediaFileURL = _mediaFileURL; 23 | 24 | - (NSString *)description 25 | { 26 | return [NSString stringWithFormat:@"%@ id:%@ url:%@ play:%d", [super description], 27 | self.identifier, self.mediaFileURL, self.playMediaFile]; 28 | } 29 | 30 | - (void)trackImpressions 31 | { 32 | // Trigger those babies, async of course! 33 | VLogV(self.impressionURLs); 34 | [self.impressionURLs enumerateObjectsUsingBlock:^(NSURL *url, NSUInteger idx, BOOL *stop) { 35 | VLogV(url); 36 | [self sendAsynchronousRequest:url context:@"trackImpressions"]; 37 | }]; 38 | } 39 | 40 | - (void)trackEvent:(NSString*)event 41 | { 42 | if ([self.trackingEvents[event] isKindOfClass:[NSDictionary class]]) { // Should be. 43 | NSDictionary *dictionary = (NSDictionary*)self.trackingEvents[event]; 44 | [dictionary enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSURL *url, BOOL *stop) { 45 | VLogV(key); 46 | VLogV(url); 47 | NSString *context = [NSString stringWithFormat:@"trackEvent: %@ (%@)", event, key]; 48 | [self sendAsynchronousRequest:url context:context]; 49 | }]; 50 | } 51 | } 52 | 53 | @end 54 | -------------------------------------------------------------------------------- /DVVASTSample/DVVASTSample/DVVASTSample-Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleDisplayName 8 | VAST Sample 9 | CFBundleExecutable 10 | ${EXECUTABLE_NAME} 11 | CFBundleIcons 12 | 13 | CFBundlePrimaryIcon 14 | 15 | CFBundleIconFiles 16 | 17 | Icon.png 18 | Icon@2x.png 19 | Icon-72.png 20 | Icon-72@2x.png 21 | 22 | UIPrerenderedIcon 23 | 24 | 25 | 26 | CFBundleIdentifier 27 | ru.denivip.${PRODUCT_NAME:rfc1034identifier} 28 | CFBundleInfoDictionaryVersion 29 | 6.0 30 | CFBundleName 31 | ${PRODUCT_NAME} 32 | CFBundlePackageType 33 | APPL 34 | CFBundleShortVersionString 35 | 1.0 36 | CFBundleSignature 37 | ???? 38 | CFBundleVersion 39 | 1.0 40 | LSRequiresIPhoneOS 41 | 42 | UIMainStoryboardFile 43 | MainStoryboard 44 | UIPrerenderedIcon 45 | 46 | UIRequiredDeviceCapabilities 47 | 48 | armv7 49 | 50 | UIStatusBarStyle 51 | UIStatusBarStyleBlackTranslucent 52 | UISupportedInterfaceOrientations 53 | 54 | UIInterfaceOrientationPortrait 55 | UIInterfaceOrientationLandscapeLeft 56 | UIInterfaceOrientationLandscapeRight 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /DVVAST/Classes/DVVideoPlayBreak.m: -------------------------------------------------------------------------------- 1 | // 2 | // DVVideoPlayBreak.m 3 | // DVVASTSample 4 | // 5 | // Created by Nikolay Morev on 8/7/12. 6 | // Copyright (c) 2012 DENIVIP Media. All rights reserved. 7 | // 8 | 9 | #import "DVVideoPlayBreak.h" 10 | 11 | 12 | @implementation DVVideoPlayBreak 13 | 14 | @synthesize timeOffsetType = _timeOffsetType; 15 | @synthesize timeFromStart = _timeFromStart; 16 | @synthesize percentage = _percentage; 17 | @synthesize position = _position; 18 | @synthesize types = _types; 19 | @synthesize adServingTemplateURL = _adServingTemplateURL; 20 | @synthesize identifier = _identifier; 21 | 22 | + (id)playBreakBeforeStartWithAdTemplateURL:(NSURL *)adTemplateURL 23 | { 24 | DVVideoPlayBreak *obj = [[[self class] alloc] init]; 25 | if (obj) { 26 | obj.timeOffsetType = DVVideoPlayBreakTimeOffsetStart; 27 | obj.adServingTemplateURL = adTemplateURL; 28 | } 29 | return obj; 30 | } 31 | 32 | + (id)playBreakAfterEndWithAdTemplateURL:(NSURL *)adTemplateURL 33 | { 34 | DVVideoPlayBreak *obj = [[[self class] alloc] init]; 35 | if (obj) { 36 | obj.timeOffsetType = DVVideoPlayBreakTimeOffsetEnd; 37 | obj.adServingTemplateURL = adTemplateURL; 38 | } 39 | return obj; 40 | } 41 | 42 | + (id)playBreakAtTimeFromStart:(CMTime)timeFromStart withAdTemplateURL:(NSURL *)adTemplateURL 43 | { 44 | DVVideoPlayBreak *obj = [[[self class] alloc] init]; 45 | if (obj) { 46 | obj.timeOffsetType = DVVideoPlayBreakTimeOffsetFromStart; 47 | obj.timeFromStart = timeFromStart; 48 | obj.adServingTemplateURL = adTemplateURL; 49 | } 50 | return obj; 51 | } 52 | 53 | - (NSString *)description 54 | { 55 | NSString *defaultDescription = [super description]; 56 | 57 | NSString *time = nil; 58 | switch (self.timeOffsetType) { 59 | case DVVideoPlayBreakTimeOffsetStart: 60 | time = @"pre-roll"; 61 | break; 62 | case DVVideoPlayBreakTimeOffsetEnd: 63 | time = @"post-roll"; 64 | break; 65 | case DVVideoPlayBreakTimeOffsetFromStart: 66 | { 67 | CFStringRef timeDescription = CMTimeCopyDescription(nil, self.timeFromStart); 68 | time = [NSString stringWithFormat:@"@%@", timeDescription]; 69 | CFRelease(timeDescription); 70 | } 71 | break; 72 | 73 | case DVVideoPlayBreakTimeOffsetPercentage: 74 | case DVVideoPlayBreakTimeOffsetPositional: 75 | NSAssert(NO, @"Not supported"); 76 | break; 77 | } 78 | 79 | return [NSString stringWithFormat:@"%@ %@ %@", defaultDescription, time, self.adServingTemplateURL]; 80 | } 81 | 82 | @end 83 | -------------------------------------------------------------------------------- /DVVAST/Classes/DVVideoMultipleAdPlaylist.m: -------------------------------------------------------------------------------- 1 | // 2 | // DVVideoMultipleAdPlaylist.m 3 | // DVVASTSample 4 | // 5 | // Created by Nikolay Morev on 8/7/12. 6 | // Copyright (c) 2012 DENIVIP Media. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "DVVideoMultipleAdPlaylist.h" 11 | #import "DVVideoPlayBreak.h" 12 | 13 | 14 | @implementation DVVideoMultipleAdPlaylist 15 | 16 | @synthesize playBreaks = _playBreaks; 17 | 18 | - (NSArray *)midRollTimes 19 | { 20 | NSMutableArray *times = [NSMutableArray arrayWithCapacity:[self.playBreaks count]]; 21 | 22 | [self.playBreaks enumerateObjectsUsingBlock:^(DVVideoPlayBreak *brk, NSUInteger idx, BOOL *stop) { 23 | switch (brk.timeOffsetType) { 24 | case DVVideoPlayBreakTimeOffsetFromStart: 25 | [times addObject:[NSValue valueWithCMTime:brk.timeFromStart]]; 26 | break; 27 | 28 | case DVVideoPlayBreakTimeOffsetStart: 29 | case DVVideoPlayBreakTimeOffsetEnd: 30 | // use other methods to test for pre-rolls and post-rolls 31 | break; 32 | 33 | case DVVideoPlayBreakTimeOffsetPercentage: 34 | case DVVideoPlayBreakTimeOffsetPositional: 35 | NSAssert(NO, @"Not implemented"); 36 | break; 37 | } 38 | }]; 39 | 40 | return [NSArray arrayWithArray:times]; 41 | } 42 | 43 | - (NSArray *)midRollPlayBreaksWithTime:(CMTime)time approximate:(BOOL)approximate 44 | { 45 | NSMutableArray *playBreaks = [NSMutableArray array]; 46 | 47 | [self.playBreaks enumerateObjectsUsingBlock:^(DVVideoPlayBreak *brk, NSUInteger idx, BOOL *stop) { 48 | if (brk.timeOffsetType == DVVideoPlayBreakTimeOffsetFromStart && 49 | ((!approximate && CMTimeCompare(brk.timeFromStart, time) == 0) || 50 | (approximate && CMTimeCompare(CMTimeAbsoluteValue(CMTimeSubtract(brk.timeFromStart, time)), 51 | CMTimeMake(1, 1)) == -1))) { 52 | [playBreaks addObject:brk]; 53 | } 54 | }]; 55 | 56 | return [NSArray arrayWithArray:playBreaks]; 57 | } 58 | 59 | - (NSArray *)midRollPlayBreaksWithTime:(CMTime)time 60 | { 61 | return [self midRollPlayBreaksWithTime:time approximate:NO]; 62 | } 63 | 64 | - (NSArray *)preRollPlayBreaks 65 | { 66 | NSMutableArray *playBreaks = [NSMutableArray array]; 67 | 68 | [self.playBreaks enumerateObjectsUsingBlock:^(DVVideoPlayBreak *brk, NSUInteger idx, BOOL *stop) { 69 | if (brk.timeOffsetType == DVVideoPlayBreakTimeOffsetStart) { 70 | [playBreaks addObject:brk]; 71 | } 72 | }]; 73 | 74 | return [NSArray arrayWithArray:playBreaks]; 75 | } 76 | 77 | - (NSArray *)postRollPlayBreaks 78 | { 79 | NSMutableArray *playBreaks = [NSMutableArray array]; 80 | 81 | [self.playBreaks enumerateObjectsUsingBlock:^(DVVideoPlayBreak *brk, NSUInteger idx, BOOL *stop) { 82 | if (brk.timeOffsetType == DVVideoPlayBreakTimeOffsetEnd) { 83 | [playBreaks addObject:brk]; 84 | } 85 | }]; 86 | 87 | return [NSArray arrayWithArray:playBreaks]; 88 | } 89 | 90 | @end 91 | -------------------------------------------------------------------------------- /DVVASTSample/DVVASTSample/DVAppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // DVAppDelegate.m 3 | // DVVASTSample 4 | // 5 | // Created by Nikolay Morev on 8/7/12. 6 | // Copyright (c) 2012 DENIVIP Media. All rights reserved. 7 | // 8 | 9 | #import "DVAppDelegate.h" 10 | #import 11 | #import 12 | 13 | 14 | static void MyPropertyListener(void *inClientData, 15 | AudioSessionPropertyID inID, 16 | UInt32 inDataSize, 17 | const void *inData) 18 | { 19 | NSLog(@"kAudioSessionProperty_ServerDied"); 20 | } 21 | 22 | 23 | @implementation DVAppDelegate 24 | 25 | void uncaughtExceptionHandler(NSException *exception) { 26 | NSLog(@"CRASH: %@", exception); 27 | NSLog(@"Stack Trace: %@", [exception callStackSymbols]); 28 | // Internal error reporting 29 | } 30 | 31 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 32 | { 33 | NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler); 34 | AVAudioSession *audioSession = [AVAudioSession sharedInstance]; // to implicitly initialize 35 | [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil]; 36 | 37 | OSStatus err = AudioSessionAddPropertyListener(kAudioSessionProperty_ServerDied, 38 | MyPropertyListener, 39 | (__bridge void *)(self)); 40 | NSAssert(err == noErr, @"AudioSessionAddPropertyListener error %li", err); 41 | 42 | return YES; 43 | } 44 | 45 | - (void)applicationWillResignActive:(UIApplication *)application 46 | { 47 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 48 | // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 49 | } 50 | 51 | - (void)applicationDidEnterBackground:(UIApplication *)application 52 | { 53 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 54 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 55 | } 56 | 57 | - (void)applicationWillEnterForeground:(UIApplication *)application 58 | { 59 | // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 60 | } 61 | 62 | - (void)applicationDidBecomeActive:(UIApplication *)application 63 | { 64 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 65 | } 66 | 67 | - (void)applicationWillTerminate:(UIApplication *)application 68 | { 69 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 70 | } 71 | 72 | @end 73 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ######################### 2 | # .gitignore file for Xcode4 / OS X Source projects 3 | # 4 | # Version 2.0 5 | # For latest version, see: http://stackoverflow.com/questions/49478/git-ignore-file-for-xcode-projects 6 | # 7 | # 2013 updates: 8 | # - fixed the broken "save personal Schemes" 9 | # 10 | # NB: if you are storing "built" products, this WILL NOT WORK, 11 | # and you should use a different .gitignore (or none at all) 12 | # This file is for SOURCE projects, where there are many extra 13 | # files that we want to exclude 14 | # 15 | ######################### 16 | 17 | ##### 18 | # OS X temporary files that should never be committed 19 | 20 | .DS_Store 21 | *.swp 22 | *.lock 23 | profile 24 | 25 | 26 | #### 27 | # Xcode temporary files that should never be committed 28 | # 29 | # NB: NIB/XIB files still exist even on Storyboard projects, so we want this... 30 | 31 | *~.nib 32 | 33 | 34 | #### 35 | # Xcode build files - 36 | # 37 | # NB: slash on the end, so we only remove the FOLDER, not any files that were badly named "DerivedData" 38 | 39 | DerivedData/ 40 | 41 | # NB: slash on the end, so we only remove the FOLDER, not any files that were badly named "build" 42 | 43 | build/ 44 | 45 | 46 | ##### 47 | # Xcode private settings (window sizes, bookmarks, breakpoints, custom executables, smart groups) 48 | # 49 | # This is complicated: 50 | # 51 | # SOMETIMES you need to put this file in version control. 52 | # Apple designed it poorly - if you use "custom executables", they are 53 | # saved in this file. 54 | # 99% of projects do NOT use those, so they do NOT want to version control this file. 55 | # ..but if you're in the 1%, comment out the line "*.pbxuser" 56 | 57 | *.pbxuser 58 | *.mode1v3 59 | *.mode2v3 60 | *.perspectivev3 61 | # NB: also, whitelist the default ones, some projects need to use these 62 | !default.pbxuser 63 | !default.mode1v3 64 | !default.mode2v3 65 | !default.perspectivev3 66 | 67 | 68 | #### 69 | # Xcode 4 - semi-personal settings 70 | # 71 | # 72 | # OPTION 1: --------------------------------- 73 | # throw away ALL personal settings (including custom schemes! 74 | # - unless they are "shared") 75 | # 76 | # NB: this is exclusive with OPTION 2 below 77 | xcuserdata 78 | 79 | # OPTION 2: --------------------------------- 80 | # get rid of ALL personal settings, but KEEP SOME OF THEM 81 | # - NB: you must manually uncomment the bits you want to keep 82 | # 83 | # NB: this is exclusive with OPTION 1 above 84 | # 85 | #xcuserdata/**/* 86 | 87 | # (requires option 2 above): Personal Schemes 88 | # 89 | #!xcuserdata/**/xcschemes/* 90 | 91 | #### 92 | # XCode 4 workspaces - more detailed 93 | # 94 | # Workspaces are important! They are a core feature of Xcode - don't exclude them :) 95 | # 96 | # Workspace layout is quite spammy. For reference: 97 | # 98 | # /(root)/ 99 | # /(project-name).xcodeproj/ 100 | # project.pbxproj 101 | # /project.xcworkspace/ 102 | # contents.xcworkspacedata 103 | # /xcuserdata/ 104 | # /(your name)/xcuserdatad/ 105 | # UserInterfaceState.xcuserstate 106 | # /xcsshareddata/ 107 | # /xcschemes/ 108 | # (shared scheme name).xcscheme 109 | # /xcuserdata/ 110 | # /(your name)/xcuserdatad/ 111 | # (private scheme).xcscheme 112 | # xcschememanagement.plist 113 | # 114 | # 115 | 116 | #### 117 | # Xcode 4 - Deprecated classes 118 | # 119 | # Allegedly, if you manually "deprecate" your classes, they get moved here. 120 | # 121 | # We're using source-control, so this is a "feature" that we do not want! 122 | 123 | *.moved-aside 124 | 125 | 126 | #### 127 | # UNKNOWN: recommended by others, but I can't discover what these files are 128 | # 129 | # ...none. Everything is now explained. 130 | /Videos 131 | /App Icon Template \[2.0\] 132 | /Pods 133 | !/Podfile.lock 134 | -------------------------------------------------------------------------------- /DVVASTSample/DVVASTSample/DVViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // DVViewController.m 3 | // DVVASTSample 4 | // 5 | // Created by Nikolay Morev on 8/7/12. 6 | // Copyright (c) 2012 DENIVIP Media. All rights reserved. 7 | // 8 | 9 | #import "DVViewController.h" 10 | #import 11 | #import "DVVideoPlayBreak.h" 12 | #import "DVTimeIntervalFormatter.h" 13 | #import "DVVideoMultipleAdPlaylist.h" 14 | 15 | 16 | static void *DVViewControllerCurrentPlayerItemObservationContext = &DVViewControllerCurrentPlayerItemObservationContext; 17 | static void *DVViewControllerPlayerItemStatusObservationContext = &DVViewControllerPlayerItemStatusObservationContext; 18 | 19 | #define OPENX_AD_TAG_WITH_ZONE(ZONE_ID) ([NSURL URLWithString:[NSString stringWithFormat:@"http://openx.denivip.ru/delivery/fc.php?block=0&script=bannerTypeHtml:vastInlineBannerTypeHtml:vastInlineHtml&format=vast&nz=1&charset=UTF-8&r=0.1978856846690178&zones=z%%3D%u", (ZONE_ID)]]) 20 | 21 | @interface DVViewController () 22 | 23 | @property (nonatomic, strong) id periodicTimeObserver; 24 | @property (nonatomic) BOOL didStartPlayback; 25 | 26 | @end 27 | 28 | 29 | @implementation DVViewController 30 | 31 | @synthesize playerView = _playerView; 32 | @synthesize currentTimeLabel = _currentTimeLabel; 33 | @synthesize currentItemTitleLabel = _currentItemTitleLabel; 34 | @synthesize player = _player; 35 | @synthesize periodicTimeObserver = _periodicTimeObserver; 36 | 37 | #pragma mark - Player 38 | 39 | - (void)startPlaybackWithContentURL:(NSURL *)contentURL 40 | { 41 | AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:contentURL]; 42 | 43 | [playerItem addObserver:self 44 | forKeyPath:@"status" 45 | options:NSKeyValueObservingOptionInitial|NSKeyValueObservingOptionNew 46 | context:DVViewControllerPlayerItemStatusObservationContext]; 47 | self.player.contentPlayerItem = playerItem; 48 | self.didStartPlayback = NO; 49 | [self.player replaceCurrentItemWithPlayerItem:playerItem]; 50 | } 51 | 52 | - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context 53 | { 54 | if (context == DVViewControllerCurrentPlayerItemObservationContext) { 55 | self.currentItemTitleLabel.text = [((AVURLAsset *)self.player.currentItem.asset).URL absoluteString]; 56 | } 57 | else if (context == DVViewControllerPlayerItemStatusObservationContext) { 58 | AVPlayerItemStatus status = [[change objectForKey:NSKeyValueChangeNewKey] integerValue]; 59 | VLog(@"DVViewControllerPlayerItemStatusObservationContext %i", status); 60 | switch (status) { 61 | case AVPlayerItemStatusReadyToPlay: 62 | if (! self.didStartPlayback) { 63 | dispatch_async(dispatch_get_main_queue(), ^{ 64 | self.didStartPlayback = YES; 65 | [self.player play]; 66 | }); 67 | } 68 | break; 69 | 70 | case AVPlayerItemStatusFailed: 71 | VLog(@"%@", self.player.currentItem.error); 72 | break; 73 | 74 | default: 75 | break; 76 | } 77 | } 78 | else { 79 | [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; 80 | } 81 | } 82 | 83 | #pragma mark - Controls 84 | 85 | - (IBAction)rewindButton:(id)sender 86 | { 87 | [self.player seekToTime:CMTimeSubtract(self.player.currentTime, CMTimeMake(60, 1))]; 88 | } 89 | 90 | - (IBAction)fastForwardButton:(id)sender 91 | { 92 | [self.player seekToTime:CMTimeAdd(self.player.currentTime, CMTimeMake(60, 1))]; 93 | } 94 | 95 | - (IBAction)endButton:(id)sender 96 | { 97 | NSArray *seekableTimeRanges = self.player.currentItem.seekableTimeRanges; 98 | CMTimeRange seekableRange = [[seekableTimeRanges objectAtIndex:0] CMTimeRangeValue]; 99 | [self.player seekToTime:CMTimeSubtract(CMTimeAdd(seekableRange.start, seekableRange.duration), 100 | CMTimeMake(10, 1))]; 101 | } 102 | 103 | - (IBAction)playPauseButton:(id)sender 104 | { 105 | if (self.player.rate > 0) { 106 | [self.player pause]; 107 | } 108 | else { 109 | [self.player play]; 110 | } 111 | } 112 | 113 | #pragma mark - View Controller 114 | 115 | - (void)viewDidLoad 116 | { 117 | [super viewDidLoad]; 118 | 119 | self.player = [[DVIABPlayer alloc] init]; 120 | // Example of a specific HTTP Header you would want to pass to your server for capping reasons — uncomment to test ;) 121 | // self.player.httpHeaders = @{@"User-Agent": @"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)"}; 122 | self.player.playerLayer = (AVPlayerLayer *)self.playerView.layer; 123 | ((AVPlayerLayer *)self.playerView.layer).player = self.player; 124 | 125 | [self.player addObserver:self 126 | forKeyPath:@"currentItem" 127 | options:NSKeyValueObservingOptionInitial|NSKeyValueObservingOptionNew 128 | context:DVViewControllerCurrentPlayerItemObservationContext]; 129 | 130 | __block DVViewController *SELF = self; 131 | self.periodicTimeObserver = [self.player addPeriodicTimeObserverForInterval:CMTimeMake(1, 10) queue:NULL usingBlock:^(CMTime time) { 132 | DVTimeIntervalFormatter *formatter = [[DVTimeIntervalFormatter alloc] init]; 133 | SELF.currentTimeLabel.text = [NSString stringWithFormat:@"%@ / %@", 134 | [formatter stringWithTimeInterval:CMTimeGetSeconds(time)], 135 | [formatter stringWithTimeInterval:CMTimeGetSeconds(SELF.player.currentItem.duration)]]; 136 | }]; 137 | 138 | DVVideoMultipleAdPlaylist *adPlaylist = [[DVVideoMultipleAdPlaylist alloc] init]; 139 | adPlaylist.playBreaks = [NSArray arrayWithObjects: 140 | [DVVideoPlayBreak playBreakBeforeStartWithAdTemplateURL:OPENX_AD_TAG_WITH_ZONE(18)], 141 | [DVVideoPlayBreak playBreakAtTimeFromStart:CMTimeMake(10, 1) withAdTemplateURL:OPENX_AD_TAG_WITH_ZONE(19)], 142 | [DVVideoPlayBreak playBreakAfterEndWithAdTemplateURL:OPENX_AD_TAG_WITH_ZONE(20)], 143 | nil]; 144 | 145 | self.player.adPlaylist = adPlaylist; 146 | self.player.delegate = self; 147 | } 148 | 149 | - (void)viewWillAppear:(BOOL)animated 150 | { 151 | [super viewWillAppear:animated]; 152 | 153 | NSURL *contentURL = [NSURL URLWithString:@"https://devimages.apple.com.edgekey.net/resources/http-streaming/examples/bipbop_4x3/bipbop_4x3_variant.m3u8"]; 154 | // NSURL *contentURL = [NSURL URLWithString:@"http://denivip.ru/sites/default/files/ios-iab/content.mp4"]; 155 | [self startPlaybackWithContentURL:contentURL]; 156 | } 157 | 158 | - (void)viewDidUnload 159 | { 160 | [self setPlayerView:nil]; 161 | [self setCurrentTimeLabel:nil]; 162 | [self setCurrentItemTitleLabel:nil]; 163 | 164 | [self.player removeTimeObserver:self.periodicTimeObserver]; 165 | self.periodicTimeObserver = nil; 166 | 167 | [self.player removeObserver:self forKeyPath:@"currentItem" 168 | context:DVViewControllerCurrentPlayerItemObservationContext]; 169 | self.player = nil; 170 | 171 | [super viewDidUnload]; 172 | // Release any retained subviews of the main view. 173 | } 174 | 175 | - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 176 | { 177 | return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 178 | } 179 | 180 | #pragma mark - Player Delegate 181 | 182 | - (BOOL)player:(DVIABPlayer *)player shouldPauseForAdBreak:(DVVideoPlayBreak *)playBreak 183 | { 184 | return YES; 185 | } 186 | 187 | - (void)player:(DVIABPlayer *)player didFailPlayBreak:(DVVideoPlayBreak *)playBreak withError:(NSError *)error 188 | { 189 | VLogV(error); 190 | } 191 | 192 | @end 193 | -------------------------------------------------------------------------------- /DVVASTSample/DVVASTSample/en.lproj/MainStoryboard.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 29 | 36 | 51 | 66 | 81 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | -------------------------------------------------------------------------------- /DVVAST/Classes/DVVideoAdServingTemplate+Parsing.m: -------------------------------------------------------------------------------- 1 | // 2 | // DVVideoAdServingTemplate+Parsing.m 3 | // DVVASTSample 4 | // 5 | // Created by Nikolay Morev on 8/7/12. Augmented by Manuel "StuFF mc" Carrasco Molina in 2013 — https://github.com/stuffmc/ios-vast-player/tree/dev 6 | // Copyright (c) 2012 DENIVIP Media. All rights reserved. 7 | // 8 | 9 | #import "DVVideoAdServingTemplate+Parsing.h" 10 | #import "DVVideoAd.h" 11 | #import "DVInlineVideoAd.h" 12 | #import "DVWrapperVideoAd.h" 13 | #import "DVTimeIntervalFormatter.h" 14 | #import "NSString+DDXML.h" 15 | 16 | @implementation DDXMLElement (VAST) 17 | 18 | - (BOOL)isEmpty 19 | { 20 | return !self.stringValue || [[self.stringValue stringByTrimming] isEqualToString:@""]; 21 | } 22 | 23 | @end 24 | 25 | 26 | @implementation DVVideoAdServingTemplate (Parsing) 27 | 28 | - (BOOL)populateInlineVideoAd:(DVInlineVideoAd *)videoAd withXMLElement:(DDXMLElement *)element error:(NSError **)error 29 | { 30 | videoAd.playMediaFile = YES; 31 | videoAd.system = [[[element elementsForName:@"AdSystem"] objectAtIndex:0] stringValue]; 32 | videoAd.title = [[[element elementsForName:@"AdSystem"] objectAtIndex:0] stringValue]; 33 | 34 | // VAST 2 — Multiple elements 35 | NSArray *impressionElements = [element elementsForName:@"Impression"]; 36 | if (impressionElements.count == 1) { 37 | // VAST 1 — Multiple elements in a single 38 | DDXMLElement *impressionElement = [impressionElements objectAtIndex:0]; 39 | impressionElements = [impressionElement elementsForName:@"URL"]; 40 | } 41 | NSMutableArray *impressionURLs = [NSMutableArray array]; 42 | [impressionElements enumerateObjectsUsingBlock:^(DDXMLElement *impressionElement, NSUInteger idx, BOOL *stop) { 43 | [self addURLElement:impressionElement toArray:impressionURLs]; 44 | }]; 45 | VLogV(impressionURLs); 46 | videoAd.impressionURLs = impressionURLs; 47 | if (impressionURLs.count) { 48 | // For compatibility sake (code using ios-vast-player's "single" impressionURL) 49 | videoAd.impressionURL = impressionURLs[0]; 50 | } 51 | VLogV(videoAd.impressionURLs); 52 | 53 | NSArray *videos = [element elementsForName:@"Video"]; 54 | DDXMLElement *videoElement = nil; 55 | if (videos && videos.count) { 56 | videoElement = [videos objectAtIndex:0]; 57 | } else { 58 | NSArray *creatives = [element elementsForName:@"Creatives"]; 59 | if (creatives && creatives.count) { 60 | NSArray *creative = [[creatives objectAtIndex:0] elementsForName:@"Creative"]; 61 | if (creative && creative.count) { 62 | NSArray *linears = [[creative objectAtIndex:0] elementsForName:@"Linear"]; 63 | if (linears && linears.count) { 64 | videoElement = [linears objectAtIndex:0]; 65 | } 66 | } 67 | } 68 | } 69 | 70 | NSArray *durations = [videoElement elementsForName:@"Duration"]; 71 | if (durations && durations.count) { 72 | NSString *durationString = [[durations objectAtIndex:0] stringValue]; 73 | DVTimeIntervalFormatter *timeIntervalParser = [[DVTimeIntervalFormatter alloc] init]; 74 | videoAd.duration = [timeIntervalParser timeIntervalWithString:durationString]; 75 | } 76 | 77 | NSArray *videoClicksElements = [videoElement elementsForName:@"VideoClicks"]; 78 | if ([videoClicksElements count] > 0) { 79 | DDXMLElement *videoClicks = [videoClicksElements objectAtIndex:0]; 80 | NSArray *clickThroughs = [videoClicks elementsForName:@"ClickThrough"]; 81 | if (clickThroughs && clickThroughs.count) { 82 | DDXMLElement *clickThrough = [clickThroughs objectAtIndex:0]; 83 | videoAd.clickThroughURL = [NSURL URLWithString:clickThrough.stringValue]; 84 | } 85 | 86 | // VAST 2 — Multiple elements 87 | NSArray *clickTrackingElements = [videoClicks elementsForName:@"ClickTracking"]; 88 | if (clickTrackingElements.count == 1) { 89 | // VAST 1 — Multiple elements in a single 90 | DDXMLElement *clickTrackingElement = [clickTrackingElements objectAtIndex:0]; 91 | clickTrackingElements = [clickTrackingElement elementsForName:@"URL"]; 92 | } 93 | 94 | NSMutableArray *clickTrackingURLs = [NSMutableArray array]; 95 | [clickTrackingElements enumerateObjectsUsingBlock:^(DDXMLElement *clickTrackingElement, NSUInteger idx, BOOL *stop) { 96 | [self addURLElement:clickTrackingElement toArray:clickTrackingURLs]; 97 | }]; 98 | VLogV(clickTrackingURLs); 99 | videoAd.clickTrackingURLs = clickTrackingURLs; 100 | if (clickTrackingURLs.count) { 101 | // For compatibility sake (code using ios-vast-player's "single" clickTrackingURL) 102 | videoAd.clickTrackingURL = clickTrackingURLs[0]; 103 | } 104 | VLogV(videoAd.clickTrackingURLs); 105 | } 106 | 107 | #define TRACKING_EVENTS @"TrackingEvents" 108 | NSArray *trackingEvents = [videoElement elementsForName:TRACKING_EVENTS]; // VAST 2 has that tag in the Video tag 109 | if (!trackingEvents || !trackingEvents.count) { // VAST 1 has it at the same level than the video tag. 110 | trackingEvents = [element elementsForName:TRACKING_EVENTS]; 111 | } 112 | if (trackingEvents.count) { 113 | DDXMLElement *trackingEvent = [trackingEvents objectAtIndex:0]; 114 | NSArray *trackingElements = [trackingEvent elementsForName:@"Tracking"]; 115 | // NSMutableDictionary *dictionary = videoAd.trackingEvents ? [videoAd.trackingEvents mutableCopy] : [NSMutableDictionary dictionary]; 116 | NSMutableDictionary *dictionary = videoAd.trackingEvents ? [videoAd.trackingEvents mutableCopy] : [NSMutableDictionary dictionary]; 117 | [trackingElements enumerateObjectsUsingBlock:^(DDXMLElement *trackingElement, NSUInteger idx, BOOL *stop) { 118 | NSString *event = [trackingElement attributeForName:@"event"].stringValue; 119 | NSArray *urls = [trackingElement elementsForName:@"URL"]; 120 | NSMutableDictionary *innerDictionary = dictionary[event] ? [dictionary[event] mutableCopy] : [NSMutableDictionary dictionary]; 121 | if (!urls.count) { 122 | if (!trackingElement.isEmpty) { 123 | VLogV(trackingElement.stringValue); 124 | NSString *key = [NSString stringWithFormat:@"url-%lu", (unsigned long)innerDictionary.allKeys.count]; 125 | VLogV(key); 126 | innerDictionary[key] = [NSURL URLWithString:trackingElement.stringValue]; 127 | } 128 | } else { 129 | [urls enumerateObjectsUsingBlock:^(DDXMLElement *url, NSUInteger idx, BOOL *stop) { 130 | if (!url.isEmpty) { 131 | NSString *attribute = [url attributeForName:@"id"].stringValue; 132 | innerDictionary[attribute] = [NSURL URLWithString:url.stringValue]; 133 | } 134 | }]; 135 | } 136 | dictionary[event] = innerDictionary; 137 | }]; 138 | videoAd.trackingEvents = dictionary; 139 | } 140 | VLogV(videoAd.trackingEvents); 141 | 142 | NSArray *mediaFilesArray = [videoElement elementsForName:@"MediaFiles"]; 143 | if (mediaFilesArray && mediaFilesArray.count) { 144 | DDXMLElement *mediaFiles = [mediaFilesArray objectAtIndex:0]; 145 | DDXMLElement *mediaFile = nil; 146 | for (DDXMLElement *currentMF in [mediaFiles elementsForName:@"MediaFile"]) { 147 | NSString *type = [[currentMF attributeForName:@"type"] stringValue]; 148 | if ([type isEqualToString:@"mobile/m3u8"] || [type isEqualToString:@"video/mp4"] || [type isEqualToString:@"video/x-mp4"]) { 149 | mediaFile = currentMF; 150 | break; 151 | } 152 | } 153 | NSArray *urls = [mediaFile elementsForName:@"URL"]; 154 | DDXMLDocument *url = urls && urls.count ? [urls objectAtIndex:0] : mediaFile; 155 | videoAd.mediaFileURL = [NSURL URLWithString:[url stringValue]]; 156 | } 157 | 158 | // Looking for the tag that will tell me not to play this media. 159 | NSArray *extensions = [element elementsForName:@"Extensions"]; 160 | if (extensions && extensions.count) { 161 | NSArray *extension = [[extensions objectAtIndex:0] elementsForName:@"Extension"]; 162 | if (extension && extension.count) { 163 | NSArray *fallback = [[extension objectAtIndex:0] elementsForName:@"Fallback"]; 164 | videoAd.playMediaFile = !fallback || !fallback.count; 165 | } 166 | } 167 | 168 | return YES; 169 | } 170 | 171 | - (void)addURLElement:(DDXMLElement*)element toArray:(NSMutableArray*)array 172 | { 173 | if (!element.isEmpty) { 174 | NSURL *url = [NSURL URLWithString:element.stringValue]; 175 | VLogV(url); 176 | if (url) { 177 | [array addObject:url]; 178 | } 179 | } 180 | } 181 | 182 | - (DVVideoAd *)videoAdWithXMLElement:(DDXMLElement *)element error:(NSError **)error 183 | { 184 | DVVideoAd *videoAd = nil; 185 | 186 | DDXMLElement *adContents = (DDXMLElement *)[element childAtIndex:0]; 187 | NSString *adContentsName = [adContents name]; 188 | if ([adContentsName isEqualToString:@"InLine"]) { 189 | videoAd = [[DVInlineVideoAd alloc] init]; 190 | NSError *inlineVideoAdError = nil; 191 | if (! [self populateInlineVideoAd:(DVInlineVideoAd *)videoAd 192 | withXMLElement:adContents 193 | error:&inlineVideoAdError]) { 194 | if (error != nil) { 195 | NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys: 196 | inlineVideoAdError, NSUnderlyingErrorKey, nil]; 197 | *error = [NSError errorWithDomain:DVVideoAdServingTemplateErrorDomain 198 | code:DVVideoAdServingTemplateSchemaValidationErrorCode 199 | userInfo:userInfo]; 200 | } 201 | return nil; 202 | } 203 | } 204 | else if ([adContentsName isEqualToString:@"Wrapper"]) { 205 | videoAd = [[DVWrapperVideoAd alloc] init]; 206 | 207 | NSArray *vastTagArray = [adContents elementsForName:@"VASTAdTagURL"]; 208 | DDXMLElement *vastTagURI = nil; 209 | if (vastTagArray && vastTagArray.count) { 210 | // VAST 1.x 211 | vastTagURI = [[[vastTagArray objectAtIndex:0] elementsForName:@"URL"] objectAtIndex:0]; 212 | } else { 213 | // VAST 2.0 214 | vastTagArray = [adContents elementsForName:@"VASTAdTagURI"]; 215 | vastTagURI = [vastTagArray objectAtIndex:0]; 216 | } 217 | // TODO: Parse this (inline) + self (wrapper!) 218 | ((DVWrapperVideoAd*)videoAd).URL = [NSURL URLWithString:[vastTagURI stringValue]]; 219 | VLogV(((DVWrapperVideoAd*)videoAd).URL); 220 | 221 | NSError *inlineVideoAdError = nil; 222 | if (! [self populateInlineVideoAd:(DVInlineVideoAd *)videoAd 223 | withXMLElement:adContents 224 | error:&inlineVideoAdError]) { 225 | if (error != nil) { 226 | NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys: 227 | inlineVideoAdError, NSUnderlyingErrorKey, nil]; 228 | *error = [NSError errorWithDomain:DVVideoAdServingTemplateErrorDomain 229 | code:DVVideoAdServingTemplateSchemaValidationErrorCode 230 | userInfo:userInfo]; 231 | } 232 | return nil; 233 | } 234 | videoAd.playMediaFile = NO; 235 | } 236 | else { 237 | if (error != nil) *error = [NSError errorWithDomain:DVVideoAdServingTemplateErrorDomain 238 | code:DVVideoAdServingTemplateUnexpectedAdType 239 | userInfo:nil]; 240 | return nil; 241 | } 242 | 243 | DDXMLNode *idAttribute = [element attributeForName:@"id"]; 244 | videoAd.identifier = [idAttribute stringValue]; 245 | 246 | return videoAd; 247 | } 248 | 249 | - (id)initWithData:(NSData *)data error:(NSError *__autoreleasing *)error_ 250 | { 251 | NSError *error = nil; 252 | self.document = [[DDXMLDocument alloc] initWithData:data options:0 error:&error]; 253 | if (! self.document) { 254 | if (error_ != nil) { 255 | NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys: 256 | error, NSUnderlyingErrorKey, nil]; 257 | *error_ = [NSError errorWithDomain:DVVideoAdServingTemplateErrorDomain 258 | code:DVVideoAdServingTemplateXMLParsingErrorCode 259 | userInfo:userInfo]; 260 | } 261 | return nil; 262 | } 263 | 264 | self = [self initWithXMLDocument:self.document error:&error]; 265 | if (! self) { 266 | if (error_ != nil) *error_ = error; 267 | return nil; 268 | } 269 | 270 | return self; 271 | } 272 | 273 | - (id)initWithXMLDocument:(DDXMLDocument *)document error:(NSError *__autoreleasing *)error 274 | { 275 | if (self = [self init]) { 276 | 277 | NSMutableArray *ads = [NSMutableArray array]; 278 | 279 | DDXMLElement *rootElement = [document rootElement]; 280 | NSArray *adElements = [rootElement elementsForName:@"Ad"]; 281 | VLogV(adElements); 282 | for (DDXMLElement *adElement in adElements) { 283 | NSError *videoAdError = nil; 284 | DVVideoAd *videoAd = [self videoAdWithXMLElement:adElement error:&videoAdError]; 285 | if (! videoAd) { 286 | if (error != nil) { 287 | NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys: 288 | videoAdError, NSUnderlyingErrorKey, nil]; 289 | *error = [NSError errorWithDomain:DVVideoAdServingTemplateErrorDomain 290 | code:DVVideoAdServingTemplateSchemaValidationErrorCode 291 | userInfo:userInfo]; 292 | } 293 | return nil; 294 | } 295 | 296 | [ads addObject:videoAd]; 297 | } 298 | 299 | self.ads = [NSArray arrayWithArray:ads]; 300 | } 301 | return self; 302 | } 303 | 304 | @end 305 | -------------------------------------------------------------------------------- /DVVASTSample/DVVASTSample.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 4C47F94916D292B500EFCA8F /* Default-568h@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 4C47F94816D292B500EFCA8F /* Default-568h@2x.png */; }; 11 | D469A237EA4141DAA6A4E459 /* libPods.a in Frameworks */ = {isa = PBXBuildFile; fileRef = BEC01DE053E64BCE93E8A4C2 /* libPods.a */; }; 12 | E3361BA915D151D2002C1905 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E3361BA815D151D2002C1905 /* UIKit.framework */; }; 13 | E3361BAB15D151D2002C1905 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E3361BAA15D151D2002C1905 /* Foundation.framework */; }; 14 | E3361BAD15D151D2002C1905 /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E3361BAC15D151D2002C1905 /* CoreGraphics.framework */; }; 15 | E3361BB315D151D2002C1905 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = E3361BB115D151D2002C1905 /* InfoPlist.strings */; }; 16 | E3361BB515D151D2002C1905 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = E3361BB415D151D2002C1905 /* main.m */; }; 17 | E3361BB915D151D2002C1905 /* DVAppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = E3361BB815D151D2002C1905 /* DVAppDelegate.m */; }; 18 | E3361BBC15D151D2002C1905 /* MainStoryboard.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = E3361BBA15D151D2002C1905 /* MainStoryboard.storyboard */; }; 19 | E3361BBF15D151D2002C1905 /* DVViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = E3361BBE15D151D2002C1905 /* DVViewController.m */; }; 20 | E38381D715D26CC40056C4CE /* DVPlayerView.m in Sources */ = {isa = PBXBuildFile; fileRef = E38381D615D26CC40056C4CE /* DVPlayerView.m */; }; 21 | E38381DB15D290350056C4CE /* Icon.png in Resources */ = {isa = PBXBuildFile; fileRef = E38381DA15D290340056C4CE /* Icon.png */; }; 22 | E38381DD15D2903B0056C4CE /* Icon@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = E38381DC15D2903B0056C4CE /* Icon@2x.png */; }; 23 | E38381DE15D3EAB20056C4CE /* CoreMedia.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E3361BCC15D15E70002C1905 /* CoreMedia.framework */; }; 24 | E38381DF15D3EAB70056C4CE /* AVFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E3361BC915D15B06002C1905 /* AVFoundation.framework */; }; 25 | E38381E115D3EFBF0056C4CE /* Icon-72.png in Resources */ = {isa = PBXBuildFile; fileRef = E38381E015D3EFBF0056C4CE /* Icon-72.png */; }; 26 | E38381E315D3EFC30056C4CE /* Icon-72@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = E38381E215D3EFC30056C4CE /* Icon-72@2x.png */; }; 27 | E3FA909E15D50F67002EEEDE /* AudioToolbox.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E3FA909D15D50F67002EEEDE /* AudioToolbox.framework */; }; 28 | /* End PBXBuildFile section */ 29 | 30 | /* Begin PBXFileReference section */ 31 | 089C6176C5AF475EAC1BE890 /* Pods.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = Pods.xcconfig; path = ../Pods/Pods.xcconfig; sourceTree = SOURCE_ROOT; }; 32 | 4C47F94816D292B500EFCA8F /* Default-568h@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Default-568h@2x.png"; sourceTree = ""; }; 33 | BEC01DE053E64BCE93E8A4C2 /* libPods.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libPods.a; sourceTree = BUILT_PRODUCTS_DIR; }; 34 | E3361BA415D151D2002C1905 /* DVVASTSample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = DVVASTSample.app; sourceTree = BUILT_PRODUCTS_DIR; }; 35 | E3361BA815D151D2002C1905 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; }; 36 | E3361BAA15D151D2002C1905 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; }; 37 | E3361BAC15D151D2002C1905 /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = System/Library/Frameworks/CoreGraphics.framework; sourceTree = SDKROOT; }; 38 | E3361BB015D151D2002C1905 /* DVVASTSample-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "DVVASTSample-Info.plist"; sourceTree = ""; }; 39 | E3361BB215D151D2002C1905 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = ""; }; 40 | E3361BB415D151D2002C1905 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 41 | E3361BB615D151D2002C1905 /* DVVASTSample-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "DVVASTSample-Prefix.pch"; sourceTree = ""; }; 42 | E3361BB715D151D2002C1905 /* DVAppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = DVAppDelegate.h; sourceTree = ""; }; 43 | E3361BB815D151D2002C1905 /* DVAppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = DVAppDelegate.m; sourceTree = ""; }; 44 | E3361BBB15D151D2002C1905 /* en */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = en; path = en.lproj/MainStoryboard.storyboard; sourceTree = ""; }; 45 | E3361BBD15D151D2002C1905 /* DVViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = DVViewController.h; sourceTree = ""; }; 46 | E3361BBE15D151D2002C1905 /* DVViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = DVViewController.m; sourceTree = ""; }; 47 | E3361BC915D15B06002C1905 /* AVFoundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AVFoundation.framework; path = System/Library/Frameworks/AVFoundation.framework; sourceTree = SDKROOT; }; 48 | E3361BCC15D15E70002C1905 /* CoreMedia.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreMedia.framework; path = System/Library/Frameworks/CoreMedia.framework; sourceTree = SDKROOT; }; 49 | E38381D515D26CC40056C4CE /* DVPlayerView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DVPlayerView.h; sourceTree = ""; }; 50 | E38381D615D26CC40056C4CE /* DVPlayerView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DVPlayerView.m; sourceTree = ""; }; 51 | E38381D815D270020056C4CE /* QuartzCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QuartzCore.framework; path = System/Library/Frameworks/QuartzCore.framework; sourceTree = SDKROOT; }; 52 | E38381DA15D290340056C4CE /* Icon.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = Icon.png; sourceTree = ""; }; 53 | E38381DC15D2903B0056C4CE /* Icon@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Icon@2x.png"; sourceTree = ""; }; 54 | E38381E015D3EFBF0056C4CE /* Icon-72.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Icon-72.png"; sourceTree = ""; }; 55 | E38381E215D3EFC30056C4CE /* Icon-72@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Icon-72@2x.png"; sourceTree = ""; }; 56 | E3FA909D15D50F67002EEEDE /* AudioToolbox.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AudioToolbox.framework; path = System/Library/Frameworks/AudioToolbox.framework; sourceTree = SDKROOT; }; 57 | /* End PBXFileReference section */ 58 | 59 | /* Begin PBXFrameworksBuildPhase section */ 60 | E3361BA115D151D2002C1905 /* Frameworks */ = { 61 | isa = PBXFrameworksBuildPhase; 62 | buildActionMask = 2147483647; 63 | files = ( 64 | E3FA909E15D50F67002EEEDE /* AudioToolbox.framework in Frameworks */, 65 | E38381DF15D3EAB70056C4CE /* AVFoundation.framework in Frameworks */, 66 | E38381DE15D3EAB20056C4CE /* CoreMedia.framework in Frameworks */, 67 | E3361BA915D151D2002C1905 /* UIKit.framework in Frameworks */, 68 | E3361BAB15D151D2002C1905 /* Foundation.framework in Frameworks */, 69 | E3361BAD15D151D2002C1905 /* CoreGraphics.framework in Frameworks */, 70 | D469A237EA4141DAA6A4E459 /* libPods.a in Frameworks */, 71 | ); 72 | runOnlyForDeploymentPostprocessing = 0; 73 | }; 74 | /* End PBXFrameworksBuildPhase section */ 75 | 76 | /* Begin PBXGroup section */ 77 | E3361B9915D151D2002C1905 = { 78 | isa = PBXGroup; 79 | children = ( 80 | 4C47F94816D292B500EFCA8F /* Default-568h@2x.png */, 81 | E38381E215D3EFC30056C4CE /* Icon-72@2x.png */, 82 | E38381E015D3EFBF0056C4CE /* Icon-72.png */, 83 | E38381DC15D2903B0056C4CE /* Icon@2x.png */, 84 | E38381DA15D290340056C4CE /* Icon.png */, 85 | E3361BAE15D151D2002C1905 /* DVVASTSample */, 86 | E3361BA715D151D2002C1905 /* Frameworks */, 87 | E3361BA515D151D2002C1905 /* Products */, 88 | 089C6176C5AF475EAC1BE890 /* Pods.xcconfig */, 89 | ); 90 | sourceTree = ""; 91 | }; 92 | E3361BA515D151D2002C1905 /* Products */ = { 93 | isa = PBXGroup; 94 | children = ( 95 | E3361BA415D151D2002C1905 /* DVVASTSample.app */, 96 | ); 97 | name = Products; 98 | sourceTree = ""; 99 | }; 100 | E3361BA715D151D2002C1905 /* Frameworks */ = { 101 | isa = PBXGroup; 102 | children = ( 103 | E3FA909D15D50F67002EEEDE /* AudioToolbox.framework */, 104 | E38381D815D270020056C4CE /* QuartzCore.framework */, 105 | E3361BCC15D15E70002C1905 /* CoreMedia.framework */, 106 | E3361BC915D15B06002C1905 /* AVFoundation.framework */, 107 | E3361BA815D151D2002C1905 /* UIKit.framework */, 108 | E3361BAA15D151D2002C1905 /* Foundation.framework */, 109 | E3361BAC15D151D2002C1905 /* CoreGraphics.framework */, 110 | BEC01DE053E64BCE93E8A4C2 /* libPods.a */, 111 | ); 112 | name = Frameworks; 113 | sourceTree = ""; 114 | }; 115 | E3361BAE15D151D2002C1905 /* DVVASTSample */ = { 116 | isa = PBXGroup; 117 | children = ( 118 | E3361BB715D151D2002C1905 /* DVAppDelegate.h */, 119 | E3361BB815D151D2002C1905 /* DVAppDelegate.m */, 120 | E3361BBA15D151D2002C1905 /* MainStoryboard.storyboard */, 121 | E3361BBD15D151D2002C1905 /* DVViewController.h */, 122 | E3361BBE15D151D2002C1905 /* DVViewController.m */, 123 | E3361BAF15D151D2002C1905 /* Supporting Files */, 124 | E38381D515D26CC40056C4CE /* DVPlayerView.h */, 125 | E38381D615D26CC40056C4CE /* DVPlayerView.m */, 126 | ); 127 | path = DVVASTSample; 128 | sourceTree = ""; 129 | }; 130 | E3361BAF15D151D2002C1905 /* Supporting Files */ = { 131 | isa = PBXGroup; 132 | children = ( 133 | E3361BB015D151D2002C1905 /* DVVASTSample-Info.plist */, 134 | E3361BB115D151D2002C1905 /* InfoPlist.strings */, 135 | E3361BB415D151D2002C1905 /* main.m */, 136 | E3361BB615D151D2002C1905 /* DVVASTSample-Prefix.pch */, 137 | ); 138 | name = "Supporting Files"; 139 | sourceTree = ""; 140 | }; 141 | /* End PBXGroup section */ 142 | 143 | /* Begin PBXNativeTarget section */ 144 | E3361BA315D151D2002C1905 /* DVVASTSample */ = { 145 | isa = PBXNativeTarget; 146 | buildConfigurationList = E3361BC215D151D2002C1905 /* Build configuration list for PBXNativeTarget "DVVASTSample" */; 147 | buildPhases = ( 148 | 28502A96C3FC4123AF26BEE0 /* Check Pods Manifest.lock */, 149 | E3361BA015D151D2002C1905 /* Sources */, 150 | E3361BA115D151D2002C1905 /* Frameworks */, 151 | E3361BA215D151D2002C1905 /* Resources */, 152 | 6B5259A029114EC982B1C8F3 /* Copy Pods Resources */, 153 | ); 154 | buildRules = ( 155 | ); 156 | dependencies = ( 157 | ); 158 | name = DVVASTSample; 159 | productName = DVVASTSample; 160 | productReference = E3361BA415D151D2002C1905 /* DVVASTSample.app */; 161 | productType = "com.apple.product-type.application"; 162 | }; 163 | /* End PBXNativeTarget section */ 164 | 165 | /* Begin PBXProject section */ 166 | E3361B9B15D151D2002C1905 /* Project object */ = { 167 | isa = PBXProject; 168 | attributes = { 169 | CLASSPREFIX = DV; 170 | LastUpgradeCheck = 0460; 171 | ORGANIZATIONNAME = "DENIVIP Media"; 172 | }; 173 | buildConfigurationList = E3361B9E15D151D2002C1905 /* Build configuration list for PBXProject "DVVASTSample" */; 174 | compatibilityVersion = "Xcode 3.2"; 175 | developmentRegion = English; 176 | hasScannedForEncodings = 0; 177 | knownRegions = ( 178 | en, 179 | ); 180 | mainGroup = E3361B9915D151D2002C1905; 181 | productRefGroup = E3361BA515D151D2002C1905 /* Products */; 182 | projectDirPath = ""; 183 | projectRoot = ""; 184 | targets = ( 185 | E3361BA315D151D2002C1905 /* DVVASTSample */, 186 | ); 187 | }; 188 | /* End PBXProject section */ 189 | 190 | /* Begin PBXResourcesBuildPhase section */ 191 | E3361BA215D151D2002C1905 /* Resources */ = { 192 | isa = PBXResourcesBuildPhase; 193 | buildActionMask = 2147483647; 194 | files = ( 195 | E3361BB315D151D2002C1905 /* InfoPlist.strings in Resources */, 196 | E3361BBC15D151D2002C1905 /* MainStoryboard.storyboard in Resources */, 197 | E38381DB15D290350056C4CE /* Icon.png in Resources */, 198 | E38381DD15D2903B0056C4CE /* Icon@2x.png in Resources */, 199 | E38381E115D3EFBF0056C4CE /* Icon-72.png in Resources */, 200 | E38381E315D3EFC30056C4CE /* Icon-72@2x.png in Resources */, 201 | 4C47F94916D292B500EFCA8F /* Default-568h@2x.png in Resources */, 202 | ); 203 | runOnlyForDeploymentPostprocessing = 0; 204 | }; 205 | /* End PBXResourcesBuildPhase section */ 206 | 207 | /* Begin PBXShellScriptBuildPhase section */ 208 | 28502A96C3FC4123AF26BEE0 /* Check Pods Manifest.lock */ = { 209 | isa = PBXShellScriptBuildPhase; 210 | buildActionMask = 2147483647; 211 | files = ( 212 | ); 213 | inputPaths = ( 214 | ); 215 | name = "Check Pods Manifest.lock"; 216 | outputPaths = ( 217 | ); 218 | runOnlyForDeploymentPostprocessing = 0; 219 | shellPath = /bin/sh; 220 | shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [[ $? != 0 ]] ; then\n cat << EOM\nerror: The sanbox is not in sync with the Podfile.lock. Run 'pod install'.\nEOM\n exit 1\nfi\n"; 221 | }; 222 | 6B5259A029114EC982B1C8F3 /* Copy Pods Resources */ = { 223 | isa = PBXShellScriptBuildPhase; 224 | buildActionMask = 2147483647; 225 | files = ( 226 | ); 227 | inputPaths = ( 228 | ); 229 | name = "Copy Pods Resources"; 230 | outputPaths = ( 231 | ); 232 | runOnlyForDeploymentPostprocessing = 0; 233 | shellPath = /bin/sh; 234 | shellScript = "\"${SRCROOT}/../Pods/Pods-resources.sh\"\n"; 235 | }; 236 | /* End PBXShellScriptBuildPhase section */ 237 | 238 | /* Begin PBXSourcesBuildPhase section */ 239 | E3361BA015D151D2002C1905 /* Sources */ = { 240 | isa = PBXSourcesBuildPhase; 241 | buildActionMask = 2147483647; 242 | files = ( 243 | E3361BB515D151D2002C1905 /* main.m in Sources */, 244 | E3361BB915D151D2002C1905 /* DVAppDelegate.m in Sources */, 245 | E3361BBF15D151D2002C1905 /* DVViewController.m in Sources */, 246 | E38381D715D26CC40056C4CE /* DVPlayerView.m in Sources */, 247 | ); 248 | runOnlyForDeploymentPostprocessing = 0; 249 | }; 250 | /* End PBXSourcesBuildPhase section */ 251 | 252 | /* Begin PBXVariantGroup section */ 253 | E3361BB115D151D2002C1905 /* InfoPlist.strings */ = { 254 | isa = PBXVariantGroup; 255 | children = ( 256 | E3361BB215D151D2002C1905 /* en */, 257 | ); 258 | name = InfoPlist.strings; 259 | sourceTree = ""; 260 | }; 261 | E3361BBA15D151D2002C1905 /* MainStoryboard.storyboard */ = { 262 | isa = PBXVariantGroup; 263 | children = ( 264 | E3361BBB15D151D2002C1905 /* en */, 265 | ); 266 | name = MainStoryboard.storyboard; 267 | sourceTree = ""; 268 | }; 269 | /* End PBXVariantGroup section */ 270 | 271 | /* Begin XCBuildConfiguration section */ 272 | E3361BC015D151D2002C1905 /* Debug */ = { 273 | isa = XCBuildConfiguration; 274 | buildSettings = { 275 | ALWAYS_SEARCH_USER_PATHS = NO; 276 | ARCHS = "$(ARCHS_STANDARD_32_BIT)"; 277 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 278 | CLANG_ENABLE_OBJC_ARC = YES; 279 | CLANG_WARN_CONSTANT_CONVERSION = YES; 280 | CLANG_WARN_ENUM_CONVERSION = YES; 281 | CLANG_WARN_INT_CONVERSION = YES; 282 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 283 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 284 | COPY_PHASE_STRIP = NO; 285 | GCC_C_LANGUAGE_STANDARD = gnu99; 286 | GCC_DYNAMIC_NO_PIC = NO; 287 | GCC_OPTIMIZATION_LEVEL = 0; 288 | GCC_PREPROCESSOR_DEFINITIONS = ( 289 | "DEBUG=1", 290 | "$(inherited)", 291 | ); 292 | GCC_SYMBOLS_PRIVATE_EXTERN = NO; 293 | GCC_VERSION = com.apple.compilers.llvm.clang.1_0; 294 | GCC_WARN_ABOUT_RETURN_TYPE = YES; 295 | GCC_WARN_UNINITIALIZED_AUTOS = YES; 296 | GCC_WARN_UNUSED_VARIABLE = YES; 297 | HEADER_SEARCH_PATHS = "$(inherited)"; 298 | IPHONEOS_DEPLOYMENT_TARGET = 5.1; 299 | OTHER_LDFLAGS = "$(inherited)"; 300 | SDKROOT = iphoneos; 301 | }; 302 | name = Debug; 303 | }; 304 | E3361BC115D151D2002C1905 /* Release */ = { 305 | isa = XCBuildConfiguration; 306 | buildSettings = { 307 | ALWAYS_SEARCH_USER_PATHS = NO; 308 | ARCHS = "$(ARCHS_STANDARD_32_BIT)"; 309 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 310 | CLANG_ENABLE_OBJC_ARC = YES; 311 | CLANG_WARN_CONSTANT_CONVERSION = YES; 312 | CLANG_WARN_ENUM_CONVERSION = YES; 313 | CLANG_WARN_INT_CONVERSION = YES; 314 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 315 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 316 | COPY_PHASE_STRIP = YES; 317 | GCC_C_LANGUAGE_STANDARD = gnu99; 318 | GCC_VERSION = com.apple.compilers.llvm.clang.1_0; 319 | GCC_WARN_ABOUT_RETURN_TYPE = YES; 320 | GCC_WARN_UNINITIALIZED_AUTOS = YES; 321 | GCC_WARN_UNUSED_VARIABLE = YES; 322 | HEADER_SEARCH_PATHS = "$(inherited)"; 323 | IPHONEOS_DEPLOYMENT_TARGET = 5.1; 324 | OTHER_CFLAGS = "-DNS_BLOCK_ASSERTIONS=1"; 325 | OTHER_LDFLAGS = "$(inherited)"; 326 | SDKROOT = iphoneos; 327 | VALIDATE_PRODUCT = YES; 328 | }; 329 | name = Release; 330 | }; 331 | E3361BC315D151D2002C1905 /* Debug */ = { 332 | isa = XCBuildConfiguration; 333 | baseConfigurationReference = 089C6176C5AF475EAC1BE890 /* Pods.xcconfig */; 334 | buildSettings = { 335 | GCC_PRECOMPILE_PREFIX_HEADER = YES; 336 | GCC_PREFIX_HEADER = "DVVASTSample/DVVASTSample-Prefix.pch"; 337 | HEADER_SEARCH_PATHS = ( 338 | "${PODS_HEADERS_SEARCH_PATHS}", 339 | "$(inherited)", 340 | ); 341 | INFOPLIST_FILE = "DVVASTSample/DVVASTSample-Info.plist"; 342 | IPHONEOS_DEPLOYMENT_TARGET = 5.1; 343 | OTHER_LDFLAGS = "$(inherited)"; 344 | PRODUCT_NAME = "$(TARGET_NAME)"; 345 | TARGETED_DEVICE_FAMILY = "1,2"; 346 | WRAPPER_EXTENSION = app; 347 | }; 348 | name = Debug; 349 | }; 350 | E3361BC415D151D2002C1905 /* Release */ = { 351 | isa = XCBuildConfiguration; 352 | baseConfigurationReference = 089C6176C5AF475EAC1BE890 /* Pods.xcconfig */; 353 | buildSettings = { 354 | GCC_PRECOMPILE_PREFIX_HEADER = YES; 355 | GCC_PREFIX_HEADER = "DVVASTSample/DVVASTSample-Prefix.pch"; 356 | HEADER_SEARCH_PATHS = ( 357 | "${PODS_HEADERS_SEARCH_PATHS}", 358 | "$(inherited)", 359 | ); 360 | INFOPLIST_FILE = "DVVASTSample/DVVASTSample-Info.plist"; 361 | IPHONEOS_DEPLOYMENT_TARGET = 5.1; 362 | OTHER_LDFLAGS = "$(inherited)"; 363 | PRODUCT_NAME = "$(TARGET_NAME)"; 364 | TARGETED_DEVICE_FAMILY = "1,2"; 365 | WRAPPER_EXTENSION = app; 366 | }; 367 | name = Release; 368 | }; 369 | /* End XCBuildConfiguration section */ 370 | 371 | /* Begin XCConfigurationList section */ 372 | E3361B9E15D151D2002C1905 /* Build configuration list for PBXProject "DVVASTSample" */ = { 373 | isa = XCConfigurationList; 374 | buildConfigurations = ( 375 | E3361BC015D151D2002C1905 /* Debug */, 376 | E3361BC115D151D2002C1905 /* Release */, 377 | ); 378 | defaultConfigurationIsVisible = 0; 379 | defaultConfigurationName = Release; 380 | }; 381 | E3361BC215D151D2002C1905 /* Build configuration list for PBXNativeTarget "DVVASTSample" */ = { 382 | isa = XCConfigurationList; 383 | buildConfigurations = ( 384 | E3361BC315D151D2002C1905 /* Debug */, 385 | E3361BC415D151D2002C1905 /* Release */, 386 | ); 387 | defaultConfigurationIsVisible = 0; 388 | defaultConfigurationName = Release; 389 | }; 390 | /* End XCConfigurationList section */ 391 | }; 392 | rootObject = E3361B9B15D151D2002C1905 /* Project object */; 393 | } 394 | -------------------------------------------------------------------------------- /DVVAST/Classes/DVIABPlayer.m: -------------------------------------------------------------------------------- 1 | // 2 | // DVVASTPlayer.m 3 | // DVVASTSample 4 | // 5 | // Created by Nikolay Morev on 8/7/12. 6 | // Copyright (c) 2012 DENIVIP Media. All rights reserved. 7 | // 8 | 9 | #import "DVIABPlayer.h" 10 | #import "DVVideoAdServingTemplate+Parsing.h" 11 | #import "DVVideoAd.h" 12 | #import "DVInlineVideoAd.h" 13 | #import "DVWrapperVideoAd.h" 14 | 15 | 16 | #define AD_REQUEST_TIMEOUT_INTERVAL ((NSTimeInterval)5.f) 17 | #define AD_PLAY_BREAK_MIN_INTERVAL_BETWEEN ((NSTimeInterval)5.f) 18 | 19 | static void *DVIABPlayerInlineAdPlayerItemStatusObservationContext = &DVIABPlayerInlineAdPlayerItemStatusObservationContext; 20 | static void *DVIABContentPlayerRateObservationContext = &DVIABContentPlayerRateObservationContext; 21 | static void *DVIABAdPlayerRateObservationContext = &DVIABAdPlayerRateObservationContext; 22 | 23 | 24 | NSString *const DVIABPlayerErrorDomain = @"DVIABPlayerErrorDomain"; 25 | 26 | 27 | @interface DVIABPlayer () 28 | 29 | @property (nonatomic, strong) AVPlayer *adPlayer; 30 | 31 | @property (nonatomic, strong) id playBreaksTimeObserver; 32 | @property (nonatomic, strong) id periodicAdTimeObserver; 33 | @property (nonatomic, strong) id periodicTimeObserver; 34 | @property (nonatomic, strong) NSMutableArray *playBreaksQueue; 35 | @property (nonatomic, strong) DVVideoPlayBreak *currentPlayBreak; 36 | @property (nonatomic, strong) NSMutableData *adRequestData; 37 | @property (nonatomic, strong) NSMutableArray *adsQueue; 38 | @property (nonatomic, strong) AVPlayerItem *currentInlineAdPlayerItem; 39 | @property (nonatomic) BOOL contentPlayerItemDidReachEnd, didFinishPlayBreakRecently, firstQuartile, midpoint, thirdQuartile; 40 | @property (nonatomic, strong, readonly) DVInlineVideoAd *currentInlineAd; 41 | @property (nonatomic, strong) DVWrapperVideoAd *wrapper; 42 | 43 | - (void)startPlayBreaksFromQueue; 44 | - (void)finishCurrentPlayBreak; 45 | - (void)fetchPlayBreakAdTemplate:(DVVideoPlayBreak *)playBreak; 46 | - (void)startAdsFromQueue; 47 | - (void)playInlineAd:(DVInlineVideoAd *)videoAd; 48 | - (void)finishCurrentInlineAd:(AVPlayerItem *)playerItem; 49 | 50 | @end 51 | 52 | 53 | @implementation DVIABPlayer { 54 | BOOL paused; 55 | } 56 | 57 | @synthesize currentAd = _currentAd; 58 | 59 | - (id)init 60 | { 61 | if (self = [super init]) { 62 | [self addObserver:self 63 | forKeyPath:@"rate" 64 | options:NSKeyValueObservingOptionNew 65 | context:DVIABContentPlayerRateObservationContext]; 66 | } 67 | return self; 68 | } 69 | 70 | - (DVInlineVideoAd*)currentInlineAd 71 | { 72 | if ([self.currentAd isKindOfClass:[DVInlineVideoAd class]]) { 73 | return (DVInlineVideoAd*)self.currentAd; 74 | } else { 75 | return nil; 76 | } 77 | } 78 | 79 | @synthesize adPlayer = _adPlayer; 80 | 81 | - (void)setAdPlayer:(AVPlayer *)adPlayer 82 | { 83 | if (adPlayer != _adPlayer && _adPlayer != nil) { 84 | [_adPlayer removeObserver:self 85 | forKeyPath:@"rate" 86 | context:DVIABAdPlayerRateObservationContext]; 87 | } 88 | 89 | _adPlayer = adPlayer; 90 | 91 | if (_adPlayer != nil) { 92 | [_adPlayer addObserver:self 93 | forKeyPath:@"rate" 94 | options:NSKeyValueObservingOptionNew 95 | context:DVIABAdPlayerRateObservationContext]; 96 | } 97 | } 98 | 99 | @synthesize playerLayer = _playerLayer; 100 | @synthesize delegate = _delegate; 101 | @synthesize playBreaksQueue = _playBreaksQueue; 102 | @synthesize adRequestData = _adRequestData; 103 | @synthesize contentPlayerItem = _contentPlayerItem; 104 | 105 | - (void)setContentPlayerItem:(AVPlayerItem *)contentPlayerItem 106 | { 107 | if (contentPlayerItem == nil && _contentPlayerItem != nil) { 108 | [[NSNotificationCenter defaultCenter] removeObserver:self 109 | name:AVPlayerItemDidPlayToEndTimeNotification 110 | object:_contentPlayerItem]; 111 | } 112 | 113 | _contentPlayerItem = contentPlayerItem; 114 | 115 | if (_contentPlayerItem != nil) { 116 | [[NSNotificationCenter defaultCenter] addObserver:self 117 | selector:@selector(contentPlayerItemDidReachEnd:) 118 | name:AVPlayerItemDidPlayToEndTimeNotification 119 | object:_contentPlayerItem]; 120 | } 121 | } 122 | 123 | - (void)contentPlayerItemDidReachEnd:(NSNotification *)notification 124 | { 125 | VLogC(); 126 | 127 | dispatch_async(dispatch_get_main_queue(), ^{ 128 | if (! self.contentPlayerItemDidReachEnd) { 129 | self.playBreaksQueue = [[self.adPlaylist postRollPlayBreaks] mutableCopy]; 130 | [self startPlayBreaksFromQueue]; 131 | } 132 | 133 | self.contentPlayerItemDidReachEnd = YES; 134 | }); 135 | } 136 | 137 | - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context 138 | { 139 | if (context == DVIABPlayerInlineAdPlayerItemStatusObservationContext) { 140 | AVPlayerItemStatus status = [[change objectForKey:NSKeyValueChangeNewKey] integerValue]; 141 | VLog(@"DVIABPlayerInlineAdPlayerItemStatusObservationContext %i", status); 142 | 143 | dispatch_async(dispatch_get_main_queue(), ^{ 144 | switch (status) { 145 | case AVPlayerItemStatusReadyToPlay: 146 | self.playerLayer.player = self.adPlayer; 147 | [self.currentInlineAd trackEvent:@"start"]; 148 | [self.currentInlineAd trackImpressions]; 149 | [self.adPlayer play]; 150 | break; 151 | 152 | case AVPlayerItemStatusFailed: 153 | VLog(@"AVPlayerItemStatusFailed %@", self.currentInlineAdPlayerItem.error); 154 | [self finishCurrentInlineAd:self.currentInlineAdPlayerItem]; 155 | break; 156 | 157 | case AVPlayerItemStatusUnknown: 158 | break; 159 | } 160 | }); 161 | } 162 | else if (context == DVIABContentPlayerRateObservationContext) { 163 | float rate = [[change objectForKey:NSKeyValueChangeNewKey] floatValue]; 164 | VLog(@"DVIABPlayerRateObservationContext %@ %f", self.currentItem, rate); 165 | 166 | dispatch_async(dispatch_get_main_queue(), ^{ 167 | if (rate > 0) { 168 | self.contentPlayerItemDidReachEnd = NO; 169 | 170 | if (CMTimeCompare(CMTimeAbsoluteValue(self.currentItem.currentTime), 171 | CMTimeMake(1, 1)) == -1) { 172 | self.playBreaksQueue = [[self.adPlaylist preRollPlayBreaks] mutableCopy]; 173 | [self startPlayBreaksFromQueue]; 174 | } 175 | } 176 | }); 177 | } 178 | else if (context == DVIABAdPlayerRateObservationContext) { 179 | // Sometimes AVPlayer just stops playback before reaching end. 180 | // In this case we need to call play. 181 | 182 | float rate = [[change objectForKey:NSKeyValueChangeNewKey] floatValue]; 183 | VLog(@"DVIABPlayerRateObservationContext %@ %f", self.currentItem, rate); 184 | 185 | dispatch_async(dispatch_get_main_queue(), ^{ 186 | if (rate == 0 && !paused) { 187 | VLogV(self.playerLayer); 188 | self.playerLayer.player = self.adPlayer; 189 | [self.adPlayer play]; 190 | } 191 | paused = NO; 192 | }); 193 | } 194 | else { 195 | [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; 196 | } 197 | } 198 | 199 | @synthesize playBreaksTimeObserver = _playBreaksTimeObserver; 200 | @synthesize adPlaylist = _adPlaylist; 201 | 202 | - (void)setAdPlaylist:(DVVideoMultipleAdPlaylist *)adPlaylist 203 | { 204 | if (adPlaylist == nil && self.playBreaksTimeObserver != nil) { 205 | [self removeTimeObserver:self.playBreaksTimeObserver]; 206 | [self removeTimeObserver:self.periodicTimeObserver]; 207 | [self removeTimeObserver:self.periodicAdTimeObserver]; 208 | self.playBreaksTimeObserver = nil; 209 | } 210 | 211 | _adPlaylist = adPlaylist; 212 | 213 | if (_adPlaylist != nil) { 214 | NSMutableArray *boundaryTimes = [NSMutableArray array]; 215 | [boundaryTimes addObjectsFromArray:[[_adPlaylist midRollTimes] mutableCopy]]; 216 | 217 | VLogV(boundaryTimes); 218 | 219 | DVIABPlayer* __block player = self; 220 | if (boundaryTimes && boundaryTimes.count) { 221 | self.playBreaksTimeObserver = [self addBoundaryTimeObserverForTimes:boundaryTimes queue:NULL usingBlock:^{ 222 | if (player.currentItem != player.contentPlayerItem) { 223 | return; 224 | } 225 | 226 | CMTime currentTime = player.currentTime; 227 | VLog(@"playBreaksTimeObserver %@", CFBridgingRelease(CMTimeCopyDescription(nil, currentTime))); 228 | 229 | NSArray *playBreaks = [player.adPlaylist midRollPlayBreaksWithTime:currentTime approximate:YES]; 230 | NSCAssert([playBreaks count], @"No play breaks found for boundary time"); 231 | player.playBreaksQueue = [playBreaks mutableCopy]; 232 | [player startPlayBreaksFromQueue]; 233 | }]; 234 | } 235 | 236 | __block CMTime previousTime = kCMTimeNegativeInfinity; 237 | self.periodicTimeObserver = [self addPeriodicTimeObserverForInterval:CMTimeMakeWithSeconds(AD_PLAY_BREAK_MIN_INTERVAL_BETWEEN, 1) queue:NULL usingBlock:^(CMTime time) { 238 | // VLogI((int)time.value); 239 | if (player.currentItem == player.contentPlayerItem && 240 | CMTimeCompare(CMTimeMakeWithSeconds(AD_PLAY_BREAK_MIN_INTERVAL_BETWEEN, 1), 241 | CMTimeAbsoluteValue(CMTimeSubtract(previousTime, time))) == -1) { 242 | previousTime = time; 243 | player.didFinishPlayBreakRecently = NO; 244 | } 245 | }]; 246 | } 247 | } 248 | 249 | - (BOOL)delegateAllowsToPauseForAdBreak:(DVVideoPlayBreak *)playBreak 250 | { 251 | return (! [self.delegate respondsToSelector:@selector(player:shouldPauseForAdBreak:)] || 252 | [self.delegate player:self shouldPauseForAdBreak:playBreak]); 253 | } 254 | 255 | - (void)startPlayBreaksFromQueue 256 | { 257 | VLogV(self.playBreaksQueue); 258 | 259 | if (self.didFinishPlayBreakRecently) { 260 | // previous ad break happened not long ago, skip this one 261 | [self finishPlayBreaksQueue]; 262 | return; 263 | } 264 | 265 | if ([self.playBreaksQueue count] > 0) { 266 | [self pause]; 267 | 268 | DVVideoPlayBreak *brk = [self.playBreaksQueue objectAtIndex:0]; 269 | [self.playBreaksQueue removeObjectAtIndex:0]; 270 | 271 | if (! [self delegateAllowsToPauseForAdBreak:brk]) { 272 | [self startPlayBreaksFromQueue]; 273 | } 274 | 275 | self.currentPlayBreak = brk; 276 | 277 | [self fetchPlayBreakAdTemplate:self.currentPlayBreak]; 278 | } 279 | else { 280 | [self finishPlayBreaksQueue]; 281 | } 282 | } 283 | 284 | - (void)finishPlayBreaksQueue 285 | { 286 | self.didFinishPlayBreakRecently = YES; 287 | self.playerLayer.player = self; 288 | self.adPlayer = nil; 289 | 290 | if (! self.contentPlayerItemDidReachEnd) { 291 | [self play]; 292 | } 293 | } 294 | 295 | - (void)finishCurrentPlayBreak 296 | { 297 | VLogV(self.currentPlayBreak); 298 | 299 | self.currentPlayBreak = nil; 300 | [self startPlayBreaksFromQueue]; 301 | } 302 | 303 | - (void)startAdsFromQueue 304 | { 305 | VLogV(self.adsQueue); 306 | 307 | if ([self.adsQueue count] > 0) { 308 | _currentAd = [self.adsQueue objectAtIndex:0]; 309 | [self.adsQueue removeObjectAtIndex:0]; 310 | 311 | if (_currentAd.playMediaFile) { 312 | if (self.currentInlineAd) { 313 | [self playInlineAd:self.currentInlineAd]; 314 | } else { 315 | NSAssert(NO, @"Not supported"); 316 | [self finishCurrentInlineAd:nil]; 317 | } 318 | } else { 319 | [self finishCurrentInlineAd:nil]; 320 | } 321 | } 322 | else { 323 | [self finishCurrentPlayBreak]; 324 | } 325 | } 326 | 327 | - (void)playInlineAd:(DVInlineVideoAd *)videoAd 328 | { 329 | VLogV(videoAd); 330 | 331 | AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithURL:videoAd.mediaFileURL]; 332 | 333 | [[NSNotificationCenter defaultCenter] addObserver:self 334 | selector:@selector(inlineAdPlayerItemDidReachEnd:) 335 | name:AVPlayerItemDidPlayToEndTimeNotification 336 | object:playerItem]; 337 | [[NSNotificationCenter defaultCenter] addObserver:self 338 | selector:@selector(inlineAdPlayerItemDidFailToReachEnd:) 339 | name:AVPlayerItemFailedToPlayToEndTimeNotification 340 | object:playerItem]; 341 | [playerItem addObserver:self 342 | forKeyPath:@"status" 343 | options:NSKeyValueObservingOptionInitial|NSKeyValueObservingOptionNew 344 | context:DVIABPlayerInlineAdPlayerItemStatusObservationContext]; 345 | 346 | self.currentInlineAdPlayerItem = playerItem; 347 | 348 | self.adPlayer = [[AVPlayer alloc] initWithPlayerItem:playerItem]; 349 | Float64 duration = CMTimeGetSeconds(playerItem.duration); 350 | // VLogF(duration); 351 | typeof(self) SELF = self; 352 | self.firstQuartile = self.midpoint = self.thirdQuartile = NO; 353 | self.periodicAdTimeObserver = [self.adPlayer addPeriodicTimeObserverForInterval:CMTimeMakeWithSeconds(1, 1) queue:NULL usingBlock:^(CMTime time) { 354 | Float64 current = CMTimeGetSeconds(time)/duration; 355 | if (current >= .25 && !SELF.firstQuartile) { 356 | SELF.firstQuartile = YES; 357 | [SELF.currentInlineAd trackEvent:@"firstQuartile"]; 358 | } 359 | if (current >= .5 && !SELF.midpoint) { 360 | SELF.midpoint = YES; 361 | [SELF.currentInlineAd trackEvent:@"midpoint"]; 362 | } 363 | if (current >= .75 && !SELF.thirdQuartile) { 364 | SELF.thirdQuartile = YES; 365 | [SELF.currentInlineAd trackEvent:@"thirdQuartile"]; 366 | } 367 | // VLogF(current); 368 | }]; 369 | 370 | // Other TrackingEvents (Not Implemented) 371 | // —————————————————————————————————————— 372 | // fullscreen 373 | // mute 374 | // unmute 375 | // expand 376 | // collapse 377 | // acceptInvitation 378 | // close 379 | } 380 | 381 | - (void)inlineAdPlayerItemDidReachEnd:(NSNotification *)notification 382 | { 383 | AVPlayerItem *playerItem = [notification object]; 384 | VLogV(playerItem); 385 | 386 | dispatch_async(dispatch_get_main_queue(), ^{ 387 | [self finishCurrentInlineAd:playerItem]; 388 | }); 389 | } 390 | 391 | - (void)inlineAdPlayerItemDidFailToReachEnd:(NSNotification *)notification 392 | { 393 | AVPlayerItem *playerItem = [notification object]; 394 | VLog(@"%@ userInfo:%@", playerItem, [notification userInfo]); 395 | 396 | dispatch_async(dispatch_get_main_queue(), ^{ 397 | [self finishCurrentInlineAd:playerItem]; 398 | }); 399 | } 400 | 401 | - (void)finishCurrentInlineAd:(AVPlayerItem *)playerItem 402 | { 403 | VLogV(playerItem); 404 | 405 | [self.currentInlineAd trackEvent:@"complete"]; 406 | 407 | if (playerItem != nil) { 408 | [[NSNotificationCenter defaultCenter] removeObserver:self 409 | name:AVPlayerItemDidPlayToEndTimeNotification 410 | object:playerItem]; 411 | [[NSNotificationCenter defaultCenter] removeObserver:self 412 | name:AVPlayerItemFailedToPlayToEndTimeNotification 413 | object:playerItem]; 414 | [playerItem removeObserver:self forKeyPath:@"status" 415 | context:DVIABPlayerInlineAdPlayerItemStatusObservationContext]; 416 | } 417 | 418 | [self startAdsFromQueue]; 419 | } 420 | 421 | - (void)pause 422 | { 423 | if (self.adPlayer) { 424 | paused = YES; 425 | [self.currentInlineAd trackEvent:@"pause"]; 426 | [self.adPlayer pause]; 427 | } else { 428 | [super pause]; 429 | } 430 | } 431 | 432 | - (void)play 433 | { 434 | if (self.adPlayer) { 435 | [self.adPlayer play]; 436 | } else { 437 | [super play]; 438 | } 439 | } 440 | 441 | - (void)dealloc 442 | { 443 | self.adPlayer = nil; // remove observers 444 | self.adPlaylist = nil; // to remove time observer 445 | self.contentPlayerItem = nil; // to remove notification observer 446 | [self removeObserver:self forKeyPath:@"rate" 447 | context:DVIABContentPlayerRateObservationContext]; 448 | } 449 | 450 | #pragma mark - Networking 451 | 452 | - (void)fetchPlayBreakAdTemplate:(DVVideoPlayBreak *)playBreak 453 | { 454 | VLogV(playBreak); 455 | 456 | NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:playBreak.adServingTemplateURL 457 | cachePolicy:NSURLRequestReloadIgnoringCacheData 458 | timeoutInterval:AD_REQUEST_TIMEOUT_INTERVAL]; 459 | 460 | // Set HTTP Headers if any where passed. 461 | for (NSString *field in _httpHeaders) { 462 | [request setValue:_httpHeaders[field] forHTTPHeaderField:field]; 463 | } 464 | 465 | NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 466 | if (! connection) { 467 | if ([self.delegate respondsToSelector:@selector(player:didFailPlayBreak:withError:)]) { 468 | NSError *error = [NSError errorWithDomain:DVIABPlayerErrorDomain 469 | code:DVIABPlayerInvalidAdTemplateURLErrorCode 470 | userInfo:nil]; 471 | [self.delegate player:self didFailPlayBreak:playBreak withError:error]; 472 | } 473 | 474 | self.currentPlayBreak = nil; 475 | 476 | [self startPlayBreaksFromQueue]; 477 | return; 478 | } 479 | 480 | self.adRequestData = [NSMutableData data]; 481 | } 482 | 483 | - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 484 | { 485 | self.adRequestData = [NSMutableData data]; 486 | } 487 | 488 | - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 489 | { 490 | [self.adRequestData appendData:data]; 491 | } 492 | 493 | - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 494 | { 495 | self.adRequestData = nil; 496 | 497 | if ([self.delegate respondsToSelector:@selector(player:didFailPlayBreak:withError:)]) { 498 | [self.delegate player:self didFailPlayBreak:self.currentPlayBreak withError:error]; 499 | } 500 | 501 | [self finishCurrentPlayBreak]; 502 | } 503 | 504 | - (void)connectionDidFinishLoading:(NSURLConnection *)connection 505 | { 506 | VLog(@"%@", [[NSString alloc] initWithData:self.adRequestData encoding:NSUTF8StringEncoding]); 507 | 508 | NSError *error = nil; 509 | DVVideoAdServingTemplate *adTemplate = [[DVVideoAdServingTemplate alloc] initWithData:self.adRequestData error:&error]; 510 | self.adRequestData = nil; 511 | if (! adTemplate) { 512 | if ([self.delegate respondsToSelector:@selector(player:didFailPlayBreak:withError:)]) { 513 | [self.delegate player:self didFailPlayBreak:self.currentPlayBreak withError:error]; 514 | } 515 | 516 | [self finishCurrentPlayBreak]; 517 | return; 518 | } 519 | 520 | VLogV(adTemplate.ads); 521 | 522 | if ((adTemplate.ads.count && [adTemplate.ads[0] isKindOfClass:[DVWrapperVideoAd class]]) || self.wrapper) { 523 | if (!self.wrapper) { 524 | self.wrapper = (DVWrapperVideoAd*)adTemplate.ads[0]; 525 | } 526 | if (!self.adsQueue) { // adTemplate.ads.count && adTemplate.ads[0] == wrapper 527 | // Supercharge with the inline ad. 528 | self.adsQueue = [adTemplate.ads mutableCopy]; 529 | [self fetchPlayBreakAdTemplate:_wrapper.videoPlayBreak]; 530 | } else { 531 | // Probably means we've supercharged and we're ready to go! 532 | // TODO: We should move this in DVVideoAdServingTemplate 533 | NSArray *adElements = [adTemplate.document.rootElement elementsForName:@"Ad"]; 534 | VLogV(adElements); 535 | for (DDXMLElement *adElement in adElements) { 536 | DDXMLElement *adContents = (DDXMLElement *)[adElement childAtIndex:0]; 537 | NSString *adContentsName = [adContents name]; 538 | if ([adContentsName isEqualToString:@"InLine"]) { 539 | NSError *error = nil; 540 | [adTemplate populateInlineVideoAd:_wrapper withXMLElement:adContents error:&error]; 541 | if (error) { 542 | VLogV(error); 543 | } 544 | } 545 | } 546 | self.adsQueue = [@[_wrapper] mutableCopy]; 547 | [self startAdsFromQueue]; 548 | } 549 | } else { 550 | // Simply play the inline ad. 551 | self.adsQueue = [adTemplate.ads mutableCopy]; 552 | [self startAdsFromQueue]; 553 | } 554 | } 555 | 556 | @end 557 | --------------------------------------------------------------------------------