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
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220
  221
  222
  223
  224
  225
  226
  227
  228
  229
  230
  231
  232
  233
  234
  235
  236
  237
  238
  239
  240
  241
  242
  243
  244
  245
  246
  247
  248
  249
  250
  251
  252
  253
  254
  255
  256
  257
  258
  259
  260
  261
  262
  263
  264
  265
  266
  267
  268
  269
  270
  271
  272
  273
  274
  275
  276
  277
  278
  279
  280
  281
  282
  283
  284
  285
  286
  287
  288
  289
  290
  291
  292
  293
  294
  295
  296
  297
  298
  299
  300
  301
  302
  303

media / capture / video / video_capture_buffer_pool_impl.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_buffer_pool_impl.h"

#include <memory>

#include "base/feature_list.h"
#include "base/logging.h"
#include "base/memory/platform_shared_memory_region.h"
#include "base/memory/ptr_util.h"
#include "base/notreached.h"
#include "build/build_config.h"
#include "media/capture/video/video_capture_buffer_handle.h"
#include "media/capture/video/video_capture_buffer_pool_util.h"
#include "media/capture/video/video_capture_buffer_tracker.h"
#include "media/capture/video/video_capture_buffer_tracker_factory_impl.h"
#include "media/capture/video/video_capture_device_client.h"
#include "ui/gfx/buffer_format_util.h"

namespace media {

VideoCaptureBufferPoolImpl::VideoCaptureBufferPoolImpl(
    VideoCaptureBufferType buffer_type)
    : VideoCaptureBufferPoolImpl(
          buffer_type,
          kVideoCaptureDefaultMaxBufferPoolSize,
          std::make_unique<media::VideoCaptureBufferTrackerFactoryImpl>()) {}

VideoCaptureBufferPoolImpl::VideoCaptureBufferPoolImpl(
    VideoCaptureBufferType buffer_type,
    int count)
    : VideoCaptureBufferPoolImpl(
          buffer_type,
          count,
          std::make_unique<media::VideoCaptureBufferTrackerFactoryImpl>()) {}

VideoCaptureBufferPoolImpl::VideoCaptureBufferPoolImpl(
    VideoCaptureBufferType buffer_type,
    int count,
    std::unique_ptr<VideoCaptureBufferTrackerFactory> buffer_tracker_factory)
    : buffer_type_(buffer_type),
      count_(count),
      buffer_tracker_factory_(std::move(buffer_tracker_factory)) {
  DCHECK_GT(count, 0);
}

VideoCaptureBufferPoolImpl::~VideoCaptureBufferPoolImpl() = default;

base::UnsafeSharedMemoryRegion
VideoCaptureBufferPoolImpl::DuplicateAsUnsafeRegion(int buffer_id) {
  base::AutoLock lock(lock_);

  VideoCaptureBufferTracker* tracker = GetTracker(buffer_id);
  if (!tracker) {
    NOTREACHED() << "Invalid buffer_id.";
  }
  return tracker->DuplicateAsUnsafeRegion();
}

std::unique_ptr<VideoCaptureBufferHandle>
VideoCaptureBufferPoolImpl::GetHandleForInProcessAccess(int buffer_id) {
  base::AutoLock lock(lock_);

  VideoCaptureBufferTracker* tracker = GetTracker(buffer_id);
  if (!tracker) {
    NOTREACHED() << "Invalid buffer_id.";
  }

  return tracker->GetMemoryMappedAccess();
}

gfx::GpuMemoryBufferHandle VideoCaptureBufferPoolImpl::GetGpuMemoryBufferHandle(
    int buffer_id) {
  base::AutoLock lock(lock_);
  VideoCaptureBufferTracker* tracker = GetTracker(buffer_id);
  if (!tracker) {
    NOTREACHED() << "Invalid buffer_id.";
  }

  return tracker->GetGpuMemoryBufferHandle();
}

VideoCaptureBufferType VideoCaptureBufferPoolImpl::GetBufferType(
    int buffer_id) {
  base::AutoLock lock(lock_);

  VideoCaptureBufferTracker* tracker = GetTracker(buffer_id);
  if (!tracker) {
    NOTREACHED() << "Unrecognized buffer id, buffer_id=" << buffer_id;
  }

  return tracker->GetBufferType();
}

VideoCaptureDevice::Client::ReserveResult
VideoCaptureBufferPoolImpl::ReserveForProducer(
    const gfx::Size& dimensions,
    VideoPixelFormat format,
    const mojom::PlaneStridesPtr& strides,
    int frame_feedback_id,
    int* buffer_id,
    int* buffer_id_to_drop) {
  base::AutoLock lock(lock_);
  return ReserveForProducerInternal(dimensions, format, strides,
                                    frame_feedback_id, buffer_id,
                                    buffer_id_to_drop);
}

void VideoCaptureBufferPoolImpl::RelinquishProducerReservation(int buffer_id) {
  base::AutoLock lock(lock_);
  VideoCaptureBufferTracker* tracker = GetTracker(buffer_id);
  if (!tracker) {
    NOTREACHED() << "Invalid buffer_id.";
  }
  tracker->SetHeldByProducer(false);
}

VideoCaptureDevice::Client::ReserveResult
VideoCaptureBufferPoolImpl::ReserveIdForExternalBuffer(
    CapturedExternalVideoBuffer buffer,
    const gfx::Size& dimensions,
    int* buffer_id_to_drop,
    int* buffer_id) {
  DCHECK(buffer_id);
  DCHECK(buffer_id_to_drop);
  base::AutoLock lock(lock_);

  // Look for a tracker that matches this buffer and is not in use. While
  // iterating, find the least recently used tracker.
  *buffer_id_to_drop = kInvalidId;
  *buffer_id = kInvalidId;
  auto lru_tracker_it = trackers_.end();
  for (auto it = trackers_.begin(); it != trackers_.end(); ++it) {
    VideoCaptureBufferTracker* const tracker = it->second.get();
    if (tracker->IsHeldByProducerOrConsumer()) {
      continue;
    }

    if (tracker->IsSameGpuMemoryBuffer(buffer.handle)) {
      tracker->SetHeldByProducer(true);
      tracker->UpdateExternalData(std::move(buffer));
      *buffer_id = it->first;
      return VideoCaptureDevice::Client::ReserveResult::kSucceeded;
    }

    if (lru_tracker_it == trackers_.end() ||
        lru_tracker_it->second->LastCustomerUseSequenceNumber() >
            tracker->LastCustomerUseSequenceNumber()) {
      lru_tracker_it = it;
    }
  }

  // Preferably grow the pool by creating a new tracker. If we're at maximum
  // size, reallocate by deleting an existing one.
  if (trackers_.size() == static_cast<size_t>(count_)) {
    if (lru_tracker_it == trackers_.end()) {
      // We're out of space, and can't find an unused tracker to reallocate.
      DLOG(ERROR) << __func__
                  << " max buffer count exceeded count_ = " << count_;
      return VideoCaptureDevice::Client::ReserveResult::kMaxBufferCountExceeded;
    }
    *buffer_id_to_drop = lru_tracker_it->first;
    trackers_.erase(lru_tracker_it);
  }

  // Create the new tracker.
  auto tracker =
      buffer_tracker_factory_->CreateTrackerForExternalGpuMemoryBuffer(
          std::move(buffer.handle));
#if BUILDFLAG(IS_WIN)
  // Windows needs to create buffer from external handle, but mac doesn't.
  if (!tracker ||
      !tracker->Init(dimensions, buffer.format.pixel_format, nullptr)) {
    DLOG(ERROR) << "Error initializing VideoCaptureBufferTracker";
    return VideoCaptureDevice::Client::ReserveResult::kAllocationFailed;
  }
  tracker->UpdateExternalData(std::move(buffer));
#endif
  tracker->SetHeldByProducer(true);
  const int new_buffer_id = next_buffer_id_++;
  trackers_[new_buffer_id] = std::move(tracker);
  *buffer_id = new_buffer_id;
  return VideoCaptureDevice::Client::ReserveResult::kSucceeded;
}

void VideoCaptureBufferPoolImpl::HoldForConsumers(int buffer_id,
                                                  int num_clients) {
  base::AutoLock lock(lock_);
  VideoCaptureBufferTracker* tracker = GetTracker(buffer_id);
  if (!tracker) {
    NOTREACHED() << "Invalid buffer_id.";
  }
  tracker->AddConsumerHolds(num_clients);
  // Note: The buffer will stay held by the producer until
  // RelinquishProducerReservation() (usually called by destructor of the object
  // wrapping this tracker, e.g. a VideoFrame).
}

void VideoCaptureBufferPoolImpl::RelinquishConsumerHold(int buffer_id,
                                                        int num_clients) {
  base::AutoLock lock(lock_);
  VideoCaptureBufferTracker* tracker = GetTracker(buffer_id);
  if (!tracker) {
    NOTREACHED() << "Invalid buffer_id.";
  }
  tracker->RemoveConsumerHolds(num_clients);
}

double VideoCaptureBufferPoolImpl::GetBufferPoolUtilization() const {
  base::AutoLock lock(lock_);
  int num_buffers_held = 0;
  for (const auto& entry : trackers_) {
    VideoCaptureBufferTracker* const tracker = entry.second.get();
    if (tracker->IsHeldByProducerOrConsumer())
      ++num_buffers_held;
  }
  return static_cast<double>(num_buffers_held) / count_;
}

VideoCaptureDevice::Client::ReserveResult
VideoCaptureBufferPoolImpl::ReserveForProducerInternal(
    const gfx::Size& dimensions,
    VideoPixelFormat pixel_format,
    const mojom::PlaneStridesPtr& strides,
    int frame_feedback_id,
    int* buffer_id,
    int* buffer_id_to_drop) {
  lock_.AssertAcquired();

  // Look for a tracker that's allocated, big enough, and not in use. Track the
  // largest one that's not big enough, in case we have to reallocate a tracker.
  *buffer_id_to_drop = kInvalidId;
  *buffer_id = kInvalidId;
  uint32_t largest_memory_size_in_bytes = 0;
  auto tracker_to_drop = trackers_.end();
  for (auto it = trackers_.begin(); it != trackers_.end(); ++it) {
    VideoCaptureBufferTracker* const tracker = it->second.get();
    if (!tracker->IsHeldByProducerOrConsumer()) {
      if (tracker->IsReusableForFormat(dimensions, pixel_format, strides)) {
        // Reuse this buffer
        tracker->SetHeldByProducer(true);
        tracker->set_frame_feedback_id(frame_feedback_id);
        *buffer_id = it->first;
        return VideoCaptureDevice::Client::ReserveResult::kSucceeded;
      }
      if (tracker->GetMemorySizeInBytes() > largest_memory_size_in_bytes) {
        largest_memory_size_in_bytes = tracker->GetMemorySizeInBytes();
        tracker_to_drop = it;
      }
    }
  }

  // Preferably grow the pool by creating a new tracker. If we're at maximum
  // size, reallocate by deleting an existing one.
  if (trackers_.size() == static_cast<size_t>(count_)) {
    if (tracker_to_drop == trackers_.end()) {
      // We're out of space, and can't find an unused tracker to reallocate.
      DLOG(ERROR) << __func__
                  << " max buffer count exceeded count_ = " << count_;
      return VideoCaptureDevice::Client::ReserveResult::kMaxBufferCountExceeded;
    }
    *buffer_id_to_drop = tracker_to_drop->first;
    trackers_.erase(tracker_to_drop);
  }

  // Create the new tracker.
  VideoCaptureBufferType buffer_type = buffer_type_;
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC)
  // If MediaFoundationD3D11VideoCapture or VideoCaptureDeviceAVFoundation fails
  // to produce NV12 as is expected on these platforms when the target buffer
  // type is `kGpuMemoryBuffer`, a shared memory buffer may be sent instead.
  if (buffer_type == VideoCaptureBufferType::kGpuMemoryBuffer &&
      pixel_format != PIXEL_FORMAT_NV12
#if BUILDFLAG(IS_MAC)
      && base::FeatureList::IsEnabled(kFallbackToSharedMemoryIfNotNv12OnMac)
#endif
  ) {
    buffer_type = VideoCaptureBufferType::kSharedMemory;
  }
#endif
  std::unique_ptr<VideoCaptureBufferTracker> tracker =
      buffer_tracker_factory_->CreateTracker(buffer_type);
  if (!tracker || !tracker->Init(dimensions, pixel_format, strides)) {
    DLOG(ERROR) << "Error initializing VideoCaptureBufferTracker";
    return VideoCaptureDevice::Client::ReserveResult::kAllocationFailed;
  }

  tracker->SetHeldByProducer(true);
  tracker->set_frame_feedback_id(frame_feedback_id);
  const int new_buffer_id = next_buffer_id_++;
  trackers_[new_buffer_id] = std::move(tracker);
  *buffer_id = new_buffer_id;
  return VideoCaptureDevice::Client::ReserveResult::kSucceeded;
}

VideoCaptureBufferTracker* VideoCaptureBufferPoolImpl::GetTracker(
    int buffer_id) {
  auto it = trackers_.find(buffer_id);
  return (it == trackers_.end()) ? nullptr : it->second.get();
}
}  // namespace media