demuxer_stream_adapter.h revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
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_FILTERS_DEMUXER_STREAM_ADAPTER_H_
6#define CHROMECAST_MEDIA_CMA_FILTERS_DEMUXER_STREAM_ADAPTER_H_
7
8#include "base/callback.h"
9#include "base/memory/scoped_ptr.h"
10#include "base/memory/weak_ptr.h"
11#include "base/threading/thread_checker.h"
12#include "base/time/time.h"
13#include "chromecast/media/cma/base/coded_frame_provider.h"
14#include "media/base/audio_decoder_config.h"
15#include "media/base/demuxer_stream.h"
16#include "media/base/video_decoder_config.h"
17
18namespace base {
19class SingleThreadTaskRunner;
20}
21
22namespace media {
23class DemuxerStream;
24}
25
26namespace chromecast {
27namespace media {
28class BalancedMediaTaskRunnerFactory;
29class MediaTaskRunner;
30
31// DemuxerStreamAdapter wraps a DemuxerStream into a CodedFrameProvider.
32class DemuxerStreamAdapter : public CodedFrameProvider {
33 public:
34  DemuxerStreamAdapter(
35      const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
36      const scoped_refptr<BalancedMediaTaskRunnerFactory>&
37      media_task_runner_factory,
38      ::media::DemuxerStream* demuxer_stream);
39  virtual ~DemuxerStreamAdapter();
40
41  // CodedFrameProvider implementation.
42  virtual void Read(const ReadCB& read_cb) OVERRIDE;
43  virtual void Flush(const base::Closure& flush_cb) OVERRIDE;
44
45 private:
46  void ResetMediaTaskRunner();
47
48  void ReadInternal(const ReadCB& read_cb);
49  void RequestBuffer(const ReadCB& read_cb);
50
51  // Callback invoked from the demuxer stream to signal a buffer is ready.
52  void OnNewBuffer(const ReadCB& read_cb,
53                   ::media::DemuxerStream::Status status,
54                   const scoped_refptr< ::media::DecoderBuffer>& input);
55
56  base::ThreadChecker thread_checker_;
57
58  // Task runner DemuxerStreamAdapter is running on.
59  scoped_refptr<base::SingleThreadTaskRunner> const task_runner_;
60
61  // Media task runner to pace requests to the DemuxerStream.
62  scoped_refptr<BalancedMediaTaskRunnerFactory> const
63      media_task_runner_factory_;
64  scoped_refptr<MediaTaskRunner> media_task_runner_;
65  base::TimeDelta max_pts_;
66
67  // Frames are provided by |demuxer_stream_|.
68  ::media::DemuxerStream* const demuxer_stream_;
69
70  // Indicate if there is a pending read.
71  bool is_pending_read_;
72
73  // Indicate if |demuxer_stream_| has a pending read.
74  bool is_pending_demuxer_read_;
75
76  // In case of a pending flush operation, this is the callback
77  // that is invoked when flush is completed.
78  base::Closure flush_cb_;
79
80  // Audio/video configuration that applies to the next frame.
81  ::media::AudioDecoderConfig audio_config_;
82  ::media::VideoDecoderConfig video_config_;
83
84  base::WeakPtrFactory<DemuxerStreamAdapter> weak_factory_;
85  base::WeakPtr<DemuxerStreamAdapter> weak_this_;
86
87  DISALLOW_COPY_AND_ASSIGN(DemuxerStreamAdapter);
88};
89
90}  // namespace media
91}  // namespace chromecast
92
93#endif  // CHROMECAST_MEDIA_CMA_FILTERS_DEMUXER_STREAM_ADAPTER_H_
94