Skip to content

Commit

Permalink
Merge branch 'feature/airplay-stability' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
pyby committed Dec 15, 2017
2 parents d08e485 + ca35628 commit 28766c9
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 28 deletions.
6 changes: 6 additions & 0 deletions Demo/Resources/VideoDemoConfiguration.plist
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@
<key>url</key>
<string>http://srgplayerswivod-vh.akamaihd.net/i/2016/09/13/,Hoechstgelegener_Windpark-42441158-360k,Hoechstgelegener_Windpark-42441158-720k,.mp4.csmil/master.m3u8</string>
</dict>
<dict>
<key>name</key>
<string>19h30</string>
<key>url</key>
<string>https://rtsvodww-vh.akamaihd.net/i/tj/2017/tj_20171011_full_f_1040162-,301k,101k,701k,1201k,2001k,.mp4.csmil/master.m3u8?__b__=800</string>
</dict>
<dict>
<key>name</key>
<string>Gotthard 360 video</string>
Expand Down
20 changes: 1 addition & 19 deletions Framework/Sources/Controllers/SRGMediaPlayerController.m
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ @interface SRGMediaPlayerController () {
@property (nonatomic, weak) id<SRGSegment> targetSegment; // Will be nilled when reached
@property (nonatomic, weak) id<SRGSegment> currentSegment;

@property (nonatomic, getter=isTogglingAirplay) BOOL togglingAirplay;

@property (nonatomic) AVPictureInPictureController *pictureInPictureController;

@property (nonatomic) NSValue *startTimeValue; // Will be nilled when reached
Expand Down Expand Up @@ -185,14 +183,6 @@ - (void)setPlayer:(AVPlayer *)player
@strongify(self)
@strongify(player)

// Inhibit rate changes due to Airplay being enabled or disabled, until the original rate has been restored
if (self.togglingAirplay) {
if (player.rate != 0.f) {
self.togglingAirplay = NO;
}
return;
}

AVPlayerItem *playerItem = player.currentItem;

// Only respond to rate changes when the item is ready to play
Expand Down Expand Up @@ -223,16 +213,8 @@ - (void)setPlayer:(AVPlayer *)player

[player srg_addMainThreadObserver:self keyPath:@keypath(player.externalPlaybackActive) options:0 block:^(MAKVONotification *notification) {
@strongify(self)
@strongify(player)

[[NSNotificationCenter defaultCenter] postNotificationName:SRGMediaPlayerExternalPlaybackStateDidChangeNotification object:self];

// When entering or exiting Airplay, the player rate will temporarily be set to 0 if it wasn't. To avoid generating
// incorrect controller state transitions, we inhibit player rate change observations until the rate has been
// restored
if (player.rate != 0.f) {
self.togglingAirplay = YES;
}
}];

[player srg_addMainThreadObserver:self keyPath:@keypath(player.currentItem.playbackLikelyToKeepUp) options:0 block:^(MAKVONotification *notification) {
Expand Down Expand Up @@ -354,7 +336,7 @@ - (CMTimeRange)timeRange

if (! firstSeekableTimeRangeValue || CMTIMERANGE_IS_INVALID(firstSeekableTimeRange)
|| ! lastSeekableTimeRangeValue || CMTIMERANGE_IS_INVALID(lastSeekableTimeRange)) {
return playerItem.loadedTimeRanges ? kCMTimeRangeZero : kCMTimeRangeInvalid;
return (playerItem.loadedTimeRanges.count != 0) ? kCMTimeRangeZero : kCMTimeRangeInvalid;
}

CMTimeRange timeRange = CMTimeRangeFromTimeToTime(firstSeekableTimeRange.start, CMTimeRangeGetEnd(lastSeekableTimeRange));
Expand Down
6 changes: 6 additions & 0 deletions Framework/Sources/Views/SRGMediaPlayerView.m
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,16 @@ - (instancetype)initWithCoder:(NSCoder *)aDecoder
- (void)setPlayer:(AVPlayer *)player
{
[_player removeObserver:self keyPath:@keypath(_player.currentItem.tracks)];
[_player removeObserver:self keyPath:@keypath(_player.externalPlaybackActive)];

_player = player;

[player srg_addMainThreadObserver:self keyPath:@keypath(player.currentItem.tracks) options:0 block:^(MAKVONotification *notification) {
[self updateSubviews];
}];
[player srg_addMainThreadObserver:self keyPath:@keypath(player.externalPlaybackActive) options:0 block:^(MAKVONotification *notification) {
[self updateSubviews];
}];

[self updateSubviewsWithPlayer:player];
}
Expand Down Expand Up @@ -99,6 +103,8 @@ - (void)updateSubviews
- (void)updateSubviewsWithPlayer:(AVPlayer *)player
{
if (player) {
self.playbackView.hidden = player.externalPlaybackActive;

AVAssetTrack *videoAssetTrack = [player.currentItem srg_assetTracksWithMediaType:AVMediaTypeVideo].firstObject;
if (videoAssetTrack) {
CGSize assetDimensions = CGSizeApplyAffineTransform(videoAssetTrack.naturalSize, videoAssetTrack.preferredTransform);
Expand Down
22 changes: 13 additions & 9 deletions Tests/Sources/PlaybackTestCase.m
Original file line number Diff line number Diff line change
Expand Up @@ -752,25 +752,29 @@ - (void)testLivePause
[self waitForExpectationsWithTimeout:20. handler:nil];
}

- (void)testMediaInformationAvailabilityAfterPrepare
- (void)testMediaInformationWhenPreparingToPlay
{
XCTestExpectation *preparationExpectation = [self expectationWithDescription:@"Prepared"];
XCTAssertEqual(self.mediaPlayerController.mediaType, SRGMediaPlayerMediaTypeUnknown);
XCTAssertEqual(self.mediaPlayerController.streamType, SRGMediaPlayerStreamTypeUnknown);

[self mpt_expectationForNotification:SRGMediaPlayerPlaybackStateDidChangeNotification object:self.mediaPlayerController handler:^BOOL(NSNotification * _Nonnull notification) {
XCTAssertEqual([notification.userInfo[SRGMediaPlayerPlaybackStateKey] integerValue], SRGMediaPlayerPlaybackStatePreparing);
XCTAssertEqual(self.mediaPlayerController.mediaType, SRGMediaPlayerMediaTypeUnknown);
XCTAssertEqual(self.mediaPlayerController.streamType, SRGMediaPlayerStreamTypeUnknown);
return YES;
}];

XCTestExpectation *expectation = [self expectationWithDescription:@"Prepared"];

[self.mediaPlayerController prepareToPlayURL:OnDemandTestURL() atTime:kCMTimeZero withSegments:nil userInfo:nil completionHandler:^{
XCTAssertEqual(self.mediaPlayerController.mediaType, SRGMediaPlayerMediaTypeVideo);
XCTAssertEqual(self.mediaPlayerController.streamType, SRGMediaPlayerStreamTypeOnDemand);
[preparationExpectation fulfill];
[expectation fulfill];
}];

[self waitForExpectationsWithTimeout:30. handler:nil];
}

- (void)testMediaInformationAvailabilityBeforePrepare
{
XCTAssertEqual(self.mediaPlayerController.mediaType, SRGMediaPlayerMediaTypeUnknown);
XCTAssertEqual(self.mediaPlayerController.streamType, SRGMediaPlayerStreamTypeUnknown);
}

- (void)testOnDemandProperties
{
[self mpt_expectationForNotification:SRGMediaPlayerPlaybackStateDidChangeNotification object:self.mediaPlayerController handler:^BOOL(NSNotification * _Nonnull notification) {
Expand Down

0 comments on commit 28766c9

Please sign in to comment.