summaryrefslogtreecommitdiff
path: root/main.c
blob: 1d30c2589deafbe53b7bdc0c0f5c6f1b5ff7778b (plain)
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
/*
 * Copyright (C) 2023 Mikhail Burakov. This file is part of streamer.
 *
 * streamer is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * streamer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with streamer.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "capture.h"
#include "colorspace.h"
#include "encode.h"
#include "gpu.h"
#include "perf.h"
#include "util.h"

// TODO(mburakov): Currently zwp_linux_dmabuf_v1 has no way to provide
// colorspace and range information to the compositor. Maybe this would change
// in the future, i.e keep an eye on color-representation Wayland protocol:
// https://gitlab.freedesktop.org/wayland/wayland-protocols/-/merge_requests/183
static const enum YuvColorspace colorspace = kItuRec601;
static const enum YuvRange range = kNarrowRange;

static volatile sig_atomic_t g_signal;
static void OnSignal(int status) { g_signal = status; }

int main(int argc, char* argv[]) {
  (void)argc;
  (void)argv;

  if (signal(SIGINT, OnSignal) == SIG_ERR ||
      signal(SIGPIPE, OnSignal) == SIG_ERR ||
      signal(SIGTERM, OnSignal) == SIG_ERR) {
    LOG("Failed to set signal handlers (%s)", strerror(errno));
    return EXIT_FAILURE;
  }

  struct AUTO(GpuContext)* gpu_context = GpuContextCreate(colorspace, range);
  if (!gpu_context) {
    LOG("Failed to create gpu context");
    return EXIT_FAILURE;
  }

  struct AUTO(CaptureContext)* capture_context =
      CaptureContextCreate(gpu_context);
  if (!capture_context) {
    LOG("Failed to create capture context");
    return EXIT_FAILURE;
  }

  struct AUTO(EncodeContext)* encode_context = NULL;

  struct TimingStats capture;
  struct TimingStats convert;
  struct TimingStats encode;
  struct TimingStats drain;
  struct TimingStats total;
  TimingStatsReset(&capture);
  TimingStatsReset(&convert);
  TimingStatsReset(&encode);
  TimingStatsReset(&drain);
  TimingStatsReset(&total);
  unsigned long long num_frames = 0;

  unsigned long long recording_started = MicrosNow();
  static const unsigned long long delta = 1000000ull / 60ull;
  for (unsigned long long next = MicrosNow() + delta; !g_signal;
       next += delta) {
    unsigned long long before_capture = MicrosNow();
    const struct GpuFrame* captured_frame =
        CaptureContextGetFrame(capture_context);
    if (!captured_frame) {
      LOG("Failed to capture frame");
      return EXIT_FAILURE;
    }

    if (!encode_context) {
      uint32_t width, height;
      GpuFrameGetSize(captured_frame, &width, &height);
      encode_context =
          EncodeContextCreate(gpu_context, width, height, colorspace, range);
      if (!encode_context) {
        LOG("Failed to create encode context");
        return EXIT_FAILURE;
      }
    }

    const struct GpuFrame* encoded_frame =
        EncodeContextGetFrame(encode_context);
    if (!encoded_frame) {
      LOG("Failed to get encoded frame");
      return EXIT_FAILURE;
    }

    unsigned long long before_convert = MicrosNow();
    if (!GpuFrameConvert(captured_frame, encoded_frame)) {
      LOG("Failed to convert frame");
      return EXIT_FAILURE;
    }

    GpuContextSync(gpu_context);
    unsigned long long before_encode = MicrosNow();
    if (!EncodeContextEncodeFrame(encode_context, STDOUT_FILENO, &encode,
                                  &drain)) {
      LOG("Failed to encode frame");
      return EXIT_FAILURE;
    }

    unsigned long long now = MicrosNow();
    TimingStatsRecord(&capture, before_convert - before_capture);
    TimingStatsRecord(&convert, before_encode - before_convert);
    TimingStatsRecord(&total, now - before_capture);

    unsigned long long period = now - recording_started;
    static const unsigned long long second = 1000000;
    if (period > 10 * second) {
      LOG("---->8-------->8-------->8----");
      TimingStatsLog(&capture, "Capture", num_frames);
      TimingStatsLog(&convert, "Convert", num_frames);
      TimingStatsLog(&encode, "Encode", num_frames);
      TimingStatsLog(&drain, "Drain", num_frames);
      TimingStatsLog(&total, "Total", num_frames);
      TimingStatsReset(&capture);
      TimingStatsReset(&convert);
      TimingStatsReset(&encode);
      TimingStatsReset(&drain);
      TimingStatsReset(&total);
      recording_started = now;
      num_frames = 0;
    }

    now = MicrosNow();
    unsigned long long micros = now < next ? next - now : 0;
    if (micros) usleep((unsigned)micros);
    num_frames++;
  }

  if (!EncodeContextEncodeFrame(encode_context, STDOUT_FILENO, NULL, NULL)) {
    LOG("Failed to drain encoder");
    return EXIT_FAILURE;
  }
  return EXIT_SUCCESS;
}