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 #ifndef MEDIA_CAST_CAST_ENVIRONMENT_H_
6 #define MEDIA_CAST_CAST_ENVIRONMENT_H_
8 #include "base/basictypes.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/single_thread_task_runner.h"
12 #include "base/time/tick_clock.h"
13 #include "base/time/time.h"
14 #include "media/cast/logging/logging_defines.h"
15 #include "media/cast/logging/logging_impl.h"
20 class CastEnvironment
: public base::RefCountedThreadSafe
<CastEnvironment
> {
22 // An enumeration of the cast threads.
24 // The main thread is where the cast system is configured and where timers
25 // and network IO is performed.
27 // The audio thread is where all send side audio processing is done,
28 // primarily encoding / decoding but also re-sampling.
30 // The video encoder thread is where the video processing is done.
35 scoped_ptr
<base::TickClock
> clock
,
36 scoped_refptr
<base::SingleThreadTaskRunner
> main_thread_proxy
,
37 scoped_refptr
<base::SingleThreadTaskRunner
> audio_thread_proxy
,
38 scoped_refptr
<base::SingleThreadTaskRunner
> video_thread_proxy
);
40 // These are the same methods in message_loop.h, but are guaranteed to either
41 // get posted to the MessageLoop if it's still alive, or be deleted otherwise.
42 // They return true iff the thread existed and the task was posted. Note that
43 // even if the task is posted, there's no guarantee that it will run, since
44 // the target thread may already have a Quit message in its queue.
45 bool PostTask(ThreadId identifier
,
46 const tracked_objects::Location
& from_here
,
47 const base::Closure
& task
);
49 bool PostDelayedTask(ThreadId identifier
,
50 const tracked_objects::Location
& from_here
,
51 const base::Closure
& task
,
52 base::TimeDelta delay
);
54 bool CurrentlyOn(ThreadId identifier
);
56 // All of the media::cast implementation must use this TickClock.
57 base::TickClock
* Clock() const { return clock_
.get(); }
59 // Logging is not thread safe. Its methods should always be called from the
61 // TODO(hubbe): Logging should be a thread-safe interface.
62 LoggingImpl
* Logging() const { return logging_
.get(); }
64 scoped_refptr
<base::SingleThreadTaskRunner
> GetTaskRunner(
65 ThreadId identifier
) const;
67 bool HasAudioThread() {
68 return audio_thread_proxy_
? true : false;
71 bool HasVideoThread() {
72 return video_thread_proxy_
? true : false;
76 virtual ~CastEnvironment();
78 // Subclasses may override these.
79 scoped_refptr
<base::SingleThreadTaskRunner
> main_thread_proxy_
;
80 scoped_refptr
<base::SingleThreadTaskRunner
> audio_thread_proxy_
;
81 scoped_refptr
<base::SingleThreadTaskRunner
> video_thread_proxy_
;
82 scoped_ptr
<base::TickClock
> clock_
;
83 scoped_ptr
<LoggingImpl
> logging_
;
86 friend class base::RefCountedThreadSafe
<CastEnvironment
>;
88 DISALLOW_COPY_AND_ASSIGN(CastEnvironment
);
94 #endif // MEDIA_CAST_CAST_ENVIRONMENT_H_