File: player.h

package info (click to toggle)
freespace2 24.2.0%2Brepack-1
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 43,716 kB
  • sloc: cpp: 595,001; ansic: 21,741; python: 1,174; sh: 457; makefile: 248; xml: 181
file content (137 lines) | stat: -rw-r--r-- 3,512 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#pragma once

#include <memory>

#include <thread>

#include "globalincs/pstypes.h"
#include "sound/openal.h"

#include "Decoder.h"
#include "VideoPresenter.h"

namespace cutscene {
class Decoder;

struct PlayerState {
	MovieProperties props;
	Decoder* decoder = nullptr;

	bool playbackHasBegun = false;

	// Timing state
	std::uint64_t playback_time = 0;

	// Audio state
	bool audioInited = false;
	bool hasAudio = false;
	ALuint audioSid = 0;
	SCP_vector<ALuint> audioBuffers;
	SCP_queue<ALuint> unqueuedAudioBuffers;

	// Graphics state following
	bool videoInited = false;

	VideoFramePtr currentFrame;
	VideoFramePtr nextFrame;
	bool newFrameAdded = false;

	std::unique_ptr<player::VideoPresenter> videoPresenter;

	SCP_queue<SubtitleFramePtr> queued_subtitles;
	SubtitleFramePtr currentSubtitle;

	PlayerState() {
	}

	PlayerState(const PlayerState&) = delete;
	PlayerState& operator=(const PlayerState&) = delete;
};

/**
 * @brief A movie player
 */
class Player {
  private:
	std::unique_ptr<Decoder> m_decoder;

	std::unique_ptr<std::thread> m_decoderThread;

	PlayerState m_state;

	bool processDecoderData();

  private:
	void decoderThread();

	Player(std::unique_ptr<Decoder>&& decoder, const PlaybackProperties& properties);
  public:
	~Player();

	Player(const Player&) = delete;
	Player& operator=(const Player&) = delete;

	/**
	 * @brief Update the player and increase display timestamp by specified delta time
	 * @param diff_time_micro The time to increase the display timestamp in microseconds (not milliseconds!)
	 * @return @c true if there is more content to display. @c false if the player reached the end of the movie and has
	 * 			displayed the last frame.
	 */
	bool update(uint64_t diff_time_micro);

	/**
	 * @brief Gives access to the internal state of the player
	 *
	 * @warning This should only be used for debugging purposed!
	 *
	 * @return A reference to the internal state
	 */
	const PlayerState& getInternalState() const;

	/**
	 * @brief Gets the properties of the movie that this player is displaying
	 * @return A reference to the movie properties
	 */
	const MovieProperties& getMovieProperties() const;

	/**
	 * @brief Determines if this player can start displaying video.
	 * @return true if the cutscene is ready, false otherwise
	 */
	bool isPlaybackReady() const;

	/**
	 * @brief Draws the current movie frame at the specified coordinates.
	 *
	 * This may not draw anything if there is currently not a frame to display.
	 *
	 * @param x1 The X coordinate of the top left corner
	 * @param y1 The Y coordinate of the top left corner
	 * @param x2 The X coordinate of the bottom right corner
	 * @param y2 The Y coordinate of the bottom right corner
	 */
	void draw(float x1, float y1, float x2, float y2, float alpha = 1.0f);

	/**
	 * @brief Gets the subtitle text that should be displayed now
	 * @return The text, encoded in UTF-8
	 */
	SCP_string getCurrentSubtitle();

	/**
	 * @brief Stops playback
	 *
	 * This is also automatically called when this instance is destroyed.
	 */
	void stopPlayback();

	/**
	 * @brief Creates a player
	 * The player is configured to play the movie with the specified name
	 * @param name The movie to play
	 * @param properties A structure describing how the movie will be used
	 * @return @c nullptr if the specified movie could not be opened, a valid Player pointer otherwise
	 */
	static std::unique_ptr<Player> newPlayer(const SCP_string& name, const PlaybackProperties& properties = PlaybackProperties());
};
}