summaryrefslogtreecommitdiffstats
path: root/chrome
diff options
context:
space:
mode:
authorralphl@chromium.org <ralphl@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-02-11 18:07:01 +0000
committerralphl@chromium.org <ralphl@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-02-11 18:07:01 +0000
commit4d430ef1335bdfbfe04be7ff7a048c2aff06c000 (patch)
tree1abd9c9064e8ff823f546365bfe9e0ac9dd50bf6 /chrome
parent9bae4b9a123cda92bc3bd0da16784d9ec75d1494 (diff)
downloadchromium_src-4d430ef1335bdfbfe04be7ff7a048c2aff06c000.zip
chromium_src-4d430ef1335bdfbfe04be7ff7a048c2aff06c000.tar.gz
chromium_src-4d430ef1335bdfbfe04be7ff7a048c2aff06c000.tar.bz2
Almost complete implementation of the Chrome video renderer. Still needs to implement color space conversion for final bitblt.
Review URL: http://codereview.chromium.org/21037 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@9575 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r--chrome/renderer/media/video_renderer_impl.cc298
-rw-r--r--chrome/renderer/media/video_renderer_impl.h150
2 files changed, 421 insertions, 27 deletions
diff --git a/chrome/renderer/media/video_renderer_impl.cc b/chrome/renderer/media/video_renderer_impl.cc
index 8159f00..d400e58 100644
--- a/chrome/renderer/media/video_renderer_impl.cc
+++ b/chrome/renderer/media/video_renderer_impl.cc
@@ -3,36 +3,308 @@
// LICENSE file.
#include "chrome/renderer/media/video_renderer_impl.h"
+#include "media/base/buffers.h"
+#include "media/base/filter_host.h"
+#include "media/base/pipeline.h"
+
+using media::MediaFormat;
+using media::VideoFrame;
+
+
+// The amount of time allowed to pre-submit a frame. If UpdateQueue is called
+// with a time within this limit, it will skip to the next frame in the queue
+// even though the value for time it is called with is not yet at the frame's
+// timestamp. Time is specified in microseconds.
+static const int64 kFrameSkipAheadLimit = 5000;
+
+// If there are no frames in the queue, then this value is used for the
+// amount of time to sleep until the next time update callback. Time is
+// specified in microseconds.
+static const int64 kSleepIfNoFrame = 15000;
+
+// Number of reads to have pending.
+// TODO(ralph): Re-examine the pull model -- perhaps I was wrong. I think
+// that perhaps the demuxer is the point where pull becomes push.
+static const size_t kDefaultNumberOfFrames = 4;
+
+// Value used for the current_frame_timestamp_ to indicate that the
+// |current_frame_| member should be treated as invalid.
+static const int64 kNoCurrentFrame = -1;
+
+//------------------------------------------------------------------------------
VideoRendererImpl::VideoRendererImpl(WebMediaPlayerDelegateImpl* delegate)
- : delegate_(delegate) {
+ : delegate_(delegate),
+ submit_reads_task_(NULL),
+ number_of_reads_needed_(kDefaultNumberOfFrames),
+ current_frame_timestamp_(
+ base::TimeDelta::FromMicroseconds(kNoCurrentFrame)),
+ preroll_complete_(false) {
}
VideoRendererImpl::~VideoRendererImpl() {
+ Stop();
+}
+
+// static
+bool VideoRendererImpl::IsMediaFormatSupported(
+ const media::MediaFormat* media_format) {
+ int width;
+ int height;
+ return ParseMediaFormat(media_format, &width, &height);
+}
+
+// static
+bool VideoRendererImpl::ParseMediaFormat(const media::MediaFormat* media_format,
+ int* width_out,
+ int* height_out) {
+ DCHECK(media_format && width_out && height_out);
+ std::string mime_type;
+ return (media_format->GetAsString(MediaFormat::kMimeType, &mime_type) &&
+ mime_type.compare(media::mime_type::kUncompressedVideo) == 0 &&
+ media_format->GetAsInteger(MediaFormat::kWidth, width_out) &&
+ media_format->GetAsInteger(MediaFormat::kHeight, height_out));
}
void VideoRendererImpl::Stop() {
- // TODO(scherkus): implement Stop.
- NOTIMPLEMENTED();
+ AutoLock auto_lock(lock_);
+ DiscardAllFrames();
+ if (submit_reads_task_) {
+ // The task is owned by the message loop, so we don't delete it here. We
+ // know the task won't call us because we canceled it, and we know we are
+ // on the pipeline thread, since we're in the filer's Stop method, so there
+ // is no threading problem. Just let the task be run by the message loop
+ // and then be killed
+ submit_reads_task_->Cancel();
+ submit_reads_task_ = NULL;
+ }
+ delegate_ = NULL; // This indicates we're no longer running
+ decoder_ = NULL; // Release reference to the decoder
}
bool VideoRendererImpl::Initialize(media::VideoDecoder* decoder) {
- // TODO(scherkus): implement Initialize.
- NOTIMPLEMENTED();
- return false;
+ int width;
+ int height;
+ if (!ParseMediaFormat(decoder_->GetMediaFormat(), &width, &height)) {
+ return false;
+ }
+ current_frame_.setConfig(SkBitmap::kARGB_8888_Config, width, height);
+ if (!current_frame_.allocPixels(NULL, NULL)) {
+ NOTREACHED();
+ return false;
+ }
+ rect_.SetRect(0, 0, width, height);
+ delegate_->SetVideoRenderer(this);
+ host_->SetVideoSize(width, height);
+ host_->SetTimeUpdateCallback(
+ NewCallback(this, &VideoRendererImpl::TimeUpdateCallback));
+ SubmitReads();
+ return true;
}
-bool VideoRendererImpl::IsMediaFormatSupported(
- const media::MediaFormat* format) {
- // TODO(hclam): check the format correctly.
- return true;
+void VideoRendererImpl::SubmitReads() {
+ int number_to_read;
+ {
+ AutoLock auto_lock(lock_);
+ submit_reads_task_ = NULL;
+ number_to_read = number_of_reads_needed_;
+ number_of_reads_needed_ = 0;
+ }
+ while (number_to_read > 0) {
+ decoder_->Read(new media::AssignableBuffer<VideoRendererImpl,
+ media::VideoFrame>(this));
+ --number_to_read;
+ }
}
+void VideoRendererImpl::SetRect(const gfx::Rect& rect) {
+ rect_ = rect;
+ // TODO(ralphl) What are all these rects???
+}
+
+// This method is always called on the renderer's thread, so it will not be
+// reentered. However, it does maniuplate the queue and the current frame
+// timestamp, so those manipulations need to happen with the lock held.
void VideoRendererImpl::Paint(skia::PlatformCanvas *canvas,
const gfx::Rect& rect) {
- // TODO(hclam): add stuff here.
+ VideoFrame* video_frame;
+ base::TimeDelta time_of_next_frame;
+ bool need_to_convert_frame = false;
+ {
+ AutoLock auto_lock(lock_);
+ UpdateQueue(host_->GetPipelineStatus()->GetTime(), NULL, &video_frame,
+ &time_of_next_frame);
+ if (video_frame) {
+ // if the |current_frame_| bitmap already has the RGB image of the
+ // front video_frame then there's on no need to call CopyToCurentFrame
+ // to convert the video_frame to RBG. If we do need to convert a new
+ // frame, then remember the time of the frame so we might be able to skip
+ // this step if asked to repaint multiple times. Note that the
+ // |current_frame_timestamp_| member needs to only be accessed with the
+ // |lock_| acquired, so we set the timestamp here even though the
+ // conversion won't take place until we call CopyToCurrentFrame. It's
+ // not a problem because this method is the only place where the current
+ // frame is updated, and it is always called on the renderer's thread.
+ const base::TimeDelta frame_timestamp = video_frame->GetTimestamp();
+ need_to_convert_frame = (current_frame_timestamp_ != frame_timestamp);
+ if (need_to_convert_frame) {
+ current_frame_timestamp_ = frame_timestamp;
+ }
+ }
+ }
+
+ // We no longer hold the |lock_|. Don't access members other than |host_| and
+ // |current_frame_|.
+ if (video_frame) {
+ if (need_to_convert_frame) {
+ CopyToCurrentFrame(video_frame);
+ }
+ video_frame->Release();
+ SkMatrix matrix;
+ matrix.setTranslate(static_cast<SkScalar>(rect.x()),
+ static_cast<SkScalar>(rect.y()));
+ // TODO(ralphl): I have no idea what's going on here. How are these
+ // rects related to eachother? What does SetRect() mean?
+ matrix.preScale(static_cast<SkScalar>(rect.width() / rect_.width()),
+ static_cast<SkScalar>(rect.height() / rect_.height()));
+ canvas->drawBitmapMatrix(current_frame_, matrix, NULL);
+ }
+ host_->ScheduleTimeUpdateCallback(time_of_next_frame);
}
-void VideoRendererImpl::SetRect(const gfx::Rect& rect) {
- // TODO(hclam): add stuff here.
+void VideoRendererImpl::CopyToCurrentFrame(VideoFrame* video_frame) {
+ media::VideoSurface frame_in;
+ if (video_frame->Lock(&frame_in)) {
+ // TODO(ralphl): Actually do the color space conversion here!
+ // This is temporary code to set the bits of the current_frame_ to
+ // blue.
+ current_frame_.eraseRGB(0x00, 0x00, 0xFF);
+ video_frame->Unlock();
+ } else {
+ NOTREACHED();
+ }
+}
+
+// Assumes |lock_| has been acquired!
+bool VideoRendererImpl::UpdateQueue(base::TimeDelta time,
+ VideoFrame* new_frame,
+ VideoFrame** front_frame_out,
+ base::TimeDelta* time_of_next_frame) {
+ bool updated_front = false;
+
+ // If a new frame is passed in then put it at the back of the queue. If the
+ // queue was empty, then we've updated the front too.
+ if (new_frame) {
+ updated_front = queue_.empty();
+ new_frame->AddRef();
+ queue_.push_back(new_frame);
+ }
+
+ // Now make sure that the front of the queue is the correct frame to display
+ // right now. Discard any frames that are past the current time. If any
+ // frames are discarded then increment the |number_of_reads_needed_| member.
+ while (queue_.size() > 1 &&
+ queue_.front()->GetTimestamp() +
+ base::TimeDelta::FromMicroseconds(kFrameSkipAheadLimit) >= time) {
+ queue_.front()->Release();
+ queue_.pop_front();
+ updated_front = true;
+ ++number_of_reads_needed_;
+ }
+
+ // If the caller wants the front frame then return it, with the reference
+ // count incremented. The caller must call Release() on the returned frame.
+ if (front_frame_out) {
+ if (queue_.empty()) {
+ *front_frame_out = NULL;
+ } else {
+ *front_frame_out = queue_.front();
+ (*front_frame_out)->AddRef();
+ }
+ }
+
+ // If the caller wants the time of the next frame, return our best guess:
+ // If no frame, then wait for a while
+ // If only one frame, then use the duration of the front frame
+ // If there is more than one frame, return the time of the next frame.
+ if (time_of_next_frame) {
+ if (queue_.empty()) {
+ *time_of_next_frame = host_->GetPipelineStatus()->GetInterpolatedTime() +
+ base::TimeDelta::FromMicroseconds(kSleepIfNoFrame);
+ } else {
+ if (queue_.size() == 1) {
+ *time_of_next_frame = queue_.front()->GetTimestamp() +
+ queue_.front()->GetDuration();
+ } else {
+ *time_of_next_frame = queue_[1]->GetTimestamp();
+ }
+ }
+ }
+
+ // If any frames have been removed we need to call the decoder again. Note
+ // that the PostSubmitReadsTask method will only post the task if there are
+ // pending reads.
+ PostSubmitReadsTask();
+
+ // True if the front of the queue is a new frame.
+ return updated_front;
}
+
+// Assumes |lock_| has been acquired!
+void VideoRendererImpl::DiscardAllFrames() {
+ while (!queue_.empty()) {
+ queue_.front()->Release();
+ queue_.pop_front();
+ ++number_of_reads_needed_;
+ }
+ current_frame_timestamp_ = base::TimeDelta::FromMicroseconds(kNoCurrentFrame);
+}
+
+// Assumes |lock_| has been acquired!
+void VideoRendererImpl::PostSubmitReadsTask() {
+ if (number_of_reads_needed_ > 0 && !submit_reads_task_) {
+ submit_reads_task_ = NewRunnableMethod(this,
+ &VideoRendererImpl::SubmitReads);
+ host_->PostTask(submit_reads_task_);
+ }
+}
+
+void VideoRendererImpl::TimeUpdateCallback(base::TimeDelta time) {
+ AutoLock auto_lock(lock_);
+ if (IsRunning() && UpdateQueue(time, NULL, NULL, NULL)) {
+ delegate_->PostRepaintTask();
+ }
+}
+
+void VideoRendererImpl::OnAssignment(VideoFrame* video_frame) {
+ bool call_initialized = false;
+ {
+ AutoLock auto_lock(lock_);
+ if (IsRunning()) {
+ // TODO(ralphl): if (!preroll_complete_ && EndOfStream) call_init = true
+ // and preroll_complete_ = true.
+ // TODO(ralphl): If(Seek()) then discard but we don't have SeekFrame().
+ if (false) {
+ // TODO(ralphl): this is the seek() logic.
+ DiscardAllFrames();
+ ++number_of_reads_needed_;
+ PostSubmitReadsTask();
+ } else {
+ if (UpdateQueue(host_->GetPipelineStatus()->GetInterpolatedTime(),
+ video_frame, NULL, NULL)) {
+ delegate_->PostRepaintTask();
+ }
+ if (!preroll_complete_ && queue_.size() == kDefaultNumberOfFrames) {
+ preroll_complete_ = true;
+ call_initialized = true;
+ }
+ }
+ }
+ }
+ // |lock_| no longer held. Call the pipeline if we've just entered a
+ // completed preroll state.
+ if (call_initialized) {
+ host_->InitializationComplete();
+ }
+}
+
diff --git a/chrome/renderer/media/video_renderer_impl.h b/chrome/renderer/media/video_renderer_impl.h
index 38a7124..134cc9a 100644
--- a/chrome/renderer/media/video_renderer_impl.h
+++ b/chrome/renderer/media/video_renderer_impl.h
@@ -11,31 +11,37 @@
// Paint()
// SetRect()
-#ifndef CHROME_RENDERER_MEDIA_VIDEO_RENDERER_H_
-#define CHROME_RENDERER_MEDIA_VIDEO_RENDERER_H_
+#ifndef CHROME_RENDERER_MEDIA_VIDEO_RENDERER_IMPL_H_
+#define CHROME_RENDERER_MEDIA_VIDEO_RENDERER_IMPL_H_
+#include <deque>
+
+#include "base/lock.h"
+#include "base/task.h"
#include "base/gfx/platform_canvas.h"
+#include "chrome/renderer/webmediaplayer_delegate_impl.h"
#include "base/gfx/rect.h"
#include "media/base/factory.h"
#include "media/base/filters.h"
+#include "webkit/glue/webmediaplayer_delegate.h"
-class WebMediaPlayerDelegateImpl;
+class SubmitReadsTask;
class VideoRendererImpl : public media::VideoRenderer {
public:
- VideoRendererImpl(WebMediaPlayerDelegateImpl* delegate);
-
// media::MediaFilter implementation.
virtual void Stop();
// media::VideoRenderer implementation.
virtual bool Initialize(media::VideoDecoder* decoder);
- // Called from WebMediaPlayerDelegateImpl from WebKit's main thread.
- void Paint(skia::PlatformCanvas* canvas, const gfx::Rect& rect);
+ // Methods for painting called by the WebMediaPlayerDelegateImpl
+ // TODO(ralphl): What the *$*%##@ is this? What does it mean? How do we
+ // treat the "rect"? Is is clipping?
+ virtual void SetRect(const gfx::Rect& rect);
- // Called from WebMediaPlayerDelegateImpl from WebKit's main thread.
- void SetRect(const gfx::Rect& rect);
+ // TODO(ralphl): What is this rect? Is it relative to the canvas?
+ virtual void Paint(skia::PlatformCanvas* canvas, const gfx::Rect& rect);
// Static method for creating factory for this object.
static media::FilterFactory* CreateFactory(
@@ -44,17 +50,133 @@ class VideoRendererImpl : public media::VideoRenderer {
VideoRendererImpl, WebMediaPlayerDelegateImpl*>(delegate);
}
+ // Implementation of AssignableBuffer<this>::OnAssignment method.
+ void OnAssignment(media::VideoFrame* video_frame);
+
+ private:
+ friend class SubmitReadsTask;
+ friend class media::FilterFactoryImpl1<VideoRendererImpl,
+ WebMediaPlayerDelegateImpl*>;
+
+ // Constructor and destructor are private. Only the filter factory is
+ // allowed to create instances.
+ explicit VideoRendererImpl(WebMediaPlayerDelegateImpl* delegate);
+ virtual ~VideoRendererImpl();
+
// Answers question from the factory to see if we accept |format|.
static bool IsMediaFormatSupported(const media::MediaFormat* format);
- protected:
- virtual ~VideoRendererImpl();
+ // Used by the IsMediaFormatSupported and Initialize methods. Examines the
+ // |media_format| and returns true if the format is supported. Both output
+ // parameters, |width_out| and |height_out| are required and must not be NULL.
+ static bool ParseMediaFormat(const media::MediaFormat* media_format,
+ int* width_out,
+ int* height_out);
- private:
+ // Used internally to post a task that will call the SubmitReads() method.
+ // The |lock_| must be acquired before calling this method. If the value of
+ // |number_of_reads_needed_| is 0 or if there is already a pending task then
+ // this method simply returns and does not post a new task.
+ void PostSubmitReadsTask();
+
+ // Examines the |number_of_reads_needed_| member and calls the decoder to
+ // read the necessary number of frames.
+ void SubmitReads();
+
+ // For simplicity, we use the |delegate_| member to indicate if we have been
+ // stopped or not.
+ bool IsRunning() const { return (delegate_ != NULL); }
+
+ // Throw away all frames in the queue. The |lock_| must have been acquired
+ // before calling this method.
+ void DiscardAllFrames();
+
+ // This method is always called with the object's |lock_| acquired..
+ // The bool return value indicates weather or not the front of the queue has
+ // been updated. If this method returns true, then the front of the queue
+ // is a new video frame, otherwise, the front is the same as the last call.
+ // Given the current |time|, this method updates the state of the video frame
+ // queue. The caller may pass in a |new_frame| which will be added to the
+ // queue in the appropriate position based on the frame's timestamp. The
+ // |front_frame_out| and |time_next_frame_out| parameters are both optional
+ // and can be NULL. If |front_frame_out| is non-NULL, then it is returned as
+ // either NULL, which indicates that there are no frames in the queue, or
+ // it will be a pointer to the frame at the front of the queue. NOTE: THIS
+ // FRAME'S REFERENCE COUNT HAS BEEN INCREMENTED BY THIS CALL. THE
+ // CALLING FUNCTION MUST CALL |front_frame_out|->Release();
+ // If the |time_next_frame_out| parameter is non-NULL then it will be assigned
+ // the stream time of the next frame in the queue. This is used by the Paint
+ // method to schedule a time update callback at the appropriate presentation
+ // time for the next frame.
+ bool UpdateQueue(base::TimeDelta time,
+ media::VideoFrame* new_frame,
+ media::VideoFrame** front_frame_out,
+ base::TimeDelta* time_next_frame_out);
+
+ // Internal method used by the Paint method to convert the specified video
+ // frame to RGB, placing the converted pixels in the |current_frame_| bitmap.
+ void CopyToCurrentFrame(media::VideoFrame* video_frame);
+
+ // Called when the clock is updated by the audio renderer of when a scheduled
+ // callback is called based on the interpolated current position of the media
+ // stream.
+ void TimeUpdateCallback(base::TimeDelta time);
+
+ // Critical section. There is only one for this object. Used to serialize
+ // access to the following members:
+ // |queue_| for obvious reasons
+ // |delegate_| because it is used by methods that can be called from random
+ // threads to determine if the render has been stopped (it will
+ // be NULL if stopped)
+ // |submit_reads_task_| to prevent multiple scheduling of the task and to
+ // allow for safe cancelation of the task.
+ // |current_frame_timestamp_| because member is used by the render
+ // thread in the Paint method and by DiscardAllFrames which can be
+ // called on the decoder's thread when a seek occurs.
+ // |number_of_reads_needed_| is modified by UpdateQueue from the decoder
+ // thread, the renderer thread, and the pipeline thread.
+ // |preroll_complete_| has a very small potential race condition if the
+ // OnAssignment method were reentered for the last frame in the queue
+ // and an end-of-stream frame.
+ Lock lock_;
+
+ // Pointer to our parent object that is called to request repaints.
WebMediaPlayerDelegateImpl* delegate_;
+ // Pointer to the decoder that will feed us with video frames.
+ scoped_refptr<media::VideoDecoder> decoder_;
+
+ // If non-NULL then a task has been scheduled to submit read requests to the
+ // video decoder.
+ CancelableTask* submit_reads_task_;
+
+ // The number of buffers we need to request. This member is updated by any
+ // method that removes frames from the queue, such as UpdateQueue and
+ // DiscardAllFrames.
+ int number_of_reads_needed_;
+
+ // An RGB bitmap of the current frame. Note that we use the timestamp to
+ // determine if the frame contents need to be color space converted, or if the
+ // |current_frame_| member contains the correct image for the queue front.
+ SkBitmap current_frame_;
+ base::TimeDelta current_frame_timestamp_;
+
+ // TODO(ralphl): Try to understand all of the various "rect" and dimension
+ // aspects of the renderer and document it so that other people can understand
+ // it too. I can not accurately describe what the role of this member is now.
+ gfx::Rect rect_;
+
+ // The queue of video frames. The front of the queue is the frame that should
+ // be displayed.
+ typedef std::deque<media::VideoFrame*> VideoFrameQueue;
+ VideoFrameQueue queue_;
+
+ // True if we have received a full queue of video frames from the decoder.
+ // We don't call FilterHost::InitializationComplete() until the the queue
+ // is full.
+ bool preroll_complete_;
+
DISALLOW_COPY_AND_ASSIGN(VideoRendererImpl);
};
-#endif // CHROME_RENDERER_MEDIA_VIDEO_RENDERER_H_
-
+#endif // CHROME_RENDERER_MEDIA_VIDEO_RENDERER_IMPL_H_