Skip to content

Commit

Permalink
add digest support for camera 4
Browse files Browse the repository at this point in the history
note you must define username and password in camerapreview.py and allcameraplayer.py
this is just a starting point currently only supports snapshot url, mjpeg support is basicly the same
feel free to adapt if I no longer update.
  • Loading branch information
Gigabyte committed Nov 25, 2020
1 parent 1c40c19 commit bc1d5ff
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
12 changes: 10 additions & 2 deletions resources/lib/allcameraplayer.py
Expand Up @@ -9,6 +9,7 @@
import xbmc, xbmcaddon, xbmcvfs, xbmcgui
import threading, os, requests#, time
from urllib import urlretrieve
from requests.auth import HTTPDigestAuth
import settings, monitor, utils
from resources.lib.ipcam_api_wrapper import CameraAPIWrapper as Camera
import socket
Expand Down Expand Up @@ -139,8 +140,15 @@ def getImagesSnapshot(self, camera, url, control, prefix):

try:
filename = os.path.join(_datapath, '%s_%s.%d.jpg') %(prefix, camera.number, x)
urlretrieve(url, filename)

if '4' in camera.number: #DIGEST IS ONLY ENABLED ON CAMERA 4
r = requests.get(url, stream = True, timeout = TIMEOUT, auth=HTTPDigestAuth('USERNAME', 'PASSWORD')) #You must define your user and password here!!!
with open(filename, 'wb') as fd:
for chunk in r.iter_content(chunk_size=128):
fd.write(chunk)

else:
urlretrieve(url, filename)

if os.path.exists(filename):
control[0].setImage(filename, useCache = False)
xbmcvfs.delete(os.path.join(_datapath, '%s_%s.%d.jpg') %(prefix, camera.number, x - 1))
Expand Down
17 changes: 12 additions & 5 deletions resources/lib/camerapreview.py
Expand Up @@ -10,6 +10,7 @@
import os, requests, time
import utils, settings, cameraplayer, allcameraplayer, monitor
from urllib import urlretrieve
from requests.auth import HTTPDigestAuth
import threading
import socket
TIMEOUT = settings.getSetting_int('request_timeout')
Expand Down Expand Up @@ -123,7 +124,6 @@ def start(self):

if url == '':
url = self.camera.getStreamUrl(2, stream_type)

utils.log(2, 'Camera %s :: Preview Window Opened - Manual: %s; Stream Type: %d; URL: %s' %(self.camera.number, self.monitor.openRequest_manual(self.camera.number), stream_type, url))

if stream_type == 0:
Expand Down Expand Up @@ -234,8 +234,8 @@ def getImagesMjpeg(self, url):
""" Update camera position with mjpeg frames """

try:
stream = requests.get(url, stream = True, timeout = TIMEOUT).raw

# stream = requests.get(url, stream = True, timeout = TIMEOUT, auth=HTTPDigestAuth('USERNAME', 'PASSWORD')).raw #uncomment if you need digest support for mjpeg
stream = requests.get(url, stream = True, timeout = TIMEOUT).raw
except requests.RequestException as e:
utils.log(3, e)
self.img1.setImage(_error, useCache = False)
Expand Down Expand Up @@ -265,13 +265,20 @@ def getImagesMjpeg(self, url):

def getImagesSnapshot(self, url, *args, **kwargs):
""" Update camera position with snapshots """

x = 0
while not self.monitor.abortRequested() and not self.monitor.stopped() and self.monitor.previewOpened(self.camera.number):

try:
filename = os.path.join(_datapath, '%s_%s.%d.jpg') %(self.prefix, self.camera.number, x)
urlretrieve(url, filename)
if '4' in self.camera.number: #DIGEST IS ONLY ENABLED ON CAMERA 4
r = requests.get(url, stream = True, timeout = TIMEOUT, auth=HTTPDigestAuth('USERNAME', 'PASSWORD')) #You must define your user and password here!!!
with open(filename, 'wb') as fd:
for chunk in r.iter_content(chunk_size=128):
fd.write(chunk)

else:
urlretrieve(url, filename)

if os.path.exists(filename):
self.img1.setImage(filename, useCache = False)
Expand Down

0 comments on commit bc1d5ff

Please sign in to comment.