1 /**
2  * Copyright (c) 2006-2016 LOVE Development Team
3  *
4  * This software is provided 'as-is', without any express or implied
5  * warranty.  In no event will the authors be held liable for any damages
6  * arising from the use of this software.
7  *
8  * Permission is granted to anyone to use this software for any purpose,
9  * including commercial applications, and to alter it and redistribute it
10  * freely, subject to the following restrictions:
11  *
12  * 1. The origin of this software must not be misrepresented; you must not
13  *    claim that you wrote the original software. If you use this software
14  *    in a product, an acknowledgment in the product documentation would be
15  *    appreciated but is not required.
16  * 2. Altered source versions must be plainly marked as such, and must not be
17  *    misrepresented as being the original software.
18  * 3. This notice may not be removed or altered from any source distribution.
19  **/
20 
21 #include "VideoStream.h"
22 
23 using love::thread::Lock;
24 
25 namespace love
26 {
27 namespace video
28 {
29 
setSync(VideoStream::FrameSync * frameSync)30 void VideoStream::setSync(VideoStream::FrameSync *frameSync)
31 {
32 	this->frameSync = frameSync;
33 }
34 
getSync() const35 VideoStream::FrameSync *VideoStream::getSync() const
36 {
37 	return frameSync;
38 }
39 
play()40 void VideoStream::play()
41 {
42 	frameSync->play();
43 }
44 
pause()45 void VideoStream::pause()
46 {
47 	frameSync->pause();
48 }
49 
seek(double offset)50 void VideoStream::seek(double offset)
51 {
52 	frameSync->seek(offset);
53 }
54 
tell() const55 double VideoStream::tell() const
56 {
57 	return frameSync->tell();
58 }
59 
isPlaying() const60 bool VideoStream::isPlaying() const
61 {
62 	return frameSync->isPlaying();
63 }
64 
Frame()65 VideoStream::Frame::Frame()
66 	: yplane(nullptr)
67 	, cbplane(nullptr)
68 	, crplane(nullptr)
69 {
70 }
71 
~Frame()72 VideoStream::Frame::~Frame()
73 {
74 	delete[] yplane;
75 	delete[] cbplane;
76 	delete[] crplane;
77 }
78 
copyState(const VideoStream::FrameSync * other)79 void VideoStream::FrameSync::copyState(const VideoStream::FrameSync *other)
80 {
81 	seek(other->tell());
82 	if (other->isPlaying())
83 		play();
84 	else
85 		pause();
86 }
87 
tell() const88 double VideoStream::FrameSync::tell() const
89 {
90 	return getPosition();
91 }
92 
DeltaSync()93 VideoStream::DeltaSync::DeltaSync()
94 	: playing(false)
95 	, position(0)
96 	, speed(1)
97 {
98 }
99 
~DeltaSync()100 VideoStream::DeltaSync::~DeltaSync()
101 {
102 }
103 
getPosition() const104 double VideoStream::DeltaSync::getPosition() const
105 {
106 	return position;
107 }
108 
update(double dt)109 void VideoStream::DeltaSync::update(double dt)
110 {
111 	Lock l(mutex);
112 	if (playing)
113 		position += dt*speed;
114 }
115 
play()116 void VideoStream::DeltaSync::play()
117 {
118 	playing = true;
119 }
120 
pause()121 void VideoStream::DeltaSync::pause()
122 {
123 	playing = false;
124 }
125 
seek(double time)126 void VideoStream::DeltaSync::seek(double time)
127 {
128 	Lock l(mutex);
129 	position = time;
130 }
131 
isPlaying() const132 bool VideoStream::DeltaSync::isPlaying() const
133 {
134 	return playing;
135 }
136 
SourceSync(love::audio::Source * source)137 VideoStream::SourceSync::SourceSync(love::audio::Source *source)
138 	: source(source)
139 {
140 }
141 
getPosition() const142 double VideoStream::SourceSync::getPosition() const
143 {
144 	return source->tell(love::audio::Source::UNIT_SECONDS);
145 }
146 
play()147 void VideoStream::SourceSync::play()
148 {
149 	source->play();
150 }
151 
pause()152 void VideoStream::SourceSync::pause()
153 {
154 	source->pause();
155 }
156 
seek(double time)157 void VideoStream::SourceSync::seek(double time)
158 {
159 	source->seek(time, love::audio::Source::UNIT_SECONDS);
160 }
161 
isPlaying() const162 bool VideoStream::SourceSync::isPlaying() const
163 {
164 	return !source->isStopped() && !source->isPaused();
165 }
166 
167 } // video
168 } // love
169