axmol/core/ui/UIVideoPlayer-ios.mm

533 lines
15 KiB
Plaintext
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2014-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
Copyright (c) 2022 Bytedance Inc.
2019-11-23 20:27:39 +08:00
2022-10-01 16:24:52 +08:00
https://axmolengine.github.io/
2019-11-23 20:27:39 +08:00
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "ui/UIVideoPlayer.h"
2019-11-23 20:27:39 +08:00
// No Available on tvOS
2022-07-16 10:43:05 +08:00
#if AX_TARGET_PLATFORM == AX_PLATFORM_IOS && !defined(AX_TARGET_OS_TVOS)
2019-11-23 20:27:39 +08:00
2022-08-08 18:02:17 +08:00
using namespace ax::ui;
2019-11-23 20:27:39 +08:00
//-------------------------------------------------------------------------------------
# include "platform/ios/CCEAGLView-ios.h"
# import <AVKit/AVPlayerViewController.h>
# import <CoreMedia/CMTime.h>
# include "base/CCDirector.h"
# include "platform/CCFileUtils.h"
2019-11-23 20:27:39 +08:00
@interface UIVideoViewWrapperIos : NSObject
typedef NS_ENUM(NSInteger, PlayerbackState) {
PlayerbackStateUnknown = 0,
PlayerbackStatePaused,
PlayerbackStopped,
PlayerbackStatePlaying,
PlayerbackStateCompleted
};
@property(assign, nonatomic) AVPlayerViewController* playerController;
- (void)setFrame:(int)left:(int)top:(int)width:(int)height;
- (void)setURL:(int)videoSource:(std::string&)videoUrl;
- (void)play;
- (void)pause;
- (void)resume;
- (void)stop;
- (void)seekTo:(float)sec;
- (void)setVisible:(BOOL)visible;
- (void)setKeepRatioEnabled:(BOOL)enabled;
- (void)setFullScreenEnabled:(BOOL)enabled;
- (BOOL)isFullScreenEnabled;
- (void)showPlaybackControls:(BOOL)value;
- (void)setRepeatEnabled:(BOOL)enabled;
- (void)setUserInteractionEnabled:(BOOL)userInteractionEnabled;
- (id)init:(void*)videoPlayer;
- (void)videoFinished:(NSNotification*)notification;
2019-11-23 20:27:39 +08:00
@end
@implementation UIVideoViewWrapperIos {
2019-11-23 20:27:39 +08:00
int _left;
int _top;
int _width;
int _height;
BOOL _keepRatioEnabled;
BOOL _repeatEnabled;
BOOL _showPlaybackControls;
BOOL _userInteractionEnabled;
PlayerbackState _state;
VideoPlayer* _videoPlayer;
}
- (id)init:(void*)videoPlayer
2019-11-23 20:27:39 +08:00
{
if (self = [super init])
{
2019-11-23 20:27:39 +08:00
self.playerController = [AVPlayerViewController new];
[self setRepeatEnabled:FALSE];
[self showPlaybackControls:TRUE];
[self setUserInteractionEnabled:TRUE];
[self setKeepRatioEnabled:FALSE];
2019-11-23 20:27:39 +08:00
_videoPlayer = (VideoPlayer*)videoPlayer;
_state = PlayerbackStateUnknown;
2019-11-23 20:27:39 +08:00
}
return self;
}
- (void)dealloc
2019-11-23 20:27:39 +08:00
{
_videoPlayer = nullptr;
[self clean];
[self.playerController release];
[super dealloc];
}
- (void)clean
2019-11-23 20:27:39 +08:00
{
[self stop];
[self removePlayerEventListener];
[self.playerController.view removeFromSuperview];
}
- (void)setFrame:(int)left:(int)top:(int)width:(int)height
2019-11-23 20:27:39 +08:00
{
_left = left;
_width = width;
_top = top;
2019-11-23 20:27:39 +08:00
_height = height;
[self.playerController.view setFrame:CGRectMake(left, top, width, height)];
}
- (void)setFullScreenEnabled:(BOOL)enabled
2019-11-23 20:27:39 +08:00
{
// AVPlayerViewController doesn't provide API to enable fullscreen. But you can toggle
// fullsreen by the playback controllers.
}
- (BOOL)isFullScreenEnabled
2019-11-23 20:27:39 +08:00
{
return false;
}
- (void)showPlaybackControls:(BOOL)value
2019-11-23 20:27:39 +08:00
{
_showPlaybackControls = value;
2019-11-23 20:27:39 +08:00
self.playerController.showsPlaybackControls = value;
}
- (void)setRepeatEnabled:(BOOL)enabled
2019-11-23 20:27:39 +08:00
{
_repeatEnabled = enabled;
if (self.playerController.player)
{
2019-11-23 20:27:39 +08:00
if (_repeatEnabled)
self.playerController.player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
else
self.playerController.player.actionAtItemEnd = AVPlayerActionAtItemEndPause;
}
}
- (void)setUserInteractionEnabled:(BOOL)userInteractionEnabled
2019-11-23 20:27:39 +08:00
{
_userInteractionEnabled = userInteractionEnabled;
2019-11-23 20:27:39 +08:00
self.playerController.view.userInteractionEnabled = _userInteractionEnabled;
}
- (void)setURL:(int)videoSource:(std::string&)videoUrl
2019-11-23 20:27:39 +08:00
{
[self clean];
if (videoSource == 1)
self.playerController.player =
[[[AVPlayer alloc] initWithURL:[NSURL URLWithString:@(videoUrl.c_str())]] autorelease];
2019-11-23 20:27:39 +08:00
else
self.playerController.player =
[[[AVPlayer alloc] initWithURL:[NSURL fileURLWithPath:@(videoUrl.c_str())]] autorelease];
2019-11-23 20:27:39 +08:00
[self setRepeatEnabled:_repeatEnabled];
[self setKeepRatioEnabled:_keepRatioEnabled];
[self setUserInteractionEnabled:_userInteractionEnabled];
[self showPlaybackControls:_showPlaybackControls];
2022-08-08 18:02:17 +08:00
auto view = ax::Director::getInstance()->getOpenGLView();
auto eaglView = (CCEAGLView*)view->getEAGLView();
[eaglView addSubview:self.playerController.view];
2019-11-23 20:27:39 +08:00
[self registerPlayerEventListener];
}
- (void)videoFinished:(NSNotification*)notification
2019-11-23 20:27:39 +08:00
{
if (_videoPlayer != nullptr)
{
2019-11-23 20:27:39 +08:00
_videoPlayer->onPlayEvent((int)VideoPlayer::EventType::COMPLETED);
_state = PlayerbackStateCompleted;
if (_repeatEnabled)
{
2019-11-23 20:27:39 +08:00
[self seekTo:0];
[self play];
}
}
}
- (void)seekTo:(float)sec
2019-11-23 20:27:39 +08:00
{
if (self.playerController.player)
[self.playerController.player seekToTime:CMTimeMake(sec, 1)];
}
- (void)setVisible:(BOOL)visible
2019-11-23 20:27:39 +08:00
{
[self.playerController.view setHidden:!visible];
}
- (void)setKeepRatioEnabled:(BOOL)enabled
2019-11-23 20:27:39 +08:00
{
_keepRatioEnabled = enabled;
if (_keepRatioEnabled)
self.playerController.videoGravity = AVLayerVideoGravityResizeAspect;
else
self.playerController.videoGravity = AVLayerVideoGravityResizeAspectFill;
}
- (void)play
2019-11-23 20:27:39 +08:00
{
if (self.playerController.player && _state != PlayerbackStatePlaying)
{
2019-11-23 20:27:39 +08:00
[self.playerController.player play];
_state = PlayerbackStatePlaying;
_videoPlayer->onPlayEvent((int)VideoPlayer::EventType::PLAYING);
}
}
- (void)pause
2019-11-23 20:27:39 +08:00
{
if (self.playerController.player && _state == PlayerbackStatePlaying)
{
2019-11-23 20:27:39 +08:00
[self.playerController.player pause];
_state = PlayerbackStatePaused;
_videoPlayer->onPlayEvent((int)VideoPlayer::EventType::PAUSED);
}
}
- (void)resume
2019-11-23 20:27:39 +08:00
{
if (self.playerController.player && _state == PlayerbackStatePaused)
[self play];
}
- (void)stop
2019-11-23 20:27:39 +08:00
{
// AVPlayer doesn't have stop, so just pause it, and seek time to 0.
if (self.playerController.player && _state != PlayerbackStopped)
{
2019-11-23 20:27:39 +08:00
[self seekTo:0];
[self.playerController.player pause];
_state = PlayerbackStopped;
2019-11-23 20:27:39 +08:00
// stop() will be invoked in dealloc, which is invoked by _videoPlayer's destructor,
// so do't send the message when _videoPlayer is being deleted.
if (_videoPlayer)
_videoPlayer->onPlayEvent((int)VideoPlayer::EventType::STOPPED);
}
}
- (void)registerPlayerEventListener
2019-11-23 20:27:39 +08:00
{
if (self.playerController.player)
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(videoFinished:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:self.playerController.player.currentItem];
2019-11-23 20:27:39 +08:00
}
- (void)removePlayerEventListener
2019-11-23 20:27:39 +08:00
{
if (self.playerController.player)
[[NSNotificationCenter defaultCenter] removeObserver:self
name:AVPlayerItemDidPlayToEndTimeNotification
object:self.playerController.player.currentItem];
2019-11-23 20:27:39 +08:00
}
@end
//------------------------------------------------------------------------------------------------------------
VideoPlayer::VideoPlayer()
{
_videoContext = [[UIVideoViewWrapperIos alloc] init:this];
2019-11-23 20:27:39 +08:00
2022-07-16 10:43:05 +08:00
# if AX_VIDEOPLAYER_DEBUG_DRAW
2019-11-23 20:27:39 +08:00
_debugDrawNode = DrawNode::create();
addChild(_debugDrawNode);
# endif
2019-11-23 20:27:39 +08:00
}
VideoPlayer::~VideoPlayer()
{
if (_videoContext)
2019-11-23 20:27:39 +08:00
{
[((UIVideoViewWrapperIos*)_videoContext) dealloc];
2019-11-23 20:27:39 +08:00
}
}
void VideoPlayer::setFileName(std::string_view fileName)
2019-11-23 20:27:39 +08:00
{
_videoURL = FileUtils::getInstance()->fullPathForFilename(fileName);
2019-11-23 20:27:39 +08:00
_videoSource = VideoPlayer::Source::FILENAME;
[((UIVideoViewWrapperIos*)_videoContext) setURL:(int) _videoSource:_videoURL];
2019-11-23 20:27:39 +08:00
}
void VideoPlayer::setURL(std::string_view videoUrl)
2019-11-23 20:27:39 +08:00
{
_videoURL = videoUrl;
2019-11-23 20:27:39 +08:00
_videoSource = VideoPlayer::Source::URL;
[((UIVideoViewWrapperIos*)_videoContext) setURL:(int) _videoSource:_videoURL];
2019-11-23 20:27:39 +08:00
}
void VideoPlayer::setLooping(bool looping)
{
_isLooping = looping;
[((UIVideoViewWrapperIos*)_videoContext) setRepeatEnabled:_isLooping];
2019-11-23 20:27:39 +08:00
}
void VideoPlayer::setUserInputEnabled(bool enableInput)
{
_isUserInputEnabled = enableInput;
[((UIVideoViewWrapperIos*)_videoContext) setUserInteractionEnabled:enableInput];
2019-11-23 20:27:39 +08:00
}
void VideoPlayer::setStyle(StyleType style)
{
_styleType = style;
switch (style)
{
case StyleType::DEFAULT:
[((UIVideoViewWrapperIos*)_videoContext) showPlaybackControls:TRUE];
break;
2019-11-23 20:27:39 +08:00
case StyleType::NONE:
[((UIVideoViewWrapperIos*)_videoContext) showPlaybackControls:FALSE];
break;
2019-11-23 20:27:39 +08:00
}
}
void VideoPlayer::setPlayRate(float fRate) {}
void VideoPlayer::draw(Renderer* renderer, const Mat4& transform, uint32_t flags)
2019-11-23 20:27:39 +08:00
{
2022-08-08 18:02:17 +08:00
ax::ui::Widget::draw(renderer, transform, flags);
2019-11-23 20:27:39 +08:00
if (flags & FLAGS_TRANSFORM_DIRTY)
{
auto directorInstance = Director::getInstance();
auto glView = directorInstance->getOpenGLView();
auto frameSize = glView->getFrameSize();
auto scaleFactor = [static_cast<CCEAGLView*>(glView->getEAGLView()) contentScaleFactor];
2019-11-23 20:27:39 +08:00
auto winSize = directorInstance->getWinSize();
auto leftBottom = convertToWorldSpace(Vec2::ZERO);
auto rightTop = convertToWorldSpace(Vec2(_contentSize.width, _contentSize.height));
2019-11-23 20:27:39 +08:00
auto uiLeft = (frameSize.width / 2 + (leftBottom.x - winSize.width / 2) * glView->getScaleX()) / scaleFactor;
auto uiTop = (frameSize.height / 2 - (rightTop.y - winSize.height / 2) * glView->getScaleY()) / scaleFactor;
2019-11-23 20:27:39 +08:00
[((UIVideoViewWrapperIos*)_videoContext) setFrame:
uiLeft:uiTop:(rightTop.x - leftBottom.x) * glView->getScaleX() /
scaleFactor:((rightTop.y - leftBottom.y) * glView->getScaleY() /
scaleFactor)];
2019-11-23 20:27:39 +08:00
}
2022-07-16 10:43:05 +08:00
# if AX_VIDEOPLAYER_DEBUG_DRAW
2019-11-23 20:27:39 +08:00
_debugDrawNode->clear();
auto size = getContentSize();
Point vertices[4] = {Point::ZERO, Point(size.width, 0), Point(size.width, size.height), Point(0, size.height)};
2019-11-23 20:27:39 +08:00
_debugDrawNode->drawPoly(vertices, 4, true, Color4F(1.0, 1.0, 1.0, 1.0));
# endif
2019-11-23 20:27:39 +08:00
}
bool VideoPlayer::isFullScreenEnabled() const
2019-11-23 20:27:39 +08:00
{
return [((UIVideoViewWrapperIos*)_videoContext) isFullScreenEnabled];
2019-11-23 20:27:39 +08:00
}
void VideoPlayer::setFullScreenEnabled(bool enabled)
{
[((UIVideoViewWrapperIos*)_videoContext) setFullScreenEnabled:enabled];
2019-11-23 20:27:39 +08:00
}
void VideoPlayer::setKeepAspectRatioEnabled(bool enable)
{
if (_keepAspectRatioEnabled != enable)
{
_keepAspectRatioEnabled = enable;
[((UIVideoViewWrapperIos*)_videoContext) setKeepRatioEnabled:enable];
2019-11-23 20:27:39 +08:00
}
}
void VideoPlayer::play()
{
if (!_videoURL.empty())
2019-11-23 20:27:39 +08:00
{
[((UIVideoViewWrapperIos*)_videoContext) play];
2019-11-23 20:27:39 +08:00
}
}
void VideoPlayer::pause()
{
if (!_videoURL.empty())
2019-11-23 20:27:39 +08:00
{
[((UIVideoViewWrapperIos*)_videoContext) pause];
2019-11-23 20:27:39 +08:00
}
}
void VideoPlayer::resume()
{
if (!_videoURL.empty())
2019-11-23 20:27:39 +08:00
{
[((UIVideoViewWrapperIos*)_videoContext) resume];
2019-11-23 20:27:39 +08:00
}
}
void VideoPlayer::stop()
{
if (!_videoURL.empty())
2019-11-23 20:27:39 +08:00
{
[((UIVideoViewWrapperIos*)_videoContext) stop];
2019-11-23 20:27:39 +08:00
}
}
void VideoPlayer::seekTo(float sec)
{
if (!_videoURL.empty())
2019-11-23 20:27:39 +08:00
{
[((UIVideoViewWrapperIos*)_videoContext) seekTo:sec];
2019-11-23 20:27:39 +08:00
}
}
bool VideoPlayer::isPlaying() const
{
return _isPlaying;
}
bool VideoPlayer::isLooping() const
{
return _isLooping;
}
bool VideoPlayer::isUserInputEnabled() const
{
return _isUserInputEnabled;
}
void VideoPlayer::setVisible(bool visible)
{
2022-08-08 18:02:17 +08:00
ax::ui::Widget::setVisible(visible);
2019-11-23 20:27:39 +08:00
if (!visible)
{
[((UIVideoViewWrapperIos*)_videoContext) setVisible:NO];
2019-11-23 20:27:39 +08:00
}
else if (isRunning())
2019-11-23 20:27:39 +08:00
{
[((UIVideoViewWrapperIos*)_videoContext) setVisible:YES];
2019-11-23 20:27:39 +08:00
}
}
void VideoPlayer::onEnter()
{
Widget::onEnter();
if (isVisible())
{
[((UIVideoViewWrapperIos*)_videoContext) setVisible:YES];
2019-11-23 20:27:39 +08:00
}
}
void VideoPlayer::onExit()
{
Widget::onExit();
[((UIVideoViewWrapperIos*)_videoContext) setVisible:NO];
2019-11-23 20:27:39 +08:00
}
void VideoPlayer::addEventListener(const VideoPlayer::ccVideoPlayerCallback& callback)
{
_eventCallback = callback;
}
void VideoPlayer::onPlayEvent(int event)
{
if (event == (int)VideoPlayer::EventType::PLAYING)
{
2019-11-23 20:27:39 +08:00
_isPlaying = true;
}
else
{
2019-11-23 20:27:39 +08:00
_isPlaying = false;
}
if (_eventCallback)
{
_eventCallback(this, (VideoPlayer::EventType)event);
}
}
void VideoPlayer::setContentSize(const Size& contentSize)
{
Widget::setContentSize(contentSize);
// reinterpret_cast<PrivateVideoDescriptor*>(_videoContext)->_originalViewSize = contentSize;
}
2022-08-08 18:02:17 +08:00
ax::ui::Widget* VideoPlayer::createCloneInstance()
2019-11-23 20:27:39 +08:00
{
return VideoPlayer::create();
}
void VideoPlayer::copySpecialProperties(Widget* widget)
2019-11-23 20:27:39 +08:00
{
VideoPlayer* videoPlayer = dynamic_cast<VideoPlayer*>(widget);
if (videoPlayer)
{
_isPlaying = videoPlayer->_isPlaying;
_isLooping = videoPlayer->_isLooping;
_isUserInputEnabled = videoPlayer->_isUserInputEnabled;
_styleType = videoPlayer->_styleType;
_fullScreenEnabled = videoPlayer->_fullScreenEnabled;
_fullScreenDirty = videoPlayer->_fullScreenDirty;
_videoURL = videoPlayer->_videoURL;
2019-11-23 20:27:39 +08:00
_keepAspectRatioEnabled = videoPlayer->_keepAspectRatioEnabled;
_videoSource = videoPlayer->_videoSource;
_videoPlayerIndex = videoPlayer->_videoPlayerIndex;
_eventCallback = videoPlayer->_eventCallback;
2019-11-23 20:27:39 +08:00
}
}
#endif