File size: 7,720 Bytes
8aedc84
 
51f51c3
8aedc84
 
51f51c3
8aedc84
 
 
 
 
 
 
 
 
ec2d8f0
8aedc84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3367d1b
 
8aedc84
 
 
 
51f51c3
 
 
 
 
 
 
 
 
8aedc84
 
 
 
 
51f51c3
8aedc84
51f51c3
8aedc84
 
 
 
 
 
 
 
 
 
 
 
 
 
51f51c3
 
 
 
 
 
 
 
 
 
 
 
8aedc84
 
 
 
 
51f51c3
8aedc84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ec2d8f0
8aedc84
 
 
 
 
 
 
 
 
51f51c3
8aedc84
 
ec2d8f0
8aedc84
 
 
 
 
 
 
 
ec2d8f0
8aedc84
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/env python3
"""
Video Consumer Example - Updated for Workspace API

This example demonstrates how to connect as a video consumer and receive
video frames from a producer in the RobotHub TransportServer.
"""

import asyncio
import logging
import time
from pathlib import Path

import cv2
import numpy as np
from transport_server_client.video import VideoConsumer

# Setup logging
logging.basicConfig(
    level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger(__name__)


class VideoFrameHandler:
    """Handles received video frames with optional saving and display"""

    def __init__(
        self, save_frames: bool = False, output_dir: str = "./received_frames"
    ):
        self.save_frames = save_frames
        self.output_dir = Path(output_dir) if save_frames else None
        if self.output_dir:
            self.output_dir.mkdir(exist_ok=True)

        self.frame_count = 0
        self.total_bytes = 0
        self.start_time = time.time()
        self.last_log_time = time.time()

    def handle_frame(self, frame_data):
        """Process received frame data"""
        try:
            self.frame_count += 1
            current_time = time.time()

            # Extract frame information
            metadata = frame_data.metadata
            width = metadata.get("width", 0)
            height = metadata.get("height", 0)
            format_type = metadata.get("format", "unknown")

            # Convert bytes to numpy array
            frame_bytes = frame_data.data
            self.total_bytes += len(frame_bytes)

            # Reconstruct image from bytes (server sends RGB format)
            img = np.frombuffer(frame_bytes, dtype=np.uint8).reshape((height, width, 3))

            # Save frames if requested
            if self.save_frames and self.frame_count % 30 == 0:  # Save every 30th frame
                # Convert RGB to BGR for OpenCV
                img_bgr = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
                frame_path = self.output_dir / f"frame_{self.frame_count:06d}.jpg"
                cv2.imwrite(str(frame_path), img_bgr)
                logger.info(f"πŸ’Ύ Saved frame {self.frame_count} to {frame_path}")

            # Log statistics periodically
            if current_time - self.last_log_time >= 5.0:  # Every 5 seconds
                elapsed = current_time - self.start_time
                fps = self.frame_count / elapsed if elapsed > 0 else 0
                mb_received = self.total_bytes / (1024 * 1024)

                logger.info("πŸ“Š Video Stats:")
                logger.info(f"   Frames received: {self.frame_count}")
                logger.info(f"   Resolution: {width}x{height}")
                logger.info(f"   Format: {format_type}")
                logger.info(f"   Average FPS: {fps:.1f}")
                logger.info(f"   Data received: {mb_received:.2f} MB")

                self.last_log_time = current_time

        except Exception:
            logger.exception(f"❌ Error handling frame {self.frame_count}")


async def main():
    """Main consumer example"""
    # Get connection details from user
    print("Enter video room connection details:")
    workspace_id = input("Workspace ID: ").strip()
    room_id = input("Room ID (or press Enter for 'webcam'): ").strip() or "webcam"

    if not workspace_id:
        logger.error("Workspace ID is required!")
        return

    # Configuration
    base_url = "http://localhost:8000"
    duration = 60  # Run for 60 seconds
    save_frames = True  # Save some frames as proof

    logger.info("🎬 Video Consumer Example - Updated for Workspace API")
    logger.info("=" * 50)
    logger.info(f"Workspace ID: {workspace_id}")
    logger.info(f"Room ID: {room_id}")
    logger.info(f"Server: {base_url}")
    logger.info(f"Duration: {duration} seconds")
    logger.info(f"Save frames: {save_frames}")

    # Create frame handler
    frame_handler = VideoFrameHandler(save_frames=save_frames)

    # Create consumer
    consumer = VideoConsumer(base_url)

    # Set up event handlers
    consumer.on_frame_update(frame_handler.handle_frame)

    def on_stream_started(config, producer_id):
        logger.info(f"πŸš€ Stream started by producer {producer_id}")
        logger.info(f"   Config: {config}")

    def on_stream_stopped(producer_id, reason):
        logger.info(f"⏹️ Stream stopped by producer {producer_id}")
        if reason:
            logger.info(f"   Reason: {reason}")

    consumer.on_stream_started(on_stream_started)
    consumer.on_stream_stopped(on_stream_stopped)

    # Track connection progress
    connection_events = []

    try:
        logger.info("πŸ”Œ Connecting to room...")
        connected = await consumer.connect(workspace_id, room_id)

        if not connected:
            logger.error("❌ Failed to connect to room")
            return

        logger.info("βœ… Connected to room successfully")
        connection_events.append("connected")

        # Start receiving video
        logger.info("πŸ“Ί Starting video reception...")
        await consumer.start_receiving()
        connection_events.append("receiving_started")

        # Run for specified duration
        logger.info(f"⏱️ Running for {duration} seconds...")
        logger.info("πŸ“Ί Waiting for video frames... (Press Ctrl+C to stop early)")

        start_time = time.time()
        try:
            while time.time() - start_time < duration:
                await asyncio.sleep(1)

                # Show progress
                elapsed = time.time() - start_time
                if int(elapsed) % 10 == 0 and elapsed > 0:  # Every 10 seconds
                    logger.info(
                        f"⏱️ Progress: {elapsed:.0f}s - Frames: {frame_handler.frame_count}"
                    )

        except KeyboardInterrupt:
            logger.info("πŸ›‘ Stopped by user")

        # Final statistics
        elapsed = time.time() - start_time
        logger.info("πŸ“Š Final Results:")
        logger.info(f"   Test duration: {elapsed:.1f} seconds")
        logger.info(f"   Total frames: {frame_handler.frame_count}")
        logger.info(f"   Connection events: {connection_events}")

        if frame_handler.frame_count > 0:
            avg_fps = frame_handler.frame_count / elapsed
            mb_total = frame_handler.total_bytes / (1024 * 1024)

            logger.info(f"   Average FPS: {avg_fps:.1f}")
            logger.info(f"   Total data: {mb_total:.2f} MB")

            if save_frames and frame_handler.output_dir:
                saved_files = list(frame_handler.output_dir.glob("*.jpg"))
                logger.info(f"   Saved frames: {len(saved_files)}")
                if saved_files:
                    logger.info(f"   Output directory: {frame_handler.output_dir}")

            logger.info("πŸŽ‰ SUCCESS: Video consumer is working correctly!")
        else:
            logger.warning("⚠️ No frames received - check if producer is active")

    except Exception as e:
        logger.exception(f"❌ Consumer example failed: {e}")
        import traceback

        traceback.print_exc()

    finally:
        # Cleanup
        logger.info("🧹 Cleaning up...")
        try:
            await consumer.stop_receiving()
            await consumer.disconnect()
            logger.info("πŸ‘‹ Consumer stopped successfully")
        except Exception as e:
            logger.exception(f"Error during cleanup: {e}")


if __name__ == "__main__":
    try:
        asyncio.run(main())
    except KeyboardInterrupt:
        logger.info("πŸ‘‹ Goodbye!")
    except Exception as e:
        logger.exception(f"πŸ’₯ Fatal error: {e}")
        import traceback

        traceback.print_exc()