SrtpEncoder extends MessageToByteEncoder<RtpPacket> { @Override protected void encode(ChannelHandlerContext ctx, RtpPacket msg, ByteBuf out) throws Exception { final byte[] rawData = msg.getRawData(); final byte[] encoded = this.encoder.transform(rawData); if (encoded == null || encoded.length == 0) { log.warn("Channel " + ctx.channel().localAddress() + " could not encode outgoing SRTP packet."); } else { out.writeBytes(encoded); } } SrtpEncoder(PacketTransformer encoder);   }
@Test public void testEncode() { final RtpPacket rtpPacket = mock(RtpPacket.class); final PacketTransformer transformer = mock(PacketTransformer.class); final SrtpEncoder decoder = new SrtpEncoder(transformer); final EmbeddedChannel channel = new EmbeddedChannel(decoder); final byte[] encodedPayload = "encoded".getBytes(); final byte[] decodedPayload = "decoded".getBytes(); when(rtpPacket.getRawData()).thenReturn(decodedPayload); when(transformer.transform(decodedPayload)).thenReturn(encodedPayload); channel.writeOutbound(rtpPacket); Object outboundObject = channel.readOutbound(); verify(transformer).transform(decodedPayload); assertNotNull(outboundObject); assertTrue(outboundObject instanceof ByteBuf); byte[] data = new byte[encodedPayload.length]; ((ByteBuf) outboundObject).getBytes(0, data); assertEquals(new String(encodedPayload), new String(data)); }