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.
5 #ifndef CHROMECAST_MEDIA_CMA_BASE_BALANCED_TASK_RUNNER_FACTORY_H_
6 #define CHROMECAST_MEDIA_CMA_BASE_BALANCED_TASK_RUNNER_FACTORY_H_
10 #include "base/macros.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/synchronization/lock.h"
13 #include "base/time/time.h"
16 class SingleThreadTaskRunner
;
19 namespace chromecast
{
21 class BalancedMediaTaskRunner
;
22 class MediaTaskRunner
;
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|.
28 class BalancedMediaTaskRunnerFactory
29 : public base::RefCountedThreadSafe
<BalancedMediaTaskRunnerFactory
> {
31 explicit BalancedMediaTaskRunnerFactory(base::TimeDelta max_delta
);
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
);
42 typedef std::set
<scoped_refptr
<BalancedMediaTaskRunner
> > MediaTaskRunnerSet
;
44 friend class base::RefCountedThreadSafe
<BalancedMediaTaskRunnerFactory
>;
45 virtual ~BalancedMediaTaskRunnerFactory();
47 // Invoked when one of the registered media task runners received a new media
51 // Unregister a media task runner.
52 void UnregisterMediaTaskRunner(
53 const scoped_refptr
<BalancedMediaTaskRunner
>& media_task_runner
);
55 // Maximum timestamp deviation between tasks from the registered task runners.
56 const base::TimeDelta max_delta_
;
58 // Task runners created by the factory that have not been unregistered yet.
60 MediaTaskRunnerSet task_runners_
;
62 DISALLOW_COPY_AND_ASSIGN(BalancedMediaTaskRunnerFactory
);
66 } // namespace chromecast
68 #endif // CHROMECAST_MEDIA_CMA_BASE_BALANCED_TASK_RUNNER_FACTORY_H_