在线直播网站开发,淘宝佣金推广网站建设,徐州免费网站建设,小创业公司网站怎么做在 Android 音视频开发中#xff0c;“后台推流”是一个经典且棘手的需求。常见的场景包括#xff1a;行车记录仪#xff08;熄屏录像#xff09;、智能安全帽#xff08;后台回传#xff09;、执法记录仪等。
传统的做法是将推流逻辑写在 Activity 中#xff0c;但一旦…在 Android 音视频开发中“后台推流”是一个经典且棘手的需求。常见的场景包括行车记录仪熄屏录像、智能安全帽后台回传、执法记录仪等。传统的做法是将推流逻辑写在 Activity 中但一旦用户按 Home 键或熄屏Activity 及其持有的 Camera 资源极易被系统回收。为了解决这个问题我们需要构建一个基于前台服务Foreground Service的独立采集架构。本文将结合大牛直播SDKSmartMediakit的SmartPublisher模块的源码深度拆解如何实现这一系统。一、 整体架构设计UI 与业务的彻底解耦为了保证推流服务的稳定性我们采用C/SClient/Service架构模式Service 层 (StreamMediaCameraService)作为后台驻留的核心载体。负责持有Engine实例。提升进程优先级前台服务防止 OOM Killer 查杀。Engine 层 (NTStreamMediaCameraEngineImpl)业务大脑。封装 Camera2 API、AudioRecord 以及 Native 推流库。管理数据采集YUV/PCM到编码推流的全流程。UI 层 (MainActivity)纯展示与控制。只负责显示预览画面TextureView和发送控制指令开关相机、开始推流。即使 Activity 销毁如屏幕旋转、内存回收Service 和 Engine 依然存活。通信层 (NTStreamMediaBinder)通过 Binder 机制让 Activity 获取 Service 中的 Engine 句柄。二、 核心实现一构建“杀不死”的前台服务在StreamMediaCameraService.java中最关键的逻辑是适配 Android 8.0 到 Android 14 的前台服务机制。1. 为什么需要前台服务Android 系统对后台应用限制极严。如果不启动前台服务在通知栏显示一条常驻通知App 切后台后几分钟内网络和 CPU 就会被限制导致推流中断。2. 代码实现详解我们需要根据 Android 版本动态设置 Notification Channel 和 ServiceType特别是 Android 10 必须声明 Camera/Microphone 类型。// [文件: StreamMediaCameraService.java] public void start_foreground_service() { try { String channelId nt_camera_service; String channelName Background Camera Service; // 1. 创建 NotificationManager NotificationManager manager (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); // 2. 适配 Android 8.0 (Oreo): 必须创建 NotificationChannel if (Build.VERSION.SDK_INT 26) { NotificationChannel channel new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH); manager.createNotificationChannel(channel); } // 3. 构建动态的 Notification 内容实时显示当前状态 StringBuilder sb new StringBuilder(256); sb.append(流媒体采集服务正在运行;); if (engine_ ! null) { if (engine_.is_opened_camera()) sb.append(摄像头已开启;); if (engine_.is_audio_record_running()) sb.append(麦克风已开启;); if (engine_.is_rtsp_stream_running()) sb.append(已输出RTSP流;); } Notification.Builder builder new Notification.Builder(this) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle(摄像头采集前台服务) .setContentText(sb.toString()); if (Build.VERSION.SDK_INT 26) builder.setChannelId(channelId); Notification notification builder.build(); // 4. 启动前台服务 (核心适配点) final int id 111; if (Build.VERSION.SDK_INT 29) { // Android 9.0 及以下 startForeground(id, notification); } else { // Android 10.0 (Q/R/S...): 必须显式声明服务类型 int type 0; // 动态检查权限防止 Crash if (Build.VERSION.SDK_INT 30 check_camera_permission()) type | ServiceInfo.FOREGROUND_SERVICE_TYPE_CAMERA; if (Build.VERSION.SDK_INT 30 check_record_audio_permission()) type | ServiceInfo.FOREGROUND_SERVICE_TYPE_MICROPHONE; startForeground(id, notification, type); } } catch (Exception e) { Log.e(TAG, start_foreground Exception:, e); stopSelf(); } }注意在AndroidManifest.xml中也必须对应声明service android:name.StreamMediaCameraService android:foregroundServiceTypecamera|microphone /三、 核心实现二Activity 与 Service 的安全绑定MainActivity不直接newEngine而是通过bindService获取。这保证了 Engine 的生命周期跟随 Service 而非 Activity。1. Binder 定义// [文件: NTStreamMediaBinder.java] public class NTStreamMediaBinder extends Binder { // 使用 AtomicReference 保证多线程安全 private final AtomicReferenceNTStreamMediaCameraEngine engine_ new AtomicReference(); public NTStreamMediaCameraEngine get_stream_media_camera_engine() { return engine_.get(); } // ... attach/detach 逻辑 }2. Service 中的绑定逻辑// [文件: StreamMediaCameraService.java] Override public void onCreate() { super.onCreate(); // Service 创建时初始化 Engine确保 Engine 和 Service 同生共死 this.engine_ new NTStreamMediaCameraEngineImpl(getApplication(), service_handler_, running_thread_, lib_publisher_); } Override public IBinder onBind(Intent intent) { // 当 Activity 绑定时将 Engine 挂载到 Binder 上 binder_.attach(engine_); return binder_; }3. Activity 中的获取逻辑// [文件: MainActivity.java] private void onServiceConnected(NTStreamMediaBinder binder) { media_binder_ binder; // 关键点从 Binder 中拿到了活着的 Engine 实例 media_engine_ get_stream_media_engine(); if (media_engine_ ! null) { // 重新注册回调因为 Activity 重建后回调对象变了 media_engine_callback_ new NTStreamEngineCallbackImpl(); media_engine_callback_.reset(MainActivity.this); media_engine_.register_callback(media_engine_callback_); // 恢复预览 View如果有的话 if (is_preview_camera_ preview_view_.isAvailable()) media_engine_.set_camera_preview_view(preview_view_); } // ... }四、 核心实现三Camera2 数据采集与 Native 推流这是整个系统的数据引擎。NTStreamMediaCameraEngineImpl负责协调Camera2Helper采集数据并将数据投递给 JNI 层。1. 数据的产生 (Camera2 API)在Camera2Helper.java中我们通过ImageReader获取 YUV 数据格式通常为YUV_420_888。2. 数据的流转 (Java - Native)在NTStreamMediaCameraEngineImpl中我们实现了Camera2Listener。这是性能最敏感的部分直接决定了推流的延迟和流畅度。// [文件: NTStreamMediaCameraEngineImpl.java] Override public void onCameraImageData(Image image) { if (null image) return; // 只有当推流开启时才处理数据节省 CPU if (!stream_publisher_.is_publishing()) return; // 校验格式必须是 YUV_420_888 if (image.getFormat() ! ImageFormat.YUV_420_888) return; Image.Plane[] planes image.getPlanes(); // 获取 Y, U, V 三个平面的数据指针和步长 ByteBuffer y_buffer planes[0].getBuffer(); ByteBuffer u_buffer planes[1].getBuffer(); ByteBuffer v_buffer planes[2].getBuffer(); int y_row_stride planes[0].getRowStride(); int u_row_stride planes[1].getRowStride(); int v_row_stride planes[2].getRowStride(); int uv_pixel_stride planes[1].getPixelStride(); // 关键参数决定是 I420 还是 NV21 等 int width image.getWidth(); int height image.getHeight(); // 获取旋转角度处理横竖屏推流的关键 int rotation_degree cameraImageRotationDegree_.get(); // 【核心调用】直接将 ByteBuffer 传递给 Native 层 (JNI) // 避免了在 Java 层进行 byte[] 拷贝极大提升性能 stream_publisher_.PostLayerImageYUV420888ByteBuffer( 0, 0, 0, y_buffer, 0, y_row_stride, u_buffer, 0, u_row_stride, v_buffer, 0, v_row_stride, uv_pixel_stride, width, height, 0, 0, 0, 0, // 翻转和缩放参数 0, rotation_degree ); }代码亮点零拷贝 (Zero Copy): 使用ByteBuffer直接在 Java 和 C 之间传递内存地址避免了byte[]数组的反复拷贝这对于 1080P/30fps 的高清流至关重要。动态旋转: 通过rotation_degree参数底层 SDK 自动处理图像旋转确保观看端看到的画面方向正确。五、 核心实现四RTSP Server 与多路分发系统不仅支持 RTMP 推流还内置了一个轻量级的 RTSP Server。这意味着手机本身变成了一个 IPC网络摄像机。// [文件: NTStreamMediaCameraEngineImpl.java] private boolean start_rtsp_stream_internal(String stream_name) { // 1. 确保 RTSP Server 端口已监听 long rtsp_server_handle rtsp_server_.get_native(); if(0 rtsp_server_handle) return false; // 2. 创建 SDK 推流实例 if (!test_and_create_sdk_instance()) return false; // 3. 设置流名称 (例如 rtsp://192.168.1.100:8554/stream1) stream_publisher_.SetRtspStreamName(stream_name); // 4. 将推流实例挂载到 Server 上 stream_publisher_.ClearRtspStreamServer(); stream_publisher_.AddRtspStreamServer(rtsp_server_handle); // 5. 启动流 if (!stream_publisher_.StartRtspStream()) { return false; } // 6. 启动水印线程 (可选) start_video_layer_post_thread(); return true; }六、 工业级细节电池优化白名单仅仅依靠前台服务在某些深度定制的 ROM如 MIUI、EMUI或 Android 高版本 Doze 模式下仍可能被限制网络。在MainActivity中我们必须请求用户豁免电池优化。// [文件: MainActivity.java] // 判断是否已在白名单中 private boolean isIgnoringBatteryOptimizations(){ if (Build.VERSION.SDK_INT Build.VERSION_CODES.M) { String packageName getPackageName(); PowerManager pm (PowerManager) getSystemService(POWER_SERVICE); return pm.isIgnoringBatteryOptimizations(packageName); } return false; } // 跳转系统设置申请白名单 private void gotoSettingIgnoringBatteryOptimizations() { if (Build.VERSION.SDK_INT Build.VERSION_CODES.M) { try { Intent intent new Intent(); String packageName getPackageName(); intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS); intent.setData(Uri.parse(package: packageName)); startActivityForResult(intent, REQUEST_IGNORE_BATTERY_CODE); } catch (Exception e) { Log.e(TAG, gotoSettingIgnoringBatteryOptimizations Exception:, e); } } }七、 总结通过上述代码的拆解我们利用大牛直播SDK的轻量级RTSP服务模块构建了一个完整的 Android 后台流媒体采集系统稳定性利用NotificationServiceForegroundServiceType适配确保进程在后台不被查杀。高性能通过Camera2 APIByteBufferJNI实现 YUV 数据的零拷贝传输。灵活性Service承载业务Activity仅作展示实现了完美的解耦支持断线重连和后台静默推流。功能全同时支持 RTMP 推流、RTSP 服务端、水印叠加、软硬编码自动切换。这套架构不仅适用于 Demo 演示更是开发行车记录仪、远程协助、无人机图传等商业级产品的坚实基础。 CSDN官方博客音视频牛哥-CSDN博客