summaryrefslogtreecommitdiffstats
path: root/media/cast/cast_environment.cc
blob: dddd0f7cd78347c82139bd99a4fc4fdb1c6cacf8 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// 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/cast/cast_environment.h"

#include "base/bind.h"
#include "base/location.h"
#include "base/logging.h"

using base::TaskRunner;

namespace {

void DeleteLoggingOnMainThread(scoped_ptr<media::cast::LoggingImpl> logging) {
  logging.reset();
}

}  // namespace

namespace media {
namespace cast {

CastEnvironment::CastEnvironment(
    scoped_ptr<base::TickClock> clock,
    scoped_refptr<TaskRunner> main_thread_proxy,
    scoped_refptr<TaskRunner> audio_encode_thread_proxy,
    scoped_refptr<TaskRunner> audio_decode_thread_proxy,
    scoped_refptr<TaskRunner> video_encode_thread_proxy,
    scoped_refptr<TaskRunner> video_decode_thread_proxy,
    scoped_refptr<TaskRunner> transport_thread_proxy,
    const CastLoggingConfig& config)
    : clock_(clock.Pass()),
      main_thread_proxy_(main_thread_proxy),
      audio_encode_thread_proxy_(audio_encode_thread_proxy),
      audio_decode_thread_proxy_(audio_decode_thread_proxy),
      video_encode_thread_proxy_(video_encode_thread_proxy),
      video_decode_thread_proxy_(video_decode_thread_proxy),
      transport_thread_proxy_(transport_thread_proxy),
      logging_(new LoggingImpl(main_thread_proxy, config)) {
  DCHECK(main_thread_proxy) << "Main thread required";
}

CastEnvironment::~CastEnvironment() {
  // Logging must be deleted on the main thread.
  if (main_thread_proxy_->RunsTasksOnCurrentThread()) {
    logging_.reset();
  } else {
    main_thread_proxy_->PostTask(
        FROM_HERE,
        base::Bind(&DeleteLoggingOnMainThread, base::Passed(&logging_)));
  }
}

bool CastEnvironment::PostTask(ThreadId identifier,
                               const tracked_objects::Location& from_here,
                               const base::Closure& task) {
  scoped_refptr<TaskRunner> task_runner =
      GetMessageTaskRunnerForThread(identifier);

  return task_runner->PostTask(from_here, task);
}

bool CastEnvironment::PostDelayedTask(ThreadId identifier,
                                 const tracked_objects::Location& from_here,
                                 const base::Closure& task,
                                 base::TimeDelta delay) {
  scoped_refptr<TaskRunner> task_runner =
      GetMessageTaskRunnerForThread(identifier);

  return task_runner->PostDelayedTask(from_here, task, delay);
}

scoped_refptr<TaskRunner> CastEnvironment::GetMessageTaskRunnerForThread(
    ThreadId identifier) {
  switch (identifier) {
    case CastEnvironment::MAIN:
      return main_thread_proxy_;
    case CastEnvironment::AUDIO_ENCODER:
      return audio_encode_thread_proxy_;
    case CastEnvironment::AUDIO_DECODER:
      return audio_decode_thread_proxy_;
    case CastEnvironment::VIDEO_ENCODER:
      return video_encode_thread_proxy_;
    case CastEnvironment::VIDEO_DECODER:
      return video_decode_thread_proxy_;
    case CastEnvironment::TRANSPORT:
      return transport_thread_proxy_;
    default:
      NOTREACHED() << "Invalid Thread identifier";
      return NULL;
  }
}

bool CastEnvironment::CurrentlyOn(ThreadId identifier) {
  switch (identifier) {
    case CastEnvironment::MAIN:
      return main_thread_proxy_->RunsTasksOnCurrentThread();
    case CastEnvironment::AUDIO_ENCODER:
      return audio_encode_thread_proxy_->RunsTasksOnCurrentThread();
    case CastEnvironment::AUDIO_DECODER:
      return audio_decode_thread_proxy_->RunsTasksOnCurrentThread();
    case CastEnvironment::VIDEO_ENCODER:
      return video_encode_thread_proxy_->RunsTasksOnCurrentThread();
    case CastEnvironment::VIDEO_DECODER:
      return video_decode_thread_proxy_->RunsTasksOnCurrentThread();
    case CastEnvironment::TRANSPORT:
      return transport_thread_proxy_->RunsTasksOnCurrentThread();
    default:
      NOTREACHED() << "Invalid thread identifier";
      return false;
  }
}

base::TickClock* CastEnvironment::Clock() const {
  return clock_.get();
}

LoggingImpl* CastEnvironment::Logging() {
  DCHECK(CurrentlyOn(CastEnvironment::MAIN)) <<
      "Must be called from main thread";
  return logging_.get();
}

}  // namespace cast
}  // namespace media