-
Notifications
You must be signed in to change notification settings - Fork 260
/
Copy pathnull_encoder.cpp
66 lines (60 loc) · 1.67 KB
/
null_encoder.cpp
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
/* SPDX-License-Identifier: BSD-2-Clause */
/*
* Copyright (C) 2020, Raspberry Pi (Trading) Ltd.
*
* null_encoder.cpp - dummy "do nothing" video encoder.
*/
#include <chrono>
#include <iostream>
#include <stdexcept>
#include "null_encoder.hpp"
NullEncoder::NullEncoder(VideoOptions const *options) : Encoder(options), abort_(false)
{
LOG(2, "Opened NullEncoder");
output_thread_ = std::thread(&NullEncoder::outputThread, this);
}
NullEncoder::~NullEncoder()
{
abort_ = true;
output_thread_.join();
LOG(2, "NullEncoder closed");
}
// Push the buffer onto the output queue to be "encoded" and returned.
void NullEncoder::EncodeBuffer(int fd, size_t size, void *mem, StreamInfo const &info, int64_t timestamp_us)
{
std::lock_guard<std::mutex> lock(output_mutex_);
OutputItem item = { mem, size, timestamp_us };
output_queue_.push(item);
output_cond_var_.notify_one();
}
// Realistically we would probably want more of a queue as the caller's number
// of buffers limits the amount of queueing possible here...
void NullEncoder::outputThread()
{
OutputItem item;
while (true)
{
{
std::unique_lock<std::mutex> lock(output_mutex_);
while (true)
{
using namespace std::chrono_literals;
if (!output_queue_.empty())
{
item = output_queue_.front();
output_queue_.pop();
break;
}
else
output_cond_var_.wait_for(lock, 200ms);
if (abort_)
return;
}
}
// Ensure the input done callback happens before the output ready callback.
// This is needed as the metadata queue gets pushed in the former, and popped
// in the latter.
input_done_callback_(nullptr);
output_ready_callback_(item.mem, item.length, item.timestamp_us, true);
}
}