Skip to content

Commit b60f782

Browse files
authored
[camera] Fix preview freeze issue (#275)
* [camera] Fix preview freeze issue Signed-off-by: Boram Bae <[email protected]> * [camera] Update version Signed-off-by: Boram Bae <[email protected]> * [camera] Update based on review Signed-off-by: Boram Bae <[email protected]>
1 parent fd8b7d5 commit b60f782

File tree

5 files changed

+68
-27
lines changed

5 files changed

+68
-27
lines changed

packages/camera/CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@
44

55
## 0.2.0
66

7-
* Apply new external texture APIs
7+
* Apply new external texture APIs
8+
9+
## 0.2.1
10+
11+
* Fix a freezing preview issue

packages/camera/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ This package is not an _endorsed_ implementation of `camera`. Therefore, you hav
2626
```yaml
2727
dependencies:
2828
camera: ^0.8.1
29-
camera_tizen: ^0.2.0
29+
camera_tizen: ^0.2.1
3030
```
3131
3232
Then you can import `camera` in your Dart code:

packages/camera/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: camera_tizen
22
description: Tizen implementation of the camera plugin
33
homepage: https://github.com/flutter-tizen/plugins
44
repository: https://github.com/flutter-tizen/plugins/tree/master/packages/camera
5-
version: 0.2.0
5+
version: 0.2.1
66

77
dependencies:
88
flutter:

packages/camera/tizen/src/camera_device.cc

Lines changed: 58 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@
1616
#define VIDEO_ENCODE_BITRATE 40000000 /* bps */
1717
#define AUDIO_SOURCE_SAMPLERATE_AAC 44100
1818

19-
static uint64_t Timestamp() {
19+
namespace {
20+
21+
uint64_t Timestamp() {
2022
struct timeval tv;
2123
gettimeofday(&tv, nullptr);
2224
return (uint64_t)tv.tv_sec * 1000UL + tv.tv_usec / 1000UL;
2325
}
2426

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) {
2729
std::string file_name;
2830
char *cache_dir_path = app_get_cache_path();
2931
if (!cache_dir_path) {
@@ -38,8 +40,17 @@ static std::string CreateTempFileName(const std::string &prefix,
3840
return file_name;
3941
}
4042

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) {
4354
ExifTagOrientation orientation = ExifTagOrientation::kTopLeft;
4455

4556
switch (device_orientation) {
@@ -79,7 +90,7 @@ static ExifTagOrientation ChooseExifTagOrientatoin(
7990
return orientation;
8091
}
8192

82-
static RecorderOrientationTag ChooseRecorderOrientationTag(
93+
RecorderOrientationTag ChooseRecorderOrientationTag(
8394
OrientationType device_orientation) {
8495
RecorderOrientationTag tag = RecorderOrientationTag::kNone;
8596
switch (device_orientation) {
@@ -102,6 +113,8 @@ static RecorderOrientationTag ChooseRecorderOrientationTag(
102113
return tag;
103114
}
104115

116+
} // namespace
117+
105118
bool StringToCameraPixelFormat(std::string Image_format,
106119
CameraPixelFormat &pixel_format) {
107120
if (Image_format == "bgra8888") {
@@ -330,28 +343,40 @@ CameraDevice::CameraDevice(flutter::PluginRegistrar *registrar,
330343
[this](size_t width,
331344
size_t height) -> const FlutterDesktopGpuBuffer * {
332345
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_) {
334355
return nullptr;
335356
}
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);
338364
if (ret != MEDIA_PACKET_ERROR_NONE) {
339365
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;
342368
return nullptr;
343369
}
344-
345370
flutter_desktop_gpu_buffer_->buffer = surface;
346371
flutter_desktop_gpu_buffer_->width = width;
347372
flutter_desktop_gpu_buffer_->height = height;
348373
return flutter_desktop_gpu_buffer_.get();
349374
},
350375
[this](void *buffer) -> void {
351376
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;
355380
}
356381
}));
357382
texture_id_ =
@@ -431,6 +456,16 @@ void CameraDevice::Dispose() {
431456
if (texture_id_ != 0) {
432457
registrar_->texture_registrar()->UnregisterTexture(texture_id_);
433458
}
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+
}
434469
}
435470

436471
bool CameraDevice::ForeachCameraSupportedCaptureResolutions(
@@ -950,17 +985,17 @@ void CameraDevice::Open(
950985
SetCameraPreviewFormat(pixel_format);
951986
}
952987

953-
if (!SetCameraMediaPacketPreviewCb([](media_packet_h pkt, void *data) {
988+
if (!SetCameraMediaPacketPreviewCb([](media_packet_h packet, void *data) {
954989
auto self = static_cast<CameraDevice *>(data);
955990
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;
963995
}
996+
self->prepared_packet_ = packet;
997+
self->registrar_->texture_registrar()->MarkTextureFrameAvailable(
998+
self->texture_id_);
964999
})) {
9651000
result->Error(kCameraDeviceError, "Failed to set media callback");
9661001
return;

packages/camera/tizen/src/camera_device.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,9 @@ class CameraDevice {
325325
flutter::PluginRegistrar *registrar_{nullptr};
326326
std::unique_ptr<flutter::TextureVariant> texture_variant_;
327327
std::unique_ptr<FlutterDesktopGpuBuffer> flutter_desktop_gpu_buffer_;
328-
media_packet_h packet_{nullptr};
328+
media_packet_h current_packet_{nullptr};
329+
media_packet_h prepared_packet_{nullptr};
330+
329331
std::mutex mutex_;
330332

331333
std::unique_ptr<CameraMethodChannel> camera_method_channel_;

0 commit comments

Comments
 (0)