blob: 7f0ddb833bdb1757b9ce7c5604c627442e089c33 [file] [log] [blame]
// Copyright (c) 2006-2008 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.
// Defines various types of timestamped media buffers used for transporting
// data between filters. Every buffer contains a timestamp in microseconds
// describing the relative position of the buffer within the media stream, and
// the duration in microseconds for the length of time the buffer will be
// rendered.
// Timestamps are derived directly from the encoded media file and are commonly
// known as the presentation timestamp (PTS). Durations are a best-guess and
// are usually derived from the sample/frame rate of the media file.
// Due to encoding and transmission errors, it is not guaranteed that timestamps
// arrive in a monotonically increasing order nor that the next timestamp will
// be equal to the previous timestamp plus the duration.
// In the ideal scenario for a 25fps movie, buffers are timestamped as followed:
// Buffer0 Buffer1 Buffer2 ... BufferN
// Timestamp: 0us 40000us 80000us ... (N*40000)us
// Duration*: 40000us 40000us 40000us ... 40000us
// *25fps = 0.04s per frame = 40000us per frame
#include "base/ref_counted.h"
namespace media {
class StreamSample : public base::RefCountedThreadSafe<StreamSample> {
// Returns the timestamp of this buffer in microseconds.
virtual int64 GetTimestamp() const = 0;
// Returns the duration of this buffer in microseconds.
virtual int64 GetDuration() const = 0;
// Sets the timestamp of this buffer in microseconds.
virtual void SetTimestamp(int64 timestamp) = 0;
// Sets the duration of this buffer in microseconds.
virtual void SetDuration(int64 duration) = 0;
friend class base::RefCountedThreadSafe<StreamSample>;
virtual ~StreamSample() {}
class Buffer : public StreamSample {
// Returns a read only pointer to the buffer data.
virtual const char* GetData() const = 0;
// Returns the size of valid data in bytes.
virtual size_t GetDataSize() const = 0;
class WritableBuffer : public Buffer {
// Returns a read-write pointer to the buffer data.
virtual char* GetWritableData() = 0;
// Updates the size of valid data in bytes, which must be less than or equal
// to GetBufferSize.
virtual void SetDataSize(size_t data_size) = 0;
// Returns the maximum allocated size for this buffer.
virtual size_t GetBufferSize() const = 0;
struct VideoSurface {
static const size_t kMaxPlanes = 3;
// Surface formats roughly based on FOURCC labels, see:
enum Format {
RGB555, // 16bpp RGB packed 5:5:5
RGB565, // 16bpp RGB packed 5:6:5
RGB24, // 24bpp RGB packed 8:8:8
RGB32, // 32bpp RGB packed with extra byte 8:8:8
RGBA, // 32bpp RGBA packed 8:8:8:8
YV12, // 12bpp YVU planar 1x1 Y, 2x2 VU samples
YV16, // 16bpp YVU planar 1x1 Y, 2x1 VU samples
// Surface format.
Format format;
// Width and height of surface.
size_t width;
size_t height;
// Number of planes, typically 1 for packed RGB formats and 3 for planar
// YUV formats.
size_t planes;
// Array of strides for each plane, typically greater or equal to the width
// of the surface divided by the horizontal sampling period.
size_t strides[kMaxPlanes];
// Array of data pointers to each plane.
char* data[kMaxPlanes];
class VideoFrame : public StreamSample {
// Locks the underlying surface and fills out the given VideoSurface and
// returns true if successful, false otherwise. Any additional calls to Lock
// will fail.
virtual bool Lock(VideoSurface* surface) = 0;
// Unlocks the underlying surface, the VideoSurface acquired from Lock is no
// longer guaranteed to be valid.
virtual void Unlock() = 0;
template <class BufferType>
class Assignable {
// Assigns a buffer to the owner.
virtual void SetBuffer(BufferType* buffer) = 0;
// Notifies the owner that an assignment has been completed.
virtual void OnAssignment() = 0;
// Template for easily creating Assignable buffers. Pass in the pointer of the
// object to receive the OnAssignment callback.
template <class TOwner, class TBuffer>
class AssignableBuffer : public Assignable<TBuffer>,
public base::RefCountedThreadSafe< AssignableBuffer<TOwner, TBuffer> > {
explicit AssignableBuffer(TOwner* owner)
: owner_(owner),
buffer_(NULL) {
// AssignableBuffer<TBuffer> implementation.
virtual void SetBuffer(TBuffer* buffer) {
buffer_ = buffer;
virtual void OnAssignment() {
TOwner* owner_;
scoped_refptr<TBuffer> buffer_;
} // namespace media