Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / chromecast / media / cma / ipc_streamer / coded_frame_provider_host.cc
blobacb6f520547c76bf21e27136d7bbe4520da81a33
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 #include "chromecast/media/cma/ipc_streamer/coded_frame_provider_host.h"
7 #include "base/bind.h"
8 #include "base/callback_helpers.h"
9 #include "chromecast/media/cma/base/decoder_buffer_base.h"
10 #include "chromecast/media/cma/ipc/media_message.h"
11 #include "chromecast/media/cma/ipc/media_message_fifo.h"
12 #include "chromecast/media/cma/ipc/media_message_type.h"
13 #include "chromecast/media/cma/ipc_streamer/audio_decoder_config_marshaller.h"
14 #include "chromecast/media/cma/ipc_streamer/decoder_buffer_base_marshaller.h"
15 #include "chromecast/media/cma/ipc_streamer/video_decoder_config_marshaller.h"
16 #include "media/base/decrypt_config.h"
18 namespace chromecast {
19 namespace media {
21 CodedFrameProviderHost::CodedFrameProviderHost(
22 scoped_ptr<MediaMessageFifo> media_message_fifo)
23 : fifo_(media_message_fifo.Pass()),
24 weak_factory_(this) {
25 weak_this_ = weak_factory_.GetWeakPtr();
26 thread_checker_.DetachFromThread();
29 CodedFrameProviderHost::~CodedFrameProviderHost() {
30 DCHECK(thread_checker_.CalledOnValidThread());
33 void CodedFrameProviderHost::Read(const ReadCB& read_cb) {
34 DCHECK(thread_checker_.CalledOnValidThread());
36 // Cannot be called if there is already a pending read.
37 DCHECK(read_cb_.is_null());
38 read_cb_ = read_cb;
40 ReadMessages();
43 void CodedFrameProviderHost::Flush(const base::Closure& flush_cb) {
44 DCHECK(thread_checker_.CalledOnValidThread());
45 audio_config_ = ::media::AudioDecoderConfig();
46 video_config_ = ::media::VideoDecoderConfig();
47 read_cb_.Reset();
48 fifo_->Flush();
49 flush_cb.Run();
52 void CodedFrameProviderHost::OnFifoWriteEvent() {
53 DCHECK(thread_checker_.CalledOnValidThread());
54 ReadMessages();
57 base::Closure CodedFrameProviderHost::GetFifoWriteEventCb() {
58 return base::Bind(&CodedFrameProviderHost::OnFifoWriteEvent, weak_this_);
61 void CodedFrameProviderHost::ReadMessages() {
62 // Read messages until a frame is provided (i.e. not just the audio/video
63 // configurations).
64 while (!read_cb_.is_null()) {
65 scoped_ptr<MediaMessage> msg(fifo_->Pop());
66 if (!msg)
67 break;
69 if (msg->type() == PaddingMediaMsg) {
70 // Ignore the message.
71 } else if (msg->type() == AudioConfigMediaMsg) {
72 audio_config_ = AudioDecoderConfigMarshaller::Read(msg.get());
73 } else if (msg->type() == VideoConfigMediaMsg) {
74 video_config_ = VideoDecoderConfigMarshaller::Read(msg.get());
75 } else if (msg->type() == FrameMediaMsg) {
76 scoped_refptr<DecoderBufferBase> buffer =
77 DecoderBufferBaseMarshaller::Read(msg.Pass());
78 base::ResetAndReturn(&read_cb_).Run(
79 buffer, audio_config_, video_config_);
80 audio_config_ = ::media::AudioDecoderConfig();
81 video_config_ = ::media::VideoDecoderConfig();
82 } else {
83 // Receiving an unexpected message.
84 // Possible use case (except software bugs): the renderer process has
85 // been compromised and an invalid message value has been written to
86 // the fifo. Crash the browser process in this case to avoid further
87 // security implications (so do not use NOTREACHED which crashes only
88 // in debug builds).
89 LOG(FATAL) << "Unknown media message";
94 } // namespace media
95 } // namespace chromecast