summaryrefslogtreecommitdiffstats
path: root/media/tools/player_wtl/movie.cc
blob: 984cff1bec412c6c1ac30ed83b8deb21576afb82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/tools/player_wtl/movie.h"

#include "base/bind.h"
#include "base/memory/singleton.h"
#include "base/threading/platform_thread.h"
#include "base/utf_string_conversions.h"
#include "media/audio/audio_manager.h"
#include "media/audio/null_audio_sink.h"
#include "media/base/filter_collection.h"
#include "media/base/media_log.h"
#include "media/base/message_loop_factory.h"
#include "media/base/pipeline.h"
#include "media/filters/audio_renderer_impl.h"
#include "media/filters/ffmpeg_audio_decoder.h"
#include "media/filters/ffmpeg_demuxer.h"
#include "media/filters/ffmpeg_video_decoder.h"
#include "media/filters/file_data_source.h"
#include "media/filters/video_renderer_base.h"

namespace media {

static void OnBufferingState(media::Pipeline::BufferingState buffering_state) {}

Movie::Movie()
    : audio_manager_(AudioManager::Create()),
      enable_audio_(false),
      enable_draw_(true),
      enable_dump_yuv_file_(false),
      enable_pause_(false),
      max_threads_(0),
      play_rate_(1.0f),
      movie_dib_(NULL),
      movie_hwnd_(0) {
}

Movie::~Movie() {
}

Movie* Movie::GetInstance() {
  return Singleton<Movie>::get();
}

bool Movie::IsOpen() {
  return pipeline_ != NULL;
}

void Movie::SetFrameBuffer(HBITMAP hbmp, HWND hwnd) {
  movie_dib_ = hbmp;
  movie_hwnd_ = hwnd;
}

bool Movie::Open(const wchar_t* url, VideoRendererBase* video_renderer) {
  // Close previous movie.
  if (pipeline_) {
    Close();
  }

  message_loop_factory_.reset(new media::MessageLoopFactory());

  scoped_refptr<base::MessageLoopProxy> pipeline_loop =
      message_loop_factory_->GetMessageLoop(
          media::MessageLoopFactory::kPipeline);
  pipeline_ = new Pipeline(pipeline_loop, new media::MediaLog());

  // Open the file.
  std::string url_utf8 = WideToUTF8(string16(url));
  scoped_refptr<FileDataSource> data_source = new FileDataSource();
  if (!data_source->Initialize(url_utf8)) {
    return false;
  }

  // Create filter collection.
  scoped_ptr<FilterCollection> collection(new FilterCollection());
  collection->SetDemuxer(new FFmpegDemuxer(pipeline_loop, data_source));
  collection->AddAudioDecoder(new FFmpegAudioDecoder(
      base::Bind(&MessageLoopFactory::GetMessageLoop,
                 base::Unretained(message_loop_factory_.get()),
                 media::MessageLoopFactory::kAudioDecoder)));
  collection->GetVideoDecoders()->push_back(new FFmpegVideoDecoder(
      base::Bind(&MessageLoopFactory::GetMessageLoop,
                 base::Unretained(message_loop_factory_.get()),
                 media::MessageLoopFactory::kVideoDecoder),
      NULL));

  // TODO(vrk): Re-enabled audio. (crbug.com/112159)
  collection->AddAudioRenderer(
      new media::AudioRendererImpl(new media::NullAudioSink()));
  collection->AddVideoRenderer(video_renderer);

  // Create and start our pipeline.
  media::PipelineStatusNotification note;
  pipeline_->Start(
      collection.Pass(),
      media::PipelineStatusCB(),
      media::PipelineStatusCB(),
      note.Callback(),
      base::Bind(&OnBufferingState));

  // Wait until the pipeline is fully initialized.
  note.Wait();
  if (note.status() != PIPELINE_OK)
    return false;
  pipeline_->SetPlaybackRate(play_rate_);
  return true;
}

void Movie::Play(float rate) {
  // Begin playback.
  if (pipeline_)
    pipeline_->SetPlaybackRate(enable_pause_ ? 0.0f : rate);
  if (rate > 0.0f)
    play_rate_ = rate;
}

// Get playback rate.
float Movie::GetPlayRate() {
  return play_rate_;
}

// Get movie duration in seconds.
float Movie::GetDuration() {
  float duration = 0.f;
  if (pipeline_)
    duration = (pipeline_->GetMediaDuration()).InMicroseconds() / 1000000.0f;
  return duration;
}

// Get current movie position in seconds.
float Movie::GetPosition() {
  float position = 0.f;
  if (pipeline_)
    position = (pipeline_->GetMediaTime()).InMicroseconds() / 1000000.0f;
  return position;
}

// Set current movie position in seconds.
void Movie::SetPosition(float position) {
  int64 us = static_cast<int64>(position * 1000000);
  base::TimeDelta time = base::TimeDelta::FromMicroseconds(us);
  if (pipeline_)
    pipeline_->Seek(time, media::PipelineStatusCB());
}


// Set playback pause.
void Movie::SetPause(bool pause) {
  enable_pause_ = pause;
  Play(play_rate_);
}

// Get playback pause state.
bool Movie::GetPause() {
  return enable_pause_;
}

void Movie::SetAudioEnable(bool enable_audio) {
}

bool Movie::GetAudioEnable() {
  return enable_audio_;
}

void Movie::SetDrawEnable(bool enable_draw) {
  enable_draw_ = enable_draw;
}

bool Movie::GetDrawEnable() {
  return enable_draw_;
}

void Movie::SetDumpYuvFileEnable(bool enable_dump_yuv_file) {
  enable_dump_yuv_file_ = enable_dump_yuv_file;
}

bool Movie::GetDumpYuvFileEnable() {
  return enable_dump_yuv_file_;
}

// Teardown.
void Movie::Close() {
  if (pipeline_) {
    pipeline_->Stop(base::Closure());
    pipeline_ = NULL;
  }

  message_loop_factory_.reset();
}

}  // namespace media