summaryrefslogtreecommitdiffstats
path: root/media/base/fake_text_track_stream.cc
blob: f18e5403b4e3c2ebbf19ea1f2078657f6b8935f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/base/fake_text_track_stream.h"

#include "base/bind.h"
#include "base/callback_helpers.h"
#include "media/base/decoder_buffer.h"
#include "media/filters/webvtt_util.h"

namespace media {

FakeTextTrackStream::FakeTextTrackStream()
    : task_runner_(base::MessageLoopProxy::current()),
      stopping_(false) {
}

FakeTextTrackStream::~FakeTextTrackStream() {
  DCHECK(read_cb_.is_null());
}

void FakeTextTrackStream::Read(const ReadCB& read_cb) {
  DCHECK(!read_cb.is_null());
  DCHECK(read_cb_.is_null());
  OnRead();
  read_cb_ = read_cb;

  if (stopping_) {
    task_runner_->PostTask(FROM_HERE, base::Bind(
        &FakeTextTrackStream::AbortPendingRead, base::Unretained(this)));
  }
}

DemuxerStream::Type FakeTextTrackStream::type() {
  return DemuxerStream::TEXT;
}

bool FakeTextTrackStream::SupportsConfigChanges() { return false; }

VideoRotation FakeTextTrackStream::video_rotation() {
  return VIDEO_ROTATION_0;
}

void FakeTextTrackStream::SatisfyPendingRead(
    const base::TimeDelta& start,
    const base::TimeDelta& duration,
    const std::string& id,
    const std::string& content,
    const std::string& settings) {
  DCHECK(!read_cb_.is_null());

  const uint8* const data_buf = reinterpret_cast<const uint8*>(content.data());
  const int data_len = static_cast<int>(content.size());

  std::vector<uint8> side_data;
  MakeSideData(id.begin(), id.end(),
                settings.begin(), settings.end(),
                &side_data);

  const uint8* const sd_buf = &side_data[0];
  const int sd_len = static_cast<int>(side_data.size());

  scoped_refptr<DecoderBuffer> buffer;
  buffer = DecoderBuffer::CopyFrom(data_buf, data_len, sd_buf, sd_len);

  buffer->set_timestamp(start);
  buffer->set_duration(duration);

  base::ResetAndReturn(&read_cb_).Run(kOk, buffer);
}

void FakeTextTrackStream::AbortPendingRead() {
  DCHECK(!read_cb_.is_null());
  base::ResetAndReturn(&read_cb_).Run(kAborted, NULL);
}

void FakeTextTrackStream::SendEosNotification() {
  DCHECK(!read_cb_.is_null());
  base::ResetAndReturn(&read_cb_).Run(kOk, DecoderBuffer::CreateEOSBuffer());
}

void FakeTextTrackStream::Stop() {
  stopping_ = true;
  if (!read_cb_.is_null())
    AbortPendingRead();
}

}  // namespace media