QtPlayer.cpp
Go to the documentation of this file.
/**
* @file
* @brief Source file for QtPlayer class
* @author Duzy Chan <code@duzy.info>
* @author Jonathan Thomas <jonathan@openshot.org>
*
* @section LICENSE
*
* Copyright (c) 2008-2014 OpenShot Studios, LLC
* <http://www.openshotstudios.com/>. This file is part of
* OpenShot Library (libopenshot), an open-source project dedicated to
* delivering high quality video editing and animation solutions to the
* world. For more information visit <http://www.openshot.org/>.
*
* OpenShot Library (libopenshot) is free software: you can redistribute it
* and/or modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* OpenShot Library (libopenshot) is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "../include/Clip.h"
#include "../include/FFmpegReader.h"
#include "../include/Timeline.h"
#include "../include/QtPlayer.h"
#include "../include/Qt/PlayerPrivate.h"
#include "../include/Qt/VideoRenderer.h"
using namespace openshot;
QtPlayer::QtPlayer() : PlayerBase(), p(new PlayerPrivate(new VideoRenderer())), threads_started(false)
{
reader = NULL;
}
QtPlayer::QtPlayer(RendererBase *rb) : PlayerBase(), p(new PlayerPrivate(rb)), threads_started(false)
{
reader = NULL;
}
QtPlayer::~QtPlayer()
{
if (mode != PLAYBACK_STOPPED)
Stop();
delete p;
}
void QtPlayer::CloseAudioDevice()
{
// Close audio device (only do this once, when all audio playback is finished)
AudioDeviceManagerSingleton::Instance(0)->CloseAudioDevice();
}
void QtPlayer::SetSource(const std::string &source)
{
FFmpegReader *ffreader = new FFmpegReader(source);
ffreader->DisplayInfo();
//reader = new FrameMapper(ffreader, ffreader->info.fps, PULLDOWN_NONE, ffreader->info.sample_rate, ffreader->info.channels, ffreader->info.channel_layout);
reader = new Timeline(ffreader->info.width, ffreader->info.height, ffreader->info.fps, ffreader->info.sample_rate, ffreader->info.channels, ffreader->info.channel_layout);
Clip *c = new Clip(source);
Timeline* tm = (Timeline*)reader;
tm->AddClip(c);
tm->Open();
// ZmqLogger::Instance()->Path("/home/jonathan/.openshot_qt/libopenshot.log");
// ZmqLogger::Instance()->Enable(true);
// Set the reader
Reader(reader);
}
void QtPlayer::Play()
{
// Set mode to playing, and speed to normal
mode = PLAYBACK_PLAY;
Speed(1);
if (reader && !threads_started) {
// Start thread only once
p->startPlayback();
threads_started = true;
}
}
void QtPlayer::Loading()
{
mode = PLAYBACK_LOADING;
}
/// Get the current mode
PlaybackMode QtPlayer::Mode()
{
return mode;
}
void QtPlayer::Pause()
{
mode = PLAYBACK_PAUSED;
Speed(0);
}
int QtPlayer::Position()
{
return p->video_position;
}
void QtPlayer::Seek(int64_t new_frame)
{
// Check for seek
if (reader && threads_started && new_frame > 0) {
// Notify cache thread that seek has occurred
p->videoCache->Seek(new_frame);
// Update current position
p->video_position = new_frame;
// Clear last position (to force refresh)
p->last_video_position = 0;
// Notify audio thread that seek has occurred
p->audioPlayback->Seek(new_frame);
}
}
void QtPlayer::Stop()
{
// Change mode to stopped
mode = PLAYBACK_STOPPED;
// Notify threads of stopping
if (reader && threads_started) {
p->videoCache->Stop();
p->audioPlayback->Stop();
// Kill all threads
p->stopPlayback();
}
p->video_position = 0;
threads_started = false;
}
// Set the reader object
void QtPlayer::Reader(ReaderBase *new_reader)
{
// Set new reader. Note: Be sure to close and dispose of the old reader after calling this
reader = new_reader;
p->reader = new_reader;
p->videoCache->Reader(new_reader);
p->audioPlayback->Reader(new_reader);
}
// Get the current reader, such as a FFmpegReader
ReaderBase* QtPlayer::Reader() {
return reader;
}
// Set the QWidget pointer to display the video on (as a LONG pointer id)
void QtPlayer::SetQWidget(int64_t qwidget_address) {
// Update override QWidget address on the video renderer
p->renderer->OverrideWidget(qwidget_address);
}
// Get the Renderer pointer address (for Python to cast back into a QObject)
int64_t QtPlayer::GetRendererQObject() {
return (int64_t)(VideoRenderer*)p->renderer;
}
// Get the Playback speed
float QtPlayer::Speed() {
return speed;
}
// Set the Playback speed multiplier (1.0 = normal speed, <1.0 = slower, >1.0 faster)
void QtPlayer::Speed(float new_speed) {
speed = new_speed;
p->speed = new_speed;
p->videoCache->setSpeed(new_speed);
if (p->reader->info.has_audio)
p->audioPlayback->setSpeed(new_speed);
}
// Get the Volume
float QtPlayer::Volume() {
return volume;
}
// Set the Volume multiplier (1.0 = normal volume, <1.0 = quieter, >1.0 louder)
void QtPlayer::Volume(float new_volume) {
volume = new_volume;
}
openshot::QtPlayer::Seek
void Seek(int64_t new_frame)
Seek to a specific frame in the player.
Definition: QtPlayer.cpp:117
openshot::ReaderInfo::sample_rate
int sample_rate
The number of audio samples per second (44100 is a common sample rate)
Definition: ReaderBase.h:81
openshot::QtPlayer::CloseAudioDevice
void CloseAudioDevice()
Close audio device.
Definition: QtPlayer.cpp:56
openshot::PLAYBACK_PLAY
@ PLAYBACK_PLAY
Play the video normally.
Definition: PlayerBase.h:45
openshot::QtPlayer::Position
int Position()
Get the current frame number being played.
Definition: QtPlayer.cpp:112
openshot::Timeline::AddClip
void AddClip(Clip *clip)
Add an openshot::Clip to the timeline.
Definition: Timeline.cpp:71
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: AudioBufferSource.h:45
openshot::QtPlayer::Stop
void Stop()
Stop the video player and clear the cached frames.
Definition: QtPlayer.cpp:135
openshot::QtPlayer::Reader
ReaderBase * Reader()
Get the current reader, such as a FFmpegReader.
Definition: QtPlayer.cpp:164
openshot::Clip
This class represents a clip (used to arrange readers on the timeline)
Definition: Clip.h:109
openshot::QtPlayer::QtPlayer
QtPlayer()
Default constructor.
Definition: QtPlayer.cpp:38
openshot::ReaderInfo::channel_layout
ChannelLayout channel_layout
The channel layout (mono, stereo, 5 point surround, etc...)
Definition: ReaderBase.h:83
openshot::QtPlayer::Play
void Play()
Play the video.
Definition: QtPlayer.cpp:82
openshot::ReaderInfo::width
int width
The width of the video (in pixesl)
Definition: ReaderBase.h:67
openshot::ReaderBase::info
ReaderInfo info
Information about the current media file.
Definition: ReaderBase.h:112
openshot::QtPlayer::Loading
void Loading()
Display a loading animation.
Definition: QtPlayer.cpp:95
openshot::PlayerBase::reader
ReaderBase * reader
Definition: PlayerBase.h:63
openshot::PLAYBACK_STOPPED
@ PLAYBACK_STOPPED
Stop playing the video (clear cache, done with player)
Definition: PlayerBase.h:48
openshot::ReaderBase::DisplayInfo
void DisplayInfo()
Display file information in the standard output stream (stdout)
Definition: ReaderBase.cpp:66
openshot::ReaderInfo::height
int height
The height of the video (in pixels)
Definition: ReaderBase.h:66
openshot::PlayerBase::volume
float volume
Definition: PlayerBase.h:62
openshot::QtPlayer::Pause
void Pause()
Pause the video.
Definition: QtPlayer.cpp:106
openshot::PlayerPrivate
The private part of QtPlayer class, which contains an audio thread and video thread,...
Definition: PlayerPrivate.h:47
openshot::ReaderInfo::has_audio
bool has_audio
Determines if this file has an audio stream.
Definition: ReaderBase.h:62
openshot::QtPlayer::Mode
PlaybackMode Mode()
Get the current mode.
Definition: QtPlayer.cpp:101
openshot::Timeline
This class represents a timeline.
Definition: Timeline.h:145
VideoRenderer
Definition: VideoRenderer.h:39
openshot::PlaybackMode
PlaybackMode
This enumeration determines the mode of the video player (i.e. playing, paused, etc....
Definition: PlayerBase.h:43
openshot::Timeline::Open
void Open()
Open the reader (and start consuming resources)
Definition: Timeline.cpp:634
openshot::AudioDeviceManagerSingleton::Instance
static AudioDeviceManagerSingleton * Instance(int numChannels)
Create or get an instance of this singleton (invoke the class with this method)
Definition: AudioPlaybackThread.cpp:38
openshot::FFmpegReader
This class uses the FFmpeg libraries, to open video files and audio files, and return openshot::Frame...
Definition: FFmpegReader.h:92
openshot::PlayerBase::speed
float speed
Definition: PlayerBase.h:61
openshot::QtPlayer::Speed
float Speed()
Get the Playback speed.
Definition: QtPlayer.cpp:180
openshot::QtPlayer::Volume
float Volume()
Get the Volume.
Definition: QtPlayer.cpp:194
openshot::PLAYBACK_LOADING
@ PLAYBACK_LOADING
Loading the video (display a loading animation)
Definition: PlayerBase.h:47
openshot::QtPlayer::SetQWidget
void SetQWidget(int64_t qwidget_address)
Definition: QtPlayer.cpp:169
openshot::RendererBase
This is the base class of all Renderers in libopenshot.
Definition: RendererBase.h:45
openshot::ReaderInfo::fps
Fraction fps
Frames per second, as a fraction (i.e. 24/1 = 24 fps)
Definition: ReaderBase.h:69
openshot::QtPlayer::~QtPlayer
virtual ~QtPlayer()
Default destructor.
Definition: QtPlayer.cpp:48
openshot::PLAYBACK_PAUSED
@ PLAYBACK_PAUSED
Pause the video (holding the last displayed frame)
Definition: PlayerBase.h:46
openshot::PlayerBase
This is the base class of all Players in libopenshot.
Definition: PlayerBase.h:58
openshot::AudioDeviceManagerSingleton::CloseAudioDevice
void CloseAudioDevice()
Close audio device.
Definition: AudioPlaybackThread.cpp:56
openshot::ReaderBase
This abstract class is the base class, used by all readers in libopenshot.
Definition: ReaderBase.h:96
openshot::PlayerBase::mode
PlaybackMode mode
Definition: PlayerBase.h:64
openshot::QtPlayer::GetRendererQObject
int64_t GetRendererQObject()
Get the Renderer pointer address (for Python to cast back into a QObject)
Definition: QtPlayer.cpp:175
openshot::QtPlayer::SetSource
void SetSource(const std::string &source)
Set the source URL/path of this player (which will create an internal Reader)
Definition: QtPlayer.cpp:62
openshot::ReaderInfo::channels
int channels
The number of audio channels used in the audio stream.
Definition: ReaderBase.h:82
openshot::RendererBase::OverrideWidget
virtual void OverrideWidget(int64_t qwidget_address)=0
Allow manual override of the QWidget that is used to display.