Skip to content

Commit

Permalink
I did a little refactoring, and change the chunk size behaviour. Now
Browse files Browse the repository at this point in the history
there are two different variables that are affected by this:

1) frame_size that is used by ffmpeg and avcon.
    frame_size = 32 * chunk_size.
2) buffer_size that is used by the Flask server.
    buffer_size = 2 * chunk_size**2

These changes are another attempt to find a solution for #61.
  • Loading branch information
muammar committed Jun 2, 2017
1 parent fa50395 commit 8488835
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 37 deletions.
14 changes: 8 additions & 6 deletions mkchromecast/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,13 @@
parser.add_argument(
'--chunk-size',
type=int,
default='32768',
default='1024',
help=
'''
Set the chunk size for streaming in the Flask server. Default to 32768. This
option only works when using the ffmpeg or avconv backends.
Set the chunk size base for streaming in the Flask server. Default to 1024.
This option only works when using the ffmpeg or avconv backends. This number is
the base to set both the buffer_size (defined by 2 * chunk_size**2) in Flask
server and the frame_size (defined by 32 * chunk_size).
Example:
Expand Down Expand Up @@ -728,11 +730,11 @@
avoid = ['parec', 'node']

if isinstance(args.segment_time, int) and backend not in avoid:
segmenttime = args.segment_time
segment_time = args.segment_time
elif isinstance(args.segment_time, float) or backend in avoid:
segmenttime = None
segment_time = None
else:
segmenttime = None
segment_time = None

"""
Video
Expand Down
66 changes: 35 additions & 31 deletions mkchromecast/audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,14 @@
tray = mkchromecast.__init__.tray
adevice = mkchromecast.__init__.adevice
chunk_size = mkchromecast.__init__.chunk_size
segmenttime = mkchromecast.__init__.segmenttime
segment_time = mkchromecast.__init__.segment_time

frame_size = 32 * chunk_size
buffer_size = 2 * chunk_size**2

if debug == True:
print(':::audio::: chunk_size: ', chunk_size)
print(':::audio::: chunk_size, frame_size, buffer_size: %s, %s, %s'
% (chunk_size, frame_size, buffer_size))
debug = mkchromecast.__init__.debug
sourceurl = mkchromecast.__init__.sourceurl
config = ConfigParser.RawConfigParser()
Expand Down Expand Up @@ -276,8 +280,8 @@ def modalsa():
print (command)
return

def set_segmenttime():
string = [ '-f', 'segment', '-segment_time', str(segmenttime) ]
def set_segment_time():
string = [ '-f', 'segment', '-segment_time', str(segment_time) ]
for element in string:
command.insert(-9, element)
return
Expand All @@ -293,8 +297,8 @@ def set_segmenttime():
backend,
'-ac', '2',
'-ar', '44100',
'-frame_size', str(chunk_size),
'-fragment_size', str(chunk_size),
'-frame_size', str(frame_size),
'-fragment_size', str(frame_size),
'-f', 'pulse',
'-i', 'mkchromecast.monitor',
'-f', 'mp3',
Expand All @@ -307,8 +311,8 @@ def set_segmenttime():
if adevice != None:
modalsa()

if segmenttime != None:
set_segmenttime()
if segment_time != None:
set_segment_time()

elif (platform == 'Linux' and backends_dict[backend] == 'parec' or
backends_dict[backend] == 'gstreamer'):
Expand Down Expand Up @@ -357,8 +361,8 @@ def set_segmenttime():
'-b:a', bitrate,
'pipe:'
]
if segmenttime != None:
set_segmenttime()
if segment_time != None:
set_segment_time()

"""
OGG 192k
Expand All @@ -370,8 +374,8 @@ def set_segmenttime():
backend,
'-ac', '2',
'-ar', '44100',
'-frame_size', str(chunk_size),
'-fragment_size', str(chunk_size),
'-frame_size', str(frame_size),
'-fragment_size', str(frame_size),
'-f', 'pulse',
'-i', 'mkchromecast.monitor',
'-f', 'ogg',
Expand All @@ -384,8 +388,8 @@ def set_segmenttime():
if adevice != None:
modalsa()

if segmenttime != None:
set_segmenttime()
if segment_time != None:
set_segment_time()

elif (platform == 'Linux' and backends_dict[backend] == 'parec' or
backends_dict[backend] == 'gstreamer'):
Expand Down Expand Up @@ -447,8 +451,8 @@ def set_segmenttime():
backend,
'-ac', '2',
'-ar', '44100',
'-frame_size', str(chunk_size),
'-fragment_size', str(chunk_size),
'-frame_size', str(frame_size),
'-fragment_size', str(frame_size),
'-f', 'pulse',
'-i', 'mkchromecast.monitor',
'-f', 'adts',
Expand Down Expand Up @@ -511,8 +515,8 @@ def set_segmenttime():
'-b:a', bitrate,
'pipe:'
]
if segmenttime != None:
set_segmenttime()
if segment_time != None:
set_segment_time()
if platform == 'Darwin':
cutoff = ['-cutoff', '18000']
for element in cutoff:
Expand All @@ -529,8 +533,8 @@ def set_segmenttime():
backend,
'-ac', '2',
'-ar', '44100',
'-frame_size', str(chunk_size),
'-fragment_size', str(chunk_size),
'-frame_size', str(frame_size),
'-fragment_size', str(frame_size),
'-f', 'pulse',
'-i', 'mkchromecast.monitor',
'-f', 'wav',
Expand All @@ -542,8 +546,8 @@ def set_segmenttime():
if adevice != None:
modalsa()

if segmenttime != None:
set_segmenttime()
if segment_time != None:
set_segment_time()

elif (platform == 'Linux' and backends_dict[backend] == 'parec' or
backends_dict[backend] == 'gstreamer'):
Expand Down Expand Up @@ -573,8 +577,8 @@ def set_segmenttime():
'-ar', samplerate,
'pipe:'
]
if segmenttime != None:
set_segmenttime()
if segment_time != None:
set_segment_time()

"""
FLAC 24-Bit (values taken from: https://trac.ffmpeg.org/wiki/Encode/HighQualityAudio) except for parec.
Expand All @@ -585,8 +589,8 @@ def set_segmenttime():
backend,
'-ac', '2',
'-ar', '44100',
'-frame_size', str(chunk_size),
'-fragment_size', str(chunk_size),
'-frame_size', str(frame_size),
'-fragment_size', str(frame_size),
'-f', 'pulse',
'-i', 'mkchromecast.monitor',
'-f', 'flac',
Expand All @@ -599,8 +603,8 @@ def set_segmenttime():
if adevice != None:
modalsa()

if segmenttime != None:
set_segmenttime()
if segment_time != None:
set_segment_time()

elif (platform == 'Linux' and backends_dict[backend] == 'parec' or
backends_dict[backend] == 'gstreamer'):
Expand All @@ -627,8 +631,8 @@ def set_segmenttime():
'-b:a', bitrate,
'pipe:'
]
if segmenttime != None:
set_segmenttime()
if segment_time != None:
set_segment_time()

verbose_backend = ['ffmpeg', 'avconv']
if debug == False and backends_dict[backend] in verbose_backend:
Expand Down Expand Up @@ -699,7 +703,7 @@ def stream():
process = Popen(command, stdin=gst.stdout, stdout=PIPE, bufsize=-1)
else:
process = Popen(command, stdout=PIPE, bufsize=-1)
read_chunk = partial(os.read, process.stdout.fileno(), chunk_size)
read_chunk = partial(os.read, process.stdout.fileno(), buffer_size)
return Response(iter(read_chunk, b''), mimetype=mtype)

def start_app():
Expand Down

1 comment on commit 8488835

@muammar
Copy link
Owner Author

@muammar muammar commented on 8488835 Jun 2, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that a chunk-size of 512 as basis works very well with WAV.

Please sign in to comment.