jeffreydwalter / arlo

Python module for interacting with Netgear's Arlo camera system.
Apache License 2.0
520 stars 123 forks source link

Streaming Multiple Arlo Go Cameras #144

Closed slobglob closed 4 years ago

slobglob commented 4 years ago

What version of Python are you using (python -V)?

python -V
Python 2.7.14

What operating system and processor architecture are you using (python -c 'import platform; print(platform.uname());')?

python -c 'import platform; print(platform.uname());' 
('Darwin', 'Shais-MacBook-Pro-2.local', '19.0.0', 'Darwin Kernel Version 19.0.0: Thu Oct 17 16:17:15 PDT 2019; root:xnu-6153.41.3~29/RELEASE_X86_64', 'x86_64', 'i386')

Which Python packages do you have installed (run the pip freeze or pip3 freeze command and paste output)?

pip freeze
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
arlo==1.2.30
astroid==1.6.3
awscli==1.15.32
backports.functools-lru-cache==1.5
botocore==1.10.32
cachetools==3.1.1
certifi==2019.9.11
chardet==3.0.4
Click==7.0
colorama==0.3.9
configobj==5.0.6
configparser==3.5.0
docutils==0.14
enum34==1.1.6
Flask==1.1.1
futures==3.2.0
google-api-core==1.14.3
google-api-python-client==1.7.11
google-auth==1.6.3
google-auth-httplib2==0.0.3
google-cloud-core==1.0.3
google-cloud-logging==1.14.0
google-python-cloud-debugger==2.8
googleapis-common-protos==1.6.0
grpcio==1.24.1
httplib2==0.14.0
idna==2.8
isort==4.3.4
itsdangerous==1.1.0
Jinja2==2.10.3
jmespath==0.9.3
lazy-object-proxy==1.3.1
MarkupSafe==1.1.1
mccabe==0.6.1
monotonic==1.5
protobuf==3.10.0
pyasn1==0.4.7
pyasn1-modules==0.2.7
pycairo==1.16.3
pylint==1.8.4
PySocks==1.7.1
python-dateutil==2.7.3
pytz==2019.3
PyYAML==3.12
requests==2.22.0
rsa==3.4.2
s3transfer==0.1.13
singledispatch==3.4.0.3
six==1.12.0
sseclient==0.0.22
uritemplate==3.0.0
urllib3==1.25.6
Werkzeug==0.16.0
wrapt==1.10.11

Which version of ffmpeg are you using (ffmpeg -version)?

ffmpeg -version 
ffmpeg version 4.0.2 

Which Arlo hardware do you have (camera types - [Arlo, Pro, Q, etc.], basestation model, etc.)?

Arlo Go

What did you do?

I'm trying to stream multiple cameras that are linked to the same account I'm using the example script arlo-streamingvideo.py to stream 2 (or more) Arlo Go cameras in parallel.

from arlo import Arlo
import logging
import sys

from subprocess import call
import time

# For urllib3 debugging logs
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
USERNAME = 'user@example.com'
PASSWORD = 'supersecretpassword'

try:

    arlo = Arlo(USERNAME, PASSWORD)
    cameras = arlo.GetDevices('camera')
    urlA = arlo.StartStream(cameras[0], cameras[0])
    time.sleep(10)
    urlB = arlo.StartStream(cameras[1], cameras[1])
    time.sleep(100)
    #call(['ffmpeg', '-re', '-i', url, '-t', '10', '-acodec', 'copy', '-vcodec', 'copy', 'test.mp4'])

except Exception as e:
    print(e)

The script itself doesn't stream the actual url anywhere, but I check to see if the logs make sense.

What did you expect to see?

What did you see instead?

The issue that I've noticed is related to the EventStream design, every Go camera (which is a basestation as well) initiates its own EventStream object on /subscribe, and from my understanding and digging through the official Arlo site, the /subscribe EventStream should be per logged in account (i.e. per Arlo object) and not per camera/basestation (correct me if I'm wrong). What happens with the current design is that the first camera (i.e. camera[0]) is initiated (/startStream) fine, when the 2nd camera (i.e. camera[1]) is initiated (/startStream) with its own EventStream i get a logout action from the event on the first EventStream what makes the /notify requests stop and eventually the rtsps url itself will be invalid thus making only the 2nd stream (camera[1]) stay alive and streamable.

If I'll add a 3rd camera to this flow, it'll do the same and cancel the 2nd camera.

I'm trying to modify library to support multiple cameras at once and hopefully to submit a PR, but still didn't get it right. I'm trying to see if it makes sense to separate the sseclient from the basestation EventStream and have it per Arlo object. And the EventStream for the basestation itself will take care of the heartbeat and the rest of the camera logic.

Right now I'm just worried that I missed something and the original design is fine.

Does any of this makes sense?

Does this issue reproduce with the latest release?

Didn't check, but from the changes between the releases I think there will be no difference.

slobglob commented 4 years ago

@jeffreydwalter I eventually fixed it with this commit: https://github.com/slobglob/arlo/commit/29e93b4683f85b145fe08337c255627215b798e6 for now. It works fine for me at least with the minimal testing I did so far.

Let me know if it makes sense, I'll create a PR if you want. If you have any idea of how to improve it, let me know.

jeffreydwalter commented 4 years ago

Hey Shai! If that fixes things I'm happy to have the PR. Thanks!

slobglob commented 4 years ago

Sure thing! I'll do some more QA and final fixes then I'll send in a PR. Why did you have the sse object within the EventStream to begin with? Is it a bug? Or I misunderstood the design? @jeffreydwalter

jeffreydwalter commented 4 years ago

Awesome, thanks!

I embedded the sse client in the EventStream because the sse IS the event stream, so it made sense to me. I only had a single basestation, so I never had to deal with multiple base stations and wrongly assumed that each basestation had it's own subscription. Are you sure it's the GET /subscribe that's logging you out and not the Login call?

jeffreydwalter commented 4 years ago

If it's true that all the basestations share a single event stream, then there's lots of cleanup that can happen... I made a branch and refactored the code to remove the collection of event_streams in favor of a single event_stream.

Give this branch a try: https://github.com/jeffreydwalter/arlo/pull/145

jeffreydwalter commented 4 years ago

Inconveniently, my base station died, so I can't test it out... :/ Let me know how it goes.

slobglob commented 4 years ago

Yeah, I was pretty positive that the sse stream logged me out, and now with my modifications I'm 100% positive since it doesn't happen anymore. The main issue here is that I'm using only Arlo Go cameras which is a basestation by itself, so multiple basestations require only one /subscribe EventStream :)

jeffreydwalter commented 4 years ago

Right on. The funny thing is that I tested my code with multiple Arlo go cameras and never had that issue. Are you 100% sure there wasn't a second call to Login?

jeffreydwalter commented 4 years ago

Give my branch a try and see how it goes. Having an event_stream object for each basestation is definitely not necessary and probably causes a lot of contention for messages from the queue.

slobglob commented 4 years ago

Yes, I'm 100% there's only one login call, but I'll double check. That's the first thing I suspected that happens when I got the logout event, then after a few hours of debugging I got to the conclusion its the EventStream that logs me out.

Maybe Arlo changed their API? Does that ever happen?

I'll take a look at your branch soon. Thanks again.

slobglob commented 4 years ago

@jeffreydwalter I'm not sure your PR will do the trick. When having just one EventStream the heartbeat function will stop and then the stream itself will die as well.

jeffreydwalter commented 4 years ago

Did you try it? Why do you think the heartbeat will stop? The heartbeat runs in its' own thread.

slobglob commented 4 years ago

Yeah I get what you're saying. I'll have to test and see.

I'll keep you posted. Thanks man.

jeffreydwalter commented 4 years ago

@slobglob did you get a chance to test out the https://github.com/jeffreydwalter/arlo/tree/single-event-stream branch with multiple cameras? I'd really appreciate your feedback since I don't even have a single Arlo camera to test with anymore (my basestation died). :/

slobglob commented 4 years ago

@jeffreydwalter hey, unfortunately didn't have the time to check that solution yet. I'll get back to the Arlo soon and will definitely give it a shot. Thanks for the effort and the interest.

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

jeffreydwalter commented 4 years ago

Hey @slobglob any update on this? I'd love to merge this in if it fixes the problems. Please let me know asap. Thanks!

slobglob commented 4 years ago

Not yet, I'll be getting back to this in the next month or so. I'll keep you updated. Sorry for the delay.

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

jeffreydwalter commented 4 years ago

@slobglob hey man, you still alive? I just merged in the changes to use 1 event stream for multiple basestations. I no longer have any Arlo cameras, so if you're still work with Arlo stuff, I'd appreciate a little feedback about the change.

slobglob commented 4 years ago

Hey,

Alive and kicking my friend. How have you been?

I don't have any arlos at the moment but we might work on that sometimes soon. Sorry I couldn't help. :)