File: client_video_dispatcher.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (114 lines) | stat: -rw-r--r-- 3,812 bytes parent folder | download | duplicates (7)
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
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "remoting/protocol/client_video_dispatcher.h"

#include <utility>

#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "net/socket/stream_socket.h"
#include "remoting/base/compound_buffer.h"
#include "remoting/base/constants.h"
#include "remoting/proto/control.pb.h"
#include "remoting/proto/video.pb.h"
#include "remoting/protocol/client_stub.h"
#include "remoting/protocol/message_pipe.h"
#include "remoting/protocol/message_serialization.h"
#include "remoting/protocol/video_stub.h"

namespace remoting::protocol {

struct ClientVideoDispatcher::PendingFrame {
  explicit PendingFrame(int frame_id) : frame_id(frame_id), done(false) {}
  int frame_id;
  bool done;
};

ClientVideoDispatcher::ClientVideoDispatcher(VideoStub* video_stub,
                                             ClientStub* client_stub)
    : ChannelDispatcherBase(kVideoChannelName),
      video_stub_(video_stub),
      client_stub_(client_stub) {}

ClientVideoDispatcher::~ClientVideoDispatcher() = default;

void ClientVideoDispatcher::OnIncomingMessage(
    std::unique_ptr<CompoundBuffer> message) {
  std::unique_ptr<VideoPacket> video_packet =
      ParseMessage<VideoPacket>(message.get());
  if (!video_packet) {
    return;
  }

  int frame_id = video_packet->frame_id();

  if (!video_packet->has_frame_id()) {
    video_stub_->ProcessVideoPacket(std::move(video_packet), base::DoNothing());
    return;
  }

  bool resolution_changed = false;

  if (video_packet->format().has_screen_width() &&
      video_packet->format().has_screen_height()) {
    webrtc::DesktopSize frame_size(video_packet->format().screen_width(),
                                   video_packet->format().screen_height());
    if (!screen_size_.equals(frame_size)) {
      screen_size_ = frame_size;
      resolution_changed = true;
    }
  }

  if (video_packet->format().has_x_dpi() &&
      video_packet->format().has_y_dpi()) {
    webrtc::DesktopVector screen_dpi(video_packet->format().x_dpi(),
                                     video_packet->format().y_dpi());
    if (!screen_dpi_.equals(screen_dpi)) {
      screen_dpi_ = screen_dpi;
      resolution_changed = true;
    }
  }

  // Simulate DesktopLayout message whenever screen size/resolution changes.
  if (resolution_changed) {
    VideoLayout layout;
    VideoTrackLayout* video_track = layout.add_video_track();
    video_track->set_position_x(0);
    video_track->set_position_y(0);
    video_track->set_width(screen_size_.width() * kDefaultDpi /
                           screen_dpi_.x());
    video_track->set_height(screen_size_.height() * kDefaultDpi /
                            screen_dpi_.y());
    video_track->set_x_dpi(screen_dpi_.x());
    video_track->set_y_dpi(screen_dpi_.y());
    client_stub_->SetVideoLayout(layout);
  }

  auto pending_frame =
      pending_frames_.insert(pending_frames_.end(), PendingFrame(frame_id));

  video_stub_->ProcessVideoPacket(
      std::move(video_packet),
      base::BindOnce(&ClientVideoDispatcher::OnPacketDone,
                     weak_factory_.GetWeakPtr(), pending_frame));
}

void ClientVideoDispatcher::OnPacketDone(
    PendingFramesList::iterator pending_frame) {
  // Mark the frame as done.
  DCHECK(!pending_frame->done);
  pending_frame->done = true;

  // Send VideoAck for all packets in the head of the queue that have finished
  // rendering.
  while (!pending_frames_.empty() && pending_frames_.front().done) {
    VideoAck ack_message;
    ack_message.set_frame_id(pending_frames_.front().frame_id);
    message_pipe()->Send(&ack_message, {});
    pending_frames_.pop_front();
  }
}

}  // namespace remoting::protocol