2003-03-26 19:57:29 +08:00
|
|
|
//
|
|
|
|
// ZoneMinder Remote Camera Class Implementation, $Date$, $Revision$
|
2008-07-25 17:33:23 +08:00
|
|
|
// Copyright (C) 2001-2008 Philip Coombes
|
2003-03-26 19:57:29 +08:00
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License
|
|
|
|
// as published by the Free Software Foundation; either version 2
|
|
|
|
// of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
2016-12-26 23:23:16 +08:00
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2003-03-26 19:57:29 +08:00
|
|
|
//
|
|
|
|
|
|
|
|
#include "zm_remote_camera.h"
|
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
#include "zm_utils.h"
|
2021-02-04 11:47:28 +08:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netdb.h>
|
2021-02-22 21:03:10 +08:00
|
|
|
#include <sys/socket.h>
|
2004-02-16 03:17:38 +08:00
|
|
|
|
2016-04-01 00:54:56 +08:00
|
|
|
RemoteCamera::RemoteCamera(
|
2021-02-08 02:12:39 +08:00
|
|
|
const Monitor *monitor,
|
2016-04-30 20:27:10 +08:00
|
|
|
const std::string &p_protocol,
|
|
|
|
const std::string &p_host,
|
|
|
|
const std::string &p_port,
|
|
|
|
const std::string &p_path,
|
|
|
|
int p_width,
|
|
|
|
int p_height,
|
|
|
|
int p_colours,
|
|
|
|
int p_brightness,
|
|
|
|
int p_contrast,
|
|
|
|
int p_hue,
|
|
|
|
int p_colour,
|
|
|
|
bool p_capture,
|
|
|
|
bool p_record_audio
|
2016-04-01 00:54:56 +08:00
|
|
|
) :
|
2021-02-08 02:12:39 +08:00
|
|
|
Camera( monitor, REMOTE_SRC, p_width, p_height, p_colours, ZM_SUBPIX_ORDER_DEFAULT_FOR_COLOUR(p_colours), p_brightness, p_contrast, p_hue, p_colour, p_capture, p_record_audio ),
|
2008-07-16 16:35:59 +08:00
|
|
|
protocol( p_protocol ),
|
|
|
|
host( p_host ),
|
|
|
|
port( p_port ),
|
|
|
|
path( p_path ),
|
2017-11-17 20:52:26 +08:00
|
|
|
hp( 0 ),
|
|
|
|
mNeedAuth(false),
|
2020-08-26 07:45:48 +08:00
|
|
|
mAuthenticator(nullptr)
|
2008-07-16 16:35:59 +08:00
|
|
|
{
|
2009-05-28 16:47:59 +08:00
|
|
|
if ( path[0] != '/' )
|
|
|
|
path = '/'+path;
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
|
|
|
|
2017-05-20 21:06:12 +08:00
|
|
|
RemoteCamera::~RemoteCamera() {
|
2020-08-26 07:45:48 +08:00
|
|
|
if ( hp != nullptr ) {
|
2018-11-02 22:26:39 +08:00
|
|
|
freeaddrinfo(hp);
|
2020-08-26 07:45:48 +08:00
|
|
|
hp = nullptr;
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
2018-05-26 02:20:54 +08:00
|
|
|
if ( mAuthenticator ) {
|
|
|
|
delete mAuthenticator;
|
2020-08-26 07:45:48 +08:00
|
|
|
mAuthenticator = nullptr;
|
2018-05-26 02:20:54 +08:00
|
|
|
}
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
|
|
|
|
2017-05-20 21:06:12 +08:00
|
|
|
void RemoteCamera::Initialise() {
|
2016-04-04 22:11:48 +08:00
|
|
|
if( protocol.empty() )
|
|
|
|
Fatal( "No protocol specified for remote camera" );
|
2004-02-16 03:17:38 +08:00
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
if( host.empty() )
|
|
|
|
Fatal( "No host specified for remote camera" );
|
2004-02-16 03:17:38 +08:00
|
|
|
|
2018-11-02 22:26:39 +08:00
|
|
|
if ( port.empty() )
|
|
|
|
Fatal("No port specified for remote camera");
|
2008-07-16 16:35:59 +08:00
|
|
|
|
|
|
|
//if( path.empty() )
|
|
|
|
//Fatal( "No path specified for remote camera" );
|
2004-02-16 03:17:38 +08:00
|
|
|
|
2003-03-26 19:57:29 +08:00
|
|
|
// Cache as much as we can to speed things up
|
2016-04-04 22:11:48 +08:00
|
|
|
std::string::size_type authIndex = host.rfind( '@' );
|
2004-02-16 03:17:38 +08:00
|
|
|
|
2017-05-20 21:06:12 +08:00
|
|
|
if ( authIndex != std::string::npos ) {
|
2016-04-04 22:11:48 +08:00
|
|
|
auth = host.substr( 0, authIndex );
|
|
|
|
host.erase( 0, authIndex+1 );
|
2021-04-04 06:39:40 +08:00
|
|
|
auth64 = Base64Encode(auth);
|
2014-11-20 23:44:31 +08:00
|
|
|
|
2016-04-04 22:11:48 +08:00
|
|
|
authIndex = auth.rfind( ':' );
|
|
|
|
username = auth.substr(0,authIndex);
|
|
|
|
password = auth.substr( authIndex+1, auth.length() );
|
|
|
|
}
|
2014-11-20 23:44:31 +08:00
|
|
|
|
2017-05-24 03:17:46 +08:00
|
|
|
mNeedAuth = false;
|
2015-12-17 22:47:10 +08:00
|
|
|
mAuthenticator = new zm::Authenticator(username,password);
|
2014-11-20 05:40:05 +08:00
|
|
|
|
2013-11-22 21:30:05 +08:00
|
|
|
struct addrinfo hints;
|
|
|
|
memset(&hints, 0, sizeof(hints));
|
|
|
|
hints.ai_family = AF_UNSPEC;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
2013-10-11 14:26:48 +08:00
|
|
|
|
2016-04-04 22:11:48 +08:00
|
|
|
int ret = getaddrinfo(host.c_str(), port.c_str(), &hints, &hp);
|
2017-05-20 21:06:12 +08:00
|
|
|
if ( ret != 0 ) {
|
2016-04-04 22:11:48 +08:00
|
|
|
Fatal( "Can't getaddrinfo(%s port %s): %s", host.c_str(), port.c_str(), gai_strerror(ret) );
|
|
|
|
}
|
2020-08-26 07:45:48 +08:00
|
|
|
struct addrinfo *p = nullptr;
|
2018-11-02 22:26:39 +08:00
|
|
|
int addr_count = 0;
|
2020-08-26 07:45:48 +08:00
|
|
|
for ( p = hp; p != nullptr; p = p->ai_next ) {
|
2018-11-02 22:26:39 +08:00
|
|
|
addr_count++;
|
|
|
|
}
|
|
|
|
Debug(1, "%d addresses returned", addr_count);
|
2003-03-26 19:57:29 +08:00
|
|
|
}
|
2017-10-02 21:11:55 +08:00
|
|
|
|
|
|
|
int RemoteCamera::Read( int fd, char *buf, int size ) {
|
|
|
|
int ReceivedBytes = 0;
|
|
|
|
while ( ReceivedBytes < size ) {
|
|
|
|
// recv blocks until we get data, but it may be of ARBITRARY LENGTH and INCOMPLETE
|
|
|
|
int bytes_to_recv = size - ReceivedBytes;
|
|
|
|
if ( SOCKET_BUF_SIZE < bytes_to_recv )
|
|
|
|
bytes_to_recv = SOCKET_BUF_SIZE;
|
2017-11-08 10:21:51 +08:00
|
|
|
//Debug(3, "Aiming to receive %d of %d bytes", bytes_to_recv, size );
|
2017-11-17 20:52:26 +08:00
|
|
|
int bytes = recv(fd, &buf[ReceivedBytes], bytes_to_recv, 0); //socket, buffer, len, flags
|
2017-10-02 21:11:55 +08:00
|
|
|
if ( bytes <= 0 ) {
|
|
|
|
Error("RemoteCamera::Read Recv error. Closing Socket\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ReceivedBytes += bytes;
|
|
|
|
}
|
|
|
|
return ReceivedBytes;
|
|
|
|
}
|