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/bind.h"
8 #include "base/location.h"
9 #include "base/logging.h"
10
11 using base::SingleThreadTaskRunner;
12
13 namespace {
14
DeleteLoggingOnMainThread(scoped_ptr<media::cast::LoggingImpl> logging)15 void DeleteLoggingOnMainThread(scoped_ptr<media::cast::LoggingImpl> logging) {
16 logging.reset();
17 }
18
19 } // namespace
20
21 namespace media {
22 namespace cast {
23
CastEnvironment(scoped_ptr<base::TickClock> clock,scoped_refptr<SingleThreadTaskRunner> main_thread_proxy,scoped_refptr<SingleThreadTaskRunner> audio_thread_proxy,scoped_refptr<SingleThreadTaskRunner> video_thread_proxy)24 CastEnvironment::CastEnvironment(
25 scoped_ptr<base::TickClock> clock,
26 scoped_refptr<SingleThreadTaskRunner> main_thread_proxy,
27 scoped_refptr<SingleThreadTaskRunner> audio_thread_proxy,
28 scoped_refptr<SingleThreadTaskRunner> video_thread_proxy)
29 : main_thread_proxy_(main_thread_proxy),
30 audio_thread_proxy_(audio_thread_proxy),
31 video_thread_proxy_(video_thread_proxy),
32 clock_(clock.Pass()),
33 logging_(new LoggingImpl) {}
34
~CastEnvironment()35 CastEnvironment::~CastEnvironment() {
36 // Logging must be deleted on the main thread.
37 if (main_thread_proxy_.get() &&
38 !main_thread_proxy_->RunsTasksOnCurrentThread()) {
39 main_thread_proxy_->PostTask(
40 FROM_HERE,
41 base::Bind(&DeleteLoggingOnMainThread, base::Passed(&logging_)));
42 }
43 }
44
PostTask(ThreadId identifier,const tracked_objects::Location & from_here,const base::Closure & task)45 bool CastEnvironment::PostTask(ThreadId identifier,
46 const tracked_objects::Location& from_here,
47 const base::Closure& task) {
48 return GetTaskRunner(identifier)->PostTask(from_here, task);
49 }
50
PostDelayedTask(ThreadId identifier,const tracked_objects::Location & from_here,const base::Closure & task,base::TimeDelta delay)51 bool CastEnvironment::PostDelayedTask(
52 ThreadId identifier,
53 const tracked_objects::Location& from_here,
54 const base::Closure& task,
55 base::TimeDelta delay) {
56 return GetTaskRunner(identifier)->PostDelayedTask(from_here, task, delay);
57 }
58
GetTaskRunner(ThreadId identifier) const59 scoped_refptr<SingleThreadTaskRunner> CastEnvironment::GetTaskRunner(
60 ThreadId identifier) const {
61 switch (identifier) {
62 case CastEnvironment::MAIN:
63 return main_thread_proxy_;
64 case CastEnvironment::AUDIO:
65 return audio_thread_proxy_;
66 case CastEnvironment::VIDEO:
67 return video_thread_proxy_;
68 default:
69 NOTREACHED() << "Invalid Thread identifier";
70 return NULL;
71 }
72 }
73
CurrentlyOn(ThreadId identifier)74 bool CastEnvironment::CurrentlyOn(ThreadId identifier) {
75 switch (identifier) {
76 case CastEnvironment::MAIN:
77 return main_thread_proxy_.get() &&
78 main_thread_proxy_->RunsTasksOnCurrentThread();
79 case CastEnvironment::AUDIO:
80 return audio_thread_proxy_.get() &&
81 audio_thread_proxy_->RunsTasksOnCurrentThread();
82 case CastEnvironment::VIDEO:
83 return video_thread_proxy_.get() &&
84 video_thread_proxy_->RunsTasksOnCurrentThread();
85 default:
86 NOTREACHED() << "Invalid thread identifier";
87 return false;
88 }
89 }
90
91 } // namespace cast
92 } // namespace media
93