OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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/webm/webm_cluster_parser.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "media/base/data_buffer.h" |
| 9 #include "media/webm/webm_constants.h" |
| 10 |
| 11 namespace media { |
| 12 |
| 13 static Buffer* CreateBuffer(const uint8* data, size_t size) { |
| 14 scoped_array<uint8> buf(new uint8[size]); |
| 15 memcpy(buf.get(), data, size); |
| 16 return new DataBuffer(buf.release(), size); |
| 17 } |
| 18 |
| 19 WebMClusterParser::WebMClusterParser(int64 timecode_scale, |
| 20 int audio_track_num, |
| 21 base::TimeDelta audio_default_duration, |
| 22 int video_track_num, |
| 23 base::TimeDelta video_default_duration) |
| 24 : timecode_multiplier_(timecode_scale / 1000.0), |
| 25 audio_track_num_(audio_track_num), |
| 26 audio_default_duration_(audio_default_duration), |
| 27 video_track_num_(video_track_num), |
| 28 video_default_duration_(video_default_duration), |
| 29 last_block_timecode_(-1), |
| 30 cluster_timecode_(-1) { |
| 31 } |
| 32 |
| 33 WebMClusterParser::~WebMClusterParser() {} |
| 34 |
| 35 int WebMClusterParser::Parse(const uint8* buf, int size) { |
| 36 last_block_timecode_ = -1; |
| 37 cluster_timecode_ = -1; |
| 38 audio_buffers_.clear(); |
| 39 video_buffers_.clear(); |
| 40 |
| 41 return WebMParseListElement(buf, size, kWebMIdCluster, 1, this); |
| 42 } |
| 43 |
| 44 bool WebMClusterParser::OnListStart(int id) { |
| 45 if (id == kWebMIdCluster) |
| 46 cluster_timecode_ = -1; |
| 47 |
| 48 return true; |
| 49 } |
| 50 |
| 51 bool WebMClusterParser::OnListEnd(int id) { |
| 52 if (id == kWebMIdCluster) |
| 53 cluster_timecode_ = -1; |
| 54 |
| 55 return true; |
| 56 } |
| 57 |
| 58 bool WebMClusterParser::OnUInt(int id, int64 val) { |
| 59 if (id == kWebMIdTimecode) { |
| 60 if (cluster_timecode_ != -1) |
| 61 return false; |
| 62 |
| 63 cluster_timecode_ = val; |
| 64 } |
| 65 |
| 66 return true; |
| 67 } |
| 68 |
| 69 bool WebMClusterParser::OnFloat(int id, double val) { |
| 70 VLOG(1) << "Unexpected float element with ID " << std::hex << id; |
| 71 return false; |
| 72 } |
| 73 |
| 74 bool WebMClusterParser::OnBinary(int id, const uint8* data, int size) { |
| 75 VLOG(1) << "Unexpected binary element with ID " << std::hex << id; |
| 76 return false; |
| 77 } |
| 78 |
| 79 bool WebMClusterParser::OnString(int id, const std::string& str) { |
| 80 VLOG(1) << "Unexpected string element with ID " << std::hex << id; |
| 81 return false; |
| 82 } |
| 83 |
| 84 bool WebMClusterParser::OnSimpleBlock(int track_num, int timecode, |
| 85 int flags, |
| 86 const uint8* data, int size) { |
| 87 if (cluster_timecode_ == -1) { |
| 88 VLOG(1) << "Got SimpleBlock before cluster timecode."; |
| 89 return false; |
| 90 } |
| 91 |
| 92 if (timecode < 0) { |
| 93 VLOG(1) << "Got SimpleBlock with negative timecode offset " << timecode; |
| 94 return false; |
| 95 } |
| 96 |
| 97 if (last_block_timecode_ != -1 && timecode < last_block_timecode_) { |
| 98 VLOG(1) << "Got SimpleBlock with a timecode before the previous block."; |
| 99 return false; |
| 100 } |
| 101 |
| 102 last_block_timecode_ = timecode; |
| 103 |
| 104 base::TimeDelta timestamp = base::TimeDelta::FromMicroseconds( |
| 105 (cluster_timecode_ + timecode) * timecode_multiplier_); |
| 106 |
| 107 scoped_refptr<Buffer> buffer(CreateBuffer(data, size)); |
| 108 buffer->SetTimestamp(timestamp); |
| 109 BufferQueue* queue = NULL; |
| 110 |
| 111 if (track_num == audio_track_num_) { |
| 112 buffer->SetDuration(audio_default_duration_); |
| 113 queue = &audio_buffers_; |
| 114 } else if (track_num == video_track_num_) { |
| 115 buffer->SetDuration(video_default_duration_); |
| 116 queue = &video_buffers_; |
| 117 } else { |
| 118 VLOG(1) << "Unexpected track number " << track_num; |
| 119 return false; |
| 120 } |
| 121 |
| 122 if (!queue->empty() && |
| 123 buffer->GetTimestamp() == queue->back()->GetTimestamp()) { |
| 124 VLOG(1) << "Got SimpleBlock timecode is not strictly monotonically " |
| 125 << "increasing for track " << track_num; |
| 126 return false; |
| 127 } |
| 128 |
| 129 queue->push_back(buffer); |
| 130 return true; |
| 131 } |
| 132 |
| 133 } // namespace media |
OLD | NEW |