func WriteToInputPort(data uintptr, length C.size_t, pts float64, bufSize C.size_t) int { newPts := DvdToIsmdPts(pts) if flushFlag { start := newPts log.Printf("Flush %f", float64(newPts)/90000.0) Flush() SendStartPacket(start, m_viddec_input_port) SetBaseTime(GetCurrentTime() + 90000) SetState(C.ISMD_DEV_STATE_PLAY) flushFlag = false } for length > 0 { var ( buffer_handle C.ismd_buffer_handle_t buffer_desc C.ismd_buffer_descriptor_t ) if ismd_ret := C.ismd_buffer_alloc(bufSize, &buffer_handle); ismd_ret != C.ISMD_SUCCESS { log.Printf("CIntelSMDVideo::WriteToInputPort ismd_buffer_alloc failed <%d>", ismd_ret) return 0 } else if ismd_ret = C.ismd_buffer_read_desc(buffer_handle, &buffer_desc); ismd_ret != C.ISMD_SUCCESS { log.Printf("CIntelSMDVideo::WriteToInputPort ismd_buffer_read_desc failed <%d>", ismd_ret) return 0 } cp := length if cp > bufSize { cp = bufSize } ptr := C.mymap(buffer_desc.phys.base, buffer_desc.phys.size) C.memcpy(ptr, unsafe.Pointer(data), cp) //ptr to data should be the data being copied to viddec C.myunmap(ptr, buffer_desc.phys.size) buffer_desc.phys.level = C.int(cp) buf_attrs := (*C.ismd_es_buf_attr_t)(unsafe.Pointer(&buffer_desc.attributes[0])) buf_attrs.original_pts = newPts buf_attrs.local_pts = newPts buf_attrs.discontinuity = false if ismd_ret := C.ismd_buffer_update_desc(buffer_handle, &buffer_desc); ismd_ret != C.ISMD_SUCCESS { log.Printf("-- ismd_buffer_update_desc failed <%d>", ismd_ret) return 0 } //the input port should be full frequently when doing file, or any other push mode var ismd_ret C.ismd_result_t for counter := 0; counter < 5; counter++ { if ismd_ret = C.ismd_port_write(m_viddec_input_port, buffer_handle); ismd_ret != C.ISMD_SUCCESS { time.Sleep(100 * time.Millisecond) } else { break } } if length > bufSize { length -= bufSize data += uintptr(bufSize) } else { length = 0 } if ismd_ret != C.ISMD_SUCCESS { log.Printf("CIntelSMDVideo::WriteToInputPort failed. %d", ismd_ret) C.ismd_buffer_dereference(buffer_handle) return 0 } } return 1 }
func main() { var ( m_audioProcessor C.ismd_audio_processor_t device C.ismd_dev_t input_port C.ismd_port_handle_t ) log.Printf("open: %x, %x", C.ismd_audio_open_global_processor(&m_audioProcessor), m_audioProcessor) defer C.ismd_audio_close_processor(m_audioProcessor) log.Printf("conf clock: %d", C.ismd_audio_configure_master_clock(m_audioProcessor, 36864000, C.ISMD_AUDIO_CLK_SRC_EXTERNAL)) log.Printf("add: %d", C.ismd_audio_add_input_port(m_audioProcessor, false, &device, &input_port)) defer C.ismd_audio_remove_input(m_audioProcessor, device) log.Printf("set data format: %d", C.ismd_audio_input_set_data_format(m_audioProcessor, device, C.ISMD_AUDIO_MEDIA_FMT_PCM)) var tmp int64 = C.AUDIO_CHAN_CONFIG_2_CH log.Printf("set format: %d", C.ismd_audio_input_set_pcm_format(m_audioProcessor, device, 16, 48000, C.int(tmp))) log.Printf("en2: %d", C.ismd_audio_input_enable(m_audioProcessor, device)) defer C.ismd_audio_input_disable(m_audioProcessor, device) for _, a := range []C.int{C.GEN3_HW_OUTPUT_HDMI /*, C.GEN3_HW_OUTPUT_SPDIF, C.GEN3_HW_OUTPUT_I2S0, C.GEN3_HW_OUTPUT_I2S1*/} { var ( output_config C.ismd_audio_output_config_t audio_output C.ismd_audio_output_t = -1 ) if a == C.GEN3_HW_OUTPUT_SPDIF { output_config.out_mode = C.ISMD_AUDIO_OUTPUT_PASSTHROUGH } else { output_config.out_mode = C.ISMD_AUDIO_OUTPUT_PCM } output_config.stream_delay = 0 output_config.sample_size = 16 output_config.ch_config = C.ISMD_AUDIO_STEREO output_config.ch_map = 0 output_config.sample_rate = 48000 log.Printf("addp: %x", C.ismd_audio_add_phys_output(m_audioProcessor, a, output_config, &audio_output)) defer C.ismd_audio_remove_output(m_audioProcessor, audio_output) log.Printf("Enable: %x", C.ismd_audio_output_enable(m_audioProcessor, audio_output)) defer C.ismd_audio_output_disable(m_audioProcessor, audio_output) } var passthrough_config C.ismd_audio_input_pass_through_config_t log.Printf("set primary: %d", C.ismd_audio_input_set_as_primary(m_audioProcessor, device, passthrough_config)) log.Printf("Play: %d", C.ismd_dev_set_state(device, C.ISMD_DEV_STATE_PLAY)) defer C.ismd_dev_set_state(device, C.ISMD_DEV_STATE_STOP) const pitch = (440.0 * 2 * math.Pi) / 48000.0 const lrpitch = 2 * math.Pi / 48000.0 var pos float64 var lrpos float64 for i := 0; i < 1000; i++ { var ( buffer C.ismd_buffer_handle_t desc C.ismd_buffer_descriptor_t ) if al := C.ismd_buffer_alloc(4096, &buffer); al != 0 { log.Println("Alloc failed: ", al) } else if d := C.ismd_buffer_read_desc(buffer, &desc); d != 0 { log.Println("read desc failed") } raw := C.mymap(desc.phys.base, 4096) ptr := (*[2048]int16)(unsafe.Pointer(raw)) for j := 0; j < 2048; j += 2 { p := 0.5 + 0.5*math.Sin(lrpos) ptr[j] = (int16)(p * 16767 * math.Sin(pos)) ptr[j+1] = (int16)((1 - p) * 16767 * math.Sin(pos)) pos += pitch lrpos += lrpitch } desc.phys.level = 4096 if u := C.ismd_buffer_update_desc(buffer, &desc); u != 0 { log.Println("Error updating desc: %d", u) } C.myunmap(raw, 4096) for counter := 0; counter < 20; counter++ { if result := C.ismd_port_write(input_port, buffer); result != 0 { time.Sleep(time.Millisecond * 100) } else { break } } // ismd_port_write takes the reference //C.ismd_buffer_dereference(buffer) } }