简体   繁体   中英

How to get MJPEG Stream from IP Camera Panasonic?

I have a problem when using CURL to get MJPEG Stream from IP Camera Panasonic.

Here is my code.

int _tmain(int argc, _TCHAR* argv[])
{
    CURL *curl;
    CURLcode res;
    /* Minimalistic http request */
    char request[1000];
    strcpy(request, "GET /nphMotionJpeg?Resolution=640x480&Quality=Standard HTTP/1.1\r\n\r\n") ;

    curl_socket_t sockfd; /* socket */
    long sockextr;
    size_t iolen;

    curl = curl_easy_init();
    if(curl) 
    {
        curl_easy_setopt(curl, CURLOPT_URL, "http://192.168.1.253");
        curl_easy_setopt(curl, CURLOPT_USERPWD, "my_usr:my_pass");

        /* Do not do the transfer - only connect to host */
        curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1L);
        res = curl_easy_perform(curl);

        if(CURLE_OK != res)
        {
            printf("Error: %s\n", strerror(res));
            return 1;
        }

        res = curl_easy_getinfo(curl, CURLINFO_LASTSOCKET, &sockextr);

        if(CURLE_OK != res)
        {
            printf("Error: %s\n", curl_easy_strerror(res));
            return 1;
        }

        sockfd = sockextr;

        /* wait for the socket to become ready for sending */
        if(wait_on_socket(sockfd, false, 6000L) < 0)
        {
            printf("Error: timeout.\n");
            return 1;
        }

        iolen = 0;
        res = curl_easy_send(curl, request, strlen(request), &iolen);

        if(CURLE_OK != res)
        {
            printf("Error: %s\n", curl_easy_strerror(res));
            return 1;
        }
        //puts("Reading response.");

        /* read the response */
        for(;;)
        {
            char* buf = new char[1024*100];
            iolen = 0;
            wait_on_socket(sockfd, true, 60000L);
            res = curl_easy_recv(curl, buf, 1024*100, &iolen);
            if(CURLE_OK != res)
                break;

            fstream f;
            f.open("out.txt", ios::out|ios::binary|ios::app);
            if(!f.fail())
            {
                f.write(buf,iolen);
                f.close();
            }
            delete []buf;
        }

        /* always cleanup */
        curl_easy_cleanup(curl);
    }
    return 0;
}

The result is content in buf after the first loop as description of Panasonic document.

But it only have 3 frames jpeg in data responding. and total size only is 3056 bytes. That mean is about 1KB/Jpeg image.It is wrong.

And in the second loop, the curl_easy_recv() always return CURLE_UNSUPPORTED_PROTOCOL.

I also change the request string same as description of Panasonic document: "GET http://xxx.xxx.xxx.xxx:yy/nphMotionJpeg?Resolution=640x480&Quality=Standard HTTP/1.0\\r\\n" OR "GET http://usr:pass@xxx.xxx.xxx.xxx:yy/nphMotionJpeg?Resolution=640x480&Quality=Standard HTTP/1.0\\r\\n" --> it will response "BAD REQUEST".

The model of my camera is Panasonic BL-C111CE.

Here's a much better example source code you could start from:

http://curl.haxx.se/libcurl/c/url2file.html

You really SHOULD avoid CURLOPT_CONNECT_ONLY, curl_easy_recv() and curl_easy_send() unless you know PERFECTLY well what you're doing and why the "normal" API isn't good enough. More often than otherwise they are the wrong answer to your problems. libcurl does HTTP perfectly fine on its own already!

Nitpick: at one point you call strerror() on a return code from libcurl, which won't show the correct error string...

I have found out the problem. The reason with command "GET /nphMotionJpeg?Resolution=640x480&Quality=Standard HTTP/1.1\\r\\n\\r\\n" is not permitted to access video. I solve it by changing the setting in my camera that can permit Guest user access to video.

Thanks all!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM