blob: 6b0013f5b93b1dcad4eb313bd648b857cf867860 [file] [log] [blame]
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <map>
#include <queue>
#include <vector>
#include "base/basictypes.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gl/gl_bindings.h"
#include "ui/gl/gl_context.h"
#include "ui/gl/gl_surface.h"
namespace base {
class MessageLoop;
class WaitableEvent;
namespace content {
class VideoFrameTexture : public base::RefCounted<VideoFrameTexture> {
uint32 texture_id() const { return texture_id_; }
uint32 texture_target() const { return texture_target_; }
VideoFrameTexture(uint32 texture_target,
uint32 texture_id,
const base::Closure& no_longer_needed_cb);
friend class base::RefCounted<VideoFrameTexture>;
uint32 texture_target_;
uint32 texture_id_;
base::Closure no_longer_needed_cb_;
struct RenderingHelperParams {
// The rendering FPS.
int rendering_fps;
// The desired size of each window. We play each stream in its own window
// on the screen.
std::vector<gfx::Size> window_sizes;
// The members below are only used for the thumbnail mode where all frames
// are rendered in sequence onto one FBO for comparison/verification purposes.
// Whether the frames are rendered as scaled thumbnails within a
// larger FBO that is in turn rendered to the window.
bool render_as_thumbnails;
// The size of the FBO containing all visible thumbnails.
gfx::Size thumbnails_page_size;
// The size of each thumbnail within the FBO.
gfx::Size thumbnail_size;
// Creates and draws textures used by the video decoder.
// This class is not thread safe and thus all the methods of this class
// (except for ctor/dtor) ensure they're being run on a single thread.
class RenderingHelper {
static bool InitializeOneOff();
// Create the render context and windows by the specified dimensions.
void Initialize(const RenderingHelperParams& params,
base::WaitableEvent* done);
// Undo the effects of Initialize() and signal |*done|.
void UnInitialize(base::WaitableEvent* done);
// Return a newly-created GLES2 texture id of the specified size, and
// signal |*done|.
void CreateTexture(uint32 texture_target,
uint32* texture_id,
const gfx::Size& size,
base::WaitableEvent* done);
// Render thumbnail in the |texture_id| to the FBO buffer using target
// |texture_target|.
void RenderThumbnail(uint32 texture_target, uint32 texture_id);
// Queues the |video_frame| for rendering.
void QueueVideoFrame(size_t window_id,
scoped_refptr<VideoFrameTexture> video_frame);
// Drops all the pending video frames of the specified window.
void DropPendingFrames(size_t window_id);
// Delete |texture_id|.
void DeleteTexture(uint32 texture_id);
// Get the platform specific handle to the OpenGL display.
void* GetGLDisplay();
// Get the platform specific handle to the OpenGL context.
void* GetGLContext();
// Get rendered thumbnails as RGB.
// Sets alpha_solid to true if the alpha channel is entirely 0xff.
void GetThumbnailsAsRGB(std::vector<unsigned char>* rgb,
bool* alpha_solid,
base::WaitableEvent* done);
struct RenderedVideo {
// The rect on the screen where the video will be rendered.
gfx::Rect render_area;
// True if the last (and the only one) frame in pending_frames has
// been rendered. We keep the last remaining frame in pending_frames even
// after it has been rendered, so that we have something to display if the
// client is falling behind on providing us with new frames during
// timer-driven playback.
bool last_frame_rendered;
// The video frames pending for rendering.
std::queue<scoped_refptr<VideoFrameTexture> > pending_frames;
void Clear();
void RenderContent();
void LayoutRenderingAreas(const std::vector<gfx::Size>& window_sizes);
// Render |texture_id| to the current view port of the screen using target
// |texture_target|.
void RenderTexture(uint32 texture_target, uint32 texture_id);
// Timer to trigger the RenderContent() repeatly.
scoped_ptr<base::RepeatingTimer<RenderingHelper> > render_timer_;
base::MessageLoop* message_loop_;
scoped_refptr<gfx::GLContext> gl_context_;
scoped_refptr<gfx::GLSurface> gl_surface_;
gfx::AcceleratedWidget window_;
gfx::Size screen_size_;
std::vector<RenderedVideo> videos_;
bool render_as_thumbnails_;
int frame_count_;
GLuint thumbnails_fbo_id_;
GLuint thumbnails_texture_id_;
gfx::Size thumbnails_fbo_size_;
gfx::Size thumbnail_size_;
GLuint program_;
base::TimeDelta frame_duration_;
} // namespace content