1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "media/base/stream_parser_buffer.h"
6
7 #include <algorithm>
8
9 #include "base/check_op.h"
10 #include "base/memory/ptr_util.h"
11 #include "media/base/timestamp_constants.h"
12
13 namespace media {
14
CreateEOSBuffer()15 scoped_refptr<StreamParserBuffer> StreamParserBuffer::CreateEOSBuffer() {
16 return base::WrapRefCounted(new StreamParserBuffer(
17 NULL, 0, NULL, 0, false, DemuxerStream::UNKNOWN, 0));
18 }
19
CopyFrom(const uint8_t * data,int data_size,bool is_key_frame,Type type,TrackId track_id)20 scoped_refptr<StreamParserBuffer> StreamParserBuffer::CopyFrom(
21 const uint8_t* data,
22 int data_size,
23 bool is_key_frame,
24 Type type,
25 TrackId track_id) {
26 return base::WrapRefCounted(new StreamParserBuffer(
27 data, data_size, NULL, 0, is_key_frame, type, track_id));
28 }
29
CopyFrom(const uint8_t * data,int data_size,const uint8_t * side_data,int side_data_size,bool is_key_frame,Type type,TrackId track_id)30 scoped_refptr<StreamParserBuffer> StreamParserBuffer::CopyFrom(
31 const uint8_t* data,
32 int data_size,
33 const uint8_t* side_data,
34 int side_data_size,
35 bool is_key_frame,
36 Type type,
37 TrackId track_id) {
38 return base::WrapRefCounted(
39 new StreamParserBuffer(data, data_size, side_data, side_data_size,
40 is_key_frame, type, track_id));
41 }
42
GetDecodeTimestamp() const43 DecodeTimestamp StreamParserBuffer::GetDecodeTimestamp() const {
44 if (decode_timestamp_ == kNoDecodeTimestamp())
45 return DecodeTimestamp::FromPresentationTime(timestamp());
46 return decode_timestamp_;
47 }
48
SetDecodeTimestamp(DecodeTimestamp timestamp)49 void StreamParserBuffer::SetDecodeTimestamp(DecodeTimestamp timestamp) {
50 decode_timestamp_ = timestamp;
51 if (preroll_buffer_)
52 preroll_buffer_->SetDecodeTimestamp(timestamp);
53 }
54
StreamParserBuffer(const uint8_t * data,int data_size,const uint8_t * side_data,int side_data_size,bool is_key_frame,Type type,TrackId track_id)55 StreamParserBuffer::StreamParserBuffer(const uint8_t* data,
56 int data_size,
57 const uint8_t* side_data,
58 int side_data_size,
59 bool is_key_frame,
60 Type type,
61 TrackId track_id)
62 : DecoderBuffer(data, data_size, side_data, side_data_size),
63 decode_timestamp_(kNoDecodeTimestamp()),
64 config_id_(kInvalidConfigId),
65 type_(type),
66 track_id_(track_id),
67 is_duration_estimated_(false) {
68 // TODO(scherkus): Should DataBuffer constructor accept a timestamp and
69 // duration to force clients to set them? Today they end up being zero which
70 // is both a common and valid value and could lead to bugs.
71 if (data) {
72 set_duration(kNoTimestamp);
73 }
74
75 if (is_key_frame)
76 set_is_key_frame(true);
77 }
78
79 StreamParserBuffer::~StreamParserBuffer() = default;
80
GetConfigId() const81 int StreamParserBuffer::GetConfigId() const {
82 return config_id_;
83 }
84
SetConfigId(int config_id)85 void StreamParserBuffer::SetConfigId(int config_id) {
86 config_id_ = config_id;
87 if (preroll_buffer_)
88 preroll_buffer_->SetConfigId(config_id);
89 }
90
GetTypeName() const91 const char* StreamParserBuffer::GetTypeName() const {
92 return DemuxerStream::GetTypeName(type());
93 }
94
SetPrerollBuffer(scoped_refptr<StreamParserBuffer> preroll_buffer)95 void StreamParserBuffer::SetPrerollBuffer(
96 scoped_refptr<StreamParserBuffer> preroll_buffer) {
97 DCHECK(!preroll_buffer_);
98 DCHECK(!end_of_stream());
99 DCHECK(!preroll_buffer->end_of_stream());
100 DCHECK(!preroll_buffer->preroll_buffer_);
101 DCHECK(preroll_buffer->timestamp() <= timestamp());
102 DCHECK(preroll_buffer->discard_padding() == DecoderBuffer::DiscardPadding());
103 DCHECK_EQ(preroll_buffer->type(), type());
104 DCHECK_EQ(preroll_buffer->track_id(), track_id());
105
106 preroll_buffer_ = std::move(preroll_buffer);
107 preroll_buffer_->set_timestamp(timestamp());
108 preroll_buffer_->SetConfigId(GetConfigId());
109 preroll_buffer_->SetDecodeTimestamp(GetDecodeTimestamp());
110
111 // Mark the entire buffer for discard.
112 preroll_buffer_->set_discard_padding(
113 std::make_pair(kInfiniteDuration, base::TimeDelta()));
114 }
115
set_timestamp(base::TimeDelta timestamp)116 void StreamParserBuffer::set_timestamp(base::TimeDelta timestamp) {
117 DecoderBuffer::set_timestamp(timestamp);
118 if (preroll_buffer_)
119 preroll_buffer_->set_timestamp(timestamp);
120 }
121
122 } // namespace media
123