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.
4
5#include "content/renderer/media/texttrack_impl.h"
6
7#include "base/bind.h"
8#include "base/location.h"
9#include "base/message_loop/message_loop_proxy.h"
10#include "content/renderer/media/webinbandtexttrack_impl.h"
11#include "media/base/bind_to_loop.h"
12#include "third_party/WebKit/public/platform/WebInbandTextTrackClient.h"
13#include "third_party/WebKit/public/platform/WebMediaPlayerClient.h"
14
15namespace content {
16
17TextTrackImpl::TextTrackImpl(
18    const scoped_refptr<base::MessageLoopProxy>& message_loop,
19    blink::WebMediaPlayerClient* client,
20    scoped_ptr<WebInbandTextTrackImpl> text_track)
21    : message_loop_(message_loop),
22      client_(client),
23      text_track_(text_track.Pass()) {
24  client_->addTextTrack(text_track_.get());
25}
26
27TextTrackImpl::~TextTrackImpl() {
28  message_loop_->PostTask(
29      FROM_HERE,
30      base::Bind(&TextTrackImpl::OnRemoveTrack,
31                 client_,
32                 base::Passed(&text_track_)));
33}
34
35void TextTrackImpl::addWebVTTCue(const base::TimeDelta& start,
36                                 const base::TimeDelta& end,
37                                 const std::string& id,
38                                 const std::string& content,
39                                 const std::string& settings) {
40  message_loop_->PostTask(
41    FROM_HERE,
42    base::Bind(&TextTrackImpl::OnAddCue,
43                text_track_.get(),
44                start, end,
45                id, content, settings));
46}
47
48void TextTrackImpl::OnAddCue(WebInbandTextTrackImpl* text_track,
49                             const base::TimeDelta& start,
50                             const base::TimeDelta& end,
51                             const std::string& id,
52                             const std::string& content,
53                             const std::string& settings) {
54  if (blink::WebInbandTextTrackClient* client = text_track->client()) {
55    client->addWebVTTCue(start.InSecondsF(),
56                         end.InSecondsF(),
57                         blink::WebString::fromUTF8(id),
58                         blink::WebString::fromUTF8(content),
59                         blink::WebString::fromUTF8(settings));
60  }
61}
62
63void TextTrackImpl::OnRemoveTrack(
64    blink::WebMediaPlayerClient* client,
65    scoped_ptr<WebInbandTextTrackImpl> text_track) {
66  if (text_track->client())
67    client->removeTextTrack(text_track.get());
68}
69
70}  // namespace content
71