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 #ifndef MEDIA_CAST_CAST_ENVIRONMENT_H_ 6 #define MEDIA_CAST_CAST_ENVIRONMENT_H_ 7 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" 16 17 namespace media { 18 namespace cast { 19 20 class CastEnvironment : public base::RefCountedThreadSafe<CastEnvironment> { 21 public: 22 // An enumeration of the cast threads. 23 enum ThreadId { 24 // The main thread is where the cast system is configured and where timers 25 // and network IO is performed. 26 MAIN, 27 // The audio thread is where all send side audio processing is done, 28 // primarily encoding / decoding but also re-sampling. 29 AUDIO, 30 // The video encoder thread is where the video processing is done. 31 VIDEO, 32 }; 33 34 CastEnvironment( 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); 39 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); 48 49 bool PostDelayedTask(ThreadId identifier, 50 const tracked_objects::Location& from_here, 51 const base::Closure& task, 52 base::TimeDelta delay); 53 54 bool CurrentlyOn(ThreadId identifier); 55 56 // All of the media::cast implementation must use this TickClock. 57 base::TickClock* Clock() const { return clock_.get(); } 58 59 // Logging is not thread safe. Its methods should always be called from the 60 // main thread. 61 // TODO(hubbe): Logging should be a thread-safe interface. 62 LoggingImpl* Logging() const { return logging_.get(); } 63 64 scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunner( 65 ThreadId identifier) const; 66 67 bool HasAudioThread() { return audio_thread_proxy_.get() ? true : false; } 68 69 bool HasVideoThread() { return video_thread_proxy_.get() ? true : false; } 70 71 protected: 72 virtual ~CastEnvironment(); 73 74 // Subclasses may override these. 75 scoped_refptr<base::SingleThreadTaskRunner> main_thread_proxy_; 76 scoped_refptr<base::SingleThreadTaskRunner> audio_thread_proxy_; 77 scoped_refptr<base::SingleThreadTaskRunner> video_thread_proxy_; 78 scoped_ptr<base::TickClock> clock_; 79 scoped_ptr<LoggingImpl> logging_; 80 81 private: 82 friend class base::RefCountedThreadSafe<CastEnvironment>; 83 84 DISALLOW_COPY_AND_ASSIGN(CastEnvironment); 85 }; 86 87 } // namespace cast 88 } // namespace media 89 90 #endif // MEDIA_CAST_CAST_ENVIRONMENT_H_ 91