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

media / capture / video / video_capture_device.cc [blame]

// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/capture/video/video_capture_device.h"

#include <string_view>

#include "base/command_line.h"
#include "base/containers/fixed_flat_set.h"
#include "base/functional/callback.h"
#include "base/i18n/timezone.h"
#include "base/strings/string_util.h"
#include "base/token.h"
#include "build/build_config.h"
#include "media/base/media_switches.h"
#include "media/base/video_frame_metadata.h"
#include "media/capture/mojom/video_capture_types.mojom.h"

namespace media {

CapturedExternalVideoBuffer::CapturedExternalVideoBuffer(
    gfx::GpuMemoryBufferHandle handle,
    VideoCaptureFormat format,
    gfx::ColorSpace color_space)
    : handle(std::move(handle)),
      format(std::move(format)),
      color_space(std::move(color_space)) {}

#if BUILDFLAG(IS_WIN)
CapturedExternalVideoBuffer::CapturedExternalVideoBuffer(
    Microsoft::WRL::ComPtr<IMFMediaBuffer> imf_buffer,
    gfx::GpuMemoryBufferHandle handle,
    VideoCaptureFormat format,
    gfx::ColorSpace color_space)
    : imf_buffer(std::move(imf_buffer)),
      handle(std::move(handle)),
      format(std::move(format)),
      color_space(std::move(color_space)) {}
#endif

CapturedExternalVideoBuffer::CapturedExternalVideoBuffer(
    CapturedExternalVideoBuffer&& other)
    : handle(std::move(other.handle)),
      format(std::move(other.format)),
      color_space(std::move(other.color_space)) {
#if BUILDFLAG(IS_WIN)
  imf_buffer = std::move(other.imf_buffer);
#endif
}

CapturedExternalVideoBuffer& CapturedExternalVideoBuffer::operator=(
    CapturedExternalVideoBuffer&& other) {
  handle = std::move(other.handle);
  format = std::move(other.format);
  color_space = std::move(other.color_space);
#if BUILDFLAG(IS_WIN)
  imf_buffer = std::move(other.imf_buffer);
#endif
  return *this;
}

CapturedExternalVideoBuffer::~CapturedExternalVideoBuffer() = default;

VideoCaptureDevice::Client::Buffer::Buffer() : id(0), frame_feedback_id(0) {}

VideoCaptureDevice::Client::Buffer::Buffer(
    int buffer_id,
    int frame_feedback_id,
    std::unique_ptr<HandleProvider> handle_provider,
    std::unique_ptr<ScopedAccessPermission> access_permission)
    : id(buffer_id),
      frame_feedback_id(frame_feedback_id),
      handle_provider(std::move(handle_provider)),
      access_permission(std::move(access_permission)) {}

VideoCaptureDevice::Client::Buffer::Buffer(
    VideoCaptureDevice::Client::Buffer&& other) = default;

VideoCaptureDevice::Client::Buffer::~Buffer() = default;

VideoCaptureDevice::Client::Buffer& VideoCaptureDevice::Client::Buffer::
operator=(VideoCaptureDevice::Client::Buffer&& other) = default;

void VideoCaptureDevice::Client::OnIncomingCapturedData(
    const uint8_t* data,
    int length,
    const VideoCaptureFormat& frame_format,
    const gfx::ColorSpace& color_space,
    int clockwise_rotation,
    bool flip_y,
    base::TimeTicks reference_time,
    base::TimeDelta timestamp,
    std::optional<base::TimeTicks> capture_begin_timestamp,
    const std::optional<VideoFrameMetadata>& metadata) {
  OnIncomingCapturedData(data, length, frame_format, color_space,
                         clockwise_rotation, flip_y, reference_time, timestamp,
                         capture_begin_timestamp, metadata,
                         /*frame_feedback_id=*/0);
}

void VideoCaptureDevice::Client::OnIncomingCapturedGfxBuffer(
    gfx::GpuMemoryBuffer* buffer,
    const VideoCaptureFormat& frame_format,
    int clockwise_rotation,
    base::TimeTicks reference_time,
    base::TimeDelta timestamp,
    std::optional<base::TimeTicks> capture_begin_timestamp,
    const std::optional<VideoFrameMetadata>& metadata) {
  OnIncomingCapturedGfxBuffer(buffer, frame_format, clockwise_rotation,
                              reference_time, timestamp,
                              capture_begin_timestamp, metadata,
                              /*frame_feedback_id=*/0);
}

VideoCaptureDevice::~VideoCaptureDevice() = default;

void VideoCaptureDevice::ApplySubCaptureTarget(
    mojom::SubCaptureTargetType type,
    const base::Token& target,
    uint32_t sub_capture_target_version,
    base::OnceCallback<void(media::mojom::ApplySubCaptureTargetResult)>
        callback) {
  std::move(callback).Run(
      media::mojom::ApplySubCaptureTargetResult::kUnsupportedCaptureDevice);
}

void VideoCaptureDevice::GetPhotoState(GetPhotoStateCallback callback) {}

void VideoCaptureDevice::SetPhotoOptions(mojom::PhotoSettingsPtr settings,
                                         SetPhotoOptionsCallback callback) {}

void VideoCaptureDevice::TakePhoto(TakePhotoCallback callback) {}

// static
PowerLineFrequency VideoCaptureDevice::GetPowerLineFrequencyForLocation() {
  const std::string current_country = base::CountryCodeForCurrentTimezone();
  if (current_country.empty()) {
    return PowerLineFrequency::kDefault;
  }

  // Sorted out list of countries with 60Hz power line frequency, from
  // http://en.wikipedia.org/wiki/Mains_electricity_by_country
  constexpr auto kCountriesUsing60Hz = base::MakeFixedFlatSet<std::string_view>(
      {"AI", "AO", "AS", "AW", "AZ", "BM", "BR", "BS", "BZ", "CA", "CO",
       "CR", "CU", "DO", "EC", "FM", "GT", "GU", "GY", "HN", "HT", "JP",
       "KN", "KR", "KY", "MS", "MX", "NI", "PA", "PE", "PF", "PH", "PR",
       "PW", "SA", "SR", "SV", "TT", "TW", "UM", "US", "VG", "VI", "VE"});
  if (kCountriesUsing60Hz.contains(current_country)) {
    return PowerLineFrequency::k60Hz;
  }
  return PowerLineFrequency::k50Hz;
}

// static
PowerLineFrequency VideoCaptureDevice::GetPowerLineFrequency(
    const VideoCaptureParams& params) {
  switch (params.power_line_frequency) {
    case PowerLineFrequency::k50Hz:  // fall through
    case PowerLineFrequency::k60Hz:
      return params.power_line_frequency;
    default:
      return GetPowerLineFrequencyForLocation();
  }
}

VideoCaptureFrameDropReason ConvertReservationFailureToFrameDropReason(
    VideoCaptureDevice::Client::ReserveResult reserve_result) {
  switch (reserve_result) {
    case VideoCaptureDevice::Client::ReserveResult::kSucceeded:
      return VideoCaptureFrameDropReason::kNone;
    case VideoCaptureDevice::Client::ReserveResult::kMaxBufferCountExceeded:
      return VideoCaptureFrameDropReason::kBufferPoolMaxBufferCountExceeded;
    case VideoCaptureDevice::Client::ReserveResult::kAllocationFailed:
      return VideoCaptureFrameDropReason::kBufferPoolBufferAllocationFailed;
  }
}

}  // namespace media