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/pipeline/video_pipeline_impl.h"
8 #include "chromecast/media/cma/backend/video_pipeline_device.h"
9 #include "chromecast/media/cma/base/buffering_defs.h"
10 #include "chromecast/media/cma/base/cma_logging.h"
11 #include "chromecast/media/cma/base/coded_frame_provider.h"
12 #include "chromecast/media/cma/pipeline/av_pipeline_impl.h"
13 #include "media/base/video_decoder_config.h"
15 namespace chromecast
{
19 const size_t kMaxVideoFrameSize
= 1024 * 1024;
22 VideoPipelineImpl::VideoPipelineImpl(VideoPipelineDevice
* video_device
)
23 : video_device_(video_device
),
25 weak_this_
= weak_factory_
.GetWeakPtr();
26 av_pipeline_impl_
.reset(new AvPipelineImpl(
28 base::Bind(&VideoPipelineImpl::OnUpdateConfig
, base::Unretained(this))));
31 VideoPipelineImpl::~VideoPipelineImpl() {
34 void VideoPipelineImpl::SetCodedFrameProvider(
35 scoped_ptr
<CodedFrameProvider
> frame_provider
) {
36 av_pipeline_impl_
->SetCodedFrameProvider(
37 frame_provider
.Pass(), kAppVideoBufferSize
, kMaxVideoFrameSize
);
40 bool VideoPipelineImpl::StartPlayingFrom(
42 const scoped_refptr
<BufferingState
>& buffering_state
) {
43 CMALOG(kLogControl
) << "VideoPipelineImpl::StartPlayingFrom t0="
44 << time
.InMilliseconds();
46 // Reset the pipeline statistics.
47 previous_stats_
= ::media::PipelineStatistics();
50 if (av_pipeline_impl_
->GetState() == AvPipelineImpl::kError
)
52 DCHECK_EQ(av_pipeline_impl_
->GetState(), AvPipelineImpl::kFlushed
);
54 if (!av_pipeline_impl_
->StartPlayingFrom(time
, buffering_state
)) {
55 av_pipeline_impl_
->TransitionToState(AvPipelineImpl::kError
);
58 av_pipeline_impl_
->TransitionToState(AvPipelineImpl::kPlaying
);
63 void VideoPipelineImpl::Flush(const ::media::PipelineStatusCB
& status_cb
) {
64 CMALOG(kLogControl
) << "VideoPipelineImpl::Flush";
65 if (av_pipeline_impl_
->GetState() == AvPipelineImpl::kError
) {
66 status_cb
.Run(::media::PIPELINE_ERROR_ABORT
);
69 DCHECK_EQ(av_pipeline_impl_
->GetState(), AvPipelineImpl::kPlaying
);
70 av_pipeline_impl_
->TransitionToState(AvPipelineImpl::kFlushing
);
71 av_pipeline_impl_
->Flush(
72 base::Bind(&VideoPipelineImpl::OnFlushDone
, weak_this_
, status_cb
));
75 void VideoPipelineImpl::OnFlushDone(
76 const ::media::PipelineStatusCB
& status_cb
) {
77 CMALOG(kLogControl
) << "VideoPipelineImpl::OnFlushDone";
78 if (av_pipeline_impl_
->GetState() == AvPipelineImpl::kError
) {
79 status_cb
.Run(::media::PIPELINE_ERROR_ABORT
);
82 av_pipeline_impl_
->TransitionToState(AvPipelineImpl::kFlushed
);
83 status_cb
.Run(::media::PIPELINE_OK
);
86 void VideoPipelineImpl::Stop() {
87 CMALOG(kLogControl
) << "VideoPipelineImpl::Stop";
88 av_pipeline_impl_
->Stop();
89 av_pipeline_impl_
->TransitionToState(AvPipelineImpl::kStopped
);
92 void VideoPipelineImpl::SetCdm(BrowserCdmCast
* media_keys
) {
93 av_pipeline_impl_
->SetCdm(media_keys
);
96 void VideoPipelineImpl::SetClient(const VideoPipelineClient
& client
) {
97 video_client_
= client
;
98 av_pipeline_impl_
->SetClient(client
.av_pipeline_client
);
101 void VideoPipelineImpl::Initialize(
102 const ::media::VideoDecoderConfig
& video_config
,
103 scoped_ptr
<CodedFrameProvider
> frame_provider
,
104 const ::media::PipelineStatusCB
& status_cb
) {
105 CMALOG(kLogControl
) << "VideoPipelineImpl::Initialize "
106 << video_config
.AsHumanReadableString();
107 VideoPipelineDevice::VideoClient client
;
108 client
.natural_size_changed_cb
=
109 base::Bind(&VideoPipelineImpl::OnNaturalSizeChanged
, weak_this_
);
110 video_device_
->SetVideoClient(client
);
112 SetCodedFrameProvider(frame_provider
.Pass());
114 if (!video_device_
->SetConfig(video_config
) ||
115 !av_pipeline_impl_
->Initialize()) {
116 status_cb
.Run(::media::PIPELINE_ERROR_INITIALIZATION_FAILED
);
119 av_pipeline_impl_
->TransitionToState(AvPipelineImpl::kFlushed
);
120 status_cb
.Run(::media::PIPELINE_OK
);
123 void VideoPipelineImpl::OnUpdateConfig(
124 const ::media::AudioDecoderConfig
& audio_config
,
125 const ::media::VideoDecoderConfig
& video_config
) {
126 if (video_config
.IsValidConfig()) {
127 CMALOG(kLogControl
) << "VideoPipelineImpl::OnUpdateConfig "
128 << video_config
.AsHumanReadableString();
130 bool success
= video_device_
->SetConfig(video_config
);
132 !video_client_
.av_pipeline_client
.playback_error_cb
.is_null()) {
133 video_client_
.av_pipeline_client
.playback_error_cb
.Run(
134 ::media::PIPELINE_ERROR_DECODE
);
139 void VideoPipelineImpl::OnNaturalSizeChanged(const gfx::Size
& size
) {
140 if (av_pipeline_impl_
->GetState() != AvPipelineImpl::kPlaying
)
143 if (!video_client_
.natural_size_changed_cb
.is_null())
144 video_client_
.natural_size_changed_cb
.Run(size
);
147 void VideoPipelineImpl::UpdateStatistics() {
148 if (video_client_
.av_pipeline_client
.statistics_cb
.is_null())
151 MediaComponentDevice::Statistics device_stats
;
152 if (!video_device_
->GetStatistics(&device_stats
))
155 ::media::PipelineStatistics current_stats
;
156 current_stats
.video_bytes_decoded
= device_stats
.decoded_bytes
;
157 current_stats
.video_frames_decoded
= device_stats
.decoded_samples
;
158 current_stats
.video_frames_dropped
= device_stats
.dropped_samples
;
160 ::media::PipelineStatistics delta_stats
;
161 delta_stats
.video_bytes_decoded
=
162 current_stats
.video_bytes_decoded
- previous_stats_
.video_bytes_decoded
;
163 delta_stats
.video_frames_decoded
=
164 current_stats
.video_frames_decoded
- previous_stats_
.video_frames_decoded
;
165 delta_stats
.video_frames_dropped
=
166 current_stats
.video_frames_dropped
- previous_stats_
.video_frames_dropped
;
168 previous_stats_
= current_stats
;
170 video_client_
.av_pipeline_client
.statistics_cb
.Run(delta_stats
);
174 } // namespace chromecast