Files
video-v1/vav2/platforms/windows/applications/vav2player/Vav2Player/VideoPlayerControl.xaml.h

219 lines
8.2 KiB
C++

#pragma once
#include "VideoPlayerControl.g.h"
#include "VavCore/VavCore.h" // VavCore C API
// GPU rendering temporarily disabled for VavCore migration
#include "src/Rendering/IVideoRenderer.h"
#include "src/Rendering/SimpleGPURenderer.h"
using namespace Vav2Player;
#include <memory>
#include <string>
#include <atomic>
#include <queue>
#include <mutex>
#include <chrono>
namespace winrt::Vav2Player::implementation
{
struct VideoPlayerControl : VideoPlayerControlT<VideoPlayerControl>
{
VideoPlayerControl();
~VideoPlayerControl();
// Events
void UserControl_Loaded(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::RoutedEventArgs const& e);
void UserControl_Unloaded(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::RoutedEventArgs const& e);
void UserControl_SizeChanged(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::SizeChangedEventArgs const& e);
void HoverDetector_PointerEntered(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::Input::PointerRoutedEventArgs const& e);
void HoverDetector_PointerExited(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::Input::PointerRoutedEventArgs const& e);
// Public Properties
winrt::hstring VideoSource();
void VideoSource(winrt::hstring const& value);
bool ShowControls();
void ShowControls(bool value);
bool AutoPlay();
void AutoPlay(bool value);
Vav2Player::VideoDecoderType DecoderType();
void DecoderType(Vav2Player::VideoDecoderType value);
bool UseHardwareRendering();
void UseHardwareRendering(bool value);
VavCoreDecoderType GetInternalDecoderType();
void SetInternalDecoderType(VavCoreDecoderType value);
// Public Methods
void LoadVideo(winrt::hstring const& filePath);
void Play();
void Pause();
void Stop();
void Seek(double timeSeconds);
void RefreshDecoderSettings();
// Status Properties
bool IsVideoPlaying();
bool IsVideoLoaded();
double CurrentTime();
double Duration();
winrt::hstring Status();
private:
// VavCore components (replaces direct decoder usage)
VavCorePlayer* m_vavCorePlayer;
// Video rendering components (optimized)
winrt::Microsoft::UI::Xaml::Media::Imaging::WriteableBitmap m_renderBitmap{ nullptr };
std::vector<uint8_t> m_bgraBuffer; // Reusable buffer for BGRA conversion
uint32_t m_lastFrameWidth = 0;
uint32_t m_lastFrameHeight = 0;
// GPU rendering re-enabled for VavCore
std::unique_ptr<IVideoRenderer> m_gpuRenderer;
bool m_useHardwareRendering = true; // GPU rendering enabled
// Playback timer for continuous frame processing
winrt::Microsoft::UI::Xaml::DispatcherTimer m_playbackTimer;
// High-resolution timer for accurate frame timing
std::unique_ptr<std::thread> m_timingThread;
std::atomic<bool> m_shouldStopTiming{false};
// Video dimensions
uint32_t m_videoWidth = 0;
uint32_t m_videoHeight = 0;
// AspectFit state
bool m_hasValidVideoSize = false;
// Configuration
winrt::hstring m_videoSource;
bool m_showControls = true;
bool m_autoPlay = false;
VavCoreDecoderType m_decoderType = VAVCORE_DECODER_AUTO;
// Playback state
std::atomic<bool> m_isPlaying{ false };
std::atomic<bool> m_isLoaded{ false };
std::atomic<bool> m_isInitialized{ false };
std::atomic<bool> m_frameProcessing{ false }; // Prevents dispatcher queue overflow
uint64_t m_currentFrame = 0;
uint64_t m_totalFrames = 0;
double m_frameRate = 30.0;
double m_currentTime = 0.0;
double m_duration = 0.0;
winrt::hstring m_status = L"Ready";
// Basic timing and error tracking
std::chrono::high_resolution_clock::time_point m_playbackStartTime;
uint64_t m_framesDecodeErrors = 0;
// D3D Surface support
bool m_useD3DSurfaces = false;
VavCoreSurfaceType m_supportedSurfaceType = VAVCORE_SURFACE_CPU;
void* m_d3dDevice = nullptr; // ID3D11Device* or ID3D12Device*
// Phase 2 Optimization: Memory Pool System
class MemoryPool
{
private:
std::queue<winrt::Microsoft::UI::Xaml::Media::Imaging::WriteableBitmap> _bitmapPool;
std::queue<std::vector<uint8_t>> _bufferPool;
std::mutex _poolMutex;
const size_t MAX_POOL_SIZE = 10;
// Statistics tracking
int _bitmapPoolHits = 0;
int _bitmapPoolMisses = 0;
int _bufferPoolHits = 0;
int _bufferPoolMisses = 0;
public:
winrt::Microsoft::UI::Xaml::Media::Imaging::WriteableBitmap GetBitmap(uint32_t width, uint32_t height);
void ReturnBitmap(winrt::Microsoft::UI::Xaml::Media::Imaging::WriteableBitmap bitmap);
std::vector<uint8_t> GetBuffer(size_t size);
void ReturnBuffer(std::vector<uint8_t> buffer);
void PrintStats();
};
// Phase 2 Optimization: Advanced Performance Monitor
class AdvancedPerformanceMonitor
{
private:
std::queue<double> _decodingTimes;
std::queue<double> _renderingTimes;
std::queue<double> _totalFrameTimes;
std::chrono::high_resolution_clock::time_point _frameStartTime;
std::chrono::high_resolution_clock::time_point _decodeStartTime;
std::chrono::high_resolution_clock::time_point _renderStartTime;
// Adaptive quality control
int _consecutiveSlowFrames = 0;
int _consecutiveFastFrames = 0;
bool _qualityReductionActive = false;
const int SLOW_FRAME_THRESHOLD = 3;
const int FAST_FRAME_THRESHOLD = 8;
const size_t SAMPLE_SIZE = 30; // 30 frame rolling average
public:
void RecordFrameStart();
void RecordDecodeStart();
void RecordDecodeEnd();
void RecordRenderStart();
void RecordRenderEnd();
void RecordFrameEnd();
struct PerformanceStats {
double AverageDecodeTime;
double AverageRenderTime;
double AverageTotalTime;
double CurrentFPS;
bool QualityReductionActive;
};
PerformanceStats GetStats();
bool ShouldReduceQuality();
bool ShouldRestoreQuality();
void CheckForQualityAdjustment(double frameTime);
};
// Phase 2 Optimization instances
std::unique_ptr<MemoryPool> m_memoryPool;
std::unique_ptr<AdvancedPerformanceMonitor> m_performanceMonitor;
// Helper methods
void InitializeVideoRenderer();
// GPU rendering methods re-enabled for VavCore
bool TryInitializeGPURenderer();
void SetRenderingMode(bool useGPU);
void ProcessSingleFrame();
void RenderFrameToScreen(const VavCoreVideoFrame& frame);
void RenderFrameSoftware(const VavCoreVideoFrame& frame);
void ProcessSingleFrameLegacy();
void ConvertYUVToBGRA(const VavCoreVideoFrame& yuv_frame, uint8_t* bgra_buffer, uint32_t width, uint32_t height);
void UpdateVideoImageAspectFit(int videoWidth, int videoHeight);
void ApplyAspectFitIfReady();
void UpdateStatus(winrt::hstring const& message);
void ResetVideoState();
void LoadDecoderSettings();
// D3D Surface methods
bool InitializeD3DSurfaceSupport();
bool CreateD3D11Device();
void ReleaseD3D11Device();
void ProcessSingleFrameWithSurfaces();
bool CreateD3DTexture(uint32_t width, uint32_t height, void** texture);
void RenderD3DSurfaceToScreen(void* d3dTexture, const VavCoreVideoFrame& frame);
};
}
namespace winrt::Vav2Player::factory_implementation
{
struct VideoPlayerControl : VideoPlayerControlT<VideoPlayerControl, implementation::VideoPlayerControl>
{
};
}