Skip to content

Commit 76f6090

Browse files
committed
streamlined the consumer set up
1 parent 002899d commit 76f6090

4 files changed

Lines changed: 31 additions & 41 deletions

File tree

amazon_kinesis_video_consumer_library/kinesis_video_streams_parser.py

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
import logging
4747
from threading import Thread
4848
import ebmlite
49+
from boto3 import Session
50+
from botocore.response import StreamingBody
4951

5052
# Init the logger.
5153
log = logging.getLogger(__name__)
@@ -54,28 +56,25 @@
5456
class KvsConsumerLibrary(Thread):
5557
def __init__(
5658
self,
57-
stream_name,
58-
get_media_response_object,
59+
kvs_stream_name: str,
5960
on_fragment_arrived,
6061
on_read_stream_complete,
6162
on_read_stream_exception,
6263
):
63-
"""
64-
Initialize the KVS media consumer library
65-
"""
6664
# Call the Thread class's init function
67-
Thread.__init__(self)
65+
super().__init__()
6866

6967
# Used to trigger graceful exit of this thread
7068
self._stop_get_media = False
7169

7270
# Init the local vars.
71+
self.kvs_stream_name = kvs_stream_name
7372
log.info("Initilizing KvsConsumerLibrary...")
74-
self.stream_name = stream_name
75-
self.get_media_response_object = get_media_response_object
73+
7674
self.on_fragment_arrived_callback = on_fragment_arrived
7775
self.on_read_stream_complete_callback = on_read_stream_complete
7876
self.on_read_stream_exception = on_read_stream_exception
77+
self.__acquire_stream_buffer(kvs_stream_name)
7978

8079
log.info("Loading EBMLlite MKV Schema....")
8180
self.schema = ebmlite.loadSchema("matroska.xml")
@@ -84,6 +83,23 @@ def __init__(
8483
raise KeyError("Could not find master element in Matroska schema")
8584
self.matroska_master_element_type = master
8685

86+
def __acquire_stream_buffer(self, kvs_stream_name: str):
87+
session = Session(region_name='eu-west-2')
88+
kvs_client = session.client("kinesisvideo")
89+
response = kvs_client.get_data_endpoint(
90+
StreamName=kvs_stream_name,
91+
APIName="GET_MEDIA"
92+
)
93+
get_media_endpoint = response["DataEndpoint"]
94+
kvs_media_client = session.client(
95+
"kinesis-video-media", endpoint_url=get_media_endpoint
96+
)
97+
get_media_response_object = kvs_media_client.get_media(
98+
StreamName=kvs_stream_name,
99+
StartSelector={"StartSelectorType": "EARLIEST"}
100+
)
101+
self.kvs_streaming_buffer: StreamingBody = get_media_response_object["Payload"]
102+
87103
def _get_ebml_header_elements(
88104
self, fragement_dom: ebmlite.Document
89105
) -> list[ebmlite.Element]:
@@ -138,9 +154,6 @@ def run(self):
138154
"""
139155

140156
try:
141-
# Get the steam botocore.response.StreamingBody object from
142-
# the provided GetMedia response
143-
kvs_streaming_buffer = self.get_media_response_object["Payload"]
144157

145158
#########################################
146159
# Iterate through reading and parsing streaming body
@@ -153,7 +166,7 @@ def run(self):
153166

154167
# Uses the StreamingBody object iterator to read in (default
155168
# 1024 byte) chunks from the streaming buffer.
156-
for chunk in kvs_streaming_buffer:
169+
for chunk in self.kvs_streaming_buffer:
157170
if self._stop_get_media:
158171
break
159172

@@ -202,7 +215,7 @@ def run(self):
202215

203216
# Forward fragment to the on_fragment_arrived callback.
204217
self.on_fragment_arrived_callback(
205-
self.stream_name,
218+
self.kvs_stream_name,
206219
fragment_bytes,
207220
fragment_dom,
208221
fragment_receive_duration,
@@ -228,8 +241,8 @@ def run(self):
228241
#############################################
229242
# call the on_stream_read_complete() callback and exit the
230243
# thread.
231-
self.on_read_stream_complete_callback(self.stream_name)
244+
self.on_read_stream_complete_callback(self.kvs_stream_name)
232245

233246
except Exception as err:
234247
# Pass any exceptions to exception callback.
235-
self.on_read_stream_exception(self.stream_name, err)
248+
self.on_read_stream_exception(self.kvs_stream_name, err)

kvs_consumer_library_example.py

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -78,27 +78,10 @@ def service_loop(self):
7878
####################################################
7979
# Start an instance of the KvsConsumerLibrary reading in a Kinesis Video Stream
8080

81-
# Get the KVS Endpoint for the GetMedia Call for this stream
82-
log.info(f"Getting KVS GetMedia Endpoint for stream: {KVS_STREAM01_NAME} ........")
83-
get_media_endpoint = self._get_data_endpoint(KVS_STREAM01_NAME, "GET_MEDIA")
84-
85-
# Get the KVS Media client for the GetMedia API call
86-
log.info(f"Initializing KVS Media client for stream: {KVS_STREAM01_NAME}........")
87-
kvs_media_client = self.session.client(
88-
"kinesis-video-media", endpoint_url=get_media_endpoint
89-
)
90-
91-
# Make a KVS GetMedia API call with the desired KVS stream and StartSelector type and time bounding.
92-
log.info(f"Requesting KVS GetMedia Response for stream: {KVS_STREAM01_NAME}........")
93-
get_media_response = kvs_media_client.get_media(
94-
StreamName=KVS_STREAM01_NAME, StartSelector={"StartSelectorType": "EARLIEST"}
95-
)
96-
9781
# Initialize an instance of the KvsConsumerLibrary, provide the GetMedia response and the required call-backs
9882
log.info(f"Starting KvsConsumerLibrary for stream: {KVS_STREAM01_NAME}........")
9983
my_stream01_consumer = KvsConsumerLibrary(
10084
KVS_STREAM01_NAME,
101-
get_media_response,
10285
self.on_fragment_arrived,
10386
self.on_stream_read_complete,
10487
self.on_stream_read_exception,
@@ -301,15 +284,6 @@ def on_stream_read_exception(self, stream_name, error):
301284
f"####### ERROR: Exception on read stream: {stream_name}\n####### Fragment Tags:\n{self.last_good_fragment_tags}\nError Message:{error}"
302285
)
303286

304-
####################################################
305-
# KVS Helpers
306-
def _get_data_endpoint(self, stream_name, api_name):
307-
"""
308-
Convenience method to get the KVS client endpoint for specific API calls.
309-
"""
310-
response = self.kvs_client.get_data_endpoint(StreamName=stream_name, APIName=api_name)
311-
return response["DataEndpoint"]
312-
313287

314288
if __name__ == "__main__":
315289
"""

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ requires-python = ">=3.13"
77
dependencies = [
88
"av>=17.1.0",
99
"boto3>=1.43.28",
10+
"botocore>=1.43.28",
1011
"ebmlite>=3.4.1",
1112
"librosa>=0.11.0",
1213
"numpy>=2.4.6",

uv.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)