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_IPC_STREAMER_AV_STREAMER_PROXY_H_
6#define CHROMECAST_MEDIA_CMA_IPC_STREAMER_AV_STREAMER_PROXY_H_
7
8#include "base/callback.h"
9#include "base/macros.h"
10#include "base/memory/scoped_ptr.h"
11#include "base/memory/weak_ptr.h"
12#include "base/threading/thread_checker.h"
13#include "media/base/audio_decoder_config.h"
14#include "media/base/video_decoder_config.h"
15
16namespace chromecast {
17namespace media {
18class CodedFrameProvider;
19class DecoderBufferBase;
20class MediaMessageFifo;
21
22// AvStreamerProxy streams audio/video data from a coded frame provider
23// to a media message fifo.
24class AvStreamerProxy {
25 public:
26  AvStreamerProxy();
27  ~AvStreamerProxy();
28
29  // AvStreamerProxy gets frames and audio/video config
30  // from the |frame_provider| and feed them into the |fifo|.
31  void SetCodedFrameProvider(scoped_ptr<CodedFrameProvider> frame_provider);
32  void SetMediaMessageFifo(scoped_ptr<MediaMessageFifo> fifo);
33
34  // Starts fetching A/V buffers.
35  void Start();
36
37  // Stops fetching A/V buffers and flush the pending buffers,
38  // invoking |flush_cb| when done.
39  void StopAndFlush(const base::Closure& flush_cb);
40
41  // Event invoked when some data have been read from the fifo.
42  // This means some data can now possibly be written into the fifo.
43  void OnFifoReadEvent();
44
45 private:
46  void RequestBufferIfNeeded();
47
48  void OnNewBuffer(const scoped_refptr<DecoderBufferBase>& buffer,
49                   const ::media::AudioDecoderConfig& audio_config,
50                   const ::media::VideoDecoderConfig& video_config);
51
52  void ProcessPendingData();
53
54  bool SendAudioDecoderConfig(const ::media::AudioDecoderConfig& config);
55  bool SendVideoDecoderConfig(const ::media::VideoDecoderConfig& config);
56  bool SendBuffer(const scoped_refptr<DecoderBufferBase>& buffer);
57
58  base::ThreadChecker thread_checker_;
59
60  scoped_ptr<CodedFrameProvider> frame_provider_;
61
62  // Fifo used to convey A/V configs and buffers.
63  scoped_ptr<MediaMessageFifo> fifo_;
64
65  // State.
66  bool is_running_;
67
68  // Indicates if there is a pending request to the coded frame provider.
69  bool pending_read_;
70
71  // Pending config & buffer.
72  // |pending_av_data_| is set as long as one of the pending audio/video
73  // config or buffer is valid.
74  bool pending_av_data_;
75  ::media::AudioDecoderConfig pending_audio_config_;
76  ::media::VideoDecoderConfig pending_video_config_;
77  scoped_refptr<DecoderBufferBase> pending_buffer_;
78
79  base::WeakPtrFactory<AvStreamerProxy> weak_factory_;
80  base::WeakPtr<AvStreamerProxy> weak_this_;
81
82  DISALLOW_COPY_AND_ASSIGN(AvStreamerProxy);
83};
84
85}  // namespace media
86}  // namespace chromecast
87
88#endif  // CHROMECAST_MEDIA_CMA_IPC_STREAMER_AV_STREAMER_PROXY_H_
89