File: mojo_mjpeg_decode_accelerator.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; 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 (116 lines) | stat: -rw-r--r-- 4,026 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
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/chromeos_camera/mojo_mjpeg_decode_accelerator.h"

#include <stddef.h>

#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/single_thread_task_runner.h"
#include "build/build_config.h"
#include "mojo/public/cpp/system/platform_handle.h"

namespace chromeos_camera {

MojoMjpegDecodeAccelerator::MojoMjpegDecodeAccelerator(
    scoped_refptr<base::SequencedTaskRunner> io_task_runner,
    mojo::PendingRemote<chromeos_camera::mojom::MjpegDecodeAccelerator>
        jpeg_decoder)
    : io_task_runner_(std::move(io_task_runner)),
      jpeg_decoder_remote_(std::move(jpeg_decoder)) {}

MojoMjpegDecodeAccelerator::~MojoMjpegDecodeAccelerator() {
  DCHECK(io_task_runner_->RunsTasksInCurrentSequence());
}

void MojoMjpegDecodeAccelerator::InitializeAsync(
    MjpegDecodeAccelerator::Client* client,
    MjpegDecodeAccelerator::InitCB init_cb) {
  DCHECK(io_task_runner_->RunsTasksInCurrentSequence());

  jpeg_decoder_.Bind(std::move(jpeg_decoder_remote_));

  // base::Unretained is safe because |this| owns |jpeg_decoder_|.
  jpeg_decoder_.set_disconnect_handler(
      base::BindOnce(&MojoMjpegDecodeAccelerator::OnLostConnectionToJpegDecoder,
                     base::Unretained(this)));
  jpeg_decoder_->Initialize(
      base::BindOnce(&MojoMjpegDecodeAccelerator::OnInitializeDone,
                     base::Unretained(this), std::move(init_cb), client));
}

void MojoMjpegDecodeAccelerator::Decode(
    media::BitstreamBuffer bitstream_buffer,
    media::VideoPixelFormat format,
    const gfx::Size& coded_size,
    base::UnsafeSharedMemoryRegion output_region) {
  DCHECK(io_task_runner_->RunsTasksInCurrentSequence());
  DCHECK(jpeg_decoder_.is_bound());

  const size_t output_buffer_size =
      media::VideoFrame::AllocationSize(format, coded_size);
  DCHECK_GE(output_region.GetSize(), output_buffer_size);

  mojo::ScopedSharedBufferHandle output_frame_handle =
      mojo::WrapUnsafeSharedMemoryRegion(std::move(output_region));
  if (!output_frame_handle.is_valid()) {
    DLOG(ERROR) << "Failed to duplicate handle of VideoFrame";
    return;
  }

  // base::Unretained is safe because |this| owns |jpeg_decoder_|.
  jpeg_decoder_->Decode(std::move(bitstream_buffer), coded_size,
                        std::move(output_frame_handle),
                        base::checked_cast<uint32_t>(output_buffer_size),
                        base::BindOnce(&MojoMjpegDecodeAccelerator::OnDecodeAck,
                                       base::Unretained(this)));
}

bool MojoMjpegDecodeAccelerator::IsSupported() {
  return true;
}

void MojoMjpegDecodeAccelerator::OnInitializeDone(
    MjpegDecodeAccelerator::InitCB init_cb,
    MjpegDecodeAccelerator::Client* client,
    bool success) {
  DCHECK(io_task_runner_->RunsTasksInCurrentSequence());

  if (success)
    client_ = client;

  std::move(init_cb).Run(success);
}

void MojoMjpegDecodeAccelerator::OnDecodeAck(
    int32_t bitstream_buffer_id,
    ::chromeos_camera::MjpegDecodeAccelerator::Error error) {
  DCHECK(io_task_runner_->RunsTasksInCurrentSequence());

  if (!client_)
    return;

  if (error == ::chromeos_camera::MjpegDecodeAccelerator::Error::NO_ERRORS) {
    client_->VideoFrameReady(bitstream_buffer_id);
    return;
  }

  // Only NotifyError once.
  // Client::NotifyError() may trigger deletion of |this|, so calling it needs
  // to be the last thing done on this stack!
  MjpegDecodeAccelerator::Client* client = client_;
  client_ = nullptr;
  client->NotifyError(bitstream_buffer_id, error);
}

void MojoMjpegDecodeAccelerator::OnLostConnectionToJpegDecoder() {
  DCHECK(io_task_runner_->RunsTasksInCurrentSequence());
  OnDecodeAck(
      MjpegDecodeAccelerator::kInvalidTaskId,
      ::chromeos_camera::MjpegDecodeAccelerator::Error::PLATFORM_FAILURE);
}

}  // namespace chromeos_camera