2014-02-14 23:21:05 +00:00
|
|
|
// Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
2013-09-24 04:17:12 +00:00
|
|
|
|
|
|
|
#ifndef MEDIA_BASE_MEDIA_SAMPLE_H_
|
|
|
|
#define MEDIA_BASE_MEDIA_SAMPLE_H_
|
|
|
|
|
|
|
|
#include <deque>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/base/logging.h"
|
|
|
|
#include "packager/base/memory/ref_counted.h"
|
|
|
|
#include "packager/base/memory/scoped_ptr.h"
|
2013-09-24 04:17:12 +00:00
|
|
|
|
2014-09-19 20:41:13 +00:00
|
|
|
namespace edash_packager {
|
2013-09-24 04:17:12 +00:00
|
|
|
namespace media {
|
|
|
|
|
2014-01-24 18:46:46 +00:00
|
|
|
/// Class to hold a media sample.
|
2013-11-12 20:32:44 +00:00
|
|
|
class MediaSample : public base::RefCountedThreadSafe<MediaSample> {
|
2013-09-24 04:17:12 +00:00
|
|
|
public:
|
2014-01-24 18:46:46 +00:00
|
|
|
/// Create a MediaSample object from input.
|
|
|
|
/// @param data points to the buffer containing the sample data.
|
|
|
|
/// Must not be NULL.
|
|
|
|
/// @param size indicates sample size in bytes. Must not be negative.
|
|
|
|
/// @param is_key_frame indicates whether the sample is a key frame.
|
2014-09-30 21:52:21 +00:00
|
|
|
static scoped_refptr<MediaSample> CopyFrom(const uint8_t* data,
|
2014-03-31 22:25:45 +00:00
|
|
|
size_t size,
|
2013-09-24 04:17:12 +00:00
|
|
|
bool is_key_frame);
|
|
|
|
|
2014-01-24 18:46:46 +00:00
|
|
|
/// Create a MediaSample object from input.
|
|
|
|
/// @param data points to the buffer containing the sample data.
|
|
|
|
/// Must not be NULL.
|
|
|
|
/// @param side_data points to the buffer containing the additional data.
|
|
|
|
/// Some containers allow additional data to be specified.
|
|
|
|
/// Must not be NULL.
|
|
|
|
/// @param size indicates sample size in bytes. Must not be negative.
|
|
|
|
/// @param side_data_size indicates additional sample data size in bytes.
|
|
|
|
/// Must not be negative.
|
|
|
|
/// @param is_key_frame indicates whether the sample is a key frame.
|
2014-09-30 21:52:21 +00:00
|
|
|
static scoped_refptr<MediaSample> CopyFrom(const uint8_t* data,
|
2014-03-31 22:25:45 +00:00
|
|
|
size_t size,
|
2014-09-30 21:52:21 +00:00
|
|
|
const uint8_t* side_data,
|
2014-03-31 22:25:45 +00:00
|
|
|
size_t side_data_size,
|
2013-09-24 04:17:12 +00:00
|
|
|
bool is_key_frame);
|
|
|
|
|
2014-07-14 21:35:57 +00:00
|
|
|
/// Create a MediaSample object with default members.
|
|
|
|
static scoped_refptr<MediaSample> CreateEmptyMediaSample();
|
|
|
|
|
2014-01-24 18:46:46 +00:00
|
|
|
/// Create a MediaSample indicating we've reached end of stream.
|
|
|
|
/// Calling any method other than end_of_stream() on the resulting buffer
|
|
|
|
/// is disallowed.
|
2013-09-24 04:17:12 +00:00
|
|
|
static scoped_refptr<MediaSample> CreateEOSBuffer();
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t dts() const {
|
2013-09-24 04:17:12 +00:00
|
|
|
DCHECK(!end_of_stream());
|
|
|
|
return dts_;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
void set_dts(int64_t dts) { dts_ = dts; }
|
2013-09-24 04:17:12 +00:00
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t pts() const {
|
2013-09-24 04:17:12 +00:00
|
|
|
DCHECK(!end_of_stream());
|
|
|
|
return pts_;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
void set_pts(int64_t pts) { pts_ = pts; }
|
2013-09-24 04:17:12 +00:00
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t duration() const {
|
2013-09-24 04:17:12 +00:00
|
|
|
DCHECK(!end_of_stream());
|
|
|
|
return duration_;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
void set_duration(int64_t duration) {
|
2013-09-24 04:17:12 +00:00
|
|
|
DCHECK(!end_of_stream());
|
|
|
|
duration_ = duration;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_key_frame() const {
|
|
|
|
DCHECK(!end_of_stream());
|
|
|
|
return is_key_frame_;
|
|
|
|
}
|
|
|
|
|
2015-01-12 21:17:10 +00:00
|
|
|
bool is_encrypted() const {
|
|
|
|
DCHECK(!end_of_stream());
|
|
|
|
return is_encrypted_;
|
|
|
|
}
|
2014-09-30 21:52:21 +00:00
|
|
|
const uint8_t* data() const {
|
2013-09-24 04:17:12 +00:00
|
|
|
DCHECK(!end_of_stream());
|
2013-11-12 20:32:44 +00:00
|
|
|
return &data_[0];
|
2013-09-24 04:17:12 +00:00
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint8_t* writable_data() {
|
2013-09-24 04:17:12 +00:00
|
|
|
DCHECK(!end_of_stream());
|
2013-11-12 20:32:44 +00:00
|
|
|
return &data_[0];
|
2013-09-24 04:17:12 +00:00
|
|
|
}
|
|
|
|
|
2014-03-31 22:25:45 +00:00
|
|
|
size_t data_size() const {
|
2013-09-24 04:17:12 +00:00
|
|
|
DCHECK(!end_of_stream());
|
|
|
|
return data_.size();
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
const uint8_t* side_data() const {
|
2013-09-24 04:17:12 +00:00
|
|
|
DCHECK(!end_of_stream());
|
2013-11-12 20:32:44 +00:00
|
|
|
return &side_data_[0];
|
2013-09-24 04:17:12 +00:00
|
|
|
}
|
|
|
|
|
2014-03-31 22:25:45 +00:00
|
|
|
size_t side_data_size() const {
|
2013-09-24 04:17:12 +00:00
|
|
|
DCHECK(!end_of_stream());
|
|
|
|
return side_data_.size();
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
void set_data(const uint8_t* data, const size_t data_size) {
|
2014-09-16 19:55:49 +00:00
|
|
|
data_.assign(data, data + data_size);
|
2014-07-14 21:35:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void set_is_key_frame(bool value) {
|
|
|
|
is_key_frame_ = value;
|
|
|
|
}
|
|
|
|
|
2015-01-12 21:17:10 +00:00
|
|
|
void set_is_encrypted(bool value) {
|
|
|
|
is_encrypted_ = value;
|
|
|
|
}
|
|
|
|
|
2013-09-24 04:17:12 +00:00
|
|
|
// If there's no data in this buffer, it represents end of stream.
|
2013-11-12 20:32:44 +00:00
|
|
|
bool end_of_stream() const { return data_.size() == 0; }
|
2013-09-24 04:17:12 +00:00
|
|
|
|
2014-01-24 18:46:46 +00:00
|
|
|
/// @return a human-readable string describing |*this|.
|
2013-10-11 21:44:55 +00:00
|
|
|
std::string ToString() const;
|
2013-09-24 04:17:12 +00:00
|
|
|
|
2014-04-24 18:37:33 +00:00
|
|
|
private:
|
2013-09-24 04:17:12 +00:00
|
|
|
friend class base::RefCountedThreadSafe<MediaSample>;
|
|
|
|
|
2014-04-24 18:37:33 +00:00
|
|
|
// Create a MediaSample. Buffer will be padded and aligned as necessary.
|
|
|
|
// |data|,|side_data| can be NULL, which indicates an empty sample.
|
|
|
|
// |size|,|side_data_size| should not be negative.
|
2014-09-30 21:52:21 +00:00
|
|
|
MediaSample(const uint8_t* data,
|
2014-03-31 22:25:45 +00:00
|
|
|
size_t size,
|
2014-09-30 21:52:21 +00:00
|
|
|
const uint8_t* side_data,
|
2014-03-31 22:25:45 +00:00
|
|
|
size_t side_data_size,
|
2013-09-24 04:17:12 +00:00
|
|
|
bool is_key_frame);
|
2014-09-16 19:55:49 +00:00
|
|
|
MediaSample();
|
2013-09-24 04:17:12 +00:00
|
|
|
virtual ~MediaSample();
|
|
|
|
|
|
|
|
// Decoding time stamp.
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t dts_;
|
2013-09-24 04:17:12 +00:00
|
|
|
// Presentation time stamp.
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t pts_;
|
|
|
|
int64_t duration_;
|
2013-09-24 04:17:12 +00:00
|
|
|
bool is_key_frame_;
|
2015-01-12 21:17:10 +00:00
|
|
|
// is sample encrypted ?
|
|
|
|
bool is_encrypted_;
|
2013-09-24 04:17:12 +00:00
|
|
|
|
|
|
|
// Main buffer data.
|
2014-09-30 21:52:21 +00:00
|
|
|
std::vector<uint8_t> data_;
|
2013-09-24 04:17:12 +00:00
|
|
|
// Contain additional buffers to complete the main one. Needed by WebM
|
|
|
|
// http://www.matroska.org/technical/specs/index.html BlockAdditional[A5].
|
|
|
|
// Not used by mp4 and other containers.
|
2014-09-30 21:52:21 +00:00
|
|
|
std::vector<uint8_t> side_data_;
|
2013-09-24 04:17:12 +00:00
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(MediaSample);
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::deque<scoped_refptr<MediaSample> > BufferQueue;
|
|
|
|
|
|
|
|
} // namespace media
|
2014-09-19 20:41:13 +00:00
|
|
|
} // namespace edash_packager
|
2013-09-24 04:17:12 +00:00
|
|
|
|
|
|
|
#endif // MEDIA_BASE_MEDIA_SAMPLE_H_
|