tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

adapted_video_track_source.cc (4283B)


      1 /*
      2 *  Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
      3 *
      4 *  Use of this source code is governed by a BSD-style license
      5 *  that can be found in the LICENSE file in the root of the source
      6 *  tree. An additional intellectual property rights grant can be found
      7 *  in the file PATENTS.  All contributing project authors may
      8 *  be found in the AUTHORS file in the root of the source tree.
      9 */
     10 
     11 #include "media/base/adapted_video_track_source.h"
     12 
     13 #include <cstdint>
     14 
     15 #include "api/scoped_refptr.h"
     16 #include "api/video/i420_buffer.h"
     17 #include "api/video/video_frame.h"
     18 #include "api/video/video_frame_buffer.h"
     19 #include "api/video/video_rotation.h"
     20 #include "api/video/video_sink_interface.h"
     21 #include "api/video/video_source_interface.h"
     22 #include "api/video_track_source_constraints.h"
     23 #include "rtc_base/synchronization/mutex.h"
     24 #include "rtc_base/time_utils.h"
     25 
     26 namespace webrtc {
     27 
     28 AdaptedVideoTrackSource::AdaptedVideoTrackSource() = default;
     29 
     30 AdaptedVideoTrackSource::AdaptedVideoTrackSource(int required_alignment)
     31    : video_adapter_(required_alignment) {}
     32 
     33 AdaptedVideoTrackSource::~AdaptedVideoTrackSource() = default;
     34 
     35 bool AdaptedVideoTrackSource::GetStats(Stats* stats) {
     36  MutexLock lock(&stats_mutex_);
     37 
     38  if (!stats_) {
     39    return false;
     40  }
     41 
     42  *stats = *stats_;
     43  return true;
     44 }
     45 
     46 void AdaptedVideoTrackSource::OnFrame(const VideoFrame& frame) {
     47  scoped_refptr<VideoFrameBuffer> buffer(frame.video_frame_buffer());
     48  /* Note that this is a "best effort" approach to
     49     wants.rotation_applied; apply_rotation_ can change from false to
     50     true between the check of apply_rotation() and the call to
     51     broadcaster_.OnFrame(), in which case we generate a frame with
     52     pending rotation despite some sink with wants.rotation_applied ==
     53     true was just added. The VideoBroadcaster enforces
     54     synchronization for us in this case, by not passing the frame on
     55     to sinks which don't want it. */
     56  if (apply_rotation() && frame.rotation() != kVideoRotation_0 &&
     57      buffer->type() == VideoFrameBuffer::Type::kI420) {
     58    /* Apply pending rotation. */
     59    VideoFrame rotated_frame(frame);
     60    rotated_frame.set_video_frame_buffer(
     61        I420Buffer::Rotate(*buffer->GetI420(), frame.rotation()));
     62    rotated_frame.set_rotation(kVideoRotation_0);
     63    broadcaster_.OnFrame(rotated_frame);
     64  } else {
     65    broadcaster_.OnFrame(frame);
     66  }
     67 }
     68 
     69 void AdaptedVideoTrackSource::OnFrameDropped() {
     70  broadcaster_.OnDiscardedFrame();
     71 }
     72 
     73 void AdaptedVideoTrackSource::AddOrUpdateSink(
     74    VideoSinkInterface<VideoFrame>* sink,
     75    const VideoSinkWants& wants) {
     76  broadcaster_.AddOrUpdateSink(sink, wants);
     77  OnSinkWantsChanged(broadcaster_.wants());
     78 }
     79 
     80 void AdaptedVideoTrackSource::RemoveSink(VideoSinkInterface<VideoFrame>* sink) {
     81  broadcaster_.RemoveSink(sink);
     82  OnSinkWantsChanged(broadcaster_.wants());
     83 }
     84 
     85 bool AdaptedVideoTrackSource::apply_rotation() {
     86  return broadcaster_.wants().rotation_applied;
     87 }
     88 
     89 void AdaptedVideoTrackSource::OnSinkWantsChanged(const VideoSinkWants& wants) {
     90  video_adapter_.OnSinkWants(wants);
     91 }
     92 
     93 bool AdaptedVideoTrackSource::AdaptFrame(int width,
     94                                         int height,
     95                                         int64_t time_us,
     96                                         int* out_width,
     97                                         int* out_height,
     98                                         int* crop_width,
     99                                         int* crop_height,
    100                                         int* crop_x,
    101                                         int* crop_y) {
    102  {
    103    MutexLock lock(&stats_mutex_);
    104    stats_ = Stats{.input_width = width, .input_height = height};
    105  }
    106 
    107  if (!broadcaster_.frame_wanted()) {
    108    return false;
    109  }
    110 
    111  if (!video_adapter_.AdaptFrameResolution(
    112          width, height, time_us * kNumNanosecsPerMicrosec, crop_width,
    113          crop_height, out_width, out_height)) {
    114    broadcaster_.OnDiscardedFrame();
    115    // VideoAdapter dropped the frame.
    116    return false;
    117  }
    118 
    119  *crop_x = (width - *crop_width) / 2;
    120  *crop_y = (height - *crop_height) / 2;
    121  return true;
    122 }
    123 
    124 void AdaptedVideoTrackSource::ProcessConstraints(
    125    const VideoTrackSourceConstraints& constraints) {
    126  broadcaster_.ProcessConstraints(constraints);
    127 }
    128 
    129 }  // namespace webrtc