| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "media/filters/ffmpeg_demuxer.h" | 5 #include "media/filters/ffmpeg_demuxer.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/base64.h" | 10 #include "base/base64.h" |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 // Convert the packet if there is a bitstream filter. | 109 // Convert the packet if there is a bitstream filter. |
| 110 if (packet->data && bitstream_converter_enabled_ && | 110 if (packet->data && bitstream_converter_enabled_ && |
| 111 !bitstream_converter_->ConvertPacket(packet.get())) { | 111 !bitstream_converter_->ConvertPacket(packet.get())) { |
| 112 LOG(ERROR) << "Format conversion failed."; | 112 LOG(ERROR) << "Format conversion failed."; |
| 113 } | 113 } |
| 114 | 114 |
| 115 // Get side data if any. For now, the only type of side_data is VP8 Alpha. We | 115 // Get side data if any. For now, the only type of side_data is VP8 Alpha. We |
| 116 // keep this generic so that other side_data types in the future can be | 116 // keep this generic so that other side_data types in the future can be |
| 117 // handled the same way as well. | 117 // handled the same way as well. |
| 118 av_packet_split_side_data(packet.get()); | 118 av_packet_split_side_data(packet.get()); |
| 119 |
| 119 scoped_refptr<DecoderBuffer> buffer; | 120 scoped_refptr<DecoderBuffer> buffer; |
| 120 | 121 |
| 121 if (type() == DemuxerStream::TEXT) { | 122 if (type() == DemuxerStream::TEXT) { |
| 122 int id_size = 0; | 123 int id_size = 0; |
| 123 uint8* id_data = av_packet_get_side_data( | 124 uint8* id_data = av_packet_get_side_data( |
| 124 packet.get(), | 125 packet.get(), |
| 125 AV_PKT_DATA_WEBVTT_IDENTIFIER, | 126 AV_PKT_DATA_WEBVTT_IDENTIFIER, |
| 126 &id_size); | 127 &id_size); |
| 127 | 128 |
| 128 int settings_size = 0; | 129 int settings_size = 0; |
| 129 uint8* settings_data = av_packet_get_side_data( | 130 uint8* settings_data = av_packet_get_side_data( |
| 130 packet.get(), | 131 packet.get(), |
| 131 AV_PKT_DATA_WEBVTT_SETTINGS, | 132 AV_PKT_DATA_WEBVTT_SETTINGS, |
| 132 &settings_size); | 133 &settings_size); |
| 133 | 134 |
| 134 std::vector<uint8> side_data; | 135 std::vector<uint8> side_data; |
| 135 MakeSideData(id_data, id_data + id_size, | 136 MakeSideData(id_data, id_data + id_size, |
| 136 settings_data, settings_data + settings_size, | 137 settings_data, settings_data + settings_size, |
| 137 &side_data); | 138 &side_data); |
| 138 | 139 |
| 139 buffer = DecoderBuffer::CopyFrom(packet.get()->data, packet.get()->size, | 140 buffer = DecoderBuffer::CopyFrom(packet.get()->data, packet.get()->size, |
| 140 side_data.data(), side_data.size()); | 141 side_data.data(), side_data.size()); |
| 141 } else { | 142 } else { |
| 142 int side_data_size = 0; | 143 int side_data_size = 0; |
| 143 uint8* side_data = av_packet_get_side_data( | 144 uint8* side_data = av_packet_get_side_data( |
| 144 packet.get(), | 145 packet.get(), |
| 145 AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL, | 146 AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL, |
| 146 &side_data_size); | 147 &side_data_size); |
| 147 | 148 |
| 149 scoped_ptr<DecryptConfig> decrypt_config; |
| 150 int data_offset = 0; |
| 151 if ((type() == DemuxerStream::AUDIO && audio_config_.is_encrypted()) || |
| 152 (type() == DemuxerStream::VIDEO && video_config_.is_encrypted())) { |
| 153 if (!WebMCreateDecryptConfig( |
| 154 packet->data, packet->size, |
| 155 reinterpret_cast<const uint8*>(encryption_key_id_.data()), |
| 156 encryption_key_id_.size(), |
| 157 &decrypt_config, |
| 158 &data_offset)) { |
| 159 LOG(ERROR) << "Creation of DecryptConfig failed."; |
| 160 } |
| 161 } |
| 162 |
| 148 // If a packet is returned by FFmpeg's av_parser_parse2() the packet will | 163 // If a packet is returned by FFmpeg's av_parser_parse2() the packet will |
| 149 // reference inner memory of FFmpeg. As such we should transfer the packet | 164 // reference inner memory of FFmpeg. As such we should transfer the packet |
| 150 // into memory we control. | 165 // into memory we control. |
| 151 if (side_data_size > 0) { | 166 if (side_data_size > 0) { |
| 152 buffer = DecoderBuffer::CopyFrom(packet.get()->data, packet.get()->size, | 167 buffer = DecoderBuffer::CopyFrom(packet.get()->data + data_offset, |
| 168 packet.get()->size - data_offset, |
| 153 side_data, side_data_size); | 169 side_data, side_data_size); |
| 154 } else { | 170 } else { |
| 155 buffer = DecoderBuffer::CopyFrom(packet.get()->data, packet.get()->size); | 171 buffer = DecoderBuffer::CopyFrom(packet.get()->data + data_offset, |
| 172 packet.get()->size - data_offset); |
| 156 } | 173 } |
| 157 | 174 |
| 158 int skip_samples_size = 0; | 175 int skip_samples_size = 0; |
| 159 uint8* skip_samples = av_packet_get_side_data(packet.get(), | 176 uint8* skip_samples = av_packet_get_side_data(packet.get(), |
| 160 AV_PKT_DATA_SKIP_SAMPLES, | 177 AV_PKT_DATA_SKIP_SAMPLES, |
| 161 &skip_samples_size); | 178 &skip_samples_size); |
| 162 const int kSkipSamplesValidSize = 10; | 179 const int kSkipSamplesValidSize = 10; |
| 163 const int kSkipSamplesOffset = 4; | 180 const int kSkipSamplesOffset = 4; |
| 164 if (skip_samples_size >= kSkipSamplesValidSize) { | 181 if (skip_samples_size >= kSkipSamplesValidSize) { |
| 165 int discard_padding_samples = base::ByteSwapToLE32( | 182 int discard_padding_samples = base::ByteSwapToLE32( |
| 166 *(reinterpret_cast<const uint32*>(skip_samples + | 183 *(reinterpret_cast<const uint32*>(skip_samples + |
| 167 kSkipSamplesOffset))); | 184 kSkipSamplesOffset))); |
| 168 // TODO(vigneshv): Change decoder buffer to use number of samples so that | 185 // TODO(vigneshv): Change decoder buffer to use number of samples so that |
| 169 // this conversion can be avoided. | 186 // this conversion can be avoided. |
| 170 buffer->set_discard_padding(base::TimeDelta::FromMicroseconds( | 187 buffer->set_discard_padding(base::TimeDelta::FromMicroseconds( |
| 171 discard_padding_samples * 1000000.0 / | 188 discard_padding_samples * 1000000.0 / |
| 172 audio_decoder_config().samples_per_second())); | 189 audio_decoder_config().samples_per_second())); |
| 173 } | 190 } |
| 174 } | |
| 175 | 191 |
| 176 if ((type() == DemuxerStream::AUDIO && audio_config_.is_encrypted()) || | 192 if (decrypt_config) |
| 177 (type() == DemuxerStream::VIDEO && video_config_.is_encrypted())) { | 193 buffer->set_decrypt_config(decrypt_config.Pass()); |
| 178 scoped_ptr<DecryptConfig> config(WebMCreateDecryptConfig( | |
| 179 packet->data, packet->size, | |
| 180 reinterpret_cast<const uint8*>(encryption_key_id_.data()), | |
| 181 encryption_key_id_.size())); | |
| 182 if (!config) | |
| 183 LOG(ERROR) << "Creation of DecryptConfig failed."; | |
| 184 buffer->set_decrypt_config(config.Pass()); | |
| 185 } | 194 } |
| 186 | 195 |
| 187 buffer->set_timestamp(ConvertStreamTimestamp( | 196 buffer->set_timestamp(ConvertStreamTimestamp( |
| 188 stream_->time_base, packet->pts)); | 197 stream_->time_base, packet->pts)); |
| 189 buffer->set_duration(ConvertStreamTimestamp( | 198 buffer->set_duration(ConvertStreamTimestamp( |
| 190 stream_->time_base, packet->duration)); | 199 stream_->time_base, packet->duration)); |
| 191 if (buffer->timestamp() != kNoTimestamp() && | 200 if (buffer->timestamp() != kNoTimestamp() && |
| 192 last_packet_timestamp_ != kNoTimestamp() && | 201 last_packet_timestamp_ != kNoTimestamp() && |
| 193 last_packet_timestamp_ < buffer->timestamp()) { | 202 last_packet_timestamp_ < buffer->timestamp()) { |
| 194 buffered_ranges_.Add(last_packet_timestamp_, buffer->timestamp()); | 203 buffered_ranges_.Add(last_packet_timestamp_, buffer->timestamp()); |
| (...skipping 739 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 934 } | 943 } |
| 935 for (size_t i = 0; i < buffered.size(); ++i) | 944 for (size_t i = 0; i < buffered.size(); ++i) |
| 936 host_->AddBufferedTimeRange(buffered.start(i), buffered.end(i)); | 945 host_->AddBufferedTimeRange(buffered.start(i), buffered.end(i)); |
| 937 } | 946 } |
| 938 | 947 |
| 939 void FFmpegDemuxer::OnDataSourceError() { | 948 void FFmpegDemuxer::OnDataSourceError() { |
| 940 host_->OnDemuxerError(PIPELINE_ERROR_READ); | 949 host_->OnDemuxerError(PIPELINE_ERROR_READ); |
| 941 } | 950 } |
| 942 | 951 |
| 943 } // namespace media | 952 } // namespace media |
| OLD | NEW |