Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / media / base / fake_text_track_stream.cc
blob183e61ef2caf14ef7ce5e0dcd6ff8e74b6db7cea
1 // Copyright 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.
5 #include "media/base/fake_text_track_stream.h"
7 #include "base/bind.h"
8 #include "base/callback_helpers.h"
9 #include "media/base/decoder_buffer.h"
10 #include "media/filters/webvtt_util.h"
12 namespace media {
14 FakeTextTrackStream::FakeTextTrackStream()
15 : task_runner_(base::MessageLoopProxy::current()),
16 stopping_(false) {
19 FakeTextTrackStream::~FakeTextTrackStream() {
20 DCHECK(read_cb_.is_null());
23 void FakeTextTrackStream::Read(const ReadCB& read_cb) {
24 DCHECK(!read_cb.is_null());
25 DCHECK(read_cb_.is_null());
26 OnRead();
27 read_cb_ = read_cb;
29 if (stopping_) {
30 task_runner_->PostTask(FROM_HERE, base::Bind(
31 &FakeTextTrackStream::AbortPendingRead, base::Unretained(this)));
35 DemuxerStream::Type FakeTextTrackStream::type() const {
36 return DemuxerStream::TEXT;
39 bool FakeTextTrackStream::SupportsConfigChanges() { return false; }
41 VideoRotation FakeTextTrackStream::video_rotation() {
42 return VIDEO_ROTATION_0;
45 void FakeTextTrackStream::SatisfyPendingRead(
46 const base::TimeDelta& start,
47 const base::TimeDelta& duration,
48 const std::string& id,
49 const std::string& content,
50 const std::string& settings) {
51 DCHECK(!read_cb_.is_null());
53 const uint8* const data_buf = reinterpret_cast<const uint8*>(content.data());
54 const int data_len = static_cast<int>(content.size());
56 std::vector<uint8> side_data;
57 MakeSideData(id.begin(), id.end(),
58 settings.begin(), settings.end(),
59 &side_data);
61 const uint8* const sd_buf = &side_data[0];
62 const int sd_len = static_cast<int>(side_data.size());
64 scoped_refptr<DecoderBuffer> buffer;
65 buffer = DecoderBuffer::CopyFrom(data_buf, data_len, sd_buf, sd_len);
67 buffer->set_timestamp(start);
68 buffer->set_duration(duration);
70 // Assume all fake text buffers are keyframes.
71 buffer->set_is_key_frame(true);
73 base::ResetAndReturn(&read_cb_).Run(kOk, buffer);
76 void FakeTextTrackStream::AbortPendingRead() {
77 DCHECK(!read_cb_.is_null());
78 base::ResetAndReturn(&read_cb_).Run(kAborted, NULL);
81 void FakeTextTrackStream::SendEosNotification() {
82 DCHECK(!read_cb_.is_null());
83 base::ResetAndReturn(&read_cb_).Run(kOk, DecoderBuffer::CreateEOSBuffer());
86 void FakeTextTrackStream::Stop() {
87 stopping_ = true;
88 if (!read_cb_.is_null())
89 AbortPendingRead();
92 } // namespace media