NovelRaspy
Posts: 8
Joined: Tue Apr 26, 2016 7:06 pm

Change mjpg-streamer output settings at runtime

Wed Jan 31, 2018 6:58 pm

Hi, I'm developing a simple home surveillance system using a raspberry pi zero w as an IP Camera. I'm using mjpg-streamer to stream video. The system logic is to start stream if and only if the pir sensor detect something. I found out that starting mjpg-streamer can requere up to 10 seconds, but is too much time, so i thought start mjpg-streamer at the startup but stream on a port only when is necessary. Is it possible? And if not, is there any other solution?
Last edited by NovelRaspy on Thu Feb 08, 2018 11:26 pm, edited 2 times in total.

NovelRaspy
Posts: 8
Joined: Tue Apr 26, 2016 7:06 pm

Re: Change mjpg-streamer output settings at runtime

Wed Feb 07, 2018 5:58 pm

I decided to use a workaround. I start mjpg-streamer in daemon mode then i check if is writing on a port in order to use the SIGSTOP and SIGCONT. Now i get a new problem, when i try to read from the streaming port, i get no output.
That's my code.

Code: Select all

# Read from socket Example
import socket
import sys
import time

class NetworkError(RuntimeError):
    pass

def retryer(max_retries=10, timeout=5):
    def wraps(func):
        exceptions = (
            socket.herror,
            socket.gaierror,
            socket.timeout,
            ConnectionError
        )
        def inner(*args, **kwargs):
            for i in range(max_retries):
                try:
                    result = func(*args, **kwargs)
                except exceptions:
                    time.sleep(timeout)
                    continue
                else:
                    return result
            else:
                raise NetworkError
        return inner
    return wraps

@retryer(max_retries=10, timeout=0.5)
def socket_read_check(port):
    # Get hostaname
    hostname = socket.gethostname()
    # Create a TCP/IP socket
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # Connect the socket to the port where the server is listening
    server_address = (hostname, port)
    sock.connect(server_address)

    while True:
        data = sock.recv(4096)
        print(data.decode("utf-8"))
        if len(data) > 0:
            break
        time.sleep(0.5)
    sock.close()


if __name__ == "__main__":
    socket_read_check(8080)

What am I wrong?

NovelRaspy
Posts: 8
Joined: Tue Apr 26, 2016 7:06 pm

Re: Change mjpg-streamer output settings at runtime

Thu Feb 08, 2018 10:49 pm

Ok, i fond the answer reading the mjpg-streamer source. It is necessary to send an HTTP request in order to get an answer. In the file located at mjpg-streamer/mjpg-streamer-experimental/plugins/input_http/mjpg-proxy.c in line 113 is defined the request. That's my python code:

Code: Select all

# Read from socket Example
import socket
import sys
import time

class NetworkError(RuntimeError):
    pass

def retryer(max_retries=10, timeout=5):
    def wraps(func):
        exceptions = (
            socket.herror,
            socket.gaierror,
            socket.timeout,
            ConnectionError
        )
        def inner(*args, **kwargs):
            for i in range(max_retries):
                try:
                    result = func(*args, **kwargs)
                except exceptions:
                    time.sleep(timeout)
                    continue
                else:
                    return result
            else:
                raise NetworkError
        return inner
    return wraps

@retryer(max_retries=10, timeout=0.5)
def socket_read_check(port):
    # Get hostaname
    hostname = socket.gethostname()
    # Create a TCP/IP socket
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # Connect the socket to the port where the server is listening
    server_address = (hostname, port)
    sock.connect(server_address)
    # send the request
    sock.send(b'GET /?action=stream HTTP/1.0\r\n\r\n')
    print ("sent ?action=stream")
    while True:
        data = sock.recv(1024)
        # # print the decoded byte response
        # print(data.decode("utf-8"))
        print(data)
        if len(data) > 0:
            break
        time.sleep(0.5)
    sock.close()


if __name__ == "__main__":
    socket_read_check(8080)

Return to “Graphics, sound and multimedia”

Who is online

Users browsing this forum: k-pi and 7 guests