Loading...
Searching...
No Matches
QtPlayer.cpp
Go to the documentation of this file.
// Copyright (c) 2008-2019 OpenShot Studios, LLC
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#include "QtPlayer.h"
#include "AudioDevices.h"
#include "Clip.h"
#include "FFmpegReader.h"
#include "Timeline.h"
#include "Qt/PlayerPrivate.h"
#include "Qt/VideoRenderer.h"
namespace openshot
{
using AudioDeviceList = std::vector<std::pair<std::string, std::string>>;
// Delegating constructor
QtPlayer::QtPlayer()
: QtPlayer::QtPlayer(new VideoRenderer())
{ }
// Constructor
QtPlayer::QtPlayer(openshot::RendererBase *rb)
: PlayerBase()
, p(new openshot::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)
openshot::AudioDeviceManagerSingleton::Instance()->CloseAudioDevice();
}
// Return any error string during initialization
std::string QtPlayer::GetError() {
if (reader && threads_started) {
return p->audioPlayback->getError();
} else {
return "";
}
}
// Return the default audio sample rate (from the system)
double QtPlayer::GetDefaultSampleRate() {
if (reader && threads_started) {
return p->audioPlayback->getDefaultSampleRate();
} else {
return 0;
}
}
AudioDeviceList QtPlayer::GetAudioDeviceNames() {
AudioDevices devs;
return devs.getNames();
}
// Get current audio device or last attempted (if none succeeded)
AudioDeviceInfo QtPlayer::GetCurrentAudioDevice() {
return p->audioPlayback->getCurrentAudioDevice();
}
// Set the source JSON of an openshot::Timelime
void QtPlayer::SetTimelineSource(const std::string &json) {
// Create timeline instance (720p, since we have no re-scaling in this player yet)
reader = new Timeline(1280, 720, openshot::Fraction(30, 1), 44100, 2, openshot::LAYOUT_STEREO);
Timeline* tm = (Timeline*)reader;
tm->SetJson(json);
tm->DisplayInfo();
tm->Open();
// Set the reader
Reader(reader);
}
void QtPlayer::SetSource(const std::string &source)
{
FFmpegReader *ffreader = new FFmpegReader(source);
ffreader->DisplayInfo();
// Use default sample rate (or use the FFmpegReader's audio settings if any)
int sample_rate = 44100;
if (ffreader->info.sample_rate > 0)
sample_rate = ffreader->info.sample_rate;
// Use default channels (or use the FFmpegReader's audio settings if any)
int channels = 2;
if (ffreader->info.channels > 0)
channels = ffreader->info.channels;
// Use default channel layout (or use the FFmpegReader's audio settings if any)
openshot::ChannelLayout channel_layout = openshot::LAYOUT_STEREO;
if (channels != 2)
channel_layout = ffreader->info.channel_layout;
// Create timeline instance (720p, since we have no re-scaling in this player yet)
reader = new Timeline(1280, 720, ffreader->info.fps, sample_rate, channels, channel_layout);
Clip *c = new Clip(source);
Timeline* tm = (Timeline*)reader;
tm->AddClip(c);
tm->Open();
// 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;
}
openshot::PlaybackMode QtPlayer::Mode()
{
return mode;
}
void QtPlayer::Pause()
{
mode = PLAYBACK_PAUSED;
Speed(0);
}
int64_t 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, true);
// Notify audio thread that seek has occurred
p->audioPlayback->Seek(new_frame);
// Update current position
p->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(openshot::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
openshot::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 && 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;
}
}
AudioDevices.h
Header file for Audio Device Info struct.
Clip.h
Header file for Clip class.
FFmpegReader.h
Header file for FFmpegReader class.
PlayerPrivate.h
Source file for PlayerPrivate class.
QtPlayer.h
Header file for QtPlayer class.
Timeline.h
Header file for Timeline class.
VideoRenderer.h
Header file for Video Renderer class.
VideoRenderer
Definition VideoRenderer.h:25
openshot::AudioDeviceManagerSingleton::CloseAudioDevice
void CloseAudioDevice()
Close audio device.
Definition AudioPlaybackThread.cpp:178
openshot::AudioDeviceManagerSingleton::Instance
static AudioDeviceManagerSingleton * Instance()
Override with default sample rate & channels (44100, 2) and no preferred audio device.
Definition AudioPlaybackThread.cpp:39
openshot::AudioDevices
A class which probes the available audio devices.
Definition AudioDevices.h:46
openshot::AudioDevices::getNames
AudioDeviceList getNames()
Definition AudioDevices.cpp:21
openshot::Clip
This class represents a clip (used to arrange readers on the timeline).
Definition Clip.h:89
openshot::FFmpegReader
This class uses the FFmpeg libraries, to open video files and audio files, and return openshot::Frame...
Definition FFmpegReader.h:103
openshot::Fraction
This class represents a fraction.
Definition Fraction.h:30
openshot::PlayerBase
This is the base class of all Players in libopenshot.
Definition PlayerBase.h:42
openshot::PlayerBase::volume
float volume
Definition PlayerBase.h:45
openshot::PlayerBase::mode
PlaybackMode mode
Definition PlayerBase.h:47
openshot::PlayerBase::reader
openshot::ReaderBase * reader
Definition PlayerBase.h:46
openshot::PlayerBase::speed
float speed
Definition PlayerBase.h:44
openshot::PlayerPrivate
The private part of QtPlayer class, which contains an audio thread and video thread,...
Definition PlayerPrivate.h:31
openshot::QtPlayer::Loading
void Loading()
Display a loading animation.
Definition QtPlayer.cpp:144
openshot::QtPlayer::Seek
void Seek(int64_t new_frame)
Seek to a specific frame in the player.
Definition QtPlayer.cpp:166
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:99
openshot::QtPlayer::GetAudioDeviceNames
AudioDeviceList GetAudioDeviceNames()
Get Audio Devices from JUCE.
Definition QtPlayer.cpp:75
openshot::QtPlayer::GetCurrentAudioDevice
AudioDeviceInfo GetCurrentAudioDevice()
Get current audio device or last attempted.
Definition QtPlayer.cpp:81
openshot::QtPlayer::Position
int64_t Position()
Get the current frame number being played.
Definition QtPlayer.cpp:161
openshot::QtPlayer::QtPlayer
QtPlayer()
Default constructor.
Definition QtPlayer.cpp:29
openshot::QtPlayer::SetQWidget
void SetQWidget(int64_t qwidget_address)
Definition QtPlayer.cpp:215
openshot::QtPlayer::GetError
std::string GetError()
Get Error (if any).
Definition QtPlayer.cpp:57
openshot::QtPlayer::CloseAudioDevice
void CloseAudioDevice()
Close audio device.
Definition QtPlayer.cpp:50
openshot::QtPlayer::Volume
float Volume()
Get the Volume.
Definition QtPlayer.cpp:241
openshot::QtPlayer::~QtPlayer
virtual ~QtPlayer()
Default destructor.
Definition QtPlayer.cpp:42
openshot::QtPlayer::Speed
float Speed()
Get the Playback speed.
Definition QtPlayer.cpp:226
openshot::QtPlayer::Play
void Play()
Play the video.
Definition QtPlayer.cpp:131
openshot::QtPlayer::GetDefaultSampleRate
double GetDefaultSampleRate()
Return the default audio sample rate (from the system).
Definition QtPlayer.cpp:66
openshot::QtPlayer::Mode
openshot::PlaybackMode Mode()
Get the current mode.
Definition QtPlayer.cpp:150
openshot::QtPlayer::Pause
void Pause()
Pause the video.
Definition QtPlayer.cpp:155
openshot::QtPlayer::Reader
openshot::ReaderBase * Reader()
Get the current reader, such as a FFmpegReader.
Definition QtPlayer.cpp:210
openshot::QtPlayer::SetTimelineSource
void SetTimelineSource(const std::string &json)
Set the source JSON of an openshot::Timelime.
Definition QtPlayer.cpp:86
openshot::QtPlayer::GetRendererQObject
int64_t GetRendererQObject()
Get the Renderer pointer address (for Python to cast back into a QObject).
Definition QtPlayer.cpp:221
openshot::QtPlayer::Stop
void Stop()
Stop the video player and clear the cached frames.
Definition QtPlayer.cpp:181
openshot::ReaderBase
This abstract class is the base class, used by all readers in libopenshot.
Definition ReaderBase.h:76
openshot::ReaderBase::info
openshot::ReaderInfo info
Information about the current media file.
Definition ReaderBase.h:88
openshot::ReaderBase::DisplayInfo
void DisplayInfo(std::ostream *out=&std::cout)
Display file information in the standard output stream (stdout).
Definition ReaderBase.cpp:60
openshot::RendererBase
This is the base class of all Renderers in libopenshot.
Definition RendererBase.h:31
openshot::Timeline
This class represents a timeline.
Definition Timeline.h:154
openshot::Timeline::AddClip
void AddClip(openshot::Clip *clip)
Add an openshot::Clip to the timeline.
Definition Timeline.cpp:333
openshot::Timeline::Open
void Open() override
Open the reader (and start consuming resources).
Definition Timeline.cpp:925
openshot::Timeline::SetJson
void SetJson(const std::string value) override
Load JSON string into this object.
Definition Timeline.cpp:1228
openshot
This namespace is the default namespace for all code in the openshot library.
Definition Compressor.h:29
openshot::AudioDeviceList
std::vector< std::pair< std::string, std::string > > AudioDeviceList
Definition AudioDevices.h:42
openshot::ChannelLayout
ChannelLayout
This enumeration determines the audio channel layout (such as stereo, mono, 5 point surround,...
Definition ChannelLayouts.h:29
openshot::LAYOUT_STEREO
@ LAYOUT_STEREO
Definition ChannelLayouts.h:31
openshot::PlaybackMode
PlaybackMode
This enumeration determines the mode of the video player (i.e. playing, paused, etc....
Definition PlayerBase.h:27
openshot::PLAYBACK_LOADING
@ PLAYBACK_LOADING
Loading the video (display a loading animation).
Definition PlayerBase.h:30
openshot::PLAYBACK_PAUSED
@ PLAYBACK_PAUSED
Pause the video (holding the last displayed frame).
Definition PlayerBase.h:29
openshot::PLAYBACK_STOPPED
@ PLAYBACK_STOPPED
Stop playing the video (clear cache, done with player).
Definition PlayerBase.h:31
openshot::PLAYBACK_PLAY
@ PLAYBACK_PLAY
Play the video normally.
Definition PlayerBase.h:28
openshot::AudioDeviceInfo
This struct hold information about Audio Devices.
Definition AudioDevices.h:27
openshot::ReaderInfo::channels
int channels
The number of audio channels used in the audio stream.
Definition ReaderBase.h:61
openshot::ReaderInfo::fps
openshot::Fraction fps
Frames per second, as a fraction (i.e. 24/1 = 24 fps).
Definition ReaderBase.h:48
openshot::ReaderInfo::channel_layout
openshot::ChannelLayout channel_layout
The channel layout (mono, stereo, 5 point surround, etc...).
Definition ReaderBase.h:62
openshot::ReaderInfo::sample_rate
int sample_rate
The number of audio samples per second (44100 is a common sample rate).
Definition ReaderBase.h:60