Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013 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/filters/ffmpeg_text_decoder.h" | |
| 6 | |
| 7 #include "base/callback_helpers.h" | |
| 8 #include "media/base/bind_to_loop.h" | |
| 9 #include "media/base/decoder_buffer.h" | |
| 10 #include "media/base/demuxer.h" | |
| 11 #include "media/base/text_buffer.h" | |
| 12 #include "media/filters/ffmpeg_glue.h" | |
| 13 | |
| 14 namespace media { | |
| 15 | |
| 16 FFmpegTextDecoder::FFmpegTextDecoder( | |
| 17 const scoped_refptr<base::MessageLoopProxy>& message_loop) | |
| 18 : message_loop_(message_loop), | |
| 19 weak_factory_(this) { | |
| 20 } | |
| 21 | |
| 22 FFmpegTextDecoder::~FFmpegTextDecoder() { | |
| 23 } | |
| 24 | |
| 25 void FFmpegTextDecoder::Initialize(Demuxer* demuxer) { | |
| 26 DCHECK(message_loop_->BelongsToCurrentThread()); | |
| 27 DCHECK_NE(demuxer, static_cast<Demuxer*>(NULL)); | |
| 28 | |
| 29 FFmpegGlue::InitializeFFmpeg(); | |
| 30 | |
| 31 if (!demuxer_streams_.empty()) { | |
| 32 // TODO(scherkus): initialization currently happens more than once in | |
| 33 // PipelineIntegrationTest.BasicPlayback. | |
|
acolwell GONE FROM CHROMIUM
2013/09/12 00:15:15
Do we really need this? I don't think we should be
Matthew Heaney (Chromium)
2013/09/13 19:51:54
Done.
| |
| 34 LOG(ERROR) << "Initialize has already been called."; | |
| 35 CHECK(false); | |
| 36 } | |
| 37 | |
| 38 weak_this_ = weak_factory_.GetWeakPtr(); | |
| 39 | |
| 40 int stream_count = demuxer->GetStreamCount(); | |
| 41 demuxer_streams_.reserve(stream_count); | |
| 42 | |
| 43 for (int idx = 0; idx < stream_count; ++idx) { | |
| 44 DemuxerStream* stream = demuxer->GetStreamByIndex(idx); | |
| 45 if (stream == NULL || stream->type() != DemuxerStream::TEXT) { | |
| 46 demuxer_streams_.push_back(NULL); | |
| 47 } else { | |
| 48 demuxer_streams_.push_back(stream); | |
| 49 } | |
| 50 } | |
| 51 | |
| 52 read_callbacks_.resize(stream_count); | |
| 53 } | |
| 54 | |
| 55 void FFmpegTextDecoder::Read(int index, const ReadCB& read_cb) { | |
| 56 DCHECK(message_loop_->BelongsToCurrentThread()); | |
| 57 DCHECK(!read_cb.is_null()); | |
| 58 | |
| 59 ReadCB& cb = read_callbacks_[index]; | |
| 60 CHECK(cb.is_null()) << "Overlapping decodes are not supported."; | |
| 61 | |
| 62 cb = BindToCurrentLoop(read_cb); | |
| 63 | |
| 64 DemuxerStream* stream = demuxer_streams_[index]; | |
| 65 CHECK(stream); | |
| 66 | |
| 67 stream->Read(base::Bind( | |
| 68 &FFmpegTextDecoder::BufferReady, weak_this_, index)); | |
| 69 } | |
| 70 | |
| 71 void FFmpegTextDecoder::BufferReady( | |
|
acolwell GONE FROM CHROMIUM
2013/09/12 00:15:15
nit:There doesn't appear to be anything FFmpeg spe
Matthew Heaney (Chromium)
2013/09/13 19:51:54
Done.
| |
| 72 int index, | |
| 73 DemuxerStream::Status status, | |
| 74 const scoped_refptr<DecoderBuffer>& input) { | |
| 75 DCHECK(message_loop_->BelongsToCurrentThread()); | |
| 76 | |
| 77 ReadCB& read_cb = read_callbacks_[index]; | |
| 78 DCHECK(!read_cb.is_null()); | |
| 79 | |
| 80 if (status == DemuxerStream::kAborted) { | |
| 81 DCHECK(!input.get()); | |
| 82 base::ResetAndReturn(&read_cb).Run(index, NULL); | |
| 83 return; | |
| 84 } | |
| 85 | |
| 86 if (input->end_of_stream()) { | |
| 87 base::ResetAndReturn(&read_cb).Run(index, NULL); | |
| 88 return; | |
| 89 } | |
| 90 | |
| 91 DCHECK_EQ(status, DemuxerStream::kOk); | |
| 92 DCHECK(input.get()); | |
| 93 | |
| 94 scoped_refptr<TextBuffer> text_buffer( | |
| 95 new TextBuffer(input->timestamp(), | |
| 96 input->duration())); | |
| 97 | |
| 98 // The side data here contains both the cue id and cue settings, | |
| 99 // separated using a 0xFF marker. | |
| 100 const uint8* side_data = input->side_data(); | |
| 101 DCHECK_NE(side_data, static_cast<const uint8*>(NULL)); | |
| 102 | |
| 103 int side_data_size = input->side_data_size(); | |
| 104 DCHECK_GE(side_data_size, 1); | |
| 105 | |
| 106 const uint8* side_data_end = side_data + side_data_size; | |
| 107 | |
| 108 const uint8* id_end = std::find(side_data, side_data_end, 0xFF); | |
| 109 DCHECK_NE(id_end, side_data_end); | |
| 110 DCHECK_EQ(*id_end, 0xFF); | |
| 111 | |
| 112 int id_size = id_end - side_data; | |
| 113 DCHECK_GE(id_size, 0); | |
| 114 | |
| 115 text_buffer->set_id(side_data, id_size); | |
| 116 | |
| 117 int settings_size = side_data_size - id_size - 1; | |
| 118 DCHECK_GE(settings_size, 0); | |
| 119 | |
| 120 text_buffer->set_settings(id_end + 1, settings_size); | |
| 121 text_buffer->set_text(input->data(), input->data_size()); | |
| 122 | |
| 123 base::ResetAndReturn(&read_cb).Run(index, text_buffer); | |
| 124 } | |
| 125 | |
| 126 } // namespace media | |
| OLD | NEW |