File: simple_webcam_client_streaming.cpp

package info (click to toggle)
robotraconteur 1.2.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 101,380 kB
  • sloc: cpp: 1,149,268; cs: 87,653; java: 58,127; python: 26,897; ansic: 356; sh: 152; makefile: 90; xml: 51
file content (103 lines) | stat: -rw-r--r-- 2,942 bytes parent folder | download
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

#include <RobotRaconteur.h>
#include "robotraconteur_generated.h"

#include <opencv2/highgui/highgui.hpp>

namespace RR = RobotRaconteur;
namespace cam = ::experimental::simplewebcam3;

// Simple client to read streaming images from the Webcam pipe to show
// a live view from the cameras

// This example expects the simple_webcam_service single camera service to be running

// Convert WebcamImage to OpenCV format
cv::Mat webcam_image_to_mat(cam::WebcamImagePtr image)
{
    cv::Mat frame2(image->height, image->width, CV_8UC3);
    memcpy(frame2.data, &image->data->at(0), image->data->size());
    return frame2;
}

cv::Mat current_frame;

// Function to handle when a new frame is received
// This function will be called by a separate thread by
// Robot Raconteur.
void new_frame(RR::PipeEndpointPtr<cam::WebcamImagePtr> pipe_ep)
{
    while (pipe_ep->Available() > 0)
    {
        cam::WebcamImagePtr image = pipe_ep->ReceivePacket();
        if (image->data)
        {
            current_frame = webcam_image_to_mat(image);
        }
    }
}

int main(int argc, char* argv[])
{
    std::string url = "rr+tcp://localhost:22355?service=webcam";
    if (argc > 1)
    {
        url = argv[1];
    }

    try
    {
        // Use node setup to help initialize client node
        RR::ClientNodeSetup node_setup(ROBOTRACONTEUR_SERVICE_TYPES);

        // Connect to the service
        cam::WebcamPtr c1 = RR::rr_cast<cam::Webcam>(
            RR::RobotRaconteurNode::s()->ConnectService(url, "", nullptr, NULL, "experimental.simplewebcam3.Webcam"));

        // Connect to the frame_stream pipe and receive a PipeEndpoint
        // PipeEndpoints a symmetric on client and service meaning that
        // you can send and receive on both ends
        RR::PipeEndpointPtr<cam::WebcamImagePtr> p = c1->get_frame_stream()->Connect(-1);
        // Add a callback for when a new pipe packet is received
        p->PacketReceivedEvent.connect([](RR::PipeEndpointPtr<cam::WebcamImagePtr> ep) { new_frame(ep); });

        // Show a named window
        cv::namedWindow("Image");

        try
        {
            // Start streaming image packets
            c1->start_streaming();
        }
        catch (std::exception&)
        {}

        // Loop through and show the new image if available
        while (true)
        {
            if (!current_frame.empty())
            {
                cv::imshow("Image", current_frame);
            }
            // Break the loop if "enter" is pressed on a window
            if (cv::waitKey(50) != -1)
                break;
        }

        // Stop streaming images
        c1->stop_streaming();

        // Close the window
        cv::destroyAllWindows();

        // Close the PipeEndpoint
        p->Close();

        return 0;
    }
    catch (std::exception& e)
    {
        std::cout << "Error occurred in client: " << std::string(e.what()) << std::endl;
        return -1;
    }
}