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.
5 #include "media/base/stream_parser_buffer.h"
7 #include "base/logging.h"
8 #include "media/base/buffers.h"
12 static scoped_refptr
<StreamParserBuffer
> CopyBuffer(
13 const StreamParserBuffer
& buffer
) {
14 if (buffer
.end_of_stream())
15 return StreamParserBuffer::CreateEOSBuffer();
17 scoped_refptr
<StreamParserBuffer
> copied_buffer
=
18 StreamParserBuffer::CopyFrom(buffer
.data(),
21 buffer
.side_data_size(),
25 copied_buffer
->SetDecodeTimestamp(buffer
.GetDecodeTimestamp());
26 copied_buffer
->SetConfigId(buffer
.GetConfigId());
27 copied_buffer
->set_timestamp(buffer
.timestamp());
28 copied_buffer
->set_duration(buffer
.duration());
29 copied_buffer
->set_discard_padding(buffer
.discard_padding());
30 const DecryptConfig
* decrypt_config
= buffer
.decrypt_config();
32 copied_buffer
->set_decrypt_config(
33 make_scoped_ptr(new DecryptConfig(decrypt_config
->key_id(),
35 decrypt_config
->subsamples())));
41 scoped_refptr
<StreamParserBuffer
> StreamParserBuffer::CreateEOSBuffer() {
42 return make_scoped_refptr(new StreamParserBuffer(NULL
, 0, NULL
, 0, false,
43 DemuxerStream::UNKNOWN
, 0));
46 scoped_refptr
<StreamParserBuffer
> StreamParserBuffer::CopyFrom(
47 const uint8
* data
, int data_size
, bool is_keyframe
, Type type
,
49 return make_scoped_refptr(
50 new StreamParserBuffer(data
, data_size
, NULL
, 0, is_keyframe
, type
,
54 scoped_refptr
<StreamParserBuffer
> StreamParserBuffer::CopyFrom(
55 const uint8
* data
, int data_size
,
56 const uint8
* side_data
, int side_data_size
,
57 bool is_keyframe
, Type type
, TrackId track_id
) {
58 return make_scoped_refptr(
59 new StreamParserBuffer(data
, data_size
, side_data
, side_data_size
,
60 is_keyframe
, type
, track_id
));
63 base::TimeDelta
StreamParserBuffer::GetDecodeTimestamp() const {
64 if (decode_timestamp_
== kNoTimestamp())
66 return decode_timestamp_
;
69 void StreamParserBuffer::SetDecodeTimestamp(const base::TimeDelta
& timestamp
) {
70 decode_timestamp_
= timestamp
;
73 StreamParserBuffer::StreamParserBuffer(const uint8
* data
, int data_size
,
74 const uint8
* side_data
,
75 int side_data_size
, bool is_keyframe
,
76 Type type
, TrackId track_id
)
77 : DecoderBuffer(data
, data_size
, side_data
, side_data_size
),
78 is_keyframe_(is_keyframe
),
79 decode_timestamp_(kNoTimestamp()),
80 config_id_(kInvalidConfigId
),
83 // TODO(scherkus): Should DataBuffer constructor accept a timestamp and
84 // duration to force clients to set them? Today they end up being zero which
85 // is both a common and valid value and could lead to bugs.
87 set_duration(kNoTimestamp());
91 StreamParserBuffer::~StreamParserBuffer() {}
93 int StreamParserBuffer::GetConfigId() const {
97 void StreamParserBuffer::SetConfigId(int config_id
) {
98 config_id_
= config_id
;
101 void StreamParserBuffer::ConvertToSpliceBuffer(
102 const BufferQueue
& pre_splice_buffers
) {
103 DCHECK(splice_buffers_
.empty());
104 DCHECK(!end_of_stream());
106 // Make a copy of this first, before making any changes.
107 scoped_refptr
<StreamParserBuffer
> overlapping_buffer
= CopyBuffer(*this);
109 const scoped_refptr
<StreamParserBuffer
>& first_splice_buffer
=
110 pre_splice_buffers
.front();
112 // Ensure the given buffers are actually before the splice point.
113 DCHECK(first_splice_buffer
->timestamp() <= overlapping_buffer
->timestamp());
115 // TODO(dalecurtis): We should also clear |data| and |side_data|, but since
116 // that implies EOS care must be taken to ensure there are no clients relying
119 // Rewrite |this| buffer as a splice buffer.
120 SetDecodeTimestamp(first_splice_buffer
->GetDecodeTimestamp());
121 SetConfigId(first_splice_buffer
->GetConfigId());
122 set_timestamp(first_splice_buffer
->timestamp());
123 is_keyframe_
= first_splice_buffer
->IsKeyframe();
124 type_
= first_splice_buffer
->type();
125 track_id_
= first_splice_buffer
->track_id();
126 set_splice_timestamp(overlapping_buffer
->timestamp());
128 // The splice duration is the duration of all buffers before the splice plus
129 // the highest ending timestamp after the splice point.
131 std::max(overlapping_buffer
->timestamp() + overlapping_buffer
->duration(),
132 pre_splice_buffers
.back()->timestamp() +
133 pre_splice_buffers
.back()->duration()) -
134 first_splice_buffer
->timestamp());
136 // Copy all pre splice buffers into our wrapper buffer.
137 for (BufferQueue::const_iterator it
= pre_splice_buffers
.begin();
138 it
!= pre_splice_buffers
.end();
140 const scoped_refptr
<StreamParserBuffer
>& buffer
= *it
;
141 DCHECK(!buffer
->end_of_stream());
142 DCHECK(buffer
->get_splice_buffers().empty());
143 buffer
->set_splice_timestamp(splice_timestamp());
144 splice_buffers_
.push_back(CopyBuffer(*buffer
));
147 splice_buffers_
.push_back(overlapping_buffer
);