File: thumbnail_media_parser_impl.h

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (123 lines) | stat: -rw-r--r-- 4,574 bytes parent folder | download | duplicates (5)
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
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_THUMBNAIL_GENERATOR_ANDROID_THUMBNAIL_MEDIA_PARSER_IMPL_H_
#define CHROME_BROWSER_THUMBNAIL_GENERATOR_ANDROID_THUMBNAIL_MEDIA_PARSER_IMPL_H_

#include <memory>
#include <string>
#include <vector>

#include "base/files/file_path.h"
#include "base/functional/callback.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/task/sequenced_task_runner.h"
#include "base/timer/timer.h"
#include "chrome/browser/thumbnail/generator/android/stats.h"
#include "chrome/browser/thumbnail/generator/android/thumbnail_media_parser.h"
#include "chrome/common/media_galleries/metadata_types.h"
#include "media/base/media_log.h"
#include "media/mojo/mojom/interface_factory.mojom.h"
#include "mojo/public/cpp/bindings/remote.h"

namespace media {
class GpuVideoAcceleratorFactories;
class MojoVideoDecoder;
class VideoDecoderConfig;
class VideoThumbnailDecoder;
}  // namespace media

class SkBitmap;

// Parse media file in remote process using ffmpeg to extract video frame.
// The decoding may happen in utility or GPU process based on video codec.
// For video file, the thumbnail will be the a video key frame.
class ThumbnailMediaParserImpl : public ThumbnailMediaParser,
                                 public MediaParserProvider,
                                 public media::MediaLog {
 public:
  ThumbnailMediaParserImpl(const std::string& mime_type,
                           const base::FilePath& file_path);

  ThumbnailMediaParserImpl(const ThumbnailMediaParserImpl&) = delete;
  ThumbnailMediaParserImpl& operator=(const ThumbnailMediaParserImpl&) = delete;

  ~ThumbnailMediaParserImpl() override;

  // ThumbnailMediaParser implementation.
  void Start(ParseCompleteCB parse_complete_cb) override;

 private:
  void OnReadFileSize(std::optional<int64_t> file_size);

  // MediaParserProvider implementation:
  void OnMediaParserCreated() override;
  void OnConnectionError() override;

  // Called after media metadata are parsed.
  void OnMediaMetadataParsed(
      bool parse_success,
      chrome::mojom::MediaMetadataPtr metadata,
      const std::vector<metadata::AttachedImage>& attached_images);

  // Retrieves an encoded video frame.
  void RetrieveEncodedVideoFrame();
  void OnVideoFrameRetrieved(chrome::mojom::ExtractVideoFrameResultPtr result);

  // Decodes the video frame.
  void OnGpuVideoAcceleratorFactoriesReady(
      std::unique_ptr<media::GpuVideoAcceleratorFactories>);
  void DecodeVideoFrame();
  void OnVideoFrameDecoded(scoped_refptr<media::VideoFrame> decoded_frame);

  // Renders the video frame to bitmap.
  void RenderVideoFrame(scoped_refptr<media::VideoFrame> video_frame);

  media::mojom::InterfaceFactory* GetMediaInterfaceFactory();
  void OnDecoderConnectionError();

  // Overlays media data source read operation. Gradually read data from media
  // file.
  void OnMediaDataReady(chrome::mojom::MediaDataSource::ReadCallback callback,
                        std::string data);

  void NotifyComplete(SkBitmap bitmap);
  void OnError(MediaParserEvent event);

  int64_t size_;
  std::string mime_type_;
  base::FilePath file_path_;

  ParseCompleteCB parse_complete_cb_;
  chrome::mojom::MediaMetadataPtr metadata_;

  // Used to read media files chunks to feed to IPC channel.
  std::unique_ptr<chrome::mojom::MediaDataSource> media_data_source_;

  // The task runner to do blocking disk IO.
  scoped_refptr<base::SequencedTaskRunner> file_task_runner_;

  // A timer to prevent unresponsive Android decoder during video file parsing,
  // and the parser will fail gracefully after timeout.
  base::OneShotTimer timer_;

  // Cached video frame data, which contains either encoded frame or decoded
  // video frame. Encoded frame is extracted with ffmpeg, the data can be large
  // for high resolution video.
  chrome::mojom::VideoFrameDataPtr video_frame_data_;

  // Objects used to decode the video into media::VideoFrame with
  // MojoVideoDecoder.
  media::VideoDecoderConfig config_;
  // `gpu_factories_` must outlive `decoder_`.
  std::unique_ptr<media::GpuVideoAcceleratorFactories> gpu_factories_;
  std::unique_ptr<media::VideoThumbnailDecoder> decoder_;
  mojo::Remote<media::mojom::InterfaceFactory> media_interface_factory_;
  bool decode_done_;

  base::WeakPtrFactory<ThumbnailMediaParserImpl> weak_factory_{this};
};

#endif  // CHROME_BROWSER_THUMBNAIL_GENERATOR_ANDROID_THUMBNAIL_MEDIA_PARSER_IMPL_H_