summaryrefslogtreecommitdiff
path: root/video/quicktime_material.h
blob: a80b6443eaf9af946e400b49e29f8e96e03a5a92 (plain) (blame)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
//=============================================================================


#ifndef QUICKTIME_MATERIAL_H
#define QUICKTIME_MATERIAL_H

#ifdef _WIN32
#pragma once
#endif


//-----------------------------------------------------------------------------
// Forward declarations
//-----------------------------------------------------------------------------
class IFileSystem;
class IMaterialSystem;
class CQuickTimeMaterial;

//-----------------------------------------------------------------------------
// Global interfaces - you already did the needed includes, right?
//-----------------------------------------------------------------------------
extern IFileSystem		*g_pFileSystem;
extern IMaterialSystem	*materials;

//-----------------------------------------------------------------------------
// Quicktime includes
//-----------------------------------------------------------------------------
#if defined ( OSX )
	#include <quicktime/QTML.h>
	#include <quicktime/Movies.h>
	#include <quicktime/MediaHandlers.h>
#elif defined ( WIN32 )
	#include <QTML.h>
	#include <Movies.h>
	#include <windows.h>
	#include <MediaHandlers.h>
#elif
    #error "Quicktime not supported on this target platform"	
#endif


#include "video/ivideoservices.h"

#include "video_macros.h"
#include "quicktime_common.h"

#include "materialsystem/itexture.h"
#include "materialsystem/imaterialsystem.h"
#include "materialsystem/MaterialSystemUtil.h"


// -----------------------------------------------------------------------------
// Texture regenerator - callback to get new movie pixels into the texture
// -----------------------------------------------------------------------------
class CQuicktimeMaterialRGBTextureRegenerator : public ITextureRegenerator
{
	public:
		CQuicktimeMaterialRGBTextureRegenerator();
		~CQuicktimeMaterialRGBTextureRegenerator();
	
		void				SetSourceGWorld( GWorldPtr theGWorld, int nWidth, int nHeight );

		// Inherited from ITextureRegenerator
		virtual void		RegenerateTextureBits( ITexture *pTexture, IVTFTexture *pVTFTexture, Rect_t *pRect );
		virtual void		Release();

	private:
		GWorldPtr			m_SrcGWorld;
		int					m_nSourceWidth;
		int					m_nSourceHeight;
};



// -----------------------------------------------------------------------------
// Class used to play a QuickTime video onto a texture 
// -----------------------------------------------------------------------------
class CQuickTimeMaterial : public IVideoMaterial
{
	public:
		CQuickTimeMaterial();
		~CQuickTimeMaterial();
		
		static const int			MAX_QT_FILENAME_LEN = 255;
		static const int			MAX_MATERIAL_NAME_LEN = 255;	
		static const int			TEXTURE_SIZE_ALIGNMENT = 8;

		// Initializes, shuts down the material
		bool						Init( const char *pMaterialName, const char *pFileName, VideoPlaybackFlags_t flags );
		void						Shutdown();

		// Video information functions		
		virtual const char		   *GetVideoFileName();									// Gets the file name of the video this material is playing
		virtual VideoResult_t		GetLastResult();									// Gets detailed info on the last operation
		
		virtual VideoFrameRate_t   &GetVideoFrameRate();								// Returns the frame rate of the associated video in FPS

		// Audio Functions
		virtual bool				HasAudio();											// Query if the video has an audio track
		
		virtual bool				SetVolume( float fVolume );							// Adjust the playback volume
		virtual float				GetVolume();										// Query the current volume
		virtual void				SetMuted( bool bMuteState );						// Mute/UnMutes the audio playback
		virtual bool				IsMuted();											// Query muted status
		
		virtual VideoResult_t		SoundDeviceCommand( VideoSoundDeviceOperation_t operation, void *pDevice = nullptr, void *pData = nullptr );		// Assign Sound Device for this Video Material
		
		// Video playback state functions
		virtual bool				IsVideoReadyToPlay();								// Queries if the video material was initialized successfully and is ready for playback, but not playing or finished
		virtual bool				IsVideoPlaying();									// Is the video currently playing (and needs update calls, etc)
		virtual bool				IsNewFrameReady();									// Do we have a new frame to get & display?
		virtual bool				IsFinishedPlaying();								// Have we reached the end of the movie

		virtual bool				StartVideo();										// Starts the video playing
		virtual bool				StopVideo();										// Terminates the video playing

		virtual void				SetLooping( bool bLoopVideo );						// Sets the video to loop (or not)
		virtual bool				IsLooping();										// Queries if the video is looping
		
		virtual void				SetPaused( bool bPauseState );						// Pauses or Unpauses video playback
		virtual bool				IsPaused();											// Queries if the video is paused

		// Position in playback functions
		virtual float				GetVideoDuration();									// Returns the duration of the associated video in seconds
		virtual int					GetFrameCount();									// Returns the total number of (unique) frames in the video
		
		virtual bool				SetFrame( int FrameNum );							// Sets the current frame # in the video to play next 
		virtual int					GetCurrentFrame();									// Gets the current frame # for the video playback, 0 Based
		
		virtual bool				SetTime( float flTime );							// Sets the video playback to specified time (in seconds)
		virtual float				GetCurrentVideoTime();								// Gets the current time in the video playback
		
		// Update function
		virtual bool				Update();											// Updates the video frame to reflect the time passed, true = new frame available

		// Material / Texture Info functions
		virtual IMaterial		   *GetMaterial();										// Gets the IMaterial associated with an video material

		virtual void				GetVideoTexCoordRange( float *pMaxU, float *pMaxV ) ;		// Returns the max texture coordinate of the video portion of the material surface ( 0.0, 0.0 to U, V )
		virtual void				GetVideoImageSize( int *pWidth, int *pHeight );				// Returns the frame size of the Video Image Frame in pixels ( the stored in a subrect of the material itself)
		


	private:
		friend class CQuicktimeMaterialRGBTextureRegenerator;

		void 						Reset();											// clears internal state
		void 						SetQTFileName( const char *theQTMovieFileName );
		VideoResult_t				SetResult( VideoResult_t status );
		
		// Initializes, shuts down the video stream
		void 						OpenQTMovie( const char *theQTMovieFileName );
		void 						CloseQTFile();

		// Initializes, shuts down the procedural texture
		void						CreateProceduralTexture( const char *pTextureName );
		void						DestroyProceduralTexture();

		// Initializes, shuts down the procedural material
		void 						CreateProceduralMaterial( const char *pMaterialName );
		void 						DestroyProceduralMaterial();

		CQuicktimeMaterialRGBTextureRegenerator	m_TextureRegen;

		VideoResult_t				m_LastResult;
		
		CMaterialReference			m_Material;						// Ref to Material used for rendering the video frame
		CTextureReference			m_Texture;						// Ref to the renderable texture which contains the most recent video frame (in a sub-rect)

		float						m_TexCordU;						// Max U texture coordinate of the texture sub-rect which holds the video frame
		float						m_TexCordV;						// Max V texture coordinate of the texture sub-rect which holds the video frame

		int							m_VideoFrameWidth;				// Size of the movie frame in pixels
		int							m_VideoFrameHeight;

		char					   *m_pFileName;					// resolved filename of the movie being played
		VideoPlaybackFlags_t		m_PlaybackFlags;				// option flags user supplied

		bool						m_bInitCalled;
		bool						m_bMovieInitialized;
		bool						m_bMoviePlaying;
		bool						m_bMovieFinishedPlaying;
		bool						m_bMoviePaused;
		bool						m_bLoopMovie;
		
		bool						m_bHasAudio;
		bool						m_bMuted;
		
		float						m_CurrentVolume;
		
		// QuickTime Stuff
		Movie						m_QTMovie;
		
		TimeScale					m_QTMovieTimeScale;				// Units per second
		TimeValue					m_QTMovieDuration;				// movie duration in TimeScale Units Per Second
		float						m_QTMovieDurationinSec;			// movie duration in seconds
		VideoFrameRate_t			m_QTMovieFrameRate;				// Frame Rate of movie
		int							m_QTMovieFrameCount;
		
		Rect						m_QTMovieRect;
		GWorldPtr					m_MovieGWorld;

		QTAudioContextRef			m_AudioContext;

		TimeValue					m_MovieFirstFrameTime;
		TimeValue					m_NextInterestingTimeToPlay;
		TimeValue					m_MoviePauseTime; 

};







#endif // QUICKTIME_MATERIAL_H