16
16
#define VIDEO_ENCODE_BITRATE 40000000 /* bps */
17
17
#define AUDIO_SOURCE_SAMPLERATE_AAC 44100
18
18
19
- static uint64_t Timestamp () {
19
+ namespace {
20
+
21
+ uint64_t Timestamp () {
20
22
struct timeval tv;
21
23
gettimeofday (&tv, nullptr );
22
24
return (uint64_t )tv.tv_sec * 1000UL + tv.tv_usec / 1000UL ;
23
25
}
24
26
25
- static std::string CreateTempFileName (const std::string &prefix,
26
- const std::string &extension) {
27
+ std::string CreateTempFileName (const std::string &prefix,
28
+ const std::string &extension) {
27
29
std::string file_name;
28
30
char *cache_dir_path = app_get_cache_path ();
29
31
if (!cache_dir_path) {
@@ -38,8 +40,17 @@ static std::string CreateTempFileName(const std::string &prefix,
38
40
return file_name;
39
41
}
40
42
41
- static ExifTagOrientation ChooseExifTagOrientatoin (
42
- OrientationType device_orientation, bool is_front_lens_facing) {
43
+ bool IsValidMediaPacket (media_packet_h media_packet) {
44
+ tbm_surface_h surface = nullptr ;
45
+ int ret = media_packet_get_tbm_surface (media_packet, &surface);
46
+ if (ret != MEDIA_PACKET_ERROR_NONE) {
47
+ return false ;
48
+ }
49
+ return true ;
50
+ }
51
+
52
+ ExifTagOrientation ChooseExifTagOrientatoin (OrientationType device_orientation,
53
+ bool is_front_lens_facing) {
43
54
ExifTagOrientation orientation = ExifTagOrientation::kTopLeft ;
44
55
45
56
switch (device_orientation) {
@@ -79,7 +90,7 @@ static ExifTagOrientation ChooseExifTagOrientatoin(
79
90
return orientation;
80
91
}
81
92
82
- static RecorderOrientationTag ChooseRecorderOrientationTag (
93
+ RecorderOrientationTag ChooseRecorderOrientationTag (
83
94
OrientationType device_orientation) {
84
95
RecorderOrientationTag tag = RecorderOrientationTag::kNone ;
85
96
switch (device_orientation) {
@@ -102,6 +113,8 @@ static RecorderOrientationTag ChooseRecorderOrientationTag(
102
113
return tag;
103
114
}
104
115
116
+ } // namespace
117
+
105
118
bool StringToCameraPixelFormat (std::string Image_format,
106
119
CameraPixelFormat &pixel_format) {
107
120
if (Image_format == " bgra8888" ) {
@@ -330,28 +343,40 @@ CameraDevice::CameraDevice(flutter::PluginRegistrar *registrar,
330
343
[this ](size_t width,
331
344
size_t height) -> const FlutterDesktopGpuBuffer * {
332
345
std::lock_guard<std::mutex> lock (mutex_);
333
- if (packet_ == nullptr ) {
346
+ if (prepared_packet_ && !IsValidMediaPacket (prepared_packet_)) {
347
+ media_packet_destroy (prepared_packet_);
348
+ prepared_packet_ = nullptr ;
349
+ }
350
+ if (current_packet_ && !IsValidMediaPacket (current_packet_)) {
351
+ media_packet_destroy (current_packet_);
352
+ current_packet_ = nullptr ;
353
+ }
354
+ if (!prepared_packet_ && !current_packet_) {
334
355
return nullptr ;
335
356
}
336
- tbm_surface_h surface;
337
- int ret = media_packet_get_tbm_surface (packet_, &surface);
357
+ if (prepared_packet_ && !current_packet_) {
358
+ current_packet_ = prepared_packet_;
359
+ prepared_packet_ = nullptr ;
360
+ }
361
+
362
+ tbm_surface_h surface = nullptr ;
363
+ int ret = media_packet_get_tbm_surface (current_packet_, &surface);
338
364
if (ret != MEDIA_PACKET_ERROR_NONE) {
339
365
LOG_ERROR (" media_packet_get_tbm_surface failed, error: %d" , ret);
340
- media_packet_destroy (packet_ );
341
- packet_ = nullptr ;
366
+ media_packet_destroy (current_packet_ );
367
+ current_packet_ = nullptr ;
342
368
return nullptr ;
343
369
}
344
-
345
370
flutter_desktop_gpu_buffer_->buffer = surface;
346
371
flutter_desktop_gpu_buffer_->width = width;
347
372
flutter_desktop_gpu_buffer_->height = height;
348
373
return flutter_desktop_gpu_buffer_.get ();
349
374
},
350
375
[this ](void *buffer) -> void {
351
376
std::lock_guard<std::mutex> lock (mutex_);
352
- if (packet_ ) {
353
- media_packet_destroy (packet_ );
354
- packet_ = nullptr ;
377
+ if (current_packet_ ) {
378
+ media_packet_destroy (current_packet_ );
379
+ current_packet_ = nullptr ;
355
380
}
356
381
}));
357
382
texture_id_ =
@@ -431,6 +456,16 @@ void CameraDevice::Dispose() {
431
456
if (texture_id_ != 0 ) {
432
457
registrar_->texture_registrar ()->UnregisterTexture (texture_id_);
433
458
}
459
+
460
+ if (current_packet_) {
461
+ media_packet_destroy (current_packet_);
462
+ current_packet_ = nullptr ;
463
+ }
464
+
465
+ if (prepared_packet_) {
466
+ media_packet_destroy (prepared_packet_);
467
+ prepared_packet_ = nullptr ;
468
+ }
434
469
}
435
470
436
471
bool CameraDevice::ForeachCameraSupportedCaptureResolutions (
@@ -950,17 +985,17 @@ void CameraDevice::Open(
950
985
SetCameraPreviewFormat (pixel_format);
951
986
}
952
987
953
- if (!SetCameraMediaPacketPreviewCb ([](media_packet_h pkt , void *data) {
988
+ if (!SetCameraMediaPacketPreviewCb ([](media_packet_h packet , void *data) {
954
989
auto self = static_cast <CameraDevice *>(data);
955
990
std::lock_guard<std::mutex> lock (self->mutex_ );
956
- media_packet_h unused_packet = self->packet_ ;
957
- self->packet_ = pkt;
958
- if (unused_packet != nullptr ) {
959
- media_packet_destroy (unused_packet);
960
- } else {
961
- self->registrar_ ->texture_registrar ()->MarkTextureFrameAvailable (
962
- self->texture_id_ );
991
+ if (self->prepared_packet_ ) {
992
+ media_packet_destroy (self->prepared_packet_ );
993
+ self->prepared_packet_ = packet;
994
+ return ;
963
995
}
996
+ self->prepared_packet_ = packet;
997
+ self->registrar_ ->texture_registrar ()->MarkTextureFrameAvailable (
998
+ self->texture_id_ );
964
999
})) {
965
1000
result->Error (kCameraDeviceError , " Failed to set media callback" );
966
1001
return ;
0 commit comments