158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// found in the LICENSE file.
458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
54e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "media/cast/cast_environment.h"
658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/bind.h"
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/location.h"
958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "base/logging.h"
1058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)using base::SingleThreadTaskRunner;
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace {
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void DeleteLoggingOnMainThread(scoped_ptr<media::cast::LoggingImpl> logging) {
165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  logging.reset();
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace
2068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
2158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)namespace media {
2258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)namespace cast {
2358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
244e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)CastEnvironment::CastEnvironment(
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    scoped_ptr<base::TickClock> clock,
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    scoped_refptr<SingleThreadTaskRunner> main_thread_proxy,
27effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    scoped_refptr<SingleThreadTaskRunner> audio_thread_proxy,
28effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    scoped_refptr<SingleThreadTaskRunner> video_thread_proxy)
29a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    : main_thread_proxy_(main_thread_proxy),
30effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      audio_thread_proxy_(audio_thread_proxy),
31effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      video_thread_proxy_(video_thread_proxy),
32a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      clock_(clock.Pass()),
33effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      logging_(new LoggingImpl) {}
3458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)CastEnvironment::~CastEnvironment() {
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Logging must be deleted on the main thread.
371320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (main_thread_proxy_.get() &&
381320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      !main_thread_proxy_->RunsTasksOnCurrentThread()) {
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    main_thread_proxy_->PostTask(
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        FROM_HERE,
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        base::Bind(&DeleteLoggingOnMainThread, base::Passed(&logging_)));
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
444e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
454e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)bool CastEnvironment::PostTask(ThreadId identifier,
465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                               const tracked_objects::Location& from_here,
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                               const base::Closure& task) {
48a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return GetTaskRunner(identifier)->PostTask(from_here, task);
4958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
5058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool CastEnvironment::PostDelayedTask(
525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ThreadId identifier,
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const tracked_objects::Location& from_here,
545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const base::Closure& task,
555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    base::TimeDelta delay) {
56a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return GetTaskRunner(identifier)->PostDelayedTask(from_here, task, delay);
5758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
5858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
59a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)scoped_refptr<SingleThreadTaskRunner> CastEnvironment::GetTaskRunner(
60a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    ThreadId identifier) const {
6158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  switch (identifier) {
624e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    case CastEnvironment::MAIN:
6358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      return main_thread_proxy_;
64effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    case CastEnvironment::AUDIO:
65effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      return audio_thread_proxy_;
66effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    case CastEnvironment::VIDEO:
67effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      return video_thread_proxy_;
684e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    default:
69f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      NOTREACHED() << "Invalid Thread identifier";
704e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      return NULL;
7158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
7258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
7358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
744e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)bool CastEnvironment::CurrentlyOn(ThreadId identifier) {
754e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  switch (identifier) {
764e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    case CastEnvironment::MAIN:
771320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      return main_thread_proxy_.get() &&
78a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)             main_thread_proxy_->RunsTasksOnCurrentThread();
79effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    case CastEnvironment::AUDIO:
801320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      return audio_thread_proxy_.get() &&
81effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch             audio_thread_proxy_->RunsTasksOnCurrentThread();
82effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    case CastEnvironment::VIDEO:
831320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      return video_thread_proxy_.get() &&
84effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch             video_thread_proxy_->RunsTasksOnCurrentThread();
854e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    default:
86f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      NOTREACHED() << "Invalid thread identifier";
874e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      return false;
884e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  }
894e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
904e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
9158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}  // namespace cast
9258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}  // namespace media
93