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 <memory> 9 10 #include "base/macros.h" 11 #include "base/memory/ref_counted.h" 12 #include "base/single_thread_task_runner.h" 13 #include "base/time/tick_clock.h" 14 #include "base/time/time.h" 15 #include "media/cast/logging/log_event_dispatcher.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 const 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 base::Location& from_here, 47 base::OnceClosure task); 48 49 bool PostDelayedTask(ThreadId identifier, 50 const base::Location& from_here, 51 base::OnceClosure task, 52 base::TimeDelta delay); 53 54 bool CurrentlyOn(ThreadId identifier); 55 56 // All of the media::cast implementation must use this TickClock. Clock()57 const base::TickClock* Clock() const { return clock_; } 58 59 // Thread-safe log event dispatcher. logger()60 LogEventDispatcher* logger() { return &logger_; } 61 62 scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunner( 63 ThreadId identifier) const; 64 HasAudioThread()65 bool HasAudioThread() { return audio_thread_proxy_.get() ? true : false; } 66 HasVideoThread()67 bool HasVideoThread() { return video_thread_proxy_.get() ? true : false; } 68 69 protected: 70 virtual ~CastEnvironment(); 71 72 // Subclasses may final these. 73 scoped_refptr<base::SingleThreadTaskRunner> main_thread_proxy_; 74 scoped_refptr<base::SingleThreadTaskRunner> audio_thread_proxy_; 75 scoped_refptr<base::SingleThreadTaskRunner> video_thread_proxy_; 76 const base::TickClock* clock_; 77 LogEventDispatcher logger_; 78 79 private: 80 friend class base::RefCountedThreadSafe<CastEnvironment>; 81 82 DISALLOW_COPY_AND_ASSIGN(CastEnvironment); 83 }; 84 85 } // namespace cast 86 } // namespace media 87 88 #endif // MEDIA_CAST_CAST_ENVIRONMENT_H_ 89