Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Libuvc - fixed bug on start and stop stream #2283

Merged
merged 1 commit into from
Aug 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/libuvc/libuvc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ namespace librealsense
_is_capturing = false;
_is_started = false;
}
uvc_stop_streaming(_device_handle);
}

void power_D0() {
Expand Down
1 change: 1 addition & 0 deletions src/libuvc/libuvc_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ struct uvc_stream_handle {
uint8_t *outbuf, *holdbuf;
std::mutex cb_mutex;
std::condition_variable cb_cond;
std::condition_variable cb_cancel;
std::thread cb_thread;
uint32_t last_polled_seq;
uvc_frame_callback_t *user_cb;
Expand Down
45 changes: 19 additions & 26 deletions src/libuvc/stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -699,10 +699,7 @@ uvc_error_t uvc_probe_stream_ctrl(
*/
void _uvc_swap_buffers(uvc_stream_handle_t *strmh) {
uint8_t *tmp_buf;

{
std::unique_lock<std::mutex> lock(strmh->cb_mutex);

/* swap the buffers */
tmp_buf = strmh->holdbuf;
strmh->hold_bytes = strmh->got_bytes;
Expand Down Expand Up @@ -840,9 +837,14 @@ void LIBUSB_CALL _uvc_stream_callback(struct libusb_transfer *transfer) {
switch (transfer->status) {
case LIBUSB_TRANSFER_COMPLETED:
if (transfer->num_iso_packets == 0) {
std::unique_lock<std::mutex> lock(strmh->cb_mutex);
/* This is a bulk mode transfer, so it just has one payload transfer */
_uvc_process_payload(strmh, transfer->buffer, transfer->actual_length);
} else {


}
else
{
/* This is an isochronous mode transfer, so each packet has a payload transfer */
int packet_id;

Expand Down Expand Up @@ -887,22 +889,24 @@ void LIBUSB_CALL _uvc_stream_callback(struct libusb_transfer *transfer) {
}

resubmit = 0;
strmh->cb_cancel.notify_all();
}
strmh->cb_cond.notify_all();


break;
}
case LIBUSB_TRANSFER_TIMED_OUT:
case LIBUSB_TRANSFER_STALL:
case LIBUSB_TRANSFER_OVERFLOW:
case LIBUSB_TRANSFER_ERROR:

UVC_DEBUG("retrying transfer, status = %d", transfer->status);
break;
}

if ( strmh->running && resubmit )
libusb_submit_transfer(transfer);
if (strmh && strmh->running && resubmit )
{
auto res = libusb_submit_transfer(transfer);
}
}

/** Begin streaming video from the camera into the callback function.
Expand Down Expand Up @@ -1194,7 +1198,6 @@ void *_uvc_user_caller(void *arg) {
_uvc_populate_frame(strmh);
}


strmh->user_cb(&strmh->frame, strmh->user_ptr);
} while(1);

Expand Down Expand Up @@ -1381,32 +1384,21 @@ uvc_error_t uvc_stream_stop(uvc_stream_handle_t *strmh) {
if (!strmh->running)
return UVC_ERROR_INVALID_PARAM;

strmh->running = 0;

{
std::unique_lock<std::mutex> lock(strmh->cb_mutex);

for (i = 0; i < LIBUVC_NUM_TRANSFER_BUFS; i++) {
std::unique_lock<std::mutex> lock(strmh->cb_mutex);
strmh->running = 0;
for (i = 0; i < LIBUVC_NUM_TRANSFER_BUFS; i++) {
if (strmh->transfers[i] != NULL) {
int res = libusb_cancel_transfer(strmh->transfers[i]);
if (res < 0 && res != LIBUSB_ERROR_NOT_FOUND) {
if (res < 0) {
free(strmh->transfers[i]->buffer);
libusb_free_transfer(strmh->transfers[i]);
strmh->transfers[i] = NULL;
}
else
strmh->cb_cancel.wait(lock);
}
}

/* Wait for transfers to complete/cancel */
do {
for (i = 0; i < LIBUVC_NUM_TRANSFER_BUFS; i++) {
if (strmh->transfers[i] != NULL)
break;
}
if (i == LIBUVC_NUM_TRANSFER_BUFS)
break;
strmh->cb_cond.wait(lock);
} while (1);
}
// Kick the user thread awake
strmh->cb_cond.notify_all();
Expand All @@ -1418,6 +1410,7 @@ uvc_error_t uvc_stream_stop(uvc_stream_handle_t *strmh) {
* LIBUSB_TRANSFER_CANCELLED transfer) */
strmh->cb_thread.join();
}
auto res = libusb_clear_halt(strmh->devh->usb_devh,strmh->stream_if->bEndpointAddress);

return UVC_SUCCESS;
}
Expand Down