summaryrefslogtreecommitdiff
path: root/main.c
blob: 0c3138a7ddaef257345644cca5c0381b3a70901e (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
/*
 * 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 <limits.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

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

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

struct TimingStats {
  unsigned long long min;
  unsigned long long max;
  unsigned long long sum;
};

static void TimingStatsRecord(struct TimingStats* timing_stats,
                              unsigned long long value) {
  timing_stats->min = MIN(timing_stats->min, value);
  timing_stats->max = MAX(timing_stats->max, value);
  timing_stats->sum += value;
}

static void TimingStatsLog(const struct TimingStats* timing_stats,
                           const char* name, unsigned long long counter) {
  LOG("%s min/avg/max: %llu/%llu/%llu", name, timing_stats->min,
      timing_stats->sum / counter, timing_stats->max);
}

static unsigned long long MicrosNow(void) {
  struct timespec ts = {.tv_sec = 0, .tv_nsec = 0};
  clock_gettime(CLOCK_MONOTONIC, &ts);
  return (unsigned long long)ts.tv_sec * 1000000ull +
         (unsigned long long)ts.tv_nsec / 1000ull;
}

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

  if (signal(SIGINT, 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();
  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 = {.min = ULLONG_MAX};
  struct TimingStats convert = {.min = ULLONG_MAX};
  struct TimingStats encode = {.min = ULLONG_MAX};
  struct TimingStats total = {.min = ULLONG_MAX};
  unsigned long long num_frames = 0;

  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);
      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)) {
      LOG("Failed to encode frame");
      return EXIT_FAILURE;
    }

    unsigned long long now = MicrosNow();
    if (num_frames++) {
      // mburakov: Do not record stats for first (lazy) frame.
      TimingStatsRecord(&capture, before_convert - before_capture);
      TimingStatsRecord(&convert, before_encode - before_convert);
      TimingStatsRecord(&encode, now - before_encode);
      TimingStatsRecord(&total, now - before_capture);
    }
    unsigned long long micros = now < next ? next - now : 0;
    if (micros) usleep((unsigned)micros);
  }

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

  num_frames--;
  TimingStatsLog(&capture, "Capture", num_frames);
  TimingStatsLog(&convert, "Convert", num_frames);
  TimingStatsLog(&encode, "Encode", num_frames);
  TimingStatsLog(&total, "Total", num_frames);
  return EXIT_SUCCESS;
}