1// Copyright 2014 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 CHROMECAST_MEDIA_CMA_BASE_BALANCED_TASK_RUNNER_FACTORY_H_ 6#define CHROMECAST_MEDIA_CMA_BASE_BALANCED_TASK_RUNNER_FACTORY_H_ 7 8#include <set> 9 10#include "base/macros.h" 11#include "base/memory/ref_counted.h" 12#include "base/synchronization/lock.h" 13#include "base/time/time.h" 14 15namespace base { 16class SingleThreadTaskRunner; 17} 18 19namespace chromecast { 20namespace media { 21class BalancedMediaTaskRunner; 22class MediaTaskRunner; 23 24// BalancedMediaTaskRunnerFactory - 25// Create media tasks runners that are loosely synchronized between each other. 26// For two tasks T1 and T2 with timestamps ts1 and ts2, the scheduler ensures 27// T2 is not scheduled before T1 if ts2 > ts1 + |max_delta|. 28class BalancedMediaTaskRunnerFactory 29 : public base::RefCountedThreadSafe<BalancedMediaTaskRunnerFactory> { 30 public: 31 explicit BalancedMediaTaskRunnerFactory(base::TimeDelta max_delta); 32 33 // Creates a media task runner using |task_runner| as the underlying 34 // regular task runner. 35 // Restriction on the returned media task runner: 36 // - can only schedule only one media task at a time. 37 // - timestamps of tasks posted on that task runner must be increasing. 38 scoped_refptr<MediaTaskRunner> CreateMediaTaskRunner( 39 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner); 40 41 private: 42 typedef std::set<scoped_refptr<BalancedMediaTaskRunner> > MediaTaskRunnerSet; 43 44 friend class base::RefCountedThreadSafe<BalancedMediaTaskRunnerFactory>; 45 virtual ~BalancedMediaTaskRunnerFactory(); 46 47 // Invoked when one of the registered media task runners received a new media 48 // task. 49 void OnNewTask(); 50 51 // Unregister a media task runner. 52 void UnregisterMediaTaskRunner( 53 const scoped_refptr<BalancedMediaTaskRunner>& media_task_runner); 54 55 // Maximum timestamp deviation between tasks from the registered task runners. 56 const base::TimeDelta max_delta_; 57 58 // Task runners created by the factory that have not been unregistered yet. 59 base::Lock lock_; 60 MediaTaskRunnerSet task_runners_; 61 62 DISALLOW_COPY_AND_ASSIGN(BalancedMediaTaskRunnerFactory); 63}; 64 65} // namespace media 66} // namespace chromecast 67 68#endif // CHROMECAST_MEDIA_CMA_BASE_BALANCED_TASK_RUNNER_FACTORY_H_ 69