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

media / cast / cast_environment.cc [blame]

// Copyright 2013 The Chromium Authors
// 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 <utility>

#include "base/functional/bind.h"
#include "base/location.h"
#include "base/notreached.h"
#include "base/task/single_thread_task_runner.h"

using base::SingleThreadTaskRunner;

namespace media {
namespace cast {

CastEnvironment::CastEnvironment(
    const base::TickClock* clock,
    scoped_refptr<SingleThreadTaskRunner> main_thread_proxy,
    scoped_refptr<SingleThreadTaskRunner> audio_thread_proxy,
    scoped_refptr<SingleThreadTaskRunner> video_thread_proxy)
    : main_thread_proxy_(main_thread_proxy),
      audio_thread_proxy_(audio_thread_proxy),
      video_thread_proxy_(video_thread_proxy),
      clock_(clock),
      logger_(this) {}

CastEnvironment::~CastEnvironment() = default;

bool CastEnvironment::PostTask(ThreadId identifier,
                               const base::Location& from_here,
                               base::OnceClosure task) {
  return GetTaskRunner(identifier)->PostTask(from_here, std::move(task));
}

bool CastEnvironment::PostDelayedTask(ThreadId identifier,
                                      const base::Location& from_here,
                                      base::OnceClosure task,
                                      base::TimeDelta delay) {
  return GetTaskRunner(identifier)
      ->PostDelayedTask(from_here, std::move(task), delay);
}

scoped_refptr<SingleThreadTaskRunner> CastEnvironment::GetTaskRunner(
    ThreadId identifier) const {
  switch (identifier) {
    case CastEnvironment::MAIN:
      return main_thread_proxy_;
    case CastEnvironment::AUDIO:
      return audio_thread_proxy_;
    case CastEnvironment::VIDEO:
      return video_thread_proxy_;
    default:
      NOTREACHED() << "Invalid Thread identifier";
  }
}

bool CastEnvironment::CurrentlyOn(ThreadId identifier) {
  switch (identifier) {
    case CastEnvironment::MAIN:
      return main_thread_proxy_.get() &&
             main_thread_proxy_->RunsTasksInCurrentSequence();
    case CastEnvironment::AUDIO:
      return audio_thread_proxy_.get() &&
             audio_thread_proxy_->RunsTasksInCurrentSequence();
    case CastEnvironment::VIDEO:
      return video_thread_proxy_.get() &&
             video_thread_proxy_->RunsTasksInCurrentSequence();
    default:
      NOTREACHED() << "Invalid thread identifier";
  }
}

}  // namespace cast
}  // namespace media