ndkcamera_recording.cpp
7.1 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Tencent is pleased to support the open source community by making ncnn available.
//
// Copyright (C) 2025 THL A29 Limited, a Tencent company. All rights reserved.
//
// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
//
// https://opensource.org/licenses/BSD-3-Clause
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
#include "ndkcamera.h"
#include <android/log.h>
#include <chrono>
#include <unistd.h>
#define LOG_TAG "NdkCameraRecording"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
bool NdkCameraWindow::startRecording(const char* filepath) {
std::lock_guard<std::mutex> lock(recording_mutex);
if (recording_active) {
LOGE("Recording already active");
return false;
}
// 初始化录制状态
video_encoder = nullptr;
audio_encoder = nullptr;
media_muxer = nullptr;
recording_active = true;
video_track_index = -1;
audio_track_index = -1;
muxer_started = false;
// 创建录制线程
recording_thread = new std::thread(&NdkCameraWindow::recording_worker, this);
LOGI("Recording started: %s", filepath);
return true;
}
bool NdkCameraWindow::stopRecording() {
{
std::lock_guard<std::mutex> lock(recording_mutex);
if (!recording_active) {
LOGE("Recording not active");
return false;
}
recording_active = false;
}
// 等待录制线程结束
if (recording_thread && recording_thread->joinable()) {
recording_thread->join();
delete recording_thread;
recording_thread = nullptr;
}
// 清理资源
if (media_muxer) {
AMediaMuxer_stop(media_muxer);
AMediaMuxer_delete(media_muxer);
media_muxer = nullptr;
}
if (video_encoder) {
AMediaCodec_stop(video_encoder);
AMediaCodec_delete(video_encoder);
video_encoder = nullptr;
}
if (audio_encoder) {
AMediaCodec_stop(audio_encoder);
AMediaCodec_delete(audio_encoder);
audio_encoder = nullptr;
}
LOGI("Recording stopped");
return true;
}
bool NdkCameraWindow::isRecording() const {
std::lock_guard<std::mutex> lock(recording_mutex);
return recording_active;
}
void NdkCameraWindow::recording_worker() {
// 这里实现录制工作线程
// 由于MediaCodec实现较复杂,这里先提供一个框架
// 实际实现需要处理视频编码、音频编码和混合器
while (recording_active) {
// 处理视频帧队列
std::vector<uint8_t> frame;
{
std::lock_guard<std::mutex> lock(recording_mutex);
if (!video_frame_queue.empty()) {
frame = video_frame_queue.front();
video_frame_queue.pop();
}
}
if (!frame.empty()) {
// 处理视频帧编码
// 这里需要实现MediaCodec编码逻辑
}
// 短暂休眠避免过度占用CPU
usleep(1000); // 1ms
}
}
bool NdkCameraWindow::setup_video_encoder(int width, int height, int fps) {
// 创建视频编码器
const char* mime_type = "video/avc";
video_encoder = AMediaCodec_createEncoderByType(mime_type);
if (!video_encoder) {
LOGE("Failed to create video encoder");
return false;
}
// 配置视频格式
AMediaFormat* format = AMediaFormat_new();
AMediaFormat_setString(format, AMEDIAFORMAT_KEY_MIME, mime_type);
AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_WIDTH, width);
AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_HEIGHT, height);
AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_FRAME_RATE, fps);
AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_I_FRAME_INTERVAL, 1);
AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_BIT_RATE, 2000000);
AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_COLOR_FORMAT,
AMEDIACODEC_COLOR_FormatYUV420Flexible);
// 配置编码器
media_status_t status = AMediaCodec_configure(video_encoder, format,
nullptr, nullptr,
AMEDIACODEC_CONFIGURE_FLAG_ENCODE);
AMediaFormat_delete(format);
if (status != AMEDIA_OK) {
LOGE("Failed to configure video encoder: %d", status);
AMediaCodec_delete(video_encoder);
video_encoder = nullptr;
return false;
}
// 启动编码器
status = AMediaCodec_start(video_encoder);
if (status != AMEDIA_OK) {
LOGE("Failed to start video encoder: %d", status);
AMediaCodec_delete(video_encoder);
video_encoder = nullptr;
return false;
}
LOGI("Video encoder setup: %dx%d %dfps", width, height, fps);
return true;
}
bool NdkCameraWindow::setup_audio_encoder() {
// 创建音频编码器
const char* mime_type = "audio/mp4a-latm";
audio_encoder = AMediaCodec_createEncoderByType(mime_type);
if (!audio_encoder) {
LOGE("Failed to create audio encoder");
return false;
}
// 配置音频格式
AMediaFormat* format = AMediaFormat_new();
AMediaFormat_setString(format, AMEDIAFORMAT_KEY_MIME, mime_type);
AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_SAMPLE_RATE, 44100);
AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_CHANNEL_COUNT, 1);
AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_BIT_RATE, 128000);
AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_AAC_PROFILE, 2); // AAC LC
// 配置编码器
media_status_t status = AMediaCodec_configure(audio_encoder, format,
nullptr, nullptr,
AMEDIACODEC_CONFIGURE_FLAG_ENCODE);
AMediaFormat_delete(format);
if (status != AMEDIA_OK) {
LOGE("Failed to configure audio encoder: %d", status);
AMediaCodec_delete(audio_encoder);
audio_encoder = nullptr;
return false;
}
// 启动编码器
status = AMediaCodec_start(audio_encoder);
if (status != AMEDIA_OK) {
LOGE("Failed to start audio encoder: %d", status);
AMediaCodec_delete(audio_encoder);
audio_encoder = nullptr;
return false;
}
LOGI("Audio encoder setup");
return true;
}
void NdkCameraWindow::encode_video_frame(const unsigned char* nv21_data, int width, int height, int64_t timestamp_us) {
if (!video_encoder || !recording_active) {
return;
}
// 将视频帧添加到队列供录制线程处理
std::vector<uint8_t> frame_data(nv21_data, nv21_data + width * height * 3 / 2);
std::lock_guard<std::mutex> lock(recording_mutex);
video_frame_queue.push(std::move(frame_data));
}