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.
5 #include "media/cast/cast_environment.h"
8 #include "base/location.h"
9 #include "base/logging.h"
11 using base::TaskRunner
;
15 void DeleteLoggingOnMainThread(scoped_ptr
<media::cast::LoggingImpl
> logging
) {
24 CastEnvironment::CastEnvironment(
25 base::TickClock
* clock
,
26 scoped_refptr
<TaskRunner
> main_thread_proxy
,
27 scoped_refptr
<TaskRunner
> audio_encode_thread_proxy
,
28 scoped_refptr
<TaskRunner
> audio_decode_thread_proxy
,
29 scoped_refptr
<TaskRunner
> video_encode_thread_proxy
,
30 scoped_refptr
<TaskRunner
> video_decode_thread_proxy
,
31 scoped_refptr
<TaskRunner
> transport_thread_proxy
,
32 const CastLoggingConfig
& config
)
34 main_thread_proxy_(main_thread_proxy
),
35 audio_encode_thread_proxy_(audio_encode_thread_proxy
),
36 audio_decode_thread_proxy_(audio_decode_thread_proxy
),
37 video_encode_thread_proxy_(video_encode_thread_proxy
),
38 video_decode_thread_proxy_(video_decode_thread_proxy
),
39 transport_thread_proxy_(transport_thread_proxy
),
40 logging_(new LoggingImpl(main_thread_proxy
, config
)) {
41 DCHECK(main_thread_proxy
) << "Main thread required";
44 CastEnvironment::~CastEnvironment() {
45 // Logging must be deleted on the main thread.
46 if (main_thread_proxy_
->RunsTasksOnCurrentThread()) {
49 main_thread_proxy_
->PostTask(
51 base::Bind(&DeleteLoggingOnMainThread
, base::Passed(&logging_
)));
55 bool CastEnvironment::PostTask(ThreadId identifier
,
56 const tracked_objects::Location
& from_here
,
57 const base::Closure
& task
) {
58 scoped_refptr
<TaskRunner
> task_runner
=
59 GetMessageTaskRunnerForThread(identifier
);
61 return task_runner
->PostTask(from_here
, task
);
64 bool CastEnvironment::PostDelayedTask(ThreadId identifier
,
65 const tracked_objects::Location
& from_here
,
66 const base::Closure
& task
,
67 base::TimeDelta delay
) {
68 scoped_refptr
<TaskRunner
> task_runner
=
69 GetMessageTaskRunnerForThread(identifier
);
71 return task_runner
->PostDelayedTask(from_here
, task
, delay
);
74 scoped_refptr
<TaskRunner
> CastEnvironment::GetMessageTaskRunnerForThread(
75 ThreadId identifier
) {
77 case CastEnvironment::MAIN
:
78 return main_thread_proxy_
;
79 case CastEnvironment::AUDIO_ENCODER
:
80 return audio_encode_thread_proxy_
;
81 case CastEnvironment::AUDIO_DECODER
:
82 return audio_decode_thread_proxy_
;
83 case CastEnvironment::VIDEO_ENCODER
:
84 return video_encode_thread_proxy_
;
85 case CastEnvironment::VIDEO_DECODER
:
86 return video_decode_thread_proxy_
;
87 case CastEnvironment::TRANSPORT
:
88 return transport_thread_proxy_
;
90 NOTREACHED() << "Invalid Thread identifier";
95 bool CastEnvironment::CurrentlyOn(ThreadId identifier
) {
97 case CastEnvironment::MAIN
:
98 return main_thread_proxy_
->RunsTasksOnCurrentThread();
99 case CastEnvironment::AUDIO_ENCODER
:
100 return audio_encode_thread_proxy_
->RunsTasksOnCurrentThread();
101 case CastEnvironment::AUDIO_DECODER
:
102 return audio_decode_thread_proxy_
->RunsTasksOnCurrentThread();
103 case CastEnvironment::VIDEO_ENCODER
:
104 return video_encode_thread_proxy_
->RunsTasksOnCurrentThread();
105 case CastEnvironment::VIDEO_DECODER
:
106 return video_decode_thread_proxy_
->RunsTasksOnCurrentThread();
107 case CastEnvironment::TRANSPORT
:
108 return transport_thread_proxy_
->RunsTasksOnCurrentThread();
110 NOTREACHED() << "Invalid thread identifier";
115 base::TickClock
* CastEnvironment::Clock() const {
119 LoggingImpl
* CastEnvironment::Logging() {
120 DCHECK(CurrentlyOn(CastEnvironment::MAIN
)) <<
121 "Must be called from main thread";
122 return logging_
.get();