Home | History | Annotate | Download | only in cast
      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 "media/cast/cast_environment.h"
      6 
      7 #include "base/logging.h"
      8 
      9 using base::TaskRunner;
     10 
     11 namespace media {
     12 namespace cast {
     13 
     14 CastEnvironment::CastEnvironment(
     15     base::TickClock* clock,
     16     scoped_refptr<TaskRunner> main_thread_proxy,
     17     scoped_refptr<TaskRunner> audio_encode_thread_proxy,
     18     scoped_refptr<TaskRunner> audio_decode_thread_proxy,
     19     scoped_refptr<TaskRunner> video_encode_thread_proxy,
     20     scoped_refptr<TaskRunner> video_decode_thread_proxy,
     21     const CastLoggingConfig& config)
     22     : clock_(clock),
     23       main_thread_proxy_(main_thread_proxy),
     24       audio_encode_thread_proxy_(audio_encode_thread_proxy),
     25       audio_decode_thread_proxy_(audio_decode_thread_proxy),
     26       video_encode_thread_proxy_(video_encode_thread_proxy),
     27       video_decode_thread_proxy_(video_decode_thread_proxy),
     28       logging_(new LoggingImpl(clock, main_thread_proxy, config)) {
     29   DCHECK(main_thread_proxy) << "Main thread required";
     30 }
     31 
     32 CastEnvironment::~CastEnvironment() {}
     33 
     34 bool CastEnvironment::PostTask(ThreadId identifier,
     35                           const tracked_objects::Location& from_here,
     36                           const base::Closure& task) {
     37   scoped_refptr<TaskRunner> task_runner =
     38       GetMessageTaskRunnerForThread(identifier);
     39 
     40   return task_runner->PostTask(from_here, task);
     41 }
     42 
     43 bool CastEnvironment::PostDelayedTask(ThreadId identifier,
     44                                  const tracked_objects::Location& from_here,
     45                                  const base::Closure& task,
     46                                  base::TimeDelta delay) {
     47   scoped_refptr<TaskRunner> task_runner =
     48       GetMessageTaskRunnerForThread(identifier);
     49 
     50   return task_runner->PostDelayedTask(from_here, task, delay);
     51 }
     52 
     53 scoped_refptr<TaskRunner> CastEnvironment::GetMessageTaskRunnerForThread(
     54     ThreadId identifier) {
     55   switch (identifier) {
     56     case CastEnvironment::MAIN:
     57       return main_thread_proxy_;
     58     case CastEnvironment::AUDIO_ENCODER:
     59       return audio_encode_thread_proxy_;
     60     case CastEnvironment::AUDIO_DECODER:
     61       return audio_decode_thread_proxy_;
     62     case CastEnvironment::VIDEO_ENCODER:
     63       return video_encode_thread_proxy_;
     64     case CastEnvironment::VIDEO_DECODER:
     65       return video_decode_thread_proxy_;
     66     default:
     67       NOTREACHED() << "Invalid Thread identifier";
     68       return NULL;
     69   }
     70 }
     71 
     72 bool CastEnvironment::CurrentlyOn(ThreadId identifier) {
     73   switch (identifier) {
     74     case CastEnvironment::MAIN:
     75       return main_thread_proxy_->RunsTasksOnCurrentThread();
     76     case CastEnvironment::AUDIO_ENCODER:
     77       return audio_encode_thread_proxy_->RunsTasksOnCurrentThread();
     78     case CastEnvironment::AUDIO_DECODER:
     79       return audio_decode_thread_proxy_->RunsTasksOnCurrentThread();
     80     case CastEnvironment::VIDEO_ENCODER:
     81       return video_encode_thread_proxy_->RunsTasksOnCurrentThread();
     82     case CastEnvironment::VIDEO_DECODER:
     83       return video_decode_thread_proxy_->RunsTasksOnCurrentThread();
     84     default:
     85       NOTREACHED() << "Invalid thread identifier";
     86       return false;
     87   }
     88 }
     89 
     90 base::TickClock* CastEnvironment::Clock() const {
     91   return clock_;
     92 }
     93 
     94 LoggingImpl* CastEnvironment::Logging() {
     95   DCHECK(CurrentlyOn(CastEnvironment::MAIN)) <<
     96       "Must be called from main thread";
     97   return logging_.get();
     98 }
     99 
    100 }  // namespace cast
    101 }  // namespace media
    102